diff options
13 files changed, 182 insertions, 141 deletions
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index a8959c56ae22..48d11c328b97 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -2869,7 +2869,8 @@ public class NotificationManagerService extends SystemService { adjustedSbn.getUser(), GroupHelper.AUTOGROUP_KEY, System.currentTimeMillis()); summaryRecord = new NotificationRecord(getContext(), summarySbn, - notificationRecord.getChannel()); + notificationRecord.getChannel(), mRankingHelper.supportsChannels( + summarySbn.getPackageName(), summarySbn.getUid())); summaries.put(pkg, summarySbn.getKey()); } } @@ -3210,7 +3211,8 @@ public class NotificationManagerService extends SystemService { final StatusBarNotification n = new StatusBarNotification( pkg, opPkg, id, tag, notificationUid, callingPid, notification, user, null, System.currentTimeMillis()); - final NotificationRecord r = new NotificationRecord(getContext(), n, channel); + final NotificationRecord r = new NotificationRecord(getContext(), n, channel, + mRankingHelper.supportsChannels(pkg, notificationUid)); if (!checkDisqualifyingFeatures(userId, notificationUid, id,tag, r)) { return; diff --git a/services/core/java/com/android/server/notification/NotificationRecord.java b/services/core/java/com/android/server/notification/NotificationRecord.java index b51a4d10a053..90257daae453 100644 --- a/services/core/java/com/android/server/notification/NotificationRecord.java +++ b/services/core/java/com/android/server/notification/NotificationRecord.java @@ -115,7 +115,7 @@ public final class NotificationRecord { private int mSuppressedVisualEffects = 0; private String mUserExplanation; private String mPeopleExplanation; - private boolean mPreChannelsNotification = true; + private boolean mSupportsChannels = false; private Uri mSound; private long[] mVibration; private AudioAttributes mAttributes; @@ -128,7 +128,7 @@ public final class NotificationRecord { @VisibleForTesting public NotificationRecord(Context context, StatusBarNotification sbn, - NotificationChannel channel) + NotificationChannel channel, boolean supportsChannels) { this.sbn = sbn; mOriginalFlags = sbn.getNotification().flags; @@ -138,7 +138,7 @@ public final class NotificationRecord { mContext = context; stats = new NotificationUsageStats.SingleNotificationStats(); mChannel = channel; - mPreChannelsNotification = isPreChannelsNotification(); + mSupportsChannels = supportsChannels; mSound = calculateSound(); mVibration = calculateVibration(); mAttributes = calculateAttributes(); @@ -146,27 +146,11 @@ public final class NotificationRecord { mLight = calculateLights(); } - private boolean isPreChannelsNotification() { - try { - if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(getChannel().getId())) { - final ApplicationInfo applicationInfo = - mContext.getPackageManager().getApplicationInfoAsUser(sbn.getPackageName(), - 0, UserHandle.getUserId(sbn.getUid())); - if (applicationInfo.targetSdkVersion <= Build.VERSION_CODES.N_MR1) { - return true; - } - } - } catch (NameNotFoundException e) { - Slog.e(TAG, "Can't find package", e); - } - return false; - } - private Uri calculateSound() { final Notification n = sbn.getNotification(); Uri sound = mChannel.getSound(); - if (mPreChannelsNotification && (getChannel().getUserLockedFields() + if (!mSupportsChannels && (getChannel().getUserLockedFields() & NotificationChannel.USER_LOCKED_SOUND) == 0) { final boolean useDefaultSound = (n.defaults & Notification.DEFAULT_SOUND) != 0; @@ -191,7 +175,7 @@ public final class NotificationRecord { : defaultLightColor; Light light = getChannel().shouldShowLights() ? new Light(channelLightColor, defaultLightOn, defaultLightOff) : null; - if (mPreChannelsNotification + if (!mSupportsChannels && (getChannel().getUserLockedFields() & NotificationChannel.USER_LOCKED_LIGHTS) == 0) { final Notification notification = sbn.getNotification(); @@ -222,7 +206,7 @@ public final class NotificationRecord { } else { vibration = null; } - if (mPreChannelsNotification + if (!mSupportsChannels && (getChannel().getUserLockedFields() & NotificationChannel.USER_LOCKED_VIBRATION) == 0) { final Notification notification = sbn.getNotification(); @@ -244,7 +228,7 @@ public final class NotificationRecord { attributes = Notification.AUDIO_ATTRIBUTES_DEFAULT; } - if (mPreChannelsNotification + if (!mSupportsChannels && (getChannel().getUserLockedFields() & NotificationChannel.USER_LOCKED_SOUND) == 0) { if (n.audioAttributes != null) { @@ -293,7 +277,7 @@ public final class NotificationRecord { stats.requestedImportance = requestedImportance; stats.isNoisy = mSound != null || mVibration != null; - if (mPreChannelsNotification + if (!mSupportsChannels && (importance == IMPORTANCE_UNSPECIFIED || (getChannel().getUserLockedFields() & NotificationChannel.USER_LOCKED_IMPORTANCE) == 0)) { @@ -460,7 +444,7 @@ public final class NotificationRecord { pw.println(prefix + "mVisibleSinceMs=" + mVisibleSinceMs); pw.println(prefix + "mUpdateTimeMs=" + mUpdateTimeMs); pw.println(prefix + "mSuppressedVisualEffects= " + mSuppressedVisualEffects); - if (mPreChannelsNotification) { + if (!mSupportsChannels) { pw.println(prefix + String.format("defaults=0x%08x flags=0x%08x", notification.defaults, notification.flags)); pw.println(prefix + "n.sound=" + notification.sound); diff --git a/services/core/java/com/android/server/notification/RankingConfig.java b/services/core/java/com/android/server/notification/RankingConfig.java index 4d19b52c327a..14d796f59c37 100644 --- a/services/core/java/com/android/server/notification/RankingConfig.java +++ b/services/core/java/com/android/server/notification/RankingConfig.java @@ -42,4 +42,6 @@ public interface RankingConfig { void permanentlyDeleteNotificationChannel(String pkg, int uid, String channelId); void permanentlyDeleteNotificationChannels(String pkg, int uid); ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg, int uid, boolean includeDeleted); + + boolean supportsChannels(String pkg, int uid); } diff --git a/services/core/java/com/android/server/notification/RankingHelper.java b/services/core/java/com/android/server/notification/RankingHelper.java index 850b730f3dff..3481556ddb8f 100644 --- a/services/core/java/com/android/server/notification/RankingHelper.java +++ b/services/core/java/com/android/server/notification/RankingHelper.java @@ -273,8 +273,14 @@ public class RankingHelper implements RankingConfig { } private boolean shouldHaveDefaultChannel(Record r) throws NameNotFoundException { + if (supportsChannels(r)) { + return false; + } + final int userId = UserHandle.getUserId(r.uid); - final ApplicationInfo applicationInfo = mPm.getApplicationInfoAsUser(r.pkg, 0, userId); + final ApplicationInfo applicationInfo = mPm.getApplicationInfoAsUser(r.pkg, + PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, + userId); if (applicationInfo.targetSdkVersion > Build.VERSION_CODES.N_MR1) { // O apps should not have the default channel. return false; @@ -500,6 +506,31 @@ public class RankingHelper implements RankingConfig { } @Override + public boolean supportsChannels(String pkg, int uid) { + Record r = getOrCreateRecord(pkg, uid); + + if (r == null) { + return false; + } + + if (r.channels.size() == 1 + && r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) { + return false; + } + + return true; + } + + private boolean supportsChannels(Record r) { + if (r.channels.size() == 1 + && r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) { + return false; + } + + return (r.channels.size() > 0); + } + + @Override public void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group, boolean fromTargetApp) { Preconditions.checkNotNull(pkg); @@ -571,6 +602,10 @@ public class RankingHelper implements RankingConfig { r.channels.put(channel.getId(), channel); MetricsLogger.action(getChannelLog(channel, pkg).setType( MetricsProto.MetricsEvent.TYPE_OPEN)); + + // Remove Default Channel. + r.channels.remove(NotificationChannel.DEFAULT_CHANNEL_ID); + updateConfig(); } @@ -667,13 +702,7 @@ public class RankingHelper implements RankingConfig { if (r == null) { return; } - int N = r.channels.size() - 1; - for (int i = N; i >= 0; i--) { - String key = r.channels.keyAt(i); - if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(key)) { - r.channels.remove(key); - } - } + r.channels.clear(); updateConfig(); } @@ -1025,6 +1054,8 @@ public class RankingHelper implements RankingConfig { final int uid = uidList[i]; synchronized (mRecords) { mRecords.remove(recordKey(pkg, uid)); + // reset to default settings and re-add misc channel for pre-O apps + getOrCreateRecord(pkg, uid); } mRestoredWithoutUids.remove(pkg); updated = true; diff --git a/services/tests/notification/src/com/android/server/notification/BadgeExtractorTest.java b/services/tests/notification/src/com/android/server/notification/BadgeExtractorTest.java index 0cf4994ebbce..2e8b0682f53d 100644 --- a/services/tests/notification/src/com/android/server/notification/BadgeExtractorTest.java +++ b/services/tests/notification/src/com/android/server/notification/BadgeExtractorTest.java @@ -69,7 +69,7 @@ public class BadgeExtractorTest { Notification n = builder.build(); StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, mId, mTag, mUid, mPid, n, mUser, null, System.currentTimeMillis()); - NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); + NotificationRecord r = new NotificationRecord(getContext(), sbn, channel, true); return r; } diff --git a/services/tests/notification/src/com/android/server/notification/BuzzBeepBlinkTest.java b/services/tests/notification/src/com/android/server/notification/BuzzBeepBlinkTest.java index d4904f5aecf7..5e8b3d43682b 100644 --- a/services/tests/notification/src/com/android/server/notification/BuzzBeepBlinkTest.java +++ b/services/tests/notification/src/com/android/server/notification/BuzzBeepBlinkTest.java @@ -259,7 +259,7 @@ public class BuzzBeepBlinkTest { StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, id, mTag, mUid, mPid, n, mUser, null, System.currentTimeMillis()); - NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); + NotificationRecord r = new NotificationRecord(getContext(), sbn, channel, true); mService.addNotification(r); return r; } @@ -769,7 +769,7 @@ public class BuzzBeepBlinkTest { StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, 0, mTag, mUid, mPid, n, mUser, null, System.currentTimeMillis()); - NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); + NotificationRecord r = new NotificationRecord(getContext(), sbn, channel, true); mService.addNotification(r); mService.buzzBeepBlinkLocked(r); diff --git a/services/tests/notification/src/com/android/server/notification/GlobalSortKeyComparatorTest.java b/services/tests/notification/src/com/android/server/notification/GlobalSortKeyComparatorTest.java index 24cb72e8b0fb..33d2d079d87e 100644 --- a/services/tests/notification/src/com/android/server/notification/GlobalSortKeyComparatorTest.java +++ b/services/tests/notification/src/com/android/server/notification/GlobalSortKeyComparatorTest.java @@ -53,21 +53,21 @@ public class GlobalSortKeyComparatorTest { new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), - "", 1499), getDefaultChannel()); + "", 1499), getDefaultChannel(), true); left.setGlobalSortKey("first"); NotificationRecord right = new NotificationRecord(InstrumentationRegistry.getContext(), new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), - "", 1499), getDefaultChannel()); + "", 1499), getDefaultChannel(), true); right.setGlobalSortKey("second"); NotificationRecord last = new NotificationRecord(InstrumentationRegistry.getContext(), new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), - "", 1499), getDefaultChannel()); + "", 1499), getDefaultChannel(), true); final List<NotificationRecord> expected = new ArrayList<>(); @@ -93,13 +93,13 @@ public class GlobalSortKeyComparatorTest { new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), - "", 1499), getDefaultChannel()); + "", 1499), getDefaultChannel(), true); NotificationRecord right = new NotificationRecord(InstrumentationRegistry.getContext(), new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), - "", 1499), getDefaultChannel()); + "", 1499), getDefaultChannel(), true); right.setGlobalSortKey("not null"); final List<NotificationRecord> expected = new ArrayList<>(); @@ -124,14 +124,14 @@ public class GlobalSortKeyComparatorTest { new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), - "", 1499), getDefaultChannel()); + "", 1499), getDefaultChannel(), true); left.setGlobalSortKey("not null"); NotificationRecord right = new NotificationRecord(InstrumentationRegistry.getContext(), new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), - "", 1499), getDefaultChannel()); + "", 1499), getDefaultChannel(), true); final List<NotificationRecord> expected = new ArrayList<>(); expected.add(left); diff --git a/services/tests/notification/src/com/android/server/notification/ImportanceExtractorTest.java b/services/tests/notification/src/com/android/server/notification/ImportanceExtractorTest.java index 3dbd803e0a76..7a2dbaf98692 100644 --- a/services/tests/notification/src/com/android/server/notification/ImportanceExtractorTest.java +++ b/services/tests/notification/src/com/android/server/notification/ImportanceExtractorTest.java @@ -71,7 +71,7 @@ public class ImportanceExtractorTest { Notification n = builder.build(); StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, mId, mTag, mUid, mPid, n, mUser, null, System.currentTimeMillis()); - NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); + NotificationRecord r = new NotificationRecord(getContext(), sbn, channel, true); return r; } diff --git a/services/tests/notification/src/com/android/server/notification/NotificationComparatorTest.java b/services/tests/notification/src/com/android/server/notification/NotificationComparatorTest.java index 84945ab5acd3..ccd2db0ebf2c 100644 --- a/services/tests/notification/src/com/android/server/notification/NotificationComparatorTest.java +++ b/services/tests/notification/src/com/android/server/notification/NotificationComparatorTest.java @@ -108,7 +108,7 @@ public class NotificationComparatorTest { .build(); mRecordMinCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg, callPkg, 1, "minCall", callUid, callUid, n1, - new UserHandle(userId), "", 2000), getDefaultChannel()); + new UserHandle(userId), "", 2000), getDefaultChannel(), false); mRecordMinCall.setUserImportance(NotificationManager.IMPORTANCE_MIN); Notification n2 = new Notification.Builder(mContext, TEST_CHANNEL_ID) @@ -118,7 +118,7 @@ public class NotificationComparatorTest { .build(); mRecordHighCall = new NotificationRecord(mContext, new StatusBarNotification(callPkg, callPkg, 1, "highcall", callUid, callUid, n2, - new UserHandle(userId), "", 1999), getDefaultChannel()); + new UserHandle(userId), "", 1999), getDefaultChannel(), false); mRecordHighCall.setUserImportance(NotificationManager.IMPORTANCE_HIGH); Notification n3 = new Notification.Builder(mContext, TEST_CHANNEL_ID) @@ -128,14 +128,14 @@ public class NotificationComparatorTest { .build(); mRecordDefaultMedia = new NotificationRecord(mContext, new StatusBarNotification(pkg2, pkg2, 1, "media", uid2, uid2, n3, new UserHandle(userId), - "", 1499), getDefaultChannel()); + "", 1499), getDefaultChannel(), false); mRecordDefaultMedia.setUserImportance(NotificationManager.IMPORTANCE_DEFAULT); Notification n4 = new Notification.Builder(mContext, TEST_CHANNEL_ID) .setStyle(new Notification.MessagingStyle("sender!")).build(); mRecordInlineReply = new NotificationRecord(mContext, new StatusBarNotification(pkg2, pkg2, 1, "inlinereply", uid2, uid2, n4, new UserHandle(userId), - "", 1599), getDefaultChannel()); + "", 1599), getDefaultChannel(), false); mRecordInlineReply.setUserImportance(NotificationManager.IMPORTANCE_HIGH); mRecordInlineReply.setPackagePriority(Notification.PRIORITY_MAX); @@ -143,27 +143,27 @@ public class NotificationComparatorTest { .setCategory(Notification.CATEGORY_MESSAGE).build(); mRecordSms = new NotificationRecord(mContext, new StatusBarNotification(smsPkg, smsPkg, 1, "sms", smsUid, smsUid, n5, new UserHandle(userId), - "", 1299), getDefaultChannel()); + "", 1299), getDefaultChannel(), false); mRecordSms.setUserImportance(NotificationManager.IMPORTANCE_DEFAULT); Notification n6 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build(); mRecordStarredContact = new NotificationRecord(mContext, new StatusBarNotification(pkg2, pkg2, 1, "starred", uid2, uid2, n6, new UserHandle(userId), - "", 1259), getDefaultChannel()); + "", 1259), getDefaultChannel(), false); mRecordStarredContact.setContactAffinity(ValidateNotificationPeople.STARRED_CONTACT); mRecordStarredContact.setUserImportance(NotificationManager.IMPORTANCE_DEFAULT); Notification n7 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build(); mRecordContact = new NotificationRecord(mContext, new StatusBarNotification(pkg2, pkg2, 1, "contact", uid2, uid2, n7, new UserHandle(userId), - "", 1259), getDefaultChannel()); + "", 1259), getDefaultChannel(), false); mRecordContact.setContactAffinity(ValidateNotificationPeople.VALID_CONTACT); mRecordContact.setUserImportance(NotificationManager.IMPORTANCE_DEFAULT); Notification n8 = new Notification.Builder(mContext, TEST_CHANNEL_ID).build(); mRecordUrgent = new NotificationRecord(mContext, new StatusBarNotification(pkg2, pkg2, 1, "urgent", uid2, uid2, n8, new UserHandle(userId), - "", 1258), getDefaultChannel()); + "", 1258), getDefaultChannel(), false); mRecordUrgent.setUserImportance(NotificationManager.IMPORTANCE_HIGH); Notification n9 = new Notification.Builder(mContext, TEST_CHANNEL_ID) @@ -173,7 +173,7 @@ public class NotificationComparatorTest { .build(); mRecordCheater = new NotificationRecord(mContext, new StatusBarNotification(pkg2, pkg2, 1, "cheater", uid2, uid2, n9, new UserHandle(userId), - "", 9258), getDefaultChannel()); + "", 9258), getDefaultChannel(), false); mRecordCheater.setUserImportance(NotificationManager.IMPORTANCE_LOW); mRecordCheater.setPackagePriority(Notification.PRIORITY_MAX); @@ -181,7 +181,7 @@ public class NotificationComparatorTest { .setStyle(new Notification.InboxStyle().setSummaryText("message!")).build(); mRecordEmail = new NotificationRecord(mContext, new StatusBarNotification(pkg2, pkg2, 1, "email", uid2, uid2, n10, new UserHandle(userId), - "", 1599), getDefaultChannel()); + "", 1599), getDefaultChannel(), false); mRecordEmail.setUserImportance(NotificationManager.IMPORTANCE_HIGH); Notification n11 = new Notification.Builder(mContext, TEST_CHANNEL_ID) @@ -190,7 +190,7 @@ public class NotificationComparatorTest { .build(); mRecordCheaterColorized = new NotificationRecord(mContext, new StatusBarNotification(pkg2, pkg2, 1, "cheater", uid2, uid2, n11, new UserHandle(userId), - "", 9258), getDefaultChannel()); + "", 9258), getDefaultChannel(), false); mRecordCheaterColorized.setUserImportance(NotificationManager.IMPORTANCE_LOW); } diff --git a/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java index 177c02d63280..f3eb4f817262 100644 --- a/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -165,7 +165,7 @@ public class NotificationManagerServiceTest { StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, "tag", uid, 0, nb.build(), new UserHandle(uid), null, 0); - return new NotificationRecord(mContext, sbn, channel); + return new NotificationRecord(mContext, sbn, channel, true); } private NotificationRecord generateNotificationRecord(NotificationChannel channel) { return generateNotificationRecord(channel, null); @@ -184,7 +184,7 @@ public class NotificationManagerServiceTest { } StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", uid, 0, nb.build(), new UserHandle(uid), null, 0); - return new NotificationRecord(mContext, sbn, channel); + return new NotificationRecord(mContext, sbn, channel, true); } @Test diff --git a/services/tests/notification/src/com/android/server/notification/NotificationRecordTest.java b/services/tests/notification/src/com/android/server/notification/NotificationRecordTest.java index 1c8ca84815d3..66f379970d5a 100644 --- a/services/tests/notification/src/com/android/server/notification/NotificationRecordTest.java +++ b/services/tests/notification/src/com/android/server/notification/NotificationRecordTest.java @@ -21,7 +21,6 @@ import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; -import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.when; @@ -61,6 +60,10 @@ public class NotificationRecordTest { private final Context mMockContext = Mockito.mock(Context.class); @Mock PackageManager mPm; + // constants for targetSdk version. N is pre channels, O is post. + private final boolean N = false; + private final boolean O = true; + private final String pkg = "com.android.server.notification"; private final int uid = 9583; private final String pkg2 = "pkg2"; @@ -76,7 +79,6 @@ public class NotificationRecordTest { new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "test", NotificationManager.IMPORTANCE_UNSPECIFIED); private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser()); - final ApplicationInfo legacy = new ApplicationInfo(); final ApplicationInfo upgrade = new ApplicationInfo(); private static final long[] CUSTOM_VIBRATION = new long[] { @@ -100,17 +102,13 @@ public class NotificationRecordTest { InstrumentationRegistry.getContext().getResources()); when(mMockContext.getPackageManager()).thenReturn(mPm); - legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; upgrade.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1; - try { - when(mPm.getApplicationInfoAsUser(eq(pkg), anyInt(), anyInt())).thenReturn(legacy); - when(mPm.getApplicationInfoAsUser(eq(pkg2), anyInt(), anyInt())).thenReturn(upgrade); - } catch (PackageManager.NameNotFoundException e) {} + when(mMockContext.getApplicationInfo()).thenReturn(upgrade); } - private StatusBarNotification getNotification(boolean preO, boolean noisy, boolean defaultSound, - boolean buzzy, boolean defaultVibration, boolean lights, boolean defaultLights) { - when(mMockContext.getApplicationInfo()).thenReturn(preO ? legacy : upgrade); + private StatusBarNotification getNotification(boolean supportsChannels, boolean noisy, + boolean defaultSound, boolean buzzy, boolean defaultVibration, boolean lights, + boolean defaultLights) { final Builder builder = new Builder(mMockContext) .setContentTitle("foo") .setSmallIcon(android.R.drawable.sym_def_app_icon) @@ -149,13 +147,13 @@ public class NotificationRecordTest { } builder.setDefaults(defaults); - if (!preO) { + if (supportsChannels) { builder.setChannelId(channelId); } Notification n = builder.build(); - if (preO) { + if (!supportsChannels) { return new StatusBarNotification(pkg, pkg, id1, tag1, uid, uid, n, mUser, null, uid); } else { @@ -172,11 +170,11 @@ public class NotificationRecordTest { public void testSound_default_preUpgradeUsesNotification() throws Exception { defaultChannel.setSound(null, null); // pre upgrade, default sound. - StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(N, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, record.getSound()); assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes()); } @@ -185,11 +183,11 @@ public class NotificationRecordTest { public void testSound_custom_preUpgradeUsesNotification() throws Exception { defaultChannel.setSound(null, null); // pre upgrade, custom sound. - StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(N, true /* noisy */, false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertEquals(CUSTOM_SOUND, record.getSound()); assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes()); } @@ -199,11 +197,11 @@ public class NotificationRecordTest { defaultChannel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES); defaultChannel.lockFields(NotificationChannel.USER_LOCKED_SOUND); // pre upgrade, default sound. - StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(N, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertEquals(CUSTOM_SOUND, record.getSound()); assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes()); } @@ -211,11 +209,11 @@ public class NotificationRecordTest { @Test public void testSound_noSound_preUpgrade() throws Exception { // pre upgrade, default sound. - StatusBarNotification sbn = getNotification(true /*preO */, false /* noisy */, + StatusBarNotification sbn = getNotification(N, false /* noisy */, false /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertEquals(null, record.getSound()); assertEquals(Notification.AUDIO_ATTRIBUTES_DEFAULT, record.getAudioAttributes()); } @@ -224,11 +222,11 @@ public class NotificationRecordTest { public void testSound_default_upgradeUsesChannel() throws Exception { channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES); // post upgrade, default sound. - StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(O, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel, O); assertEquals(CUSTOM_SOUND, record.getSound()); assertEquals(CUSTOM_ATTRIBUTES, record.getAudioAttributes()); } @@ -237,11 +235,11 @@ public class NotificationRecordTest { public void testVibration_default_preUpgradeUsesNotification() throws Exception { defaultChannel.enableVibration(false); // pre upgrade, default vibration. - StatusBarNotification sbn = getNotification(true /*preO */, false /* noisy */, + StatusBarNotification sbn = getNotification(N, false /* noisy */, false /* defaultSound */, true /* buzzy */, true /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertNotNull(record.getVibration()); } @@ -249,11 +247,11 @@ public class NotificationRecordTest { public void testVibration_custom_preUpgradeUsesNotification() throws Exception { defaultChannel.enableVibration(false); // pre upgrade, custom vibration. - StatusBarNotification sbn = getNotification(true /*preO */, false /* noisy */, + StatusBarNotification sbn = getNotification(N, false /* noisy */, false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertEquals(CUSTOM_VIBRATION, record.getVibration()); } @@ -262,11 +260,11 @@ public class NotificationRecordTest { defaultChannel.enableVibration(true); defaultChannel.lockFields(NotificationChannel.USER_LOCKED_VIBRATION); // pre upgrade, custom vibration. - StatusBarNotification sbn = getNotification(true /*preO */, false /* noisy */, + StatusBarNotification sbn = getNotification(N, false /* noisy */, false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertTrue(!Objects.equals(CUSTOM_VIBRATION, record.getVibration())); } @@ -274,20 +272,20 @@ public class NotificationRecordTest { public void testVibration_custom_upgradeUsesChannel() throws Exception { channel.enableVibration(true); // post upgrade, custom vibration. - StatusBarNotification sbn = getNotification(false /*preO */, false /* noisy */, + StatusBarNotification sbn = getNotification(O, false /* noisy */, false /* defaultSound */, true /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel, O); assertEquals(CUSTOM_CHANNEL_VIBRATION, record.getVibration()); } @Test public void testImportance_preUpgrade() throws Exception { - StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(N, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance()); } @@ -295,11 +293,11 @@ public class NotificationRecordTest { public void testImportance_locked_preUpgrade() throws Exception { defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW); defaultChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); - StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(N, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertEquals(NotificationManager.IMPORTANCE_LOW, record.getImportance()); } @@ -307,39 +305,39 @@ public class NotificationRecordTest { public void testImportance_locked_unspecified_preUpgrade() throws Exception { defaultChannel.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED); defaultChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); - StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(N, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertEquals(NotificationManager.IMPORTANCE_HIGH, record.getImportance()); } @Test public void testImportance_upgrade() throws Exception { - StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(O, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel, O); assertEquals(NotificationManager.IMPORTANCE_DEFAULT, record.getImportance()); } @Test public void testLights_preUpgrade_noLight() throws Exception { - StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(N, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertNull(record.getLight()); } @Test public void testLights_preUpgrade() throws Exception { - StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(N, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, true /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertEquals(CUSTOM_LIGHT, record.getLight()); } @@ -347,11 +345,11 @@ public class NotificationRecordTest { public void testLights_locked_preUpgrade() throws Exception { defaultChannel.enableLights(true); defaultChannel.lockFields(NotificationChannel.USER_LOCKED_LIGHTS); - StatusBarNotification sbn = getNotification(true /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(N, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, true /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, N); assertFalse(CUSTOM_LIGHT.equals(record.getLight())); } @@ -366,10 +364,10 @@ public class NotificationRecordTest { NotificationRecord.Light expected = new NotificationRecord.Light( defaultLightColor, defaultLightOn, defaultLightOff); - StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(O, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, true /* lights */, true /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel, O); assertEquals(expected, record.getLight()); } @@ -382,19 +380,19 @@ public class NotificationRecordTest { NotificationRecord.Light expected = new NotificationRecord.Light( Color.BLUE, defaultLightOn, defaultLightOff); - StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(O, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, true /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel, O); assertEquals(expected, record.getLight()); } @Test public void testLights_upgrade_noLight() throws Exception { - StatusBarNotification sbn = getNotification(false /*preO */, true /* noisy */, + StatusBarNotification sbn = getNotification(O, true /* noisy */, true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, false /* lights */, false /*defaultLights */); - NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, defaultChannel, O); assertNull(record.getLight()); } } diff --git a/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java b/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java index c48e73879b13..e2428b908f41 100644 --- a/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java +++ b/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java @@ -124,7 +124,7 @@ public class RankingHelperTest { .build(); mRecordGroupGSortA = new NotificationRecord(getContext(), new StatusBarNotification( "package", "package", 1, null, 0, 0, mNotiGroupGSortA, user, - null, System.currentTimeMillis()), getDefaultChannel()); + null, System.currentTimeMillis()), getDefaultChannel(), false); mNotiGroupGSortB = new Notification.Builder(getContext(), TEST_CHANNEL_ID) .setContentTitle("B") @@ -134,7 +134,7 @@ public class RankingHelperTest { .build(); mRecordGroupGSortB = new NotificationRecord(getContext(), new StatusBarNotification( "package", "package", 1, null, 0, 0, mNotiGroupGSortB, user, - null, System.currentTimeMillis()), getDefaultChannel()); + null, System.currentTimeMillis()), getDefaultChannel(), false); mNotiNoGroup = new Notification.Builder(getContext(), TEST_CHANNEL_ID) .setContentTitle("C") @@ -142,7 +142,7 @@ public class RankingHelperTest { .build(); mRecordNoGroup = new NotificationRecord(getContext(), new StatusBarNotification( "package", "package", 1, null, 0, 0, mNotiNoGroup, user, - null, System.currentTimeMillis()), getDefaultChannel()); + null, System.currentTimeMillis()), getDefaultChannel(), false); mNotiNoGroup2 = new Notification.Builder(getContext(), TEST_CHANNEL_ID) .setContentTitle("D") @@ -150,7 +150,7 @@ public class RankingHelperTest { .build(); mRecordNoGroup2 = new NotificationRecord(getContext(), new StatusBarNotification( "package", "package", 1, null, 0, 0, mNotiNoGroup2, user, - null, System.currentTimeMillis()), getDefaultChannel()); + null, System.currentTimeMillis()), getDefaultChannel(), false); mNotiNoGroupSortA = new Notification.Builder(getContext(), TEST_CHANNEL_ID) .setContentTitle("E") @@ -159,7 +159,7 @@ public class RankingHelperTest { .build(); mRecordNoGroupSortA = new NotificationRecord(getContext(), new StatusBarNotification( "package", "package", 1, null, 0, 0, mNotiNoGroupSortA, user, - null, System.currentTimeMillis()), getDefaultChannel()); + null, System.currentTimeMillis()), getDefaultChannel(), false); mAudioAttributes = new AudioAttributes.Builder() .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) @@ -373,7 +373,7 @@ public class RankingHelperTest { assertNull(mHelper.getNotificationChannel(PKG, UID, channel1.getId(), false)); assertNull(mHelper.getNotificationChannel(PKG, UID, channel3.getId(), false)); assertNull(mHelper.getNotificationChannelGroup(ncg.getId(), PKG, UID)); - //assertEquals(ncg2, mHelper.getNotificationChannelGroup(ncg2.getId(), PKG, UID)); + assertEquals(ncg2, mHelper.getNotificationChannelGroup(ncg2.getId(), PKG, UID)); assertEquals(channel2, mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false)); } @@ -696,20 +696,14 @@ public class RankingHelperTest { // Returns only non-deleted channels List<NotificationChannel> channels = mHelper.getNotificationChannels(PKG, UID, false).getList(); - assertEquals(2, channels.size()); // Default channel + non-deleted channel - for (NotificationChannel nc : channels) { - if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) { - compareChannels(channel2, nc); - } - } + assertEquals(1, channels.size()); + compareChannels(channel2, channels.get(0)); // Returns deleted channels too channels = mHelper.getNotificationChannels(PKG, UID, true).getList(); - assertEquals(3, channels.size()); // Includes default channel + assertEquals(2, channels.size()); for (NotificationChannel nc : channels) { - if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) { - compareChannels(channelMap.get(nc.getId()), nc); - } + compareChannels(channelMap.get(nc.getId()), nc); } } @@ -807,8 +801,8 @@ public class RankingHelperTest { mHelper.permanentlyDeleteNotificationChannels(PKG, UID); - // Only default channel remains - assertEquals(1, mHelper.getNotificationChannels(PKG, UID, true).getList().size()); + // No channels remain + assertEquals(0, mHelper.getNotificationChannels(PKG, UID, true).getList().size()); } @Test @@ -857,13 +851,32 @@ public class RankingHelperTest { mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID}); - assertEquals(0, mHelper.getNotificationChannels(PKG, UID, true).getList().size()); + // since this is a pre upgrade app, clearing data should restore the default channel + assertEquals(1, mHelper.getNotificationChannels(PKG, UID, true).getList().size()); + assertEquals(NotificationChannel.DEFAULT_CHANNEL_ID, + mHelper.getNotificationChannels(PKG, UID, true).getList().get(0).getId()); // Not deleted mHelper.createNotificationChannel(PKG, UID, channel1, true); - mHelper.onPackagesChanged(false, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID}); - assertEquals(2, mHelper.getNotificationChannels(PKG, UID, false).getList().size()); + assertEquals(1, mHelper.getNotificationChannels(PKG, UID, false).getList().size()); + } + + @Test + public void testOnPackageChanged_packageRemoval_updatedPackage() throws Exception { + // Deleted + NotificationChannel channel1 = + new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); + mHelper.createNotificationChannel(UPDATED_PKG, UID2, channel1, true); + mHelper.onPackagesChanged( + true, UserHandle.USER_SYSTEM, new String[]{UPDATED_PKG}, new int[]{UID2}); + assertEquals(0, mHelper.getNotificationChannels(UPDATED_PKG, UID2, true).getList().size()); + + // Not deleted + mHelper.createNotificationChannel(UPDATED_PKG, UID2, channel1, true); + mHelper.onPackagesChanged( + false, UserHandle.USER_SYSTEM, new String[]{UPDATED_PKG}, new int[]{UID2}); + assertEquals(1, mHelper.getNotificationChannels(UPDATED_PKG, UID2, false).getList().size()); } @Test @@ -884,7 +897,23 @@ public class RankingHelperTest { mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID}); - assertEquals(0, mHelper.getNotificationChannelGroups(PKG, UID, true).getList().size()); + // default channel restored + assertEquals(1, mHelper.getNotificationChannelGroups(PKG, UID, true).getList().size()); + assertNull(mHelper.getNotificationChannelGroups(PKG, UID, true).getList().get(0).getId()); + } + + @Test + public void testOnPackageChanged_packageRemoval_groups_upgraded() throws Exception { + NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); + mHelper.createNotificationChannelGroup(UPDATED_PKG, UID2, ncg, true); + NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2"); + mHelper.createNotificationChannelGroup(UPDATED_PKG, UID2, ncg2, true); + + mHelper.onPackagesChanged( + true, UserHandle.USER_SYSTEM, new String[]{UPDATED_PKG}, new int[]{UID2}); + + assertEquals(0, + mHelper.getNotificationChannelGroups(UPDATED_PKG, UID2, true).getList().size()); } @Test @@ -958,9 +987,8 @@ public class RankingHelperTest { assertEquals(3, actual.size()); for (NotificationChannelGroup group : actual) { if (group.getId() == null) { - assertEquals(2, group.getChannels().size()); // misc channel too - assertTrue(channel3.getId().equals(group.getChannels().get(0).getId()) - || channel3.getId().equals(group.getChannels().get(1).getId())); + assertEquals(1, group.getChannels().size()); + assertTrue(channel3.getId().equals(group.getChannels().get(0).getId())); } else if (group.getId().equals(ncg.getId())) { assertEquals(2, group.getChannels().size()); if (group.getChannels().get(0).getId().equals(channel1.getId())) { @@ -994,12 +1022,8 @@ public class RankingHelperTest { List<NotificationChannelGroup> actual = mHelper.getNotificationChannelGroups(PKG, UID, true).getList(); - assertEquals(2, actual.size()); - for (NotificationChannelGroup group : actual) { - if (Objects.equals(group.getId(), ncg.getId())) { - assertEquals(1, group.getChannels().size()); - } - } + assertEquals(1, actual.size()); + assertEquals(1, actual.get(0).getChannels().size()); } @Test diff --git a/services/tests/notification/src/com/android/server/notification/SnoozeHelperTest.java b/services/tests/notification/src/com/android/server/notification/SnoozeHelperTest.java index bc25860e8e92..b1cb4d7d4e67 100644 --- a/services/tests/notification/src/com/android/server/notification/SnoozeHelperTest.java +++ b/services/tests/notification/src/com/android/server/notification/SnoozeHelperTest.java @@ -312,7 +312,7 @@ public class SnoozeHelperTest { TEST_CHANNEL_ID, "name", NotificationManager.IMPORTANCE_LOW); return new NotificationRecord(getContext(), new StatusBarNotification( pkg, pkg, id, tag, 0, 0, n, user, null, - System.currentTimeMillis()), notificationChannel); + System.currentTimeMillis()), notificationChannel, true); } private NotificationRecord getNotificationRecord(String pkg, int id, String tag, |