|
| 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.trip.MapboxTripStarterExtra.MAPBOX_TRIP_STARTER_FOLLOW_DEVICE |
| 11 | +import com.mapbox.navigation.core.trip.MapboxTripStarterExtra.MAPBOX_TRIP_STARTER_REPLAY_ROUTE |
| 12 | +import com.mapbox.navigation.core.trip.session.TripSessionState |
| 13 | +import kotlinx.coroutines.CoroutineScope |
| 14 | +import kotlinx.coroutines.Dispatchers |
| 15 | +import kotlinx.coroutines.SupervisorJob |
| 16 | +import kotlinx.coroutines.cancel |
| 17 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 18 | +import kotlinx.coroutines.flow.collect |
| 19 | +import kotlinx.coroutines.flow.update |
| 20 | +import kotlinx.coroutines.launch |
| 21 | + |
| 22 | +/** |
| 23 | + * This makes it simpler to start a MapboxNavigation trip session. It can be used to enable replay |
| 24 | + * or to follow the device location. The [MapboxTripStarter] is not able to observe when location |
| 25 | + * permissions change, so you must notify it of changes. It will check location permissions upon |
| 26 | + * attach. The location permissions are not required for replay sessions. |
| 27 | + * |
| 28 | + * In order to share the state between an App and Android Auto, it is recommended to make this |
| 29 | + * class a singleton. That will be done automatically if you use [getRegisteredInstance]. |
| 30 | + */ |
| 31 | +@ExperimentalPreviewMapboxNavigationAPI |
| 32 | +@SuppressLint("MissingPermission") |
| 33 | +class MapboxTripStarter private constructor(): MapboxNavigationObserver { |
| 34 | + |
| 35 | + private val optionsFlow = MutableStateFlow(MapboxTripStarterOptions.Builder().build()) |
| 36 | + private var replayRouteTripSession: ReplayRouteSession? = null |
| 37 | + private var mapboxNavigation: MapboxNavigation? = null |
| 38 | + |
| 39 | + private lateinit var coroutineScope: CoroutineScope |
| 40 | + |
| 41 | + /** |
| 42 | + * Signals that the [mapboxNavigation] instance is ready for use. |
| 43 | + * @param mapboxNavigation |
| 44 | + */ |
| 45 | + override fun onAttached(mapboxNavigation: MapboxNavigation) { |
| 46 | + this.mapboxNavigation = mapboxNavigation |
| 47 | + coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) |
| 48 | + |
| 49 | + // Initialize the options to be aware of the location permissions |
| 50 | + val context = mapboxNavigation.navigationOptions.applicationContext |
| 51 | + val granted = PermissionsManager.areLocationPermissionsGranted(context) |
| 52 | + optionsFlow.update { it.toBuilder().isLocationPermissionGranted(granted).build() } |
| 53 | + |
| 54 | + // Observe any changes to the options |
| 55 | + coroutineScope.launch { |
| 56 | + optionsFlow.collect { options -> |
| 57 | + onStarterOptionsChanged(mapboxNavigation, options) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Signals that the [mapboxNavigation] instance is being detached. |
| 64 | + * @param mapboxNavigation |
| 65 | + */ |
| 66 | + override fun onDetached(mapboxNavigation: MapboxNavigation) { |
| 67 | + coroutineScope.cancel() |
| 68 | + onTripDisabled(mapboxNavigation) |
| 69 | + this.mapboxNavigation = null |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get the options that are currently set. |
| 74 | + */ |
| 75 | + fun getOptions(): MapboxTripStarterOptions = optionsFlow.value |
| 76 | + |
| 77 | + /** |
| 78 | + * Set new options. |
| 79 | + */ |
| 80 | + fun setOptions(options: MapboxTripStarterOptions) = apply { |
| 81 | + checkOptions(options) |
| 82 | + this.optionsFlow.value = options |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Apply changes to existing options. |
| 87 | + */ |
| 88 | + fun update(function: (MapboxTripStarterOptions.Builder) -> MapboxTripStarterOptions.Builder) { |
| 89 | + this.optionsFlow.update { |
| 90 | + val nextOptions = function.invoke(it.toBuilder()).build() |
| 91 | + checkOptions(nextOptions) |
| 92 | + nextOptions |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Throws an error if location permissions are set to true but the location permissions are |
| 98 | + * not actually granted. |
| 99 | + */ |
| 100 | + private fun checkOptions(options: MapboxTripStarterOptions) { |
| 101 | + val mapboxNavigation = this.mapboxNavigation |
| 102 | + checkNotNull(mapboxNavigation) { |
| 103 | + "MapboxTripStarter cannot be used while MapboxNavigation is detached." |
| 104 | + } |
| 105 | + val context = mapboxNavigation.navigationOptions.applicationContext |
| 106 | + val granted = options.isLocationPermissionGranted |
| 107 | + if (granted && !PermissionsManager.areLocationPermissionsGranted(context)) { |
| 108 | + error( |
| 109 | + "updateLocationPermissions can only be set to true when location permissions" + |
| 110 | + " have been accepted." |
| 111 | + ) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private fun onStarterOptionsChanged( |
| 116 | + mapboxNavigation: MapboxNavigation, |
| 117 | + options: MapboxTripStarterOptions |
| 118 | + ) { |
| 119 | + if (options.tripType == MAPBOX_TRIP_STARTER_REPLAY_ROUTE) { |
| 120 | + onReplayTripEnabled(mapboxNavigation) |
| 121 | + } else if (options.tripType == MAPBOX_TRIP_STARTER_FOLLOW_DEVICE && |
| 122 | + options.isLocationPermissionGranted |
| 123 | + ) { |
| 124 | + onTripSessionEnabled(mapboxNavigation) |
| 125 | + } else { |
| 126 | + onTripDisabled(mapboxNavigation) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private fun onReplayTripEnabled(mapboxNavigation: MapboxNavigation) { |
| 131 | + replayRouteTripSession?.onDetached(mapboxNavigation) |
| 132 | + replayRouteTripSession = ReplayRouteSession().also { |
| 133 | + it.onAttached(mapboxNavigation) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private fun onTripSessionEnabled(mapboxNavigation: MapboxNavigation) { |
| 138 | + replayRouteTripSession?.onDetached(mapboxNavigation) |
| 139 | + replayRouteTripSession = null |
| 140 | + if (mapboxNavigation.getTripSessionState() != TripSessionState.STARTED) { |
| 141 | + mapboxNavigation.startTripSession() |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private fun onTripDisabled(mapboxNavigation: MapboxNavigation) { |
| 146 | + replayRouteTripSession?.onDetached(mapboxNavigation) |
| 147 | + replayRouteTripSession = null |
| 148 | + mapboxNavigation.stopTripSession() |
| 149 | + } |
| 150 | + |
| 151 | + companion object { |
| 152 | + |
| 153 | + /** |
| 154 | + * Construct an instance without registering to [MapboxNavigationApp]. |
| 155 | + */ |
| 156 | + @JvmStatic |
| 157 | + fun create() = MapboxTripStarter() |
| 158 | + |
| 159 | + /** |
| 160 | + * Get the registered instance or create one and register it to [MapboxNavigationApp]. |
| 161 | + */ |
| 162 | + @JvmStatic |
| 163 | + fun getRegisteredInstance(): MapboxTripStarter = MapboxNavigationApp |
| 164 | + .getObservers(MapboxTripStarter::class) |
| 165 | + .firstOrNull() ?: create().also { MapboxNavigationApp.registerObserver(it) } |
| 166 | + } |
| 167 | +} |
0 commit comments