Skip to content

Commit d4a714d

Browse files
authored
NN: adopt set route reason (#6620)
1 parent 1a7522f commit d4a714d

20 files changed

Lines changed: 325 additions & 356 deletions

File tree

libnavigation-core/src/main/java/com/mapbox/navigation/core/MapboxNavigation.kt

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -839,13 +839,10 @@ class MapboxNavigation @VisibleForTesting internal constructor(
839839

840840
// Telemetry uses this field to determine what type of event should be triggered.
841841
val setRoutesInfo = when {
842-
routes.isEmpty() -> BasicSetRoutesInfo(
843-
RoutesExtra.ROUTES_UPDATE_REASON_CLEAN_UP,
844-
initialLegIndex
845-
)
842+
routes.isEmpty() -> SetRoutes.CleanUp
846843
routes.first() == directionsSession.routes.firstOrNull() ->
847-
SetAlternativeRoutesInfo(initialLegIndex)
848-
else -> BasicSetRoutesInfo(RoutesExtra.ROUTES_UPDATE_REASON_NEW, initialLegIndex)
844+
SetRoutes.Alternatives(initialLegIndex)
845+
else -> SetRoutes.NewRoutes(initialLegIndex)
849846
}
850847
internalSetNavigationRoutes(
851848
routes,
@@ -952,7 +949,7 @@ class MapboxNavigation @VisibleForTesting internal constructor(
952949

953950
private fun internalSetNavigationRoutes(
954951
routes: List<NavigationRoute>,
955-
setRoutesInfo: SetRoutesInfo,
952+
setRoutesInfo: SetRoutes,
956953
callback: RoutesSetCallback? = null,
957954
) {
958955
rerouteController?.interrupt()
@@ -1000,10 +997,7 @@ class MapboxNavigation @VisibleForTesting internal constructor(
1000997
routeUpdateMutex.withLock {
1001998
val routes = directionsSession.routes
1002999
val legIndex = latestLegIndex ?: directionsSession.initialLegIndex
1003-
setRoutesToTripSession(
1004-
routes,
1005-
BasicSetRoutesInfo(RoutesExtra.ROUTES_UPDATE_REASON_NEW, legIndex)
1006-
)
1000+
setRoutesToTripSession(routes, SetRoutes.NewRoutes(legIndex))
10071001
}
10081002
}
10091003
}
@@ -1014,7 +1008,7 @@ class MapboxNavigation @VisibleForTesting internal constructor(
10141008
*/
10151009
private suspend fun setRoutesToTripSession(
10161010
routes: List<NavigationRoute>,
1017-
setRoutesInfo: SetRoutesInfo,
1011+
setRoutesInfo: SetRoutes,
10181012
): NativeSetRouteResult {
10191013
return tripSession.setRoutes(routes, setRoutesInfo).apply {
10201014
if (this is NativeSetRouteValue) {
@@ -1118,10 +1112,7 @@ class MapboxNavigation @VisibleForTesting internal constructor(
11181112
tripSession.unregisterAllEHorizonObservers()
11191113
tripSession.unregisterAllFallbackVersionsObservers()
11201114
routeAlternativesController.unregisterAll()
1121-
internalSetNavigationRoutes(
1122-
emptyList(),
1123-
BasicSetRoutesInfo(RoutesExtra.ROUTES_UPDATE_REASON_CLEAN_UP)
1124-
)
1115+
internalSetNavigationRoutes(emptyList(), SetRoutes.CleanUp)
11251116
resetTripSession()
11261117
navigator.unregisterAllObservers()
11271118
navigationVersionSwitchObservers.clear()
@@ -1822,7 +1813,7 @@ class MapboxNavigation @VisibleForTesting internal constructor(
18221813
)
18231814
internalSetNavigationRoutes(
18241815
refreshed.routes,
1825-
SetRefreshedRoutesInfo(refreshed.routeProgressData),
1816+
SetRoutes.RefreshRoutes(refreshed.routeProgressData),
18261817
)
18271818
}
18281819
}
@@ -1898,8 +1889,11 @@ class MapboxNavigation @VisibleForTesting internal constructor(
18981889
navigator.setRoutes(
18991890
primaryRoute = routes[0],
19001891
startingLeg = tripSession.getRouteProgress()?.currentLegProgress?.legIndex ?: 0,
1901-
setRoutesReason = SetRoutesReason.NEW_ROUTE,
1902-
alternatives = routes.drop(1)
1892+
alternatives = routes.drop(1),
1893+
when (isFallback) {
1894+
true -> SetRoutesReason.FALLBACK_TO_OFFLINE
1895+
false -> SetRoutesReason.RESTORE_TO_ONLINE
1896+
}
19031897
)
19041898
}
19051899
}
@@ -1909,7 +1903,7 @@ class MapboxNavigation @VisibleForTesting internal constructor(
19091903
rerouteController?.reroute { routes, _ ->
19101904
internalSetNavigationRoutes(
19111905
routes,
1912-
BasicSetRoutesInfo(RoutesExtra.ROUTES_UPDATE_REASON_REROUTE)
1906+
SetRoutes.Reroute
19131907
)
19141908
}
19151909
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.mapbox.navigation.core
2+
3+
internal sealed class SetRoutes {
4+
5+
internal object CleanUp : SetRoutes()
6+
7+
internal data class NewRoutes(
8+
val legIndex: Int,
9+
) : SetRoutes()
10+
11+
internal object Reroute : SetRoutes()
12+
13+
internal data class Alternatives(
14+
val legIndex: Int,
15+
) : SetRoutes()
16+
17+
internal data class RefreshRoutes(
18+
val routeProgressData: RouteProgressData
19+
) : SetRoutes()
20+
}
Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +0,0 @@
1-
package com.mapbox.navigation.core
2-
3-
import com.mapbox.navigation.core.directions.session.RoutesExtra
4-
5-
internal sealed class SetRoutesInfo(
6-
@RoutesExtra.RoutesUpdateReason val reason: String,
7-
val legIndex: Int,
8-
) {
9-
override fun equals(other: Any?): Boolean {
10-
if (this === other) return true
11-
if (javaClass != other?.javaClass) return false
12-
13-
other as SetRoutesInfo
14-
15-
if (reason != other.reason) return false
16-
if (legIndex != other.legIndex) return false
17-
18-
return true
19-
}
20-
21-
override fun hashCode(): Int {
22-
var result = reason.hashCode()
23-
result = 31 * result + legIndex
24-
return result
25-
}
26-
27-
override fun toString(): String {
28-
return "SetRoutesInfo(reason=$reason, legIndex=$legIndex)"
29-
}
30-
}
31-
32-
internal class BasicSetRoutesInfo(
33-
@RoutesExtra.RoutesUpdateReason reason: String,
34-
legIndex: Int = 0,
35-
) : SetRoutesInfo(reason, legIndex) {
36-
override fun toString(): String {
37-
return "BasicSetRoutesInfo() ${super.toString()}"
38-
}
39-
40-
override fun equals(other: Any?): Boolean {
41-
if (this === other) return true
42-
if (javaClass != other?.javaClass) return false
43-
if (!super.equals(other)) return false
44-
return true
45-
}
46-
}
47-
48-
internal class SetAlternativeRoutesInfo(legIndex: Int) : SetRoutesInfo(
49-
RoutesExtra.ROUTES_UPDATE_REASON_ALTERNATIVE,
50-
legIndex
51-
) {
52-
override fun toString(): String {
53-
return "SetAlternativeRoutesInfo() ${super.toString()}"
54-
}
55-
56-
override fun equals(other: Any?): Boolean {
57-
if (this === other) return true
58-
if (javaClass != other?.javaClass) return false
59-
if (!super.equals(other)) return false
60-
return true
61-
}
62-
}
63-
64-
internal class SetRefreshedRoutesInfo(
65-
val routeProgressData: RouteProgressData
66-
) : SetRoutesInfo(RoutesExtra.ROUTES_UPDATE_REASON_REFRESH, routeProgressData.legIndex) {
67-
68-
override fun equals(other: Any?): Boolean {
69-
if (this === other) return true
70-
if (javaClass != other?.javaClass) return false
71-
if (!super.equals(other)) return false
72-
73-
other as SetRefreshedRoutesInfo
74-
75-
if (routeProgressData != other.routeProgressData) return false
76-
77-
return true
78-
}
79-
80-
override fun hashCode(): Int {
81-
var result = super.hashCode()
82-
result = 31 * result + routeProgressData.hashCode()
83-
return result
84-
}
85-
86-
override fun toString(): String {
87-
return "SetRefreshedRoutesInfo(" +
88-
"routeProgressData=$routeProgressData) " +
89-
super.toString() +
90-
""
91-
}
92-
}

libnavigation-core/src/main/java/com/mapbox/navigation/core/directions/session/DirectionsSession.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.mapbox.api.directions.v5.models.RouteOptions
44
import com.mapbox.navigation.base.route.NavigationRoute
55
import com.mapbox.navigation.base.route.NavigationRouterCallback
66
import com.mapbox.navigation.base.route.Router
7-
import com.mapbox.navigation.core.SetRoutesInfo
7+
import com.mapbox.navigation.core.SetRoutes
88

99
internal interface DirectionsSession : RouteRefresh {
1010

@@ -20,7 +20,7 @@ internal interface DirectionsSession : RouteRefresh {
2020

2121
fun setRoutes(
2222
routes: List<NavigationRoute>,
23-
setRoutesInfo: SetRoutesInfo,
23+
setRoutesInfo: SetRoutes,
2424
)
2525

2626
/**

libnavigation-core/src/main/java/com/mapbox/navigation/core/directions/session/MapboxDirectionsSession.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import com.mapbox.navigation.base.route.NavigationRoute
1010
import com.mapbox.navigation.base.route.NavigationRouterCallback
1111
import com.mapbox.navigation.base.route.NavigationRouterRefreshCallback
1212
import com.mapbox.navigation.base.route.Router
13-
import com.mapbox.navigation.core.SetRoutesInfo
13+
import com.mapbox.navigation.core.SetRoutes
14+
import com.mapbox.navigation.core.internal.utils.initialLegIndex
15+
import com.mapbox.navigation.core.internal.utils.mapToReason
1416
import java.util.concurrent.CopyOnWriteArraySet
1517

1618
/**
@@ -42,20 +44,24 @@ internal class MapboxDirectionsSession(
4244
@RoutesExtra.RoutesUpdateReason
4345
private var routesUpdateReason: String = RoutesExtra.ROUTES_UPDATE_REASON_CLEAN_UP
4446

45-
override var initialLegIndex = 0
47+
override var initialLegIndex = DEFAULT_INITIAL_LEG_INDEX
4648
private set
4749

50+
internal companion object {
51+
internal const val DEFAULT_INITIAL_LEG_INDEX = 0
52+
}
53+
4854
override fun setRoutes(
4955
routes: List<NavigationRoute>,
50-
setRoutesInfo: SetRoutesInfo,
56+
setRoutesInfo: SetRoutes,
5157
) {
52-
this.initialLegIndex = setRoutesInfo.legIndex
58+
this.initialLegIndex = setRoutesInfo.initialLegIndex()
5359
if (routesInitialized && this.routes.isEmpty() && routes.isEmpty()) {
5460
return
5561
}
5662
RouteCompatibilityCache.setDirectionsSessionResult(routes)
5763
this.routes = routes
58-
this.routesUpdateReason = setRoutesInfo.reason
64+
this.routesUpdateReason = setRoutesInfo.mapToReason()
5965
routesObservers.forEach {
6066
it.onRoutesChanged(
6167
RoutesUpdatedResult(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.mapbox.navigation.core.internal.utils
2+
3+
import com.mapbox.navigation.core.SetRoutes
4+
import com.mapbox.navigation.core.directions.session.MapboxDirectionsSession
5+
import com.mapbox.navigation.core.directions.session.RoutesExtra
6+
7+
@RoutesExtra.RoutesUpdateReason
8+
internal fun SetRoutes.mapToReason(): String =
9+
when (this) {
10+
is SetRoutes.Alternatives -> RoutesExtra.ROUTES_UPDATE_REASON_ALTERNATIVE
11+
SetRoutes.CleanUp -> RoutesExtra.ROUTES_UPDATE_REASON_CLEAN_UP
12+
is SetRoutes.NewRoutes -> RoutesExtra.ROUTES_UPDATE_REASON_NEW
13+
is SetRoutes.RefreshRoutes -> RoutesExtra.ROUTES_UPDATE_REASON_REFRESH
14+
SetRoutes.Reroute -> RoutesExtra.ROUTES_UPDATE_REASON_REROUTE
15+
}
16+
17+
internal fun SetRoutes.initialLegIndex(): Int =
18+
when (this) {
19+
is SetRoutes.Alternatives -> legIndex
20+
SetRoutes.CleanUp -> MapboxDirectionsSession.DEFAULT_INITIAL_LEG_INDEX
21+
is SetRoutes.NewRoutes -> legIndex
22+
is SetRoutes.RefreshRoutes -> routeProgressData.legIndex
23+
SetRoutes.Reroute -> MapboxDirectionsSession.DEFAULT_INITIAL_LEG_INDEX
24+
}

0 commit comments

Comments
 (0)