Update media_controls_lock_screen setting behavior

When the setting is disabled, hide the media carousel everywhere when
the device is on lockscreen, not just in the keyguard layout

Bug: 314333719
Test: manual
Test: atest MediaCarouselControllerTest
Flag: NONE

(cherry picked from commit f837163596ca6fef691e1931223209002cf3d9d0)
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:79af27be82294c31a161b61c1f853f9e59fa25a7)
Merged-In: I4f618e4013db894291e6fca9d49bceb1cb7e4bd9
Change-Id: I4f618e4013db894291e6fca9d49bceb1cb7e4bd9
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 ba7d410..acb48a9 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
@@ -18,18 +18,12 @@
 
 import android.content.Context
 import android.content.res.Configuration
-import android.database.ContentObserver
-import android.net.Uri
-import android.os.Handler
-import android.os.UserHandle
-import android.provider.Settings
 import android.view.View
 import android.view.ViewGroup
 import androidx.annotation.VisibleForTesting
 import com.android.systemui.Dumpable
 import com.android.systemui.Flags.migrateClocksToBlueprint
 import com.android.systemui.dagger.SysUISingleton
-import com.android.systemui.dagger.qualifiers.Main
 import com.android.systemui.dump.DumpManager
 import com.android.systemui.media.controls.ui.view.MediaHost
 import com.android.systemui.media.controls.ui.view.MediaHostState
@@ -43,7 +37,6 @@
 import com.android.systemui.statusbar.policy.SplitShadeStateController
 import com.android.systemui.util.asIndenting
 import com.android.systemui.util.println
-import com.android.systemui.util.settings.SecureSettings
 import com.android.systemui.util.withIncreasedIndent
 import java.io.PrintWriter
 import javax.inject.Inject
@@ -61,8 +54,6 @@
     private val bypassController: KeyguardBypassController,
     private val statusBarStateController: SysuiStatusBarStateController,
     private val context: Context,
-    private val secureSettings: SecureSettings,
-    @Main private val handler: Handler,
     configurationController: ConfigurationController,
     private val splitShadeStateController: SplitShadeStateController,
     private val logger: KeyguardMediaControllerLogger,
@@ -91,26 +82,6 @@
             }
         )
 
-        val settingsObserver: ContentObserver =
-            object : ContentObserver(handler) {
-                override fun onChange(selfChange: Boolean, uri: Uri?) {
-                    if (uri == lockScreenMediaPlayerUri) {
-                        allowMediaPlayerOnLockScreen =
-                            secureSettings.getBoolForUser(
-                                Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN,
-                                true,
-                                UserHandle.USER_CURRENT
-                            )
-                        refreshMediaPosition(reason = "allowMediaPlayerOnLockScreen changed")
-                    }
-                }
-            }
-        secureSettings.registerContentObserverForUser(
-            Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN,
-            settingsObserver,
-            UserHandle.USER_ALL
-        )
-
         // First let's set the desired state that we want for this host
         mediaHost.expansion = MediaHostState.EXPANDED
         mediaHost.showsOnlyActiveMedia = true
@@ -156,16 +127,6 @@
         private set
     private var splitShadeContainer: ViewGroup? = null
 
-    /** Track the media player setting status on lock screen. */
-    private var allowMediaPlayerOnLockScreen: Boolean =
-        secureSettings.getBoolForUser(
-            Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN,
-            true,
-            UserHandle.USER_CURRENT
-        )
-    private val lockScreenMediaPlayerUri =
-        secureSettings.getUriFor(Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN)
-
     /**
      * Attaches media container in single pane mode, situated at the top of the notifications list
      */
@@ -229,14 +190,12 @@
         // mediaHost.visible required for proper animations handling
         val isMediaHostVisible = mediaHost.visible
         val isBypassNotEnabled = !bypassController.bypassEnabled
-        val currentAllowMediaPlayerOnLockScreen = allowMediaPlayerOnLockScreen
         val useSplitShade = useSplitShade
         val shouldBeVisibleForSplitShade = shouldBeVisibleForSplitShade()
         visible =
             isMediaHostVisible &&
                 isBypassNotEnabled &&
                 keyguardOrUserSwitcher &&
-                currentAllowMediaPlayerOnLockScreen &&
                 shouldBeVisibleForSplitShade
         logger.logRefreshMediaPosition(
             reason = reason,
@@ -246,7 +205,6 @@
             keyguardOrUserSwitcher = keyguardOrUserSwitcher,
             mediaHostVisible = isMediaHostVisible,
             bypassNotEnabled = isBypassNotEnabled,
-            currentAllowMediaPlayerOnLockScreen = currentAllowMediaPlayerOnLockScreen,
             shouldBeVisibleForSplitShade = shouldBeVisibleForSplitShade,
         )
         val currActiveContainer = activeContainer
@@ -321,7 +279,6 @@
                 println("Self", this@KeyguardMediaController)
                 println("visible", visible)
                 println("useSplitShade", useSplitShade)
-                println("allowMediaPlayerOnLockScreen", allowMediaPlayerOnLockScreen)
                 println("bypassController.bypassEnabled", bypassController.bypassEnabled)
                 println("isDozeWakeUpAnimationWaiting", isDozeWakeUpAnimationWaiting)
                 println("singlePaneContainer", singlePaneContainer)
diff --git a/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/KeyguardMediaControllerLogger.kt b/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/KeyguardMediaControllerLogger.kt
index c0d9dc2..4d1827e 100644
--- a/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/KeyguardMediaControllerLogger.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/KeyguardMediaControllerLogger.kt
@@ -36,7 +36,6 @@
         keyguardOrUserSwitcher: Boolean,
         mediaHostVisible: Boolean,
         bypassNotEnabled: Boolean,
-        currentAllowMediaPlayerOnLockScreen: Boolean,
         shouldBeVisibleForSplitShade: Boolean,
     ) {
         logBuffer.log(
@@ -50,8 +49,7 @@
                 bool3 = keyguardOrUserSwitcher
                 bool4 = mediaHostVisible
                 int2 = if (bypassNotEnabled) 1 else 0
-                str2 = currentAllowMediaPlayerOnLockScreen.toString()
-                str3 = shouldBeVisibleForSplitShade.toString()
+                str2 = shouldBeVisibleForSplitShade.toString()
             },
             {
                 "refreshMediaPosition(reason=$str1, " +
@@ -60,8 +58,7 @@
                     "keyguardOrUserSwitcher=$bool3, " +
                     "mediaHostVisible=$bool4, " +
                     "bypassNotEnabled=${int2 == 1}, " +
-                    "currentAllowMediaPlayerOnLockScreen=$str2, " +
-                    "shouldBeVisibleForSplitShade=$str3)"
+                    "shouldBeVisibleForSplitShade=$str2)"
             }
         )
     }
diff --git a/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/MediaHierarchyManager.kt b/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/MediaHierarchyManager.kt
index dbd71f3..37a035c 100644
--- a/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/MediaHierarchyManager.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/controls/ui/controller/MediaHierarchyManager.kt
@@ -115,7 +115,7 @@
 ) {
 
     /** Track the media player setting status on lock screen. */
-    private var allowMediaPlayerOnLockScreen: Boolean = true
+    private var allowMediaPlayerOnLockScreen: Boolean = getMediaLockScreenSetting()
     private val lockScreenMediaPlayerUri =
         secureSettings.getUriFor(Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN)
 
@@ -485,6 +485,7 @@
                         mediaCarouselController.logSmartspaceImpression(qsExpanded)
                     }
                     updateUserVisibility()
+                    mediaCarouselController.updateHostVisibility()
                 }
 
                 override fun onDozeAmountChanged(linear: Float, eased: Float) {
@@ -569,12 +570,8 @@
             object : ContentObserver(handler) {
                 override fun onChange(selfChange: Boolean, uri: Uri?) {
                     if (uri == lockScreenMediaPlayerUri) {
-                        allowMediaPlayerOnLockScreen =
-                            secureSettings.getBoolForUser(
-                                Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN,
-                                true,
-                                UserHandle.USER_CURRENT
-                            )
+                        allowMediaPlayerOnLockScreen = getMediaLockScreenSetting()
+                        mediaCarouselController.updateHostVisibility()
                     }
                 }
             }
@@ -595,6 +592,14 @@
         }
     }
 
+    private fun getMediaLockScreenSetting(): Boolean {
+        return secureSettings.getBoolForUser(
+            Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN,
+            true,
+            UserHandle.USER_CURRENT
+        )
+    }
+
     private fun updateConfiguration() {
         distanceForFullShadeTransition =
             context.resources.getDimensionPixelSize(
@@ -634,6 +639,13 @@
         mediaCarouselController.closeGuts()
     }
 
+    /** Return true if the carousel should be hidden because lockscreen is currently visible */
+    fun isLockedAndHidden(): Boolean {
+        return !allowMediaPlayerOnLockScreen &&
+            (statusbarState == StatusBarState.SHADE_LOCKED ||
+                statusbarState == StatusBarState.KEYGUARD)
+    }
+
     private fun createUniqueObjectHost(): UniqueObjectHostView {
         val viewHost = UniqueObjectHostView(context)
         viewHost.addOnAttachStateChangeListener(
diff --git a/packages/SystemUI/src/com/android/systemui/media/controls/ui/view/MediaHost.kt b/packages/SystemUI/src/com/android/systemui/media/controls/ui/view/MediaHost.kt
index d92168b..04f9a9d 100644
--- a/packages/SystemUI/src/com/android/systemui/media/controls/ui/view/MediaHost.kt
+++ b/packages/SystemUI/src/com/android/systemui/media/controls/ui/view/MediaHost.kt
@@ -202,7 +202,9 @@
      */
     fun updateViewVisibility() {
         state.visible =
-            if (showsOnlyActiveMedia) {
+            if (mediaHierarchyManager.isLockedAndHidden()) {
+                false
+            } else if (showsOnlyActiveMedia) {
                 mediaDataManager.hasActiveMediaOrRecommendation()
             } else {
                 mediaDataManager.hasAnyMediaOrRecommendation()
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/controls/ui/controller/KeyguardMediaControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/controls/ui/controller/KeyguardMediaControllerTest.kt
index 9f5260c..37dea11 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/media/controls/ui/controller/KeyguardMediaControllerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/controls/ui/controller/KeyguardMediaControllerTest.kt
@@ -16,7 +16,6 @@
 
 package com.android.systemui.media.controls.ui.controller
 
-import android.provider.Settings
 import android.test.suitebuilder.annotation.SmallTest
 import android.testing.AndroidTestingRunner
 import android.testing.TestableLooper
@@ -37,8 +36,6 @@
 import com.android.systemui.util.animation.UniqueObjectHostView
 import com.android.systemui.util.mockito.mock
 import com.android.systemui.util.mockito.whenever
-import com.android.systemui.util.settings.FakeSettings
-import com.android.systemui.utils.os.FakeHandler
 import com.google.common.truth.Truth.assertThat
 import junit.framework.Assert.assertTrue
 import org.junit.Before
@@ -65,10 +62,7 @@
 
     private val mediaContainerView: MediaContainerView = MediaContainerView(context, null)
     private val hostView = UniqueObjectHostView(context)
-    private val settings = FakeSettings()
     private lateinit var keyguardMediaController: KeyguardMediaController
-    private lateinit var testableLooper: TestableLooper
-    private lateinit var fakeHandler: FakeHandler
     private lateinit var statusBarStateListener: StatusBarStateController.StateListener
 
     @Before
@@ -84,16 +78,12 @@
         whenever(statusBarStateController.state).thenReturn(StatusBarState.KEYGUARD)
         whenever(mediaHost.hostView).thenReturn(hostView)
         hostView.layoutParams = FrameLayout.LayoutParams(100, 100)
-        testableLooper = TestableLooper.get(this)
-        fakeHandler = FakeHandler(testableLooper.looper)
         keyguardMediaController =
             KeyguardMediaController(
                 mediaHost,
                 bypassController,
                 statusBarStateController,
                 context,
-                settings,
-                fakeHandler,
                 configurationController,
                 ResourcesSplitShadeStateController(),
                 mock<KeyguardMediaControllerLogger>(),
@@ -126,24 +116,6 @@
     }
 
     @Test
-    fun testHiddenOnKeyguard_whenMediaOnLockScreenDisabled() {
-        settings.putInt(Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, 0)
-
-        keyguardMediaController.refreshMediaPosition(TEST_REASON)
-
-        assertThat(mediaContainerView.visibility).isEqualTo(GONE)
-    }
-
-    @Test
-    fun testAvailableOnKeyguard_whenMediaOnLockScreenEnabled() {
-        settings.putInt(Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, 1)
-
-        keyguardMediaController.refreshMediaPosition(TEST_REASON)
-
-        assertThat(mediaContainerView.visibility).isEqualTo(VISIBLE)
-    }
-
-    @Test
     fun testActivatesSplitShadeContainerInSplitShadeMode() {
         val splitShadeContainer = FrameLayout(context)
         keyguardMediaController.attachSplitShadeContainer(splitShadeContainer)
diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/controls/ui/controller/MediaHierarchyManagerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/controls/ui/controller/MediaHierarchyManagerTest.kt
index 29820f7..e9b11c4 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/media/controls/ui/controller/MediaHierarchyManagerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/media/controls/ui/controller/MediaHierarchyManagerTest.kt
@@ -156,6 +156,7 @@
         verify(wakefulnessLifecycle).addObserver(wakefullnessObserver.capture())
         verify(statusBarStateController).addCallback(statusBarCallback.capture())
         verify(dreamOverlayStateController).addCallback(dreamOverlayCallback.capture())
+        whenever(mediaCarouselController.updateHostVisibility).thenReturn({})
         setupHost(lockHost, MediaHierarchyManager.LOCATION_LOCKSCREEN, LOCKSCREEN_TOP)
         setupHost(qsHost, MediaHierarchyManager.LOCATION_QS, QS_TOP)
         setupHost(qqsHost, MediaHierarchyManager.LOCATION_QQS, QQS_TOP)
@@ -613,6 +614,55 @@
         assertThat(mediaCarouselScrollHandler.visibleToUser).isFalse()
     }
 
+    @Test
+    fun keyguardState_allowedOnLockscreen_updateVisibility() {
+        settings.putBool(Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, true)
+        clearInvocations(mediaCarouselController)
+
+        statusBarCallback.value.onStatePreChange(StatusBarState.SHADE, StatusBarState.KEYGUARD)
+        statusBarCallback.value.onStateChanged(StatusBarState.KEYGUARD)
+
+        verify(mediaCarouselController).updateHostVisibility
+        assertThat(mediaHierarchyManager.isLockedAndHidden()).isFalse()
+    }
+
+    @Test
+    fun keyguardState_notAllowedOnLockscreen_updateVisibility() {
+        settings.putBool(Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, false)
+        clearInvocations(mediaCarouselController)
+
+        statusBarCallback.value.onStatePreChange(StatusBarState.SHADE, StatusBarState.KEYGUARD)
+        statusBarCallback.value.onStateChanged(StatusBarState.KEYGUARD)
+
+        verify(mediaCarouselController).updateHostVisibility
+        assertThat(mediaHierarchyManager.isLockedAndHidden()).isTrue()
+    }
+
+    @Test
+    fun keyguardGone_updateVisibility() {
+        settings.putBool(Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, false)
+        clearInvocations(mediaCarouselController)
+
+        statusBarCallback.value.onStatePreChange(StatusBarState.KEYGUARD, StatusBarState.SHADE)
+        statusBarCallback.value.onStateChanged(StatusBarState.SHADE)
+
+        verify(mediaCarouselController).updateHostVisibility
+        assertThat(mediaHierarchyManager.isLockedAndHidden()).isFalse()
+    }
+
+    @Test
+    fun lockscreenSettingChanged_updateVisibility() {
+        settings.putBool(Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, true)
+        statusBarCallback.value.onStatePreChange(StatusBarState.SHADE, StatusBarState.KEYGUARD)
+        statusBarCallback.value.onStateChanged(StatusBarState.KEYGUARD)
+        clearInvocations(mediaCarouselController)
+
+        settings.putBool(Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, false)
+
+        verify(mediaCarouselController).updateHostVisibility
+        assertThat(mediaHierarchyManager.isLockedAndHidden()).isTrue()
+    }
+
     private fun enableSplitShade() {
         context
             .getOrCreateTestableResources()