@@ -8,144 +8,70 @@ 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
3521
3622/* *
3723 * After a route has been selected. This view gives turn-by-turn instructions
3824 * for completing the route.
3925 */
4026@OptIn(MapboxExperimental ::class )
4127class ActiveGuidanceScreen (
42- private val carActiveGuidanceContext : CarActiveGuidanceCarContext ,
28+ private val mainCarContext : MainCarContext ,
4329 private val actionProviders : List <MapboxActionProvider >,
44- private val placesLayerUtil : PlacesListOnMapLayerUtil = PlacesListOnMapLayerUtil (),
45- ) : Screen(carActiveGuidanceContext.carContext) {
30+ ) : Screen(mainCarContext.carContext) {
4631
47- val carRouteLine = CarRouteLine (carActiveGuidanceContext. mainCarContext)
48- val carLocationRenderer = CarLocationRenderer (carActiveGuidanceContext. mainCarContext)
49- val carSpeedLimitRenderer = CarSpeedLimitRenderer (carActiveGuidanceContext. mainCarContext)
32+ val carRouteLine = CarRouteLine (mainCarContext)
33+ val carLocationRenderer = CarLocationRenderer (mainCarContext)
34+ val carSpeedLimitRenderer = CarSpeedLimitRenderer (mainCarContext)
5035 val carNavigationCamera = CarNavigationCamera (
5136 initialCarCameraMode = CarCameraMode .FOLLOWING ,
5237 alternativeCarCameraMode = CarCameraMode .OVERVIEW ,
5338 )
5439 private val roadLabelSurfaceLayer = RoadLabelSurfaceLayer (carContext)
5540 private val navigationInfoProvider = CarNavigationInfoProvider ()
5641 .invalidateOnChange(this )
42+ private val carActiveGuidanceMarkers = CarActiveGuidanceMarkers ()
43+ private val carArrivalTrigger = CarArrivalTrigger ()
5744 private val mapActionStripBuilder = MainMapActionStrip (this , carNavigationCamera)
5845
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- }
109-
11046 init {
11147 logAndroidAuto(" ActiveGuidanceScreen constructor" )
112-
11348 lifecycle.addObserver(object : DefaultLifecycleObserver {
114-
115- override fun onCreate (owner : LifecycleOwner ) {
116- logAndroidAuto(" ActiveGuidanceScreen onCreate" )
117- carActiveGuidanceContext.mapboxNavigation.registerArrivalObserver(arrivalObserver)
118- }
119-
12049 override fun onResume (owner : LifecycleOwner ) {
12150 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(
51+ mainCarContext .mapboxCarMap.registerObserver(carLocationRenderer)
52+ mainCarContext .mapboxCarMap.registerObserver(roadLabelSurfaceLayer)
53+ mainCarContext .mapboxCarMap.registerObserver(carSpeedLimitRenderer)
54+ mainCarContext .mapboxCarMap.registerObserver(carNavigationCamera)
55+ mainCarContext .mapboxCarMap.setGestureHandler(
12756 carNavigationCamera.gestureHandler
12857 )
129- carActiveGuidanceContext.mapboxCarMap.registerObserver(carRouteLine)
130- carActiveGuidanceContext.mapboxCarMap.registerObserver(surfaceListener)
131- carActiveGuidanceContext.mapboxCarMap.registerObserver(navigationInfoProvider)
58+ mainCarContext.mapboxCarMap.registerObserver(carRouteLine)
59+ mainCarContext.mapboxCarMap.registerObserver(carActiveGuidanceMarkers)
60+ mainCarContext.mapboxCarMap.registerObserver(navigationInfoProvider)
61+ mainCarContext.mapboxCarMap.registerObserver(carArrivalTrigger)
13262 }
13363
13464 override fun onPause (owner : LifecycleOwner ) {
13565 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)
66+ mainCarContext.mapboxCarMap.unregisterObserver(roadLabelSurfaceLayer)
67+ mainCarContext.mapboxCarMap.unregisterObserver(carLocationRenderer)
68+ mainCarContext.mapboxCarMap.unregisterObserver(carSpeedLimitRenderer)
69+ mainCarContext.mapboxCarMap.unregisterObserver(carNavigationCamera)
70+ mainCarContext.mapboxCarMap.setGestureHandler(null )
71+ mainCarContext.mapboxCarMap.unregisterObserver(carRouteLine)
72+ mainCarContext.mapboxCarMap.unregisterObserver(carActiveGuidanceMarkers)
73+ mainCarContext.mapboxCarMap.unregisterObserver(navigationInfoProvider)
74+ mainCarContext.mapboxCarMap.unregisterObserver(carArrivalTrigger)
14975 }
15076 })
15177 }
@@ -167,7 +93,7 @@ class ActiveGuidanceScreen(
16793 Action .Builder ()
16894 .setTitle(carContext.getString(R .string.car_action_navigation_stop_button))
16995 .setOnClickListener {
170- stopNavigation ()
96+ carArrivalTrigger.triggerArrival ()
17197 }.build()
17298 )
17399 }.build()
@@ -179,9 +105,4 @@ class ActiveGuidanceScreen(
179105 .apply { navigationInfoProvider.setNavigationInfo(this ) }
180106 .build()
181107 }
182-
183- private fun stopNavigation () {
184- logAndroidAuto(" ActiveGuidanceScreen stopNavigation" )
185- MapboxCarApp .updateCarAppState(ArrivalState )
186- }
187108}
0 commit comments