Loading core/java/android/app/NotificationHistory.java +3 −2 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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 + '}'; } Loading Loading @@ -285,9 +288,7 @@ public final class NotificationHistory implements Parcelable { if (!hasNextNotification()) { return null; } HistoricalNotification n = readNotificationFromParcel(mParcel); mIndex++; if (!hasNextNotification()) { mParcel.recycle(); Loading core/tests/coretests/src/android/app/NotificationHistoryTest.java +2 −0 Original line number Diff line number Diff line Loading @@ -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(); } } services/core/java/com/android/server/notification/NotificationHistoryManager.java +7 −2 Original line number Diff line number Diff line Loading @@ -358,10 +358,12 @@ public class NotificationHistoryManager { false, this, UserHandle.USER_ALL); synchronized (mLock) { for (UserInfo userInfo : mUserManager.getUsers()) { if (!userInfo.isProfile()) { update(null, userInfo.id); } } } } void stopObserving() { ContentResolver resolver = mContext.getContentResolver(); Loading @@ -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); } } } } Loading services/core/java/com/android/server/notification/NotificationManagerService.java +1 −0 Original line number Diff line number Diff line Loading @@ -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()) Loading services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryManagerTest.java +78 −18 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -58,9 +56,9 @@ public class NotificationHistoryManagerTest extends UiServiceTestCase { UserManager mUserManager; @Mock NotificationHistoryDatabase mDb; @Mock Handler mHandler; List<UserInfo> mUsers; int[] mProfiles; int mProfileId = 11; NotificationHistoryManager mHistoryManager; Loading Loading @@ -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); for (UserInfo info : mUsers) { Settings.Secure.putIntForUser(getContext().getContentResolver(), Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 1, info.id); } 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(), mHandler); mHistoryManager.onBootPhaseAppsCanStart(); } mHistoryManager = new NotificationHistoryManager(getContext(), null); @After public void tearDown() { mHistoryManager.onDestroy(); for (UserInfo info : mUsers) { Settings.Secure.putIntForUser(getContext().getContentResolver(), Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 1, info.id); mHistoryManager.mSettingsObserver.update(null, info.id); } } @Test Loading Loading @@ -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 Loading @@ -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"; Loading Loading
core/java/android/app/NotificationHistory.java +3 −2 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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 + '}'; } Loading Loading @@ -285,9 +288,7 @@ public final class NotificationHistory implements Parcelable { if (!hasNextNotification()) { return null; } HistoricalNotification n = readNotificationFromParcel(mParcel); mIndex++; if (!hasNextNotification()) { mParcel.recycle(); Loading
core/tests/coretests/src/android/app/NotificationHistoryTest.java +2 −0 Original line number Diff line number Diff line Loading @@ -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(); } }
services/core/java/com/android/server/notification/NotificationHistoryManager.java +7 −2 Original line number Diff line number Diff line Loading @@ -358,10 +358,12 @@ public class NotificationHistoryManager { false, this, UserHandle.USER_ALL); synchronized (mLock) { for (UserInfo userInfo : mUserManager.getUsers()) { if (!userInfo.isProfile()) { update(null, userInfo.id); } } } } void stopObserving() { ContentResolver resolver = mContext.getContentResolver(); Loading @@ -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); } } } } Loading
services/core/java/com/android/server/notification/NotificationManagerService.java +1 −0 Original line number Diff line number Diff line Loading @@ -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()) Loading
services/tests/uiservicestests/src/com/android/server/notification/NotificationHistoryManagerTest.java +78 −18 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -58,9 +56,9 @@ public class NotificationHistoryManagerTest extends UiServiceTestCase { UserManager mUserManager; @Mock NotificationHistoryDatabase mDb; @Mock Handler mHandler; List<UserInfo> mUsers; int[] mProfiles; int mProfileId = 11; NotificationHistoryManager mHistoryManager; Loading Loading @@ -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); for (UserInfo info : mUsers) { Settings.Secure.putIntForUser(getContext().getContentResolver(), Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 1, info.id); } 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(), mHandler); mHistoryManager.onBootPhaseAppsCanStart(); } mHistoryManager = new NotificationHistoryManager(getContext(), null); @After public void tearDown() { mHistoryManager.onDestroy(); for (UserInfo info : mUsers) { Settings.Secure.putIntForUser(getContext().getContentResolver(), Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 1, info.id); mHistoryManager.mSettingsObserver.update(null, info.id); } } @Test Loading Loading @@ -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 Loading @@ -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"; Loading