diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 1e6e5cb97f458..52a5dc1e89826 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -735,19 +735,22 @@ public class NotificationManagerService extends SystemService { final List activeUsers = mUm.getUsers(); for (UserInfo userInfo : activeUsers) { int userId = userInfo.getUserHandle().getIdentifier(); - if (isNASMigrationDone(userId)) { + if (isNASMigrationDone(userId) || mUm.isManagedProfile(userId)) { continue; } if (mAssistants.hasUserSet(userId)) { - mAssistants.loadDefaultsFromConfig(false); ComponentName defaultFromConfig = mAssistants.getDefaultFromConfig(); List allowedComponents = mAssistants.getAllowedComponents(userId); - if (allowedComponents.contains(defaultFromConfig)) { + if (allowedComponents.size() == 0) { + setNASMigrationDone(userId); + mAssistants.clearDefaults(); + continue; + } else if (allowedComponents.contains(defaultFromConfig)) { setNASMigrationDone(userId); mAssistants.resetDefaultFromConfig(); continue; } - //User selected different NAS or none, need onboarding + //User selected different NAS, need onboarding enqueueNotificationInternal(getContext().getPackageName(), getContext().getOpPackageName(), Binder.getCallingUid(), Binder.getCallingPid(), TAG, @@ -819,9 +822,11 @@ public class NotificationManagerService extends SystemService { } @VisibleForTesting - void setNASMigrationDone(int userId) { - Settings.Secure.putIntForUser(getContext().getContentResolver(), - Settings.Secure.NAS_SETTINGS_UPDATED, 1, userId); + void setNASMigrationDone(int baseUserId) { + for (int profileId : mUm.getProfileIds(baseUserId, false)) { + Settings.Secure.putIntForUser(getContext().getContentResolver(), + Settings.Secure.NAS_SETTINGS_UPDATED, 1, profileId); + } } @VisibleForTesting @@ -5171,12 +5176,7 @@ public class NotificationManagerService extends SystemService { @Override public ComponentName getDefaultNotificationAssistant() { checkCallerIsSystem(); - ArraySet defaultComponents = mAssistants.getDefaultComponents(); - if (defaultComponents.size() > 1) { - Slog.w(TAG, "More than one default NotificationAssistant: " - + defaultComponents.size()); - } - return CollectionUtils.firstOrNull(defaultComponents); + return mAssistants.getDefaultFromConfig(); } @Override @@ -9403,6 +9403,9 @@ public class NotificationManagerService extends SystemService { } ComponentName getDefaultFromConfig() { + if (mDefaultFromConfig == null) { + loadDefaultsFromConfig(false); + } return mDefaultFromConfig; } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationAssistantsTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationAssistantsTest.java index c11ac3a3e3a18..6722fff80d0bb 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationAssistantsTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationAssistantsTest.java @@ -213,7 +213,7 @@ public class NotificationAssistantsTest extends UiServiceTestCase { mAssistants.loadDefaultsFromConfig(); assertEquals(new ArraySet<>(Arrays.asList(oldDefaultComponent)), mAssistants.getDefaultComponents()); - assertNull(mAssistants.getDefaultFromConfig()); + assertNull(mAssistants.mDefaultFromConfig); // Test loadDefaultFromConfig(false) only updates the mDefaultFromConfig when(mContext.getResources().getString( 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 37d7198ef1506..a810acc68b2a1 100755 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -5753,11 +5753,12 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { } @Test - public void testNASSettingUpgrade_userSetNull_showOnBoarding() throws RemoteException { + public void testNASSettingUpgrade_userSetNull_noOnBoarding() throws RemoteException { ComponentName newDefaultComponent = ComponentName.unflattenFromString("package/Component1"); TestableNotificationManagerService service = spy(mService); int userId = 11; setUsers(new int[]{userId}); + when(mUm.getProfileIds(userId, false)).thenReturn(new int[]{userId}); setNASMigrationDone(false, userId); when(mAssistants.getDefaultFromConfig()) .thenReturn(newDefaultComponent); @@ -5766,29 +5767,29 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { when(mAssistants.hasUserSet(userId)).thenReturn(true); service.migrateDefaultNASShowNotificationIfNecessary(); - assertFalse(service.isNASMigrationDone(userId)); - verify(service, times(1)).createNASUpgradeNotification(eq(userId)); - verify(mAssistants, times(0)).resetDefaultFromConfig(); + assertTrue(service.isNASMigrationDone(userId)); + verify(service, times(0)).createNASUpgradeNotification(eq(userId)); + verify(mAssistants, times(1)).clearDefaults(); + } - //Test user clear data before enable/disable from onboarding notification - ArrayMap> changedListeners = - generateResetComponentValues(); - when(mListeners.resetComponents(anyString(), anyInt())).thenReturn(changedListeners); - ArrayMap> changes = new ArrayMap<>(); - changes.put(true, new ArrayList(Arrays.asList(newDefaultComponent))); - changes.put(false, new ArrayList()); - when(mAssistants.resetComponents(anyString(), anyInt())).thenReturn(changes); + @Test + public void testNASSettingUpgrade_userSetSameDefault_noOnBoarding() throws RemoteException { + ComponentName defaultComponent = ComponentName.unflattenFromString("package/Component1"); + TestableNotificationManagerService service = spy(mService); + int userId = 11; + setUsers(new int[]{userId}); + when(mUm.getProfileIds(userId, false)).thenReturn(new int[]{userId}); + setNASMigrationDone(false, userId); + when(mAssistants.getDefaultFromConfig()) + .thenReturn(defaultComponent); + when(mAssistants.getAllowedComponents(anyInt())) + .thenReturn(new ArrayList(Arrays.asList(defaultComponent))); + when(mAssistants.hasUserSet(userId)).thenReturn(true); - //Clear data - service.getBinderService().clearData("package", userId, false); - //Test migrate flow again service.migrateDefaultNASShowNotificationIfNecessary(); - - //The notification should be still there - assertFalse(service.isNASMigrationDone(userId)); - verify(service, times(2)).createNASUpgradeNotification(eq(userId)); - verify(mAssistants, times(0)).resetDefaultFromConfig(); - assertEquals(null, service.getApprovedAssistant(userId)); + assertTrue(service.isNASMigrationDone(userId)); + verify(service, times(0)).createNASUpgradeNotification(eq(userId)); + verify(mAssistants, times(1)).resetDefaultFromConfig(); } @Test @@ -5842,6 +5843,9 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { int userId1 = 11; int userId2 = 12; setUsers(new int[]{userId1, userId2}); + when(mUm.getProfileIds(userId1, false)).thenReturn(new int[]{userId1}); + when(mUm.getProfileIds(userId2, false)).thenReturn(new int[]{userId2}); + setNASMigrationDone(false, userId1); setNASMigrationDone(false, userId2); when(mAssistants.getDefaultComponents()) @@ -5867,6 +5871,43 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { verify(mAssistants, times(1)).resetDefaultFromConfig(); } + @Test + public void testNASSettingUpgrade_multiProfile() throws RemoteException { + ComponentName oldDefaultComponent = ComponentName.unflattenFromString("package/Component1"); + ComponentName newDefaultComponent = ComponentName.unflattenFromString("package/Component2"); + TestableNotificationManagerService service = spy(mService); + int userId1 = 11; + int userId2 = 12; //work profile + setUsers(new int[]{userId1, userId2}); + when(mUm.isManagedProfile(userId2)).thenReturn(true); + when(mUm.getProfileIds(userId1, false)).thenReturn(new int[]{userId1, userId2}); + + setNASMigrationDone(false, userId1); + setNASMigrationDone(false, userId2); + when(mAssistants.getDefaultComponents()) + .thenReturn(new ArraySet<>(Arrays.asList(oldDefaultComponent))); + when(mAssistants.getDefaultFromConfig()) + .thenReturn(newDefaultComponent); + //Both profiles: need onboarding + when(mAssistants.getAllowedComponents(userId1)) + .thenReturn(Arrays.asList(oldDefaultComponent)); + when(mAssistants.getAllowedComponents(userId2)) + .thenReturn(Arrays.asList(oldDefaultComponent)); + + when(mAssistants.hasUserSet(userId1)).thenReturn(true); + when(mAssistants.hasUserSet(userId2)).thenReturn(true); + + service.migrateDefaultNASShowNotificationIfNecessary(); + assertFalse(service.isNASMigrationDone(userId1)); + assertFalse(service.isNASMigrationDone(userId2)); + + // only user1 get notification + verify(service, times(1)).createNASUpgradeNotification(eq(userId1)); + verify(service, times(0)).createNASUpgradeNotification(eq(userId2)); + } + + + @Test public void testNASSettingUpgrade_clearDataAfterMigrationIsDone() throws RemoteException { ComponentName defaultComponent = ComponentName.unflattenFromString("package/Component"); @@ -5898,15 +5939,21 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { } @Test - public void testNASUpgradeNotificationDisableBroadcast() { - int userId = 11; - setUsers(new int[]{userId}); + public void testNASUpgradeNotificationDisableBroadcast_multiProfile() { + int userId1 = 11; + int userId2 = 12; + setUsers(new int[]{userId1, userId2}); + when(mUm.isManagedProfile(userId2)).thenReturn(true); + when(mUm.getProfileIds(userId1, false)).thenReturn(new int[]{userId1, userId2}); + TestableNotificationManagerService service = spy(mService); - setNASMigrationDone(false, userId); + setNASMigrationDone(false, userId1); + setNASMigrationDone(false, userId2); - simulateNASUpgradeBroadcast(ACTION_DISABLE_NAS, userId); + simulateNASUpgradeBroadcast(ACTION_DISABLE_NAS, userId1); - assertTrue(service.isNASMigrationDone(userId)); + assertTrue(service.isNASMigrationDone(userId1)); + assertTrue(service.isNASMigrationDone(userId2)); // User disabled the NAS from notification, the default stored in xml should be null // rather than the new default verify(mAssistants, times(1)).clearDefaults(); @@ -5914,7 +5961,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { //No more notification after disabled service.migrateDefaultNASShowNotificationIfNecessary(); - verify(service, times(0)).createNASUpgradeNotification(eq(userId)); + verify(service, times(0)).createNASUpgradeNotification(anyInt()); } @Test @@ -5922,6 +5969,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { int userId1 = 11; int userId2 = 12; setUsers(new int[]{userId1, userId2}); + when(mUm.getProfileIds(userId1, false)).thenReturn(new int[]{userId1}); + TestableNotificationManagerService service = spy(mService); setNASMigrationDone(false, userId1); setNASMigrationDone(false, userId2);