|
| 1 | +package com.mapbox.navigation.core.trip |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import com.mapbox.android.core.permissions.PermissionsManager |
| 5 | +import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI |
| 6 | +import com.mapbox.navigation.core.MapboxNavigation |
| 7 | +import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp |
| 8 | +import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver |
| 9 | +import com.mapbox.navigation.core.replay.route.ReplayRouteSession |
| 10 | +import com.mapbox.navigation.core.replay.route.ReplayRouteSessionOptions |
| 11 | +import com.mapbox.navigation.core.trip.MapboxTripStarter.Companion.getRegisteredInstance |
| 12 | +import com.mapbox.navigation.utils.internal.logI |
| 13 | +import kotlinx.coroutines.CoroutineScope |
| 14 | +import kotlinx.coroutines.Dispatchers |
| 15 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 16 | +import kotlinx.coroutines.SupervisorJob |
| 17 | +import kotlinx.coroutines.cancel |
| 18 | +import kotlinx.coroutines.flow.Flow |
| 19 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 20 | +import kotlinx.coroutines.flow.flatMapLatest |
| 21 | +import kotlinx.coroutines.flow.launchIn |
| 22 | +import kotlinx.coroutines.flow.onEach |
| 23 | + |
| 24 | +/** |
| 25 | + * The [MapboxTripStarter] makes it simpler to switch between a trip session and replay. |
| 26 | + * |
| 27 | + * This is not able to observe when location permissions change, so you may need to refresh the |
| 28 | + * state with [refreshLocationPermissions]. Location permissions are not required for replay. |
| 29 | + * |
| 30 | + * There should be one instance of this class at a time. For example, an app Activity and car |
| 31 | + * Session will need to use the same instance. That will be done automatically if you use |
| 32 | + * [getRegisteredInstance]. |
| 33 | + */ |
| 34 | +@ExperimentalPreviewMapboxNavigationAPI |
| 35 | +class MapboxTripStarter internal constructor() : MapboxNavigationObserver { |
| 36 | + |
| 37 | + private val tripType = MutableStateFlow<MapboxTripStarterType>( |
| 38 | + MapboxTripStarterType.MapMatching |
| 39 | + ) |
| 40 | + private val replayRouteSessionOptions = MutableStateFlow( |
| 41 | + ReplayRouteSessionOptions.Builder().build() |
| 42 | + ) |
| 43 | + private val isLocationPermissionGranted = MutableStateFlow(false) |
| 44 | + private var replayRouteTripSession: ReplayRouteSession? = null |
| 45 | + private var mapboxNavigation: MapboxNavigation? = null |
| 46 | + |
| 47 | + private lateinit var coroutineScope: CoroutineScope |
| 48 | + |
| 49 | + /** |
| 50 | + * Signals that the [mapboxNavigation] instance is ready for use. |
| 51 | + * |
| 52 | + * @param mapboxNavigation |
| 53 | + */ |
| 54 | + override fun onAttached(mapboxNavigation: MapboxNavigation) { |
| 55 | + this.mapboxNavigation = mapboxNavigation |
| 56 | + coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) |
| 57 | + |
| 58 | + // Initialize the options to be aware of the location permissions |
| 59 | + val context = mapboxNavigation.navigationOptions.applicationContext |
| 60 | + val granted = PermissionsManager.areLocationPermissionsGranted(context) |
| 61 | + isLocationPermissionGranted.value = granted |
| 62 | + |
| 63 | + // Observe changes to state |
| 64 | + observeStateFlow(mapboxNavigation).launchIn(coroutineScope) |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Signals that the [mapboxNavigation] instance is being detached. |
| 69 | + * |
| 70 | + * @param mapboxNavigation |
| 71 | + */ |
| 72 | + override fun onDetached(mapboxNavigation: MapboxNavigation) { |
| 73 | + coroutineScope.cancel() |
| 74 | + onTripDisabled(mapboxNavigation) |
| 75 | + this.mapboxNavigation = null |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * [enableMapMatching] will not work unless location permissions have been granted. Refresh |
| 80 | + * the location permissions after they are granted to ensure the trip session will start. |
| 81 | + */ |
| 82 | + fun refreshLocationPermissions() = apply { |
| 83 | + mapboxNavigation?.navigationOptions?.applicationContext?.let { context -> |
| 84 | + val granted = PermissionsManager.areLocationPermissionsGranted(context) |
| 85 | + isLocationPermissionGranted.value = granted |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * This is the default mode for the [MapboxTripStarter]. This can be used to disable |
| 91 | + * [enableReplayRoute]. Make sure location permissions have been accepted or this will have no |
| 92 | + * effect on the experience. |
| 93 | + */ |
| 94 | + fun enableMapMatching() = apply { |
| 95 | + if (!isLocationPermissionGranted.value) { |
| 96 | + refreshLocationPermissions() |
| 97 | + } |
| 98 | + tripType.value = MapboxTripStarterType.MapMatching |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get the current [ReplayRouteSessionOptions]. This can be used with [enableReplayRoute] to |
| 103 | + * make minor adjustments to the current options. |
| 104 | + */ |
| 105 | + fun getReplayRouteSessionOptions(): ReplayRouteSessionOptions = replayRouteSessionOptions.value |
| 106 | + |
| 107 | + /** |
| 108 | + * Enables a mode where the primary route is simulated by an artificial driver. Set the route |
| 109 | + * with [MapboxNavigation.setNavigationRoutes]. Can be used with [getReplayRouteSessionOptions] |
| 110 | + * to make minor adjustments to the current options. |
| 111 | + * |
| 112 | + * @param options optional options to use for route replay. |
| 113 | + */ |
| 114 | + fun enableReplayRoute( |
| 115 | + options: ReplayRouteSessionOptions? = null |
| 116 | + ) = apply { |
| 117 | + options?.let { options -> replayRouteSessionOptions.value = options } |
| 118 | + tripType.value = MapboxTripStarterType.ReplayRoute |
| 119 | + } |
| 120 | + |
| 121 | + @OptIn(ExperimentalCoroutinesApi::class) |
| 122 | + private fun observeStateFlow(mapboxNavigation: MapboxNavigation): Flow<*> { |
| 123 | + return tripType.flatMapLatest { tripType -> |
| 124 | + when (tripType) { |
| 125 | + MapboxTripStarterType.ReplayRoute -> |
| 126 | + replayRouteSessionOptions.onEach { options -> |
| 127 | + onReplayTripEnabled(mapboxNavigation, options) |
| 128 | + } |
| 129 | + MapboxTripStarterType.MapMatching -> |
| 130 | + isLocationPermissionGranted.onEach { granted -> |
| 131 | + onMapMatchingEnabled(mapboxNavigation, granted) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Internally called when the trip type has been set to replay route. |
| 139 | + * |
| 140 | + * @param mapboxNavigation |
| 141 | + * @param options parameters for the [ReplayRouteSession] |
| 142 | + */ |
| 143 | + private fun onReplayTripEnabled( |
| 144 | + mapboxNavigation: MapboxNavigation, |
| 145 | + options: ReplayRouteSessionOptions |
| 146 | + ) { |
| 147 | + replayRouteTripSession?.onDetached(mapboxNavigation) |
| 148 | + replayRouteTripSession = ReplayRouteSession().also { |
| 149 | + it.setOptions(options) |
| 150 | + it.onAttached(mapboxNavigation) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Internally called when the trip type has been set to map matching. |
| 156 | + * |
| 157 | + * @param mapboxNavigation |
| 158 | + * @param granted true when location permissions are accepted, false otherwise |
| 159 | + */ |
| 160 | + @SuppressLint("MissingPermission") |
| 161 | + private fun onMapMatchingEnabled(mapboxNavigation: MapboxNavigation, granted: Boolean) { |
| 162 | + if (granted) { |
| 163 | + replayRouteTripSession?.onDetached(mapboxNavigation) |
| 164 | + replayRouteTripSession = null |
| 165 | + mapboxNavigation.startTripSession() |
| 166 | + } else { |
| 167 | + logI(LOG_CATEGORY) { |
| 168 | + "startTripSession was not called. Accept location permissions and call " + |
| 169 | + "mapboxTripStarter.refreshLocationPermissions()" |
| 170 | + } |
| 171 | + onTripDisabled(mapboxNavigation) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Internally called when the trip session needs to be stopped. |
| 177 | + * |
| 178 | + * @param mapboxNavigation |
| 179 | + */ |
| 180 | + private fun onTripDisabled(mapboxNavigation: MapboxNavigation) { |
| 181 | + replayRouteTripSession?.onDetached(mapboxNavigation) |
| 182 | + replayRouteTripSession = null |
| 183 | + mapboxNavigation.stopTripSession() |
| 184 | + } |
| 185 | + |
| 186 | + companion object { |
| 187 | + private const val LOG_CATEGORY = "MapboxTripStarter" |
| 188 | + |
| 189 | + /** |
| 190 | + * Construct an instance without registering to [MapboxNavigationApp]. |
| 191 | + */ |
| 192 | + @JvmStatic |
| 193 | + fun create() = MapboxTripStarter() |
| 194 | + |
| 195 | + /** |
| 196 | + * Get the registered instance or create one and register it to [MapboxNavigationApp]. |
| 197 | + */ |
| 198 | + @JvmStatic |
| 199 | + fun getRegisteredInstance(): MapboxTripStarter = MapboxNavigationApp |
| 200 | + .getObservers(MapboxTripStarter::class) |
| 201 | + .firstOrNull() ?: MapboxTripStarter().also { MapboxNavigationApp.registerObserver(it) } |
| 202 | + } |
| 203 | +} |
0 commit comments