From 5b983a6d8c3ade113b070e7fedbac5960e807001 Mon Sep 17 00:00:00 2001 From: Lethe <37437842+0xlethe@users.noreply.github.com> Date: Wed, 22 May 2024 11:55:00 +0900 Subject: [PATCH] feat: ContextCompat.checkSelfPermission run with try-catch --- .../CaptureProtectionModule.java | 27 +++++++++++-------- package.json | 2 +- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/android/src/main/java/com/captureprotection/CaptureProtectionModule.java b/android/src/main/java/com/captureprotection/CaptureProtectionModule.java index 4089492..edcce43 100644 --- a/android/src/main/java/com/captureprotection/CaptureProtectionModule.java +++ b/android/src/main/java/com/captureprotection/CaptureProtectionModule.java @@ -208,17 +208,22 @@ private boolean requestStoragePermission() { return true; } - 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 (ContextCompat.checkSelfPermission(getCurrentActivity(), - 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); + 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 (ContextCompat.checkSelfPermission(getCurrentActivity(), + 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); + return false; + } + } catch (Exception e) { + Log.e(NAME, "requestStoragePermission has raise Exception: " + e.getLocalizedMessage()); return false; } } diff --git a/package.json b/package.json index 02de675..df5e1d3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-capture-protection", - "version": "1.9.6", + "version": "1.9.7", "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",