incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [39/50] [abbrv] js commit: [android] Implement online events based Native->JS bridge.
Date Fri, 24 Aug 2012 21:57:39 GMT
[android] Implement online events based Native->JS bridge.


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/0d43c0b6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/0d43c0b6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/0d43c0b6

Branch: refs/heads/master
Commit: 0d43c0b62078b4909f4d5c718903bd14aac0b8ab
Parents: e472851
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Tue Aug 21 14:09:40 2012 -0400
Committer: Anis Kadri <anis.kadri@gmail.com>
Committed: Fri Aug 24 13:49:59 2012 -0700

----------------------------------------------------------------------
 lib/android/exec.js                   |   24 ++++++++++++++++++++++--
 lib/android/plugin/android/polling.js |   27 +++++++++++++++++----------
 2 files changed, 39 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0d43c0b6/lib/android/exec.js
----------------------------------------------------------------------
diff --git a/lib/android/exec.js b/lib/android/exec.js
index 43bcd5f..4c300c5 100644
--- a/lib/android/exec.js
+++ b/lib/android/exec.js
@@ -23,10 +23,18 @@ var cordova = require('cordova'),
         LOCATION_CHANGE: 2  // Not yet implemented
     },
     nativeToJsModes = {
+        // Polls for messages using the prompt() 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.
         HANGING_GET: 1,
-        LOAD_URL: 2,  // Not yet implemented
-        ONLINE_EVENT: 3,  // Not yet implemented
+        // For LOAD_URL to be viable, it would need to have a work-around for
+        // the bug where the soft-keyboard gets dismissed when a message is sent.
+        LOAD_URL: 2,
+        // For the ONLINE_EVENT to be viable, it would need to intercept all event
+        // listeners (both through addEventListener and window.ononline) as well
+        // as set the navigator property itself.
+        ONLINE_EVENT: 3,
         PRIVATE_API: 4  // Not yet implemented
     };
 
@@ -105,6 +113,10 @@ function androidExec(success, fail, service, action, args) {
     }
 };
 
+function onOnLineEvent(e) {
+    while (polling.pollOnce());
+}
+
 androidExec.jsToNativeModes = jsToNativeModes;
 androidExec.nativeToJsModes = nativeToJsModes;
 
@@ -124,14 +136,22 @@ androidExec.setNativeToJsBridgeMode = function(mode) {
         polling.stop();
     } else if (nativeToJsBridgeMode == nativeToJsModes.HANGING_GET) {
         callback.stop();
+    } else if (nativeToJsBridgeMode == nativeToJsModes.ONLINE_EVENT) {
+        window.removeEventListener('online', onOnLineEvent, false);
+        window.removeEventListener('offline', onOnLineEvent, false);
     }
+
     nativeToJsBridgeMode = mode;
     // Tell the native side to switch modes.
     prompt(mode, "gap_bridge_mode:");
+
     if (mode == nativeToJsModes.POLLING) {
         polling.start();
     } else if (mode == nativeToJsModes.HANGING_GET) {
         callback.start();
+    } else if (mode == nativeToJsModes.ONLINE_EVENT) {
+        window.addEventListener('online', onOnLineEvent, false);
+        window.addEventListener('offline', onOnLineEvent, false);
     }
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0d43c0b6/lib/android/plugin/android/polling.js
----------------------------------------------------------------------
diff --git a/lib/android/plugin/android/polling.js b/lib/android/plugin/android/polling.js
index 406dc62..71b7c37 100644
--- a/lib/android/plugin/android/polling.js
+++ b/lib/android/plugin/android/polling.js
@@ -1,12 +1,8 @@
 var cordova = require('cordova'),
-    period = 50,
+    POLL_INTERVAL = 50,
     enabled = false;
 
-
-function doPoll() {
-    if (!enabled) {
-        return;
-    }
+function pollOnce() {
     var msg = prompt("", "gap_poll:");
     if (msg) {
         try {
@@ -16,10 +12,20 @@ function doPoll() {
             console.log("JSCallbackPolling: Message from Server: " + msg);
             console.log("JSCallbackPolling Error: "+e);
         }
-        setTimeout(doPoll, 1);
-    } else {
-        setTimeout(doPoll, period);
+        return true;
     }
+    return false;
+}
+
+function doPoll() {
+    if (!enabled) {
+        return;
+    }
+    var nextDelay = POLL_INTERVAL;
+    if (pollOnce()) {
+        nextDelay = 0;
+    }
+    setTimeout(doPoll, nextDelay);
 }
 
 module.exports = {
@@ -29,6 +35,7 @@ module.exports = {
     },
     stop: function() {
         enabled = false;
-    }
+    },
+    pollOnce: pollOnce
 };
 


Mime
View raw message