-
Notifications
You must be signed in to change notification settings - Fork 319
Remove stopTripSession from ReplayRouteSession #6817
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Improved `MapboxNavigation#startReplayTripSession` and `ReplayRouteSession` so that the previous trip session does not need to be stopped. :warning: `ReplayRouteSession#onDetached` removed the call to `stopTripSession`. |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -267,6 +267,28 @@ internal class MapboxTripSession( | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| private val onRawLocationUpdate: (Location) -> Unit = { rawLocation -> | ||||||||
| val locationHash = rawLocation.hashCode() | ||||||||
| logD( | ||||||||
| "updateRawLocation; system elapsed time: ${System.nanoTime()}; " + | ||||||||
| "location ($locationHash) elapsed time: ${rawLocation.elapsedRealtimeNanos}", | ||||||||
| LOG_CATEGORY | ||||||||
| ) | ||||||||
| this.rawLocation = rawLocation | ||||||||
| locationObservers.forEach { it.onNewRawLocation(rawLocation) } | ||||||||
| mainJobController.scope.launch(start = CoroutineStart.UNDISPATCHED) { | ||||||||
| logD( | ||||||||
| "updateRawLocation; notify navigator for ($locationHash) - start", | ||||||||
| LOG_CATEGORY | ||||||||
| ) | ||||||||
| navigator.updateLocation(rawLocation.toFixLocation()) | ||||||||
| logD( | ||||||||
| "updateRawLocation; notify navigator for ($locationHash) - end", | ||||||||
| LOG_CATEGORY | ||||||||
| ) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| init { | ||||||||
| navigator.setNativeNavigatorRecreationObserver { | ||||||||
| if (fallbackVersionsObservers.isNotEmpty()) { | ||||||||
|
|
@@ -297,39 +319,14 @@ internal class MapboxTripSession( | |||||||
| * Start MapboxTripSession | ||||||||
| */ | ||||||||
| override fun start(withTripService: Boolean, withReplayEnabled: Boolean) { | ||||||||
| if (state == TripSessionState.STARTED) { | ||||||||
| return | ||||||||
| } | ||||||||
| navigator.addNavigatorObserver(navigatorObserver) | ||||||||
| if (withTripService) { | ||||||||
| tripService.startService() | ||||||||
| } | ||||||||
| tripSessionLocationEngine.startLocationUpdates(withReplayEnabled) { | ||||||||
| updateRawLocation(it) | ||||||||
| } | ||||||||
| state = TripSessionState.STARTED | ||||||||
| } | ||||||||
|
|
||||||||
| private fun updateRawLocation(rawLocation: Location) { | ||||||||
| val locationHash = rawLocation.hashCode() | ||||||||
| logD( | ||||||||
| "updateRawLocation; system elapsed time: ${System.nanoTime()}; " + | ||||||||
| "location ($locationHash) elapsed time: ${rawLocation.elapsedRealtimeNanos}", | ||||||||
| LOG_CATEGORY | ||||||||
| ) | ||||||||
| this.rawLocation = rawLocation | ||||||||
| locationObservers.forEach { it.onNewRawLocation(rawLocation) } | ||||||||
| mainJobController.scope.launch(start = CoroutineStart.UNDISPATCHED) { | ||||||||
| logD( | ||||||||
| "updateRawLocation; notify navigator for ($locationHash) - start", | ||||||||
| LOG_CATEGORY | ||||||||
| ) | ||||||||
| navigator.updateLocation(rawLocation.toFixLocation()) | ||||||||
| logD( | ||||||||
| "updateRawLocation; notify navigator for ($locationHash) - end", | ||||||||
| LOG_CATEGORY | ||||||||
| ) | ||||||||
| if (state != TripSessionState.STARTED) { | ||||||||
| navigator.addNavigatorObserver(navigatorObserver) | ||||||||
| if (withTripService) { | ||||||||
| tripService.startService() | ||||||||
| } | ||||||||
| state = TripSessionState.STARTED | ||||||||
| } | ||||||||
| tripSessionLocationEngine.startLocationUpdates(withReplayEnabled, onRawLocationUpdate) | ||||||||
kmadsen marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we need to call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 yeah. A new location engine seems like a good reason to reset. I'm hesitant, should it go inside the Lines 117 to 119 in 61415ea
|
||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,55 +24,60 @@ import java.util.concurrent.TimeUnit | |
| * trip session is not using replay, use the [NavigationOptions.locationEngine]. | ||
| */ | ||
| internal class TripSessionLocationEngine constructor( | ||
| val navigationOptions: NavigationOptions | ||
| private val navigationOptions: NavigationOptions, | ||
| private val replayLocationEngineProvider: (MapboxReplayer) -> LocationEngine = { | ||
| ReplayLocationEngine(it) | ||
| } | ||
| ) { | ||
|
|
||
| val mapboxReplayer: MapboxReplayer by lazy { MapboxReplayer() } | ||
|
|
||
| private val replayLocationEngine: ReplayLocationEngine by lazy { | ||
| ReplayLocationEngine(mapboxReplayer) | ||
| private val replayLocationEngine: LocationEngine by lazy { | ||
| replayLocationEngineProvider.invoke(mapboxReplayer) | ||
| } | ||
| private var locationEngine: LocationEngine = navigationOptions.locationEngine | ||
| private var activeLocationEngine: LocationEngine? = null | ||
| private var onRawLocationUpdate: (Location) -> Unit = { } | ||
|
|
||
| private val locationEngineCallback = object : LocationEngineCallback<LocationEngineResult> { | ||
| override fun onSuccess(result: LocationEngineResult?) { | ||
| logD(LOG_CATEGORY) { | ||
| "successful location engine callback $result" | ||
| } | ||
| result?.locations?.lastOrNull()?.let { | ||
| logIfLocationIsNotFreshEnough(it) | ||
| onRawLocationUpdate(it) | ||
| } | ||
| } | ||
|
|
||
| override fun onFailure(exception: Exception) { | ||
| logD("location on failure exception=$exception", LOG_CATEGORY) | ||
| } | ||
| } | ||
|
|
||
| @SuppressLint("MissingPermission") | ||
| fun startLocationUpdates(isReplayEnabled: Boolean, onRawLocationUpdate: (Location) -> Unit) { | ||
| logD(LOG_CATEGORY) { | ||
| "starting location updates for ${if (isReplayEnabled) "replay " else ""}location engine" | ||
| } | ||
| stopLocationUpdates() | ||
| this.onRawLocationUpdate = onRawLocationUpdate | ||
| val locationEngine = if (isReplayEnabled) { | ||
| activeLocationEngine = if (isReplayEnabled) { | ||
| replayLocationEngine | ||
| } else { | ||
| navigationOptions.locationEngine | ||
| } | ||
| locationEngine.requestLocationUpdates( | ||
| activeLocationEngine?.requestLocationUpdates( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is built to manage a single location engine subscription, because it would be sending duplicates if it had more than one subscription. The There could be improvements to the logic, but i'm not seeing why it would be needed. Do you see an issue?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm only saying that it's not obvious how to use this API. It looks like you can invoke |
||
| navigationOptions.locationEngineRequest, | ||
| locationEngineCallback, | ||
| Looper.getMainLooper() | ||
| ) | ||
| locationEngine.getLastLocation(locationEngineCallback) | ||
| activeLocationEngine?.getLastLocation(locationEngineCallback) | ||
| } | ||
|
|
||
| fun stopLocationUpdates() { | ||
| onRawLocationUpdate = { } | ||
| locationEngine.removeLocationUpdates(locationEngineCallback) | ||
| } | ||
|
|
||
| private var locationEngineCallback = object : LocationEngineCallback<LocationEngineResult> { | ||
| override fun onSuccess(result: LocationEngineResult?) { | ||
| logD(LOG_CATEGORY) { | ||
| "successful location engine callback $result" | ||
| } | ||
| result?.locations?.lastOrNull()?.let { | ||
| logIfLocationIsNotFreshEnough(it) | ||
| onRawLocationUpdate(it) | ||
| } | ||
| } | ||
|
|
||
| override fun onFailure(exception: Exception) { | ||
| logD("location on failure exception=$exception", LOG_CATEGORY) | ||
| } | ||
| activeLocationEngine?.removeLocationUpdates(locationEngineCallback) | ||
| activeLocationEngine = null | ||
| } | ||
|
|
||
| private fun logIfLocationIsNotFreshEnough(location: Location) { | ||
|
|
@@ -88,7 +93,6 @@ internal class TripSessionLocationEngine constructor( | |
| } | ||
|
|
||
| private companion object { | ||
|
|
||
| private const val DELAYED_LOCATION_WARNING_THRESHOLD_MS = 500 // 0.5s | ||
| private const val LOG_CATEGORY = "TripSessionLocationEngine" | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.