From a55802f0e492c7317624bf9036c556d8a9dd66f4 Mon Sep 17 00:00:00 2001 From: wesleyakio Date: Thu, 5 Dec 2013 18:19:08 -0200 Subject: [PATCH 1/6] Trying to make WebIntent with phonegap build --- plugin.xml | 6 +- src/android/WebIntent.java | 129 ++++++++++++++++++++++--------------- www/webintent.js | 11 ++-- 3 files changed, 86 insertions(+), 60 deletions(-) diff --git a/plugin.xml b/plugin.xml index a126a27..5e9802c 100644 --- a/plugin.xml +++ b/plugin.xml @@ -3,7 +3,7 @@ WebIntent Web Intent Plugin @@ -18,11 +18,11 @@ - + - + diff --git a/src/android/WebIntent.java b/src/android/WebIntent.java index c1b2d87..da962d0 100644 --- a/src/android/WebIntent.java +++ b/src/android/WebIntent.java @@ -1,4 +1,4 @@ -package com.borismus.webintent; +package net.tunts.webintent; import java.util.HashMap; import java.util.Map; @@ -13,40 +13,38 @@ import android.util.Log; import android.text.Html; -import org.apache.cordova.api.Plugin; +import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; +import org.apache.cordova.api.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 @@ -67,48 +65,72 @@ public PluginResult execute(String action, JSONArray args, String callbackId) { } startActivity(obj.getString("action"), uri, type, extrasMap); - return new PluginResult(PluginResult.Status.OK); + 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 = ((DroidGap) 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 = ((DroidGap) 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 = ((DroidGap) 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 +150,40 @@ 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) { Intent i = (uri != null ? new Intent(action, uri) : new Intent(action)); - + 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 +200,7 @@ void startActivity(String action, Uri uri, String type, Map extr i.putExtra(key, value); } } - ((DroidGap)this.cordova.getActivity()).startActivity(i); + ((DroidGap) this.cordova.getActivity()).startActivity(i); } void sendBroadcast(String action, Map extras) { @@ -184,6 +211,6 @@ void sendBroadcast(String action, Map extras) { intent.putExtra(key, value); } - ((DroidGap)this.cordova.getActivity()).sendBroadcast(intent); + ((DroidGap) this.cordova.getActivity()).sendBroadcast(intent); } -} +} \ No newline at end of file diff --git a/www/webintent.js b/www/webintent.js index 542645a..4cab387 100644 --- a/www/webintent.js +++ b/www/webintent.js @@ -63,11 +63,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); From e26bcce5f27c9c3fa14e29c0af095e0aeab8c912 Mon Sep 17 00:00:00 2001 From: wesleyakio Date: Fri, 6 Dec 2013 17:30:16 -0200 Subject: [PATCH 2/6] A few more fixes for 2.9.1 --- plugin.xml | 2 +- src/android/WebIntent.java | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/plugin.xml b/plugin.xml index 5e9802c..94b510f 100644 --- a/plugin.xml +++ b/plugin.xml @@ -18,7 +18,7 @@ - + diff --git a/src/android/WebIntent.java b/src/android/WebIntent.java index da962d0..f52f1e7 100644 --- a/src/android/WebIntent.java +++ b/src/android/WebIntent.java @@ -3,7 +3,7 @@ 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; @@ -13,9 +13,9 @@ import android.util.Log; import android.text.Html; -import org.apache.cordova.api.CordovaPlugin; -import org.apache.cordova.api.PluginResult; -import org.apache.cordova.api.CallbackContext; +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: @@ -200,7 +200,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) { @@ -211,6 +211,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 From e5d334207af414c8c0f1c5fba264e37f98bcb6d0 Mon Sep 17 00:00:00 2001 From: wesleyakio Date: Fri, 6 Dec 2013 22:47:54 -0200 Subject: [PATCH 3/6] Stuff I forgot to change on the last merge --- src/android/WebIntent.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/android/WebIntent.java b/src/android/WebIntent.java index f52f1e7..9076a21 100644 --- a/src/android/WebIntent.java +++ b/src/android/WebIntent.java @@ -74,7 +74,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo callbackContext.sendPluginResult(res); return false; } - Intent i = ((DroidGap) this.cordova.getActivity()).getIntent(); + Intent i = ((CordovaActivity) this.cordova.getActivity()).getIntent(); String extraName = args.getString(0); PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName)); callbackContext.sendPluginResult(res); @@ -86,7 +86,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo 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)) { @@ -107,7 +107,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo return false; } - Intent i = ((DroidGap) this.cordova.getActivity()).getIntent(); + Intent i = ((CordovaActivity) this.cordova.getActivity()).getIntent(); String uri = i.getDataString(); callbackContext.success(uri); From 2629478c0d0884c9ba34950c5bc4a02e6dc2fd71 Mon Sep 17 00:00:00 2001 From: wesleyakio Date: Sun, 8 Dec 2013 19:56:14 -0200 Subject: [PATCH 4/6] Added support for invoking a specific Activity in a given package --- plugin.xml | 6 +++--- src/android/WebIntent.java | 18 +++++++++++++++--- www/webintent.js | 1 + 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/plugin.xml b/plugin.xml index 94b510f..8458517 100644 --- a/plugin.xml +++ b/plugin.xml @@ -4,14 +4,14 @@ xmlns:rim="http://www.blackberry.com/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" id="net.tunts.webintent" - version="0.1.0"> + version="0.2.0"> WebIntent Web Intent Plugin Apache 2.0 cordova,webintent - - + + diff --git a/src/android/WebIntent.java b/src/android/WebIntent.java index 9076a21..b00fc50 100644 --- a/src/android/WebIntent.java +++ b/src/android/WebIntent.java @@ -10,7 +10,6 @@ import android.content.Intent; import android.net.Uri; -import android.util.Log; import android.text.Html; import org.apache.cordova.CordovaPlugin; @@ -52,7 +51,9 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo 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) { @@ -64,7 +65,14 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo } } - startActivity(obj.getString("action"), uri, type, extrasMap); + if (handler != null) { + handlerMap = new HashMap(); + + handlerMap.put("packageName", handler.getString("packageName")); + handlerMap.put("className", handler.getString("className")); + } + + startActivity(obj.getString("action"), uri, type, extrasMap, handlerMap); callbackContext.success(); return true; @@ -173,9 +181,13 @@ public void onNewIntent(Intent intent) { } } - 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 } else { diff --git a/www/webintent.js b/www/webintent.js index 4cab387..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"; From 1b379bf499751d12bdec356df3b28fa3b495d7cc Mon Sep 17 00:00:00 2001 From: wesleyakio Date: Sun, 15 Dec 2013 20:10:45 -0200 Subject: [PATCH 5/6] Adding error handler for ActivityNotFoundException. --- plugin.xml | 2 +- src/android/WebIntent.java | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/plugin.xml b/plugin.xml index 8458517..7dc4d21 100644 --- a/plugin.xml +++ b/plugin.xml @@ -4,7 +4,7 @@ xmlns:rim="http://www.blackberry.com/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" id="net.tunts.webintent" - version="0.2.0"> + version="0.2.1"> WebIntent Web Intent Plugin Apache 2.0 diff --git a/src/android/WebIntent.java b/src/android/WebIntent.java index b00fc50..180111d 100644 --- a/src/android/WebIntent.java +++ b/src/android/WebIntent.java @@ -8,6 +8,7 @@ import org.json.JSONException; import org.json.JSONObject; +import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; import android.text.Html; @@ -72,7 +73,13 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo handlerMap.put("className", handler.getString("className")); } - startActivity(obj.getString("action"), uri, type, extrasMap, handlerMap); + try{ + startActivity(obj.getString("action"), uri, type, extrasMap, handlerMap); + } catch (ActivityNotFoundException e){ + callbackContext.error(e.getMessage()); + return false; + } + callbackContext.success(); return true; From c2bff4693c816c9ccdfa9869981438b536c8683d Mon Sep 17 00:00:00 2001 From: Dmitry Konstantinov Date: Wed, 25 Dec 2013 17:58:01 +0400 Subject: [PATCH 6/6] Activity start example by package and class name --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) 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: