From e3eec9959c79ca4a4bcda0cb3ad818c1e5919571 Mon Sep 17 00:00:00 2001 From: Alexis THOMAS Date: Sun, 24 Dec 2023 19:54:51 +0100 Subject: [PATCH 1/2] feat(android): use already imported Build class --- src/android/FileUtils.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java index 0edf90a0..78107a57 100644 --- a/src/android/FileUtils.java +++ b/src/android/FileUtils.java @@ -534,7 +534,7 @@ public void run(JSONArray args) throws FileNotFoundException, JSONException, Mal private void getReadPermission(String rawArgs, int action, CallbackContext callbackContext) { int requestCode = pendingRequests.createRequest(rawArgs, action, callbackContext); - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { PermissionHelper.requestPermissions(this, requestCode, new String[]{Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_MEDIA_VIDEO, Manifest.permission.READ_MEDIA_AUDIO}); } else { @@ -543,7 +543,7 @@ private void getReadPermission(String rawArgs, int action, CallbackContext callb } private void getWritePermission(String rawArgs, int action, CallbackContext callbackContext) { - if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { int requestCode = pendingRequests.createRequest(rawArgs, action, callbackContext); PermissionHelper.requestPermission(this, requestCode, Manifest.permission.WRITE_EXTERNAL_STORAGE); } @@ -559,7 +559,7 @@ private void getWritePermission(String rawArgs, int action, CallbackContext call * @return */ private boolean hasReadPermission() { - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { return PermissionHelper.hasPermission(this, Manifest.permission.READ_MEDIA_IMAGES) && PermissionHelper.hasPermission(this, Manifest.permission.READ_MEDIA_VIDEO) && PermissionHelper.hasPermission(this, Manifest.permission.READ_MEDIA_AUDIO); @@ -570,7 +570,7 @@ private boolean hasReadPermission() { private boolean hasWritePermission() { // Starting with API 33, requesting WRITE_EXTERNAL_STORAGE is an auto permission rejection - return android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU ? true : PermissionHelper.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); } From f8f2d187d7c14c767e524505fdadff018d29093c Mon Sep 17 00:00:00 2001 From: Alexis THOMAS Date: Tue, 26 Dec 2023 18:57:40 +0100 Subject: [PATCH 2/2] fix(android): restore getWritePermission, but check can have permission --- src/android/FileUtils.java | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java index 78107a57..9078dbb6 100644 --- a/src/android/FileUtils.java +++ b/src/android/FileUtils.java @@ -543,10 +543,8 @@ private void getReadPermission(String rawArgs, int action, CallbackContext callb } private void getWritePermission(String rawArgs, int action, CallbackContext callbackContext) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { - int requestCode = pendingRequests.createRequest(rawArgs, action, callbackContext); - PermissionHelper.requestPermission(this, requestCode, Manifest.permission.WRITE_EXTERNAL_STORAGE); - } + int requestCode = pendingRequests.createRequest(rawArgs, action, callbackContext); + PermissionHelper.requestPermission(this, requestCode, Manifest.permission.WRITE_EXTERNAL_STORAGE); } /** @@ -568,11 +566,21 @@ private boolean hasReadPermission() { } } + private boolean canHaveReadPermission() { + return true; + } + + /** + * Starting with API 33, requesting WRITE_EXTERNAL_STORAGE is an auto permission rejection. + * + * @return + */ private boolean hasWritePermission() { - // Starting with API 33, requesting WRITE_EXTERNAL_STORAGE is an auto permission rejection - return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU - ? true - : PermissionHelper.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); + return PermissionHelper.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); + } + + private boolean canHaveWritePermission() { + return Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU; } private boolean needPermission(String nativeURL, int permissionType) throws JSONException { @@ -584,9 +592,9 @@ private boolean needPermission(String nativeURL, int permissionType) throws JSON allowedStorageDirectories.add(j.getString("externalApplicationStorageDirectory")); } - if (permissionType == READ && hasReadPermission()) { + if (permissionType == READ && (!canHaveReadPermission() || hasReadPermission())) { return false; - } else if (permissionType == WRITE && hasWritePermission()) { + } else if (permissionType == WRITE && (!canHaveWritePermission() || hasWritePermission())) { return false; }