Skip to content

Commit

Permalink
Allow for downstream upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
kmadsen committed Sep 21, 2022
1 parent c015c41 commit 7ea29d8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
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 @@ -17,7 +17,7 @@ import java.util.concurrent.CopyOnWriteArraySet
* Maintains the surface state for [MapboxCarMap].
*/
@MapboxExperimental
internal class CarMapSurfaceOwner(
class CarMapSurfaceOwner(
var gestureHandler: MapboxCarMapGestureHandler? = DefaultMapboxCarMapGestureHandler()
) : SurfaceCallback {

Expand All @@ -35,14 +35,14 @@ internal class CarMapSurfaceOwner(

private val carMapObservers = CopyOnWriteArraySet<MapboxCarMapObserver>()

fun setup(carContext: CarContext, mapInitOptions: MapInitOptions) = apply {
internal fun setup(carContext: CarContext, mapInitOptions: MapInitOptions) = apply {
this.carContext = carContext
this.mapInitOptions = mapInitOptions
}

fun isSetup(): Boolean = this::carContext.isInitialized && this::mapInitOptions.isInitialized
internal fun isSetup(): Boolean = this::carContext.isInitialized && this::mapInitOptions.isInitialized

fun registerObserver(mapboxCarMapObserver: MapboxCarMapObserver) {
internal fun registerObserver(mapboxCarMapObserver: MapboxCarMapObserver) {
carMapObservers.add(mapboxCarMapObserver)
logI(TAG, "registerObserver + 1 = ${carMapObservers.size}")

Expand All @@ -55,13 +55,13 @@ internal class CarMapSurfaceOwner(
}
}

fun unregisterObserver(mapboxCarMapObserver: MapboxCarMapObserver) {
internal fun unregisterObserver(mapboxCarMapObserver: MapboxCarMapObserver) {
carMapObservers.remove(mapboxCarMapObserver)
mapboxCarMapSurface?.let { mapboxCarMapObserver.onDetached(it) }
logI(TAG, "unregisterObserver - 1 = ${carMapObservers.size}")
}

fun clearObservers() {
internal fun clearObservers() {
this.mapboxCarMapSurface?.let { surface -> carMapObservers.forEach { it.onDetached(surface) } }
carMapObservers.clear()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.graphics.Rect
import androidx.car.app.AppManager
import androidx.car.app.CarContext
import androidx.car.app.Session
import androidx.car.app.SurfaceCallback
import androidx.lifecycle.Lifecycle
import com.mapbox.maps.EdgeInsets
import com.mapbox.maps.MapInitOptions
Expand Down Expand Up @@ -72,12 +73,33 @@ 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)
}

/**
* Instead of using [setup], this function allows you to create your own [SurfaceCallback] and
* forward the calls to the [CarMapSurfaceOwner]. This makes it possible for you to adopt new
* 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 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 set up the MapboxCarMap MapInitOptions with a CarContext"
}
carMapSurfaceOwner.setup(carContext, mapInitOptions)
return carMapSurfaceOwner
}

/**
* Returns the current [MapboxCarMapSurface]. It is recommended to use [registerObserver] and
* [MapboxCarMapObserver] to attach and detach your customizations.
Expand Down

0 comments on commit 7ea29d8

Please sign in to comment.