From c92c8ce98ff4cdb6d28ebc4b32297dbc1fbb5ce8 Mon Sep 17 00:00:00 2001 From: Amit Ghosh Date: Tue, 12 Jun 2018 00:34:54 +0600 Subject: [PATCH] Fix: Exception handling --- src/android/SMSPlugin.java | 324 +++++++++++++++++++++---------------- 1 file changed, 181 insertions(+), 143 deletions(-) diff --git a/src/android/SMSPlugin.java b/src/android/SMSPlugin.java index 59895f2..bf149fb 100644 --- a/src/android/SMSPlugin.java +++ b/src/android/SMSPlugin.java @@ -16,7 +16,6 @@ import android.os.Handler; import android.telephony.SmsManager; import android.telephony.SmsMessage; -import android.telephony.PhoneNumberUtils; import android.util.Log; import java.security.MessageDigest; @@ -30,9 +29,9 @@ import org.json.JSONObject; public class SMSPlugin -extends CordovaPlugin { + extends CordovaPlugin { private static final String LOGTAG = "SMSPlugin"; - + public static final String ACTION_SET_OPTIONS = "setOptions"; private static final String ACTION_START_WATCH = "startWatch"; private static final String ACTION_STOP_WATCH = "stopWatch"; @@ -41,13 +40,13 @@ public class SMSPlugin private static final String ACTION_DELETE_SMS = "deleteSMS"; private static final String ACTION_RESTORE_SMS = "restoreSMS"; private static final String ACTION_SEND_SMS = "sendSMS"; - + public static final String OPT_LICENSE = "license"; private static final String SEND_SMS_ACTION = "SENT_SMS_ACTION"; private static final String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION"; private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; public static final String SMS_EXTRA_NAME = "pdus"; - + public static final String SMS_URI_ALL = "content://sms/"; public static final String SMS_URI_INBOX = "content://sms/inbox"; public static final String SMS_URI_SEND = "content://sms/sent"; @@ -55,7 +54,7 @@ public class SMSPlugin public static final String SMS_URI_OUTBOX = "content://sms/outbox"; public static final String SMS_URI_FAILED = "content://sms/failed"; public static final String SMS_URI_QUEUED = "content://sms/queued"; - + public static final String BOX = "box"; public static final String ADDRESS = "address"; public static final String BODY = "body"; @@ -69,19 +68,19 @@ public class SMSPlugin public static final String REPLY_PATH_PRESENT = "reply_path_present"; public static final String TYPE = "type"; public static final String PROTOCOL = "protocol"; - + public static final int MESSAGE_TYPE_INBOX = 1; public static final int MESSAGE_TYPE_SENT = 2; public static final int MESSAGE_IS_NOT_READ = 0; public static final int MESSAGE_IS_READ = 1; public static final int MESSAGE_IS_NOT_SEEN = 0; public static final int MESSAGE_IS_SEEN = 1; - + private static final String SMS_GENERAL_ERROR = "SMS_GENERAL_ERROR"; private static final String NO_SMS_SERVICE_AVAILABLE = "NO_SMS_SERVICE_AVAILABLE"; private static final String SMS_FEATURE_NOT_SUPPORTED = "SMS_FEATURE_NOT_SUPPORTED"; private static final String SENDING_SMS_ID = "SENDING_SMS"; - + private ContentObserver mObserver = null; private BroadcastReceiver mReceiver = null; private boolean mIntercept = false; @@ -103,17 +102,37 @@ public boolean execute(String action, JSONArray inputs, CallbackContext callback result = this.enableIntercept(on_off, callbackContext); } else if (ACTION_DELETE_SMS.equals(action)) { JSONObject msg = inputs.optJSONObject(0); - result = this.deleteSMS(msg, callbackContext); + try { + result = this.deleteSMS(msg, callbackContext); + } catch (Exception e) { + Log.e(LOGTAG, "deleteSMS : " + e.getMessage()); + e.printStackTrace(); + } } else if (ACTION_RESTORE_SMS.equals(action)) { JSONArray smsList = inputs.optJSONArray(0); - result = this.restoreSMS(smsList, callbackContext); + try { + result = this.restoreSMS(smsList, callbackContext); + } catch (Exception e) { + Log.e(LOGTAG, "restoreSMS : " + e.getMessage()); + e.printStackTrace(); + } } else if (ACTION_LIST_SMS.equals(action)) { JSONObject filters = inputs.optJSONObject(0); - result = this.listSMS(filters, callbackContext); + try { + result = this.listSMS(filters, callbackContext); + } catch (Exception e) { + Log.e(LOGTAG, "listSMS : " + e.getMessage()); + e.printStackTrace(); + } } else if (ACTION_SEND_SMS.equals(action)) { JSONArray addressList = inputs.optJSONArray(0); String message = inputs.optString(1); - result = this.sendSMS(addressList, message, callbackContext); + try { + result = this.sendSMS(addressList, message, callbackContext); + } catch (Exception e) { + Log.e(LOGTAG, "sendSMS : " + e.getMessage()); + e.printStackTrace(); + } } else { Log.d(LOGTAG, String.format("Invalid action passed: %s", action)); result = new PluginResult(PluginResult.Status.INVALID_ACTION); @@ -150,22 +169,25 @@ public final String md5(String s) { hexString.append(h); } return hexString.toString(); - } - catch (NoSuchAlgorithmException digest) { + } catch (NoSuchAlgorithmException digest) { return ""; } } private PluginResult startWatch(CallbackContext callbackContext) { - Log.d(LOGTAG, ACTION_START_WATCH); - if (this.mObserver == null) { - this.createContentObserver(); - } - if (this.mReceiver == null) { - this.createIncomingSMSReceiver(); - } - if (callbackContext != null) { - callbackContext.success(); + try { + Log.d(LOGTAG, ACTION_START_WATCH); + if (this.mObserver == null) { + this.createContentObserver(); + } + if (this.mReceiver == null) { + this.createIncomingSMSReceiver(); + } + if (callbackContext != null) { + callbackContext.success(); + } + } catch (Exception e) { + Log.e(LOGTAG, "startWatch : " + e.getMessage()); } return null; } @@ -198,27 +220,26 @@ private PluginResult enableIntercept(boolean on_off, CallbackContext callbackCon return null; } - private PluginResult sendSMS(JSONArray addressList, String text, CallbackContext callbackContext) { + private PluginResult sendSMS(JSONArray addressList, String text, CallbackContext callbackContext) throws Exception { Log.d(LOGTAG, ACTION_SEND_SMS); if (this.cordova.getActivity().getPackageManager().hasSystemFeature("android.hardware.telephony")) { int n; if ((n = addressList.length()) > 0) { - PendingIntent sentIntent = PendingIntent.getBroadcast((Context)this.cordova.getActivity(), (int)0, (Intent)new Intent("SENDING_SMS"), (int)0); + PendingIntent sentIntent = PendingIntent.getBroadcast((Context) this.cordova.getActivity(), (int) 0, (Intent) new Intent("SENDING_SMS"), (int) 0); SmsManager sms = SmsManager.getDefault(); for (int i = 0; i < n; ++i) { String address; if ((address = addressList.optString(i)).length() <= 0) continue; - sms.sendTextMessage(address, null, text, sentIntent, (PendingIntent)null); + sms.sendTextMessage(address, null, text, sentIntent, (PendingIntent) null); } } else { - PendingIntent sentIntent = PendingIntent.getActivity((Context)this.cordova.getActivity(), (int)0, (Intent)new Intent("android.intent.action.VIEW"), (int)0); + PendingIntent sentIntent = PendingIntent.getActivity((Context) this.cordova.getActivity(), (int) 0, (Intent) new Intent("android.intent.action.VIEW"), (int) 0); Intent intent = new Intent("android.intent.action.VIEW"); intent.putExtra("sms_body", text); intent.setType("vnd.android-dir/mms-sms"); try { sentIntent.send(this.cordova.getActivity().getApplicationContext(), 0, intent); - } - catch (PendingIntent.CanceledException e) { + } catch (PendingIntent.CanceledException e) { e.printStackTrace(); } } @@ -229,7 +250,7 @@ private PluginResult sendSMS(JSONArray addressList, String text, CallbackContext return null; } - private PluginResult listSMS(JSONObject filter, CallbackContext callbackContext) { + private PluginResult listSMS(JSONObject filter, CallbackContext callbackContext) throws Exception { Log.i(LOGTAG, ACTION_LIST_SMS); String uri_filter = filter.has(BOX) ? filter.optString(BOX) : "inbox"; int fread = filter.has(READ) ? filter.optInt(READ) : -1; @@ -241,7 +262,7 @@ private PluginResult listSMS(JSONObject filter, CallbackContext callbackContext) JSONArray jsons = new JSONArray(); Activity ctx = this.cordova.getActivity(); Uri uri = Uri.parse((SMS_URI_ALL + uri_filter)); - Cursor cur = ctx.getContentResolver().query(uri, (String[])null, "", (String[])null, null); + Cursor cur = ctx.getContentResolver().query(uri, (String[]) null, "", (String[]) null, null); int i = 0; while (cur.moveToNext()) { JSONObject json; @@ -251,14 +272,14 @@ private PluginResult listSMS(JSONObject filter, CallbackContext callbackContext) } else if (fread > -1) { matchFilter = (fread == cur.getInt(cur.getColumnIndex(READ))); } else if (faddress.length() > 0) { - matchFilter = PhoneNumberUtils.compare(faddress, cur.getString(cur.getColumnIndex(ADDRESS)).trim()); + matchFilter = faddress.equals(cur.getString(cur.getColumnIndex(ADDRESS)).trim()); } else if (fcontent.length() > 0) { matchFilter = fcontent.equals(cur.getString(cur.getColumnIndex(BODY)).trim()); } else { matchFilter = true; } - if (! matchFilter) continue; - + if (!matchFilter) continue; + if (i < indexFrom) continue; if (i >= indexFrom + maxCount) break; ++i; @@ -268,60 +289,60 @@ private PluginResult listSMS(JSONObject filter, CallbackContext callbackContext) cur.close(); return null; } - jsons.put((Object)json); + jsons.put((Object) json); } cur.close(); callbackContext.success(jsons); return null; } - private JSONObject getJsonFromCursor(Cursor cur) { - JSONObject json = new JSONObject(); - - int nCol = cur.getColumnCount(); - String keys[] = cur.getColumnNames(); - - try { - for(int j=0; j" + ex.getMessage()); + ex.printStackTrace(); + } } }; @@ -411,7 +449,7 @@ public void onChange(boolean selfChange, Uri uri) { Log.d(LOGTAG, "sms inbox observer registered"); } - private PluginResult deleteSMS(JSONObject filter, CallbackContext callbackContext) { + private PluginResult deleteSMS(JSONObject filter, CallbackContext callbackContext) throws Exception { Log.d(LOGTAG, ACTION_DELETE_SMS); String uri_filter = filter.has(BOX) ? filter.optString(BOX) : "inbox"; int fread = filter.has(READ) ? filter.optInt(READ) : -1; @@ -422,61 +460,61 @@ private PluginResult deleteSMS(JSONObject filter, CallbackContext callbackContex int n = 0; try { Uri uri = Uri.parse((SMS_URI_ALL + uri_filter)); - Cursor cur = ctx.getContentResolver().query(uri, (String[])null, "", (String[])null, null); + Cursor cur = ctx.getContentResolver().query(uri, (String[]) null, "", (String[]) null, null); while (cur.moveToNext()) { int id = cur.getInt(cur.getColumnIndex("_id")); boolean matchId = fid > -1 && fid == id; int read = cur.getInt(cur.getColumnIndex(READ)); boolean matchRead = fread > -1 && fread == read; String address = cur.getString(cur.getColumnIndex(ADDRESS)).trim(); - boolean matchAddr = faddress.length() > 0 && PhoneNumberUtils.compare(faddress, address); + boolean matchAddr = faddress.length() > 0 && address.equals(faddress); String body = cur.getString(cur.getColumnIndex(BODY)).trim(); boolean matchContent = fcontent.length() > 0 && body.equals(fcontent); if (!matchId && !matchRead && !matchAddr && !matchContent) continue; - ctx.getContentResolver().delete(uri, "_id=" + id, (String[])null); + ctx.getContentResolver().delete(uri, "_id=" + id, (String[]) null); ++n; } callbackContext.success(n); - } - catch (Exception e) { + } catch (Exception e) { callbackContext.error(e.toString()); } return null; } - private JSONObject getJsonFromSmsMessage(SmsMessage sms) { - JSONObject json = new JSONObject(); - + private JSONObject getJsonFromSmsMessage(SmsMessage sms) throws Exception { + JSONObject json = new JSONObject(); + try { - json.put( ADDRESS, sms.getOriginatingAddress() ); - json.put( BODY, sms.getMessageBody() ); // May need sms.getMessageBody.toString() - json.put( DATE_SENT, sms.getTimestampMillis() ); - json.put( DATE, System.currentTimeMillis() ); - json.put( READ, MESSAGE_IS_NOT_READ ); - json.put( SEEN, MESSAGE_IS_NOT_SEEN ); - json.put( STATUS, sms.getStatus() ); - json.put( TYPE, MESSAGE_TYPE_INBOX ); - json.put( SERVICE_CENTER, sms.getServiceCenterAddress()); - - } catch ( Exception e ) { - e.printStackTrace(); + json.put(ADDRESS, sms.getOriginatingAddress()); + json.put(BODY, sms.getMessageBody()); // May need sms.getMessageBody.toString() + json.put(DATE_SENT, sms.getTimestampMillis()); + json.put(DATE, System.currentTimeMillis()); + json.put(READ, MESSAGE_IS_NOT_READ); + json.put(SEEN, MESSAGE_IS_NOT_SEEN); + json.put(STATUS, sms.getStatus()); + json.put(TYPE, MESSAGE_TYPE_INBOX); + json.put(SERVICE_CENTER, sms.getServiceCenterAddress()); + + } catch (Exception e) { + e.printStackTrace(); } - return json; + return json; } - - private ContentValues getContentValuesFromJson(JSONObject json) { - ContentValues values = new ContentValues(); - values.put( ADDRESS, json.optString(ADDRESS) ); - values.put( BODY, json.optString(BODY)); - values.put( DATE_SENT, json.optLong(DATE_SENT)); - values.put( READ, json.optInt(READ)); - values.put( SEEN, json.optInt(SEEN)); - values.put( TYPE, json.optInt(TYPE) ); - values.put( SERVICE_CENTER, json.optString(SERVICE_CENTER)); - return values; + + private ContentValues getContentValuesFromJson(JSONObject json) throws Exception { + ContentValues values = new ContentValues(); + values.put(ADDRESS, json.optString(ADDRESS)); + values.put(BODY, json.optString(BODY)); + values.put(DATE_SENT, json.optLong(DATE_SENT)); + values.put(READ, json.optInt(READ)); + values.put(SEEN, json.optInt(SEEN)); + values.put(TYPE, json.optInt(TYPE)); + values.put(SERVICE_CENTER, json.optString(SERVICE_CENTER)); + return values; } - private PluginResult restoreSMS(JSONArray array, CallbackContext callbackContext) { + + private PluginResult restoreSMS(JSONArray array, CallbackContext callbackContext) throws Exception { ContentResolver resolver = this.cordova.getActivity().getContentResolver(); Uri uri = Uri.parse(SMS_URI_INBOX); int n = array.length();