Skip to content
Open
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ OR
<plugin spec="https://github.com/okwei2000/webintent.git" source="git" />
```

## Sending Multiple Attachments

You can now use this plugin to email multiple attachments as follows:

```js
var email_attachments = [
"file:///storage/emulated/0/Android/data/com.your_company.your_app/files/file1.pdf",
"file:///storage/emulated/0/Android/data/com.your_company.your_app/files/file2.csv"
];

var external_extras = {};
external_extras[window.plugins.webintent.EXTRA_STREAM] = email_attachments.join();
external_extras[window.plugins.webintent.EXTRA_EMAIL] = "";
external_extras[window.plugins.webintent.EXTRA_SUBJECT] = "My subject text";
external_extras[window.plugins.webintent.EXTRA_TEXT] = "My email body text";

window.plugins.webintent.startActivity({action: window.plugins.webintent.ACTION_SEND_MULTIPLE, extras: external_extras, type: "text/plain"}, function() {console.log("Success!");}, function() {console.log("Failure!");});
```

## Disclaimers
- I'm not an Android nor Cordova Plugin developer, I have little knowledge on how Android and the plugin works, especially Intent. I just made this plugin works for my own purpose. I just copy & pasted code from other plugins to make it work. If you know how to clean up the code, please let me know.

Expand Down
89 changes: 56 additions & 33 deletions src/android/WebIntent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -174,10 +175,16 @@ public void onNewIntent(Intent intent) {
}

void startActivity(String action, Uri uri, String type, Map<String, String> extras) {

if (uri != null) {
String uri_string = uri.toString();
Log.i(LOG_TAG, String.format("Start Activity : action = [%s], data = [%s]", action, uri_string));
}

//Kevin W: Android 7.0 Nougat no longer allow file to be shared by file://
//https://proandroiddev.com/sharing-files-though-intents-are-you-ready-for-nougat-70f7e9294a0b
if(newApi() && uri.getScheme().equals("file")){
try{
try{
if(newApi() && (uri != null) && uri.getScheme().equals("file")){
File fileToShare = new File(new URI(uri.toString()));
Uri contentUri = FileProvider.getUriForFile(this.cordova.getActivity(),
cordova.getActivity().getPackageName() + ".provider",
Expand All @@ -191,40 +198,56 @@ void startActivity(String action, Uri uri, String type, Map<String, String> extr

((CordovaActivity)this.cordova.getActivity()).startActivity(shareIntent);
return;
}catch(URISyntaxException e){
final String errorMessage = e.getMessage();
Log.e(LOG_TAG, errorMessage);
this.onNewIntentCallbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, errorMessage));
}
}else{
Intent i = uri != null ? new Intent(action, uri) : new Intent(action);
if (type != null && uri != null) {
i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6
} else {
if (type != null) {
i.setType(type);
}
}

for (Map.Entry<String, String> entry : extras.entrySet()) {
final String key = entry.getKey();
final String value = entry.getValue();
// If type is text/html, the extra text must be sent as HTML.
if (key.equals(Intent.EXTRA_TEXT) && "text/html".equals(type)) {
i.putExtra(key, Html.fromHtml(value));
} else if (key.equals(Intent.EXTRA_STREAM)) {
// Allows sharing of images as attachments.
// `value` in this case should be the URI of a file.
final CordovaResourceApi resourceApi = webView.getResourceApi();
i.putExtra(key, resourceApi.remapUri(Uri.parse(value)));
} else if (key.equals(Intent.EXTRA_EMAIL)) {
// Allows adding the email address of the receiver.
i.putExtra(Intent.EXTRA_EMAIL, new String[] { value });
}else{
Intent i = uri != null ? new Intent(action, uri) : new Intent(action);
if (type != null && uri != null) {
i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6
} else {
i.putExtra(key, value);
if (type != null) {
i.setType(type);
}
}

for (Map.Entry<String, String> entry : extras.entrySet()) {
final String key = entry.getKey();
final String value = entry.getValue();

// If type is text/html, the extra text must be sent as HTML.
if (key.equals(Intent.EXTRA_TEXT) && "text/html".equals(type)) {
i.putExtra(key, Html.fromHtml(value));
} else if (key.equals(Intent.EXTRA_STREAM)) {
if(action.equals(Intent.ACTION_SEND_MULTIPLE)) {
ArrayList<Uri> extraUris = new ArrayList<Uri>();
for(String extraUri : value.split(",")) {
final CordovaResourceApi resourceApi = webView.getResourceApi();
File fileToShare = new File(new URI(resourceApi.remapUri(Uri.parse(extraUri)).toString()));
Uri contentUri = FileProvider.getUriForFile(this.cordova.getActivity(),
cordova.getActivity().getPackageName() + ".provider",
fileToShare);
extraUris.add(contentUri);
}
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, extraUris);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
else {
// Allows sharing of images as attachments.
// `value` in this case should be the URI of a file.
final CordovaResourceApi resourceApi = webView.getResourceApi();
i.putExtra(key, resourceApi.remapUri(Uri.parse(value)));
}
} else if (key.equals(Intent.EXTRA_EMAIL)) {
// Allows adding the email address of the receiver.
i.putExtra(Intent.EXTRA_EMAIL, new String[] { value });
} else {
i.putExtra(key, value);
}
}
((CordovaActivity)this.cordova.getActivity()).startActivity(i);
}
((CordovaActivity)this.cordova.getActivity()).startActivity(i);
}catch(URISyntaxException e){
final String errorMessage = e.getMessage();
Log.e(LOG_TAG, errorMessage);
this.onNewIntentCallbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, errorMessage));
}
}

Expand Down
1 change: 1 addition & 0 deletions www/webintent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
};

WebIntent.prototype.ACTION_SEND = "android.intent.action.SEND";
WebIntent.prototype.ACTION_SEND_MULTIPLE = "android.intent.action.SEND_MULTIPLE";
WebIntent.prototype.ACTION_VIEW= "android.intent.action.VIEW";
WebIntent.prototype.EXTRA_TEXT = "android.intent.extra.TEXT";
WebIntent.prototype.EXTRA_SUBJECT = "android.intent.extra.SUBJECT";
Expand Down