@@ -26,84 +26,79 @@ public class Sms extends CordovaPlugin {
2626 BroadcastReceiver receiver ;
2727
2828 @ Override
29- public boolean execute (String action , JSONArray args , final CallbackContext callbackContext ) throws JSONException {
30-
31- if (action .equals (ACTION_SEND_SMS )) {
32- try {
33- String phoneNumber = args .getJSONArray (0 ).join (";" ).replace ("\" " , "" );
34- String message = args .getString (1 );
35- String method = args .getString (2 );
29+ public boolean execute (String action , JSONArray args , final CallbackContext callbackContext ) throws JSONException {
30+ if (action .equals (ACTION_SEND_SMS )) {
31+ try {
32+ String phoneNumber = args .getJSONArray (0 ).join (";" ).replace ("\" " , "" );
33+ String message = args .getString (1 );
34+ String method = args .getString (2 );
35+
36+ if (!checkSupport ()) {
37+ callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .ERROR , "SMS not supported on this platform" ));
38+ return true ;
39+ }
3640
37- if (!checkSupport ()) {
38- callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .ERROR , "SMS not supported on this platform" ));
39- return true ;
40- }
41+ if (method .equalsIgnoreCase ("INTENT" )) {
42+ invokeSMSIntent (phoneNumber , message );
43+ // always passes success back to the app
44+ callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .OK ));
45+ } else {
46+ // by creating this broadcast receiver we can check whether or not the SMS was sent
47+ if (receiver == null ) {
48+ this .receiver = new BroadcastReceiver () {
49+ @ Override
50+ public void onReceive (Context context , Intent intent ) {
51+ PluginResult pluginResult ;
4152
42- if (method .equalsIgnoreCase ("INTENT" )) {
43- invokeSMSIntent (phoneNumber , message );
44- // always passes success back to the app
45- callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .OK ));
46- } else {
47- // by creating this broadcast receiver we can check whether or not the SMS was sent
48- if (receiver == null ) {
49- this .receiver = new BroadcastReceiver () {
50- @ Override
51- public void onReceive (Context context , Intent intent ) {
52- PluginResult pluginResult ;
53-
54- switch (getResultCode ()) {
55- case SmsManager .STATUS_ON_ICC_SENT :
56- pluginResult = new PluginResult (PluginResult .Status .OK );
57- pluginResult .setKeepCallback (true );
58- callbackContext .sendPluginResult (pluginResult );
59- break ;
60- case Activity .RESULT_OK :
61- pluginResult = new PluginResult (PluginResult .Status .OK );
62- pluginResult .setKeepCallback (true );
63- callbackContext .sendPluginResult (pluginResult );
64- break ;
65- case SmsManager .RESULT_ERROR_NO_SERVICE :
66- pluginResult = new PluginResult (PluginResult .Status .ERROR );
67- pluginResult .setKeepCallback (true );
68- callbackContext .sendPluginResult (pluginResult );
69- break ;
70- default :
71- pluginResult = new PluginResult (PluginResult .Status .ERROR );
72- pluginResult .setKeepCallback (true );
73- callbackContext .sendPluginResult (pluginResult );
74- break ;
75- }
76-
77- }
78-
79- };
80- final IntentFilter intentFilter = new IntentFilter ();
81- intentFilter .addAction (INTENT_FILTER_SMS_SENT );
82- cordova .getActivity ().registerReceiver (this .receiver , intentFilter );
83- }
84- send (phoneNumber , message );
85- }
86- return true ;
87- } catch (JSONException ex ) {
88- callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .JSON_EXCEPTION ));
89- }
90- }
91- return false ;
92- }
53+ switch (getResultCode ()) {
54+ case SmsManager .STATUS_ON_ICC_SENT :
55+ pluginResult = new PluginResult (PluginResult .Status .OK );
56+ pluginResult .setKeepCallback (true );
57+ callbackContext .sendPluginResult (pluginResult );
58+ break ;
59+ case Activity .RESULT_OK :
60+ pluginResult = new PluginResult (PluginResult .Status .OK );
61+ pluginResult .setKeepCallback (true );
62+ callbackContext .sendPluginResult (pluginResult );
63+ break ;
64+ case SmsManager .RESULT_ERROR_NO_SERVICE :
65+ pluginResult = new PluginResult (PluginResult .Status .ERROR );
66+ pluginResult .setKeepCallback (true );
67+ callbackContext .sendPluginResult (pluginResult );
68+ break ;
69+ default :
70+ pluginResult = new PluginResult (PluginResult .Status .ERROR );
71+ pluginResult .setKeepCallback (true );
72+ callbackContext .sendPluginResult (pluginResult );
73+ break ;
74+ }
75+ }
76+ };
77+ final IntentFilter intentFilter = new IntentFilter ();
78+ intentFilter .addAction (INTENT_FILTER_SMS_SENT );
79+ cordova .getActivity ().registerReceiver (this .receiver , intentFilter );
80+ }
81+ send (phoneNumber , message );
82+ }
83+ return true ;
84+ } catch (JSONException ex ) {
85+ callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .JSON_EXCEPTION ));
86+ }
87+ }
88+ return false ;
89+ }
9390
9491 private boolean checkSupport () {
9592 Activity ctx = this .cordova .getActivity ();
9693 return ctx .getPackageManager ().hasSystemFeature (
97- PackageManager .FEATURE_TELEPHONY );
94+ PackageManager .FEATURE_TELEPHONY );
9895 }
9996
10097 @ SuppressLint ("NewApi" )
10198 private void invokeSMSIntent (String phoneNumber , String message ) {
10299 Intent sendIntent ;
103- if ("" .equals (phoneNumber )
104- && Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
105- String defaultSmsPackageName = Telephony .Sms
106- .getDefaultSmsPackage (this .cordova .getActivity ());
100+ if ("" .equals (phoneNumber ) && Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
101+ String defaultSmsPackageName = Telephony .Sms .getDefaultSmsPackage (this .cordova .getActivity ());
107102
108103 sendIntent = new Intent (Intent .ACTION_SEND );
109104 sendIntent .setType ("text/plain" );
@@ -122,8 +117,7 @@ private void invokeSMSIntent(String phoneNumber, String message) {
122117
123118 private void send (String phoneNumber , String message ) {
124119 SmsManager manager = SmsManager .getDefault ();
125- PendingIntent sentIntent = PendingIntent .getBroadcast (this .cordova
126- .getActivity (), 0 , new Intent (INTENT_FILTER_SMS_SENT ), 0 );
120+ PendingIntent sentIntent = PendingIntent .getBroadcast (this .cordova .getActivity (), 0 , new Intent (INTENT_FILTER_SMS_SENT ), 0 );
127121
128122 // Use SendMultipartTextMessage if the message requires it
129123 int parts_size = manager .divideMessage (message ).size ();
@@ -134,10 +128,9 @@ private void send(String phoneNumber, String message) {
134128 sentIntents .add (sentIntent );
135129 }
136130 manager .sendMultipartTextMessage (phoneNumber , null , parts ,
137- sentIntents , null );
131+ sentIntents , null );
138132 } else {
139- manager .sendTextMessage (phoneNumber , null , message , sentIntent ,
140- null );
133+ manager .sendTextMessage (phoneNumber , null , message , sentIntent , null );
141134 }
142135 }
143136
0 commit comments