forked from chirag04/react-native-mail
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from adamski/revert-2-revert-1-multiple-attachm…
…ents-fix Fix multiple attachments android
- Loading branch information
Showing
1 changed file
with
42 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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]; | ||
|
@@ -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")); | ||
|
@@ -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); | ||
|
||
|
@@ -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"); | ||
} | ||
} | ||
} | ||
|