-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathSendSMSModule.java
More file actions
106 lines (87 loc) · 3.78 KB
/
SendSMSModule.java
File metadata and controls
106 lines (87 loc) · 3.78 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
package com.tkporter.sendsms;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.provider.Telephony;
import com.facebook.react.bridge.ActivityEventListener;
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;
public class SendSMSModule extends ReactContextBaseJavaModule implements ActivityEventListener {
private final ReactApplicationContext reactContext;
private Callback callback = null;
private static final int REQUEST_CODE = 5235;
public SendSMSModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
reactContext.addActivityEventListener(this);
}
@Override
public String getName() {
return "SendSMS";
}
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
//System.out.println("in module onActivityResult() request " + requestCode + " result " + resultCode);
//canceled intent
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_CANCELED) {
sendCallback(false, true, false);
}
}
@Override
public void onNewIntent(Intent intent) {
}
public void sendCallback(Boolean completed, Boolean cancelled, Boolean error) {
if (callback != null) {
callback.invoke(completed, cancelled, error);
callback = null;
}
}
@ReactMethod
public void send(ReadableMap options, final Callback callback) {
try {
this.callback = callback;
new SendSMSObserver(reactContext, this, options).start();
String body = options.hasKey("body") ? options.getString("body") : "";
ReadableArray recipients = options.hasKey("recipients") ? options.getArray("recipients") : null;
Intent sendIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(reactContext);
sendIntent = new Intent(Intent.ACTION_SEND);
if (defaultSmsPackageName != null){
sendIntent.setPackage(defaultSmsPackageName);
}
sendIntent.setType("text/plain");
}else {
sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("vnd.android-dir/mms-sms");
}
sendIntent.putExtra("sms_body", body);
sendIntent.putExtra("exit_on_sent", true);
//if recipients specified
if (recipients != null) {
//Samsung for some reason uses commas and not semicolons as a delimiter
String separator = "; ";
if(android.os.Build.MANUFACTURER.equalsIgnoreCase("Samsung")){
separator = ", ";
}
String recipientString = "";
for (int i = 0; i < recipients.size(); i++) {
recipientString += recipients.getString(i);
recipientString += separator;
}
if (recipientString.length() > 2)
recipientString = recipientString.substring(0,recipientString.length()-2);
sendIntent.putExtra("address", recipientString);
}
reactContext.startActivityForResult(sendIntent, REQUEST_CODE, sendIntent.getExtras());
} catch (Exception e) {
//error!
sendCallback(false, false, true);
throw e;
}
}
}