Skip to content

Commit 6d5fe47

Browse files
committed
Add support for onClick
1 parent 955f14c commit 6d5fe47

File tree

8 files changed

+76
-50
lines changed

8 files changed

+76
-50
lines changed

android-auto-app/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ dependencies {
4949

5050
// Upgrade the google car library to demonstrate adopting new apis.
5151
implementation("androidx.car.app:app:1.3.0-beta01")
52-
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.1")
53-
// implementation(Dependencies.kotlin)
52+
53+
implementation(Dependencies.kotlin)
5454
implementation(Dependencies.androidxAppCompat)
5555
implementation(Dependencies.androidxCoreKtx)
5656
implementation(Dependencies.googleMaterialDesign)

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ package com.mapbox.maps.testapp.auto.app
33
import android.os.Bundle
44
import android.widget.Toast
55
import androidx.appcompat.app.AppCompatActivity
6+
import androidx.appcompat.widget.SwitchCompat
67
import com.mapbox.maps.testapp.auto.R
7-
import com.mapbox.maps.testapp.auto.service.MapboxCarAppService
8-
import kotlinx.android.synthetic.main.activity_main.*
9-
8+
import com.mapbox.maps.testapp.auto.service.CarAppPreferences
109

1110
class MainActivity : AppCompatActivity() {
1211
override fun onCreate(savedInstanceState: Bundle?) {
1312
super.onCreate(savedInstanceState)
1413
setContentView(R.layout.activity_main)
14+
val switchButton: SwitchCompat = findViewById(R.id.switchButton)
1515

16+
val carAppPreferences = CarAppPreferences(applicationContext)
17+
switchButton.isChecked = carAppPreferences.isCustomCallbackEnabled()
1618
switchButton.setOnCheckedChangeListener { _, isChecked ->
17-
if (MapboxCarAppService.enableCustomCallback != isChecked) {
19+
if (carAppPreferences.isCustomCallbackEnabled() != isChecked) {
1820
Toast.makeText(
1921
this,
20-
"Custom setting changed please reconnect to the Android Auto",
21-
Toast.LENGTH_LONG)
22-
.show()
22+
"Custom setting changed, reconnect Android Auto to ensure a restart",
23+
Toast.LENGTH_LONG
24+
).show()
25+
carAppPreferences.enableCustomCallback(isChecked)
2326
}
24-
MapboxCarAppService.enableCustomCallback = isChecked
2527
}
2628
}
2729
}

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

+31-27
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import androidx.car.app.AppManager
88
import androidx.car.app.Screen
99
import androidx.car.app.ScreenManager
1010
import androidx.car.app.Session
11+
import androidx.car.app.SurfaceCallback
1112
import androidx.lifecycle.DefaultLifecycleObserver
1213
import androidx.lifecycle.LifecycleOwner
1314
import com.mapbox.maps.MapInitOptions
1415
import com.mapbox.maps.MapboxExperimental
1516
import com.mapbox.maps.extension.androidauto.MapboxCarMap
16-
import com.mapbox.maps.extension.androidauto.mapboxMapInstaller
1717
import com.mapbox.maps.logI
1818
import com.mapbox.maps.testapp.auto.car.CarAnimationThreadController
1919
import com.mapbox.maps.testapp.auto.car.CarMapShowcase
@@ -22,7 +22,8 @@ import com.mapbox.maps.testapp.auto.car.MapScreen
2222
import com.mapbox.maps.testapp.auto.car.RequestPermissionScreen
2323

2424
/**
25-
* Session class for the Mapbox Map sample app for Android Auto.
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.
2627
*/
2728
@OptIn(MapboxExperimental::class)
2829
class CustomMapSession : Session() {
@@ -33,37 +34,36 @@ class CustomMapSession : Session() {
3334
private val mapboxCarMap = MapboxCarMap()
3435

3536
init {
36-
lifecycle.addObserver(object : DefaultLifecycleObserver {
37-
override fun onCreate(owner: LifecycleOwner) {
38-
val mapInitOptions = MapInitOptions(carContext)
39-
mapboxCarMap.registerObserver(carAnimationThreadController)
40-
mapboxCarMap.registerObserver(carMapWidgets)
41-
mapboxCarMap.registerObserver(carMapShowcase)
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)
4243

43-
val handle = mapboxCarMap.setupWithCustomCallback(carContext, mapInitOptions)
44-
carContext.getCarService(AppManager::class.java)
45-
.setSurfaceCallback(object : CustomSurfaceCallback(handle) {
46-
override fun onClick(x: Float, y: Float) {
47-
super.onClick(x, y)
48-
logI("CustomMapSession", "onClick $x $y")
49-
}
50-
})
51-
}
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+
}
5253

53-
override fun onDestroy(owner: LifecycleOwner) {
54-
mapboxCarMap.unregisterObserver(carMapShowcase)
55-
mapboxCarMap.unregisterObserver(carMapWidgets)
56-
mapboxCarMap.unregisterObserver(carAnimationThreadController)
57-
}
58-
})
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+
})
5963
}
6064

6165
override fun onCreateScreen(intent: Intent): Screen {
62-
// The onCreate is guaranteed to be called before onCreateScreen. You can pass the
63-
// mapboxCarMap to other screens. Each screen can register and unregister observers.
64-
// This allows you to scope behaviors to sessions, screens, or events.
6566
val mapScreen = MapScreen(mapboxCarMap)
66-
6767
return if (carContext.checkSelfPermission(ACCESS_FINE_LOCATION) != PERMISSION_GRANTED) {
6868
carContext.getCarService(ScreenManager::class.java)
6969
.push(mapScreen)
@@ -74,4 +74,8 @@ class CustomMapSession : Session() {
7474
override fun onCarConfigurationChanged(newConfiguration: Configuration) {
7575
carMapShowcase.loadMapStyle(carContext)
7676
}
77+
78+
private fun onMapSurfaceClick(x: Float, y: Float) {
79+
logI("CustomMapSession", "onClick $x $y")
80+
}
7781
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.mapbox.maps.testapp.auto.service
2+
3+
import android.content.Context
4+
5+
/**
6+
* Class gives you the ability to modify the demo app shared preferences.
7+
*/
8+
class CarAppPreferences(context: Context) {
9+
10+
private val sharedPreferences by lazy {
11+
context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
12+
}
13+
14+
fun enableCustomCallback(enable: Boolean) {
15+
sharedPreferences.edit().putBoolean(BOOLEAN_KEY_ENABLE_CUSTOM_CALLBACK, enable).apply()
16+
}
17+
18+
fun isCustomCallbackEnabled() = sharedPreferences
19+
.getBoolean(BOOLEAN_KEY_ENABLE_CUSTOM_CALLBACK, false)
20+
21+
private companion object {
22+
private const val SHARED_PREFERENCES_KEY = "mapbox_maps_android_auto_app"
23+
24+
private const val BOOLEAN_KEY_ENABLE_CUSTOM_CALLBACK = "enable_custom_callback"
25+
}
26+
}

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

+1-8
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,10 @@ class MapboxCarAppService : CarAppService() {
1919
}
2020

2121
override fun onCreateSession(): Session {
22-
return if (enableCustomCallback) {
22+
return if (CarAppPreferences(this).isCustomCallbackEnabled()) {
2323
CustomMapSession()
2424
} else {
2525
MapSession()
2626
}
2727
}
28-
29-
companion object {
30-
/**
31-
* This can be toggled by the mobile app.
32-
*/
33-
var enableCustomCallback = false
34-
}
3528
}

buildSrc/src/main/kotlin/Project.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ object Dependencies {
9797

9898
object Versions {
9999
const val pluginAndroidGradle = "7.0.4"
100-
const val pluginKotlin = "1.5.31"
100+
const val pluginKotlin = "1.7.10"
101101
const val pluginLicense = "0.8.80"
102102
const val pluginDokka = "1.5.31"
103103
const val pluginJacoco = "0.2"

extension-androidauto/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Mapbox welcomes participation and contributions from everyone.
88
* Add `MapboxCarMapSessionInstaller` and `MapboxCarMapScreenInstaller` for simpler setup. ([#1603](https://github.com/mapbox/mapbox-maps-android/pull/1603))
99
* Add `Session.mapboxMapInstaller` and `Screen.mapboxMapInstaller` extension functions to create the installers. ([#1603](https://github.com/mapbox/mapbox-maps-android/pull/1603))
1010
* Change `MapboxCarMapGestureHandler` to an java interface so default methods can be added without breaking java backwards compatibility. ([#1670](https://github.com/mapbox/mapbox-maps-android/pull/1670))
11+
* Add support for intercepting the `SurfaceCallback#onClick` with an experimental method `MapboxCarMap.setupWithCustomCallback`.
1112

1213
## Bug fixes 🐞
1314

extension-androidauto/src/main/java/com/mapbox/maps/extension/androidauto/MapboxCarMap.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class MapboxCarMap {
7373
mapInitOptions: MapInitOptions,
7474
) = apply {
7575
check(mapInitOptions.context is CarContext) {
76-
"You must setup the MapboxCarMap MapInitOptions with a CarContext"
76+
"You must set up the MapboxCarMap MapInitOptions with a CarContext"
7777
}
7878
carMapSurfaceOwner.setup(carContext, mapInitOptions)
7979
carContext.getCarService(AppManager::class.java).setSurfaceCallback(carMapSurfaceOwner)
@@ -85,16 +85,16 @@ class MapboxCarMap {
8585
* api versions and continue to use the [MapboxCarMap] as designed.
8686
*
8787
* This is a temporary solution, while androidx.car.app:app:1.3.0 is rolling out
88-
* [SurfaceCallback.onClick]. If there is no longer a use for this function in the future, it
89-
* will be removed.
88+
* [SurfaceCallback.onClick]. If there is no use for this function in the future, it will be
89+
* removed.
9090
*/
9191
@MapboxExperimental
9292
fun setupWithCustomCallback(
9393
carContext: CarContext,
9494
mapInitOptions: MapInitOptions
9595
): CarMapSurfaceOwner {
9696
check(mapInitOptions.context is CarContext) {
97-
"You must setup the MapboxCarMap MapInitOptions with a CarContext"
97+
"You must set up the MapboxCarMap MapInitOptions with a CarContext"
9898
}
9999
carMapSurfaceOwner.setup(carContext, mapInitOptions)
100100
return carMapSurfaceOwner

0 commit comments

Comments
 (0)