You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2012/09/18 19:52:51 UTC

[12/14] android commit: Abstract JS->Native API calls into a class.

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.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/65a397fb
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/65a397fb
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/65a397fb

Branch: refs/heads/master
Commit: 65a397fb630a63ec6be8d3a020482a5abfff7fcb
Parents: 0a66907
Author: Andrew Grieve <ag...@chromium.org>
Authored: Thu Sep 6 11:35:09 2012 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Tue Sep 18 13:24:36 2012 -0400

----------------------------------------------------------------------
 .../org/apache/cordova/CordovaChromeClient.java    |   10 +--
 .../src/org/apache/cordova/CordovaWebView.java     |   23 ++-----
 framework/src/org/apache/cordova/ExposedJsApi.java |   53 +++++++++++++++
 3 files changed, 62 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/65a397fb/framework/src/org/apache/cordova/CordovaChromeClient.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaChromeClient.java b/framework/src/org/apache/cordova/CordovaChromeClient.java
index 4fe56ad..747265a 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

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/65a397fb/framework/src/org/apache/cordova/CordovaWebView.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java
index c9631bd..569ce01 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");
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/65a397fb/framework/src/org/apache/cordova/ExposedJsApi.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/ExposedJsApi.java b/framework/src/org/apache/cordova/ExposedJsApi.java
new file mode 100755
index 0000000..92aff9a
--- /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();
+    }
+}