diff --git a/README.md b/README.md index 5471f78..ecc1d76 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'); }; @@ -145,3 +145,5 @@ Copyright (c) 2011, IBM Corporation Copyright (c) 2010 Jesse MacFadyen, Nitobi Copyright (c) 2012 Randy McMillan + +Copyright (c) 2013 Takhfifan diff --git a/plugin.xml b/plugin.xml index a8e5d7c..4c9f592 100644 --- a/plugin.xml +++ b/plugin.xml @@ -23,7 +23,7 @@ - + diff --git a/src/android/ChildBrowser.java b/src/android/ChildBrowser.java index c63017f..e837752 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,20 @@ 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.PluginResult; +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 +59,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); + // 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) { - 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); + sendUpdate(obj, true); } + Log.d(LOG_TAG, result); + } + return true; + } else if (action.equals("close")) { + closeDialog(); + + JSONObject obj = new JSONObject(); + obj.put("type", CLOSE_EVENT); + + sendUpdate(obj, false); + 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); + sendUpdate(obj, true); } - return new PluginResult(status, result); - } catch (JSONException e) { - return new PluginResult(PluginResult.Status.JSON_EXCEPTION); + return true; } + return false; } /** @@ -144,7 +154,6 @@ public String openExternal(String url, boolean usePhoneGap) { */ private void closeDialog() { if (dialog != null) { - this.webview.stopLoading(); dialog.dismiss(); } } @@ -215,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); @@ -305,7 +315,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 +369,13 @@ 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) { + 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/src/ios/ChildBrowserCommand.m b/src/ios/ChildBrowserCommand.m index bab482d..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" @@ -51,7 +52,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]; 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); } }