2020import org .json .JSONException ;
2121
2222public class Sms extends CordovaPlugin {
23- public final String ACTION_SEND_SMS = "send" ;
24- private static final String INTENT_FILTER_SMS_SENT = "SMS_SENT" ;
23+ public final String ACTION_SEND_SMS = "send" ;
24+ private static final String INTENT_FILTER_SMS_SENT = "SMS_SENT" ;
2525
26- BroadcastReceiver receiver ;
27- private CallbackContext callbackContext ;
26+ BroadcastReceiver receiver ;
2827
29- @ Override
28+ @ Override
3029 public boolean execute (String action , JSONArray args , final CallbackContext callbackContext ) throws JSONException {
31- this .callbackContext = callbackContext ;
32-
30+
3331 if (action .equals (ACTION_SEND_SMS )) {
3432 try {
3533 String phoneNumber = args .getJSONArray (0 ).join (";" ).replace ("\" " , "" );
3634 String message = args .getString (1 );
3735 String method = args .getString (2 );
3836
3937 if (!checkSupport ()) {
40- callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .ERROR , "SMS not supported on this platform" ));
38+ callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .ERROR , "SMS not supported on this platform" ));
4139 return true ;
4240 }
4341
@@ -51,10 +49,33 @@ public boolean execute(String action, JSONArray args, final CallbackContext call
5149 this .receiver = new BroadcastReceiver () {
5250 @ Override
5351 public void onReceive (Context context , Intent intent ) {
54- callbackContext .sendPluginResult (new PluginResult (
55- receiver .getResultCode () == Activity .RESULT_OK || receiver .getResultCode () == SmsManager .STATUS_ON_ICC_SENT ?
56- PluginResult .Status .OK : PluginResult .Status .ERROR ));
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+
5777 }
78+
5879 };
5980 final IntentFilter intentFilter = new IntentFilter ();
6081 intentFilter .addAction (INTENT_FILTER_SMS_SENT );
@@ -70,63 +91,64 @@ public void onReceive(Context context, Intent intent) {
7091 return false ;
7192 }
7293
73- private boolean checkSupport () {
74- Activity ctx = this .cordova .getActivity ();
75- return ctx .getPackageManager ().hasSystemFeature (PackageManager .FEATURE_TELEPHONY );
76- }
94+ private boolean checkSupport () {
95+ Activity ctx = this .cordova .getActivity ();
96+ return ctx .getPackageManager ().hasSystemFeature (
97+ PackageManager .FEATURE_TELEPHONY );
98+ }
7799
78- @ SuppressLint ("NewApi" )
79- private void invokeSMSIntent (String phoneNumber , String message ) {
80- Intent sendIntent ;
81- if ("" .equals (phoneNumber ) && Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
82- String defaultSmsPackageName = Telephony .Sms .getDefaultSmsPackage (this .cordova .getActivity ());
100+ @ SuppressLint ("NewApi" )
101+ private void invokeSMSIntent (String phoneNumber , String message ) {
102+ 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 ());
83107
84- sendIntent = new Intent (Intent .ACTION_SEND );
85- sendIntent .setType ("text/plain" );
86- sendIntent .putExtra (Intent .EXTRA_TEXT , message );
108+ sendIntent = new Intent (Intent .ACTION_SEND );
109+ sendIntent .setType ("text/plain" );
110+ sendIntent .putExtra (Intent .EXTRA_TEXT , message );
87111
88- if (defaultSmsPackageName != null ) {
89- sendIntent .setPackage (defaultSmsPackageName );
90- }
91- } else {
92- sendIntent = new Intent (Intent .ACTION_VIEW );
93- sendIntent .setData (Uri .parse ("smsto:" + Uri .encode (phoneNumber )));
94- sendIntent .putExtra ("sms_body" , message );
95- }
96- this .cordova .getActivity ().startActivity (sendIntent );
97- }
112+ if (defaultSmsPackageName != null ) {
113+ sendIntent .setPackage (defaultSmsPackageName );
114+ }
115+ } else {
116+ sendIntent = new Intent (Intent .ACTION_VIEW );
117+ sendIntent .setData (Uri .parse ("smsto:" + Uri .encode (phoneNumber )));
118+ sendIntent .putExtra ("sms_body" , message );
119+ }
120+ this .cordova .getActivity ().startActivity (sendIntent );
121+ }
98122
99- private void send (String phoneNumber , String message ) {
100- SmsManager manager = SmsManager .getDefault ();
101- PendingIntent sentIntent = PendingIntent .getBroadcast (this .cordova .getActivity (), 0 , new Intent (INTENT_FILTER_SMS_SENT ), 0 );
123+ private void send (String phoneNumber , String message ) {
124+ SmsManager manager = SmsManager .getDefault ();
125+ PendingIntent sentIntent = PendingIntent .getBroadcast (this .cordova
126+ .getActivity (), 0 , new Intent (INTENT_FILTER_SMS_SENT ), 0 );
102127
103- // Use SendMultipartTextMessage if the message requires it
104- int parts_size = manager .divideMessage (message ).size ();
105- if (parts_size > 1 ) {
106- ArrayList <String > parts = manager .divideMessage (message );
107- ArrayList <PendingIntent > sentIntents = new ArrayList <PendingIntent >();
108- for (int i = 0 ; i < parts_size ; ++i ) {
109- sentIntents .add (sentIntent );
110- }
111- manager .sendMultipartTextMessage (phoneNumber , null , parts , sentIntents , null );
112- } else {
113- manager .sendTextMessage (phoneNumber , null , message , sentIntent , null );
114- }
128+ // Use SendMultipartTextMessage if the message requires it
129+ int parts_size = manager .divideMessage (message ).size ();
130+ if (parts_size > 1 ) {
131+ ArrayList <String > parts = manager .divideMessage (message );
132+ ArrayList <PendingIntent > sentIntents = new ArrayList <PendingIntent >();
133+ for (int i = 0 ; i < parts_size ; ++i ) {
134+ sentIntents .add (sentIntent );
135+ }
136+ manager .sendMultipartTextMessage (phoneNumber , null , parts ,
137+ sentIntents , null );
138+ } else {
139+ manager .sendTextMessage (phoneNumber , null , message , sentIntent ,
140+ null );
141+ }
142+ }
115143
116- // Don't return any result now, since status results will be sent when events come in from broadcast receiver
117- PluginResult pluginResult = new PluginResult (PluginResult .Status .NO_RESULT );
118- pluginResult .setKeepCallback (true );
119- callbackContext .sendPluginResult (pluginResult );
120- }
121-
122- @ Override
123- public void onDestroy () {
124- if (this .receiver != null ) {
125- try {
126- this .cordova .getActivity ().unregisterReceiver (this .receiver );
127- this .receiver = null ;
128- } catch (Exception ignore ) {
129- }
130- }
131- }
144+ @ Override
145+ public void onDestroy () {
146+ if (this .receiver != null ) {
147+ try {
148+ this .cordova .getActivity ().unregisterReceiver (this .receiver );
149+ this .receiver = null ;
150+ } catch (Exception ignore ) {
151+ }
152+ }
153+ }
132154}
0 commit comments