Skip to content

Commit

Permalink
fix: add navigation listener removal to android auto base screen
Browse files Browse the repository at this point in the history
  • Loading branch information
illuminati1911 authored and jokerttu committed Nov 28, 2024
1 parent 9ad55b0 commit 3567a3a
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
Expand All @@ -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 &&
Expand All @@ -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
}
Expand Down

0 comments on commit 3567a3a

Please sign in to comment.