From 50ad972883d01ec46c638726fc8d83c27395c762 Mon Sep 17 00:00:00 2001 From: Kyle Madsen Date: Fri, 9 Sep 2022 09:32:52 -0700 Subject: [PATCH] Make SharedApp backwards compatible --- CHANGELOG.md | 1 + libnavui-androidauto/CHANGELOG.md | 1 + .../com/mapbox/androidauto/MapboxCarApp.kt | 28 ++++++------ .../navigation/ui/app/internal/SharedApp.kt | 45 ++++++++++--------- .../navigation/dropin/NavigationView.kt | 4 +- qa-test-app/build.gradle | 3 ++ .../qa_test_app/QaTestApplication.kt | 10 +---- .../qa_test_app/car/MainCarSession.kt | 8 ++++ 8 files changed, 54 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bf40a0f0b9..f32e460764a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Mapbox welcomes participation and contributions from everyone. #### Features #### Bug fixes and improvements - Optimized calls to modify route arrow layer visibility. [#6308](https://github.com/mapbox/mapbox-navigation-android/pull/6308) +- Make `SharedApp` backwards compatible and removable. [#6303](https://github.com/mapbox/mapbox-navigation-android/pull/6303) ## Mapbox Navigation SDK 2.8.0-beta.3 - 09 September, 2022 ### Changelog diff --git a/libnavui-androidauto/CHANGELOG.md b/libnavui-androidauto/CHANGELOG.md index 762dff71516..9b2ddeb8da3 100644 --- a/libnavui-androidauto/CHANGELOG.md +++ b/libnavui-androidauto/CHANGELOG.md @@ -5,6 +5,7 @@ Mapbox welcomes participation and contributions from everyone. ## Unreleased #### Features #### Bug fixes and improvements +- Removed `MapboxCarApp.setup`, replace with `MapboxNavigationApp.registerObserver(MapboxCarApp)` so it can be disabled. [#6275](https://github.com/mapbox/mapbox-navigation-android/pull/6275) ## androidauto-v0.10.0 - Sep 9, 2022 ### Changelog diff --git a/libnavui-androidauto/src/main/java/com/mapbox/androidauto/MapboxCarApp.kt b/libnavui-androidauto/src/main/java/com/mapbox/androidauto/MapboxCarApp.kt index 0b334086aef..88eeff67e43 100644 --- a/libnavui-androidauto/src/main/java/com/mapbox/androidauto/MapboxCarApp.kt +++ b/libnavui-androidauto/src/main/java/com/mapbox/androidauto/MapboxCarApp.kt @@ -1,10 +1,11 @@ package com.mapbox.androidauto -import android.app.Application import com.mapbox.androidauto.navigation.location.CarAppLocation import com.mapbox.androidauto.navigation.location.impl.CarAppLocationImpl import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI +import com.mapbox.navigation.core.MapboxNavigation import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp +import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver import com.mapbox.navigation.ui.app.internal.SharedApp import com.mapbox.navigation.ui.voice.internal.MapboxAudioGuidance import kotlinx.coroutines.flow.MutableStateFlow @@ -13,10 +14,10 @@ import kotlinx.coroutines.flow.StateFlow /** * The entry point for your Mapbox Android Auto app. */ -@OptIn(ExperimentalPreviewMapboxNavigationAPI::class) -object MapboxCarApp { - +object MapboxCarApp : MapboxNavigationObserver { private val carAppStateFlow = MutableStateFlow(FreeDriveState) + @OptIn(ExperimentalPreviewMapboxNavigationAPI::class) + private val carAppLocation: CarAppLocation = CarAppLocationImpl() /** * Attach observers to the CarAppState to determine which view to show. @@ -26,8 +27,7 @@ object MapboxCarApp { /** * Location service available to the car and app. */ - fun carAppLocationService(): CarAppLocation = - MapboxNavigationApp.getObserver(CarAppLocation::class) + fun carAppLocationService(): CarAppLocation = carAppLocation /** * Audio guidance service available to the car and app. @@ -42,13 +42,13 @@ object MapboxCarApp { carAppStateFlow.value = carAppState } - /** - * Setup android auto from your [Application.onCreate] - * - * @param application used to detect when activities are foregrounded - */ - fun setup(application: Application) { - SharedApp.setup(application) - MapboxNavigationApp.registerObserver(CarAppLocationImpl()) + override fun onAttached(mapboxNavigation: MapboxNavigation) { + // TODO add after 2.8.0-rc.1 +// MapboxNavigationApp.registerObserver(SharedApp) + MapboxNavigationApp.registerObserver(carAppLocation) + } + + override fun onDetached(mapboxNavigation: MapboxNavigation) { + MapboxNavigationApp.unregisterObserver(carAppLocation) } } diff --git a/libnavui-app/src/main/java/com/mapbox/navigation/ui/app/internal/SharedApp.kt b/libnavui-app/src/main/java/com/mapbox/navigation/ui/app/internal/SharedApp.kt index fd746397e96..b6feb3cb7fd 100644 --- a/libnavui-app/src/main/java/com/mapbox/navigation/ui/app/internal/SharedApp.kt +++ b/libnavui-app/src/main/java/com/mapbox/navigation/ui/app/internal/SharedApp.kt @@ -2,6 +2,7 @@ package com.mapbox.navigation.ui.app.internal import android.content.Context import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI +import com.mapbox.navigation.core.MapboxNavigation import com.mapbox.navigation.core.internal.extensions.attachCreated import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver @@ -23,9 +24,7 @@ import com.mapbox.navigation.ui.voice.internal.impl.MapboxAudioGuidanceImpl import java.util.concurrent.atomic.AtomicBoolean @OptIn(ExperimentalPreviewMapboxNavigationAPI::class) -object SharedApp { - private var isSetup = false - +object SharedApp : MapboxNavigationObserver { val store = Store() val state get() = store.state.value @@ -42,6 +41,10 @@ object SharedApp { val destinationStateController = DestinationStateController(store) val routeStateController = RouteStateController(store) val routePreviewStateController = RoutePreviewStateController(store) + private val stateResetController = StateResetController(store, ignoreTripSessionUpdates) + private val routeAlternativeComponent = RouteAlternativeComponent { + RouteAlternativeComponentImpl(store) + } private val navigationObservers: Array = arrayOf( routeStateController, cameraStateController, @@ -51,26 +54,14 @@ object SharedApp { routePreviewStateController, audioGuidanceStateController, tripSessionStarterStateController, + stateResetController, + routeAlternativeComponent, ) - @JvmOverloads - fun setup( - context: Context, - audioGuidance: MapboxAudioGuidance? = null, - routeAlternativeContract: RouteAlternativeContract? = null - ) { - if (isSetup) return - isSetup = true - - MapboxNavigationApp.registerObserver(StateResetController(store, ignoreTripSessionUpdates)) - MapboxNavigationApp.registerObserver( - RouteAlternativeComponent { - routeAlternativeContract ?: RouteAlternativeComponentImpl(store) - } - ) - MapboxNavigationApp.lifecycleOwner.attachCreated(*navigationObservers) - MapboxNavigationApp.registerObserver(audioGuidance ?: defaultAudioGuidance(context)) - } + /** + * Requires the [Context] so will be initialized when attached. + */ + private lateinit var mapboxAudioGuidance: MapboxAudioGuidance fun tripSessionTransaction(updateSession: () -> Unit) { // Any changes to MapboxNavigation TripSession should be done within `tripSessionTransaction { }` block. @@ -87,5 +78,17 @@ object SharedApp { } } + override fun onAttached(mapboxNavigation: MapboxNavigation) { + val context = mapboxNavigation.navigationOptions.applicationContext + mapboxAudioGuidance = defaultAudioGuidance(context) + MapboxNavigationApp.registerObserver(mapboxAudioGuidance) + MapboxNavigationApp.lifecycleOwner.attachCreated(*navigationObservers) + } + + override fun onDetached(mapboxNavigation: MapboxNavigation) { + navigationObservers.forEach { MapboxNavigationApp.unregisterObserver(it) } + MapboxNavigationApp.unregisterObserver(mapboxAudioGuidance) + } + private const val DEFAULT_DATA_STORE_NAME = "mapbox_navigation_preferences" } diff --git a/libnavui-dropin/src/main/java/com/mapbox/navigation/dropin/NavigationView.kt b/libnavui-dropin/src/main/java/com/mapbox/navigation/dropin/NavigationView.kt index e8cef2606d5..7c5ad4352f9 100644 --- a/libnavui-dropin/src/main/java/com/mapbox/navigation/dropin/NavigationView.kt +++ b/libnavui-dropin/src/main/java/com/mapbox/navigation/dropin/NavigationView.kt @@ -2,7 +2,6 @@ package com.mapbox.navigation.dropin import android.Manifest import android.app.Activity -import android.app.Application import android.content.Context import android.util.AttributeSet import android.view.LayoutInflater @@ -20,6 +19,7 @@ import com.mapbox.maps.MapView import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI import com.mapbox.navigation.base.options.NavigationOptions import com.mapbox.navigation.core.internal.extensions.attachCreated +import com.mapbox.navigation.core.internal.extensions.attachStarted import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp import com.mapbox.navigation.core.replay.MapboxReplayer import com.mapbox.navigation.dropin.component.analytics.AnalyticsComponent @@ -109,7 +109,7 @@ class NavigationView @JvmOverloads constructor( keepScreenOn = true captureSystemBarsInsets() - SharedApp.setup(context.applicationContext as Application) + MapboxNavigationApp.registerObserver(SharedApp) if (!MapboxNavigationApp.isSetup()) { MapboxNavigationApp.setup( NavigationOptions.Builder(context) diff --git a/qa-test-app/build.gradle b/qa-test-app/build.gradle index 3fc0c8999fc..46e52bddc2d 100644 --- a/qa-test-app/build.gradle +++ b/qa-test-app/build.gradle @@ -98,6 +98,9 @@ dependencies { implementation project(':libnavui-androidauto') implementation dependenciesList.mapboxSearchSdk + // TODO Remove after 2.8.0-rc.1 + implementation project(':libnavui-app') + // test androidTestImplementation project(':libtesting-ui') androidTestImplementation dependenciesList.testRunner diff --git a/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/QaTestApplication.kt b/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/QaTestApplication.kt index 58d2652edb7..fa5501d96ef 100644 --- a/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/QaTestApplication.kt +++ b/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/QaTestApplication.kt @@ -1,13 +1,5 @@ package com.mapbox.navigation.qa_test_app import android.app.Application -import com.mapbox.androidauto.MapboxCarApp -class QaTestApplication : Application() { - - override fun onCreate() { - super.onCreate() - - MapboxCarApp.setup(this) - } -} +class QaTestApplication : Application() diff --git a/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/car/MainCarSession.kt b/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/car/MainCarSession.kt index ee6970fa8c1..e519b58515b 100644 --- a/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/car/MainCarSession.kt +++ b/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/car/MainCarSession.kt @@ -13,6 +13,7 @@ import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import com.mapbox.android.core.permissions.PermissionsManager +import com.mapbox.androidauto.MapboxCarApp import com.mapbox.androidauto.MapboxCarNavigationManager import com.mapbox.androidauto.car.MainCarContext import com.mapbox.androidauto.car.MainScreenManager @@ -27,9 +28,11 @@ import com.mapbox.maps.MapboxExperimental import com.mapbox.maps.extension.androidauto.MapboxCarMap import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI import com.mapbox.navigation.base.options.NavigationOptions +import com.mapbox.navigation.core.internal.extensions.attachCreated import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp import com.mapbox.navigation.core.trip.session.TripSessionState import com.mapbox.navigation.qa_test_app.utils.Utils +import com.mapbox.navigation.ui.app.internal.SharedApp import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch @@ -47,6 +50,11 @@ class MainCarSession : Session() { init { logAndroidAuto("MainCarSession constructor") + + // TODO remove after 2.8.0-rc.1 + MapboxNavigationApp.registerObserver(SharedApp) + attachCreated(MapboxCarApp) + val logoSurfaceRenderer = CarLogoSurfaceRenderer() val compassSurfaceRenderer = CarCompassSurfaceRenderer() MapboxNavigationApp.attach(lifecycleOwner = this)