Skip to content

Commit a45f7d4

Browse files
authored
- Added Waypoints to NavigationRoute; - RouteOptionsUpdater refactored. (#6005)
1 parent 5e27577 commit a45f7d4

File tree

24 files changed

+1026
-332
lines changed

24 files changed

+1026
-332
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Mapbox welcomes participation and contributions from everyone.
55
## Unreleased
66
#### Features
77
#### Bug fixes and improvements
8+
- Fixed an issue where re-routes could have failed for EV routes. [#6005](https://github.com/mapbox/mapbox-navigation-android/pull/6005)
9+
10+
:warning: `RouteProgress#remainingWaypoints` defines the amount of all remaining waypoints, including explicit (requested with `RouteOptions#coordinatesList`) and implicit (added on the fly, like Electric Vehicle waypoints - [see](https://docs.mapbox.com/api/navigation/directions/#electric-vehicle-routing)) ones.
811

912
## Mapbox Navigation SDK 2.9.0-rc.1 - 28 October, 2022
1013
### Changelog
@@ -24,7 +27,6 @@ This release depends on, and has been tested with, the following Mapbox dependen
2427
- Mapbox Java `v6.9.0-beta.1` ([release notes](https://github.com/mapbox/mapbox-java/releases/tag/v6.9.0-beta.1))
2528
- Mapbox Android Core `v5.0.2` ([release notes](https://github.com/mapbox/mapbox-events-android/releases/tag/core-5.0.2))
2629

27-
2830
## Mapbox Navigation SDK 2.9.0-beta.3 - 21 October, 2022
2931
### Changelog
3032
[Changes between v2.9.0-beta.2 and v2.9.0-beta.3](https://github.com/mapbox/mapbox-navigation-android/compare/v2.9.0-beta.2...v2.9.0-beta.3)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.mapbox.navigation.base.internal.extensions
2+
3+
import com.mapbox.api.directions.v5.models.DirectionsRoute
4+
import com.mapbox.api.directions.v5.models.RouteOptions
5+
import com.mapbox.navigation.base.internal.route.Waypoint
6+
import com.mapbox.navigation.base.trip.model.RouteProgress
7+
8+
/**
9+
* Return true if the waypoint is requested explicitly. False otherwise.
10+
*/
11+
fun Waypoint.isRequestedWaypoint(): Boolean =
12+
when (this.internalType) {
13+
Waypoint.InternalType.Regular,
14+
Waypoint.InternalType.Silent -> true
15+
Waypoint.InternalType.EvCharging -> false
16+
}
17+
18+
/**
19+
* Return true if the waypoint is tracked in [RouteProgress.currentLegProgress]#legIndex, based on
20+
* [DirectionsRoute.legs] index. False otherwise.
21+
*/
22+
fun Waypoint.isLegWaypoint(): Boolean =
23+
when (this.internalType) {
24+
Waypoint.InternalType.Regular,
25+
Waypoint.InternalType.EvCharging -> true
26+
Waypoint.InternalType.Silent -> false
27+
}
28+
29+
/**
30+
* Return the index of **next requested** coordinate. See [RouteOptions.coordinatesList]
31+
*
32+
* For instance, EV waypoints are not requested explicitly, so they are not taken into account.
33+
*/
34+
fun indexOfNextRequestedCoordinate(
35+
waypoints: List<Waypoint>,
36+
remainingWaypoints: Int,
37+
): Int? {
38+
if (remainingWaypoints > waypoints.size) {
39+
return null
40+
}
41+
val nextWaypointIndex = waypoints.size - remainingWaypoints
42+
var requestedIndex = 0
43+
waypoints.forEachIndexed { index, waypoint ->
44+
if (waypoint.isRequestedWaypoint()) {
45+
if (index >= nextWaypointIndex) {
46+
return requestedIndex
47+
}
48+
requestedIndex++
49+
}
50+
}
51+
return null
52+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.mapbox.navigation.base.internal.route
2+
3+
import androidx.annotation.IntDef
4+
import com.mapbox.geojson.Point
5+
6+
class Waypoint internal constructor(
7+
val location: Point,
8+
val name: String,
9+
val target: Point?,
10+
internal val internalType: InternalType
11+
) {
12+
13+
@Type
14+
val type: Int = when (internalType) {
15+
InternalType.Regular -> REGULAR
16+
InternalType.Silent -> SILENT
17+
InternalType.EvCharging -> EV_CHARGING
18+
}
19+
20+
companion object {
21+
const val REGULAR = 1
22+
const val SILENT = 2
23+
const val EV_CHARGING = 3
24+
}
25+
26+
@Target(
27+
AnnotationTarget.PROPERTY,
28+
AnnotationTarget.VALUE_PARAMETER,
29+
AnnotationTarget.FUNCTION,
30+
AnnotationTarget.TYPE
31+
)
32+
@Retention(AnnotationRetention.BINARY)
33+
@IntDef(REGULAR, SILENT, EV_CHARGING)
34+
annotation class Type
35+
36+
override fun equals(other: Any?): Boolean {
37+
if (this === other) return true
38+
if (javaClass != other?.javaClass) return false
39+
40+
other as Waypoint
41+
42+
if (location != other.location) return false
43+
if (type != other.type) return false
44+
if (name != other.name) return false
45+
if (target != other.target) return false
46+
47+
return true
48+
}
49+
50+
override fun hashCode(): Int {
51+
var result = location.hashCode()
52+
result = 31 * result + type
53+
result = 31 * result + name.hashCode()
54+
result = 31 * result + target.hashCode()
55+
return result
56+
}
57+
58+
override fun toString(): String {
59+
return "Waypoint(location=$location, type=$type, name='$name', target=$target)"
60+
}
61+
62+
internal enum class InternalType {
63+
Regular,
64+
Silent,
65+
EvCharging,
66+
}
67+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package com.mapbox.navigation.base.internal.utils
44

55
import com.mapbox.api.directions.v5.models.DirectionsRoute
66
import com.mapbox.api.directions.v5.models.LegStep
7+
import com.mapbox.navigation.base.internal.route.Waypoint
8+
import com.mapbox.navigation.base.internal.route.Waypoint.Companion.REGULAR
79
import com.mapbox.navigation.base.utils.ifNonNull
810

911
/**
@@ -36,3 +38,20 @@ private fun DirectionsRoute.stepsNamesAsString(): String? =
3638
?.joinToString { leg ->
3739
leg.steps()?.joinToString { step -> step.name() ?: "" } ?: ""
3840
}
41+
42+
internal fun List<com.mapbox.navigator.Waypoint>.mapToSdk(): List<Waypoint> =
43+
map { nativeWaypoint ->
44+
Waypoint(
45+
location = nativeWaypoint.location,
46+
internalType = nativeWaypoint.type.mapToSdk(),
47+
name = nativeWaypoint.name,
48+
target = nativeWaypoint.target,
49+
)
50+
}
51+
52+
private fun com.mapbox.navigator.WaypointType.mapToSdk(): Waypoint.InternalType =
53+
when (this) {
54+
com.mapbox.navigator.WaypointType.REGULAR -> Waypoint.InternalType.Regular
55+
com.mapbox.navigator.WaypointType.SILENT -> Waypoint.InternalType.Silent
56+
com.mapbox.navigator.WaypointType.EV_CHARGING -> Waypoint.InternalType.EvCharging
57+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mapbox.navigation.base.internal.utils
22

3+
import com.mapbox.navigation.base.internal.route.Waypoint
4+
import com.mapbox.navigation.base.route.NavigationRoute
35
import com.mapbox.navigator.RouterOrigin
46

57
fun RouterOrigin.mapToSdkRouteOrigin(): com.mapbox.navigation.base.route.RouterOrigin =
@@ -15,3 +17,5 @@ fun com.mapbox.navigation.base.route.RouterOrigin.mapToNativeRouteOrigin(): Rout
1517
com.mapbox.navigation.base.route.RouterOrigin.Onboard -> RouterOrigin.ONBOARD
1618
is com.mapbox.navigation.base.route.RouterOrigin.Custom -> RouterOrigin.CUSTOM
1719
}
20+
21+
fun NavigationRoute.internalWaypoints(): List<Waypoint> = waypoints
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.mapbox.navigation.base.internal.utils
2+
3+
import androidx.annotation.VisibleForTesting
4+
import com.mapbox.geojson.Point
5+
import com.mapbox.navigation.base.internal.route.Waypoint
6+
import org.jetbrains.annotations.TestOnly
7+
8+
@VisibleForTesting
9+
object WaypointFactory {
10+
@TestOnly
11+
fun provideWaypoint(
12+
location: Point,
13+
name: String,
14+
target: Point?,
15+
@Waypoint.Type type: Int,
16+
): Waypoint = Waypoint(
17+
location,
18+
name,
19+
target,
20+
when (type) {
21+
Waypoint.REGULAR -> Waypoint.InternalType.Regular
22+
Waypoint.SILENT -> Waypoint.InternalType.Silent
23+
Waypoint.EV_CHARGING -> Waypoint.InternalType.EvCharging
24+
else -> throw IllegalStateException("Unknown waypoint type $type")
25+
},
26+
)
27+
}

libnavigation-base/src/main/java/com/mapbox/navigation/base/route/NavigationRoute.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import com.mapbox.navigation.base.internal.NativeRouteParserWrapper
1717
import com.mapbox.navigation.base.internal.SDKRouteParser
1818
import com.mapbox.navigation.base.internal.factory.RoadObjectFactory.toUpcomingRoadObjects
1919
import com.mapbox.navigation.base.internal.route.RouteCompatibilityCache
20+
import com.mapbox.navigation.base.internal.route.Waypoint
21+
import com.mapbox.navigation.base.internal.route.toNavigationRoute
2022
import com.mapbox.navigation.base.internal.utils.DirectionsRouteMissingConditionsCheck
23+
import com.mapbox.navigation.base.internal.utils.mapToSdk
2124
import com.mapbox.navigation.base.internal.utils.mapToSdkRouteOrigin
2225
import com.mapbox.navigation.base.trip.model.roadobject.UpcomingRoadObject
2326
import com.mapbox.navigation.utils.internal.ThreadController
@@ -293,6 +296,8 @@ class NavigationRoute internal constructor(
293296
*/
294297
val upcomingRoadObjects = nativeRoute.routeInfo.alerts.toUpcomingRoadObjects()
295298

299+
internal val waypoints: List<Waypoint> by lazy { nativeRoute.waypoints.mapToSdk() }
300+
296301
/**
297302
* Indicates whether some other object is "equal to" this one.
298303
*

libnavigation-base/src/main/java/com/mapbox/navigation/base/trip/model/RouteProgress.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ import com.mapbox.navigation.base.trip.model.roadobject.UpcomingRoadObject
3333
* @param durationRemaining [Double] seconds time remaining until the route destination is reached.
3434
* @param fractionTraveled [Float] fraction traveled along the current route. This value is
3535
* between 0 and 1 and isn't guaranteed to reach 1 before the user reaches the end of the route.
36-
* @param remainingWaypoints [Int] number of waypoints remaining on the current route.
36+
* @param remainingWaypoints [Int] number of waypoints remaining on the current route. The waypoints number can be different
37+
* with number of requested coordinates. For instance, [EV routing](https://docs.mapbox.com/api/navigation/directions/#electric-vehicle-routing)
38+
* is adding additional waypoints, that are not requested explicitly.
3739
* @param upcomingRoadObjects list of upcoming road objects.
3840
* @param stale `true` if there were no location updates for a significant amount which causes
3941
* a lack of confidence in the progress updates being sent.

0 commit comments

Comments
 (0)