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
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);
}

View File

@ -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);
}
}

View File

@ -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();
}