diff --git a/README.md b/README.md index f01d5e0..39de67b 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,16 @@ Launches an Android intent. For example: function() {alert('Failed to open URL via Android Intent')}; ); +Launches an Anroid activity by package name and class + + window.plugins.webintent.startActivity( + { + action: window.plugins.webintent.ACTION_MAIN, + handler: { packageName: 'com.nianticproject.ingress', className: 'com.nianticproject.ingress.NemesisActivity' } + }, + function() {}, + function() {alert('Failed to open URL via Android Intent') } + ); ### hasExtra ### checks if this app was invoked with the specified extra. For example: diff --git a/plugin.xml b/plugin.xml index a126a27..7dc4d21 100644 --- a/plugin.xml +++ b/plugin.xml @@ -3,26 +3,26 @@ + id="net.tunts.webintent" + version="0.2.1"> WebIntent Web Intent Plugin Apache 2.0 cordova,webintent - - + + - + - + diff --git a/src/android/WebIntent.java b/src/android/WebIntent.java index c1b2d87..180111d 100644 --- a/src/android/WebIntent.java +++ b/src/android/WebIntent.java @@ -1,52 +1,50 @@ -package com.borismus.webintent; +package net.tunts.webintent; import java.util.HashMap; import java.util.Map; -import org.apache.cordova.DroidGap; +import org.apache.cordova.CordovaActivity; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; -import android.util.Log; import android.text.Html; -import org.apache.cordova.api.Plugin; -import org.apache.cordova.api.PluginResult; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.PluginResult; +import org.apache.cordova.CallbackContext; /** - * WebIntent is a PhoneGap plugin that bridges Android intents and web - * applications: + * WebIntent is a PhoneGap plugin that bridges Android intents and web applications: * - * 1. web apps can spawn intents that call native Android applications. 2. - * (after setting up correct intent filters for PhoneGap applications), Android - * intents can be handled by PhoneGap web applications. + * 1. web apps can spawn intents that call native Android applications. 2. (after setting up correct intent filters for + * PhoneGap applications), Android intents can be handled by PhoneGap web applications. * * @author boris@borismus.com * */ -public class WebIntent extends Plugin { +public class WebIntent extends CordovaPlugin { - private String onNewIntentCallback = null; + private CallbackContext onNewIntentCallback = null; /** * Executes the request and returns PluginResult. * - * @param action - * The action to execute. - * @param args - * JSONArray of arguments for the plugin. - * @param callbackId - * The callback id used when calling back into JavaScript. - * @return A PluginResult object with a status and message. + * @param action The action to execute. + * @param args JSONArray of arguments for the plugin. + * @param callbackContext The callbackContext used when calling back into JavaScript. + * @return boolean */ - public PluginResult execute(String action, JSONArray args, String callbackId) { + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { try { if (action.equals("startActivity")) { if (args.length() != 1) { - return new PluginResult(PluginResult.Status.INVALID_ACTION); + PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION); + callbackContext.sendPluginResult(res); + return false; } // Parse the arguments @@ -54,7 +52,9 @@ public PluginResult execute(String action, JSONArray args, String callbackId) { String type = obj.has("type") ? obj.getString("type") : null; Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null; JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null; + JSONObject handler = obj.has("handler") ? obj.getJSONObject("handler") : null; Map extrasMap = new HashMap(); + Map handlerMap = null; // Populate the extras if any exist if (extras != null) { @@ -66,49 +66,86 @@ public PluginResult execute(String action, JSONArray args, String callbackId) { } } - startActivity(obj.getString("action"), uri, type, extrasMap); - return new PluginResult(PluginResult.Status.OK); + if (handler != null) { + handlerMap = new HashMap(); + + handlerMap.put("packageName", handler.getString("packageName")); + handlerMap.put("className", handler.getString("className")); + } + + try{ + startActivity(obj.getString("action"), uri, type, extrasMap, handlerMap); + } catch (ActivityNotFoundException e){ + callbackContext.error(e.getMessage()); + return false; + } + + callbackContext.success(); + return true; } else if (action.equals("hasExtra")) { if (args.length() != 1) { - return new PluginResult(PluginResult.Status.INVALID_ACTION); + PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION); + callbackContext.sendPluginResult(res); + return false; } - Intent i = ((DroidGap)this.cordova.getActivity()).getIntent(); + Intent i = ((CordovaActivity) this.cordova.getActivity()).getIntent(); String extraName = args.getString(0); - return new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName)); + PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName)); + callbackContext.sendPluginResult(res); + return true; } else if (action.equals("getExtra")) { if (args.length() != 1) { - return new PluginResult(PluginResult.Status.INVALID_ACTION); + PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION); + callbackContext.sendPluginResult(res); + return false; } - Intent i = ((DroidGap)this.cordova.getActivity()).getIntent(); + Intent i = ((CordovaActivity) this.cordova.getActivity()).getIntent(); String extraName = args.getString(0); + if (i.hasExtra(extraName)) { - return new PluginResult(PluginResult.Status.OK, i.getStringExtra(extraName)); + PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName)); + callbackContext.sendPluginResult(res); + return true; + } else { - return new PluginResult(PluginResult.Status.ERROR); + PluginResult res = new PluginResult(PluginResult.Status.ERROR); + callbackContext.sendPluginResult(res); + return false; } + } else if (action.equals("getUri")) { if (args.length() != 0) { - return new PluginResult(PluginResult.Status.INVALID_ACTION); + PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION); + callbackContext.sendPluginResult(res); + return false; } - Intent i = ((DroidGap)this.cordova.getActivity()).getIntent(); + Intent i = ((CordovaActivity) this.cordova.getActivity()).getIntent(); String uri = i.getDataString(); - return new PluginResult(PluginResult.Status.OK, uri); + + callbackContext.success(uri); + return true; + } else if (action.equals("onNewIntent")) { if (args.length() != 0) { - return new PluginResult(PluginResult.Status.INVALID_ACTION); + PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION); + callbackContext.sendPluginResult(res); + return false; } - this.onNewIntentCallback = callbackId; - PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT); - result.setKeepCallback(true); - return result; - } else if (action.equals("sendBroadcast")) - { + this.onNewIntentCallback = callbackContext; + PluginResult res = new PluginResult(PluginResult.Status.NO_RESULT); + res.setKeepCallback(true); + callbackContext.sendPluginResult(res); + return true; + + } else if (action.equals("sendBroadcast")) { if (args.length() != 1) { - return new PluginResult(PluginResult.Status.INVALID_ACTION); + PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION); + callbackContext.sendPluginResult(res); + return false; } // Parse the arguments @@ -128,35 +165,44 @@ public PluginResult execute(String action, JSONArray args, String callbackId) { } sendBroadcast(obj.getString("action"), extrasMap); - return new PluginResult(PluginResult.Status.OK); + callbackContext.success(); + return true; + } - return new PluginResult(PluginResult.Status.INVALID_ACTION); - } catch (JSONException e) { - e.printStackTrace(); - return new PluginResult(PluginResult.Status.JSON_EXCEPTION); + + PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION); + callbackContext.sendPluginResult(res); + return false; + + } + catch (JSONException e) { + callbackContext.error(e.getMessage()); + return false; } } @Override public void onNewIntent(Intent intent) { if (this.onNewIntentCallback != null) { - PluginResult result = new PluginResult(PluginResult.Status.OK, intent.getDataString()); - result.setKeepCallback(true); - this.success(result, this.onNewIntentCallback); + this.onNewIntentCallback.success(intent.getDataString()); } } - void startActivity(String action, Uri uri, String type, Map extras) { + void startActivity(String action, Uri uri, String type, Map extras, Map handlerMap) { Intent i = (uri != null ? new Intent(action, uri) : new Intent(action)); - + + if (handlerMap != null) { + i.setClassName(handlerMap.get("packageName"), handlerMap.get("className")); + } + if (type != null && uri != null) { - i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6 + i.setDataAndType(uri, type); // Fix the crash problem with android 2.3.6 } else { if (type != null) { i.setType(type); } } - + for (String key : extras.keySet()) { String value = extras.get(key); // If type is text html, the extra text must sent as HTML @@ -173,7 +219,7 @@ void startActivity(String action, Uri uri, String type, Map extr i.putExtra(key, value); } } - ((DroidGap)this.cordova.getActivity()).startActivity(i); + ((CordovaActivity) this.cordova.getActivity()).startActivity(i); } void sendBroadcast(String action, Map extras) { @@ -184,6 +230,6 @@ void sendBroadcast(String action, Map extras) { intent.putExtra(key, value); } - ((DroidGap)this.cordova.getActivity()).sendBroadcast(intent); + ((CordovaActivity) this.cordova.getActivity()).sendBroadcast(intent); } -} +} \ No newline at end of file diff --git a/www/webintent.js b/www/webintent.js index 542645a..9ed1d60 100644 --- a/www/webintent.js +++ b/www/webintent.js @@ -10,6 +10,7 @@ WebIntent.prototype.ACTION_SEND = "android.intent.action.SEND"; WebIntent.prototype.ACTION_VIEW= "android.intent.action.VIEW"; + WebIntent.prototype.ACTION_MAIN = "android.intent.action.MAIN"; WebIntent.prototype.EXTRA_TEXT = "android.intent.extra.TEXT"; WebIntent.prototype.EXTRA_SUBJECT = "android.intent.extra.SUBJECT"; WebIntent.prototype.EXTRA_STREAM = "android.intent.extra.STREAM"; @@ -63,11 +64,10 @@ }, 'WebIntent', 'sendBroadcast', [params]); }; - cordova.addConstructor(function() { - window.webintent = new WebIntent(); + window.webintent = new WebIntent(); + + // backwards compatibility + window.plugins = window.plugins || {}; + window.plugins.webintent = window.webintent; - // backwards compatibility - window.plugins = window.plugins || {}; - window.plugins.webintent = window.webintent; - }); })(window.PhoneGap || window.Cordova || window.cordova);