Skip to content

Commit 2a4e87a

Browse files
author
Cameron Mace
authored
location serializer added (#591)
1 parent 77b3777 commit 2a4e87a

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/navigation/MapboxNavigationEvent.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public static Hashtable<String, Object> buildFeedbackEvent(
135135
String upcomingModifier, String upcomingName, String previousInstruction, String previousType,
136136
String previousModifier, String previousName, int distance, int duration, int stepDistanceRemaining,
137137
int stepDurationRemaining, int stepCount, int originalStepCount) {
138+
NavigationLocation navigationLoc = new NavigationLocation();
138139
Hashtable<String, Object> event = getMetadata(sdKIdentifier, sdkVersion, sessionIdentifier,
139140
lat, lng, geometry, profile, estimatedDistance, estimatedDuration, rerouteCount,
140141
isSimulation, originalRequestIdentifier, requestIdentifier, originalGeometry,
@@ -150,12 +151,12 @@ public static Hashtable<String, Object> buildFeedbackEvent(
150151
event.put(KEY_START_TIMESTAMP, TelemetryUtils.generateCreateDateFormatted(startTimestamp));
151152
event.put(KEY_FEEDBACK_TYPE, feedbackType);
152153
if (locationsBefore != null) {
153-
event.put(KEY_LOCATIONS_BEFORE, locationsBefore);
154+
event.put(KEY_LOCATIONS_BEFORE, navigationLoc.getSerializedJson(locationsBefore));
154155
} else {
155156
event.put(KEY_LOCATIONS_BEFORE, JSONObject.NULL);
156157
}
157158
if (locationsAfter != null) {
158-
event.put(KEY_LOCATIONS_AFTER, locationsAfter);
159+
event.put(KEY_LOCATIONS_AFTER, navigationLoc.getSerializedJson(locationsAfter));
159160
} else {
160161
event.put(KEY_LOCATIONS_AFTER, JSONObject.NULL);
161162
}
@@ -180,6 +181,7 @@ public static Hashtable<String, Object> buildRerouteEvent(
180181
String upcomingName, String previousInstruction, String previousType, String previousModifier,
181182
String previousName, int distance, int duration, int stepDistanceRemaining, int stepDurationRemaining,
182183
int stepCount, int originalStepCount) {
184+
NavigationLocation navigationLoc = new NavigationLocation();
183185
Hashtable<String, Object> event = getMetadata(sdKIdentifier, sdkVersion, sessionIdentifier,
184186
lat, lng, geometry, profile, estimatedDistance, estimatedDuration, rerouteCount,
185187
isSimulation, originalRequestIdentifier, requestIdentifier, originalGeometry,
@@ -191,12 +193,12 @@ public static Hashtable<String, Object> buildRerouteEvent(
191193
event.put(KEY_FEEDBACK_ID, feedbackId);
192194
event.put(KEY_START_TIMESTAMP, TelemetryUtils.generateCreateDateFormatted(startTimestamp));
193195
if (locationsBefore != null) {
194-
event.put(KEY_LOCATIONS_BEFORE, locationsBefore);
196+
event.put(KEY_LOCATIONS_BEFORE, navigationLoc.getSerializedJson(locationsBefore));
195197
} else {
196198
event.put(KEY_LOCATIONS_BEFORE, JSONObject.NULL);
197199
}
198200
if (locationsAfter != null) {
199-
event.put(KEY_LOCATIONS_AFTER, locationsAfter);
201+
event.put(KEY_LOCATIONS_AFTER, navigationLoc.getSerializedJson(locationsAfter));
200202
} else {
201203
event.put(KEY_LOCATIONS_AFTER, JSONObject.NULL);
202204
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.mapbox.services.android.telemetry.navigation;
2+
3+
import android.location.Location;
4+
import android.util.JsonToken;
5+
6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
9+
import java.text.DateFormat;
10+
import java.text.SimpleDateFormat;
11+
import java.util.Date;
12+
import java.util.HashMap;
13+
import java.util.Locale;
14+
import java.util.TimeZone;
15+
16+
public class NavigationLocation {
17+
18+
private static final String UTC_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
19+
private static final String ACCURACY = "horizontalAccuracy";
20+
private static final String TIMESTAMP = "timestamp";
21+
private static final String ALTITUDE = "altitude";
22+
private static final String COURSE = "course";
23+
private static final String LONGITUDE = "lng";
24+
private static final String LATITUDE = "lat";
25+
private static final String SPEED = "speed";
26+
27+
public JSONArray getSerializedJson(Location[] locations) {
28+
JSONArray jsonArray = new JSONArray();
29+
for (Location loc : locations) {
30+
jsonArray.put(serializeToJson(loc));
31+
}
32+
return jsonArray;
33+
}
34+
35+
private JSONObject serializeToJson(Location location) {
36+
formatLocationTime(location);
37+
38+
HashMap<String, Object> map = new HashMap<>();
39+
// Potentially null values
40+
map.put(ALTITUDE, location.hasAltitude() ? location.getAltitude() : JsonToken.NULL);
41+
map.put(ACCURACY, location.hasAccuracy() ? location.getAccuracy() : JsonToken.NULL);
42+
map.put(COURSE, location.hasBearing() ? location.getBearing() : JsonToken.NULL);
43+
map.put(SPEED, location.hasSpeed() ? location.getSpeed() : JsonToken.NULL);
44+
45+
// Never null values
46+
map.put(LATITUDE, location.getLatitude());
47+
map.put(LONGITUDE, location.getLongitude());
48+
map.put(TIMESTAMP, formatLocationTime(location));
49+
50+
return new JSONObject(map);
51+
}
52+
53+
private static String formatLocationTime(Location location) {
54+
DateFormat formatter = new SimpleDateFormat(UTC_TIME_FORMAT, Locale.US);
55+
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
56+
return formatter.format(new Date(location.getTime()));
57+
}
58+
}

0 commit comments

Comments
 (0)