diff --git a/packages/SystemUI/src/com/android/systemui/media/KeyguardMediaController.kt b/packages/SystemUI/src/com/android/systemui/media/KeyguardMediaController.kt index 2ea139e4b4804..b668e881230da 100644 --- a/packages/SystemUI/src/com/android/systemui/media/KeyguardMediaController.kt +++ b/packages/SystemUI/src/com/android/systemui/media/KeyguardMediaController.kt @@ -54,6 +54,14 @@ class KeyguardMediaController @Inject constructor( } } }) + + // First let's set the desired state that we want for this host + mediaHost.expansion = MediaHostState.COLLAPSED + mediaHost.showsOnlyActiveMedia = true + mediaHost.falsingProtectionNeeded = true + + // Let's now initialize this view, which also creates the host view for us. + mediaHost.init(MediaHierarchyManager.LOCATION_LOCKSCREEN) } var visibilityChangedListener: ((Boolean) -> Unit)? = null @@ -71,13 +79,7 @@ class KeyguardMediaController @Inject constructor( */ fun attachSinglePaneContainer(mediaView: MediaHeaderView?) { singlePaneContainer = mediaView - // First let's set the desired state that we want for this host - mediaHost.expansion = MediaHostState.COLLAPSED - mediaHost.showsOnlyActiveMedia = true - mediaHost.falsingProtectionNeeded = true - // Let's now initialize this view, which also creates the host view for us. - mediaHost.init(MediaHierarchyManager.LOCATION_LOCKSCREEN) // Required to show it for the first time, afterwards visibility is managed automatically mediaHost.visible = true mediaHost.addVisibilityChangeListener { visible -> @@ -133,6 +135,10 @@ class KeyguardMediaController @Inject constructor( } // might be called a few times for the same view, no need to add hostView again if (activeContainer?.childCount == 0) { + // Detach the hostView from its parent view if exists + mediaHost.hostView.parent ?.let { + (it as? ViewGroup)?.removeView(mediaHost.hostView) + } activeContainer.addView(mediaHost.hostView) } setVisibility(activeContainer, View.VISIBLE) diff --git a/packages/SystemUI/src/com/android/systemui/media/MediaHost.kt b/packages/SystemUI/src/com/android/systemui/media/MediaHost.kt index 2347481d1c3de..28e46409e5c68 100644 --- a/packages/SystemUI/src/com/android/systemui/media/MediaHost.kt +++ b/packages/SystemUI/src/com/android/systemui/media/MediaHost.kt @@ -25,6 +25,8 @@ class MediaHost constructor( private val tmpLocationOnScreen: IntArray = intArrayOf(0, 0) + private var inited: Boolean = false + /** * Get the current bounds on the screen. This makes sure the state is fresh and up to date */ @@ -84,6 +86,11 @@ class MediaHost constructor( * transitions. */ fun init(@MediaLocation location: Int) { + if (inited) { + return + } + inited = true + this.location = location hostView = mediaHierarchyManager.register(this) hostView.addOnAttachStateChangeListener(object : OnAttachStateChangeListener {