1
+ package com.mapbox.maps.testapp.auto.custom
2
+
3
+ import android.Manifest.permission.ACCESS_FINE_LOCATION
4
+ import android.content.Intent
5
+ import android.content.pm.PackageManager.PERMISSION_GRANTED
6
+ import android.content.res.Configuration
7
+ import androidx.car.app.AppManager
8
+ import androidx.car.app.Screen
9
+ import androidx.car.app.ScreenManager
10
+ import androidx.car.app.Session
11
+ import androidx.car.app.SurfaceCallback
12
+ import androidx.lifecycle.DefaultLifecycleObserver
13
+ import androidx.lifecycle.LifecycleOwner
14
+ import com.mapbox.maps.MapInitOptions
15
+ import com.mapbox.maps.MapboxExperimental
16
+ import com.mapbox.maps.extension.androidauto.MapboxCarMap
17
+ import com.mapbox.maps.logI
18
+ import com.mapbox.maps.testapp.auto.car.CarAnimationThreadController
19
+ import com.mapbox.maps.testapp.auto.car.CarMapShowcase
20
+ import com.mapbox.maps.testapp.auto.car.CarMapWidgets
21
+ import com.mapbox.maps.testapp.auto.car.MapScreen
22
+ import com.mapbox.maps.testapp.auto.car.RequestPermissionScreen
23
+
24
+ /* *
25
+ * This session demonstrates an ability to upgrade the androidx.car.app:app: dependency to a new
26
+ * version and use the [SurfaceCallback.onClick] function.
27
+ */
28
+ @OptIn(MapboxExperimental ::class )
29
+ class CustomMapSession : Session () {
30
+
31
+ private val carAnimationThreadController = CarAnimationThreadController ()
32
+ private val carMapWidgets = CarMapWidgets ()
33
+ private val carMapShowcase = CarMapShowcase ()
34
+ private val mapboxCarMap = MapboxCarMap ()
35
+
36
+ init {
37
+ lifecycle.addObserver(object : DefaultLifecycleObserver {
38
+ override fun onCreate (owner : LifecycleOwner ) {
39
+ val mapInitOptions = MapInitOptions (carContext)
40
+ mapboxCarMap.registerObserver(carAnimationThreadController)
41
+ mapboxCarMap.registerObserver(carMapWidgets)
42
+ mapboxCarMap.registerObserver(carMapShowcase)
43
+
44
+ val handle = mapboxCarMap.setupWithCustomCallback(carContext, mapInitOptions)
45
+ carContext.getCarService(AppManager ::class .java)
46
+ .setSurfaceCallback(object : CustomSurfaceCallback (handle) {
47
+ override fun onClick (x : Float , y : Float ) {
48
+ super .onClick(x, y)
49
+ onMapSurfaceClick(x, y)
50
+ }
51
+ })
52
+ }
53
+
54
+ override fun onDestroy (owner : LifecycleOwner ) {
55
+ // Not sure this really would cause a memory leak, but to ensure the reference is removed.
56
+ carContext.getCarService(AppManager ::class .java).setSurfaceCallback(null )
57
+
58
+ mapboxCarMap.unregisterObserver(carMapShowcase)
59
+ mapboxCarMap.unregisterObserver(carMapWidgets)
60
+ mapboxCarMap.unregisterObserver(carAnimationThreadController)
61
+ }
62
+ })
63
+ }
64
+
65
+ override fun onCreateScreen (intent : Intent ): Screen {
66
+ val mapScreen = MapScreen (mapboxCarMap)
67
+ return if (carContext.checkSelfPermission(ACCESS_FINE_LOCATION ) != PERMISSION_GRANTED ) {
68
+ carContext.getCarService(ScreenManager ::class .java)
69
+ .push(mapScreen)
70
+ RequestPermissionScreen (carContext)
71
+ } else mapScreen
72
+ }
73
+
74
+ override fun onCarConfigurationChanged (newConfiguration : Configuration ) {
75
+ carMapShowcase.loadMapStyle(carContext)
76
+ }
77
+
78
+ private fun onMapSurfaceClick (x : Float , y : Float ) {
79
+ logI(" CustomMapSession" , " onClick $x $y " )
80
+ }
81
+ }
0 commit comments