-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRNMailComposeModule.java
More file actions
335 lines (289 loc) · 11 KB
/
Copy pathRNMailComposeModule.java
File metadata and controls
335 lines (289 loc) · 11 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package com.reactlibrary.mailcompose;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.util.Base64;
import android.support.v4.content.FileProvider;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.BaseActivityEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class RNMailComposeModule extends ReactContextBaseJavaModule {
//Updating to RN 0.59.*
//Fixing issue: can only use lower 16 bits for requestCode on Intent
private static final int ACTIVITY_SEND = 65510;
private Promise mPromise;
private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) {
if (requestCode == ACTIVITY_SEND) {
if (mPromise != null) {
//no matter what is the action on the email apps, the resultCode will reply for 0 or equals to RESULT_CANCEL
//refer to: https://stackoverflow.com/questions/3778048/how-can-we-use-startactivityforresult-for-email-intent
//always treat it as sent after user redirected to the mailing apps
mPromise.resolve("sent");
mPromise = null;
}
}
}
};
public RNMailComposeModule(final ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addActivityEventListener(mActivityEventListener);
}
@Override
public String getName() {
return "RNMailCompose";
}
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put("name", getName());
return constants;
}
private void putExtra(Intent intent, String key, String value) {
if (value != null && !value.isEmpty()) {
intent.putExtra(key, value);
}
}
private void putExtra(Intent intent, String key, Spanned value) {
if (value != null) {
intent.putExtra(key, value);
}
}
private void putExtra(Intent intent, String key, String[] value) {
if (value != null && value.length > 0) {
intent.putExtra(key, value);
}
}
private void putExtra(Intent intent, String key, ArrayList<String> value) {
if (value != null && value.size() > 0) {
intent.putExtra(key, value);
}
}
private void addAttachments(Intent intent, ReadableArray attachments) {
if (attachments == null) return;
ArrayList<Uri> uris = new ArrayList<>();
for (int i = 0; i < attachments.size(); i++) {
if (attachments.getType(i) == ReadableType.Map) {
ReadableMap attachment = attachments.getMap(i);
if (attachment != null) {
byte[] blob = getBlob(attachment, "data");
String text = getString(attachment, "text");
String url = getString(attachment,"url");
// String mimeType = getString(attachment, "mimeType");
String filename = getString(attachment, "filename");
if (filename == null) {
filename = UUID.randomUUID().toString();
}
String ext = getString(attachment, "ext");
File tempFile = createTempFile(filename, ext);
if (blob != null) {
tempFile = writeBlob(tempFile, blob);
} else if (text != null) {
tempFile = writeText(tempFile, text);
} else if(url != null){
tempFile = new File(url);
}
if (tempFile != null) {
Uri tempFileUri = FileProvider.getUriForFile(getCurrentActivity(),this.getReactApplicationContext().getPackageName()+".provider",tempFile);
uris.add(tempFileUri);
}
}
}
}
if (uris.size() > 0) {
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
}
}
private boolean isEmpty(String str) {
return str == null || str.isEmpty();
}
private String getString(ReadableMap map, String key) {
if (map.hasKey(key) && map.getType(key) == ReadableType.String) {
return map.getString(key);
}
return null;
}
private String[] getStringArray(ReadableMap map, String key) {
ReadableArray array = getArray(map, key);
if (array == null) return null;
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
if (array.getType(i) == ReadableType.String) {
String str = array.getString(i);
if (!isEmpty(str)) {
list.add(str);
}
}
}
String[] arr = new String[list.size()];
return list.toArray(arr);
}
private ReadableArray getArray(ReadableMap map, String key) {
if (map.hasKey(key) && map.getType(key) == ReadableType.Array) {
return map.getArray(key);
}
return null;
}
private ReadableMap getMap(ReadableMap map, String key) {
if (map.hasKey(key) && map.getType(key) == ReadableType.Map) {
return map.getMap(key);
}
return null;
}
private byte[] getBlob(ReadableMap map, String key) {
if (map.hasKey(key) && map.getType(key) == ReadableType.String) {
String base64 = map.getString(key);
if (base64 != null && !base64.isEmpty()) {
return Base64.decode(base64, 0);
}
}
return null;
}
public static byte[] byteArrayFromUrl(String urlString) {
URL url;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
is = url.openStream();
byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time.
int n;
while ((n = is.read(byteChunk)) > 0) {
baos.write(byteChunk, 0, n);
}
} catch (IOException e) {
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// do nothing
}
}
}
return baos.toByteArray();
}
private byte[] getBlobFromUri(ReadableMap map, String key) {
if (map.hasKey(key) && map.getType(key) == ReadableType.String) {
String uri = map.getString(key);
if (uri != null && !uri.isEmpty()) {
return byteArrayFromUrl(uri);
}
}
return null;
}
private File createTempFile(String filename, String ext) {
if (filename != null && ext != null) {
try {
return File.createTempFile(filename, ext, getCurrentActivity().getBaseContext().getExternalCacheDir());
} catch (IOException e1) {
}
}
return null;
}
private File writeText(File file, String text) {
if (file != null && text != null) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));
bw.write(text);
bw.flush();
bw.close();
return file;
} catch (Exception e) {
if (bw != null) {
try {
bw.close();
} catch (Exception e1) {
}
}
}
}
return null;
}
private File writeBlob(File file, byte[] blob) {
if (file != null && blob != null) {
FileOutputStream fo = null;
try {
fo = new FileOutputStream(file);
fo.write(blob);
fo.flush();
fo.close();
return file;
} catch (Exception e) {
if (fo != null) {
try {
fo.close();
} catch (Exception e1) {
}
}
}
}
return null;
}
@ReactMethod
public void send(ReadableMap data, Promise promise) throws IOException {
if (mPromise != null) {
mPromise.reject("timeout", "Operation has timed out");
mPromise = null;
}
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
String text = getString(data, "body");
String html = getString(data, "html");
if (!isEmpty(html)) {
intent.setType("text/html");
putExtra(intent, Intent.EXTRA_TEXT, Html.fromHtml(html));
putExtra(intent, Intent.EXTRA_HTML_TEXT, Html.fromHtml(html));
} else {
intent.setType("text/plain");
if (!isEmpty(text)) {
putExtra(intent, Intent.EXTRA_TEXT, text);
}
}
putExtra(intent, Intent.EXTRA_SUBJECT, getString(data, "subject"));
putExtra(intent, Intent.EXTRA_EMAIL, getStringArray(data, "toRecipients"));
putExtra(intent, Intent.EXTRA_CC, getStringArray(data, "ccRecipients"));
putExtra(intent, Intent.EXTRA_BCC, getStringArray(data, "bccRecipients"));
addAttachments(intent, getArray(data, "attachments"));
intent.putExtra("exit_on_sent", true);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
getCurrentActivity().startActivityForResult(Intent.createChooser(intent, "Send Mail"), ACTIVITY_SEND);
mPromise = promise;
} catch (ActivityNotFoundException e) {
promise.reject("failed", "Activity Not Found");
} catch (Exception e) {
promise.reject("failed", "Unknown Error");
}
}
}