package mx.ferreyra.callnumber;

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

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.TelephonyManager;

public class CFCallNumber extends CordovaPlugin
{
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

        String number = args.getString(0);

        if( number.startsWith("tel:") == false){
            number = String.format("tel:%s", number);
        }

        if (((TelephonyManager)cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE)).getPhoneType() == TelephonyManager.PHONE_TYPE_NONE ){
            callbackContext.error("NoFeatureCallSupported");
        } else {
            try {
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse(number));
                cordova.getActivity().startActivity(intent);
                callbackContext.success();
            }
            catch (Exception e) {
                callbackContext.error("CouldNotCallPhoneNumber");
            }
        }

        return true;
    }
}

@@ -478,17 +478,16 @@
 ption %7B%0A
-%0A
         
@@ -648,316 +648,118 @@
     
-if (((TelephonyManager)cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE)).getPhoneType() == TelephonyManager.PHONE_TYPE_NONE )%7B%0A            callbackContext.error(%22NoFeatureCallSupported%22);%0A        %7D else %7B%0A            try %7B%0A                Intent intent = new Intent(Intent.ACTION_CALL);%0A    
+try %7B%0A            Intent intent = new Intent(isTelephonyEnabled() ? Intent.ACTION_CALL : Intent.ACTION_VIEW);%0A
     
@@ -801,20 +801,16 @@
 mber));%0A
-    
         
@@ -862,36 +862,32 @@
 t);%0A            
-    
 callbackContext.
@@ -897,36 +897,32 @@
 cess();%0A        
-    
 %7D%0A            ca
@@ -915,20 +915,16 @@
         
- 
-   
 catch (E
@@ -937,20 +937,16 @@
 on e) %7B%0A
-    
         
@@ -1011,44 +1011,279 @@
     
+%7D%0A%0A        return true;%0A
     %7D%0A
+%0A
     
-    %7D%0A%0A        return true
+private boolean isTelephonyEnabled()%7B%0A        TelephonyManager tm = (TelephonyManager)cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);%0A%0A        return tm != null && tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE
 ;%0A  

improve handling devices with no phone call support