*
* Example (python): * import android, time * droid = android.Android() * droid.startSensing() * time.sleep(1) * droid.eventClearBuffer() * time.sleep(1) * e = eventPoll(1).result * event_entry_number = 0 * x = e[event_entry_ number]['data']['xforce'] **
* e has the format:
* [{u'data': {u'accuracy': 0, u'pitch': -0.48766891956329345, u'xmag': -5.6875, u'azimuth':
* 0.3312483489513397, u'zforce': 8.3492730000000002, u'yforce': 4.5628165999999997, u'time':
* 1297072704.813, u'ymag': -11.125, u'zmag': -42.375, u'roll': -0.059393649548292161,
* u'xforce': 0.42223078000000003}, u'name': u'sensors', u'time': 1297072704813000L}]
* x has the string value of the x force data (0.42223078000000003) at the time of the event
* entry.
*/
@Rpc(description = "Returns and removes the oldest n events (i.e. location or sensor update, etc.) from the event buffer.",
returns = "A List of Maps of event properties.")
public List
* Example:
* import android
* from datetime import datetime
* droid = android.Android()
* t = datetime.now()
* droid.eventPost('Some Event', t)
*
*/
@Rpc(description = "Post an event to the event queue.")
public void eventPost(
@RpcParameter(name = "name", description = "Name of event") String name,
@RpcParameter(name = "data", description = "Data contained in event.") String data,
@RpcParameter(name = "enqueue",
description = "Set to False if you don't want your events to be added to the event queue, just dispatched.") @RpcOptional @RpcDefault("false") Boolean enqueue) {
postEvent(name, data, enqueue.booleanValue());
}
/**
* Post an event and queue it
*/
public void postEvent(String name, Object data) {
postEvent(name, data, true);
}
/**
* Posts an event with to the event queue.
*/
public void postEvent(String name, Object data, boolean enqueue) {
Event event = new Event(name, data);
if (enqueue) {
Log.v(String.format("postEvent(%s)", name));
synchronized (mEventQueue) {
while (mEventQueue.size() >= MAX_QUEUE_SIZE) {
mEventQueue.remove();
}
mEventQueue.add(event);
// b/77306870: Posting to the EventObservers when enqueuing an event must be
// done when mEventQueue is locked. Otherwise, we can run into the following
// race condition:
// 1) postEvent() adds the event to the event queue, and releases mEventQueue.
// Here, the thread is put to sleep.
// 2) eventWait() is called when an event is queued, and exits immediately.
// 3) eventWait() is called a second time, finds no event and creates a
// GlobalEventObserver.
// 4) postEvent() wakes back up, and continues to post the event to the observers.
// The same event sent to the first eventWait call is sent to the
// second eventWait call's observer, causing a duplicated received
// event.
postEventToNamedObservers(event);
postEventToGlobalObservers(event);
}
} else {
postEventToNamedObservers(event);
postEventToGlobalObservers(event);
}
}
/**
* Posts the event to all applicable Named Observers.
*/
private void postEventToNamedObservers(Event event) {
synchronized (mNamedEventObservers) {
for (EventObserver observer : mNamedEventObservers.get(event.getName())) {
Log.d(String.format("namedEventObserver %s received event %s",
observer,
event.getName()));
observer.onEventReceived(event);
}
}
}
/**
* Posts the event to the Global Observers list.
*/
private void postEventToGlobalObservers(Event event) {
synchronized (mGlobalEventObservers) {
for (EventObserver observer : mGlobalEventObservers) {
Log.d(String.format("globalEventObserver %s received event %s",
observer,
event.getName()));
observer.onEventReceived(event);
}
}
}
@RpcDeprecated(value = "eventPost", release = "r4")
@Rpc(description = "Post an event to the event queue.")
@RpcName(name = "postEvent")
public void rpcPostEvent(
@RpcParameter(name = "name") String name,
@RpcParameter(name = "data") String data) {
postEvent(name, data);
}
@RpcDeprecated(value = "eventPoll", release = "r4")
@Rpc(description = "Returns and removes the oldest event (i.e. location or sensor update, etc.) from the event buffer.",
returns = "Map of event properties.")
public Event receiveEvent() {
return mEventQueue.poll();
}
@RpcDeprecated(value = "eventWaitFor", release = "r4")
@Rpc(description = "Blocks until an event with the supplied name occurs. Event is removed from the buffer if removeEvent is True.",
returns = "Map of event properties.")
public Event waitForEvent(
@RpcParameter(name = "eventName") final String eventName,
@RpcOptional final Boolean removeEvent,
@RpcParameter(name = "timeout", description = "the maximum time to wait") @RpcOptional Integer timeout)
throws InterruptedException {
return eventWaitFor(eventName, removeEvent, timeout);
}
/**
* Closes this SL4A session, and sends a terminating signal to the event observers.
*/
@Rpc(description = "sl4a session is shutting down, send terminate event to client.")
public void closeSl4aSession() {
eventClearBuffer();
postEvent("EventDispatcherShutdown", null);
}
/**
* Shuts down the RPC server.
*/
@Override
public void shutdown() {
mGlobalEventObservers.clear();
mEventQueue.clear();
}
/**
* Adds a named observer to the event listening queue.
* @param eventName the name of the event to listen to
* @param observer the observer object
*/
public void addNamedEventObserver(String eventName, EventObserver observer) {
mNamedEventObservers.put(eventName, observer);
}
/**
* Adds a global event listener ot the listening queue.
* @param observer the observer object
*/
public void addGlobalEventObserver(EventObserver observer) {
mGlobalEventObservers.add(observer);
}
/**
* Removes an observer from the event listening queue.
* @param observer the observer to remove
*/
public void removeEventObserver(EventObserver observer) {
mGlobalEventObservers.remove(observer);
}
/**
* Removes a named observer from the event listening queue.
* @param eventName the name of the event being listened to.
* @param observer the observer to remove
*/
public void removeNamedEventObserver(String eventName, EventObserver observer) {
mNamedEventObservers.removeAll(eventName);
removeEventObserver(observer);
}
public class BroadcastListener extends android.content.BroadcastReceiver {
private EventFacade mParent;
private boolean mEnQueue;
public BroadcastListener(EventFacade parent, boolean enqueue) {
mParent = parent;
mEnQueue = enqueue;
}
@Override
public void onReceive(Context context, Intent intent) {
Bundle data;
if (intent.getExtras() != null) {
data = (Bundle) intent.getExtras().clone();
} else {
data = new Bundle();
}
data.putString("action", intent.getAction());
try {
mParent.eventPost("sl4a", JsonBuilder.build(data).toString(), mEnQueue);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}