[android] #2421 - Building out MapboxEventsManager with support for Staging and Prod servers

This commit is contained in:
Brad Leege
2016-02-01 16:29:22 -06:00
parent 8464d68687
commit 2690cc09f8
5 changed files with 195 additions and 0 deletions

View File

@@ -17,6 +17,16 @@ public class MapboxConstants {
*/
public static final String KEY_META_DATA_MANIFEST = "com.mapbox.AccessToken";
/**
* Key used to store staging data server url in AndroidManifest.xml
*/
public static final String KEY_META_DATA_STAGING_SERVER = "com.mapbox.TestEventsServer";
/**
* Key used to store staging data server access token in AndroidManifest.xml
*/
public static final String KEY_META_DATA_STAGING_ACCESS_TOKEN = "com.mapbox.TestEventsAccessToken";
/**
* Default animation time
*/

View File

@@ -0,0 +1,17 @@
package com.mapbox.mapboxsdk.telemetry;
import java.io.Serializable;
public class MapboxEvent implements Serializable {
public static final int version = 1;
public static final String MGLMapboxEventsUserAgent = "Sirius/1.0/ANDROID MapboxEventsiOS/1.1";
public static final String MGLMapboxEventsAPIBase = "https://api.tiles.mapbox.com";
public static final String MGLEventKeyLatitude = "lat";
public static final String MGLEventKeyLongitude = "lng";
public static final String MGLEventKeySpeed = "speed";
public static final String MGLEventKeyCourse = "course";
public static final String MGLEventKeyAltitude = "altitude";
public static final String MGLEventKeyHorizontalAccuracy = "horizontalAccuracy";
public static final String MGLEventKeyVerticalAccuracy = "verticalAccuracy";
}

View File

@@ -0,0 +1,160 @@
package com.mapbox.mapboxsdk.telemetry;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.utils.ApiAccess;
import org.json.JSONArray;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Vector;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MapboxEventManager {
private static final String TAG = "MapboxEventManager";
private static MapboxEventManager mapboxEventManager = null;
private final Vector<Hashtable<String, Object>> events = new Vector<>();
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
private Context context = null;
private String accessToken = null;
private static final String MAPBOX_EVENTS_BASE_URL = "https://api.tiles.mapbox.com";
private String eventsURL = MAPBOX_EVENTS_BASE_URL;
private MapboxEventManager(@NonNull Context context) {
super();
this.accessToken = ApiAccess.getToken(context);
this.context = context;
// Check for Staging Server Information
try {
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
String stagingURL = appInfo.metaData.getString(MapboxConstants.KEY_META_DATA_STAGING_SERVER);
String stagingAccessToken = appInfo.metaData.getString(MapboxConstants.KEY_META_DATA_STAGING_ACCESS_TOKEN);
if (!TextUtils.isEmpty(stagingURL)) {
eventsURL = stagingURL;
}
if (!TextUtils.isEmpty(stagingAccessToken)) {
this.accessToken = stagingAccessToken;
}
} catch (Exception e) {
Log.e(TAG, "Error Trying to load Staging Credentials: " + e.toString());
}
}
public static MapboxEventManager getMapboxEventManager(@NonNull Context context) {
if (mapboxEventManager == null) {
mapboxEventManager = new MapboxEventManager(context);
}
return mapboxEventManager;
}
public void addLocationEvent(Location location) {
// Add Location even to queue
Hashtable<String, Object> event = new Hashtable<>();
event.put(MapboxEvent.MGLEventKeyLatitude, location.getLatitude());
event.put(MapboxEvent.MGLEventKeyLongitude, location.getLongitude());
event.put(MapboxEvent.MGLEventKeySpeed, location.hasSpeed() ? location.getSpeed() : -1);
event.put(MapboxEvent.MGLEventKeyCourse, location.hasBearing() ? location.getBearing() : -1);
event.put(MapboxEvent.MGLEventKeyAltitude, location.hasAltitude() ? location.getAltitude() : 0);
event.put(MapboxEvent.MGLEventKeyHorizontalAccuracy, location.hasAccuracy() ? location.getAccuracy() : 0);
event.put(MapboxEvent.MGLEventKeyVerticalAccuracy, location.hasAccuracy() ? location.getAccuracy() : 0);
event.put("created", dateFormat.format(new Date()));
events.add(event);
// If size > 1 then flush
if (events.size() >= 1) {
new FlushTheEventsTask().execute();
}
}
private class FlushTheEventsTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
// Check for NetworkConnectivity
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
Log.w(TAG, "Not connected to network, so returning without attempting to send events");
return null;
}
try {
// Send data
// =========
JSONArray jsonArray = new JSONArray();
for (Hashtable<String, Object> evt : events) {
JSONObject jsonObject = new JSONObject();
jsonObject.put(MapboxEvent.MGLEventKeyLatitude, evt.get(MapboxEvent.MGLEventKeyLatitude));
jsonObject.put(MapboxEvent.MGLEventKeyLongitude, evt.get(MapboxEvent.MGLEventKeyLongitude));
jsonObject.put(MapboxEvent.MGLEventKeySpeed, evt.get(MapboxEvent.MGLEventKeySpeed));
jsonObject.put(MapboxEvent.MGLEventKeyCourse, evt.get(MapboxEvent.MGLEventKeyCourse));
jsonObject.put(MapboxEvent.MGLEventKeyAltitude, evt.get(MapboxEvent.MGLEventKeyAltitude));
jsonObject.put(MapboxEvent.MGLEventKeyHorizontalAccuracy, evt.get(MapboxEvent.MGLEventKeyHorizontalAccuracy));
jsonObject.put(MapboxEvent.MGLEventKeyVerticalAccuracy, evt.get(MapboxEvent.MGLEventKeyVerticalAccuracy));
// Basic Event Meta Data
jsonObject.put("event", "location");
jsonObject.put("version", 1);
jsonObject.put("instance", "FFFFFFFF-GGGG-HHHH-IIII-JJJJJJJJJJJJ");
jsonObject.put("created", evt.get("created"));
jsonObject.put("anonid", "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE");
jsonObject.put("appBundleId", "com.mapbox.hermes.android");
jsonArray.put(jsonObject);
}
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, jsonArray.toString());
String url = eventsURL + "/events/v1?access_token=" + accessToken;
Log.i(TAG, "url = " + url);
Request request = new Request.Builder()
.url(url)
.header("User-Agent", MapboxEvent.MGLMapboxEventsUserAgent)
.post(body)
.build();
Response response = client.newCall(request).execute();
Log.i(TAG, "Response Code from Mapbox Events Server: " + response.code());
// Reset Events
// ============
events.removeAllElements();
} catch (Exception e) {
Log.e(TAG, "FlushTheEventsTask borked: " + e);
}
return null;
}
}
}

View File

@@ -64,6 +64,8 @@ public class TelemetryLocationReceiver extends BroadcastReceiver {
Location location = (Location)intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED);
if (location != null) {
Log.i(TAG, "location received = " + location);
MapboxEventManager.getMapboxEventManager(context).addLocationEvent(location);
final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
tg.startTone(ToneGenerator.TONE_PROP_BEEP);
tg.stopTone();