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 2013/08/15 22:15:45 UTC

android commit: Tweak the online bridge to not send excess online events.

Updated Branches:
  refs/heads/master 121b74fa0 -> 166b35bc6


Tweak the online bridge to not send excess online events.

It does so by having the JS tell it when online events have fired.


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

Branch: refs/heads/master
Commit: 166b35bc6c5977cf547f4093690d554b57cb855a
Parents: 121b74f
Author: Andrew Grieve <ag...@chromium.org>
Authored: Thu Aug 15 15:55:08 2013 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Thu Aug 15 15:55:08 2013 -0400

----------------------------------------------------------------------
 .../org/apache/cordova/CordovaChromeClient.java |  2 +-
 .../src/org/apache/cordova/ExposedJsApi.java    |  6 ++--
 .../apache/cordova/NativeToJsMessageQueue.java  | 29 ++++++++++++--------
 3 files changed, 22 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/166b35bc/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 5299bc5..c55cc8f 100755
--- a/framework/src/org/apache/cordova/CordovaChromeClient.java
+++ b/framework/src/org/apache/cordova/CordovaChromeClient.java
@@ -239,7 +239,7 @@ public class CordovaChromeClient extends WebChromeClient {
 
         // Polling for JavaScript messages 
         else if (reqOk && defaultValue != null && defaultValue.equals("gap_poll:")) {
-            String r = this.appView.exposedJsApi.retrieveJsMessages();
+            String r = this.appView.exposedJsApi.retrieveJsMessages("1".equals(message));
             result.confirm(r == null ? "" : r);
         }
 

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/166b35bc/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
index 23defcb..fde5722 100755
--- a/framework/src/org/apache/cordova/ExposedJsApi.java
+++ b/framework/src/org/apache/cordova/ExposedJsApi.java
@@ -53,7 +53,7 @@ import org.json.JSONException;
             pluginManager.exec(service, action, callbackId, arguments);
             String ret = "";
             if (!NativeToJsMessageQueue.DISABLE_EXEC_CHAINING) {
-                ret = jsMessageQueue.popAndEncode();
+                ret = jsMessageQueue.popAndEncode(false);
             }
             return ret;
         } catch (Throwable e) {
@@ -70,7 +70,7 @@ import org.json.JSONException;
     }
     
     @JavascriptInterface
-    public String retrieveJsMessages() {
-        return jsMessageQueue.popAndEncode();
+    public String retrieveJsMessages(boolean fromOnlineEvent) {
+        return jsMessageQueue.popAndEncode(fromOnlineEvent);
     }
 }

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/166b35bc/framework/src/org/apache/cordova/NativeToJsMessageQueue.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/NativeToJsMessageQueue.java b/framework/src/org/apache/cordova/NativeToJsMessageQueue.java
index d6ed435..94adbc0 100755
--- a/framework/src/org/apache/cordova/NativeToJsMessageQueue.java
+++ b/framework/src/org/apache/cordova/NativeToJsMessageQueue.java
@@ -138,8 +138,9 @@ public class NativeToJsMessageQueue {
      * Combines as many messages as possible, while staying under MAX_PAYLOAD_SIZE.
      * Returns null if the queue is empty.
      */
-    public String popAndEncode() {
+    public String popAndEncode(boolean fromOnlineEvent) {
         synchronized (this) {
+            registeredListeners[activeListenerIndex].notifyOfFlush(fromOnlineEvent);
             if (queue.isEmpty()) {
                 return null;
             }
@@ -274,12 +275,13 @@ public class NativeToJsMessageQueue {
         return paused;
     }
 
-    private interface BridgeMode {
-        void onNativeToJsMessageAvailable();
+    private abstract class BridgeMode {
+        abstract void onNativeToJsMessageAvailable();
+        void notifyOfFlush(boolean fromOnlineEvent) {}
     }
     
     /** Uses webView.loadUrl("javascript:") to execute messages. */
-    private class LoadUrlBridgeMode implements BridgeMode {
+    private class LoadUrlBridgeMode extends BridgeMode {
         final Runnable runnable = new Runnable() {
             public void run() {
                 String js = popAndEncodeAsJs();
@@ -289,18 +291,17 @@ public class NativeToJsMessageQueue {
             }
         };
         
-        public void onNativeToJsMessageAvailable() {
+        @Override void onNativeToJsMessageAvailable() {
             cordova.getActivity().runOnUiThread(runnable);
         }
     }
 
     /** Uses online/offline events to tell the JS when to poll for messages. */
-    private class OnlineEventsBridgeMode implements BridgeMode {
-        boolean online = true;
+    private class OnlineEventsBridgeMode extends BridgeMode {
+        boolean online = false;
         final Runnable runnable = new Runnable() {
             public void run() {
                 if (!queue.isEmpty()) {
-                    online = !online;
                     webView.setNetworkAvailable(online);
                 }
             }                
@@ -308,16 +309,22 @@ public class NativeToJsMessageQueue {
         OnlineEventsBridgeMode() {
             webView.setNetworkAvailable(true);
         }
-        public void onNativeToJsMessageAvailable() {
+        @Override void onNativeToJsMessageAvailable() {
             cordova.getActivity().runOnUiThread(runnable);
         }
+        // Track when online/offline events are fired so that we don't fire excess events.
+        @Override void notifyOfFlush(boolean fromOnlineEvent) {
+            if (fromOnlineEvent) {
+                online = !online;
+            }
+        }
     }
     
     /**
      * Uses Java reflection to access an API that lets us eval JS.
      * Requires Android 3.2.4 or above. 
      */
-    private class PrivateApiBridgeMode implements BridgeMode {
+    private class PrivateApiBridgeMode extends BridgeMode {
     	// Message added in commit:
     	// http://omapzoom.org/?p=platform/frameworks/base.git;a=commitdiff;h=9497c5f8c4bc7c47789e5ccde01179abc31ffeb2
     	// Which first appeared in 3.2.4ish.
@@ -355,7 +362,7 @@ public class NativeToJsMessageQueue {
     		}
     	}
     	
-        public void onNativeToJsMessageAvailable() {
+        @Override void onNativeToJsMessageAvailable() {
         	if (sendMessageMethod == null && !initFailed) {
         		initReflection();
         	}