From 1f3d369e34d9a937438100ba734ced92e20d1aa6 Mon Sep 17 00:00:00 2001 From: Matt Pietal Date: Thu, 28 Apr 2022 11:18:53 -0400 Subject: [PATCH] Smartspace - Listen for SHOW_NOTIFICATIONS setting If enabled, do not show any smartspace content besides weather, for both the current user and the managed profile. This should match existing notification behavior on the lockscreen. Fixes: 220881135 Test: atest LockscreenSmartspaceControllerTest Change-Id: I8c542a3a3c5d53ee39a2d1565651e3cd21b0744e --- .../LockscreenSmartspaceController.kt | 35 +++++++++++++---- .../LockscreenSmartspaceControllerTest.kt | 39 ++++++++++++++++++- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/lockscreen/LockscreenSmartspaceController.kt b/packages/SystemUI/src/com/android/systemui/statusbar/lockscreen/LockscreenSmartspaceController.kt index a4e2d5ec08294..a35bced819c02 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/lockscreen/LockscreenSmartspaceController.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/lockscreen/LockscreenSmartspaceController.kt @@ -28,7 +28,8 @@ import android.database.ContentObserver import android.net.Uri import android.os.Handler import android.os.UserHandle -import android.provider.Settings +import android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS +import android.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS import android.util.Log import android.view.View import android.view.ViewGroup @@ -85,6 +86,7 @@ class LockscreenSmartspaceController @Inject constructor( // Smartspace can be used on multiple displays, such as when the user casts their screen private var smartspaceViews = mutableSetOf() + private var showNotifications = false private var showSensitiveContentForCurrentUser = false private var showSensitiveContentForManagedUser = false private var managedUserHandle: UserHandle? = null @@ -233,7 +235,13 @@ class LockscreenSmartspaceController @Inject constructor( deviceProvisionedController.removeCallback(deviceProvisionedListener) userTracker.addCallback(userTrackerCallback, uiExecutor) contentResolver.registerContentObserver( - secureSettings.getUriFor(Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS), + secureSettings.getUriFor(LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS), + true, + settingsObserver, + UserHandle.USER_ALL + ) + contentResolver.registerContentObserver( + secureSettings.getUriFor(LOCK_SCREEN_SHOW_NOTIFICATIONS), true, settingsObserver, UserHandle.USER_ALL @@ -286,6 +294,9 @@ class LockscreenSmartspaceController @Inject constructor( } private fun filterSmartspaceTarget(t: SmartspaceTarget): Boolean { + if (!showNotifications) { + return t.getFeatureType() == SmartspaceTarget.FEATURE_WEATHER + } return when (t.userHandle) { userTracker.userHandle -> { !t.isSensitive || showSensitiveContentForCurrentUser @@ -310,16 +321,26 @@ class LockscreenSmartspaceController @Inject constructor( } private fun reloadSmartspace() { - val setting = Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS + showNotifications = secureSettings.getIntForUser( + LOCK_SCREEN_SHOW_NOTIFICATIONS, + 0, + userTracker.userId + ) == 1 - showSensitiveContentForCurrentUser = - secureSettings.getIntForUser(setting, 0, userTracker.userId) == 1 + showSensitiveContentForCurrentUser = secureSettings.getIntForUser( + LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, + 0, + userTracker.userId + ) == 1 managedUserHandle = getWorkProfileUser() val managedId = managedUserHandle?.identifier if (managedId != null) { - showSensitiveContentForManagedUser = - secureSettings.getIntForUser(setting, 0, managedId) == 1 + showSensitiveContentForManagedUser = secureSettings.getIntForUser( + LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, + 0, + managedId + ) == 1 } session?.requestSmartspaceUpdate() diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/lockscreen/LockscreenSmartspaceControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/lockscreen/LockscreenSmartspaceControllerTest.kt index ce58a6c82142b..64aa7fb57da87 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/lockscreen/LockscreenSmartspaceControllerTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/lockscreen/LockscreenSmartspaceControllerTest.kt @@ -128,6 +128,7 @@ class LockscreenSmartspaceControllerTest : SysuiTestCase() { private val execution = FakeExecution() private val fakeParent = FrameLayout(context) private val fakePrivateLockscreenSettingUri = Uri.Builder().appendPath("test").build() + private val fakeNotifOnLockscreenSettingUri = Uri.Builder().appendPath("notif").build() private val userHandlePrimary: UserHandle = UserHandle(0) private val userHandleManaged: UserHandle = UserHandle(2) @@ -149,6 +150,8 @@ class LockscreenSmartspaceControllerTest : SysuiTestCase() { `when`(secureSettings.getUriFor(PRIVATE_LOCKSCREEN_SETTING)) .thenReturn(fakePrivateLockscreenSettingUri) + `when`(secureSettings.getUriFor(NOTIF_ON_LOCKSCREEN_SETTING)) + .thenReturn(fakeNotifOnLockscreenSettingUri) `when`(smartspaceManager.createSmartspaceSession(any())).thenReturn(smartspaceSession) `when`(plugin.getView(any())).thenReturn(createSmartspaceView(), createSmartspaceView()) `when`(userTracker.userProfiles).thenReturn(userList) @@ -160,6 +163,7 @@ class LockscreenSmartspaceControllerTest : SysuiTestCase() { setAllowPrivateNotifications(userHandlePrimary, true) setAllowPrivateNotifications(userHandleManaged, true) setAllowPrivateNotifications(userHandleSecondary, true) + setShowNotifications(userHandlePrimary, true) controller = LockscreenSmartspaceController( context, @@ -340,6 +344,26 @@ class LockscreenSmartspaceControllerTest : SysuiTestCase() { verify(plugin).onTargetsAvailable(eq(targets)) } + @Test + fun testAllTargetsAreFilteredExceptWeatherWhenNotificationsAreDisabled() { + // GIVEN the active user doesn't allow any notifications on lockscreen + setShowNotifications(userHandlePrimary, false) + connectSession() + + // WHEN we receive a list of targets + val targets = listOf( + makeTarget(1, userHandlePrimary, isSensitive = true), + makeTarget(2, userHandlePrimary), + makeTarget(3, userHandleManaged), + makeTarget(4, userHandlePrimary, featureType = SmartspaceTarget.FEATURE_WEATHER) + ) + + sessionListener.onTargetsAvailable(targets) + + // THEN all non-sensitive content is still shown + verify(plugin).onTargetsAvailable(eq(listOf(targets[3]))) + } + @Test fun testSensitiveTargetsAreFilteredOutForAppropriateUsers() { // GIVEN the active and managed users don't allow sensitive lockscreen content @@ -391,6 +415,7 @@ class LockscreenSmartspaceControllerTest : SysuiTestCase() { fun testRecognizeSwitchToSecondaryUser() { // GIVEN an inactive secondary user that doesn't allow sensitive content setAllowPrivateNotifications(userHandleSecondary, false) + setShowNotifications(userHandleSecondary, true) connectSession() // WHEN the secondary user becomes the active user @@ -518,13 +543,15 @@ class LockscreenSmartspaceControllerTest : SysuiTestCase() { fun makeTarget( id: Int, userHandle: UserHandle, - isSensitive: Boolean = false + isSensitive: Boolean = false, + featureType: Int = 0 ): SmartspaceTarget { return SmartspaceTarget.Builder( "target$id", ComponentName("testpackage", "testclass$id"), userHandle) .setSensitive(isSensitive) + .setFeatureType(featureType) .build() } @@ -536,6 +563,14 @@ class LockscreenSmartspaceControllerTest : SysuiTestCase() { ).thenReturn(if (value) 1 else 0) } + private fun setShowNotifications(user: UserHandle, value: Boolean) { + `when`(secureSettings.getIntForUser( + eq(NOTIF_ON_LOCKSCREEN_SETTING), + anyInt(), + eq(user.identifier)) + ).thenReturn(if (value) 1 else 0) + } + private fun createSmartspaceView(): SmartspaceView { return spy(object : View(context), SmartspaceView { override fun registerDataProvider(plugin: BcSmartspaceDataPlugin?) { @@ -574,3 +609,5 @@ class LockscreenSmartspaceControllerTest : SysuiTestCase() { private const val PRIVATE_LOCKSCREEN_SETTING = Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS +private const val NOTIF_ON_LOCKSCREEN_SETTING = + Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS