[android] #4066 - Implementing flush events when app terminates functionality

This commit is contained in:
Brad Leege
2016-02-26 15:26:47 -06:00
parent 3ed9f4336c
commit b3adb96e4a
2 changed files with 28 additions and 2 deletions

View File

@@ -232,6 +232,16 @@ public class MapboxEventManager {
editor.commit();
}
/**
* Immediately attempt to send all events data in the queue to the server.
*
* NOTE: Permission set to package private to enable only telemetry code to use this.
*/
void flushEventsQueueImmediately() {
Log.i(TAG, "flushEventsQueueImmediately() called...");
new FlushTheEventsTask().execute();
}
/**
* Adds a Location Event to the system for processing
* @param location Location event
@@ -328,7 +338,7 @@ public class MapboxEventManager {
events.add(event);
// Send to Server Immediately
new FlushTheEventsTask().execute();
flushEventsQueueImmediately();
Log.d(TAG, "turnstile event pushed.");
}
@@ -540,7 +550,6 @@ public class MapboxEventManager {
jsonObject.putOpt(MapboxEvent.KEY_ALTITUDE, evt.get(MapboxEvent.KEY_ALTITUDE));
jsonObject.putOpt(MapboxEvent.KEY_ZOOM, evt.get(MapboxEvent.KEY_ZOOM));
jsonObject.putOpt(MapboxEvent.ATTRIBUTE_OPERATING_SYSTEM, evt.get(MapboxEvent.ATTRIBUTE_OPERATING_SYSTEM));
jsonObject.putOpt(MapboxEvent.ATTRIBUTE_APPLICATION_STATE, evt.get(MapboxEvent.ATTRIBUTE_APPLICATION_STATE));
jsonObject.putOpt(MapboxEvent.ATTRIBUTE_USERID, evt.get(MapboxEvent.ATTRIBUTE_USERID));
jsonObject.putOpt(MapboxEvent.ATTRIBUTE_MODEL, evt.get(MapboxEvent.ATTRIBUTE_MODEL));
jsonObject.putOpt(MapboxEvent.ATTRIBUTE_RESOLUTION, evt.get(MapboxEvent.ATTRIBUTE_RESOLUTION));
@@ -563,6 +572,12 @@ public class MapboxEventManager {
jsonObject.putOpt(MapboxEvent.ATTRIBUTE_CARRIER, carrier);
}
}
if (evt.containsKey(MapboxEvent.ATTRIBUTE_APPLICATION_STATE)) {
String appState = (String)evt.get(MapboxEvent.ATTRIBUTE_APPLICATION_STATE);
if (!TextUtils.isEmpty(appState)) {
jsonObject.putOpt(MapboxEvent.ATTRIBUTE_APPLICATION_STATE, evt.get(MapboxEvent.ATTRIBUTE_APPLICATION_STATE));
}
}
// Special Cases where null has to be passed if no value exists
// Requires using put() instead of putOpt()

View File

@@ -139,6 +139,17 @@ public class TelemetryService extends Service {
}
private void shutdownTelemetry() {
// Send Any Remaining events to the server
MapboxEventManager.getMapboxEventManager().flushEventsQueueImmediately();
// Undesired, but needed trick to keep app alive long enough for data to get to server
try {
Thread.sleep(1000);
} catch (Exception e) {
Log.e(TAG, "Error while trying to sleep for 1 second: " + e);
}
unregisterReceiver(telemetryLocationReceiver);
telemetryWakeLock.release();
}