-
Notifications
You must be signed in to change notification settings - Fork 320
Add arrival controller for multiple stops #2787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
108 changes: 108 additions & 0 deletions
108
libnavigation-core/src/main/java/com/mapbox/navigation/core/stops/ArrivalController.kt
This file contains hidden or 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,108 @@ | ||
| package com.mapbox.navigation.core.stops | ||
|
|
||
| import com.mapbox.navigation.base.trip.model.RouteLegProgress | ||
| import com.mapbox.navigation.base.trip.model.RouteProgress | ||
| import com.mapbox.navigation.base.trip.model.RouteProgressState | ||
| import com.mapbox.navigation.core.MapboxNavigation | ||
|
|
||
| /** | ||
| * When navigating to points of interest, you may want to control the arrival | ||
| * experience. This interface gives you options to change arrival via [MapboxNavigation.attachArrivalController] | ||
| */ | ||
| interface ArrivalController { | ||
|
|
||
| /** | ||
| * Override the options for your arrival callback. | ||
| */ | ||
| fun arrivalOptions(): ArrivalOptions | ||
|
|
||
| /** | ||
| * Based on your [ArrivalOptions], this will be called as the next stop is approached. | ||
| * To manually navigate the next leg, return false and call [MapboxNavigation.navigateNextRouteLeg] | ||
| * | ||
| * @return true to automatically move to the next step, false to do it manually | ||
| */ | ||
| fun onStopArrival(routeLegProgress: RouteLegProgress): Boolean | ||
|
|
||
| /** | ||
| * Once the [RouteProgress.currentState] has reached [RouteProgressState.ROUTE_ARRIVED] | ||
| * for the last stop, this will be called once. | ||
| */ | ||
| fun onRouteArrival(routeProgress: RouteProgress) {} | ||
| } | ||
|
|
||
| /** | ||
| * The default controller for arrival. This will move onto the next leg automatically | ||
| * if there is one. | ||
| */ | ||
| class AutoArrivalController : ArrivalController { | ||
|
|
||
| /** | ||
| * Default arrival options | ||
| */ | ||
| override fun arrivalOptions(): ArrivalOptions = ArrivalOptions.Builder().build() | ||
|
|
||
| /** | ||
| * By default this will move onto the next step. | ||
| */ | ||
| override fun onStopArrival(routeLegProgress: RouteLegProgress): Boolean { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Choose when to be notified of arrival. | ||
| */ | ||
| data class ArrivalOptions( | ||
|
|
||
| /** | ||
| * While the next stop is less than [arrivalInSeconds] away, | ||
| * [ArrivalController.onStopArrival] will be called | ||
| */ | ||
| val arrivalInSeconds: Double?, | ||
|
|
||
| /** | ||
| * While the next stop is less than [arrivalInMeters] away, | ||
| * [ArrivalController.onStopArrival] will be called | ||
| */ | ||
| val arrivalInMeters: Double? | ||
| ) { | ||
| /** | ||
| * Build your arrival options. | ||
| */ | ||
| class Builder { | ||
|
|
||
| private var arrivalInSeconds: Double? = 5.0 | ||
| private var arrivalInMeters: Double? = 40.0 | ||
|
|
||
| /** | ||
| * (Recommended) Use time estimation for arrival, arrival is influenced by traffic conditions. | ||
| * Arrive when the estimated time to a stop is less than or equal to this threshold | ||
| */ | ||
| fun arriveInSeconds(arriveInSeconds: Double?): Builder { | ||
| this.arrivalInSeconds = arriveInSeconds | ||
| return this | ||
| } | ||
|
|
||
| /** | ||
| * Arrive when the estimated distance to a stop is less than or equal to this threshold | ||
| */ | ||
| fun arriveInMeters(arriveInMeters: Double?): Builder { | ||
| this.arrivalInMeters = arriveInMeters | ||
| return this | ||
| } | ||
|
|
||
| /** | ||
| * Build the object. If you want to disable this feature use [MapboxNavigation.removeArrivalController] | ||
| */ | ||
| fun build(): ArrivalOptions { | ||
| check(arrivalInSeconds != null || arrivalInSeconds != null) { | ||
| "Choose a method to be notified of arrival, time and/or distance." | ||
| } | ||
| return ArrivalOptions( | ||
| arrivalInSeconds = arrivalInSeconds, | ||
| arrivalInMeters = arrivalInMeters | ||
| ) | ||
| } | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
libnavigation-core/src/main/java/com/mapbox/navigation/core/stops/ArrivalProgressObserver.kt
This file contains hidden or 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,79 @@ | ||
| package com.mapbox.navigation.core.stops | ||
|
|
||
| import com.mapbox.navigation.base.trip.RouteProgressObserver | ||
| import com.mapbox.navigation.base.trip.model.RouteLegProgress | ||
| import com.mapbox.navigation.base.trip.model.RouteProgress | ||
| import com.mapbox.navigation.base.trip.model.RouteProgressState | ||
| import com.mapbox.navigation.core.trip.session.TripSession | ||
|
|
||
| internal class ArrivalProgressObserver( | ||
| private val tripSession: TripSession | ||
| ) : RouteProgressObserver { | ||
|
|
||
| private var arrivalController: ArrivalController = AutoArrivalController() | ||
| private var arrivedForRoute = false | ||
|
|
||
| fun attach(arrivalController: ArrivalController) { | ||
| this.arrivalController = arrivalController | ||
| } | ||
|
|
||
| fun navigateNextRouteLeg(): Boolean { | ||
| val numberOfLegs = tripSession.getRouteProgress()?.route()?.legs()?.size | ||
| ?: return false | ||
| val legIndex = tripSession.getRouteProgress()?.currentLegProgress()?.legIndex() | ||
| ?: return false | ||
| val nextLegIndex = legIndex + 1 | ||
| return if (nextLegIndex < numberOfLegs) { | ||
| val navigationStatus = tripSession.updateLegIndex(nextLegIndex) | ||
| return nextLegIndex == navigationStatus.legIndex | ||
| } else { | ||
| true | ||
| } | ||
| } | ||
|
|
||
| override fun onRouteProgressChanged(routeProgress: RouteProgress) { | ||
| val routeLegProgress = routeProgress.currentLegProgress() | ||
| ?: return | ||
|
|
||
| val arrivalOptions = arrivalController.arrivalOptions() | ||
| if (routeProgress.currentState() == RouteProgressState.ROUTE_ARRIVED && !hasMoreLegs(routeProgress)) { | ||
| doOnRouteArrival(routeProgress) | ||
| } else if (arrivalOptions.arrivalInSeconds != null) { | ||
| checkStopArrivalTime(arrivalOptions.arrivalInSeconds, routeLegProgress) | ||
| } else if (arrivalOptions.arrivalInMeters != null) { | ||
| checkStopArrivalDistance(arrivalOptions.arrivalInMeters, routeLegProgress) | ||
| } | ||
| arrivedForRoute = (routeProgress.currentState() ?: RouteProgressState.ROUTE_UNCERTAIN) == RouteProgressState.ROUTE_ARRIVED | ||
| } | ||
|
|
||
| private fun hasMoreLegs(routeProgress: RouteProgress): Boolean { | ||
| val currentLegIndex = routeProgress.currentLegProgress()?.legIndex() | ||
| val lastLegIndex = routeProgress.route()?.legs()?.lastIndex | ||
| return (currentLegIndex != null && lastLegIndex != null) && currentLegIndex < lastLegIndex | ||
| } | ||
|
|
||
| private fun checkStopArrivalTime(arrivalInSeconds: Double, routeLegProgress: RouteLegProgress) { | ||
| if (routeLegProgress.durationRemaining() <= arrivalInSeconds) { | ||
|
kmadsen marked this conversation as resolved.
|
||
| doOnStopArrival(routeLegProgress) | ||
| } | ||
| } | ||
|
|
||
| private fun checkStopArrivalDistance(arrivalInMeters: Double, routeLegProgress: RouteLegProgress) { | ||
| if (routeLegProgress.distanceRemaining() <= arrivalInMeters) { | ||
|
kmadsen marked this conversation as resolved.
|
||
| doOnStopArrival(routeLegProgress) | ||
| } | ||
| } | ||
|
|
||
| private fun doOnStopArrival(routeLegProgress: RouteLegProgress) { | ||
| val moveToNextLeg = arrivalController.onStopArrival(routeLegProgress) | ||
| if (moveToNextLeg) { | ||
| navigateNextRouteLeg() | ||
| } | ||
|
kmadsen marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private fun doOnRouteArrival(routeProgress: RouteProgress) { | ||
| if (!arrivedForRoute) { | ||
| arrivalController.onRouteArrival(routeProgress) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.