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 5968109..4c9f592 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,51 +1,51 @@ - + Child Browser + Child Browser Plugin + Apache 2.0 + phonegap,browser + + + + - - + - - - - - - - - + + + + - + + - + + + + + - - + + - - + + - - + + 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.h b/src/ios/ChildBrowserCommand.h index 954341a..3ffe0ac 100644 --- a/src/ios/ChildBrowserCommand.h +++ b/src/ios/ChildBrowserCommand.h @@ -17,7 +17,7 @@ @property (nonatomic, strong) NSNumber *LOCATION_CHANGE_EVENT; @property (nonatomic, strong) NSNumber *OPEN_EXTERNAL_EVENT; --(void) showWebPage:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; +-(void) showWebPage:(CDVInvokedUrlCommand*)command; -(void) onChildLocationChange:(NSString*)newLoc; -(NSDictionary*) dictionaryForEvent:(NSNumber*)event; diff --git a/src/ios/ChildBrowserCommand.m b/src/ios/ChildBrowserCommand.m index 057894a..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" @@ -24,26 +25,22 @@ - (id) initWithWebView:(UIWebView*)theWebView return self; } -- (void) showWebPage:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options // args: url -{ - self.callbackId = [arguments objectAtIndex:0]; - +- (void) showWebPage:(CDVInvokedUrlCommand*)command { + self.callbackId = command.callbackId; + if (self.childBrowser == nil) { -#if __has_feature(objc_arc) self.childBrowser = [[ChildBrowserViewController alloc] initWithScale:NO]; -#else - self.childBrowser = [[[ChildBrowserViewController alloc] initWithScale:NO] autorelease]; -#endif self.childBrowser.delegate = self; self.childBrowser.orientationDelegate = self.viewController; } + NSMutableDictionary* options = (NSMutableDictionary*)[command argumentAtIndex:1]; NSLog(@"showLocationBar %d",(int)[[options objectForKey:@"showLocationBar"] boolValue]); [self.viewController presentModalViewController:self.childBrowser animated:YES]; // objectAtIndex 0 is the callback id - NSString *url = (NSString*) [arguments objectAtIndex:1]; + NSString* url = (NSString*) [command.arguments objectAtIndex:0]; [self.childBrowser resetControls]; [self.childBrowser loadURL:url]; @@ -55,7 +52,7 @@ - (void) showWebPage:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)o [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/src/ios/ChildBrowserViewController.m b/src/ios/ChildBrowserViewController.m index e6cd29f..17517bb 100644 --- a/src/ios/ChildBrowserViewController.m +++ b/src/ios/ChildBrowserViewController.m @@ -98,20 +98,6 @@ - (void)dealloc { self.webView.delegate = nil; self.delegate = nil; self.orientationDelegate = nil; - -#if !__has_feature(objc_arc) - [self.webView release]; - [self.closeBtn release]; - [self.refreshBtn release]; - [self.addressLabel release]; - [self.backBtn release]; - [self.fwdBtn release]; - [self.safariBtn release]; - [self.spinner release]; - [self.toolbar release]; - - [super dealloc]; -#endif } -(void)closeBrowser @@ -149,7 +135,7 @@ -(IBAction) onSafariButtonPress:(id)sender if(isImage) { - NSURL* pURL = [[ [NSURL alloc] initWithString:imageURL ] autorelease]; + NSURL* pURL = [ [NSURL alloc] initWithString:imageURL ]; [ [ UIApplication sharedApplication ] openURL:pURL ]; } else @@ -284,7 +270,6 @@ - (void)addGestureRecognizer closeRG.direction = UISwipeGestureRecognizerDirectionLeft; closeRG.delegate=self; [self.view addGestureRecognizer:closeRG]; - [closeRG release]; } //- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer diff --git a/www/childbrowser.js b/www/childbrowser.js index 5ff4c2c..b285210 100644 --- a/www/childbrowser.js +++ b/www/childbrowser.js @@ -8,112 +8,104 @@ * (c) 2010 Jesse MacFadyen, Nitobi */ -var ChildBrowser = (function (gap) { - function isFunction(f) { - return typeof f === "function"; - } +function isFunction(arg) { + return typeof arg === "function"; +} - // placeholder and constants - function ChildBrowser() {} +// placeholder and constants +function ChildBrowser() {} - var CLOSE_EVENT = 0, - LOCATION_CHANGED_EVENT = 1, - OPEN_EXTERNAL_EVENT = 2; +var CLOSE_EVENT = 0, + LOCATION_CHANGED_EVENT = 1, + OPEN_EXTERNAL_EVENT = 2; - /** - * Function called when the child browser has an event. - */ - function onEvent(data) { - switch (data.type) { - case CLOSE_EVENT: - if (isFunction(ChildBrowser.onClose)) { - ChildBrowser.onClose(); - } - break; - case LOCATION_CHANGED_EVENT: - if (isFunction(ChildBrowser.onLocationChange)) { - ChildBrowser.onLocationChange(data.location); - } - break; - case OPEN_EXTERNAL_EVENT: - if (isFunction(ChildBrowser.onOpenExternal)) { - ChildBrowser.onOpenExternal(); - } - break; - } +/** + * Function called when the child browser has an event. + */ +function onEvent(data) { + switch (data.type) { + case CLOSE_EVENT: + if (isFunction(window.plugins.ChildBrowser.onClose)) { + window.plugins.ChildBrowser.onClose(); + } + break; + case LOCATION_CHANGED_EVENT: + if (isFunction(window.plugins.ChildBrowser.onLocationChange)) { + window.plugins.ChildBrowser.onLocationChange(data.location); + } + break; + case OPEN_EXTERNAL_EVENT: + if (isFunction(window.plugins.ChildBrowser.onOpenExternal)) { + window.plugins.ChildBrowser.onOpenExternal(); + } + break; } +} - /** - * Function called when the child browser has an error. - */ - function onError(data) { - if (isFunction(ChildBrowser.onError)) { - ChildBrowser.onError(data); - } +/** + * Function called when the child browser has an error. + */ +function onError(data) { + if (isFunction(window.plugins.ChildBrowser.onError)) { + window.plugins.ChildBrowser.onError(data); } +} - /** - * Maintain API consistency with iOS - */ - ChildBrowser.install = function () { - console.log('ChildBrowser.install is deprecated'); - }; +/** + * Maintain API consistency with iOS + */ +ChildBrowser.prototype.install = function () { + console.log('ChildBrowser.install is deprecated'); +}; - /** - * Display a new browser with the specified URL. - * This method loads up a new web view in a dialog. - * - * @param url The url to load - * @param options An object that specifies additional options - */ - ChildBrowser.showWebPage = function (url, options) { - if (!options) { - options = { showLocationBar: true }; - } +/** + * Display a new browser with the specified URL. + * This method loads up a new web view in a dialog. + * + * @param url The url to load + * @param options An object that specifies additional options + */ +ChildBrowser.prototype.showWebPage = function (url, options) { + if (!options) { + options = { showLocationBar: true }; + } - gap.exec(onEvent, onError, "ChildBrowser", "showWebPage", [url, options]); - }; + cordova.exec(onEvent, onError, "ChildBrowser", "showWebPage", [url, options]); +}; - /** - * Close the browser opened by showWebPage. - */ - ChildBrowser.close = function () { - gap.exec(null, null, "ChildBrowser", "close", []); - }; +/** + * Close the browser opened by showWebPage. + */ +ChildBrowser.prototype.close = function () { + cordova.exec(null, null, "ChildBrowser", "close", []); +}; - /** - * Display a new browser with the specified URL. - * This method starts a new web browser activity. - * - * @param url The url to load - * @param usePhoneGap Load url in PhoneGap webview [optional] - */ - ChildBrowser.openExternal = function(url, usePhoneGap) { - if (device.platform.toLowerCase() == 'android') { - if (usePhoneGap) { - navigator.app.loadUrl(url); - } else { - gap.exec(null, null, "ChildBrowser", "openExternal", [url, usePhoneGap]); - } +/** + * Display a new browser with the specified URL. + * This method starts a new web browser activity. + * + * @param url The url to load + * @param usePhoneGap Load url in PhoneGap webview [optional] + */ +ChildBrowser.prototype.openExternal = function(url, usePhoneGap) { + if (device.platform.toLowerCase() == 'android') { + if (usePhoneGap) { + navigator.app.loadUrl(url); } else { - ChildBrowser.showWebPage(url); - }; + cordova.exec(null, null, "ChildBrowser", "openExternal", [url, usePhoneGap]); + } + } else { + ChildBrowser.showWebPage(url); }; +}; - /** - * Load ChildBrowser - */ - gap.addConstructor(function () { - if (gap.addPlugin) { - gap.addPlugin("childBrowser", ChildBrowser); - } else { - if (!window.plugins) { - window.plugins = {}; - } - - window.plugins.childBrowser = ChildBrowser; - } - }); +/** + * Load ChildBrowser + */ +cordova.addConstructor(function () { + if (!window.plugins) { + window.plugins = {}; + } - return ChildBrowser; -})(window.cordova || window.Cordova || window.PhoneGap); + window.plugins.ChildBrowser = new ChildBrowser(); +});