diff --git a/src/android/startApp.java b/src/android/startApp.java index adb2d4f..11501d3 100644 --- a/src/android/startApp.java +++ b/src/android/startApp.java @@ -21,12 +21,15 @@ package org.apache.cordova.startapp; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; import org.json.JSONArray; +import org.json.JSONObject; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.ComponentName; import android.content.pm.PackageManager; +import java.util.Iterator; +import android.net.Uri; /** * This class provides access to vibration on the device. @@ -51,11 +54,7 @@ public class startApp extends CordovaPlugin { this.start(args, callbackContext); } else if(action.equals("check")) { - this.check( - args.getString(0), - callbackContext, - this.cordova.getActivity().getApplicationContext() - ); + this.check(args.getString(0), callbackContext); } return true; @@ -68,31 +67,85 @@ public class startApp extends CordovaPlugin { * startApp */ public void start(JSONArray args, CallbackContext callback) { - // args.getString(0) - com name - // args.getString(1) - activity - // args.getString(2) - key - // args.getString(3) - value + /** + * arguments + * ['com.application.name'] + */ + String com_name = null; + String activity = null; + Intent LaunchIntent; + try { - Intent LaunchIntent = this.cordova.getActivity().getPackageManager().getLaunchIntentForPackage(args.getString(0)); + if (args.get(0) instanceof JSONArray) { + com_name = args.getJSONArray(0).getString(0); + activity = args.getJSONArray(0).getString(1); + } + else { + com_name = args.getString(0); + } + + /** + * call activity + */ + if(activity != null) { + if(com_name.equals("action")) { + // sample: android.intent.action.VIEW + LaunchIntent = new Intent("android.intent.action." + activity); + } + else { + LaunchIntent = new Intent(); + LaunchIntent.setComponent(new ComponentName(com_name, activity)); + } + } + else { + LaunchIntent = this.cordova.getActivity().getPackageManager().getLaunchIntentForPackage(com_name); + } - if(args.length() > 3) { - ComponentName comp = new ComponentName(args.getString(0), args.getString(1)); - LaunchIntent.setComponent(comp); - LaunchIntent.putExtra(args.getString(2), args.getString(3)); + /** + * put arguments + */ + if(args.length() > 1) { + JSONArray params = args.getJSONArray(1); + JSONObject key_value; + String key; + String value; + + for(int i = 0; i < params.length(); i++) { + if (params.get(i) instanceof JSONObject) { + Iterator iter = params.getJSONObject(i).keys(); + + while (iter.hasNext()) { + key = iter.next(); + try { + value = params.getJSONObject(i).getString(key); + + LaunchIntent.putExtra(key, value); + } catch (JSONException e) { + callback.error("json params: " + e.toString()); + } + } + } + else { + LaunchIntent.setData(Uri.parse(params.getString(i))); + } + } } this.cordova.getActivity().startActivity(LaunchIntent); callback.success(); + + } catch (JSONException e) { + callback.error("json: " + e.toString()); } catch (Exception e) { - callback.error(e.toString()); + callback.error("intent: " + e.toString()); } } /** * checkApp */ - public void check(String component, CallbackContext callback, Context context) { - PackageManager pm = context.getPackageManager(); + public void check(String component, CallbackContext callback) { + PackageManager pm = this.cordova.getActivity().getApplicationContext().getPackageManager(); try { pm.getPackageInfo(component, PackageManager.GET_ACTIVITIES); callback.success();