From 08524857c025ac1def8b3b9880a1f22b760ac37a Mon Sep 17 00:00:00 2001 From: Carl Poole Date: Wed, 18 Nov 2020 20:22:38 -0600 Subject: [PATCH 1/3] Modified annotation format for permissions, allow empty permissions --- .../main/java/com/getcapacitor/Plugin.java | 101 ++++++++++++------ .../getcapacitor/annotation/Permission.java | 8 +- 2 files changed, 75 insertions(+), 34 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java index 793489885..32e410997 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java @@ -13,6 +13,7 @@ import com.getcapacitor.annotation.Permission; import com.getcapacitor.util.PermissionHelper; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -188,8 +189,10 @@ public boolean hasDefinedPermissions(String[] permissions) { */ public boolean hasDefinedPermissions(Permission[] permissions) { for (Permission perm : permissions) { - if (!PermissionHelper.hasDefinedPermission(getContext(), perm.permission())) { - return false; + for (String permString : perm.permission()) { + if (!PermissionHelper.hasDefinedPermission(getContext(), permString)) { + return false; + } } } return true; @@ -241,8 +244,10 @@ public boolean hasRequiredPermissions() { } for (Permission perm : annotation.permissions()) { - if (!hasPermission(perm.permission())) { - return false; + for (String permString : perm.permission()) { + if (!hasPermission(permString)) { + return false; + } } } @@ -272,24 +277,38 @@ public JSObject getPermissionStates() { JSObject permissionsResults = new JSObject(); CapacitorPlugin annotation = handle.getPluginAnnotation(); for (Permission perm : annotation.permissions()) { - String key = perm.alias().isEmpty() ? perm.permission() : perm.alias(); - String permissionStatus = hasPermission(perm.permission()) ? "granted" : "prompt"; - - // Check if there is a cached permission state for the "Never ask again" state - if (permissionStatus.equals("prompt")) { - SharedPreferences prefs = getContext().getSharedPreferences(PERMISSION_PREFS, Activity.MODE_PRIVATE); - String state = prefs.getString(perm.permission(), null); - - if (state != null) { - permissionStatus = state; + if (perm.permission().length == 0 || (perm.permission().length == 1 && perm.permission()[0].isEmpty())) { + String key = perm.alias(); + if (!key.isEmpty()) { + String existingResult = permissionsResults.getString(key); + + // auto set permission state to granted if the alias is empty. + if (existingResult == null) { + permissionsResults.put(key, "granted"); + } } - } + } else { + for (String permString : perm.permission()) { + String key = perm.alias().isEmpty() ? permString : perm.alias(); + String permissionStatus = hasPermission(permString) ? "granted" : "prompt"; + + // Check if there is a cached permission state for the "Never ask again" state + if (permissionStatus.equals("prompt")) { + SharedPreferences prefs = getContext().getSharedPreferences(PERMISSION_PREFS, Activity.MODE_PRIVATE); + String state = prefs.getString(permString, null); + + if (state != null) { + permissionStatus = state; + } + } - String existingResult = permissionsResults.getString(key); + String existingResult = permissionsResults.getString(key); - // multiple permissions with the same alias must all be true, otherwise all false. - if (existingResult == null || existingResult.equals("granted")) { - permissionsResults.put(key, permissionStatus); + // multiple permissions with the same alias must all be true, otherwise all false. + if (existingResult == null || existingResult.equals("granted")) { + permissionsResults.put(key, permissionStatus); + } + } } } @@ -316,12 +335,12 @@ public void pluginRequestAllPermissions() { return; } - String[] perms = new String[annotation.permissions().length]; - for (int i = 0; i < perms.length; i++) { - perms[i] = annotation.permissions()[i].permission(); + HashSet perms = new HashSet<>(); + for (Permission perm : annotation.permissions()) { + perms.addAll(Arrays.asList(perm.permission())); } - ActivityCompat.requestPermissions(getActivity(), perms, annotation.permissionRequestCode()); + ActivityCompat.requestPermissions(getActivity(), perms.toArray(new String[0]), annotation.permissionRequestCode()); } /** @@ -470,6 +489,7 @@ public void removeAllListeners(PluginCall call) { @PluginMethod public void requestPermissions(PluginCall call) { String[] perms = null; + Set autoGrantPerms = new HashSet<>(); int permissionRequestCode; // If call was made with a list of permissions to request, save them to be requested @@ -489,15 +509,25 @@ public void requestPermissions(PluginCall call) { } else { // If call was made without any custom permissions, request all from plugin annotation if (providedPermsList == null || providedPermsList.isEmpty()) { - perms = new String[annotation.permissions().length]; - for (int i = 0; i < perms.length; i++) { - perms[i] = annotation.permissions()[i].permission(); + HashSet permsSet = new HashSet<>(); + for (Permission perm : annotation.permissions()) { + if (perm.permission().length == 0 || (perm.permission().length == 1 && perm.permission()[0].isEmpty())) { + if (!perm.alias().isEmpty()) { + autoGrantPerms.add(perm.alias()); + } + } else { + permsSet.addAll(Arrays.asList(perm.permission())); + } } + + perms = permsSet.toArray(new String[0]); } else { Set permsSet = new HashSet<>(); for (Permission perm : annotation.permissions()) { - if (providedPermsList.contains(perm.alias()) || providedPermsList.contains(perm.permission())) { - permsSet.add(perm.permission()); + for (String permString : perm.permission()) { + if (providedPermsList.contains(perm.alias()) || providedPermsList.contains(permString)) { + permsSet.add(permString); + } } } @@ -517,7 +547,18 @@ public void requestPermissions(PluginCall call) { pluginRequestPermissions(perms, permissionRequestCode); } else { - call.resolve(); + // if the plugin only has auto-grant permissions, return those + if (!autoGrantPerms.isEmpty()) { + JSObject permissionsResults = new JSObject(); + + for (String perm : autoGrantPerms) { + permissionsResults.put(perm, "granted"); + } + + call.resolve(permissionsResults); + } else { + call.resolve(); + } } } @@ -578,7 +619,7 @@ protected void onRequestPermissionsResult(int requestCode, String[] permissions, * @param grantResults * @return true if permissions were saved and defined correctly, false if not */ - public boolean validatePermissions(String[] permissions, int[] grantResults) { + protected boolean validatePermissions(String[] permissions, int[] grantResults) { SharedPreferences prefs = getContext().getSharedPreferences(PERMISSION_PREFS, Activity.MODE_PRIVATE); if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { diff --git a/android/capacitor/src/main/java/com/getcapacitor/annotation/Permission.java b/android/capacitor/src/main/java/com/getcapacitor/annotation/Permission.java index d87e18c32..9c5348b17 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/annotation/Permission.java +++ b/android/capacitor/src/main/java/com/getcapacitor/annotation/Permission.java @@ -9,11 +9,11 @@ @Retention(RetentionPolicy.RUNTIME) public @interface Permission { /** - * The Android string for the permission. - * Eg: Manifest.permission.ACCESS_COARSE_LOCATION - * or "android.permission.ACCESS_COARSE_LOCATION" + * An array of Android permission strings. + * Eg: {Manifest.permission.ACCESS_COARSE_LOCATION} + * or {"android.permission.ACCESS_COARSE_LOCATION"} */ - String permission() default ""; + String[] permission() default {}; /** * An optional name to use instead of the Android permission string. From e6d97558fb7dd5b92b94825c1f787bb2fa95e2a4 Mon Sep 17 00:00:00 2001 From: Carl Poole Date: Thu, 19 Nov 2020 00:42:52 -0600 Subject: [PATCH 2/3] added comment to interesting if statement --- android/capacitor/src/main/java/com/getcapacitor/Plugin.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java index 32e410997..6ca5e5eab 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java @@ -277,6 +277,8 @@ public JSObject getPermissionStates() { JSObject permissionsResults = new JSObject(); CapacitorPlugin annotation = handle.getPluginAnnotation(); for (Permission perm : annotation.permissions()) { + // If a permission is defined with no permission constants, return "granted" for it. + // Otherwise, get its true state. if (perm.permission().length == 0 || (perm.permission().length == 1 && perm.permission()[0].isEmpty())) { String key = perm.alias(); if (!key.isEmpty()) { @@ -511,6 +513,8 @@ public void requestPermissions(PluginCall call) { if (providedPermsList == null || providedPermsList.isEmpty()) { HashSet permsSet = new HashSet<>(); for (Permission perm : annotation.permissions()) { + // If a permission is defined with no permission constants, separate it for auto-granting. + // Otherwise, it is added to the list to be requested. if (perm.permission().length == 0 || (perm.permission().length == 1 && perm.permission()[0].isEmpty())) { if (!perm.alias().isEmpty()) { autoGrantPerms.add(perm.alias()); From 9432b175d709285f1d04146d0c18637cc4a76b10 Mon Sep 17 00:00:00 2001 From: Thomas Vidas Date: Thu, 19 Nov 2020 15:35:53 -0500 Subject: [PATCH 3/3] Rename permission to strings --- .../src/main/java/com/getcapacitor/Plugin.java | 16 ++++++++-------- .../com/getcapacitor/annotation/Permission.java | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java index 6ca5e5eab..6d5b1ee3e 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java @@ -189,7 +189,7 @@ public boolean hasDefinedPermissions(String[] permissions) { */ public boolean hasDefinedPermissions(Permission[] permissions) { for (Permission perm : permissions) { - for (String permString : perm.permission()) { + for (String permString : perm.strings()) { if (!PermissionHelper.hasDefinedPermission(getContext(), permString)) { return false; } @@ -244,7 +244,7 @@ public boolean hasRequiredPermissions() { } for (Permission perm : annotation.permissions()) { - for (String permString : perm.permission()) { + for (String permString : perm.strings()) { if (!hasPermission(permString)) { return false; } @@ -279,7 +279,7 @@ public JSObject getPermissionStates() { for (Permission perm : annotation.permissions()) { // If a permission is defined with no permission constants, return "granted" for it. // Otherwise, get its true state. - if (perm.permission().length == 0 || (perm.permission().length == 1 && perm.permission()[0].isEmpty())) { + if (perm.strings().length == 0 || (perm.strings().length == 1 && perm.strings()[0].isEmpty())) { String key = perm.alias(); if (!key.isEmpty()) { String existingResult = permissionsResults.getString(key); @@ -290,7 +290,7 @@ public JSObject getPermissionStates() { } } } else { - for (String permString : perm.permission()) { + for (String permString : perm.strings()) { String key = perm.alias().isEmpty() ? permString : perm.alias(); String permissionStatus = hasPermission(permString) ? "granted" : "prompt"; @@ -339,7 +339,7 @@ public void pluginRequestAllPermissions() { HashSet perms = new HashSet<>(); for (Permission perm : annotation.permissions()) { - perms.addAll(Arrays.asList(perm.permission())); + perms.addAll(Arrays.asList(perm.strings())); } ActivityCompat.requestPermissions(getActivity(), perms.toArray(new String[0]), annotation.permissionRequestCode()); @@ -515,12 +515,12 @@ public void requestPermissions(PluginCall call) { for (Permission perm : annotation.permissions()) { // If a permission is defined with no permission constants, separate it for auto-granting. // Otherwise, it is added to the list to be requested. - if (perm.permission().length == 0 || (perm.permission().length == 1 && perm.permission()[0].isEmpty())) { + if (perm.strings().length == 0 || (perm.strings().length == 1 && perm.strings()[0].isEmpty())) { if (!perm.alias().isEmpty()) { autoGrantPerms.add(perm.alias()); } } else { - permsSet.addAll(Arrays.asList(perm.permission())); + permsSet.addAll(Arrays.asList(perm.strings())); } } @@ -528,7 +528,7 @@ public void requestPermissions(PluginCall call) { } else { Set permsSet = new HashSet<>(); for (Permission perm : annotation.permissions()) { - for (String permString : perm.permission()) { + for (String permString : perm.strings()) { if (providedPermsList.contains(perm.alias()) || providedPermsList.contains(permString)) { permsSet.add(permString); } diff --git a/android/capacitor/src/main/java/com/getcapacitor/annotation/Permission.java b/android/capacitor/src/main/java/com/getcapacitor/annotation/Permission.java index 9c5348b17..351143708 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/annotation/Permission.java +++ b/android/capacitor/src/main/java/com/getcapacitor/annotation/Permission.java @@ -13,7 +13,7 @@ * Eg: {Manifest.permission.ACCESS_COARSE_LOCATION} * or {"android.permission.ACCESS_COARSE_LOCATION"} */ - String[] permission() default {}; + String[] strings() default {}; /** * An optional name to use instead of the Android permission string.