Skip to content

Commit

Permalink
Push changes for 0.9.0.beta
Browse files Browse the repository at this point in the history
  • Loading branch information
katzer committed Nov 20, 2018
1 parent 796cbf5 commit a9d28cf
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 116 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@

## ChangeLog

#### Version 0.9.0 (not yet released)
- [__change__]: Plugin does not add any permissions by itself like `GET_ACCOUNTS` or `READ_EXTERNAL_STORAGE`.
- [__change__]: `isAvailable` does not request for missing permission (`GET_ACCOUNTS`).
- [__change__]: `hasPermission` takes 3 arguments now. The first one has to be a value of `cordova.plugins.email.permission`.
- [__change__]: `requestPermission` takes 3 arguments now. The first one has to be a value of `cordova.plugins.email.permission`.
- [__change__]: Remove support lib from being installed (Android).
- [__change__]: Remove deprecated namespace `plugin.email`.
- [enhancement]: Skip chooser if there's only the default app (#302)
- [bugfix:] Do not open old email draft [fixes #303]

#### Version 0.8.15 (08.02.2018)
- Fix iOS not working if `app:` wasn't specified.
- Fix `attachments:` to accept a string.
Expand Down
7 changes: 1 addition & 6 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-email-composer"
version="0.8.15">
version="0.9.0.beta">

<name>EmailComposer</name>

Expand All @@ -49,7 +49,6 @@
<!-- interface -->
<js-module src="www/email_composer.js" name="EmailComposer">
<clobbers target="cordova.plugins.email" />
<clobbers target="plugin.email" />
</js-module>

<!-- ios -->
Expand Down Expand Up @@ -98,10 +97,6 @@
</feature>
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
</config-file>

<config-file target="AndroidManifest.xml" parent="application">
<provider
android:name="de.appplant.cordova.emailcomposer.Provider"
Expand Down
10 changes: 5 additions & 5 deletions src/android/AssetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private Uri getUriForAssetPath (String path) {
AssetManager assets = ctx.getAssets();
InputStream in = assets.open(resPath);
FileOutputStream out = new FileOutputStream(file);
copyFile(in, out);
copyStream(in, out);
} catch (Exception e) {
Log.e(LOG_TAG, "File not found: " + resPath);
e.printStackTrace();
Expand Down Expand Up @@ -175,7 +175,7 @@ private Uri getUriForAppInternalPath (String path) {
try {
InputStream in = new FileInputStream(absPath);
FileOutputStream out = new FileOutputStream(file);
copyFile(in, out);
copyStream(in, out);
} catch (Exception e) {
Log.e(LOG_TAG, "File not found: " + absPath);
e.printStackTrace();
Expand Down Expand Up @@ -216,7 +216,7 @@ private Uri getUriForResourcePath (String path) {
Resources res = ctx.getResources();
InputStream in = res.openRawResource(resId);
FileOutputStream out = new FileOutputStream(file);
copyFile(in, out);
copyStream(in, out);
} catch (Exception e) {
Log.e(LOG_TAG, "File not found: " + resPath);
e.printStackTrace();
Expand Down Expand Up @@ -250,7 +250,7 @@ private Uri getUriForBase64Content (String str) {
byte[] bytes = Base64.decode(resData, 0);
InputStream in = new ByteArrayInputStream(bytes);
FileOutputStream out = new FileOutputStream(file);
copyFile(in, out);
copyStream(in, out);
} catch (Exception e) {
Log.e(LOG_TAG, "Invalid Base64 string");
e.printStackTrace();
Expand Down Expand Up @@ -285,7 +285,7 @@ private Uri getUriForFile(Context ctx, File file) {
* @param in The input stream.
* @param out The output stream.
*/
private void copyFile (InputStream in, FileOutputStream out) {
private void copyStream (InputStream in, FileOutputStream out) {
byte[] buffer = new byte[1024];
int read;

Expand Down
192 changes: 106 additions & 86 deletions src/android/EmailComposer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Licensed to the Apache Software Foundation (ASF) under one

package de.appplant.cordova.emailcomposer;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;

Expand All @@ -27,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -35,23 +37,15 @@ Licensed to the Apache Software Foundation (ASF) under one
import java.util.List;

import static android.Manifest.permission.GET_ACCOUNTS;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;

@SuppressWarnings("Convert2Diamond")
@SuppressWarnings({"Convert2Diamond", "Convert2Lambda"})
public class EmailComposer extends CordovaPlugin {

// The log tag for this plugin
static final String LOG_TAG = "EmailComposer";

// Required permissions to work properly
private static final String PERMISSION = GET_ACCOUNTS;

private JSONArray args;

// Request codes used to determine what to do after they have been
// granted or denied by the user.
private static final int EXEC_AVAIL_AFTER = 0;
private static final int EXEC_CHECK_AFTER = 1;

// The callback context used when calling back into JavaScript
private CallbackContext command;

Expand All @@ -69,44 +63,35 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) {

/**
* Executes the request.
*
* <p>
* This method is called from the WebView thread.
* To do a non-trivial amount of work, use:
* cordova.getThreadPool().execute(runnable);
*
* cordova.getThreadPool().execute(runnable);
* <p>
* To run on the UI thread, use:
* cordova.getActivity().runOnUiThread(runnable);
* cordova.getActivity().runOnUiThread(runnable);
*
* @param action The action to execute.
* @param args The exec() arguments in JSON form.
* @param callback The callback context used when calling
* back into JavaScript.
* @return Whether the action was valid.
* @return Whether the action was valid.
*/
@Override
public boolean execute (String action, JSONArray args,
CallbackContext callback) throws JSONException {
public boolean execute(String action, JSONArray args,
CallbackContext callback) throws JSONException {

this.args = args;
this.command = callback;

if ("open".equalsIgnoreCase(action)) {
if ("open".equalsIgnoreCase(action)) {
open(args.getJSONObject(0));
}
else if ("isAvailable".equalsIgnoreCase(action)) {
if (cordova.hasPermission(PERMISSION)) {
isAvailable(args.getString(0));
} else {
requestPermissions(EXEC_AVAIL_AFTER);
}
}
else if ("hasPermission".equalsIgnoreCase(action)) {
hasPermission();
}
else if ("requestPermission".equalsIgnoreCase(action)) {
requestPermissions(EXEC_CHECK_AFTER);
}
else {
} else if ("scan".equalsIgnoreCase(action)) {
scan(args.getString(0));
} else if ("check".equalsIgnoreCase(action)) {
check(args.optInt(0, 0));
} else if ("request".equalsIgnoreCase(action)) {
request(args.optInt(0, 0));
} else {
return false;
}

Expand All @@ -116,27 +101,26 @@ else if ("requestPermission".equalsIgnoreCase(action)) {
/**
* Returns the application context.
*/
private Context getContext() { return cordova.getActivity(); }
private Context getContext() {
return cordova.getActivity();
}

/**
* Tells if the device has the capability to send emails.
*
* @param id The app id.
*/
private void isAvailable (final String id) {
private void scan(final String id) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
Impl impl = new Impl(getContext());
Impl impl = new Impl(getContext());
boolean[] res = impl.canSendMail(id);
List<PluginResult> messages = new ArrayList<PluginResult>();

messages.add(new PluginResult(PluginResult.Status.OK, res[0]));
messages.add(new PluginResult(PluginResult.Status.OK, res[1]));

PluginResult result = new PluginResult(
PluginResult.Status.OK, messages);
messages.add(new PluginResult(Status.OK, res[0]));
messages.add(new PluginResult(Status.OK, res[1]));

command.sendPluginResult(result);
sendResult(new PluginResult(Status.OK, messages));
}
});
}
Expand All @@ -146,81 +130,117 @@ public void run() {
*
* @param props The email properties like subject or body
*/
private void open (JSONObject props) throws JSONException {
Impl impl = new Impl(getContext());
final Intent draft = impl.getDraft(props);
final EmailComposer plugin = this;
private void open(JSONObject props) {
final EmailComposer me = this;

cordova.getThreadPool().execute(new Runnable() {
public void run() {
cordova.startActivityForResult(plugin, draft, 0);
try {
Impl impl = new Impl(getContext());
Intent draft = impl.getDraft(props);

cordova.startActivityForResult(me, draft, 0);
} catch (ActivityNotFoundException e) {
onActivityResult(0, 0, null);
}
}
});
}

/**
* Check if the required permissions are granted.
* Check if the given permissions has been granted.
*
* @param code The code number of the permission to check for.
*/
private void hasPermission() {
Boolean hasPermission = cordova.hasPermission(PERMISSION);
private void check(int code) {
check(getPermission(code));
}

PluginResult result = new PluginResult(
PluginResult.Status.OK, hasPermission);
/**
* Check if the given permission has been granted.
*
* @param permission The permission to check for.
*/
private void check(String permission) {
Boolean granted = cordova.hasPermission(permission);
sendResult(new PluginResult(Status.OK, granted));
}

command.sendPluginResult(result);
/**
* 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));
}

/**
* Request permission to read account details.
* Returns the corresponding permission for the internal code.
*
* @param code The internal code number.
*
* @param requestCode The code to attach to the request.
* @return The Android permission string or "".
*/
@Override
public void requestPermissions (int requestCode) {
cordova.requestPermission(this, requestCode, PERMISSION);
private String getPermission(int code) {
switch (code) {
case 1: return READ_EXTERNAL_STORAGE;
case 2: return GET_ACCOUNTS;
default: return "";
}
}

/**
* Send plugin result and reset plugin state.
*
* @param result The result to send to the webview.
*/
private void sendResult(PluginResult result) {
if (command != null) {
command.sendPluginResult(result);
}

command = null;
}

/**
* Called when an activity you launched exits, giving you the reqCode you
* started it with, the resCode it returned, and any additional data from it.
*
* @param reqCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resCode The integer result code returned by the child activity
* through its setResult().
* @param intent An Intent, which can return result data to the caller
* (various data can be attached to Intent "extras").
* @param reqCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resCode The integer result code returned by the child activity
* through its setResult().
* @param intent An Intent, which can return result data to the caller
* (various data can be attached to Intent "extras").
*/
@Override
public void onActivityResult (int reqCode, int resCode, Intent intent) {
if (command != null) {
command.success();
}
public void onActivityResult(int reqCode, int resCode, Intent intent) {
sendResult(new PluginResult(Status.OK));
}

/**
* Called by the system when the user grants permissions.
*
* @param code The requested code.
* @param permissions The requested permissions.
* @param code The requested code.
* @param permissions The requested permissions.
* @param grantResults The grant result for the requested permissions.
*/
@Override
public void onRequestPermissionResult (int code, String[] permissions,
int[] grantResults) {
try {
switch (code) {
case EXEC_CHECK_AFTER:
hasPermission();
break;

case EXEC_AVAIL_AFTER:
isAvailable(this.args.getString(0));
break;
}
} catch (JSONException e) {
e.printStackTrace();
public void onRequestPermissionResult(int code, String[] permissions,
int[] grantResults) {

List<PluginResult> messages = new ArrayList<PluginResult>();
Boolean granted = false;

if (grantResults.length > 0) {
granted = grantResults[0] == PERMISSION_GRANTED;
}

messages.add(new PluginResult(Status.OK, granted));
messages.add(new PluginResult(Status.OK, code));

sendResult(new PluginResult(Status.OK, messages));
}

}
Loading

0 comments on commit a9d28cf

Please sign in to comment.