MediaContainerView: Log invalid visibility change

MediaContainerView's visibility is changed to VISIBLE when it should be gone. This CL logs this invalid state to find the culprit candidate.

Bug: 298213983
Test: presubmit
Flag: NONE
Change-Id: Id6d59546ffe0d028e2a4332d9983ef3b0660aa90
diff --git a/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/KeyguardMediaController.kt b/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/KeyguardMediaController.kt
index 9206af2..ba7d410 100644
--- a/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/KeyguardMediaController.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/KeyguardMediaController.kt
@@ -300,10 +300,17 @@
     private fun setVisibility(view: ViewGroup?, newVisibility: Int) {
         val currentMediaContainer = view ?: return
 
-        val previousVisibility = currentMediaContainer.visibility
-        currentMediaContainer.visibility = newVisibility
-        if (previousVisibility != newVisibility && currentMediaContainer is MediaContainerView) {
-            visibilityChangedListener?.invoke(newVisibility == View.VISIBLE)
+        val isVisible = newVisibility == View.VISIBLE
+
+        if (currentMediaContainer is MediaContainerView) {
+            val previousVisibility = currentMediaContainer.visibility
+
+            currentMediaContainer.setKeyguardVisibility(isVisible)
+            if (previousVisibility != newVisibility) {
+                visibilityChangedListener?.invoke(isVisible)
+            }
+        } else {
+            currentMediaContainer.visibility = newVisibility
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/MediaContainerView.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/MediaContainerView.kt
index bae5baa..d0c6a46 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/MediaContainerView.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/MediaContainerView.kt
@@ -22,6 +22,7 @@
 import android.graphics.Path
 import android.graphics.RectF
 import android.util.AttributeSet
+import android.util.Log
 import com.android.systemui.res.R
 import com.android.systemui.statusbar.notification.row.ExpandableView
 
@@ -87,4 +88,47 @@
     ) {
         // No animation, it doesn't need it, this would be local
     }
+
+    override fun setVisibility(visibility: Int) {
+        super.setVisibility(visibility)
+        assertMediaContainerVisibility(visibility)
+    }
+
+    /**
+     * b/298213983
+     * MediaContainerView's visibility is changed to VISIBLE when it should be GONE.
+     * This method check this state and logs.
+     */
+    private fun assertMediaContainerVisibility(visibility: Int) {
+        val currentViewState = viewState
+
+        if (currentViewState is MediaContainerViewState) {
+            if (!currentViewState.shouldBeVisible && visibility == VISIBLE) {
+                Log.wtf("MediaContainerView", "MediaContainerView should be GONE " +
+                        "but its visibility changed to VISIBLE")
+            }
+        }
+    }
+
+    fun setKeyguardVisibility(isVisible: Boolean) {
+        val currentViewState = viewState
+        if (currentViewState is MediaContainerViewState) {
+            currentViewState.shouldBeVisible = isVisible
+        }
+
+        visibility = if (isVisible) VISIBLE else GONE
+    }
+
+    override fun createExpandableViewState(): ExpandableViewState = MediaContainerViewState()
+
+    class MediaContainerViewState : ExpandableViewState() {
+        var shouldBeVisible: Boolean = false
+
+        override fun copyFrom(viewState: ViewState) {
+            super.copyFrom(viewState)
+            if (viewState is MediaContainerViewState) {
+                shouldBeVisible = viewState.shouldBeVisible
+            }
+        }
+    }
 }