From 6c310ce0c29ec99323bb5e3b34436f0ed62e09bf Mon Sep 17 00:00:00 2001 From: schauerte Date: Mon, 24 Aug 2020 11:21:47 +0200 Subject: [PATCH] added READ_CONTACTS permission to be able to access accounts --- README.md | 12 ++++++- src/android/EmailComposer.java | 58 +++++++++++++++++++--------------- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 9d107c12..47acc9e8 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,7 @@ Some functions require permissions on __Android__. The plugin itself does not ad | ---------- | ----------- | | `cordova.plugins.email.permission.READ_EXTERNAL_STORAGE` | Is needed to attach external files `file:///` located outside of the app's own file system. | | `cordova.plugins.email.permission.GET_ACCOUNTS` | Without the permission the `hasAccount()` function wont be able to look for email accounts. | +| `cordova.plugins.email.permission.READ_CONTACTS` | Without the permission the `hasAccount()` function wont be able to look for email accounts. | To check if a permission has been granted: @@ -202,7 +203,16 @@ cordova.plugins.email.requestPermission(permission, callbackFn); ``` __Note:__ The author of the app has to make sure that the permission is listed in the manifest. - +For __Android__ you may add the following lines to config.xml to achieve this: +```xml + + + + + + + +``` ## Contributing diff --git a/src/android/EmailComposer.java b/src/android/EmailComposer.java index a26bf4d4..cc3e7f8a 100755 --- a/src/android/EmailComposer.java +++ b/src/android/EmailComposer.java @@ -36,8 +36,7 @@ Licensed to the Apache Software Foundation (ASF) under one import java.util.ArrayList; import java.util.List; -import static android.Manifest.permission.GET_ACCOUNTS; -import static android.Manifest.permission.READ_EXTERNAL_STORAGE; +import static android.Manifest.permission.*; import static android.content.pm.PackageManager.PERMISSION_GRANTED; @SuppressWarnings({"Convert2Diamond", "Convert2Lambda"}) @@ -185,18 +184,25 @@ public void run() { * * @param code The code number of the permission to check for. */ - private void check(int code) { - check(getPermission(code)); + private void check(int code){ + check(getPermissions(code)); } /** - * Check if the given permission has been granted. + * Check if the given permissions have been granted. * - * @param permission The permission to check for. + * @param permissions The permissions to check for. */ - private void check(String permission) { - Boolean granted = cordova.hasPermission(permission); - sendResult(new PluginResult(Status.OK, granted)); + private void check(String... permissions){ + for (int i = 0; i < permissions.length; i++) + { + if (!cordova.hasPermission(permissions[i])) + { + sendResult(new PluginResult(Status.OK, false)); + return; + } + } + sendResult(new PluginResult(Status.OK, true)); } /** @@ -204,22 +210,25 @@ private void check(String permission) { * * @param code The code number of the permission to request for. */ - private void request(int code) { - cordova.requestPermission(this, code, getPermission(code)); + private void request(int code){ + cordova.requestPermissions(this, code, getPermissions(code)); } /** - * Returns the corresponding permission for the internal code. + * Returns the corresponding permissions for the internal code. * * @param code The internal code number. - * - * @return The Android permission string or "". + * @return Array of the the Android permission strings or []. */ - private String getPermission(int code) { - switch (code) { - case 1: return READ_EXTERNAL_STORAGE; - case 2: return GET_ACCOUNTS; - default: return ""; + private String[] getPermissions(int code){ + switch (code) + { + case 1: + return new String[]{READ_EXTERNAL_STORAGE}; + case 2: + return new String[]{GET_ACCOUNTS, READ_CONTACTS}; // see https://stackoverflow.com/a/54941079/4094951 + default: + return new String[0]; } } @@ -261,14 +270,11 @@ public void onActivityResult(int reqCode, int resCode, Intent intent) { */ @Override public void onRequestPermissionResult(int code, String[] permissions, - int[] grantResults) { - + int[] grantResults){ List messages = new ArrayList(); - Boolean granted = false; - - if (grantResults.length > 0) { - granted = grantResults[0] == PERMISSION_GRANTED; - } + boolean granted = true; + for (int i = 0; i < grantResults.length; i++) + granted = granted && (grantResults[i] == PERMISSION_GRANTED); messages.add(new PluginResult(Status.OK, granted)); messages.add(new PluginResult(Status.OK, code));