Permalink
Browse files

Add ChildBrowser plugin.

The ChildBrowser plugin exhibits bundling non source resources
(images) with a Cordova plugin. The images (toolbar icons) are
bundled in the native source directory.
  • Loading branch information...
1 parent c82a2af commit 4183c59d4ba422ef070a6cbfdacc4c55ff30edc7 @deedubbu committed Apr 27, 2012
@@ -0,0 +1,94 @@
+// file: lib/common/plugin/childbrowser.js
+cordova.define("cordova/plugin/childbrowser", function(require, exports, module) {
+ var cordova = require('cordova'),
+ exec = require('cordova/exec');
+
+ /**
+ * Constructor
+ */
+ var ChildBrowser = function() {
+ };
+
+ ChildBrowser.CLOSE_EVENT = 0;
+ ChildBrowser.LOCATION_CHANGED_EVENT = 1;
+
+ /**
+ * Display a new browser with the specified URL. This method loads up a new
+ * custom browser field.
+ *
+ * @param url
+ * The url to load
+ * @param options
+ * An object that specifies additional options
+ */
+ ChildBrowser.prototype.showWebPage = function(url, options) {
+ if (options === null || options === "undefined") {
+ var options = new Object();
+ options.showLocationBar = true;
+ }
+ exec(this._onEvent, this._onError, "ChildBrowser",
+ "showWebPage", [url, options ]);
+ };
+
+ /**
+ * Close the browser opened by showWebPage.
+ */
+ ChildBrowser.prototype.close = function() {
+ exec(null, this._onError, "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 useCordova
+ * Load url in Cordova webview [ignored]
+ */
+ ChildBrowser.prototype.openExternal = function(url, useCordova) {
+ // if (useCordova === true) {
+ // navigator.app.loadUrl(url);
+ // } else {
+ exec(null, null, "ChildBrowser", "openExternal", [ url,
+ useCordova ]);
+ // }
+ };
+
+ /**
+ * Method called when the child browser is closed.
+ */
+ ChildBrowser.prototype._onEvent = function(data) {
+ if (data.type == ChildBrowser.CLOSE_EVENT
+ && typeof window.plugins.childBrowser.onClose === "function") {
+ window.plugins.childBrowser.onClose();
+ }
+ if (data.type == ChildBrowser.LOCATION_CHANGED_EVENT
+ && typeof window.plugins.childBrowser.onLocationChange === "function") {
+ window.plugins.childBrowser.onLocationChange(data.location);
+ }
+ };
+
+ /**
+ * Method called when the child browser has an error.
+ */
+ ChildBrowser.prototype._onError = function(data) {
+ if (typeof window.plugins.childBrowser.onError === "function") {
+ window.plugins.childBrowser.onError(data);
+ }
+ };
+
+ /**
+ * Maintain API consistency with iOS
+ */
+ ChildBrowser.prototype.install = function() {
+ };
+
+
+ var childbrowser = new ChildBrowser();
+
+ module.exports = childbrowser;
+
+});
+
+cordova.addPlugin('window.plugins.childBrowser', 'cordova/plugin/childbrowser', false);
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<library isWhitelist="true">
+ <extension id="cordova.childbrowser">
+ <entryClass>org.apache.cordova.plugins.childbrowser.ChildBrowser</entryClass>
+ </extension>
+ <platforms>
+ <platform value="JAVA">
+ <target version="default" config="JAVA_XHR" />
+ </platform>
+ </platforms>
+ <configurations>
+ <configuration name="JAVA_XHR" version="1.0" comment="For XHR architecture for Java implementation">
+ <src type="text/java" path="org" comment="API implementation in JavaScript" />
+ <src type="text/javascript" path="js/common/childbrowser.js" comment="JS interface" />
+ </configuration>
+ </configurations>
+ <features>
+ <feature id="cordova.childbrowser" version="1.0.0">Cordova ChildBrowser Extension</feature>
+ </features>
+</library>
@@ -0,0 +1,186 @@
+/*
+ * PhoneGap is available under *either* the terms of the modified BSD license *or* the
+ * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
+ *
+ * Copyright (c) 2005-2011, Nitobi Software Inc.
+ * Copyright (c) 2010-2011, IBM Corporation
+ */
+package org.apache.cordova.plugins.childbrowser;
+
+import net.rim.blackberry.api.browser.Browser;
+import net.rim.blackberry.api.browser.BrowserSession;
+import net.rim.device.api.ui.UiApplication;
+
+import org.apache.cordova.api.Plugin;
+import org.apache.cordova.api.PluginResult;
+import org.apache.cordova.json4j.JSONArray;
+import org.apache.cordova.json4j.JSONException;
+import org.apache.cordova.json4j.JSONObject;
+import org.apache.cordova.util.Logger;
+
+/**
+ * The ChildBrowser plug-in. This class provides the ability to load external
+ * web sites in a custom view or the devices browser. URLs loaded by this
+ * plugin are not restricted by the whitelist.
+ *
+ * showWebPage - Open a URL in a custom browser view.
+ * close - Close a previously opened custom browser.
+ * openExternal - Load a URL in the device's browser.
+ */
+public class ChildBrowser extends Plugin {
+
+ protected final static String TAG = "ChildBrowser: ";
+
+ // Supported actions
+ private final static String ACTION_SHOW_WEBPAGE = "showWebPage";
+ private final static String ACTION_CLOSE = "close";
+ private final static String ACTION_EXTERNAL = "openExternal";
+
+ private String callbackId = null;
+ private static CustomBrowser browser = null;
+
+ private static String[] jsFiles = { "childbrowser.js" };
+
+ public ChildBrowser() {
+ super("cordova.childbrowser", "ChildBrowser", jsFiles);
+ }
+
+ /**
+ * Executes the request and returns PluginResult.
+ *
+ * @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.
+ */
+ public PluginResult execute(String action, JSONArray args, String callbackId) {
+ PluginResult result;
+
+ if (ACTION_SHOW_WEBPAGE.equals(action)) {
+ this.callbackId = callbackId;
+ result = showWebPage(args);
+ } else if (ACTION_CLOSE.equals(action)) {
+ result = closeBrowser();
+ } else if (ACTION_EXTERNAL.equals(action)) {
+ result = openExternal(args);
+ } else {
+ result = new PluginResult(PluginResult.Status.INVALID_ACTION, TAG
+ + "Invalid action: " + action);
+ }
+
+ return result;
+ }
+
+ static synchronized void clearBrowser() {
+ browser = null;
+ }
+
+ /**
+ * Close a custom browser screen.
+ *
+ * @return a PluginResult
+ */
+ private synchronized PluginResult closeBrowser() {
+ JSONObject obj = null;
+
+ if (browser != null && browser.isDisplayed()) {
+ final UiApplication uiApp = UiApplication.getUiApplication();
+ uiApp.invokeAndWait(new Runnable() {
+ public void run() {
+ try {
+ uiApp.popScreen(browser);
+ } catch (IllegalArgumentException e) {
+ Logger.log(ChildBrowser.TAG
+ + ": Caught illegal argument exception: "
+ + e.getMessage());
+ }
+ }
+ });
+ browser = null;
+
+ obj = new JSONObject();
+ try {
+ obj.put("type", CustomBrowser.CLOSE_EVENT);
+ } catch (JSONException e) {
+ return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
+ }
+ }
+
+ return new PluginResult(PluginResult.Status.OK, obj);
+ }
+
+ /**
+ * Load an URL in the device's browser application.
+ *
+ * @param args
+ * JSONArry of arguments for the action.
+ * @return a PluginResult
+ */
+ private PluginResult openExternal(JSONArray args) {
+ try {
+ String url = args.getString(0);
+
+ BrowserSession session = Browser.getDefaultSession();
+ session.displayPage(url);
+ } catch (JSONException e) {
+ return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
+ }
+
+ return new PluginResult(PluginResult.Status.OK, "");
+ }
+
+ /**
+ * Display the specified URL in a custom browser screen.
+ *
+ * @param args
+ * JSONArry of arguments for the action.
+ * @return a PluginResult
+ */
+ private synchronized PluginResult showWebPage(JSONArray args) {
+ PluginResult result;
+
+ if (browser == null) {
+ try {
+ boolean showLocationBar = true;
+ String url = args.getString(0);
+ JSONObject options = args.getJSONObject(1);
+
+ // Determine whether to show or hide navigation bar.
+ if (options != null) {
+ showLocationBar = options.optBoolean("showLocationBar",
+ true);
+ }
+
+ browser = new CustomBrowser(callbackId);
+ if (browser.init(showLocationBar)) {
+ final UiApplication uiApp = UiApplication.getUiApplication();
+ uiApp.invokeLater(new Runnable() {
+ public void run() {
+ uiApp.pushScreen(browser);
+ }
+ });
+
+ browser.loadURL(url);
+
+ // Must keep the callback for browser URL load and close
+ // events.
+ result = new PluginResult(PluginResult.Status.OK, "");
+ result.setKeepCallback(true);
+ } else {
+ result = new PluginResult(PluginResult.Status.ERROR, TAG
+ + "Failed to initialize CustomBrowser.");
+ }
+ } catch (JSONException e) {
+ return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
+ }
+ } else {
+ result = new PluginResult(PluginResult.Status.ERROR,
+ "ChildBrowser is already open.");
+ }
+
+ return result;
+ }
+}
Oops, something went wrong.

0 comments on commit 4183c59

Please sign in to comment.