From 58d00e459a2b01fa54d19b8e2050a1110acdcd46 Mon Sep 17 00:00:00 2001 From: Abid Date: Wed, 7 Jun 2023 17:12:32 +0100 Subject: [PATCH 1/5] Fix for Android 13 with the new permissions to access files. Added conditionals to check for which OS is on the device and asks for the correct permissions accordingly --- src/android/PhotoViewer.java | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/android/PhotoViewer.java b/src/android/PhotoViewer.java index 41c333e..2b9cf93 100644 --- a/src/android/PhotoViewer.java +++ b/src/android/PhotoViewer.java @@ -22,6 +22,9 @@ public class PhotoViewer extends CordovaPlugin { public static final String WRITE = Manifest.permission.WRITE_EXTERNAL_STORAGE; public static final String READ = Manifest.permission.READ_EXTERNAL_STORAGE; + public static final String READ_IMAGES = Manifest.permission.READ_MEDIA_IMAGES; + public static final String READ_VIDEO = Manifest.permission.READ_MEDIA_VIDEO; + public static final String READ_AUDIO = Manifest.permission.READ_MEDIA_AUDIO; public static final int REQ_CODE = 0; @@ -33,11 +36,18 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo if (action.equals("show")) { this.args = args; this.callbackContext = callbackContext; - - if (cordova.hasPermission(READ) && cordova.hasPermission(WRITE)) { - this.launchActivity(); + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if (cordova.hasPermission(READ_IMAGES) && cordova.hasPermission(READ_VIDEO) && cordova.hasPermission(READ_AUDIO)) { + this.launchActivity(); + } else { + this.getPermission(); + } } else { - this.getPermission(); + if (cordova.hasPermission(READ) && cordova.hasPermission(WRITE)) { + this.launchActivity(); + } else { + this.getPermission(); + } } return true; } @@ -45,7 +55,11 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo } protected void getPermission() { - cordova.requestPermissions(this, REQ_CODE, new String[]{WRITE, READ}); + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + cordova.requestPermissions(this, REQ_CODE, new String[]{READ_IMAGES, READ_VIDEO, READ_AUDIO}); + } else { + cordova.requestPermissions(this, REQ_CODE, new String[]{WRITE, READ}); + } } // From 3d787984636dcecb935301cde8b8c464e6b2ae1f Mon Sep 17 00:00:00 2001 From: Abid Date: Wed, 7 Jun 2023 17:27:08 +0100 Subject: [PATCH 2/5] Only need image reading permissions, so I'm removing video and audio permissions update. --- src/android/PhotoViewer.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/android/PhotoViewer.java b/src/android/PhotoViewer.java index 2b9cf93..5a90b14 100644 --- a/src/android/PhotoViewer.java +++ b/src/android/PhotoViewer.java @@ -23,8 +23,6 @@ public class PhotoViewer extends CordovaPlugin { public static final String WRITE = Manifest.permission.WRITE_EXTERNAL_STORAGE; public static final String READ = Manifest.permission.READ_EXTERNAL_STORAGE; public static final String READ_IMAGES = Manifest.permission.READ_MEDIA_IMAGES; - public static final String READ_VIDEO = Manifest.permission.READ_MEDIA_VIDEO; - public static final String READ_AUDIO = Manifest.permission.READ_MEDIA_AUDIO; public static final int REQ_CODE = 0; @@ -37,7 +35,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo this.args = args; this.callbackContext = callbackContext; if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - if (cordova.hasPermission(READ_IMAGES) && cordova.hasPermission(READ_VIDEO) && cordova.hasPermission(READ_AUDIO)) { + if (cordova.hasPermission(READ_IMAGES)) { this.launchActivity(); } else { this.getPermission(); @@ -56,7 +54,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo protected void getPermission() { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - cordova.requestPermissions(this, REQ_CODE, new String[]{READ_IMAGES, READ_VIDEO, READ_AUDIO}); + cordova.requestPermissions(this, REQ_CODE, new String[]{READ_IMAGES}); } else { cordova.requestPermissions(this, REQ_CODE, new String[]{WRITE, READ}); } From 41ecf721b2a07856cd6459dda2df1a8e71169554 Mon Sep 17 00:00:00 2001 From: Abid Date: Wed, 7 Jun 2023 17:40:51 +0100 Subject: [PATCH 3/5] Updated the plugin.xml permissions to include the new READ_MEDIA_IMAGES permission with an sdk condition on it --- plugin.xml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugin.xml b/plugin.xml index d573a2f..3fb061a 100644 --- a/plugin.xml +++ b/plugin.xml @@ -19,8 +19,9 @@ - - + + + From 5ebf1e534b745a2ea14507f17210b8e14322b8f4 Mon Sep 17 00:00:00 2001 From: Abid Date: Wed, 7 Jun 2023 18:01:47 +0100 Subject: [PATCH 4/5] Added the right import to use Build to access device SDK version Tested on Tab Active 3 on Android 13 Tested on Tab Active 3 on Android 12 Both working correctly. --- src/android/PhotoViewer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/android/PhotoViewer.java b/src/android/PhotoViewer.java index 5a90b14..b34bd1d 100644 --- a/src/android/PhotoViewer.java +++ b/src/android/PhotoViewer.java @@ -3,6 +3,7 @@ import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; +import android.os.Build; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; From fcdcb8cfae8a007f7d428afd702dee12f91ce176 Mon Sep 17 00:00:00 2001 From: Abid Date: Thu, 8 Jun 2023 11:08:30 +0100 Subject: [PATCH 5/5] Removing the sdk version restrictions in the plugin.xml file. If READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE are restricted by sdk then other plugins (specifically the @capacitor/camera multiple image gallery picker) it complains that these two legacy permissions are missing on an Android 13 device. If the restrictions are removed then all the plugins work. It doesn't affect the photo viewer. --- plugin.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin.xml b/plugin.xml index 3fb061a..2d9d383 100644 --- a/plugin.xml +++ b/plugin.xml @@ -19,8 +19,8 @@ - - + +