Skip to content

Commit 0c51c9a

Browse files
authored
NavigatorObserver: fix race condition on invoking TripSession#updateLegIndex for voice instructions (#6689)
1 parent 85be6da commit 0c51c9a

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Mapbox welcomes participation and contributions from everyone.
55
## Unreleased
66
#### Features
77
#### Bug fixes and improvements
8-
- Fixed `BannerInstructions` issue where the banner instruction might have been removed from `RouteProgress` at some point around a edge's leg. [#6684](https://github.com/mapbox/mapbox-navigation-android/pull/6684)
8+
- Fixed an issue where `RouteProgress#BannerInstructions` could've become `null` when `MapboxNavigation#updateLegIndex` was called. [#6684](https://github.com/mapbox/mapbox-navigation-android/pull/6684)
9+
- Fixed an issue where `RouteProgress#VoiceInstructions` could've become `null` when `MapboxNavigation#updateLegIndex` was called. [#6689](https://github.com/mapbox/mapbox-navigation-android/pull/6689)
910

1011
## Mapbox Navigation SDK 2.10.0-beta.2 - 01 December, 2022
1112
### Changelog

libnavigation-core/src/main/java/com/mapbox/navigation/core/trip/session/MapboxTripSession.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ internal class MapboxTripSession(
179179
this@MapboxTripSession.primaryRoute = newPrimaryRoute
180180
roadObjects = newPrimaryRoute?.upcomingRoadObjects ?: emptyList()
181181
isOffRoute = false
182-
invalidateLatestInstructions(bannerInstructionEvent.latestInstructionWrapper)
182+
invalidateLatestInstructions(
183+
bannerInstructionEvent.latestInstructionWrapper,
184+
lastVoiceInstruction
185+
)
183186
routeProgress = null
184187
}.mapValue {
185188
it.alternatives
@@ -204,7 +207,9 @@ internal class MapboxTripSession(
204207
private val fallbackVersionsObservers = CopyOnWriteArraySet<FallbackVersionsObserver>()
205208

206209
private val bannerInstructionEvent = BannerInstructionEvent()
207-
private var lastVoiceInstruction: VoiceInstructions? = null
210+
211+
@VisibleForTesting
212+
internal var lastVoiceInstruction: VoiceInstructions? = null
208213

209214
private var state: TripSessionState = TripSessionState.STOPPED
210215
set(value) {
@@ -590,9 +595,10 @@ internal class MapboxTripSession(
590595
updateLegIndexJob = mainJobController.scope.launch {
591596
try {
592597
val latestInstructionWrapper = bannerInstructionEvent.latestInstructionWrapper
598+
val lastVoiceInstruction = lastVoiceInstruction
593599
legIndexUpdated = navigator.updateLegIndex(legIndex)
594600
if (legIndexUpdated) {
595-
invalidateLatestInstructions(latestInstructionWrapper)
601+
invalidateLatestInstructions(latestInstructionWrapper, lastVoiceInstruction)
596602
}
597603
} finally {
598604
callback.onLegIndexUpdatedCallback(legIndexUpdated)
@@ -701,14 +707,18 @@ internal class MapboxTripSession(
701707
}
702708

703709
/**
704-
* Invalidate latest banner instruction. To get the latest banner instruction wrapper call
705-
* [BannerInstructionEvent.latestInstructionWrapper]
710+
* Invalidate latest banner and voice instruction. To get the latest banner instruction wrapper call
711+
* [BannerInstructionEvent.latestInstructionWrapper], to get the latest voice instruction
712+
* call [lastVoiceInstruction]
706713
*/
707714
private fun invalidateLatestInstructions(
708-
latestInstructionWrapper: BannerInstructionEvent.LatestInstructionWrapper?
715+
latestInstructionWrapper: BannerInstructionEvent.LatestInstructionWrapper?,
716+
voiceInstruction: VoiceInstructions?,
709717
) {
710718
bannerInstructionEvent.invalidateLatestBannerInstructions(latestInstructionWrapper)
711-
lastVoiceInstruction = null
719+
if (lastVoiceInstruction == voiceInstruction) {
720+
lastVoiceInstruction = null
721+
}
712722
}
713723

714724
private fun checkBannerInstructionEvent(

libnavigation-core/src/test/java/com/mapbox/navigation/core/trip/session/MapboxTripSessionTest.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,7 @@ class MapboxTripSessionTest {
16701670
latest banner instructions to remove legacy instructions only
16711671
*/
16721672
@Test
1673-
fun `updateLegIndex latest banner instructions pre-save to later clen up them`() =
1673+
fun `updateLegIndex latest banner and voice instructions pre-save to later clean up them`() =
16741674
coroutineRule.runBlockingTest {
16751675
val idx = -1
16761676
val mockLatestInstructionWrapper =
@@ -1681,15 +1681,19 @@ class MapboxTripSessionTest {
16811681
coEvery { navigator.updateLegIndex(idx) } coAnswers {
16821682
true
16831683
}
1684+
val mockVoiceInstruction = mockk<VoiceInstructions>()
16841685

1686+
tripSession.lastVoiceInstruction = mockVoiceInstruction
16851687
tripSession.updateLegIndex(idx, mockk(relaxUnitFun = true))
16861688

16871689
coVerifyOrder {
16881690
bannerInstructionEvent.latestInstructionWrapper
16891691
navigator.updateLegIndex(idx)
16901692
bannerInstructionEvent
16911693
.invalidateLatestBannerInstructions(mockLatestInstructionWrapper)
1694+
tripSession.lastVoiceInstruction = null
16921695
}
1696+
assertNull(tripSession.lastVoiceInstruction)
16931697
}
16941698

16951699
@After

0 commit comments

Comments
 (0)