From 212034be2b8ed8ea76d693b8deea74672eacd6ca Mon Sep 17 00:00:00 2001 From: Kyle Madsen Date: Wed, 21 Sep 2022 10:48:04 -0700 Subject: [PATCH] Add support for onClick --- android-auto-app/build.gradle.kts | 4 +- .../maps/testapp/auto/app/MainActivity.kt | 18 +++--- .../testapp/auto/custom/CustomMapSession.kt | 58 ++++++++++--------- .../testapp/auto/service/CarAppPreferences.kt | 26 +++++++++ .../auto/service/MapboxCarAppService.kt | 9 +-- buildSrc/src/main/kotlin/Project.kt | 2 +- extension-androidauto/CHANGELOG.md | 1 + .../extension/androidauto/MapboxCarMap.kt | 4 +- 8 files changed, 74 insertions(+), 48 deletions(-) create mode 100644 android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/service/CarAppPreferences.kt diff --git a/android-auto-app/build.gradle.kts b/android-auto-app/build.gradle.kts index 93fcb30638..b86d49b366 100644 --- a/android-auto-app/build.gradle.kts +++ b/android-auto-app/build.gradle.kts @@ -49,8 +49,8 @@ dependencies { // Upgrade the google car library to demonstrate adopting new apis. implementation("androidx.car.app:app:1.3.0-beta01") - implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.1") -// implementation(Dependencies.kotlin) + + implementation(Dependencies.kotlin) implementation(Dependencies.androidxAppCompat) implementation(Dependencies.androidxCoreKtx) implementation(Dependencies.googleMaterialDesign) diff --git a/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/app/MainActivity.kt b/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/app/MainActivity.kt index 959751c40b..8686b1583e 100644 --- a/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/app/MainActivity.kt +++ b/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/app/MainActivity.kt @@ -3,25 +3,27 @@ package com.mapbox.maps.testapp.auto.app import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.widget.SwitchCompat import com.mapbox.maps.testapp.auto.R -import com.mapbox.maps.testapp.auto.service.MapboxCarAppService -import kotlinx.android.synthetic.main.activity_main.* - +import com.mapbox.maps.testapp.auto.service.CarAppPreferences class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + val switchButton: SwitchCompat = findViewById(R.id.switchButton) + val carAppPreferences = CarAppPreferences(applicationContext) + switchButton.isChecked = carAppPreferences.isCustomCallbackEnabled() switchButton.setOnCheckedChangeListener { _, isChecked -> - if (MapboxCarAppService.enableCustomCallback != isChecked) { + if (carAppPreferences.isCustomCallbackEnabled() != isChecked) { Toast.makeText( this, - "Custom setting changed please reconnect to the Android Auto", - Toast.LENGTH_LONG) - .show() + "Custom setting changed, reconnect Android Auto to ensure a restart", + Toast.LENGTH_LONG + ).show() + carAppPreferences.enableCustomCallback(isChecked) } - MapboxCarAppService.enableCustomCallback = isChecked } } } \ No newline at end of file diff --git a/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/custom/CustomMapSession.kt b/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/custom/CustomMapSession.kt index 50222e4a33..87a9da25db 100644 --- a/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/custom/CustomMapSession.kt +++ b/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/custom/CustomMapSession.kt @@ -8,12 +8,12 @@ import androidx.car.app.AppManager import androidx.car.app.Screen import androidx.car.app.ScreenManager import androidx.car.app.Session +import androidx.car.app.SurfaceCallback import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner import com.mapbox.maps.MapInitOptions import com.mapbox.maps.MapboxExperimental import com.mapbox.maps.extension.androidauto.MapboxCarMap -import com.mapbox.maps.extension.androidauto.mapboxMapInstaller import com.mapbox.maps.logI import com.mapbox.maps.testapp.auto.car.CarAnimationThreadController import com.mapbox.maps.testapp.auto.car.CarMapShowcase @@ -22,7 +22,8 @@ import com.mapbox.maps.testapp.auto.car.MapScreen import com.mapbox.maps.testapp.auto.car.RequestPermissionScreen /** - * Session class for the Mapbox Map sample app for Android Auto. + * This session demonstrates an ability to upgrade the androidx.car.app:app: dependency to a new + * version and use the [SurfaceCallback.onClick] function. */ @OptIn(MapboxExperimental::class) class CustomMapSession : Session() { @@ -33,37 +34,36 @@ class CustomMapSession : Session() { private val mapboxCarMap = MapboxCarMap() init { - lifecycle.addObserver(object : DefaultLifecycleObserver { - override fun onCreate(owner: LifecycleOwner) { - val mapInitOptions = MapInitOptions(carContext) - mapboxCarMap.registerObserver(carAnimationThreadController) - mapboxCarMap.registerObserver(carMapWidgets) - mapboxCarMap.registerObserver(carMapShowcase) + lifecycle.addObserver(object : DefaultLifecycleObserver { + override fun onCreate(owner: LifecycleOwner) { + val mapInitOptions = MapInitOptions(carContext) + mapboxCarMap.registerObserver(carAnimationThreadController) + mapboxCarMap.registerObserver(carMapWidgets) + mapboxCarMap.registerObserver(carMapShowcase) - val handle = mapboxCarMap.setupWithCustomCallback(carContext, mapInitOptions) - carContext.getCarService(AppManager::class.java) - .setSurfaceCallback(object : CustomSurfaceCallback(handle) { - override fun onClick(x: Float, y: Float) { - super.onClick(x, y) - logI("CustomMapSession", "onClick $x $y") - } - }) - } + val handle = mapboxCarMap.setupWithCustomCallback(carContext, mapInitOptions) + carContext.getCarService(AppManager::class.java) + .setSurfaceCallback(object : CustomSurfaceCallback(handle) { + override fun onClick(x: Float, y: Float) { + super.onClick(x, y) + onMapSurfaceClick(x, y) + } + }) + } - override fun onDestroy(owner: LifecycleOwner) { - mapboxCarMap.unregisterObserver(carMapShowcase) - mapboxCarMap.unregisterObserver(carMapWidgets) - mapboxCarMap.unregisterObserver(carAnimationThreadController) - } - }) + override fun onDestroy(owner: LifecycleOwner) { + // Not sure this really would cause a memory leak, but to ensure the reference is removed. + carContext.getCarService(AppManager::class.java).setSurfaceCallback(null) + + mapboxCarMap.unregisterObserver(carMapShowcase) + mapboxCarMap.unregisterObserver(carMapWidgets) + mapboxCarMap.unregisterObserver(carAnimationThreadController) + } + }) } override fun onCreateScreen(intent: Intent): Screen { - // The onCreate is guaranteed to be called before onCreateScreen. You can pass the - // mapboxCarMap to other screens. Each screen can register and unregister observers. - // This allows you to scope behaviors to sessions, screens, or events. val mapScreen = MapScreen(mapboxCarMap) - return if (carContext.checkSelfPermission(ACCESS_FINE_LOCATION) != PERMISSION_GRANTED) { carContext.getCarService(ScreenManager::class.java) .push(mapScreen) @@ -74,4 +74,8 @@ class CustomMapSession : Session() { override fun onCarConfigurationChanged(newConfiguration: Configuration) { carMapShowcase.loadMapStyle(carContext) } + + private fun onMapSurfaceClick(x: Float, y: Float) { + logI("CustomMapSession", "onClick $x $y") + } } \ No newline at end of file diff --git a/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/service/CarAppPreferences.kt b/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/service/CarAppPreferences.kt new file mode 100644 index 0000000000..c02110390d --- /dev/null +++ b/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/service/CarAppPreferences.kt @@ -0,0 +1,26 @@ +package com.mapbox.maps.testapp.auto.service + +import android.content.Context + +/** + * Class gives you the ability to modify the demo app shared preferences. + */ +class CarAppPreferences(context: Context) { + + private val sharedPreferences by lazy { + context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE) + } + + fun enableCustomCallback(enable: Boolean) { + sharedPreferences.edit().putBoolean(BOOLEAN_KEY_ENABLE_CUSTOM_CALLBACK, enable).apply() + } + + fun isCustomCallbackEnabled() = sharedPreferences + .getBoolean(BOOLEAN_KEY_ENABLE_CUSTOM_CALLBACK, false) + + private companion object { + private const val SHARED_PREFERENCES_KEY = "mapbox_maps_android_auto_app" + + private const val BOOLEAN_KEY_ENABLE_CUSTOM_CALLBACK = "enable_custom_callback" + } +} \ No newline at end of file diff --git a/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/service/MapboxCarAppService.kt b/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/service/MapboxCarAppService.kt index cd6c53e2a7..de79570816 100644 --- a/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/service/MapboxCarAppService.kt +++ b/android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/service/MapboxCarAppService.kt @@ -19,17 +19,10 @@ class MapboxCarAppService : CarAppService() { } override fun onCreateSession(): Session { - return if (enableCustomCallback) { + return if (CarAppPreferences(this).isCustomCallbackEnabled()) { CustomMapSession() } else { MapSession() } } - - companion object { - /** - * This can be toggled by the mobile app. - */ - var enableCustomCallback = false - } } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/Project.kt b/buildSrc/src/main/kotlin/Project.kt index 9d3fe35865..737e8eb0f1 100644 --- a/buildSrc/src/main/kotlin/Project.kt +++ b/buildSrc/src/main/kotlin/Project.kt @@ -97,7 +97,7 @@ object Dependencies { object Versions { const val pluginAndroidGradle = "7.0.4" - const val pluginKotlin = "1.5.31" + const val pluginKotlin = "1.7.10" const val pluginLicense = "0.8.80" const val pluginDokka = "1.5.31" const val pluginJacoco = "0.2" diff --git a/extension-androidauto/CHANGELOG.md b/extension-androidauto/CHANGELOG.md index 4fd4f2d8ed..39edb6abbb 100644 --- a/extension-androidauto/CHANGELOG.md +++ b/extension-androidauto/CHANGELOG.md @@ -8,6 +8,7 @@ Mapbox welcomes participation and contributions from everyone. * Add `MapboxCarMapSessionInstaller` and `MapboxCarMapScreenInstaller` for simpler setup. ([#1603](https://github.com/mapbox/mapbox-maps-android/pull/1603)) * Add `Session.mapboxMapInstaller` and `Screen.mapboxMapInstaller` extension functions to create the installers. ([#1603](https://github.com/mapbox/mapbox-maps-android/pull/1603)) * 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)) +* Add support for intercepting the `SurfaceCallback#onClick` with an experimental method `MapboxCarMap.setupWithCustomCallback`. ## Bug fixes 🐞 diff --git a/extension-androidauto/src/main/java/com/mapbox/maps/extension/androidauto/MapboxCarMap.kt b/extension-androidauto/src/main/java/com/mapbox/maps/extension/androidauto/MapboxCarMap.kt index 286526f89e..3d95ee3faf 100644 --- a/extension-androidauto/src/main/java/com/mapbox/maps/extension/androidauto/MapboxCarMap.kt +++ b/extension-androidauto/src/main/java/com/mapbox/maps/extension/androidauto/MapboxCarMap.kt @@ -73,7 +73,7 @@ class MapboxCarMap { mapInitOptions: MapInitOptions, ) = apply { check(mapInitOptions.context is CarContext) { - "You must setup the MapboxCarMap MapInitOptions with a CarContext" + "You must set up the MapboxCarMap MapInitOptions with a CarContext" } carMapSurfaceOwner.setup(carContext, mapInitOptions) carContext.getCarService(AppManager::class.java).setSurfaceCallback(carMapSurfaceOwner) @@ -94,7 +94,7 @@ class MapboxCarMap { mapInitOptions: MapInitOptions ): CarMapSurfaceOwner { check(mapInitOptions.context is CarContext) { - "You must setup the MapboxCarMap MapInitOptions with a CarContext" + "You must set up the MapboxCarMap MapInitOptions with a CarContext" } carMapSurfaceOwner.setup(carContext, mapInitOptions) return carMapSurfaceOwner