From 3567a3aef144e7903dab4da4a2471bcd9e1c1111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20V=C3=A4limaa?= Date: Fri, 22 Nov 2024 15:55:16 +0800 Subject: [PATCH] fix: add navigation listener removal to android auto base screen --- .../flutter/navigation/AndroidAutoBaseScreen.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/android/src/main/kotlin/com/google/maps/flutter/navigation/AndroidAutoBaseScreen.kt b/android/src/main/kotlin/com/google/maps/flutter/navigation/AndroidAutoBaseScreen.kt index 8f49f45..575dd42 100644 --- a/android/src/main/kotlin/com/google/maps/flutter/navigation/AndroidAutoBaseScreen.kt +++ b/android/src/main/kotlin/com/google/maps/flutter/navigation/AndroidAutoBaseScreen.kt @@ -29,11 +29,15 @@ import androidx.car.app.model.Action import androidx.car.app.model.ActionStrip import androidx.car.app.model.Template import androidx.car.app.navigation.model.NavigationTemplate +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleObserver +import androidx.lifecycle.LifecycleOwner import com.google.android.gms.maps.CameraUpdateFactory import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.GoogleMapOptions import com.google.android.libraries.navigation.NavigationViewForAuto + open class AndroidAutoBaseScreen(carContext: CarContext) : Screen(carContext), SurfaceCallback, NavigationReadyListener { private val VIRTUAL_DISPLAY_NAME = "AndroidAutoNavScreen" private var mVirtualDisplay: VirtualDisplay? = null @@ -46,6 +50,10 @@ open class AndroidAutoBaseScreen(carContext: CarContext) : Screen(carContext), S init { initializeSurfaceCallback() + initializeNavigationListener() + } + + private fun initializeNavigationListener() { GoogleMapsNavigationSessionManager.navigationReadyListener = this mIsNavigationReady = GoogleMapsNavigationSessionManager.getInstance().isInitialized() } @@ -54,6 +62,13 @@ open class AndroidAutoBaseScreen(carContext: CarContext) : Screen(carContext), S carContext.getCarService(AppManager::class.java).setSurfaceCallback(this) } + private val mLifeCycleObserver: LifecycleObserver = object : DefaultLifecycleObserver { + override fun onDestroy(owner: LifecycleOwner) { + GoogleMapsNavigationSessionManager.navigationReadyListener = null + mIsNavigationReady = false + } + } + private fun isSurfaceReady(surfaceContainer: SurfaceContainer): Boolean { return surfaceContainer.surface != null && surfaceContainer.dpi != 0 && @@ -63,6 +78,7 @@ open class AndroidAutoBaseScreen(carContext: CarContext) : Screen(carContext), S override fun onSurfaceAvailable(surfaceContainer: SurfaceContainer) { super.onSurfaceAvailable(surfaceContainer) + lifecycle.addObserver(mLifeCycleObserver) if (!isSurfaceReady(surfaceContainer)) { return }