From b4bfddf63139e8cd18ce2ba64ef8b9fda6dd44a9 Mon Sep 17 00:00:00 2001 From: Pablo Guardiola Date: Tue, 29 Mar 2022 15:51:32 -0400 Subject: [PATCH] add empty waypoint names to history event map navigation route as old records may not include them causing parsing issues when replaying old history files --- .../core/history/model/HistoryEventMapper.kt | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/libnavigation-core/src/main/java/com/mapbox/navigation/core/history/model/HistoryEventMapper.kt b/libnavigation-core/src/main/java/com/mapbox/navigation/core/history/model/HistoryEventMapper.kt index 61f9a1fc68f..07d450aa44b 100644 --- a/libnavigation-core/src/main/java/com/mapbox/navigation/core/history/model/HistoryEventMapper.kt +++ b/libnavigation-core/src/main/java/com/mapbox/navigation/core/history/model/HistoryEventMapper.kt @@ -1,5 +1,7 @@ package com.mapbox.navigation.core.history.model +import com.google.gson.GsonBuilder +import com.google.gson.JsonObject import com.mapbox.api.directions.v5.DirectionsCriteria import com.mapbox.api.directions.v5.models.DirectionsResponse import com.mapbox.api.directions.v5.models.DirectionsRoute @@ -82,7 +84,9 @@ internal class HistoryEventMapper { "request URL or route options of set route history event cannot be null or empty" ) try { - val directionsResponse = DirectionsResponse.fromJson(response) + // Old records may not include waypoint names + val jsonResponse = addWaypointNames(response) + val directionsResponse = DirectionsResponse.fromJson(jsonResponse) val routeOptions = setRoute.routeRequest?.let { // Old records may include empty routeRequest if (it.isEmpty()) return@let null @@ -105,6 +109,20 @@ internal class HistoryEventMapper { } } + private fun addWaypointNames(response: String): String { + val gson = GsonBuilder().create() + val jsonObject = gson.fromJson(response, JsonObject::class.java) + val waypoints = jsonObject?.getAsJsonArray(WAYPOINTS_JSON_KEY) + val isNameNotIncluded = waypoints?.get(0)?.asJsonObject?.get(NAME_JSON_KEY) == null + if (isNameNotIncluded) { + waypoints?.forEachIndexed { index, _ -> + jsonObject.getAsJsonArray(WAYPOINTS_JSON_KEY) + .get(index).asJsonObject.addProperty(NAME_JSON_KEY, "") + } + } + return jsonObject.toString() + } + private fun mapToWaypoints(routeOptions: RouteOptions?): List { val coordinatesList = routeOptions?.coordinatesList() val waypointIndices = routeOptions?.waypointIndicesList() @@ -136,6 +154,9 @@ internal class HistoryEventMapper { ) private companion object { + private const val NANOS_PER_SECOND = 1e-9 + private const val WAYPOINTS_JSON_KEY = "waypoints" + private const val NAME_JSON_KEY = "name" } }