package org.apache.cordova.plugin.sms;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.telephony.SmsManager;
import android.util.Log;

public class Sms extends CordovaPlugin {
  private final String LOG_TAG = "SMSPlugin";
  public final String ACTION_SEND_SMS = "send";

  @Override
  public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    if (action.equals(ACTION_SEND_SMS)) {
      try {
        String phoneNumber = args.getJSONArray(0).join(";").replace("\"", "");
        String message = args.getString(1);
        String method = args.getString(2);

        if (!checkSupport()) {
          callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "SMS not supported on this platform"));
          return true;
        }

        if(method.equalsIgnoreCase("INTENT")) {
          invokeSMSIntent(phoneNumber, message);
          callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.NO_RESULT));
        } else {
          send(phoneNumber, message);
        }
        
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
        return true;
      }
      catch (JSONException ex) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
      }     
    }
    return false;
  }

  private boolean checkSupport() {
    Activity ctx = this.cordova.getActivity();
    return ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
  }

  private void invokeSMSIntent(String phoneNumber, String message) {
    //Log.d(LOG_TAG, "Starting SMS app, with number(s): " + phoneNumber + " and message " + message);
    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    sendIntent.putExtra("sms_body", message);
    sendIntent.putExtra("address", phoneNumber);
    sendIntent.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));
    this.cordova.getActivity().startActivity(sendIntent);
  }

  private void send(String phoneNumber, String message) {
    Log.d(LOG_TAG, "Sending SMS to " + phoneNumber + " and message " + message);
    SmsManager manager = SmsManager.getDefault();
    PendingIntent sentIntent = PendingIntent.getActivity(this.cordova.getActivity(), 0, new Intent(), 0);
    manager.sendTextMessage(phoneNumber, null, message, sentIntent, null);
  }
}
@@ -376,16 +376,76 @@
 et.Uri;%0A
+import android.os.Build;%0Aimport android.provider.Telephony;%0A
 import a
@@ -2067,24 +2067,190 @@
 + message);%0A
+    if(Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.KITKAT) %7B%0A      String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this.cordova.getActivity());%0A%0A  
     Intent s
@@ -2290,15 +2290,57 @@
 ION_
-VIEW
+SEND);%0A      sendIntent.setType(%22text/plain%22
 );%0A
+  
     
@@ -2363,79 +2363,276 @@
 tra(
-%22sms_body%22, message);%0A    sendIntent.putExtra(%22address%22, phoneNumber
+Intent.EXTRA_TEXT, message);%0A%0A      if (defaultSmsPackageName != null) %7B%0A        sendIntent.setPackage(defaultSmsPackageName);%0A      %7D%0A      this.cordova.getActivity().startActivity(sendIntent);%0A    %7D else %7B%0A      Intent sendIntent = new Intent(Intent.ACTION_VIEW
 );%0A
+  
     
@@ -2668,14 +2668,98 @@
 %22sms
-to:%22 +
+:%22));%0A      sendIntent.putExtra(%22sms_body%22, message);%0A      sendIntent.putExtra(%22address%22,
  Uri
@@ -2779,19 +2779,20 @@
 Number))
-)
 ;%0A
+  
     this
@@ -2841,16 +2841,22 @@
 ntent);%0A
+    %7D%0A
   %7D%0A%0A  p
@@ -2910,20 +2910,22 @@
 sage) %7B%0A
-
     
+//
 Log.d(LO

Add support for passing no number in android