Skip to content

Commit 312d55d

Browse files
authored
Update AA example to handle init failure (#2389)
1 parent 3af61b3 commit 312d55d

File tree

3 files changed

+72
-14
lines changed

3 files changed

+72
-14
lines changed

android-auto-app/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ dependencies {
6060

6161
// Please review the compatibility guide. This app is showcasing the latest features.
6262
// https://github.com/mapbox/mapbox-maps-android/tree/main/extension-androidauto#compatibility-with-maps-sdk-v10
63-
implementation("com.mapbox.maps:android:10.10.0-beta.1")
63+
implementation("com.mapbox.maps:android:10.16.6")
64+
implementation("androidx.startup:startup-runtime:1.1.1")
6465

6566
implementation(Dependencies.kotlin)
6667
implementation(Dependencies.androidxAppCompat)

android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/car/MapSession.kt

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,58 @@ import android.content.res.Configuration
77
import androidx.car.app.Screen
88
import androidx.car.app.ScreenManager
99
import androidx.car.app.Session
10+
import com.mapbox.common.BaseMapboxInitializer
11+
import com.mapbox.maps.ContextMode
1012
import com.mapbox.maps.MapInitOptions
1113
import com.mapbox.maps.MapboxExperimental
14+
import com.mapbox.maps.extension.androidauto.MapboxCarMapObserver
1215
import 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)
1821
class 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
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.mapbox.maps.testapp.auto.car
2+
3+
import androidx.car.app.CarContext
4+
import androidx.car.app.Screen
5+
import androidx.car.app.ScreenManager
6+
import androidx.car.app.model.Action
7+
import androidx.car.app.model.CarColor
8+
import androidx.car.app.model.MessageTemplate
9+
import androidx.car.app.model.ParkedOnlyOnClickListener
10+
import androidx.car.app.model.Template
11+
12+
/* Retry screen if the map failed to load. */
13+
class RetryScreen(
14+
carContext: CarContext,
15+
private val session: MapSession,
16+
private val carMapShowcase: CarMapShowcase,
17+
private val message: String
18+
) : Screen(carContext) {
19+
20+
override fun onGetTemplate(): Template {
21+
val listener = ParkedOnlyOnClickListener.create {
22+
val screen = session.tryInit(carMapShowcase)
23+
?: RetryScreen(carContext, session, carMapShowcase, "Map not yet loaded...")
24+
carContext.getCarService(ScreenManager::class.java)
25+
.push(screen)
26+
finish()
27+
}
28+
val action = Action.Builder()
29+
.setTitle("Retry loading the map")
30+
.setBackgroundColor(CarColor.GREEN)
31+
.setOnClickListener(listener)
32+
.build()
33+
return MessageTemplate.Builder(message)
34+
.addAction(action)
35+
.setHeaderAction(Action.BACK)
36+
.build()
37+
}
38+
}

0 commit comments

Comments
 (0)