Skip to content

Commit 0b74952

Browse files
committed
Android auto crash investigations
1 parent 5dc9bdf commit 0b74952

File tree

7 files changed

+75
-56
lines changed

7 files changed

+75
-56
lines changed

android-auto-app/build.gradle.kts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ android {
2929
}
3030

3131
dependencies {
32-
implementation(project(":extension-androidauto"))
32+
// implementation(project(":extension-androidauto"))
3333
implementation(libs.googleCarAppLibrary)
34+
implementation("com.mapbox.maps:android:10.18.1")
35+
implementation("com.mapbox.extension:maps-androidauto:0.5.0")
3436

3537
// Please review the compatibility guide. This app is showcasing the latest features with latest Maps SDK main branch.
3638
// https://github.com/mapbox/mapbox-maps-android/tree/main/extension-androidauto#compatibility-with-maps-sdk-v11
37-
implementation(project(":maps-sdk"))
39+
// implementation(project(":maps-sdk"))
3840

3941
implementation(libs.kotlin)
4042
implementation(libs.androidx.appCompat)

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,27 @@ package com.mapbox.maps.testapp.auto.app
22

33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
5+
import com.mapbox.geojson.Point
6+
import com.mapbox.maps.MapInitOptions
7+
import com.mapbox.maps.MapView
8+
import com.mapbox.maps.dsl.cameraOptions
9+
import com.mapbox.maps.plugin.locationcomponent.createDefault2DPuck
10+
import com.mapbox.maps.plugin.locationcomponent.location
11+
import com.mapbox.maps.plugin.locationcomponent.location2
512
import com.mapbox.maps.testapp.auto.R
613

714
class MainActivity : AppCompatActivity() {
815
override fun onCreate(savedInstanceState: Bundle?) {
916
super.onCreate(savedInstanceState)
10-
setContentView(R.layout.activity_main)
17+
// $ANDROID_HOME/extras/google/auto/desktop-head-unit --usb
18+
setContentView(
19+
MapView(this, mapInitOptions = MapInitOptions(this, cameraOptions = cameraOptions {
20+
center(Point.fromLngLat(24.9384, 60.1699))
21+
zoom(17.0)
22+
})).apply {
23+
location.enabled = true
24+
location.createDefault2DPuck(this@MainActivity, true)
25+
}
26+
)
1127
}
1228
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import com.mapbox.maps.threading.AnimationThreadController
1515
@OptIn(MapboxExperimental::class)
1616
class CarAnimationThreadController : MapboxCarMapObserver {
1717
override fun onAttached(mapboxCarMapSurface: MapboxCarMapSurface) {
18-
if (deviceRequiresBackgroundThreadAnimations()) {
18+
// if (deviceRequiresBackgroundThreadAnimations()) {
1919
AnimationThreadController.useBackgroundThread()
20-
}
20+
// }
2121
}
2222

2323
override fun onDetached(mapboxCarMapSurface: MapboxCarMapSurface) {

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import com.mapbox.maps.dsl.cameraOptions
99
import com.mapbox.maps.extension.androidauto.DefaultMapboxCarMapGestureHandler
1010
import com.mapbox.maps.extension.androidauto.MapboxCarMapObserver
1111
import com.mapbox.maps.extension.androidauto.MapboxCarMapSurface
12-
import com.mapbox.maps.plugin.PuckBearing
1312
import com.mapbox.maps.plugin.animation.camera
1413
import com.mapbox.maps.plugin.locationcomponent.OnIndicatorBearingChangedListener
1514
import com.mapbox.maps.plugin.locationcomponent.OnIndicatorPositionChangedListener
@@ -30,7 +29,7 @@ class CarCameraController : MapboxCarMapObserver {
3029
private val changePositionListener = OnIndicatorPositionChangedListener { point ->
3130
lastGpsLocation = point
3231
if (isTrackingPuck) {
33-
surface?.mapSurface?.mapboxMap?.setCamera(
32+
surface?.mapSurface?.getMapboxMap()?.setCamera(
3433
cameraOptions {
3534
center(point)
3635
padding(insets)
@@ -41,7 +40,7 @@ class CarCameraController : MapboxCarMapObserver {
4140

4241
private val changeBearingListener = OnIndicatorBearingChangedListener { bearing ->
4342
if (isTrackingPuck) {
44-
surface?.mapSurface?.mapboxMap?.setCamera(
43+
surface?.mapSurface?.getMapboxMap()?.setCamera(
4544
cameraOptions {
4645
bearing(bearing)
4746
}
@@ -67,7 +66,7 @@ class CarCameraController : MapboxCarMapObserver {
6766
override fun onAttached(mapboxCarMapSurface: MapboxCarMapSurface) {
6867
super.onAttached(mapboxCarMapSurface)
6968
this.surface = mapboxCarMapSurface
70-
mapboxCarMapSurface.mapSurface.mapboxMap.setCamera(
69+
mapboxCarMapSurface.mapSurface.getMapboxMap().setCamera(
7170
cameraOptions {
7271
pitch(previousCameraState?.pitch ?: INITIAL_PITCH)
7372
zoom(previousCameraState?.zoom ?: INITIAL_ZOOM)
@@ -76,16 +75,15 @@ class CarCameraController : MapboxCarMapObserver {
7675
)
7776
with(mapboxCarMapSurface.mapSurface.location) {
7877
// Show a 3D location puck
79-
locationPuck = CarLocationPuck.duckLocationPuckConstantSize
78+
locationPuck = CarLocationPuck.duckLocationPuckLowZoom
8079
enabled = true
81-
puckBearing = PuckBearing.HEADING
8280
addOnIndicatorPositionChangedListener(changePositionListener)
8381
addOnIndicatorBearingChangedListener(changeBearingListener)
8482
}
8583
}
8684

8785
override fun onDetached(mapboxCarMapSurface: MapboxCarMapSurface) {
88-
previousCameraState = mapboxCarMapSurface.mapSurface.mapboxMap.cameraState
86+
previousCameraState = mapboxCarMapSurface.mapSurface.getMapboxMap().cameraState
8987
with(mapboxCarMapSurface.mapSurface.location) {
9088
removeOnIndicatorPositionChangedListener(changePositionListener)
9189
removeOnIndicatorBearingChangedListener(changeBearingListener)
@@ -121,7 +119,7 @@ class CarCameraController : MapboxCarMapObserver {
121119

122120
private fun scaleEaseBy(delta: Double) {
123121
val mapSurface = surface?.mapSurface
124-
val fromZoom = mapSurface?.mapboxMap?.cameraState?.zoom ?: return
122+
val fromZoom = mapSurface?.getMapboxMap()?.cameraState?.zoom ?: return
125123
val toZoom = (fromZoom + delta).coerceIn(MIN_ZOOM_OUT, MAX_ZOOM_IN)
126124
mapSurface.camera.easeTo(cameraOptions { zoom(toZoom) })
127125
}
@@ -145,7 +143,7 @@ class CarCameraController : MapboxCarMapObserver {
145143
/**
146144
* When zooming the camera by a delta, this will prevent the camera from zooming further.
147145
*/
148-
private const val MIN_ZOOM_OUT = 0.0
146+
private const val MIN_ZOOM_OUT = 6.0
149147

150148
/**
151149
* When zooming the camera by a delta, this will prevent the camera from zooming further.
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.mapbox.maps.testapp.auto.car
22

33
import android.annotation.SuppressLint
4-
import com.mapbox.maps.ImageHolder
5-
import com.mapbox.maps.MapboxExperimental
4+
import androidx.car.app.CarContext
65
import com.mapbox.maps.R
6+
import com.mapbox.maps.extension.style.expressions.dsl.generated.literal
77
import com.mapbox.maps.plugin.LocationPuck2D
88
import com.mapbox.maps.plugin.LocationPuck3D
9-
import com.mapbox.maps.plugin.ModelScaleMode
109

1110
/**
1211
* Provides car location puck definitions.
@@ -15,32 +14,28 @@ internal object CarLocationPuck {
1514
/**
1615
* 3D location puck with the real world size.
1716
*/
18-
@OptIn(MapboxExperimental::class)
19-
val duckLocationPuckRealWorld = LocationPuck3D(
17+
val duckLocationPuckHighZoom = LocationPuck3D(
2018
modelUri = "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Duck/glTF-Embedded/Duck.gltf",
21-
modelScale = listOf(20f, 20f, 20f),
22-
modelScaleMode = ModelScaleMode.MAP,
19+
modelScaleExpression = literal(listOf(10, 10, 10)).toJson(),
2320
modelRotation = listOf(0f, 0f, -90f)
2421
)
2522

2623
/**
2724
* 3D location puck with a constant size across zoom levels.
2825
*/
29-
@OptIn(MapboxExperimental::class)
30-
val duckLocationPuckConstantSize = LocationPuck3D(
26+
val duckLocationPuckLowZoom = LocationPuck3D(
3127
modelUri = "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Duck/glTF-Embedded/Duck.gltf",
32-
modelScale = listOf(20f, 20f, 20f),
33-
modelScaleMode = ModelScaleMode.VIEWPORT,
28+
modelScale = listOf(0.2f, 0.2f, 0.2f),
3429
modelRotation = listOf(0f, 0f, -90f)
3530
)
3631

3732
/**
3833
* Classic 2D location puck with blue dot and arrow.
3934
*/
4035
@SuppressLint("UseCompatLoadingForDrawables")
41-
val classicLocationPuck2D = LocationPuck2D(
42-
topImage = ImageHolder.Companion.from(R.drawable.mapbox_user_icon),
43-
bearingImage = ImageHolder.Companion.from(R.drawable.mapbox_user_bearing_icon),
44-
shadowImage = ImageHolder.Companion.from(R.drawable.mapbox_user_stroke_icon)
36+
fun classicLocationPuck2D(carContext: CarContext) = LocationPuck2D(
37+
topImage = carContext.getDrawable(R.drawable.mapbox_user_icon),
38+
bearingImage = carContext.getDrawable(R.drawable.mapbox_user_bearing_icon),
39+
shadowImage = carContext.getDrawable(R.drawable.mapbox_user_stroke_icon)
4540
)
4641
}
Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.mapbox.maps.testapp.auto.car
22

33
import androidx.car.app.CarContext
4-
import com.mapbox.bindgen.Value
54
import com.mapbox.maps.Style
65
import com.mapbox.maps.extension.androidauto.MapboxCarMapObserver
76
import com.mapbox.maps.extension.androidauto.MapboxCarMapSurface
8-
import com.mapbox.maps.logE
7+
import com.mapbox.maps.extension.style.layers.generated.skyLayer
8+
import com.mapbox.maps.extension.style.layers.properties.generated.SkyType
9+
import com.mapbox.maps.extension.style.sources.generated.rasterDemSource
10+
import com.mapbox.maps.extension.style.style
11+
import com.mapbox.maps.extension.style.terrain.generated.terrain
912

1013
/**
1114
* Example showing how you can add a sky layer that has a sun direction,
@@ -24,28 +27,35 @@ class CarMapShowcase : MapboxCarMapObserver {
2427
this.mapboxCarMapSurface = null
2528
}
2629

27-
fun loadMapStyle(carContext: CarContext) {
28-
val lightPreset = if (carContext.isDarkMode) LightPresets.NIGHT else LightPresets.DAY
29-
30-
mapboxCarMapSurface?.mapSurface?.mapboxMap?.loadStyle(Style.STANDARD) { style ->
31-
style.setStyleImportConfigProperty(
32-
"basemap",
33-
"lightPreset",
34-
Value.valueOf(lightPreset.toString().lowercase())
35-
).error?.let {
36-
logE(TAG, it)
37-
}
38-
}
30+
fun mapStyleUri(carContext: CarContext): String {
31+
return if (carContext.isDarkMode) Style.TRAFFIC_NIGHT else Style.TRAFFIC_DAY
3932
}
4033

41-
private enum class LightPresets {
42-
DAY,
43-
DAWN,
44-
DUSK,
45-
NIGHT
34+
fun loadMapStyle(carContext: CarContext) {
35+
// https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#paint-sky-sky-atmosphere-sun
36+
// Position of the sun center [a azimuthal angle, p polar angle].
37+
// Azimuthal is degrees from north, where 0.0 is north.
38+
// Polar is degrees from overhead, where 0.0 is overhead.
39+
val sunDirection = if (carContext.isDarkMode) listOf(-50.0, 90.2) else listOf(0.0, 0.0)
40+
41+
mapboxCarMapSurface?.mapSurface?.getMapboxMap()?.loadStyle(
42+
styleExtension = style(mapStyleUri(carContext)) {
43+
+rasterDemSource(DEM_SOURCE) {
44+
url(TERRAIN_URL_TILE_RESOURCE)
45+
tileSize(514)
46+
}
47+
+terrain(DEM_SOURCE)
48+
+skyLayer(SKY_LAYER) {
49+
skyType(SkyType.ATMOSPHERE)
50+
skyAtmosphereSun(sunDirection)
51+
}
52+
}
53+
)
4654
}
4755

4856
companion object {
49-
private const val TAG = "CarMapShowcase"
57+
private const val SKY_LAYER = "sky"
58+
private const val DEM_SOURCE = "mapbox-dem"
59+
private const val TERRAIN_URL_TILE_RESOURCE = "mapbox://mapbox.mapbox-terrain-dem-v1"
5060
}
5161
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.mapbox.maps.testapp.auto.car
22

3-
import com.mapbox.common.Cancelable
43
import com.mapbox.maps.MapboxExperimental
54
import com.mapbox.maps.extension.androidauto.MapboxCarMapObserver
65
import com.mapbox.maps.extension.androidauto.MapboxCarMapSurface
76
import com.mapbox.maps.extension.androidauto.widgets.CompassWidget
87
import com.mapbox.maps.extension.androidauto.widgets.LogoWidget
8+
import com.mapbox.maps.plugin.delegates.listeners.OnCameraChangeListener
99

1010
/**
1111
* Note that the Widgets are only available when using android auto extension together with the
@@ -15,7 +15,7 @@ import com.mapbox.maps.extension.androidauto.widgets.LogoWidget
1515
class CarMapWidgets : MapboxCarMapObserver {
1616
private lateinit var logoWidget: LogoWidget
1717
private lateinit var compassWidget: CompassWidget
18-
private var cancellable: Cancelable? = null
18+
private lateinit var onCameraChangeListener: OnCameraChangeListener
1919
override fun onAttached(mapboxCarMapSurface: MapboxCarMapSurface) {
2020
super.onAttached(mapboxCarMapSurface)
2121
with(mapboxCarMapSurface) {
@@ -25,19 +25,17 @@ class CarMapWidgets : MapboxCarMapObserver {
2525
marginX = 26f,
2626
marginY = 120f,
2727
)
28+
onCameraChangeListener = OnCameraChangeListener { compassWidget.setRotation(-mapSurface.getMapboxMap().cameraState.bearing.toFloat()) }
2829
mapSurface.addWidget(logoWidget)
2930
mapSurface.addWidget(compassWidget)
30-
cancellable = mapSurface.mapboxMap.subscribeCameraChanged {
31-
compassWidget.setRotation(-mapSurface.mapboxMap.cameraState.bearing.toFloat())
32-
}
31+
mapSurface.getMapboxMap().addOnCameraChangeListener(onCameraChangeListener)
3332
}
3433
}
3534

3635
override fun onDetached(mapboxCarMapSurface: MapboxCarMapSurface) {
3736
super.onDetached(mapboxCarMapSurface)
3837
with(mapboxCarMapSurface) {
39-
cancellable?.cancel()
40-
cancellable = null
38+
mapSurface.getMapboxMap().removeOnCameraChangeListener(onCameraChangeListener)
4139
mapSurface.removeWidget(logoWidget)
4240
mapSurface.removeWidget(compassWidget)
4341
}

0 commit comments

Comments
 (0)