Merge "Smartspace - Listen for SHOW_NOTIFICATIONS setting" into tm-dev

This commit is contained in:
Matt Pietal
2022-04-29 15:45:21 +00:00
committed by Android (Google) Code Review
2 changed files with 66 additions and 8 deletions

View File

@@ -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<SmartspaceView>()
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()

View File

@@ -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