Skip to content

Commit 9917fd5

Browse files
authored
introduce NavigationRouterV2 interface (#6190)
1 parent da31154 commit 9917fd5

File tree

28 files changed

+487
-470
lines changed

28 files changed

+487
-470
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.mapbox.navigation.base.internal
2+
3+
import com.mapbox.api.directions.v5.models.DirectionsRoute
4+
5+
/**
6+
* Class holding information about a snapshot of current indices.
7+
* All the indices are consistent (taken from the same RouteProgress instance).
8+
*
9+
* @param legIndex index of a leg the user is currently on.
10+
* @param routeGeometryIndex route-wise index representing the geometry point
11+
* right in front of the user (see [DirectionsRoute.geometry]), null if unavailable.
12+
* @param legGeometryIndex leg-wise index representing the geometry point
13+
* right in front of the user (see [DirectionsRoute.geometry]), null if unavailable.
14+
*/
15+
class CurrentIndices internal constructor(
16+
val legIndex: Int,
17+
val routeGeometryIndex: Int,
18+
val legGeometryIndex: Int?,
19+
) {
20+
21+
/**
22+
* Indicates whether some other object is "equal to" this one.
23+
*/
24+
override fun equals(other: Any?): Boolean {
25+
if (this === other) return true
26+
if (javaClass != other?.javaClass) return false
27+
28+
other as CurrentIndices
29+
30+
if (legIndex != other.legIndex) return false
31+
if (routeGeometryIndex != other.routeGeometryIndex) return false
32+
if (legGeometryIndex != other.legGeometryIndex) return false
33+
34+
return true
35+
}
36+
37+
/**
38+
* Returns a hash code value for the object.
39+
*/
40+
override fun hashCode(): Int {
41+
var result = legIndex
42+
result = 31 * result + routeGeometryIndex
43+
result = 31 * result + (legGeometryIndex ?: 0)
44+
return result
45+
}
46+
47+
/**
48+
* Returns a string representation of the object.
49+
*/
50+
override fun toString(): String {
51+
return "CurrentIndices(" +
52+
"legIndex=$legIndex, " +
53+
"routeGeometryIndex=$routeGeometryIndex, " +
54+
"legGeometryIndex=$legGeometryIndex" +
55+
")"
56+
}
57+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.mapbox.navigation.base.internal
2+
3+
object CurrentIndicesFactory {
4+
5+
fun createIndices(
6+
legIndex: Int,
7+
routeGeometryIndex: Int,
8+
legGeometryIndex: Int?,
9+
): CurrentIndices = CurrentIndices(
10+
legIndex, routeGeometryIndex, legGeometryIndex
11+
)
12+
}

libnavigation-base/src/main/java/com/mapbox/navigation/base/internal/CurrentIndicesSnapshot.kt

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.mapbox.navigation.base.internal
2+
3+
import com.mapbox.api.directions.v5.models.DirectionsRoute
4+
import com.mapbox.navigation.base.route.NavigationRoute
5+
import com.mapbox.navigation.base.route.NavigationRouter
6+
import com.mapbox.navigation.base.route.NavigationRouterRefreshCallback
7+
8+
/**
9+
* Extends [NavigationRouter] to also provide ability for refreshing routes partially
10+
* using indices stored in [CurrentIndices].
11+
*/
12+
interface NavigationRouterV2 : NavigationRouter {
13+
14+
@Deprecated(
15+
"Use getRouteRefresh(" +
16+
"NavigationRoute, " +
17+
"CurrentIndices, " +
18+
"NavigationRouterRefreshCallback" +
19+
")"
20+
)
21+
override fun getRouteRefresh(
22+
route: NavigationRoute,
23+
legIndex: Int,
24+
callback: NavigationRouterRefreshCallback
25+
): Long
26+
27+
/**
28+
* Refresh the traffic annotations for a given underlying [DirectionsRoute]
29+
*
30+
* @param route [NavigationRoute] the route to refresh
31+
* @param indicesSnapshot Object containing information about consistent current indices
32+
* @param callback Callback that gets notified with the results of the request
33+
*
34+
* @return request ID, can be used to cancel the request with [cancelRouteRefreshRequest]
35+
*/
36+
fun getRouteRefresh(
37+
route: NavigationRoute,
38+
indicesSnapshot: CurrentIndices,
39+
callback: NavigationRouterRefreshCallback
40+
): Long
41+
}

libnavigation-base/src/test/java/com/mapbox/navigation/base/route/CurrentIndicesSnapshotTest.kt

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.mapbox.navigation.core
2+
3+
import androidx.annotation.MainThread
4+
import com.mapbox.navigation.base.internal.CurrentIndices
5+
import com.mapbox.navigation.base.internal.CurrentIndicesFactory
6+
import com.mapbox.navigation.base.trip.model.RouteProgress
7+
import com.mapbox.navigation.core.trip.session.RouteProgressObserver
8+
import kotlinx.coroutines.CancellableContinuation
9+
import kotlinx.coroutines.suspendCancellableCoroutine
10+
import kotlin.coroutines.resume
11+
12+
/**
13+
* Gets updates from [RouteProgress], converts them into [CurrentIndices]
14+
* and provides said info.
15+
*/
16+
@MainThread
17+
internal class CurrentIndicesProvider : RouteProgressObserver {
18+
19+
private val defaultIndicesSnapshot = CurrentIndicesFactory.createIndices(0, 0, null)
20+
private var indicesSnapshot: CurrentIndices? = null
21+
private var continuation: CancellableContinuation<CurrentIndices>? = null
22+
23+
/**
24+
* Returns either last saved value (if has one) or waits for the next update.
25+
*/
26+
suspend fun getFilledIndicesOrWait(): CurrentIndices {
27+
return (indicesSnapshot ?: suspendCancellableCoroutine { continuation = it })
28+
}
29+
30+
/**
31+
* Resets saved info to null.
32+
*/
33+
fun clear() {
34+
indicesSnapshot = null
35+
}
36+
37+
/**
38+
* Updates indices.
39+
*/
40+
override fun onRouteProgressChanged(routeProgress: RouteProgress) {
41+
indicesSnapshot = CurrentIndicesFactory.createIndices(
42+
legIndex = routeProgress.currentLegProgress?.legIndex
43+
?: defaultIndicesSnapshot.legIndex,
44+
routeGeometryIndex = routeProgress.currentRouteGeometryIndex,
45+
legGeometryIndex = routeProgress.currentLegProgress?.geometryIndex,
46+
).also {
47+
continuation?.resume(it)
48+
continuation = null
49+
}
50+
}
51+
}

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

Lines changed: 0 additions & 71 deletions
This file was deleted.

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.mapbox.common.module.provider.MapboxModuleProvider
2222
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
2323
import com.mapbox.navigation.base.extensions.applyDefaultNavigationOptions
2424
import com.mapbox.navigation.base.extensions.applyLanguageAndVoiceUnitOptions
25+
import com.mapbox.navigation.base.internal.NavigationRouterV2
2526
import com.mapbox.navigation.base.internal.trip.notification.TripNotificationInterceptorOwner
2627
import com.mapbox.navigation.base.options.HistoryRecorderOptions
2728
import com.mapbox.navigation.base.options.NavigationOptions
@@ -47,6 +48,7 @@ import com.mapbox.navigation.core.arrival.ArrivalController
4748
import com.mapbox.navigation.core.arrival.ArrivalObserver
4849
import com.mapbox.navigation.core.arrival.ArrivalProgressObserver
4950
import com.mapbox.navigation.core.arrival.AutoArrivalController
51+
import com.mapbox.navigation.core.directions.LegacyNavigationRouterAdapter
5052
import com.mapbox.navigation.core.directions.LegacyRouterAdapter
5153
import com.mapbox.navigation.core.directions.session.DirectionsSession
5254
import com.mapbox.navigation.core.directions.session.RoutesExtra
@@ -286,26 +288,25 @@ class MapboxNavigation @VisibleForTesting internal constructor(
286288
)
287289
}
288290

289-
private val currentIndicesSnapshotProvider =
290-
NavigationComponentProvider.createCurrentIndicesSnapshotProvider()
291+
private val currentIndicesProvider =
292+
NavigationComponentProvider.createCurrentIndicesProvider()
291293

292294
// Router provided via @Modules, might be outer
293-
private val moduleRouter: NavigationRouter by lazy {
295+
private val moduleRouter: NavigationRouterV2 by lazy {
294296
val result = MapboxModuleProvider.createModule<Router>(MapboxModuleType.NavigationRouter) {
295297
paramsProvider(
296298
ModuleParams.NavigationRouter(
297299
accessToken
298300
?: throw RuntimeException(MAPBOX_NAVIGATION_TOKEN_EXCEPTION_ROUTER),
299301
nativeRouter,
300-
threadController,
301-
currentIndicesSnapshotProvider
302+
threadController
302303
)
303304
)
304305
}
305-
if (result is NavigationRouter) {
306-
result
307-
} else {
308-
LegacyRouterAdapter(result)
306+
when (result) {
307+
is NavigationRouterV2 -> result
308+
is NavigationRouter -> LegacyNavigationRouterAdapter(result)
309+
else -> LegacyNavigationRouterAdapter(LegacyRouterAdapter(result))
309310
}
310311
}
311312

@@ -479,7 +480,7 @@ class MapboxNavigation @VisibleForTesting internal constructor(
479480
threadController,
480481
)
481482

482-
tripSession.registerRouteProgressObserver(currentIndicesSnapshotProvider)
483+
tripSession.registerRouteProgressObserver(currentIndicesProvider)
483484
tripSession.registerStateObserver(navigationSession)
484485
tripSession.registerStateObserver(historyRecordingStateHandler)
485486

@@ -534,7 +535,7 @@ class MapboxNavigation @VisibleForTesting internal constructor(
534535
routeRefreshController = RouteRefreshControllerProvider.createRouteRefreshController(
535536
navigationOptions.routeRefreshOptions,
536537
directionsSession,
537-
currentIndicesSnapshotProvider,
538+
currentIndicesProvider,
538539
)
539540

540541
defaultRerouteController = MapboxRerouteController(
@@ -1613,7 +1614,7 @@ class MapboxNavigation @VisibleForTesting internal constructor(
16131614

16141615
private fun createInternalRoutesObserver() = RoutesObserver { result ->
16151616
latestLegIndex = null
1616-
currentIndicesSnapshotProvider.clear()
1617+
currentIndicesProvider.clear()
16171618
if (result.navigationRoutes.isNotEmpty()) {
16181619
routeScope.launch {
16191620
val refreshed = routeRefreshController.refresh(

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.mapbox.navigation.core
22

33
import android.content.Context
4+
import com.mapbox.navigation.base.internal.NavigationRouterV2
45
import com.mapbox.navigation.base.options.DeviceProfile
56
import com.mapbox.navigation.base.options.NavigationOptions
6-
import com.mapbox.navigation.base.route.NavigationRouter
77
import com.mapbox.navigation.base.trip.notification.TripNotification
88
import com.mapbox.navigation.core.accounts.BillingController
99
import com.mapbox.navigation.core.arrival.ArrivalProgressObserver
@@ -26,7 +26,7 @@ import com.mapbox.navigator.TilesConfig
2626

2727
internal object NavigationComponentProvider {
2828
fun createDirectionsSession(
29-
router: NavigationRouter,
29+
router: NavigationRouterV2,
3030
): DirectionsSession = MapboxDirectionsSession(router)
3131

3232
fun createNativeNavigator(
@@ -94,6 +94,6 @@ internal object NavigationComponentProvider {
9494
initialState: NavigationSessionState
9595
): HistoryRecordingStateHandler = HistoryRecordingStateHandler(initialState)
9696

97-
fun createCurrentIndicesSnapshotProvider(): CurrentIndicesSnapshotProvider =
98-
CurrentIndicesSnapshotProvider()
97+
fun createCurrentIndicesProvider(): CurrentIndicesProvider =
98+
CurrentIndicesProvider()
9999
}

0 commit comments

Comments
 (0)