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);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) //At least KitKat
    {
      String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity); //Need to change the build to API 19

      Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
      sendIntent.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));

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

      if (defaultSmsPackageName != null) {
        sendIntent.setPackage(defaultSmsPackageName);
      }
      activity.startActivity(sendIntent);

    } else {
      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);
  }
}
@@ -2015,607 +2015,8 @@
 e);%0A
-    if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.KITKAT) //At least KitKat%0A    %7B%0A      String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity); //Need to change the build to API 19%0A%0A      Intent sendIntent = new Intent(Intent.ACTION_SENDTO);%0A      sendIntent.setData(Uri.parse(%22smsto:%22 + Uri.encode(phoneNumber)));%0A%0A      sendIntent.setType(%22text/plain%22);%0A      sendIntent.putExtra(Intent.EXTRA_TEXT, message);%0A%0A      if (defaultSmsPackageName != null) %7B%0A        sendIntent.setPackage(defaultSmsPackageName);%0A      %7D%0A      activity.startActivity(sendIntent);%0A%0A    %7D else %7B%0A  
     
@@ -2067,18 +2067,16 @@
 _VIEW);%0A
-  
     send
@@ -2117,20 +2117,16 @@
 e);%0A    
-  //
 sendInte
@@ -2162,26 +2162,24 @@
 umber);%0A    
- 
- 
 sendIntent.s
@@ -2237,18 +2237,16 @@
 ));%0A    
-  
 this.cor
@@ -2291,22 +2291,16 @@
 ntent);%0A
-    %7D%0A
   %7D%0A%0A  p

Fix android code