Skip to content
Merged
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
15 changes: 13 additions & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chirag.RNMail">
package="com.chirag.RNMail">

</manifest>
<application>
<provider
android:name=".RNMailFileProvider"
android:authorities="${applicationId}.rnmail.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.chirag.RNMail;

import androidx.core.content.FileProvider;

public class RNMailFileProvider extends FileProvider {

}
59 changes: 35 additions & 24 deletions android/src/main/java/com/chirag/RNMail/RNMailModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.text.Html;
import androidx.core.content.FileProvider;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand All @@ -15,6 +16,7 @@

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

/**
Expand Down Expand Up @@ -54,8 +56,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);
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"));
Expand Down Expand Up @@ -86,25 +89,33 @@ public void mail(ReadableMap options, Callback callback) {
}

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() + ".rnmail.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);
Expand All @@ -123,11 +134,11 @@ public void mail(ReadableMap options, Callback callback) {
}
} else {
String chooserTitle = "Send Mail";

if (options.hasKey("customChooserTitle") && !options.isNull("customChooserTitle")) {
chooserTitle = options.getString("customChooserTitle");
}
}

Intent chooser = Intent.createChooser(i, chooserTitle);
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Expand Down
6 changes: 6 additions & 0 deletions android/src/main/res/xml/provider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="rnmail_dl" path="Download/" />
<cache-path name="rnmail_cache" path="/" />
<root-path name="rnmail_sdcard" path="." />
</paths>