From 010cad5a86d52a6958ffe1ed5d9ee7b64c2fef41 Mon Sep 17 00:00:00 2001 From: dbaq Date: Sat, 2 Apr 2016 10:44:23 -0700 Subject: [PATCH 1/2] request SEND_SMS permission --- src/android/Sms.java | 112 ++++++++++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 38 deletions(-) diff --git a/src/android/Sms.java b/src/android/Sms.java index d6f73b4..996a1d7 100644 --- a/src/android/Sms.java +++ b/src/android/Sms.java @@ -20,54 +20,90 @@ import org.json.JSONException; public class Sms extends CordovaPlugin { + public final String ACTION_SEND_SMS = "send"; + private static final String INTENT_FILTER_SMS_SENT = "SMS_SENT"; + private static final int SEND_SMS_REQ_CODE = 0; + + private CallbackContext callbackContext; + + private JSONArray args; + @Override public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException { - + this.callbackContext = callbackContext; + this.args = args; if (action.equals(ACTION_SEND_SMS)) { - cordova.getThreadPool().execute(new Runnable() { - @Override - public void run() { - try { - //parsing arguments - String separator = ";"; - if (android.os.Build.MANUFACTURER.equalsIgnoreCase("Samsung")) { - // See http://stackoverflow.com/questions/18974898/send-sms-through-intent-to-multiple-phone-numbers/18975676#18975676 - separator = ","; - } - String phoneNumber = args.getJSONArray(0).join(separator).replace("\"", ""); - String message = args.getString(1); - String method = args.getString(2); - boolean replaceLineBreaks = Boolean.parseBoolean(args.getString(3)); - - // replacing \n by new line if the parameter replaceLineBreaks is set to true - if (replaceLineBreaks) { - message = message.replace("\\n", System.getProperty("line.separator")); - } - if (!checkSupport()) { - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "SMS not supported on this platform")); - return; - } - if (method.equalsIgnoreCase("INTENT")) { - invokeSMSIntent(phoneNumber, message); - // always passes success back to the app - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK)); - } else { - send(callbackContext, phoneNumber, message); - } - return; - } catch (JSONException ex) { - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); - } - } - }); - return true; + if (hasPermission()) { + sendSMS(); + } else { + requestPermission(); + } + return true; } return false; } + private boolean hasPermission() { + return cordova.hasPermission(android.Manifest.permission.SEND_SMS); + } + + private void requestPermission() { + cordova.requestPermission(this, SEND_SMS_REQ_CODE, android.Manifest.permission.SEND_SMS); + } + + public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException { + for (int r : grantResults) { + if (r == PackageManager.PERMISSION_DENIED) { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "User has denied permission")); + return; + } + } + sendSMS(); + } + + private boolean sendSMS() { + cordova.getThreadPool().execute(new Runnable() { + @Override + public void run() { + try { + //parsing arguments + String separator = ";"; + if (android.os.Build.MANUFACTURER.equalsIgnoreCase("Samsung")) { + // See http://stackoverflow.com/questions/18974898/send-sms-through-intent-to-multiple-phone-numbers/18975676#18975676 + separator = ","; + } + String phoneNumber = args.getJSONArray(0).join(separator).replace("\"", ""); + String message = args.getString(1); + String method = args.getString(2); + boolean replaceLineBreaks = Boolean.parseBoolean(args.getString(3)); + + // replacing \n by new line if the parameter replaceLineBreaks is set to true + if (replaceLineBreaks) { + message = message.replace("\\n", System.getProperty("line.separator")); + } + if (!checkSupport()) { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "SMS not supported on this platform")); + return; + } + if (method.equalsIgnoreCase("INTENT")) { + invokeSMSIntent(phoneNumber, message); + // always passes success back to the app + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK)); + } else { + send(callbackContext, phoneNumber, message); + } + return; + } catch (JSONException ex) { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + } + } + }); + return true; + } + private boolean checkSupport() { Activity ctx = this.cordova.getActivity(); return ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY); From 45b1d146f5b3d62003a86425897c8180a535863d Mon Sep 17 00:00:00 2001 From: dbaq Date: Sat, 2 Apr 2016 10:52:13 -0700 Subject: [PATCH 2/2] add hasPermission function --- src/android/Sms.java | 6 ++++++ www/sms.js | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/src/android/Sms.java b/src/android/Sms.java index 996a1d7..079966e 100644 --- a/src/android/Sms.java +++ b/src/android/Sms.java @@ -23,6 +23,8 @@ public class Sms extends CordovaPlugin { public final String ACTION_SEND_SMS = "send"; + public final String ACTION_HAS_PERMISSION = "has_permission"; + private static final String INTENT_FILTER_SMS_SENT = "SMS_SENT"; private static final int SEND_SMS_REQ_CODE = 0; @@ -43,6 +45,10 @@ public boolean execute(String action, final JSONArray args, final CallbackContex } return true; } + else if (action.equals(ACTION_HAS_PERMISSION)) { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, hasPermission())); + return true; + } return false; } diff --git a/www/sms.js b/www/sms.js index c011544..af851b0 100644 --- a/www/sms.js +++ b/www/sms.js @@ -42,5 +42,14 @@ sms.send = function(phone, message, options, success, failure) { ); }; +sms.hasPermission = function(success, failure) { + // fire + exec( + success, + failure, + 'Sms', + 'has_permission', [] + ); +}; module.exports = sms; \ No newline at end of file