From 65a397fb630a63ec6be8d3a020482a5abfff7fcb Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Thu, 6 Sep 2012 11:35:09 -0400 Subject: [PATCH] Abstract JS->Native API calls into a class. -setNativeToJsBridgeMode() and poll() can now be used via the JS interface exported via addJavascriptInterface. -prompt() now forwards calls to this class so that the logic will be the same whether prompt() or the JS object is used. --- .../apache/cordova/CordovaChromeClient.java | 10 ++-- .../org/apache/cordova/CordovaWebView.java | 23 +++----- .../src/org/apache/cordova/ExposedJsApi.java | 53 +++++++++++++++++++ 3 files changed, 62 insertions(+), 24 deletions(-) create mode 100755 framework/src/org/apache/cordova/ExposedJsApi.java diff --git a/framework/src/org/apache/cordova/CordovaChromeClient.java b/framework/src/org/apache/cordova/CordovaChromeClient.java index 4fe56ad1..747265a9 100755 --- a/framework/src/org/apache/cordova/CordovaChromeClient.java +++ b/framework/src/org/apache/cordova/CordovaChromeClient.java @@ -202,9 +202,7 @@ public class CordovaChromeClient extends WebChromeClient { String service = array.getString(0); String action = array.getString(1); String callbackId = array.getString(2); - boolean async = array.getBoolean(3); - PluginResult r = this.appView.pluginManager.exec(service, action, callbackId, message, async); - result.confirm(r == null ? "" : r.getJSONString()); + result.confirm(this.appView.exposedJsApi.exec(service, action, callbackId, message)); } catch (JSONException e) { e.printStackTrace(); } @@ -212,15 +210,13 @@ public class CordovaChromeClient extends WebChromeClient { // Sets the native->JS bridge mode. else if (reqOk && defaultValue != null && defaultValue.equals("gap_bridge_mode:")) { - this.appView.jsMessageQueue.setBridgeMode(Integer.parseInt(message)); + this.appView.exposedJsApi.setNativeToJsBridgeMode(Integer.parseInt(message)); result.confirm(""); } // Polling for JavaScript messages else if (reqOk && defaultValue != null && defaultValue.equals("gap_poll:")) { - // TODO(agrieve): Use popAll() here. - String r = this.appView.jsMessageQueue.pop(); - result.confirm(r); + result.confirm(this.appView.exposedJsApi.retrieveJsMessages()); } // Do NO-OP so older code doesn't display dialog diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java index c9631bd2..569ce017 100755 --- a/framework/src/org/apache/cordova/CordovaWebView.java +++ b/framework/src/org/apache/cordova/CordovaWebView.java @@ -96,6 +96,7 @@ public class CordovaWebView extends WebView { private boolean handleButton = false; NativeToJsMessageQueue jsMessageQueue; + ExposedJsApi exposedJsApi; /** * Constructor. @@ -205,8 +206,6 @@ public class CordovaWebView extends WebView { @SuppressWarnings("deprecation") @SuppressLint("NewApi") private void setup() { - jsMessageQueue = new NativeToJsMessageQueue(this, cordova); - this.setInitialScale(0); this.setVerticalScrollBarEnabled(false); this.requestFocusFromTouch(); @@ -252,14 +251,10 @@ public class CordovaWebView extends WebView { this.cordova.getActivity().registerReceiver(this.receiver, intentFilter); } // end CB-1405 - - //Start up the plugin manager - try { - this.pluginManager = new PluginManager(this, this.cordova); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + + pluginManager = new PluginManager(this, this.cordova); + jsMessageQueue = new NativeToJsMessageQueue(this, cordova); + exposedJsApi = new ExposedJsApi(pluginManager, jsMessageQueue); exposeJsInterface(); } @@ -273,13 +268,7 @@ public class CordovaWebView extends WebView { Log.i(TAG, "Disabled addJavascriptInterface() bridge callback due to a bug on the 2.3 emulator"); return; } - this.addJavascriptInterface(new Object() { - @SuppressWarnings("unused") - public String exec(String service, String action, String callbackId, String arguments) throws JSONException { - PluginResult r = pluginManager.exec(service, action, callbackId, arguments, true /* async */); - return r == null ? "" : r.getJSONString(); - } - }, "_cordovaExec"); + this.addJavascriptInterface(exposedJsApi, "_cordovaNative"); } /** diff --git a/framework/src/org/apache/cordova/ExposedJsApi.java b/framework/src/org/apache/cordova/ExposedJsApi.java new file mode 100755 index 00000000..92aff9af --- /dev/null +++ b/framework/src/org/apache/cordova/ExposedJsApi.java @@ -0,0 +1,53 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ +package org.apache.cordova; + +import org.apache.cordova.api.PluginManager; +import org.apache.cordova.api.PluginResult; +import org.json.JSONException; + +/** + * Contains APIs that the JS can call. All functions in here should also have + * an equivalent entry in CordovaChromeClient.java, and be added to + * cordova-js/lib/android/plugin/android/promptbasednativeapi.js + */ +/* package */ class ExposedJsApi { + + private PluginManager pluginManager; + private NativeToJsMessageQueue jsMessageQueue; + + public ExposedJsApi(PluginManager pluginManager, NativeToJsMessageQueue jsMessageQueue) { + this.pluginManager = pluginManager; + this.jsMessageQueue = jsMessageQueue; + } + + public String exec(String service, String action, String callbackId, String arguments) throws JSONException { + PluginResult r = pluginManager.exec(service, action, callbackId, arguments, true /* async */); + return r == null ? "" : r.getJSONString(); + } + + public void setNativeToJsBridgeMode(int value) { + jsMessageQueue.setBridgeMode(value); + } + + public String retrieveJsMessages() { + // TODO(agrieve): Use popAll() here. + return jsMessageQueue.pop(); + } +}