From 9ce4561b1897efb5646e6238c5feeff28fc28695 Mon Sep 17 00:00:00 2001 From: "behrooz shabani (everplays)" Date: Fri, 11 Oct 2013 14:45:55 +0330 Subject: [PATCH 1/8] fixes 'exec() call to unknown plugin' on android --- plugin.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index a8e5d7c..4c9f592 100644 --- a/plugin.xml +++ b/plugin.xml @@ -23,7 +23,7 @@ - + From ddc5adfdc188c70b920424af84ed08d407264e75 Mon Sep 17 00:00:00 2001 From: "behrooz shabani (everplays)" Date: Fri, 11 Oct 2013 14:51:18 +0330 Subject: [PATCH 2/8] android platform is now compatible with cordova 3.0.x --- src/android/ChildBrowser.java | 99 +++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 46 deletions(-) diff --git a/src/android/ChildBrowser.java b/src/android/ChildBrowser.java index c63017f..eaf7bdb 100644 --- a/src/android/ChildBrowser.java +++ b/src/android/ChildBrowser.java @@ -4,6 +4,7 @@ * * Copyright (c) 2005-2011, Nitobi Software Inc. * Copyright (c) 2010-2011, IBM Corporation + * Copyright (c) 2013, Takhfifan. */ package com.phonegap.plugins.childBrowser; @@ -35,15 +36,19 @@ import android.widget.ImageButton; import android.widget.LinearLayout; -import org.apache.cordova.api.*; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CordovaInterface; +import org.apache.cordova.CallbackContext; -public class ChildBrowser extends Plugin { +public class ChildBrowser extends CordovaPlugin { protected static final String LOG_TAG = "ChildBrowser"; private static int CLOSE_EVENT = 0; private static int LOCATION_CHANGED_EVENT = 1; + private static int OPEN_EXTERNAL_EVENT = 2; + private static int BROWSER_OPENED = 3; - private String browserCallbackId = null; + private CallbackContext callbackContext = null; private Dialog dialog; private WebView webview; @@ -53,57 +58,61 @@ public class ChildBrowser extends Plugin { private boolean showNavigationBar = true; /** - * Executes the request and returns PluginResult. + * Executes the request and returns boolean. * - * @param action The action to execute. - * @param args JSONArry 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 JSONArry of arguments for the plugin. + * @param callbackContext The callback context used when calling back into JavaScript. + * @return boolean */ - public PluginResult execute(String action, JSONArray args, String callbackId) { - PluginResult.Status status = PluginResult.Status.OK; + @Override + public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) + throws JSONException + { + Log.d(LOG_TAG, action); String result = ""; - try { - if (action.equals("showWebPage")) { - this.browserCallbackId = callbackId; - - // If the ChildBrowser is already open then throw an error - if (dialog != null && dialog.isShowing()) { - return new PluginResult(PluginResult.Status.ERROR, "ChildBrowser is already open"); - } + if (action.equals("showWebPage")) { + Log.d(LOG_TAG, args.getString(0)); + Log.d(LOG_TAG, action); + this.callbackContext = callbackContext; + // If the ChildBrowser is already open then throw an error + if (dialog != null && dialog.isShowing()) { + callbackContext.error("ChildBrowser is already open"); + } else { result = this.showWebPage(args.getString(0), args.optJSONObject(1)); if (result.length() > 0) { - status = PluginResult.Status.ERROR; - return new PluginResult(status, result); + callbackContext.error(result); } else { - PluginResult pluginResult = new PluginResult(status, result); - pluginResult.setKeepCallback(true); - return pluginResult; - } - } else if (action.equals("close")) { - closeDialog(); - - JSONObject obj = new JSONObject(); - obj.put("type", CLOSE_EVENT); - - PluginResult pluginResult = new PluginResult(status, obj); - pluginResult.setKeepCallback(false); - return pluginResult; - } else if (action.equals("openExternal")) { - result = this.openExternal(args.getString(0), args.optBoolean(1)); - if (result.length() > 0) { - status = PluginResult.Status.ERROR; + JSONObject obj = new JSONObject(); + obj.put("type", BROWSER_OPENED); + callbackContext.success(obj); } + Log.d(LOG_TAG, result); + } + return true; + } else if (action.equals("close")) { + closeDialog(); + + JSONObject obj = new JSONObject(); + obj.put("type", CLOSE_EVENT); + + callbackContext.success(obj); + return true; + } else if (action.equals("openExternal")) { + result = this.openExternal(args.getString(0), args.optBoolean(1)); + if (result.length() > 0) { + callbackContext.error(result); } else { - status = PluginResult.Status.INVALID_ACTION; + JSONObject obj = new JSONObject(); + obj.put("type", OPEN_EXTERNAL_EVENT); + callbackContext.success(obj); } - return new PluginResult(status, result); - } catch (JSONException e) { - return new PluginResult(PluginResult.Status.JSON_EXCEPTION); + return true; } + return false; } /** @@ -305,7 +314,7 @@ public void onClick(View v) { webview = new WebView((Context) cordova.getActivity()); webview.getSettings().setJavaScriptEnabled(true); webview.getSettings().setBuiltInZoomControls(true); - WebViewClient client = new ChildBrowserClient(ctx, edittext); + WebViewClient client = new ChildBrowserClient(cordova, edittext); webview.setWebViewClient(client); webview.loadUrl(url); webview.setId(5); @@ -359,10 +368,8 @@ private Bitmap loadDrawable(String filename) throws java.io.IOException { * @param obj a JSONObject contain event payload information */ private void sendUpdate(JSONObject obj, boolean keepCallback) { - if (this.browserCallbackId != null) { - PluginResult result = new PluginResult(PluginResult.Status.OK, obj); - result.setKeepCallback(keepCallback); - this.success(result, this.browserCallbackId); + if (this.callbackContext != null) { + callbackContext.success(obj); } } From 06348815ac3a4b9bc5e028ae78e2d9db3fe9cdf1 Mon Sep 17 00:00:00 2001 From: "behrooz shabani (everplays)" Date: Fri, 11 Oct 2013 14:53:07 +0330 Subject: [PATCH 3/8] it's plugins.ChildBrowser not plugins.childBrowser --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5471f78..94ddee1 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ As with most Cordova/PhoneGap APIs, functionality is not available until the should be included _after_ the `phonegap.js` file. All functions are called on the singleton ChildBrowser instance - accessible -as `window.plugins.childBrowser`. +as `window.plugins.ChildBrowser`. ### Methods @@ -64,7 +64,7 @@ Available options: Example: - window.plugins.childBrowser.showWebPage('http://www.google.com', + window.plugins.ChildBrowser.showWebPage('http://www.google.com', { showLocationBar: true }); #### close @@ -75,7 +75,7 @@ Closes the ChildBrowser. Example: - window.plugins.childBrowser.close(); + window.plugins.ChildBrowser.close(); #### openExternal @@ -86,12 +86,12 @@ browser will be a PhoneGap-enabled webview Example: - window.plugins.childBrowser.openExternal('http://www.google.com'); + window.plugins.ChildBrowser.openExternal('http://www.google.com'); ### Events All events can be subscribed to by assigning a function to -`window.plugins.childBrowser['on' + eventName]`; see examples below +`window.plugins.ChildBrowser['on' + eventName]`; see examples below #### close @@ -99,7 +99,7 @@ Called when the ChildBrowser has been closed Example: - window.plugins.childBrowser.onClose = function () { + window.plugins.ChildBrowser.onClose = function () { alert('childBrowser has closed'); }; @@ -111,7 +111,7 @@ loaded. Example: - window.plugins.childBrowser.onLocationChange = function (url) { + window.plugins.ChildBrowser.onLocationChange = function (url) { alert('childBrowser has loaded ' + url); }; @@ -122,7 +122,7 @@ Example: Example: - window.plugins.childBrowser.onOpenExternal = function () { + window.plugins.ChildBrowser.onOpenExternal = function () { alert('opening Mobile Safari'); }; From 40c5cd31e52fd66d7be02b53790c0b40c05cf3d9 Mon Sep 17 00:00:00 2001 From: "behrooz shabani (everplays)" Date: Fri, 11 Oct 2013 16:43:32 +0330 Subject: [PATCH 4/8] added copyright --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 94ddee1..ecc1d76 100644 --- a/README.md +++ b/README.md @@ -145,3 +145,5 @@ Copyright (c) 2011, IBM Corporation Copyright (c) 2010 Jesse MacFadyen, Nitobi Copyright (c) 2012 Randy McMillan + +Copyright (c) 2013 Takhfifan From 653851c0564a7a86b31db3061c3fc7eb3f4a2737 Mon Sep 17 00:00:00 2001 From: "behrooz shabani (everplays)" Date: Wed, 16 Oct 2013 20:02:14 +0330 Subject: [PATCH 5/8] fixed callback issues --- src/android/ChildBrowser.java | 16 +++++++++++----- www/childbrowser.js | 16 ++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/android/ChildBrowser.java b/src/android/ChildBrowser.java index eaf7bdb..22133a7 100644 --- a/src/android/ChildBrowser.java +++ b/src/android/ChildBrowser.java @@ -38,6 +38,7 @@ import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaInterface; +import org.apache.cordova.PluginResult; import org.apache.cordova.CallbackContext; public class ChildBrowser extends CordovaPlugin { @@ -75,12 +76,12 @@ public boolean execute(String action, JSONArray args, final CallbackContext call if (action.equals("showWebPage")) { Log.d(LOG_TAG, args.getString(0)); Log.d(LOG_TAG, action); - this.callbackContext = callbackContext; // If the ChildBrowser is already open then throw an error if (dialog != null && dialog.isShowing()) { callbackContext.error("ChildBrowser is already open"); } else { + this.callbackContext = callbackContext; result = this.showWebPage(args.getString(0), args.optJSONObject(1)); if (result.length() > 0) { @@ -88,7 +89,7 @@ public boolean execute(String action, JSONArray args, final CallbackContext call } else { JSONObject obj = new JSONObject(); obj.put("type", BROWSER_OPENED); - callbackContext.success(obj); + sendUpdate(obj, true); } Log.d(LOG_TAG, result); } @@ -99,7 +100,7 @@ public boolean execute(String action, JSONArray args, final CallbackContext call JSONObject obj = new JSONObject(); obj.put("type", CLOSE_EVENT); - callbackContext.success(obj); + sendUpdate(obj, false); return true; } else if (action.equals("openExternal")) { result = this.openExternal(args.getString(0), args.optBoolean(1)); @@ -108,7 +109,7 @@ public boolean execute(String action, JSONArray args, final CallbackContext call } else { JSONObject obj = new JSONObject(); obj.put("type", OPEN_EXTERNAL_EVENT); - callbackContext.success(obj); + sendUpdate(obj, true); } return true; } @@ -369,7 +370,12 @@ private Bitmap loadDrawable(String filename) throws java.io.IOException { */ private void sendUpdate(JSONObject obj, boolean keepCallback) { if (this.callbackContext != null) { - callbackContext.success(obj); + PluginResult pr = new PluginResult(PluginResult.Status.OK, obj); + pr.setKeepCallback(keepCallback); + callbackContext.sendPluginResult(pr); + Log.d("ChildBrowser", "sent plugin result via callbackContext"); + } else { + Log.d("ChildBrowser", "callbackContext is null :|"); } } diff --git a/www/childbrowser.js b/www/childbrowser.js index e54a0e0..b285210 100644 --- a/www/childbrowser.js +++ b/www/childbrowser.js @@ -25,18 +25,18 @@ var CLOSE_EVENT = 0, function onEvent(data) { switch (data.type) { case CLOSE_EVENT: - if (isFunction(ChildBrowser.onClose)) { - ChildBrowser.onClose(); + if (isFunction(window.plugins.ChildBrowser.onClose)) { + window.plugins.ChildBrowser.onClose(); } break; case LOCATION_CHANGED_EVENT: - if (isFunction(ChildBrowser.onLocationChange)) { - ChildBrowser.onLocationChange(data.location); + if (isFunction(window.plugins.ChildBrowser.onLocationChange)) { + window.plugins.ChildBrowser.onLocationChange(data.location); } break; case OPEN_EXTERNAL_EVENT: - if (isFunction(ChildBrowser.onOpenExternal)) { - ChildBrowser.onOpenExternal(); + if (isFunction(window.plugins.ChildBrowser.onOpenExternal)) { + window.plugins.ChildBrowser.onOpenExternal(); } break; } @@ -46,8 +46,8 @@ function onEvent(data) { * Function called when the child browser has an error. */ function onError(data) { - if (isFunction(ChildBrowser.onError)) { - ChildBrowser.onError(data); + if (isFunction(window.plugins.ChildBrowser.onError)) { + window.plugins.ChildBrowser.onError(data); } } From e69d868121ccb202bc32790620bb6be34bdf3199 Mon Sep 17 00:00:00 2001 From: "behrooz shabani (everplays)" Date: Wed, 16 Oct 2013 20:03:23 +0330 Subject: [PATCH 6/8] webview methods should be called within same thread --- src/android/ChildBrowser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/ChildBrowser.java b/src/android/ChildBrowser.java index 22133a7..e837752 100644 --- a/src/android/ChildBrowser.java +++ b/src/android/ChildBrowser.java @@ -154,7 +154,6 @@ public String openExternal(String url, boolean usePhoneGap) { */ private void closeDialog() { if (dialog != null) { - this.webview.stopLoading(); dialog.dismiss(); } } @@ -225,6 +224,7 @@ public void run() { dialog.setCancelable(true); dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { public void onDismiss(DialogInterface dialog) { + webview.stopLoading(); try { JSONObject obj = new JSONObject(); obj.put("type", CLOSE_EVENT); From 764ae9813582be81ced0480f896534b7937284a9 Mon Sep 17 00:00:00 2001 From: "behrooz shabani (everplays)" Date: Wed, 16 Oct 2013 20:50:22 +0330 Subject: [PATCH 7/8] fixes ERROR: Method 'close:' not defined in Plugin --- src/ios/ChildBrowserCommand.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/ChildBrowserCommand.m b/src/ios/ChildBrowserCommand.m index bab482d..a339f56 100644 --- a/src/ios/ChildBrowserCommand.m +++ b/src/ios/ChildBrowserCommand.m @@ -51,7 +51,7 @@ - (void) showWebPage:(CDVInvokedUrlCommand*)command { [childBrowser showNavigationBar:[[options objectForKey:@"showNavigationBar"] boolValue]]; } --(void) close:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options // args: url +-(void) close:(CDVInvokedUrlCommand*)command { [self.childBrowser closeBrowser]; From 40a1b62634e9a2347945705844389c425aff5926 Mon Sep 17 00:00:00 2001 From: "behrooz shabani (everplays)" Date: Wed, 16 Oct 2013 20:54:22 +0330 Subject: [PATCH 8/8] added copyright note --- src/ios/ChildBrowserCommand.m | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ios/ChildBrowserCommand.m b/src/ios/ChildBrowserCommand.m index a339f56..66712ed 100644 --- a/src/ios/ChildBrowserCommand.m +++ b/src/ios/ChildBrowserCommand.m @@ -4,6 +4,7 @@ // Copyright (c) 2011, IBM Corporation // Copyright 2011, Randy McMillan // Copyright 2012, Andrew Lunny, Adobe Systems +// Copyright 2013, Behrooz Shabani, Takhfifan // #import "ChildBrowserCommand.h"