incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [4/6] js commit: [android] Abstract JS->Native API calls into an interface.
Date Tue, 18 Sep 2012 14:47:24 GMT
[android] Abstract JS->Native API calls into an interface.

setNativeToJsBridgeMode() and poll() can now use the JS interface
exported via addJavascriptInterface if it's available.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/898933d5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/898933d5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/898933d5

Branch: refs/heads/master
Commit: 898933d5ee3d6b45335cd944864718aad5a597e0
Parents: 951136b
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Thu Sep 6 11:32:09 2012 -0400
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Tue Sep 18 10:46:48 2012 -0400

----------------------------------------------------------------------
 lib/android/exec.js                                |   16 +++++++-------
 lib/android/plugin/android/nativeapiprovider.js    |   13 ++++++++++++
 lib/android/plugin/android/polling.js              |    3 +-
 lib/android/plugin/android/promptbasednativeapi.js |   16 +++++++++++++++
 4 files changed, 39 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/898933d5/lib/android/exec.js
----------------------------------------------------------------------
diff --git a/lib/android/exec.js b/lib/android/exec.js
index b9d3ad5..3a836fe 100644
--- a/lib/android/exec.js
+++ b/lib/android/exec.js
@@ -36,6 +36,7 @@
 var cordova = require('cordova'),
     callback = require('cordova/plugin/android/callback'),
     polling = require('cordova/plugin/android/polling'),
+    nativeApiProvider = require('cordova/plugin/android/nativeapiprovider'),
     jsToNativeBridgeMode,
     nativeToJsBridgeMode,
     jsToNativeModes = {
@@ -47,7 +48,7 @@ var cordova = require('cordova'),
         LOCATION_CHANGE: 2
     },
     nativeToJsModes = {
-        // Polls for messages using the prompt() bridge.
+        // Polls for messages using the JS->Native bridge.
         POLLING: 0,
         // Does an XHR to a local server, which will send back messages. This is
         // broken on ICS when a proxy server is configured.
@@ -87,12 +88,10 @@ function androidExec(success, fail, service, action, args) {
 
       if (jsToNativeBridgeMode == jsToNativeModes.LOCATION_CHANGE) {
           window.location = 'http://cdv_exec/' + service + '#' + action + '#' + callbackId
+ '#' + argsJson;
-      } else if (jsToNativeBridgeMode == jsToNativeModes.JS_OBJECT) {
+      } else {
           // Explicit cast to string is required on Android 2.1 to convert from
           // a Java string to a JS string.
-          result = '' + _cordovaExec.exec(service, action, callbackId, argsJson);
-      } else {
-          result = prompt(argsJson, "gap:"+JSON.stringify([service, action, callbackId, true]));
+          result = '' + nativeApiProvider.get().exec(service, action, callbackId, argsJson);
       }
 
       // If a result was returned
@@ -178,10 +177,11 @@ androidExec.jsToNativeModes = jsToNativeModes;
 androidExec.nativeToJsModes = nativeToJsModes;
 
 androidExec.setJsToNativeBridgeMode = function(mode) {
-    if (mode == jsToNativeModes.JS_OBJECT && !window._cordovaExec) {
-        console.log('Falling back on PROMPT mode since _cordovaExec is missing.');
+    if (mode == jsToNativeModes.JS_OBJECT && !window._cordovaNative) {
+        console.log('Falling back on PROMPT mode since _cordovaNative is missing.');
         mode = jsToNativeModes.PROMPT;
     }
+    nativeApiProvider.setPreferPrompt(mode == jsToNativeModes.PROMPT);
     jsToNativeBridgeMode = mode;
 };
 
@@ -197,7 +197,7 @@ androidExec.setNativeToJsBridgeMode = function(mode) {
 
     nativeToJsBridgeMode = mode;
     // Tell the native side to switch modes.
-    prompt(mode, "gap_bridge_mode:");
+    nativeApiProvider.get().setNativeToJsBridgeMode(mode);
 
     if (mode == nativeToJsModes.POLLING) {
         polling.start();

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/898933d5/lib/android/plugin/android/nativeapiprovider.js
----------------------------------------------------------------------
diff --git a/lib/android/plugin/android/nativeapiprovider.js b/lib/android/plugin/android/nativeapiprovider.js
new file mode 100644
index 0000000..43ae53d
--- /dev/null
+++ b/lib/android/plugin/android/nativeapiprovider.js
@@ -0,0 +1,13 @@
+/**
+ * Exports the ExposedJsApi.java object if available, otherwise exports the PromptBasedNativeApi.
+ */
+
+var nativeApi = this._cordovaNative || require('cordova/plugin/android/promptbasednativeapi');
+var currentApi = nativeApi;
+
+module.exports = {
+    get: function() { return currentApi },
+    setPreferPrompt: function(value) {
+        currentApi = value ? require('cordova/plugin/android/promptbasednativeapi') : nativeApi;
+    }
+};

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/898933d5/lib/android/plugin/android/polling.js
----------------------------------------------------------------------
diff --git a/lib/android/plugin/android/polling.js b/lib/android/plugin/android/polling.js
index 95bca07..b3b99bb 100644
--- a/lib/android/plugin/android/polling.js
+++ b/lib/android/plugin/android/polling.js
@@ -20,11 +20,12 @@
 */
 
 var cordova = require('cordova'),
+    nativeApiProvider = require('cordova/plugin/android/nativeapiprovider'),
     POLL_INTERVAL = 50,
     enabled = false;
 
 function pollOnce() {
-    var msg = prompt("", "gap_poll:");
+    var msg = nativeApiProvider.get().retrieveJsMessages();
     if (msg) {
         try {
             eval(""+msg);

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/898933d5/lib/android/plugin/android/promptbasednativeapi.js
----------------------------------------------------------------------
diff --git a/lib/android/plugin/android/promptbasednativeapi.js b/lib/android/plugin/android/promptbasednativeapi.js
new file mode 100644
index 0000000..e89084d
--- /dev/null
+++ b/lib/android/plugin/android/promptbasednativeapi.js
@@ -0,0 +1,16 @@
+/**
+ * Implements the API of ExposedJsApi.java, but uses prompt() to communicate.
+ * This is used only on the 2.3 simulator, where addJavascriptInterface() is broken.
+ */
+
+module.exports = {
+    exec: function(service, action, callbackId, argsJson) {
+        return prompt(argsJson, 'gap:'+JSON.stringify([service, action, callbackId]));
+    },
+    setNativeToJsBridgeMode: function(value) {
+        prompt(value, 'gap_bridge_mode:');
+    },
+    retrieveJsMessages: function() {
+        return prompt('', 'gap_poll:');
+    }
+};


Mime
View raw message