-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Android Auto] Add onClick example #1682
Draft
kmadsen
wants to merge
1
commit into
main
Choose a base branch
from
km-add-support-for-onclick
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/CarAppPreferences.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.mapbox.maps.testapp.auto | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
|
||
/** | ||
* Class gives you the ability to modify preferences that can be used by the car and app. | ||
*/ | ||
class CarAppPreferences(context: Context) { | ||
|
||
val sharedPreferences: SharedPreferences by lazy { | ||
context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE) | ||
} | ||
|
||
fun enableOnClick(enable: Boolean) { | ||
sharedPreferences.edit().putBoolean(BOOLEAN_KEY_ENABLE_ON_CLICK, enable).apply() | ||
} | ||
|
||
fun isOnClickEnabled() = sharedPreferences | ||
.getBoolean(BOOLEAN_KEY_ENABLE_ON_CLICK, false) | ||
|
||
companion object { | ||
private const val SHARED_PREFERENCES_KEY = "mapbox_maps_android_auto_app" | ||
|
||
const val BOOLEAN_KEY_ENABLE_ON_CLICK = "enable_onclick" | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/app/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,29 @@ | ||
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.CarAppPreferences | ||
import com.mapbox.maps.testapp.auto.R | ||
|
||
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.isOnClickEnabled() | ||
switchButton.setOnCheckedChangeListener { _, isChecked -> | ||
if (carAppPreferences.isOnClickEnabled() != isChecked) { | ||
Toast.makeText( | ||
this, | ||
"Custom setting changed, reconnect Android Auto to ensure a restart", | ||
Toast.LENGTH_LONG | ||
).show() | ||
carAppPreferences.enableOnClick(isChecked) | ||
} | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
android-auto-app/src/main/java/com/mapbox/maps/testapp/auto/car/CarMapOnClickEnabler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.mapbox.maps.testapp.auto.car | ||
|
||
import android.content.SharedPreferences | ||
import androidx.car.app.AppManager | ||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import com.mapbox.maps.MapboxExperimental | ||
import com.mapbox.maps.extension.androidauto.MapboxCarMap | ||
import com.mapbox.maps.extension.androidauto.MapboxCarMapInitializer | ||
import com.mapbox.maps.logI | ||
import com.mapbox.maps.testapp.auto.CarAppPreferences | ||
import com.mapbox.maps.testapp.auto.custom.SurfaceCallbackInterceptor | ||
|
||
/** | ||
* In order to enable onClick, you need to override the mapboxCarMap.prepareSurfaceCallback | ||
* | ||
* This class allows you to toggle the preference from the app, to show that you can change this | ||
* setting at runtime. In real scenarios, it may be initialize the map with onClick enabled. | ||
*/ | ||
@OptIn(MapboxExperimental::class) | ||
class CarMapOnClickEnabler( | ||
private val mapboxCarMap: MapboxCarMap, | ||
private val initializer: MapboxCarMapInitializer | ||
) : DefaultLifecycleObserver { | ||
|
||
private var carAppPreferences: CarAppPreferences? = null | ||
|
||
private val listener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key -> | ||
if (key == CarAppPreferences.BOOLEAN_KEY_ENABLE_ON_CLICK) { | ||
val customCallbackEnabled = sharedPreferences.getBoolean(key, false) | ||
onEnableOnClickPreferenceChanged(customCallbackEnabled) | ||
} | ||
} | ||
|
||
override fun onResume(owner: LifecycleOwner) { | ||
super.onResume(owner) | ||
carAppPreferences = CarAppPreferences(mapboxCarMap.carContext).apply { | ||
sharedPreferences.registerOnSharedPreferenceChangeListener(listener) | ||
onEnableOnClickPreferenceChanged(isOnClickEnabled()) | ||
} | ||
} | ||
|
||
override fun onPause(owner: LifecycleOwner) { | ||
super.onPause(owner) | ||
carAppPreferences?.sharedPreferences?.unregisterOnSharedPreferenceChangeListener(listener) | ||
carAppPreferences = null | ||
} | ||
|
||
private fun onEnableOnClickPreferenceChanged(customCallbackEnabled: Boolean) { | ||
val carContext = mapboxCarMap.carContext | ||
if (customCallbackEnabled) { | ||
val surfaceCallback = mapboxCarMap.prepareSurfaceCallback( | ||
carContext, initializer.onCreate(carContext) | ||
) | ||
carContext.getCarService(AppManager::class.java) | ||
.setSurfaceCallback(object : SurfaceCallbackInterceptor(surfaceCallback) { | ||
override fun onClick(x: Float, y: Float) { | ||
super.onClick(x, y) | ||
onMapSurfaceClick(x, y) | ||
} | ||
}) | ||
} else { | ||
mapboxCarMap.setup(carContext, initializer.onCreate(carContext)) | ||
} | ||
} | ||
|
||
private fun onMapSurfaceClick(x: Float, y: Float) { | ||
logI("CarMapOnClickEnabler", "onMapSurfaceClick $x $y") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...-auto-app/src/main/java/com/mapbox/maps/testapp/auto/custom/SurfaceCallbackInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.mapbox.maps.testapp.auto.custom | ||
|
||
import android.graphics.Rect | ||
import androidx.car.app.SurfaceCallback | ||
import androidx.car.app.SurfaceContainer | ||
|
||
abstract class SurfaceCallbackInterceptor constructor( | ||
private val surfaceCallback: SurfaceCallback | ||
) : SurfaceCallback { | ||
|
||
override fun onSurfaceAvailable(surfaceContainer: SurfaceContainer) = | ||
surfaceCallback.onSurfaceAvailable(surfaceContainer) | ||
|
||
override fun onSurfaceDestroyed(surfaceContainer: SurfaceContainer) = | ||
surfaceCallback.onSurfaceDestroyed(surfaceContainer) | ||
|
||
override fun onVisibleAreaChanged(visibleArea: Rect) = | ||
surfaceCallback.onVisibleAreaChanged(visibleArea) | ||
|
||
override fun onStableAreaChanged(stableArea: Rect) = | ||
surfaceCallback.onStableAreaChanged(stableArea) | ||
|
||
override fun onScale(focusX: Float, focusY: Float, scaleFactor: Float) = | ||
surfaceCallback.onScale(focusX, focusY, scaleFactor) | ||
|
||
override fun onScroll(distanceX: Float, distanceY: Float) = | ||
surfaceCallback.onScroll(distanceX, distanceY) | ||
|
||
override fun onFling(velocityX: Float, velocityY: Float) = | ||
surfaceCallback.onFling(velocityX, velocityY) | ||
|
||
override fun onClick(x: Float, y: Float) = | ||
surfaceCallback.onClick(x, y) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can toggle between these at runtime and everything works.
All observers that are attached, will be detached and then attached with a new map surface