-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kyle Madsen
committed
Mar 23, 2020
1 parent
3104a01
commit f3f9fc2
Showing
12 changed files
with
216 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...main/java/com/mapbox/navigation/route/offboard/routerefresh/RouteRefreshCallbackMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.mapbox.navigation.route.offboard.routerefresh | ||
|
||
import com.mapbox.api.directions.v5.models.DirectionsRoute | ||
import com.mapbox.api.directionsrefresh.v1.models.DirectionsRefreshResponse | ||
import com.mapbox.navigation.base.route.RouteRefreshCallback | ||
import com.mapbox.navigation.base.route.RouteRefreshError | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
internal class RouteRefreshCallbackMapper( | ||
private val originalRoute: DirectionsRoute, | ||
private val currentLegIndex: Int, | ||
private val callback: RouteRefreshCallback | ||
) : Callback<DirectionsRefreshResponse> { | ||
|
||
override fun onResponse(call: Call<DirectionsRefreshResponse>, response: Response<DirectionsRefreshResponse>) { | ||
val routeAnnotations = response.body()?.route() | ||
var errorThrowable: Throwable? = null | ||
val refreshedDirectionsRoute = try { | ||
mapToDirectionsRoute(routeAnnotations) | ||
} catch (t: Throwable) { | ||
errorThrowable = t | ||
null | ||
} | ||
if (refreshedDirectionsRoute != null) { | ||
callback.onRefresh(refreshedDirectionsRoute) | ||
} else { | ||
callback.onError(RouteRefreshError( | ||
message = "Failed to read refresh response", | ||
throwable = errorThrowable)) | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<DirectionsRefreshResponse>, t: Throwable) { | ||
callback.onError(RouteRefreshError(throwable = t)) | ||
} | ||
|
||
private fun mapToDirectionsRoute(routeAnnotations: DirectionsRoute?): DirectionsRoute? { | ||
val validRouteAnnotations = routeAnnotations ?: return null | ||
val refreshedRouteLegs = originalRoute.legs()?.let { oldRouteLegsList -> | ||
val legs = oldRouteLegsList.toMutableList() | ||
for (i in currentLegIndex until legs.size) { | ||
validRouteAnnotations.legs()?.let { annotationHolderRouteLegsList -> | ||
val updatedAnnotation = annotationHolderRouteLegsList[i - currentLegIndex].annotation() | ||
legs[i] = legs[i].toBuilder().annotation(updatedAnnotation).build() | ||
} | ||
} | ||
legs.toList() | ||
} | ||
return originalRoute.toBuilder().legs(refreshedRouteLegs).build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
libnavigation-base/src/main/java/com/mapbox/navigation/base/route/RouteRefreshCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.mapbox.navigation.base.route | ||
|
||
import com.mapbox.api.directions.v5.models.DirectionsRoute | ||
|
||
interface RouteRefreshCallback { | ||
fun onRefresh(directionsRoute: DirectionsRoute) | ||
|
||
fun onError(error: RouteRefreshError) | ||
} | ||
|
||
data class RouteRefreshError( | ||
val message: String? = null, | ||
val throwable: Throwable? = null | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...tion-core/src/main/java/com/mapbox/navigation/core/routerefresh/RouteRefreshController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.mapbox.navigation.core.routerefresh | ||
|
||
import android.util.Log | ||
import com.mapbox.api.directions.v5.DirectionsCriteria | ||
import com.mapbox.api.directions.v5.models.DirectionsRoute | ||
import com.mapbox.navigation.base.route.RouteRefreshCallback | ||
import com.mapbox.navigation.base.route.RouteRefreshError | ||
import com.mapbox.navigation.core.directions.session.DirectionsSession | ||
import com.mapbox.navigation.core.trip.session.TripSession | ||
import com.mapbox.navigation.utils.timer.MapboxTimer | ||
import java.util.concurrent.TimeUnit | ||
import kotlinx.coroutines.Job | ||
|
||
/** | ||
* This class is responsible for refreshing the current direction route's traffic. | ||
* This does not support alternative routes. | ||
* | ||
* If the route is successfully refreshed, this class will update the [TripSession.route] | ||
* | ||
* [start] and [stop] are attached to the application lifecycle. Observing routes that | ||
* can be refreshed are handled by this class. Calling [start] will restart the refresh timer. | ||
*/ | ||
internal class RouteRefreshController( | ||
private val directionsSession: DirectionsSession, | ||
private val tripSession: TripSession | ||
) { | ||
private val routerRefreshTimer = MapboxTimer() | ||
|
||
init { | ||
routerRefreshTimer.restartAfterMillis = TimeUnit.MINUTES.toMillis(5) | ||
} | ||
|
||
fun start(): Job { | ||
stop() | ||
return routerRefreshTimer.startTimer { | ||
val route = tripSession.route | ||
val legIndex = tripSession.getRouteProgress()?.currentLegProgress()?.legIndex() ?: 0 | ||
if (supportsRefresh(route)) { | ||
directionsSession.requestRouteRefresh( | ||
route!!, | ||
legIndex, | ||
routeRefreshCallback) | ||
} | ||
} | ||
} | ||
|
||
fun stop() { | ||
routerRefreshTimer.stopJobs() | ||
} | ||
|
||
private fun supportsRefresh(route: DirectionsRoute?): Boolean { | ||
val isTrafficProfile = route?.routeOptions() | ||
?.profile()?.equals(DirectionsCriteria.PROFILE_DRIVING_TRAFFIC) | ||
return isTrafficProfile == true | ||
} | ||
|
||
private val routeRefreshCallback = object : RouteRefreshCallback { | ||
|
||
override fun onRefresh(directionsRoute: DirectionsRoute) { | ||
Log.i("RouteRefresh", "Successful refresh") | ||
tripSession.route = directionsRoute | ||
val directionsSessionRoutes = directionsSession.routes.toMutableList() | ||
if (directionsSessionRoutes.isNotEmpty()) { | ||
directionsSessionRoutes[0] = directionsRoute | ||
directionsSession.routes = directionsSessionRoutes | ||
} | ||
} | ||
|
||
override fun onError(error: RouteRefreshError) { | ||
if (error.throwable != null) { | ||
Log.e("RouteRefresh", error.message, error.throwable) | ||
} else { | ||
Log.e("RouteRefresh", error.message) | ||
} | ||
} | ||
} | ||
} |