From 4195d87b4817414e0a38f5dceee837c7cc8800ed Mon Sep 17 00:00:00 2001 From: cecilia Date: Tue, 27 Apr 2021 16:38:07 -0400 Subject: [PATCH] Init MediaHost with KeyguardMediaController's initialization, not wait until the controller is attached. This fixes the bug where media control doesn't show on lock screen. This is because the instance of MediaHost being added as MediaDataManager.Listener is not the same instance the KeyguardMediaController holds. Init media host during the controller's initialization instead of attach fixes this issue. Fixes: 185510540 Test: Studio builds Change-Id: I821b97efe0558c09dcab4497e5043c744d82a9e1 --- .../systemui/media/KeyguardMediaController.kt | 18 ++++++++++++------ .../com/android/systemui/media/MediaHost.kt | 7 +++++++ 2 files changed, 19 insertions(+), 6 deletions(-) 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 {