@@ -7,39 +7,58 @@ import android.content.res.Configuration
77import androidx.car.app.Screen
88import androidx.car.app.ScreenManager
99import androidx.car.app.Session
10+ import com.mapbox.common.BaseMapboxInitializer
11+ import com.mapbox.maps.ContextMode
1012import com.mapbox.maps.MapInitOptions
1113import com.mapbox.maps.MapboxExperimental
14+ import com.mapbox.maps.extension.androidauto.MapboxCarMapObserver
1215import com.mapbox.maps.extension.androidauto.mapboxMapInstaller
16+ import com.mapbox.maps.loader.MapboxMapsInitializerImpl
1317
1418/* *
1519 * Session class for the Mapbox Map sample app for Android Auto.
1620 */
17- @OptIn(MapboxExperimental ::class )
1821class MapSession : Session () {
19-
2022 private val carMapShowcase = CarMapShowcase ()
21- private val mapboxCarMap = mapboxMapInstaller()
22- .onCreated(CarAnimationThreadController (), CarMapWidgets (), carMapShowcase)
23- .install { carContext ->
24- // Callback is triggered when the Session calls onCreate. This allows you to specify
25- // custom MapInitOptions.
26- MapInitOptions (carContext)
27- }
28-
2923 override fun onCreateScreen (intent : Intent ): Screen {
3024 // The onCreate is guaranteed to be called before onCreateScreen. You can pass the
3125 // mapboxCarMap to other screens. Each screen can register and unregister observers.
3226 // This allows you to scope behaviors to sessions, screens, or events.
33- val mapScreen = MapScreen (mapboxCarMap)
27+
28+ // Also we cover the use-case if native libraries could not yet be loaded and show
29+ // RetryScreen in that case with UI to retry init on a button click
30+ val screen = tryInit(carMapShowcase)
31+ ? : RetryScreen (
32+ carContext,
33+ this ,
34+ carMapShowcase,
35+ " Map not available"
36+ )
3437
3538 return if (carContext.checkSelfPermission(ACCESS_FINE_LOCATION ) != PERMISSION_GRANTED ) {
3639 carContext.getCarService(ScreenManager ::class .java)
37- .push(mapScreen )
40+ .push(screen )
3841 RequestPermissionScreen (carContext)
39- } else mapScreen
42+ } else screen
4043 }
4144
4245 override fun onCarConfigurationChanged (newConfiguration : Configuration ) {
4346 carMapShowcase.loadMapStyle(carContext)
4447 }
48+ }
49+ @OptIn(MapboxExperimental ::class )
50+ internal fun MapSession.tryInit (carMapShowcase : MapboxCarMapObserver ): MapScreen ? = try {
51+ // if Mapbox was init before successful - this is no-op
52+ BaseMapboxInitializer .init (MapboxMapsInitializerImpl ::class .java)
53+ val mapboxCarMap = mapboxMapInstaller()
54+ .onCreated(CarAnimationThreadController (), CarMapWidgets (), carMapShowcase)
55+ .install { carContext ->
56+ val mapOptions = MapInitOptions (carContext).mapOptions.toBuilder()
57+ .contextMode(ContextMode .SHARED )
58+ .build()
59+ MapInitOptions (carContext, mapOptions = mapOptions)
60+ }
61+ MapScreen (mapboxCarMap)
62+ } catch (e: Throwable ) {
63+ null
4564}
0 commit comments