-
Notifications
You must be signed in to change notification settings - Fork 320
Add a MapboxTripStarter #6794
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
Add a MapboxTripStarter #6794
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Added `MapboxTripStarter` to simplify the solution for managing the trip session and replaying routes. This also makes it possible to share the replay state between drop-in-ui and android-auto. |
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
203 changes: 203 additions & 0 deletions
203
libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/MapboxTripStarter.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,203 @@ | ||
package com.mapbox.navigation.core.trip | ||
|
||
import android.annotation.SuppressLint | ||
import com.mapbox.android.core.permissions.PermissionsManager | ||
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI | ||
import com.mapbox.navigation.core.MapboxNavigation | ||
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp | ||
import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver | ||
import com.mapbox.navigation.core.replay.route.ReplayRouteSession | ||
import com.mapbox.navigation.core.replay.route.ReplayRouteSessionOptions | ||
import com.mapbox.navigation.core.trip.MapboxTripStarter.Companion.getRegisteredInstance | ||
import com.mapbox.navigation.utils.internal.logI | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
|
||
/** | ||
* The [MapboxTripStarter] makes it simpler to switch between a trip session and replay. | ||
* | ||
* This is not able to observe when location permissions change, so you may need to refresh the | ||
* state with [refreshLocationPermissions]. Location permissions are not required for replay. | ||
* | ||
* There should be one instance of this class at a time. For example, an app Activity and car | ||
* Session will need to use the same instance. That will be done automatically if you use | ||
* [getRegisteredInstance]. | ||
*/ | ||
@ExperimentalPreviewMapboxNavigationAPI | ||
class MapboxTripStarter internal constructor() : MapboxNavigationObserver { | ||
|
||
private val tripType = MutableStateFlow<MapboxTripStarterType>( | ||
MapboxTripStarterType.MapMatching | ||
) | ||
private val replayRouteSessionOptions = MutableStateFlow( | ||
ReplayRouteSessionOptions.Builder().build() | ||
) | ||
private val isLocationPermissionGranted = MutableStateFlow(false) | ||
private var replayRouteTripSession: ReplayRouteSession? = null | ||
private var mapboxNavigation: MapboxNavigation? = null | ||
kmadsen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private lateinit var coroutineScope: CoroutineScope | ||
|
||
/** | ||
* Signals that the [mapboxNavigation] instance is ready for use. | ||
* | ||
* @param mapboxNavigation | ||
*/ | ||
override fun onAttached(mapboxNavigation: MapboxNavigation) { | ||
this.mapboxNavigation = mapboxNavigation | ||
coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) | ||
kmadsen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Initialize the options to be aware of the location permissions | ||
val context = mapboxNavigation.navigationOptions.applicationContext | ||
val granted = PermissionsManager.areLocationPermissionsGranted(context) | ||
isLocationPermissionGranted.value = granted | ||
|
||
// Observe changes to state | ||
observeStateFlow(mapboxNavigation).launchIn(coroutineScope) | ||
} | ||
|
||
/** | ||
* Signals that the [mapboxNavigation] instance is being detached. | ||
* | ||
* @param mapboxNavigation | ||
*/ | ||
override fun onDetached(mapboxNavigation: MapboxNavigation) { | ||
coroutineScope.cancel() | ||
onTripDisabled(mapboxNavigation) | ||
this.mapboxNavigation = null | ||
} | ||
|
||
/** | ||
* [enableMapMatching] will not work unless location permissions have been granted. Refresh | ||
* the location permissions after they are granted to ensure the trip session will start. | ||
*/ | ||
fun refreshLocationPermissions() = apply { | ||
mapboxNavigation?.navigationOptions?.applicationContext?.let { context -> | ||
val granted = PermissionsManager.areLocationPermissionsGranted(context) | ||
isLocationPermissionGranted.value = granted | ||
} | ||
} | ||
|
||
/** | ||
* This is the default mode for the [MapboxTripStarter]. This can be used to disable | ||
* [enableReplayRoute]. Make sure location permissions have been accepted or this will have no | ||
* effect on the experience. | ||
*/ | ||
fun enableMapMatching() = apply { | ||
if (!isLocationPermissionGranted.value) { | ||
refreshLocationPermissions() | ||
} | ||
tripType.value = MapboxTripStarterType.MapMatching | ||
} | ||
|
||
/** | ||
* Get the current [ReplayRouteSessionOptions]. This can be used with [enableReplayRoute] to | ||
* make minor adjustments to the current options. | ||
*/ | ||
fun getReplayRouteSessionOptions(): ReplayRouteSessionOptions = replayRouteSessionOptions.value | ||
|
||
/** | ||
* Enables a mode where the primary route is simulated by an artificial driver. Set the route | ||
* with [MapboxNavigation.setNavigationRoutes]. Can be used with [getReplayRouteSessionOptions] | ||
* to make minor adjustments to the current options. | ||
* | ||
* @param options optional options to use for route replay. | ||
*/ | ||
fun enableReplayRoute( | ||
options: ReplayRouteSessionOptions? = null | ||
) = apply { | ||
options?.let { options -> replayRouteSessionOptions.value = options } | ||
tripType.value = MapboxTripStarterType.ReplayRoute | ||
} | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
private fun observeStateFlow(mapboxNavigation: MapboxNavigation): Flow<*> { | ||
return tripType.flatMapLatest { tripType -> | ||
when (tripType) { | ||
MapboxTripStarterType.ReplayRoute -> | ||
replayRouteSessionOptions.onEach { options -> | ||
onReplayTripEnabled(mapboxNavigation, options) | ||
} | ||
MapboxTripStarterType.MapMatching -> | ||
isLocationPermissionGranted.onEach { granted -> | ||
onMapMatchingEnabled(mapboxNavigation, granted) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Internally called when the trip type has been set to replay route. | ||
* | ||
* @param mapboxNavigation | ||
* @param options parameters for the [ReplayRouteSession] | ||
*/ | ||
private fun onReplayTripEnabled( | ||
mapboxNavigation: MapboxNavigation, | ||
options: ReplayRouteSessionOptions | ||
) { | ||
replayRouteTripSession?.onDetached(mapboxNavigation) | ||
replayRouteTripSession = ReplayRouteSession().also { | ||
it.setOptions(options) | ||
it.onAttached(mapboxNavigation) | ||
} | ||
} | ||
|
||
/** | ||
* Internally called when the trip type has been set to map matching. | ||
* | ||
* @param mapboxNavigation | ||
* @param granted true when location permissions are accepted, false otherwise | ||
*/ | ||
@SuppressLint("MissingPermission") | ||
private fun onMapMatchingEnabled(mapboxNavigation: MapboxNavigation, granted: Boolean) { | ||
if (granted) { | ||
replayRouteTripSession?.onDetached(mapboxNavigation) | ||
replayRouteTripSession = null | ||
mapboxNavigation.startTripSession() | ||
} else { | ||
logI(LOG_CATEGORY) { | ||
"startTripSession was not called. Accept location permissions and call " + | ||
"mapboxTripStarter.refreshLocationPermissions()" | ||
} | ||
onTripDisabled(mapboxNavigation) | ||
} | ||
} | ||
|
||
/** | ||
* Internally called when the trip session needs to be stopped. | ||
* | ||
* @param mapboxNavigation | ||
*/ | ||
private fun onTripDisabled(mapboxNavigation: MapboxNavigation) { | ||
replayRouteTripSession?.onDetached(mapboxNavigation) | ||
replayRouteTripSession = null | ||
mapboxNavigation.stopTripSession() | ||
} | ||
|
||
companion object { | ||
private const val LOG_CATEGORY = "MapboxTripStarter" | ||
|
||
/** | ||
* Construct an instance without registering to [MapboxNavigationApp]. | ||
*/ | ||
@JvmStatic | ||
fun create() = MapboxTripStarter() | ||
|
||
/** | ||
* Get the registered instance or create one and register it to [MapboxNavigationApp]. | ||
*/ | ||
@JvmStatic | ||
fun getRegisteredInstance(): MapboxTripStarter = MapboxNavigationApp | ||
.getObservers(MapboxTripStarter::class) | ||
.firstOrNull() ?: MapboxTripStarter().also { MapboxNavigationApp.registerObserver(it) } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/MapboxTripStarterType.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,17 @@ | ||
package com.mapbox.navigation.core.trip | ||
|
||
/** | ||
* Specifies a trip type for the [MapboxTripStarter]. | ||
*/ | ||
internal sealed class MapboxTripStarterType { | ||
|
||
/** | ||
* The [MapboxTripStarter] will use the best device location for a trip session. | ||
*/ | ||
object MapMatching : MapboxTripStarterType() | ||
|
||
/** | ||
* The [MapboxTripStarter] will enable replay for the navigation routes. | ||
*/ | ||
object ReplayRoute : MapboxTripStarterType() | ||
} |
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.