Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -75,7 +75,7 @@ Closes the ChildBrowser.

Example:

window.plugins.childBrowser.close();
window.plugins.ChildBrowser.close();

#### openExternal

Expand All @@ -86,20 +86,20 @@ 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

Called when the ChildBrowser has been closed

Example:

window.plugins.childBrowser.onClose = function () {
window.plugins.ChildBrowser.onClose = function () {
alert('childBrowser has closed');
};

Expand All @@ -111,7 +111,7 @@ loaded.

Example:

window.plugins.childBrowser.onLocationChange = function (url) {
window.plugins.ChildBrowser.onLocationChange = function (url) {
alert('childBrowser has loaded ' + url);
};

Expand All @@ -122,7 +122,7 @@ Example:

Example:

window.plugins.childBrowser.onOpenExternal = function () {
window.plugins.ChildBrowser.onOpenExternal = function () {
alert('opening Mobile Safari');
};

Expand All @@ -145,3 +145,5 @@ Copyright (c) 2011, IBM Corporation
Copyright (c) 2010 Jesse MacFadyen, Nitobi

Copyright (c) 2012 Randy McMillan

Copyright (c) 2013 Takhfifan
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</config-file>

<config-file target="res/xml/config.xml" parent="/*">
<feature name="WaitingDialog" >
<feature name="ChildBrowser" >
<param name="android-package" value="com.phonegap.plugins.childBrowser.ChildBrowser"/>
</feature>
</config-file>
Expand Down
107 changes: 60 additions & 47 deletions src/android/ChildBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -144,7 +154,6 @@ public String openExternal(String url, boolean usePhoneGap) {
*/
private void closeDialog() {
if (dialog != null) {
this.webview.stopLoading();
dialog.dismiss();
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 :|");
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/ios/ChildBrowserCommand.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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];

Expand Down
16 changes: 8 additions & 8 deletions www/childbrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
}

Expand Down