From 67b66bf010a1ae02685952a7b471008fa8b2f037 Mon Sep 17 00:00:00 2001 From: Lethe <37437842+wn-na@users.noreply.github.com> Date: Sun, 28 Jul 2024 14:19:57 +0900 Subject: [PATCH] release: 1.9.11 --- .../CaptureProtectionModule.java | 32 ++++++++++++++++--- package.json | 2 +- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/captureprotection/CaptureProtectionModule.java b/android/src/main/java/com/captureprotection/CaptureProtectionModule.java index edcce43..32fc49a 100644 --- a/android/src/main/java/com/captureprotection/CaptureProtectionModule.java +++ b/android/src/main/java/com/captureprotection/CaptureProtectionModule.java @@ -49,6 +49,10 @@ public class CaptureProtectionModule extends ReactContextBaseJavaModule implemen // Activity.ScreenCaptureCallback is Add API level 34 public static Object screenCaptureCallback = null; + private Activity getReactCurrentActivity() { + return reactContext.getCurrentActivity(); + } + private Method getScreenCaptureCallback() { if (Build.VERSION.SDK_INT < 34) { return null; @@ -213,13 +217,17 @@ private boolean requestStoragePermission() { ? "android.permission.READ_MEDIA_IMAGES" // Manifest.permission.READ_MEDIA_IMAGES : Manifest.permission.READ_EXTERNAL_STORAGE; - if (ContextCompat.checkSelfPermission(getCurrentActivity(), + if (getReactCurrentActivity() == null) { + return false; + } + + if (ContextCompat.checkSelfPermission(getReactCurrentActivity(), requestPermission) == PackageManager.PERMISSION_GRANTED) { Log.d(NAME, "Permission is granted"); return true; } else { Log.d(NAME, "Permission is revoked"); - ActivityCompat.requestPermissions(getCurrentActivity(), new String[] { requestPermission }, 1); + ActivityCompat.requestPermissions(getReactCurrentActivity(), new String[] { requestPermission }, 1); return false; } } catch (Exception e) { @@ -280,7 +288,11 @@ private void removeListener() { } private boolean isSecureFlag() { - return (getCurrentActivity().getWindow().getAttributes().flags + Activity currentActivity = getReactCurrentActivity(); + if (currentActivity == null) { + return false; + } + return (currentActivity.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_SECURE) != 0; } @@ -353,7 +365,12 @@ public void isScreenRecording(Promise promise) { public void preventScreenshot(Promise promise) { runOnUiThread(() -> { try { - getCurrentActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); + Activity currentActivity = getReactCurrentActivity(); + if (currentActivity == null) { + Log.w(NAME, "preventScreenshot: Current Activity is null"); + return; + } + currentActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); sendEvent(CaptureProtectionConstant.LISTENER_EVENT_NAME, true, true, CaptureProtectionConstant.CaptureProtectionModuleStatus.UNKNOWN.ordinal()); @@ -369,7 +386,12 @@ public void preventScreenshot(Promise promise) { public void allowScreenshot(Boolean removeListener, Promise promise) { runOnUiThread(() -> { try { - getCurrentActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE); + Activity currentActivity = getReactCurrentActivity(); + if (currentActivity == null) { + Log.w(NAME, "allowScreenshot: Current Activity is null"); + return; + } + currentActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE); sendEvent(CaptureProtectionConstant.LISTENER_EVENT_NAME, false, false, CaptureProtectionConstant.CaptureProtectionModuleStatus.UNKNOWN.ordinal()); diff --git a/package.json b/package.json index ef5b3e1..a722fd0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-capture-protection", - "version": "1.9.10", + "version": "1.9.11", "description": "It’s a library for React Native to control simple capture events(i.e. Screenshot or Screen record)", "main": "lib/commonjs/index", "module": "lib/module/index",