Tweak the online bridge to not send excess online events.

It does so by having the JS tell it when online events have fired.
This commit is contained in:
Andrew Grieve 2013-08-15 15:55:08 -04:00
parent 121b74fa0c
commit 166b35bc6c
3 changed files with 22 additions and 15 deletions

View File

@ -239,7 +239,7 @@ public class CordovaChromeClient extends WebChromeClient {
// Polling for JavaScript messages // Polling for JavaScript messages
else if (reqOk && defaultValue != null && defaultValue.equals("gap_poll:")) { 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); result.confirm(r == null ? "" : r);
} }

View File

@ -53,7 +53,7 @@ import org.json.JSONException;
pluginManager.exec(service, action, callbackId, arguments); pluginManager.exec(service, action, callbackId, arguments);
String ret = ""; String ret = "";
if (!NativeToJsMessageQueue.DISABLE_EXEC_CHAINING) { if (!NativeToJsMessageQueue.DISABLE_EXEC_CHAINING) {
ret = jsMessageQueue.popAndEncode(); ret = jsMessageQueue.popAndEncode(false);
} }
return ret; return ret;
} catch (Throwable e) { } catch (Throwable e) {
@ -70,7 +70,7 @@ import org.json.JSONException;
} }
@JavascriptInterface @JavascriptInterface
public String retrieveJsMessages() { public String retrieveJsMessages(boolean fromOnlineEvent) {
return jsMessageQueue.popAndEncode(); return jsMessageQueue.popAndEncode(fromOnlineEvent);
} }
} }

View File

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