@@ -8,144 +8,69 @@ import androidx.car.app.model.Template
88import androidx.car.app.navigation.model.NavigationTemplate
99import androidx.lifecycle.DefaultLifecycleObserver
1010import androidx.lifecycle.LifecycleOwner
11- import com.mapbox.androidauto.ArrivalState
12- import com.mapbox.androidauto.MapboxCarApp
1311import com.mapbox.androidauto.R
12+ import com.mapbox.androidauto.car.MainCarContext
1413import com.mapbox.androidauto.car.MainMapActionStrip
1514import com.mapbox.androidauto.car.action.MapboxActionProvider
1615import com.mapbox.androidauto.car.location.CarLocationRenderer
1716import com.mapbox.androidauto.car.navigation.roadlabel.RoadLabelSurfaceLayer
1817import com.mapbox.androidauto.car.navigation.speedlimit.CarSpeedLimitRenderer
19- import com.mapbox.androidauto.car.placeslistonmap.PlacesListOnMapLayerUtil
2018import com.mapbox.androidauto.car.preview.CarRouteLine
21- import com.mapbox.androidauto.internal.car.extensions.getStyle
22- import com.mapbox.androidauto.internal.car.extensions.handleStyleOnAttached
23- import com.mapbox.androidauto.internal.car.extensions.handleStyleOnDetached
2419import com.mapbox.androidauto.internal.logAndroidAuto
25- import com.mapbox.geojson.Feature
26- import com.mapbox.geojson.FeatureCollection
2720import com.mapbox.maps.MapboxExperimental
28- import com.mapbox.maps.extension.androidauto.MapboxCarMapObserver
29- import com.mapbox.maps.extension.androidauto.MapboxCarMapSurface
30- import com.mapbox.maps.plugin.delegates.listeners.OnStyleLoadedListener
31- import com.mapbox.navigation.base.trip.model.RouteLegProgress
32- import com.mapbox.navigation.base.trip.model.RouteProgress
33- import com.mapbox.navigation.core.arrival.ArrivalObserver
34- import com.mapbox.navigation.core.directions.session.RoutesObserver
21+ import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
3522
3623/* *
3724 * After a route has been selected. This view gives turn-by-turn instructions
3825 * for completing the route.
3926 */
4027@OptIn(MapboxExperimental ::class )
4128class ActiveGuidanceScreen (
42- private val carActiveGuidanceContext : CarActiveGuidanceCarContext ,
29+ private val mainCarContext : MainCarContext ,
4330 private val actionProviders : List <MapboxActionProvider >,
44- private val placesLayerUtil : PlacesListOnMapLayerUtil = PlacesListOnMapLayerUtil (),
45- ) : Screen(carActiveGuidanceContext.carContext) {
31+ ) : Screen(mainCarContext.carContext) {
4632
47- val carRouteLine = CarRouteLine (carActiveGuidanceContext. mainCarContext)
48- val carLocationRenderer = CarLocationRenderer (carActiveGuidanceContext. mainCarContext)
49- val carSpeedLimitRenderer = CarSpeedLimitRenderer (carActiveGuidanceContext. mainCarContext)
33+ val carRouteLine = CarRouteLine (mainCarContext)
34+ val carLocationRenderer = CarLocationRenderer (mainCarContext)
35+ val carSpeedLimitRenderer = CarSpeedLimitRenderer (mainCarContext)
5036 val carNavigationCamera = CarNavigationCamera (
5137 initialCarCameraMode = CarCameraMode .FOLLOWING ,
5238 alternativeCarCameraMode = CarCameraMode .OVERVIEW ,
5339 )
5440 private val roadLabelSurfaceLayer = RoadLabelSurfaceLayer (carContext)
5541 private val navigationInfoProvider = CarNavigationInfoProvider ()
5642 .invalidateOnChange(this )
43+ private val carActiveGuidanceMarkers = CarActiveGuidanceMarkers ()
5744 private val mapActionStripBuilder = MainMapActionStrip (this , carNavigationCamera)
58-
59- private val arrivalObserver = object : ArrivalObserver {
60-
61- override fun onFinalDestinationArrival (routeProgress : RouteProgress ) {
62- stopNavigation()
63- }
64-
65- override fun onNextRouteLegStart (routeLegProgress : RouteLegProgress ) {
66- // not implemented
67- }
68-
69- override fun onWaypointArrival (routeProgress : RouteProgress ) {
70- // not implemented
71- }
72- }
73-
74- private var styleLoadedListener: OnStyleLoadedListener ? = null
75-
76- private val surfaceListener = object : MapboxCarMapObserver {
77-
78- override fun onAttached (mapboxCarMapSurface : MapboxCarMapSurface ) {
79- super .onAttached(mapboxCarMapSurface)
80- logAndroidAuto(" ActiveGuidanceScreen loaded" )
81- styleLoadedListener = mapboxCarMapSurface.handleStyleOnAttached {
82- placesLayerUtil.initializePlacesListOnMapLayer(it, carContext.resources)
83- carActiveGuidanceContext.mapboxNavigation.registerRoutesObserver(routesObserver)
84- }
85- }
86-
87- override fun onDetached (mapboxCarMapSurface : MapboxCarMapSurface ) {
88- super .onDetached(mapboxCarMapSurface)
89- logAndroidAuto(" ActiveGuidanceScreen detached" )
90- carActiveGuidanceContext.mapboxNavigation.unregisterRoutesObserver(routesObserver)
91- mapboxCarMapSurface.handleStyleOnDetached(styleLoadedListener)?.let {
92- placesLayerUtil.removePlacesListOnMapLayer(it)
93- }
94- }
95- }
96-
97- private val routesObserver = RoutesObserver { result ->
98- val route = result.navigationRoutes.firstOrNull()
99- ? : return @RoutesObserver
100- val coordinate = route.routeOptions.coordinatesList().lastOrNull()
101- ? : return @RoutesObserver
102- val mapboxCarMapSurface = carActiveGuidanceContext.mapboxCarMap.carMapSurface
103- ? : return @RoutesObserver
104- val featureCollection = FeatureCollection .fromFeature(Feature .fromGeometry(coordinate))
105- mapboxCarMapSurface.getStyle()?.let {
106- placesLayerUtil.updatePlacesListOnMapLayer(it, featureCollection)
107- }
108- }
45+ private val carArrivalTrigger = CarArrivalTrigger ()
10946
11047 init {
11148 logAndroidAuto(" ActiveGuidanceScreen constructor" )
112-
11349 lifecycle.addObserver(object : DefaultLifecycleObserver {
114-
115- override fun onCreate (owner : LifecycleOwner ) {
116- logAndroidAuto(" ActiveGuidanceScreen onCreate" )
117- carActiveGuidanceContext.mapboxNavigation.registerArrivalObserver(arrivalObserver)
118- }
119-
12050 override fun onResume (owner : LifecycleOwner ) {
12151 logAndroidAuto(" ActiveGuidanceScreen onResume" )
122- carActiveGuidanceContext.mapboxCarMap.registerObserver(carLocationRenderer)
123- carActiveGuidanceContext.mapboxCarMap.registerObserver(roadLabelSurfaceLayer)
124- carActiveGuidanceContext.mapboxCarMap.registerObserver(carSpeedLimitRenderer)
125- carActiveGuidanceContext.mapboxCarMap.registerObserver(carNavigationCamera)
126- carActiveGuidanceContext.mapboxCarMap.setGestureHandler(
127- carNavigationCamera.gestureHandler
128- )
129- carActiveGuidanceContext.mapboxCarMap.registerObserver(carRouteLine)
130- carActiveGuidanceContext.mapboxCarMap.registerObserver(surfaceListener)
131- carActiveGuidanceContext.mapboxCarMap.registerObserver(navigationInfoProvider)
52+ mainCarContext.mapboxCarMap.registerObserver(carLocationRenderer)
53+ mainCarContext.mapboxCarMap.registerObserver(roadLabelSurfaceLayer)
54+ mainCarContext.mapboxCarMap.registerObserver(carSpeedLimitRenderer)
55+ mainCarContext.mapboxCarMap.registerObserver(carNavigationCamera)
56+ mainCarContext.mapboxCarMap.setGestureHandler(carNavigationCamera.gestureHandler)
57+ mainCarContext.mapboxCarMap.registerObserver(carRouteLine)
58+ mainCarContext.mapboxCarMap.registerObserver(carActiveGuidanceMarkers)
59+ mainCarContext.mapboxCarMap.registerObserver(navigationInfoProvider)
60+ MapboxNavigationApp .registerObserver(carArrivalTrigger)
13261 }
13362
13463 override fun onPause (owner : LifecycleOwner ) {
13564 logAndroidAuto(" ActiveGuidanceScreen onPause" )
136- carActiveGuidanceContext.mapboxCarMap.unregisterObserver(roadLabelSurfaceLayer)
137- carActiveGuidanceContext.mapboxCarMap.unregisterObserver(carLocationRenderer)
138- carActiveGuidanceContext.mapboxCarMap.unregisterObserver(carSpeedLimitRenderer)
139- carActiveGuidanceContext.mapboxCarMap.unregisterObserver(carNavigationCamera)
140- carActiveGuidanceContext.mapboxCarMap.setGestureHandler(null )
141- carActiveGuidanceContext.mapboxCarMap.unregisterObserver(carRouteLine)
142- carActiveGuidanceContext.mapboxCarMap.unregisterObserver(surfaceListener)
143- carActiveGuidanceContext.mapboxCarMap.unregisterObserver(navigationInfoProvider)
144- }
145-
146- override fun onDestroy (owner : LifecycleOwner ) {
147- logAndroidAuto(" ActiveGuidanceScreen onDestroy" )
148- carActiveGuidanceContext.mapboxNavigation.unregisterArrivalObserver(arrivalObserver)
65+ mainCarContext.mapboxCarMap.unregisterObserver(roadLabelSurfaceLayer)
66+ mainCarContext.mapboxCarMap.unregisterObserver(carLocationRenderer)
67+ mainCarContext.mapboxCarMap.unregisterObserver(carSpeedLimitRenderer)
68+ mainCarContext.mapboxCarMap.unregisterObserver(carNavigationCamera)
69+ mainCarContext.mapboxCarMap.setGestureHandler(null )
70+ mainCarContext.mapboxCarMap.unregisterObserver(carRouteLine)
71+ mainCarContext.mapboxCarMap.unregisterObserver(carActiveGuidanceMarkers)
72+ mainCarContext.mapboxCarMap.unregisterObserver(navigationInfoProvider)
73+ MapboxNavigationApp .unregisterObserver(carArrivalTrigger)
14974 }
15075 })
15176 }
@@ -167,7 +92,7 @@ class ActiveGuidanceScreen(
16792 Action .Builder ()
16893 .setTitle(carContext.getString(R .string.car_action_navigation_stop_button))
16994 .setOnClickListener {
170- stopNavigation ()
95+ carArrivalTrigger.triggerArrival ()
17196 }.build()
17297 )
17398 }.build()
@@ -179,9 +104,4 @@ class ActiveGuidanceScreen(
179104 .apply { navigationInfoProvider.setNavigationInfo(this ) }
180105 .build()
181106 }
182-
183- private fun stopNavigation () {
184- logAndroidAuto(" ActiveGuidanceScreen stopNavigation" )
185- MapboxCarApp .updateCarAppState(ArrivalState )
186- }
187107}
0 commit comments