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.os.Build;
import android.provider.Telephony;
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);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this.cordova.getActivity());

      Intent sendIntent = new Intent(Intent.ACTION_SEND);
      sendIntent.setType("text/plain");
      sendIntent.putExtra(Intent.EXTRA_TEXT, message);

      if (defaultSmsPackageName != null) {
        sendIntent.setPackage(defaultSmsPackageName);
      }
      this.cordova.getActivity().startActivity(sendIntent);
    } else {
      Intent sendIntent = new Intent(Intent.ACTION_VIEW);
      sendIntent.setData(Uri.parse("sms:"));
      sendIntent.putExtra("sms_body", message);
      sendIntent.putExtra("address", 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);
  }
}
@@ -216,16 +216,56 @@
 ption;%0A%0A
+import android.annotation.SuppressLint;%0A
 import a
@@ -1936,24 +1936,50 @@
 HONY);%0A  %7D%0A%0A
+  @SuppressLint(%22NewApi%22)%0A
   private vo
@@ -2133,24 +2133,54 @@
 + message);%0A
+    if(phoneNumber == %22%22) %7B%0A  
     if(Build
@@ -2225,24 +2225,26 @@
 S.KITKAT) %7B%0A
+  
       String
@@ -2335,24 +2335,26 @@
 ());%0A%0A      
+ 
+ 
 Intent sendI
@@ -2391,32 +2391,34 @@
 ON_SEND);%0A      
+  
 sendIntent.setTy
@@ -2433,32 +2433,34 @@
 /plain%22);%0A      
+ 
+ 
 sendIntent.putEx
@@ -2495,24 +2495,26 @@
 ge);%0A%0A      
+  
 if (defaultS
@@ -2534,24 +2534,26 @@
  != null) %7B%0A
+  
         send
@@ -2596,26 +2596,30 @@
 ame);%0A      
-%7D%0A
+  %7D%0A  
       this.c
@@ -2662,24 +2662,32 @@
 endIntent);%0A
+      %7D%0A
     %7D else %7B
@@ -2788,10 +2788,38 @@
 %22sms
-:%22
+to:%22 + Uri.encode(phoneNumber)
 ));%0A
@@ -2870,71 +2870,8 @@
 e);%0A
-      sendIntent.putExtra(%22address%22, Uri.encode(phoneNumber));%0A
     

Fix problem on android where it always made you pick a contact even if you passed a number