[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/18d986ba Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/18d986ba Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/18d986ba Branch: refs/heads/master Commit: 18d986babcea3f88475676ecd734f6a3906de769 Parents: 8e03c22 Author: Andrew Grieve <agri...@chromium.org> Authored: Tue Aug 21 14:09:40 2012 -0400 Committer: Andrew Grieve <agri...@chromium.org> Committed: Wed Aug 22 09:41:21 2012 -0400 ---------------------------------------------------------------------- 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/18d986ba/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/18d986ba/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 };