Skip to content

Commit

Permalink
Merge pull request #3 from adamski/revert-2-revert-1-multiple-attachm…
Browse files Browse the repository at this point in the history
…ents-fix

Fix multiple attachments android
  • Loading branch information
adamski authored Dec 11, 2017
2 parents 8023002 + 77fa8de commit 5881937
Showing 1 changed file with 42 additions and 17 deletions.
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

0 comments on commit 5881937

Please sign in to comment.