-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathSmsModule.java
More file actions
307 lines (274 loc) · 12.9 KB
/
SmsModule.java
File metadata and controls
307 lines (274 loc) · 12.9 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
package com.react;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import android.content.ContentValues;
import android.os.Bundle;
import android.widget.Toast;
import android.app.LoaderManager;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.ContentResolver;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class SmsModule extends ReactContextBaseJavaModule /*implements LoaderManager.LoaderCallbacks<Cursor>*/ {
// private LoaderManager mManager;
private Cursor smsCursor;
private Map<Long, String> smsList;
private Map<Long, Object> smsListBody;
Activity mActivity = null;
private static Context context;
private ReactContext mReactContext;
private Callback cb_autoSend_succ = null;
private Callback cb_autoSend_err = null;
public SmsModule(ReactApplicationContext reactContext) {
super(reactContext);
mReactContext = reactContext;
smsList = new HashMap<Long, String>();
context = reactContext.getApplicationContext();
}
@Override
public String getName() {
return "Sms";
}
@ReactMethod
public void list(String filter, final Callback errorCallback, final Callback successCallback) {
try {
JSONObject filterJ = new JSONObject(filter);
String uri_filter = filterJ.has("box") ? filterJ.optString("box") : "inbox";
int fread = filterJ.has("read") ? filterJ.optInt("read") : -1;
int fid = filterJ.has("_id") ? filterJ.optInt("_id") : -1;
int ftid = filterJ.has("thread_id") ? filterJ.optInt("thread_id") : -1;
String faddress = filterJ.optString("address");
String fAddressRegex = filterJ.optString("addressRegex");
String fcontent = filterJ.optString("body");
String fContentRegex = filterJ.optString("bodyRegex");
int indexFrom = filterJ.has("indexFrom") ? filterJ.optInt("indexFrom") : 0;
int maxCount = filterJ.has("maxCount") ? filterJ.optInt("maxCount") : -1;
String selection = filterJ.has("selection") ? filterJ.optString("selection") : "";
String sortOrder = filterJ.has("sortOrder") ? filterJ.optString("sortOrder") : null;
long maxDate = filterJ.has("maxDate") ? filterJ.optLong("maxDate") : -1;
long minDate = filterJ.has("minDate") ? filterJ.optLong("minDate") : -1;
Cursor cursor = context.getContentResolver().query(Uri.parse("content://sms/" + uri_filter), null, selection, null,
sortOrder);
int c = 0;
JSONArray jsons = new JSONArray();
while (cursor != null && cursor.moveToNext()) {
boolean matchFilter = true;
if (fid > -1)
matchFilter = fid == cursor.getInt(cursor.getColumnIndex("_id"));
else if (ftid > -1)
matchFilter = ftid == cursor.getInt(cursor.getColumnIndex("thread_id"));
else if (fread > -1)
matchFilter = fread == cursor.getInt(cursor.getColumnIndex("read"));
else if (faddress != null && !faddress.isEmpty())
matchFilter = faddress.equals(cursor.getString(cursor.getColumnIndex("address")).trim());
else if (fcontent != null && !fcontent.isEmpty())
matchFilter = fcontent.equals(cursor.getString(cursor.getColumnIndex("body")).trim());
if (fContentRegex != null && !fContentRegex.isEmpty())
matchFilter = matchFilter && cursor.getString(cursor.getColumnIndex("body")).matches(fContentRegex);
if (fAddressRegex != null && !fAddressRegex.isEmpty())
matchFilter = matchFilter && cursor.getString(cursor.getColumnIndex("address")).matches(fAddressRegex);
if (maxDate > -1)
matchFilter = matchFilter && maxDate >= cursor.getLong(cursor.getColumnIndex("date"));
if (minDate > -1)
matchFilter = matchFilter && minDate <= cursor.getLong(cursor.getColumnIndex("date"));
if (matchFilter) {
if (c >= indexFrom) {
if (maxCount > 0 && c >= indexFrom + maxCount)
break;
// Long dateTime = Long.parseLong(cursor.getString(cursor.getColumnIndex("date")));
// String message = cursor.getString(cursor.getColumnIndex("body"));
JSONObject json;
json = getJsonFromCursor(cursor);
jsons.put(json);
}
c++;
}
}
cursor.close();
try {
successCallback.invoke(c, jsons.toString());
} catch (Exception e) {
errorCallback.invoke(e.getMessage());
}
} catch (JSONException e) {
errorCallback.invoke(e.getMessage());
return;
}
}
private JSONObject getJsonFromCursor(Cursor cur) {
JSONObject json = new JSONObject();
int nCol = cur.getColumnCount();
String[] keys = cur.getColumnNames();
try {
for (int j = 0; j < nCol; j++)
switch (cur.getType(j)) {
case Cursor.FIELD_TYPE_NULL:
json.put(keys[j], null);
break;
case Cursor.FIELD_TYPE_INTEGER:
json.put(keys[j], cur.getLong(j));
break;
case Cursor.FIELD_TYPE_FLOAT:
json.put(keys[j], cur.getFloat(j));
break;
case Cursor.FIELD_TYPE_STRING:
json.put(keys[j], cur.getString(j));
break;
case Cursor.FIELD_TYPE_BLOB:
json.put(keys[j], cur.getBlob(j));
}
} catch (Exception e) {
return null;
}
return json;
}
@ReactMethod
public void send(String addresses, String text, final Callback errorCallback, final Callback successCallback) {
mActivity = getCurrentActivity();
try {
JSONObject jsonObject = new JSONObject(addresses);
JSONArray addressList = jsonObject.getJSONArray("addressList");
int n;
if ((n = addressList.length()) > 0) {
PendingIntent sentIntent = PendingIntent.getBroadcast(mActivity, 0, new Intent("SENDING_SMS"), 0);
SmsManager sms = SmsManager.getDefault();
for (int i = 0; i < n; i++) {
String address;
if ((address = addressList.optString(i)).length() > 0)
sms.sendTextMessage(address, null, text, sentIntent, null);
}
} else {
PendingIntent sentIntent = PendingIntent.getActivity(mActivity, 0, new Intent("android.intent.action.VIEW"), 0);
Intent intent = new Intent("android.intent.action.VIEW");
intent.putExtra("sms_body", text);
intent.setData(Uri.parse("sms:"));
try {
sentIntent.send(mActivity.getApplicationContext(), 0, intent);
successCallback.invoke("OK");
} catch (PendingIntent.CanceledException e) {
errorCallback.invoke(e.getMessage());
return;
}
}
return;
} catch (JSONException e) {
errorCallback.invoke(e.getMessage());
return;
}
}
@ReactMethod
public void delete(Integer id, final Callback errorCallback, final Callback successCallback) {
try {
int res = context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
if (res > 0) {
successCallback.invoke("OK");
} else {
errorCallback.invoke("SMS not found");
}
return;
} catch (Exception e) {
errorCallback.invoke(e.getMessage());
return;
}
}
private void sendEvent(ReactContext reactContext, String eventName, String params) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
}
private void sendCallback(String message, boolean success) {
if (success && cb_autoSend_succ != null) {
cb_autoSend_succ.invoke(message);
cb_autoSend_succ = null;
} else if (!success && cb_autoSend_err != null) {
cb_autoSend_err.invoke(message);
cb_autoSend_err = null;
}
}
@ReactMethod
public void autoSend(String phoneNumber, String message, final Callback errorCallback,
final Callback successCallback) {
cb_autoSend_succ = successCallback;
cb_autoSend_err = errorCallback;
try {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>();
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED), 0);
//---when the SMS has been sent---
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
sendCallback("SMS sent", true);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
sendCallback("Generic failure", false);
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
sendCallback("No service", false);
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
sendCallback("Null PDU", false);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
sendCallback("Radio off", false);
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
sendEvent(mReactContext, "sms_onDelivery", "SMS delivered");
break;
case Activity.RESULT_CANCELED:
sendEvent(mReactContext, "sms_onDelivery", "SMS not delivered");
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
for (int i = 0; i < parts.size(); i++) {
sentPendingIntents.add(i, sentPI);
deliveredPendingIntents.add(i, deliveredPI);
}
sms.sendMultipartTextMessage(phoneNumber, null, parts, sentPendingIntents, deliveredPendingIntents);
ContentValues values = new ContentValues();
values.put("address", phoneNumber);
values.put("body", message);
context.getContentResolver().insert(Uri.parse("content://sms/sent"), values);
} catch (Exception e) {
sendCallback(e.getMessage(), false);
}
}
}