2012-08-02 22:12:35 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko.util;
|
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
|
|
|
|
public final class EventDispatcher {
|
2013-02-25 19:42:06 +00:00
|
|
|
private static final String LOGTAG = "GeckoEventDispatcher";
|
2012-08-02 22:12:35 +00:00
|
|
|
|
|
|
|
private final Map<String, CopyOnWriteArrayList<GeckoEventListener>> mEventListeners
|
|
|
|
= new HashMap<String, CopyOnWriteArrayList<GeckoEventListener>>();
|
|
|
|
|
|
|
|
public void registerEventListener(String event, GeckoEventListener listener) {
|
|
|
|
synchronized (mEventListeners) {
|
|
|
|
CopyOnWriteArrayList<GeckoEventListener> listeners = mEventListeners.get(event);
|
|
|
|
if (listeners == null) {
|
|
|
|
// create a CopyOnWriteArrayList so that we can modify it
|
|
|
|
// concurrently with iterating through it in handleGeckoMessage.
|
|
|
|
// Otherwise we could end up throwing a ConcurrentModificationException.
|
|
|
|
listeners = new CopyOnWriteArrayList<GeckoEventListener>();
|
|
|
|
} else if (listeners.contains(listener)) {
|
|
|
|
Log.w(LOGTAG, "EventListener already registered for event \"" + event + "\"",
|
|
|
|
new IllegalArgumentException());
|
|
|
|
}
|
|
|
|
listeners.add(listener);
|
|
|
|
mEventListeners.put(event, listeners);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void unregisterEventListener(String event, GeckoEventListener listener) {
|
|
|
|
synchronized (mEventListeners) {
|
|
|
|
CopyOnWriteArrayList<GeckoEventListener> listeners = mEventListeners.get(event);
|
|
|
|
if (listeners == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
listeners.remove(listener);
|
|
|
|
if (listeners.size() == 0) {
|
|
|
|
mEventListeners.remove(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String dispatchEvent(String message) {
|
2013-02-02 01:36:38 +00:00
|
|
|
// {
|
|
|
|
// "type": "value",
|
|
|
|
// "event_specific": "value",
|
|
|
|
// ...
|
2012-08-02 22:12:35 +00:00
|
|
|
try {
|
|
|
|
JSONObject json = new JSONObject(message);
|
2013-02-02 01:36:38 +00:00
|
|
|
if (json.has("gecko")) {
|
|
|
|
json = json.getJSONObject("gecko");
|
2013-02-25 19:42:06 +00:00
|
|
|
Log.w(LOGTAG, "The 'gecko' property of the sendMessageToJava parameter is deprecated; message of type " + json.getString("type"));
|
2013-02-02 01:36:38 +00:00
|
|
|
}
|
|
|
|
String type = json.getString("type");
|
2012-08-02 22:12:35 +00:00
|
|
|
|
|
|
|
CopyOnWriteArrayList<GeckoEventListener> listeners;
|
|
|
|
synchronized (mEventListeners) {
|
|
|
|
listeners = mEventListeners.get(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listeners == null)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
String response = null;
|
|
|
|
|
|
|
|
for (GeckoEventListener listener : listeners) {
|
2013-02-02 01:36:38 +00:00
|
|
|
listener.handleMessage(type, json);
|
2012-08-02 22:12:35 +00:00
|
|
|
if (listener instanceof GeckoEventResponder) {
|
|
|
|
String newResponse = ((GeckoEventResponder)listener).getResponse();
|
|
|
|
if (response != null && newResponse != null) {
|
|
|
|
Log.e(LOGTAG, "Received two responses for message of type " + type);
|
|
|
|
}
|
|
|
|
response = newResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response != null)
|
|
|
|
return response;
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "handleGeckoMessage throws " + e, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|