diff --git a/core/java/android/app/NotificationHistory.java b/core/java/android/app/NotificationHistory.java index 8c2cc9492e39e..59dc9991c832f 100644 --- a/core/java/android/app/NotificationHistory.java +++ b/core/java/android/app/NotificationHistory.java @@ -22,6 +22,7 @@ import android.graphics.drawable.Icon; import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; +import android.util.Slog; import java.util.ArrayList; import java.util.Arrays; @@ -107,9 +108,11 @@ public final class NotificationHistory implements Parcelable { ", mChannelName='" + mChannelName + '\'' + ", mChannelId='" + mChannelId + '\'' + ", mUserId=" + mUserId + + ", mUid=" + mUid + ", mTitle='" + mTitle + '\'' + ", mText='" + mText + '\'' + ", mIcon=" + mIcon + + ", mPostedTimeMs=" + mPostedTimeMs + ", mConversationId=" + mConversationId + '}'; } @@ -285,9 +288,7 @@ public final class NotificationHistory implements Parcelable { if (!hasNextNotification()) { return null; } - HistoricalNotification n = readNotificationFromParcel(mParcel); - mIndex++; if (!hasNextNotification()) { mParcel.recycle(); diff --git a/core/tests/coretests/src/android/app/NotificationHistoryTest.java b/core/tests/coretests/src/android/app/NotificationHistoryTest.java index 0443d3a9e73f0..c9510918e5554 100644 --- a/core/tests/coretests/src/android/app/NotificationHistoryTest.java +++ b/core/tests/coretests/src/android/app/NotificationHistoryTest.java @@ -351,5 +351,7 @@ public class NotificationHistoryTest { HistoricalNotification postParcelNotification = parceledHistory.getNextNotification(); assertThat(postParcelNotification).isEqualTo(expectedEntries.get(i)); } + assertThat(parceledHistory.hasNextNotification()).isFalse(); + assertThat(parceledHistory.getNextNotification()).isNull(); } } diff --git a/services/core/java/com/android/server/notification/NotificationHistoryManager.java b/services/core/java/com/android/server/notification/NotificationHistoryManager.java index 7d680127740c1..f7fb9b71ec874 100644 --- a/services/core/java/com/android/server/notification/NotificationHistoryManager.java +++ b/services/core/java/com/android/server/notification/NotificationHistoryManager.java @@ -358,7 +358,9 @@ public class NotificationHistoryManager { false, this, UserHandle.USER_ALL); synchronized (mLock) { for (UserInfo userInfo : mUserManager.getUsers()) { - update(null, userInfo.id); + if (!userInfo.isProfile()) { + update(null, userInfo.id); + } } } } @@ -379,7 +381,10 @@ public class NotificationHistoryManager { boolean historyEnabled = Settings.Secure.getIntForUser(resolver, Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, userId) != 0; - onHistoryEnabledChanged(userId, historyEnabled); + int[] profiles = mUserManager.getProfileIds(userId, true); + for (int profileId : profiles) { + onHistoryEnabledChanged(profileId, historyEnabled); + } } } } diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index e7553b6bcea75..69a5b35a5b12c 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -2684,6 +2684,7 @@ public class NotificationManagerService extends SystemService { mHistoryManager.addNotification(new HistoricalNotification.Builder() .setPackage(r.getSbn().getPackageName()) .setUid(r.getSbn().getUid()) + .setUserId(r.getUserId()) .setChannelId(r.getChannel().getId()) .setChannelName(r.getChannel().getName().toString()) .setPostedTimeMs(System.currentTimeMillis()) 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 f7c26091a7f41..2341c10a9c910 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryManagerTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryManagerTest.java @@ -31,7 +31,6 @@ import android.app.NotificationHistory; import android.app.NotificationHistory.HistoricalNotification; import android.content.pm.UserInfo; import android.graphics.drawable.Icon; -import android.os.Handler; import android.os.UserManager; import android.provider.Settings; @@ -40,7 +39,6 @@ import androidx.test.runner.AndroidJUnit4; import com.android.server.UiServiceTestCase; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -58,9 +56,9 @@ public class NotificationHistoryManagerTest extends UiServiceTestCase { UserManager mUserManager; @Mock NotificationHistoryDatabase mDb; - @Mock - Handler mHandler; List mUsers; + int[] mProfiles; + int mProfileId = 11; NotificationHistoryManager mHistoryManager; @@ -98,26 +96,32 @@ public class NotificationHistoryManagerTest extends UiServiceTestCase { UserInfo userSystem = new UserInfo(); userSystem.id = USER_SYSTEM; mUsers.add(userSystem); - UserInfo userAll = new UserInfo(); - userAll.id = MIN_SECONDARY_USER_ID; - mUsers.add(userAll); - mUsers.add(userAll); + UserInfo userFullSecondary = new UserInfo(); + userFullSecondary.id = MIN_SECONDARY_USER_ID; + mUsers.add(userFullSecondary); + UserInfo userProfile = new UserInfo(); + userProfile.id = mProfileId; + mUsers.add(userProfile); when(mUserManager.getUsers()).thenReturn(mUsers); + mProfiles = new int[] {userSystem.id, userProfile.id}; + when(mUserManager.getProfileIds(userSystem.id, true)).thenReturn(mProfiles); + when(mUserManager.getProfileIds(userFullSecondary.id, true)) + .thenReturn(new int[] {userFullSecondary.id}); + when(mUserManager.getProfileIds(userProfile.id, true)) + .thenReturn(new int[] {userProfile.id}); + + when(mUserManager.getProfileParent(userProfile.id)).thenReturn(userSystem); + + NotificationHistoryDatabaseFactory.setTestingNotificationHistoryDatabase(mDb); + + mHistoryManager = new NotificationHistoryManager(getContext(), null); + for (UserInfo info : mUsers) { Settings.Secure.putIntForUser(getContext().getContentResolver(), Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 1, info.id); + mHistoryManager.mSettingsObserver.update(null, info.id); } - - NotificationHistoryDatabaseFactory.setTestingNotificationHistoryDatabase(mDb); - - mHistoryManager = new NotificationHistoryManager(getContext(), mHandler); - mHistoryManager.onBootPhaseAppsCanStart(); - } - - @After - public void tearDown() { - mHistoryManager.onDestroy(); } @Test @@ -150,6 +154,31 @@ public class NotificationHistoryManagerTest extends UiServiceTestCase { verify(mDb, times(1)).disableHistory(); } + @Test + public void testOnUserUnlocked_historyDisabled_withProfile() { + // create a history + mHistoryManager.onUserUnlocked(USER_SYSTEM); + assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isTrue(); + mHistoryManager.onUserUnlocked(mProfileId); + assertThat(mHistoryManager.doesHistoryExistForUser(mProfileId)).isTrue(); + // lock user + mHistoryManager.onUserStopped(USER_SYSTEM); + mHistoryManager.onUserStopped(mProfileId); + + // turn off history + Settings.Secure.putIntForUser(getContext().getContentResolver(), + Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, USER_SYSTEM); + mHistoryManager.mSettingsObserver.update(null, USER_SYSTEM); + + // unlock user, verify that history is disabled for self and profile + mHistoryManager.onUserUnlocked(USER_SYSTEM); + mHistoryManager.onUserUnlocked(mProfileId); + + assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isFalse(); + assertThat(mHistoryManager.doesHistoryExistForUser(mProfileId)).isFalse(); + verify(mDb, times(2)).disableHistory(); + } + @Test public void testOnUserUnlocked_historyDisabledThenEnabled() { // create a history @@ -176,6 +205,37 @@ public class NotificationHistoryManagerTest extends UiServiceTestCase { verify(mDb, never()).disableHistory(); } + @Test + public void testOnUserUnlocked_historyDisabledThenEnabled_multiProfile() { + // create a history + mHistoryManager.onUserUnlocked(USER_SYSTEM); + assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isTrue(); + mHistoryManager.onUserUnlocked(mProfileId); + assertThat(mHistoryManager.doesHistoryExistForUser(mProfileId)).isTrue(); + + // lock user + mHistoryManager.onUserStopped(USER_SYSTEM); + mHistoryManager.onUserStopped(mProfileId); + + // turn off history + Settings.Secure.putIntForUser(getContext().getContentResolver(), + Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, USER_SYSTEM); + mHistoryManager.mSettingsObserver.update(null, USER_SYSTEM); + + // turn on history + Settings.Secure.putIntForUser(getContext().getContentResolver(), + Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 1, USER_SYSTEM); + mHistoryManager.mSettingsObserver.update(null, USER_SYSTEM); + + // unlock user, verify that history is NOT disabled + mHistoryManager.onUserUnlocked(USER_SYSTEM); + mHistoryManager.onUserUnlocked(mProfileId); + + assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isTrue(); + assertThat(mHistoryManager.doesHistoryExistForUser(mProfileId)).isTrue(); + verify(mDb, never()).disableHistory(); + } + @Test public void testOnUserUnlocked_cleansUpRemovedPackages() { String pkg = "pkg"; 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 b6cdbfb42c2d4..64d481a2e6a0b 100755 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -4394,6 +4394,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { verify(mAppUsageStats, times(1)).reportInterruptiveNotification( anyString(), anyString(), anyInt()); + verify(mHistoryManager, times(1)).addNotification(any()); } @Test