diff options
5 files changed, 63 insertions, 18 deletions
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index 84fddcb2c22a..211bdef89247 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -12172,6 +12172,7 @@ public final class Settings { CLONE_TO_MANAGED_PROFILE.add(SHOW_IME_WITH_HARD_KEYBOARD); CLONE_TO_MANAGED_PROFILE.add(ACCESSIBILITY_BOUNCE_KEYS); CLONE_TO_MANAGED_PROFILE.add(NOTIFICATION_BUBBLES); + CLONE_TO_MANAGED_PROFILE.add(NOTIFICATION_HISTORY_ENABLED); } /** @hide */ diff --git a/services/core/java/com/android/server/notification/NotificationHistoryManager.java b/services/core/java/com/android/server/notification/NotificationHistoryManager.java index 6a46048e80e1..e3880c383632 100644 --- a/services/core/java/com/android/server/notification/NotificationHistoryManager.java +++ b/services/core/java/com/android/server/notification/NotificationHistoryManager.java @@ -118,6 +118,10 @@ public class NotificationHistoryManager { } } + public void onUserAdded(@UserIdInt int userId) { + mSettingsObserver.update(null, userId); + } + public void onUserStopped(@UserIdInt int userId) { synchronized (mLock) { mUserUnlockedStates.put(userId, false); @@ -401,9 +405,7 @@ public class NotificationHistoryManager { false, this, UserHandle.USER_ALL); synchronized (mLock) { for (UserInfo userInfo : mUserManager.getUsers()) { - if (!userInfo.isProfile()) { - update(null, userInfo.id); - } + update(null, userInfo.id); } } } @@ -424,10 +426,7 @@ public class NotificationHistoryManager { boolean historyEnabled = Settings.Secure.getIntForUser(resolver, Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, userId) != 0; - int[] profiles = mUserManager.getProfileIds(userId, true); - for (int profileId : profiles) { - onHistoryEnabledChanged(profileId, historyEnabled); - } + onHistoryEnabledChanged(userId, historyEnabled); } } } diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 7fbc0852c746..9ed3559c1389 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -2025,6 +2025,8 @@ public class NotificationManagerService extends SystemService { if (!mUserProfiles.isProfileUser(userId)) { allowDefaultApprovedServices(userId); } + mHistoryManager.onUserAdded(userId); + mSettingsObserver.update(null, userId); } } else if (action.equals(Intent.ACTION_USER_REMOVED)) { final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); @@ -2137,13 +2139,8 @@ public class NotificationManagerService extends SystemService { mPreferencesHelper.updateBubblesEnabled(); } if (uri == null || NOTIFICATION_HISTORY_ENABLED.equals(uri)) { - final IntArray userIds = mUserProfiles.getCurrentProfileIds(); - - for (int i = 0; i < userIds.size(); i++) { - mArchive.updateHistoryEnabled(userIds.get(i), - Settings.Secure.getIntForUser(resolver, - Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, - userIds.get(i)) == 1); + for (UserInfo userInfo : mUm.getUsers()) { + update(uri, userInfo.id); } } if (uri == null || NOTIFICATION_SHOW_MEDIA_ON_QUICK_SETTINGS_URI.equals(uri)) { @@ -2164,6 +2161,17 @@ public class NotificationManagerService extends SystemService { } } } + + public void update(Uri uri, int userId) { + ContentResolver resolver = getContext().getContentResolver(); + if (uri == null || NOTIFICATION_HISTORY_ENABLED.equals(uri)) { + mArchive.updateHistoryEnabled(userId, + Settings.Secure.getIntForUser(resolver, + Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, + userId) == 1); + // note: this setting is also handled in NotificationHistoryManager + } + } } private SettingsObserver mSettingsObserver; diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryManagerTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryManagerTest.java index 5892793fdb72..c10c3c28e9dd 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryManagerTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryManagerTest.java @@ -170,6 +170,10 @@ public class NotificationHistoryManagerTest extends UiServiceTestCase { Settings.Secure.putIntForUser(getContext().getContentResolver(), Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, USER_SYSTEM); mHistoryManager.mSettingsObserver.update(null, USER_SYSTEM); + // fake cloned settings to profile + Settings.Secure.putIntForUser(getContext().getContentResolver(), + Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, mProfileId); + mHistoryManager.mSettingsObserver.update(null, mProfileId); // unlock user, verify that history is disabled for self and profile mHistoryManager.onUserUnlocked(USER_SYSTEM); @@ -181,6 +185,37 @@ public class NotificationHistoryManagerTest extends UiServiceTestCase { } @Test + public void testAddProfile_historyEnabledInPrimary() { + // create a history + mHistoryManager.onUserUnlocked(MIN_SECONDARY_USER_ID); + assertThat(mHistoryManager.doesHistoryExistForUser(MIN_SECONDARY_USER_ID)).isTrue(); + + // fake Settings#CLONE_TO_MANAGED_PROFILE + int newProfileId = 99; + Settings.Secure.putIntForUser(getContext().getContentResolver(), + Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 1, newProfileId); + mUsers = new ArrayList<>(); + UserInfo userFullSecondary = new UserInfo(); + userFullSecondary.id = MIN_SECONDARY_USER_ID; + mUsers.add(userFullSecondary); + UserInfo userProfile = new UserInfo(); + userProfile.id = newProfileId; + mUsers.add(userProfile); + when(mUserManager.getUsers()).thenReturn(mUsers); + + mProfiles = new int[] {MIN_SECONDARY_USER_ID, userProfile.id}; + when(mUserManager.getProfileIds(MIN_SECONDARY_USER_ID, true)).thenReturn(mProfiles); + when(mUserManager.getProfileIds(userProfile.id, true)) + .thenReturn(new int[] {userProfile.id}); + when(mUserManager.getProfileParent(userProfile.id)).thenReturn(userFullSecondary); + + // add profile + mHistoryManager.onUserAdded(newProfileId); + mHistoryManager.onUserUnlocked(newProfileId); + assertThat(mHistoryManager.doesHistoryExistForUser(newProfileId)).isTrue(); + } + + @Test public void testOnUserUnlocked_historyDisabledThenEnabled() { // create a history mHistoryManager.onUserUnlocked(USER_SYSTEM); diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java index f1edd9a59b99..c1f35ccb69e0 100755 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -958,22 +958,24 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mTestNotificationChannel.setAllowBubbles(channelEnabled); } - private void setUpPrefsForHistory(int uid, boolean globalEnabled) throws Exception { + private void setUpPrefsForHistory(@UserIdInt int userId, boolean globalEnabled) + throws Exception { initNMS(SystemService.PHASE_ACTIVITY_MANAGER_READY); // Sets NOTIFICATION_HISTORY_ENABLED setting for calling process uid Settings.Secure.putIntForUser(mContext.getContentResolver(), - Settings.Secure.NOTIFICATION_HISTORY_ENABLED, globalEnabled ? 1 : 0, uid); + Settings.Secure.NOTIFICATION_HISTORY_ENABLED, globalEnabled ? 1 : 0, userId); // Sets NOTIFICATION_HISTORY_ENABLED setting for uid 0 Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.NOTIFICATION_HISTORY_ENABLED, globalEnabled ? 1 : 0); + setUsers(new int[] {0, userId}); // Forces an update by calling observe on mSettingsObserver, which picks up the settings // changes above. mService.onBootPhase(SystemService.PHASE_THIRD_PARTY_APPS_CAN_START, mMainLooper); assertEquals(globalEnabled, Settings.Secure.getIntForUser(mContext.getContentResolver(), - Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0 /* =def */, uid) != 0); + Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0 /* =def */, userId) != 0); } private StatusBarNotification generateSbn(String pkg, int uid, long postTime, int userId) { @@ -10443,7 +10445,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Test public void testHandleOnPackageRemoved_ClearsHistory() throws Exception { // Enables Notification History setting - setUpPrefsForHistory(mUid, true /* =enabled */); + setUpPrefsForHistory(mUserId, true /* =enabled */); // Posts a notification to the mTestNotificationChannel. final NotificationRecord notif = generateNotificationRecord( |