Skip to content

Commit

Permalink
Add support for onClick
Browse files Browse the repository at this point in the history
  • Loading branch information
kmadsen committed Sep 21, 2022
1 parent 955f14c commit 6d5fe47
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 50 deletions.
4 changes: 2 additions & 2 deletions android-auto-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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)
Expand All @@ -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")
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Project.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions extension-androidauto/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 🐞

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -85,16 +85,16 @@ class MapboxCarMap {
* api versions and continue to use the [MapboxCarMap] as designed.
*
* This is a temporary solution, while androidx.car.app:app:1.3.0 is rolling out
* [SurfaceCallback.onClick]. If there is no longer a use for this function in the future, it
* will be removed.
* [SurfaceCallback.onClick]. If there is no use for this function in the future, it will be
* removed.
*/
@MapboxExperimental
fun setupWithCustomCallback(
carContext: CarContext,
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
Expand Down

0 comments on commit 6d5fe47

Please sign in to comment.