Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added READ_CONTACTS permission to be able to access accounts #351

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is breaking the build when I insert this in config.xml, did you mean edit-config instead?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> Task :app:mergeDebugResources FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDebugResources'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > Android resource compilation failed
     /home/joao/dev/form-for-parking-violation/platforms/android/app/src/main/res/xml/config.xml:97: AAPT: error: unbound prefix.
         
     /home/joao/dev/form-for-parking-violation/platforms/android/app/src/main/res/xml/config.xml: AAPT: error: file failed to compile.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could make it work, it was missing xmlns:android="http://schemas.android.com/apk/res/android" at the widget root of config.xml

```xml
<platform name="android">
<config-file target="app/src/main/AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
</config-file>
</platform>
```

## Contributing

Expand Down
58 changes: 32 additions & 26 deletions src/android/EmailComposer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not that versed in Android Java, but it is wise to import .*?

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

@SuppressWarnings({"Convert2Diamond", "Convert2Lambda"})
Expand Down Expand Up @@ -185,41 +184,51 @@ 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));
}

/**
* Request given 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];
}
}

Expand Down Expand Up @@ -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<PluginResult> messages = new ArrayList<PluginResult>();
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));
Expand Down