7 package com.google.appinventor.components.runtime;
9 import android.util.Log;
11 import java.util.HashMap;
12 import java.util.HashSet;
23 private static final class EventClosure {
24 private final String componentId;
25 private final String eventName;
27 private EventClosure(String componentId, String eventName) {
28 this.componentId = componentId;
29 this.eventName = eventName;
33 public boolean equals(Object o) {
37 if (o ==
null || getClass() != o.getClass()) {
41 EventClosure that = (EventClosure) o;
43 if (!componentId.equals(that.componentId)) {
46 if (!eventName.equals(that.eventName)) {
54 public int hashCode() {
55 return 31 * eventName.hashCode() + componentId.hashCode();
63 private static final class EventRegistry {
70 private final HashMap<String, Set<EventClosure>> eventClosuresMap =
71 new HashMap<String, Set<EventClosure>>();
74 this.dispatchDelegate = dispatchDelegate;
78 private static final boolean DEBUG =
false;
81 mapDispatchDelegateToEventRegistry =
new HashMap<HandlesEventDispatching, EventRegistry>();
87 EventRegistry er = mapDispatchDelegateToEventRegistry.get(dispatchDelegate);
89 er =
new EventRegistry(dispatchDelegate);
90 mapDispatchDelegateToEventRegistry.put(dispatchDelegate, er);
96 return mapDispatchDelegateToEventRegistry.remove(dispatchDelegate);
110 String componentId, String eventName) {
111 EventRegistry er = getEventRegistry(dispatchDelegate);
112 Set<EventClosure> eventClosures = er.eventClosuresMap.get(eventName);
113 if (eventClosures ==
null) {
114 eventClosures =
new HashSet<EventClosure>();
115 er.eventClosuresMap.put(eventName, eventClosures);
118 eventClosures.add(
new EventClosure(componentId, eventName));
120 Log.i(
"EventDispatcher",
"Registered event closure for " +
121 componentId +
"." + eventName);
135 String componentId, String eventName) {
136 EventRegistry er = getEventRegistry(dispatchDelegate);
137 Set<EventClosure> eventClosures = er.eventClosuresMap.get(eventName);
138 if (eventClosures ==
null || eventClosures.isEmpty()) {
141 Set<EventClosure> toDelete =
new HashSet<EventClosure>();
142 for (EventClosure eventClosure : eventClosures) {
143 if (eventClosure.componentId.equals(componentId)) {
144 toDelete.add(eventClosure);
147 for (EventClosure eventClosure : toDelete) {
149 Log.i(
"EventDispatcher",
"Deleting event closure for " +
150 eventClosure.componentId +
"." + eventClosure.eventName);
152 eventClosures.remove(eventClosure);
162 for (EventRegistry er : mapDispatchDelegateToEventRegistry.values()) {
163 er.eventClosuresMap.clear();
175 EventRegistry er = removeEventRegistry(dispatchDelegate);
177 er.eventClosuresMap.clear();
190 Log.i(
"EventDispatcher",
"Trying to dispatch event " + eventName);
192 boolean dispatched =
false;
195 EventRegistry er = getEventRegistry(dispatchDelegate);
196 Set<EventClosure> eventClosures = er.eventClosuresMap.get(eventName);
197 if (eventClosures !=
null && eventClosures.size() > 0) {
198 dispatched = delegateDispatchEvent(dispatchDelegate, eventClosures, component, args);
213 Set<EventClosure> eventClosures,
219 boolean dispatched =
false;
220 for (EventClosure eventClosure : eventClosures) {
222 eventClosure.componentId,
223 eventClosure.eventName,
226 Log.i(
"EventDispatcher",
"Successfully dispatched event " +
227 eventClosure.componentId +
"." + eventClosure.eventName);
238 Log.i(
"EventDispatcher",
"makeFullEventName componentId=" + componentId +
", " +
239 "eventName=" + eventName);
241 return componentId +
'$' + eventName;