Merge "Separate managed/primary profile sensitive notifs" into rvc-dev am: d3162cf2ba

Change-Id: Ifd341b028654e9cb9912f8bcfc5dea0f5f0c2ed3
This commit is contained in:
Beverly Tai
2020-04-27 15:39:34 +00:00
committed by Automerger Merge Worker
2 changed files with 141 additions and 14 deletions

View File

@@ -184,7 +184,7 @@ public class NotificationLockscreenUserManagerImpl implements
protected final Context mContext; protected final Context mContext;
private final Handler mMainHandler; private final Handler mMainHandler;
protected final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<>(); protected final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<>();
protected final ArrayList<UserInfo> mCurrentManagedProfiles = new ArrayList<>(); protected final SparseArray<UserInfo> mCurrentManagedProfiles = new SparseArray<>();
protected int mCurrentUserId = 0; protected int mCurrentUserId = 0;
protected NotificationPresenter mPresenter; protected NotificationPresenter mPresenter;
@@ -430,8 +430,9 @@ public class NotificationLockscreenUserManagerImpl implements
*/ */
public boolean allowsManagedPrivateNotificationsInPublic() { public boolean allowsManagedPrivateNotificationsInPublic() {
synchronized (mLock) { synchronized (mLock) {
for (UserInfo profile : mCurrentManagedProfiles) { for (int i = mCurrentManagedProfiles.size() - 1; i >= 0; i--) {
if (!userAllowsPrivateNotificationsInPublic(profile.id)) { if (!userAllowsPrivateNotificationsInPublic(
mCurrentManagedProfiles.valueAt(i).id)) {
return false; return false;
} }
} }
@@ -495,15 +496,22 @@ public class NotificationLockscreenUserManagerImpl implements
public boolean needsRedaction(NotificationEntry ent) { public boolean needsRedaction(NotificationEntry ent) {
int userId = ent.getSbn().getUserId(); int userId = ent.getSbn().getUserId();
boolean currentUserWantsRedaction = !userAllowsPrivateNotificationsInPublic(mCurrentUserId); boolean isCurrentUserRedactingNotifs =
boolean notiUserWantsRedaction = !userAllowsPrivateNotificationsInPublic(userId); !userAllowsPrivateNotificationsInPublic(mCurrentUserId);
boolean redactedLockscreen = currentUserWantsRedaction || notiUserWantsRedaction; boolean isNotifForManagedProfile = mCurrentManagedProfiles.contains(userId);
boolean isNotifUserRedacted = !userAllowsPrivateNotificationsInPublic(userId);
// redact notifications if the current user is redacting notifications; however if the
// notification is associated with a managed profile, we rely on the managed profile
// setting to determine whether to redact it
boolean isNotifRedacted = (!isNotifForManagedProfile && isCurrentUserRedactingNotifs)
|| isNotifUserRedacted;
boolean notificationRequestsRedaction = boolean notificationRequestsRedaction =
ent.getSbn().getNotification().visibility == Notification.VISIBILITY_PRIVATE; ent.getSbn().getNotification().visibility == Notification.VISIBILITY_PRIVATE;
boolean userForcesRedaction = packageHasVisibilityOverride(ent.getSbn().getKey()); boolean userForcesRedaction = packageHasVisibilityOverride(ent.getSbn().getKey());
return userForcesRedaction || notificationRequestsRedaction && redactedLockscreen; return userForcesRedaction || notificationRequestsRedaction && isNotifRedacted;
} }
private boolean packageHasVisibilityOverride(String key) { private boolean packageHasVisibilityOverride(String key) {
@@ -524,7 +532,7 @@ public class NotificationLockscreenUserManagerImpl implements
for (UserInfo user : mUserManager.getProfiles(mCurrentUserId)) { for (UserInfo user : mUserManager.getProfiles(mCurrentUserId)) {
mCurrentProfiles.put(user.id, user); mCurrentProfiles.put(user.id, user);
if (UserManager.USER_TYPE_PROFILE_MANAGED.equals(user.userType)) { if (UserManager.USER_TYPE_PROFILE_MANAGED.equals(user.userType)) {
mCurrentManagedProfiles.add(user); mCurrentManagedProfiles.put(user.id, user);
} }
} }
} }
@@ -556,7 +564,7 @@ public class NotificationLockscreenUserManagerImpl implements
public boolean isAnyManagedProfilePublicMode() { public boolean isAnyManagedProfilePublicMode() {
synchronized (mLock) { synchronized (mLock) {
for (int i = mCurrentManagedProfiles.size() - 1; i >= 0; i--) { for (int i = mCurrentManagedProfiles.size() - 1; i >= 0; i--) {
if (isLockscreenPublicMode(mCurrentManagedProfiles.get(i).id)) { if (isLockscreenPublicMode(mCurrentManagedProfiles.valueAt(i).id)) {
return true; return true;
} }
} }
@@ -673,8 +681,8 @@ public class NotificationLockscreenUserManagerImpl implements
} }
pw.print(" mCurrentManagedProfiles="); pw.print(" mCurrentManagedProfiles=");
synchronized (mLock) { synchronized (mLock) {
for (UserInfo userInfo : mCurrentManagedProfiles) { for (int i = mCurrentManagedProfiles.size() - 1; i >= 0; i--) {
pw.print("" + userInfo.id + " "); pw.print("" + mCurrentManagedProfiles.valueAt(i).id + " ");
} }
} }
pw.println(); pw.println();

View File

@@ -44,6 +44,7 @@ import android.content.pm.UserInfo;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.provider.Settings; import android.provider.Settings;
import android.testing.AndroidTestingRunner; import android.testing.AndroidTestingRunner;
@@ -101,6 +102,9 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
private UserInfo mSecondaryUser; private UserInfo mSecondaryUser;
private UserInfo mWorkUser; private UserInfo mWorkUser;
private TestNotificationLockscreenUserManager mLockscreenUserManager; private TestNotificationLockscreenUserManager mLockscreenUserManager;
private NotificationEntry mCurrentUserNotif;
private NotificationEntry mSecondaryUserNotif;
private NotificationEntry mWorkProfileNotif;
@Before @Before
public void setUp() { public void setUp() {
@@ -118,6 +122,21 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
mDependency.injectTestDependency(Dependency.MAIN_HANDLER, mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
Handler.createAsync(Looper.myLooper())); Handler.createAsync(Looper.myLooper()));
Notification notifWithPrivateVisibility = new Notification();
notifWithPrivateVisibility.visibility = Notification.VISIBILITY_PRIVATE;
mCurrentUserNotif = new NotificationEntryBuilder()
.setNotification(notifWithPrivateVisibility)
.setUser(new UserHandle(mCurrentUser.id))
.build();
mSecondaryUserNotif = new NotificationEntryBuilder()
.setNotification(notifWithPrivateVisibility)
.setUser(new UserHandle(mSecondaryUser.id))
.build();
mWorkProfileNotif = new NotificationEntryBuilder()
.setNotification(notifWithPrivateVisibility)
.setUser(new UserHandle(mWorkUser.id))
.build();
mLockscreenUserManager = new TestNotificationLockscreenUserManager(mContext); mLockscreenUserManager = new TestNotificationLockscreenUserManager(mContext);
mLockscreenUserManager.setUpWithPresenter(mPresenter); mLockscreenUserManager.setUpWithPresenter(mPresenter);
} }
@@ -157,7 +176,7 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
Settings.Secure.putIntForUser(mContext.getContentResolver(), Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mCurrentUser.id); Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mCurrentUser.id);
mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
assertFalse(mLockscreenUserManager.userAllowsNotificationsInPublic(mCurrentUser.id)); assertFalse(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mCurrentUser.id));
} }
@Test @Test
@@ -165,7 +184,7 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
Settings.Secure.putIntForUser(mContext.getContentResolver(), Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mWorkUser.id); Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mWorkUser.id);
mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
assertFalse(mLockscreenUserManager.allowsManagedPrivateNotificationsInPublic()); assertFalse(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mWorkUser.id));
} }
@Test @Test
@@ -173,7 +192,107 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
Settings.Secure.putIntForUser(mContext.getContentResolver(), Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mWorkUser.id); Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mWorkUser.id);
mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false); mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
assertTrue(mLockscreenUserManager.allowsManagedPrivateNotificationsInPublic()); assertTrue(mLockscreenUserManager.userAllowsPrivateNotificationsInPublic(mWorkUser.id));
}
@Test
public void testCurrentUserPrivateNotificationsNotRedacted() {
// GIVEN current user doesn't allow private notifications to show
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mCurrentUser.id);
mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
// THEN current user's notification is redacted
assertTrue(mLockscreenUserManager.needsRedaction(mCurrentUserNotif));
}
@Test
public void testCurrentUserPrivateNotificationsRedacted() {
// GIVEN current user allows private notifications to show
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mCurrentUser.id);
mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
// THEN current user's notification isn't redacted
assertFalse(mLockscreenUserManager.needsRedaction(mCurrentUserNotif));
}
@Test
public void testWorkPrivateNotificationsRedacted() {
// GIVEN work profile doesn't private notifications to show
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mWorkUser.id);
mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
// THEN work profile notification is redacted
assertTrue(mLockscreenUserManager.needsRedaction(mWorkProfileNotif));
}
@Test
public void testWorkPrivateNotificationsNotRedacted() {
// GIVEN work profile allows private notifications to show
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mWorkUser.id);
mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
// THEN work profile notification isn't redacted
assertFalse(mLockscreenUserManager.needsRedaction(mWorkProfileNotif));
}
@Test
public void testWorkPrivateNotificationsNotRedacted_otherUsersRedacted() {
// GIVEN work profile allows private notifications to show but the other users don't
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mWorkUser.id);
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mCurrentUser.id);
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0,
mSecondaryUser.id);
mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
// THEN the work profile notification doesn't need to be redacted
assertFalse(mLockscreenUserManager.needsRedaction(mWorkProfileNotif));
// THEN the current user and secondary user notifications do need to be redacted
assertTrue(mLockscreenUserManager.needsRedaction(mCurrentUserNotif));
assertTrue(mLockscreenUserManager.needsRedaction(mSecondaryUserNotif));
}
@Test
public void testWorkProfileRedacted_otherUsersNotRedacted() {
// GIVEN work profile doesn't allow private notifications to show but the other users do
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mWorkUser.id);
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mCurrentUser.id);
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1,
mSecondaryUser.id);
mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
// THEN the work profile notification needs to be redacted
assertTrue(mLockscreenUserManager.needsRedaction(mWorkProfileNotif));
// THEN the current user and secondary user notifications don't need to be redacted
assertFalse(mLockscreenUserManager.needsRedaction(mCurrentUserNotif));
assertFalse(mLockscreenUserManager.needsRedaction(mSecondaryUserNotif));
}
@Test
public void testSecondaryUserNotRedacted_currentUserRedacted() {
// GIVEN secondary profile allows private notifications to show but the current user
// doesn't allow private notifications to show
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0, mCurrentUser.id);
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1,
mSecondaryUser.id);
mLockscreenUserManager.getLockscreenSettingsObserverForTest().onChange(false);
// THEN the secondary profile notification still needs to be redacted because the current
// user's setting takes precedence
assertTrue(mLockscreenUserManager.needsRedaction(mSecondaryUserNotif));
} }
@Test @Test