Skip to content

Instantly share code, notes, and snippets.

@geraintwhite
Last active September 1, 2020 15:07
Show Gist options
  • Save geraintwhite/3ec60fd657af831c8a5c61f9dd78c43a to your computer and use it in GitHub Desktop.
Save geraintwhite/3ec60fd657af831c8a5c61f9dd78c43a to your computer and use it in GitHub Desktop.
diff --git a/node_modules/react-native-mail/android/src/main/java/com/chirag/RNMail/RNMailModule.java b/node_modules/react-native-mail/android/src/main/java/com/chirag/RNMail/RNMailModule.java
index bcd00c4..e2ec682 100644
--- a/node_modules/react-native-mail/android/src/main/java/com/chirag/RNMail/RNMailModule.java
+++ b/node_modules/react-native-mail/android/src/main/java/com/chirag/RNMail/RNMailModule.java
@@ -5,6 +5,7 @@ import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.text.Html;
+import android.support.v4.content.FileProvider;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
@@ -15,6 +16,7 @@ import com.facebook.react.bridge.Callback;
import java.util.List;
import java.io.File;
+import java.net.URI;
import java.util.ArrayList;
/**
@@ -54,8 +56,9 @@ public class RNMailModule extends ReactContextBaseJavaModule {
@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);
+ Intent selectorIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
+ i.setSelector(selectorIntent);
if (options.hasKey("subject") && !options.isNull("subject")) {
i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
@@ -86,25 +89,33 @@ public class RNMailModule extends ReactContextBaseJavaModule {
}
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);
- Uri u = Uri.fromFile(file);
- uris.add(u);
- }
- }
-
- if (uris.size() == 1) {
- i.putExtra(Intent.EXTRA_STREAM, uris.get(0));
- } else {
- i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
- }
- }
+ ReadableArray r = options.getArray("attachments");
+ int length = r.size();
+
+ String provider = reactContext.getApplicationContext().getPackageName() + ".provider";
+ List<ResolveInfo> resolvedIntentActivities = reactContext.getPackageManager().queryIntentActivities(i,
+ PackageManager.MATCH_DEFAULT_ONLY);
+
+ 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);
+ Uri uri = FileProvider.getUriForFile(reactContext, provider, file);
+ uris.add(uri);
+
+ for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
+ String packageName = resolvedIntentInfo.activityInfo.packageName;
+ reactContext.grantUriPermission(packageName, uri,
+ Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
+ }
+ }
+ }
+
+ i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+ i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
+ }
PackageManager manager = reactContext.getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(i, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment