From 2def1c1d5a075710bfe57b916b13a3591231c5ac Mon Sep 17 00:00:00 2001 From: Lethe <37437842+wn-na@users.noreply.github.com> Date: Sat, 31 Aug 2024 22:41:13 +0900 Subject: [PATCH] feat: separate request permission --- .../CaptureProtectionModule.java | 46 +++++++++++++------ package.json | 2 +- src/index.tsx | 28 +++++++---- 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/android/src/main/java/com/captureprotection/CaptureProtectionModule.java b/android/src/main/java/com/captureprotection/CaptureProtectionModule.java index 32fc49a..6c294e2 100644 --- a/android/src/main/java/com/captureprotection/CaptureProtectionModule.java +++ b/android/src/main/java/com/captureprotection/CaptureProtectionModule.java @@ -43,6 +43,10 @@ public class CaptureProtectionModule extends ReactContextBaseJavaModule implemen private final DisplayManager displayManager; private final DisplayManager.DisplayListener displayListener; + private final String requestPermission = Build.VERSION.SDK_INT >= 33 // Build.VERSION_CODES.TIRAMISU + ? "android.permission.READ_MEDIA_IMAGES" // Manifest.permission.READ_MEDIA_IMAGES + : Manifest.permission.READ_EXTERNAL_STORAGE; + private List screens = new ArrayList<>(); private ContentObserver contentObserver = null; @@ -202,9 +206,8 @@ public void onHostDestroy() { } } - private boolean requestStoragePermission() { + private boolean checkStoragePermission() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { - Log.d(NAME, "Permission is granted for under sdk version 23"); return true; } @@ -213,23 +216,32 @@ private boolean requestStoragePermission() { } try { - String requestPermission = Build.VERSION.SDK_INT >= 33 // Build.VERSION_CODES.TIRAMISU - ? "android.permission.READ_MEDIA_IMAGES" // Manifest.permission.READ_MEDIA_IMAGES - : Manifest.permission.READ_EXTERNAL_STORAGE; - if (getReactCurrentActivity() == null) { return false; } - if (ContextCompat.checkSelfPermission(getReactCurrentActivity(), - requestPermission) == PackageManager.PERMISSION_GRANTED) { + return (ContextCompat.checkSelfPermission(getReactCurrentActivity(), + requestPermission) == PackageManager.PERMISSION_GRANTED); + } catch (Exception e) { + Log.e(NAME, "checkStoragePermission has raise Exception: " + e.getLocalizedMessage()); + return false; + } + } + + private boolean requestStoragePermission() { + try { + boolean isGranted = checkStoragePermission(); + if (getReactCurrentActivity() == null) { + return false; + } + if (isGranted) { Log.d(NAME, "Permission is granted"); return true; - } else { - Log.d(NAME, "Permission is revoked"); - ActivityCompat.requestPermissions(getReactCurrentActivity(), new String[] { requestPermission }, 1); - return false; } + + Log.d(NAME, "Permission is revoked"); + ActivityCompat.requestPermissions(getReactCurrentActivity(), new String[] { requestPermission }, 1); + return false; } catch (Exception e) { Log.e(NAME, "requestStoragePermission has raise Exception: " + e.getLocalizedMessage()); return false; @@ -238,8 +250,7 @@ private boolean requestStoragePermission() { private void addListener() { if (getScreenCaptureCallback() == null) { - if (contentObserver == null) { - requestStoragePermission(); + if (contentObserver == null && checkStoragePermission()) { contentObserver = new ContentObserver(Utils.MainHandler.INSTANCE) { @Override public void onChange(boolean selfChange, Uri uri) { @@ -424,4 +435,11 @@ public void requestPermission(Promise promise) { promise.resolve(isPermission); return; } + + @ReactMethod + public void checkPermission(Promise promise) { + boolean isPermission = checkStoragePermission(); + promise.resolve(isPermission); + return; + } } diff --git a/package.json b/package.json index a722fd0..180e61c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-capture-protection", - "version": "1.9.11", + "version": "1.9.12", "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", diff --git a/src/index.tsx b/src/index.tsx index 29a0f04..aab4157 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,3 +1,10 @@ +import React, { + createContext, + useContext, + useEffect, + useRef, + useState, +} from 'react'; import { Image, NativeEventEmitter, @@ -9,13 +16,6 @@ import { CaptureEventStatus, CaptureProtectionModuleStatus, } from './type'; -import React, { - createContext, - useContext, - useEffect, - useRef, - useState, -} from 'react'; const LINKING_ERROR = `The package 'react-native-capture-protection' doesn't seem to be linked. Make sure: \n\n` + @@ -116,6 +116,7 @@ const allowScreenRecord = async (removeListener = false): Promise => { */ const preventScreenRecord = async (isImmediate = false): Promise => { if (Platform.OS === 'android') { + await requestPermission(); return await CaptureProtectionModule?.preventScreenshot?.(); } if (Platform.OS === 'ios') { @@ -146,6 +147,7 @@ const allowScreenshot = async (removeListener = false): Promise => { */ const preventScreenshot = async (): Promise => { if (Platform.OS === 'android') { + await requestPermission(); return await CaptureProtectionModule?.preventScreenshot?.(); } if (Platform.OS === 'ios') { @@ -265,7 +267,15 @@ const isScreenRecording = async (): Promise => { */ const requestPermission = async (): Promise => { if (Platform.OS === 'android') { - return await CaptureProtectionModule?.requestPermission?.(); + try { + return await CaptureProtectionModule?.requestPermission?.(); + } catch (e) { + console.error( + '[React-native-capture-protection] requestPermission throw error', + e + ); + return false; + } } else { console.error( '[React-native-capture-protection] requestPermission is only available on Android' @@ -450,4 +460,4 @@ export const CaptureProtection = { preventBackground, }; -export { CaptureProtectionModuleStatus, CaptureEventStatus } from './type'; +export { CaptureEventStatus, CaptureProtectionModuleStatus } from './type';