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

Revert "Revert "Fix multimple attachments android"" #3

Merged
merged 8 commits into from
Dec 11, 2017
59 changes: 42 additions & 17 deletions android/src/main/java/com/chirag/RNMail/RNMailModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import android.net.Uri;
import android.text.Html;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReadableMap;

import java.util.List;
import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
* NativeModule that allows JS to open emails sending apps chooser.
Expand All @@ -34,12 +36,11 @@ public String getName() {
}

/**
* Converts a ReadableArray to a String array
*
* @param r the ReadableArray instance to convert
*
* @return array of strings
*/
* Converts a ReadableArray to a String array
*
* @param r the ReadableArray instance to convert
* @return array of strings
*/
private String[] readableArrayToStringArray(ReadableArray r) {
int length = r.size();
String[] strArray = new String[length];
Expand All @@ -53,8 +54,9 @@ private String[] readableArrayToStringArray(ReadableArray r) {

@ReactMethod
public void mail(ReadableMap options, Callback callback) {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:"));
Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("message/rfc822");


if (options.hasKey("subject") && !options.isNull("subject")) {
i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
Expand Down Expand Up @@ -99,6 +101,29 @@ public void mail(ReadableMap options, Callback callback) {
i.putExtra(Intent.EXTRA_BCC, readableArrayToStringArray(bccRecipients));
}

if (options.hasKey("attachments") && !options.isNull("attachments")) {
ReadableArray r = options.getArray("attachments");
int length = r.size();
ArrayList<Uri> uris = new ArrayList<Uri>();
for (int keyIndex = 0; keyIndex < length; keyIndex++) {
ReadableMap clip = r.getMap(keyIndex);
if (clip.hasKey("path") && !clip.isNull("path")) {
String path = clip.getString("path");
File file = new File(path);
if (file.exists()) {
uris.add(Uri.fromFile(file));
}
}
}
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
}

i.setType(null); // If we're using a selector, then clear the type to null. I don't know why this is needed, but it doesn't work without it.
final Intent restrictIntent = new Intent(Intent.ACTION_SENDTO);
Uri data = Uri.parse("mailto:[email protected]");
restrictIntent.setData(data);
i.setSelector(restrictIntent);

PackageManager manager = reactContext.getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(i, 0);

Expand All @@ -115,13 +140,13 @@ public void mail(ReadableMap options, Callback callback) {
callback.invoke("error");
}
} else {
Intent chooser = Intent.createChooser(i, "Send Mail");
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooser = Intent.createChooser(i, "Send Mail");
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
reactContext.startActivity(chooser);
} catch (Exception ex) {
}

try {
reactContext.startActivity(chooser);
} catch (Exception ex) {
callback.invoke("error");
}
}
}
Expand Down