-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
179 additions
and
8 deletions.
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