-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathRNMailModule.java
127 lines (106 loc) · 3.87 KB
/
RNMailModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.chirag.RNMail;
import android.content.Intent;
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;
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 java.util.List;
import java.io.File;
/**
* NativeModule that allows JS to open emails sending apps chooser.
*/
public class RNMailModule extends ReactContextBaseJavaModule {
ReactApplicationContext reactContext;
public RNMailModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return "RNMail";
}
/**
* 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];
for (int keyIndex = 0; keyIndex < length; keyIndex++) {
strArray[keyIndex] = r.getString(keyIndex);
}
return strArray;
}
@ReactMethod
public void mail(ReadableMap options, Callback callback) {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:"));
if (options.hasKey("subject") && !options.isNull("subject")) {
i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
}
if (options.hasKey("body") && !options.isNull("body")) {
String body = options.getString("body");
if (options.hasKey("isHTML") && options.getBoolean("isHTML")) {
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
} else {
i.putExtra(Intent.EXTRA_TEXT, body);
}
}
if (options.hasKey("recipients") && !options.isNull("recipients")) {
ReadableArray recipients = options.getArray("recipients");
i.putExtra(Intent.EXTRA_EMAIL, readableArrayToStringArray(recipients));
}
if (options.hasKey("ccRecipients") && !options.isNull("ccRecipients")) {
ReadableArray ccRecipients = options.getArray("ccRecipients");
i.putExtra(Intent.EXTRA_CC, readableArrayToStringArray(ccRecipients));
}
if (options.hasKey("bccRecipients") && !options.isNull("bccRecipients")) {
ReadableArray bccRecipients = options.getArray("bccRecipients");
i.putExtra(Intent.EXTRA_BCC, readableArrayToStringArray(bccRecipients));
}
if (options.hasKey("attachment") && !options.isNull("attachment")) {
ReadableMap attachment = options.getMap("attachment");
if (attachment.hasKey("path") && !attachment.isNull("path")) {
String path = attachment.getString("path");
File file = new File(path);
Uri p = FileProvider.getUriForFile(
reactContext,
reactContext.getApplicationContext()
.getPackageName() + ".provider", file);
i.putExtra(Intent.EXTRA_STREAM, p);
}
}
PackageManager manager = reactContext.getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(i, 0);
if (list == null || list.size() == 0) {
callback.invoke("not_available");
return;
}
if (list.size() == 1) {
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
reactContext.startActivity(i);
} catch (Exception ex) {
callback.invoke("error");
}
} else {
Intent chooser = Intent.createChooser(i, "Send Mail");
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
reactContext.startActivity(chooser);
} catch (Exception ex) {
callback.invoke("error");
}
}
}
}