diff options
| author | 2017-01-03 16:44:20 -0500 | |
|---|---|---|
| committer | 2017-03-20 16:49:59 -0400 | |
| commit | 1f17e024a1152eda828a0e9924daa30cfac1193d (patch) | |
| tree | 18671ea5297e85fdad73ca24a5c6b5e79e06342d | |
| parent | 5beefa697aeca3eeaeeac50f781908657490e211 (diff) | |
Delete the Default Channel when an app starts using channels.
New apps should create their own channels.
Not using channels will silently fail.
STOPSHIPS:
Throw to provide better feedback for developers.
Delete the default channel for all apps that target O.
Test: runtest systemui-notification
Change-Id: Ic93f103efe397f563eaaf1c2e7d8bf9093b2b2e1
5 files changed, 370 insertions, 310 deletions
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 3dcc5d997e08..411fb8050d81 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -3097,8 +3097,17 @@ public class NotificationManagerService extends SystemService { if (mIsTelevision && (new Notification.TvExtender(notification)).getChannel() != null) { channelId = (new Notification.TvExtender(notification)).getChannel(); } - final NotificationChannel channel = mRankingHelper.getNotificationChannelWithFallback(pkg, + final NotificationChannel channel = mRankingHelper.getNotificationChannel(pkg, notificationUid, channelId, false /* includeDeleted */); + if (channel == null) { + // STOPSHIP TODO: remove before release - should always throw without a valid channel. + if (channelId == null) { + Log.e(TAG, "Cannot post notification without channel ID when targeting O " + + " - notification=" + notification); + return; + } + throw new IllegalArgumentException("No Channel found for notification=" + notification); + } final StatusBarNotification n = new StatusBarNotification( pkg, opPkg, id, tag, notificationUid, callingPid, notification, user, null, System.currentTimeMillis()); diff --git a/services/core/java/com/android/server/notification/RankingConfig.java b/services/core/java/com/android/server/notification/RankingConfig.java index 6a0072216f70..e13df192acb6 100644 --- a/services/core/java/com/android/server/notification/RankingConfig.java +++ b/services/core/java/com/android/server/notification/RankingConfig.java @@ -39,7 +39,6 @@ public interface RankingConfig { void updateNotificationChannel(String pkg, int uid, NotificationChannel channel); void updateNotificationChannelFromAssistant(String pkg, int uid, NotificationChannel channel); NotificationChannel getNotificationChannel(String pkg, int uid, String channelId, boolean includeDeleted); - NotificationChannel getNotificationChannelWithFallback(String pkg, int uid, String channelId, boolean includeDeleted); void deleteNotificationChannel(String pkg, int uid, String channelId); void permanentlyDeleteNotificationChannel(String pkg, int uid, String channelId); void permanentlyDeleteNotificationChannels(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 7feaf5a6d932..ce79465d7260 100644 --- a/services/core/java/com/android/server/notification/RankingHelper.java +++ b/services/core/java/com/android/server/notification/RankingHelper.java @@ -213,7 +213,11 @@ public class RankingHelper implements RankingConfig { } } - clampDefaultChannel(r); + try { + deleteDefaultChannelIfNeeded(r); + } catch (NameNotFoundException e) { + Slog.e(TAG, "deleteDefaultChannelIfNeeded - Exception: " + e); + } } } } @@ -247,60 +251,94 @@ public class RankingHelper implements RankingConfig { r.priority = priority; r.visibility = visibility; r.showBadge = showBadge; - createDefaultChannelIfMissing(r); + + try { + createDefaultChannelIfNeeded(r); + } catch (NameNotFoundException e) { + Slog.e(TAG, "createDefaultChannelIfNeeded - Exception: " + e); + } + if (r.uid == Record.UNKNOWN_UID) { mRestoredWithoutUids.put(pkg, r); } else { mRecords.put(key, r); } - clampDefaultChannel(r); } return r; } - // Clamp the importance level of the default channel for apps targeting the new SDK version, - // unless the user has already changed the importance. - private void clampDefaultChannel(Record r) { - try { - if (r.uid != Record.UNKNOWN_UID) { - int userId = UserHandle.getUserId(r.uid); - final ApplicationInfo applicationInfo = - mPm.getApplicationInfoAsUser(r.pkg, 0, userId); - if (applicationInfo.targetSdkVersion > Build.VERSION_CODES.N_MR1) { - final NotificationChannel defaultChannel = - r.channels.get(NotificationChannel.DEFAULT_CHANNEL_ID); - if ((defaultChannel.getUserLockedFields() - & NotificationChannel.USER_LOCKED_IMPORTANCE) == 0) { - defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW); - updateConfig(); - } - } + private boolean shouldHaveDefaultChannel(Record r) throws NameNotFoundException { + final int userId = UserHandle.getUserId(r.uid); + final ApplicationInfo applicationInfo = mPm.getApplicationInfoAsUser(r.pkg, 0, userId); + if (applicationInfo.targetSdkVersion <= Build.VERSION_CODES.N_MR1) { + // Pre-O apps should have it. + return true; + } + + // STOPSHIP TODO: remove before release - O+ apps should never have a default channel. + // But for now, leave the default channel until an app has created its first channel. + boolean hasCreatedAChannel = false; + final int size = r.channels.size(); + for (int i = 0; i < size; i++) { + final NotificationChannel notificationChannel = r.channels.valueAt(i); + if (notificationChannel != null && + notificationChannel.getId() != NotificationChannel.DEFAULT_CHANNEL_ID) { + hasCreatedAChannel = true; + break; } - } catch (NameNotFoundException e) { - // oh well. } + if (!hasCreatedAChannel) { + return true; + } + + // Otherwise, should not have the default channel. + return false; } - private void createDefaultChannelIfMissing(Record r) { + private void deleteDefaultChannelIfNeeded(Record r) throws NameNotFoundException { if (!r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) { - NotificationChannel channel; - channel = new NotificationChannel( - NotificationChannel.DEFAULT_CHANNEL_ID, - mContext.getString(R.string.default_notification_channel_label), - r.importance); - channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX); - channel.setLockscreenVisibility(r.visibility); - if (r.importance != NotificationManager.IMPORTANCE_UNSPECIFIED) { - channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); - } - if (r.priority != DEFAULT_PRIORITY) { - channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); - } - if (r.visibility != DEFAULT_VISIBILITY) { - channel.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY); - } - r.channels.put(channel.getId(), channel); + // Not present + return; } + + if (shouldHaveDefaultChannel(r)) { + // Keep the default channel until upgraded. + return; + } + + // Remove Default Channel. + r.channels.remove(NotificationChannel.DEFAULT_CHANNEL_ID); + } + + private void createDefaultChannelIfNeeded(Record r) throws NameNotFoundException { + if (r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) { + // Already exists + return; + } + + if (!shouldHaveDefaultChannel(r)) { + // Keep the default channel until upgraded. + return; + } + + // Create Default Channel + NotificationChannel channel; + channel = new NotificationChannel( + NotificationChannel.DEFAULT_CHANNEL_ID, + mContext.getString(R.string.default_notification_channel_label), + r.importance); + channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX); + channel.setLockscreenVisibility(r.visibility); + if (r.importance != NotificationManager.IMPORTANCE_UNSPECIFIED) { + channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); + } + if (r.priority != DEFAULT_PRIORITY) { + channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); + } + if (r.visibility != DEFAULT_VISIBILITY) { + channel.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY); + } + r.channels.put(channel.getId(), channel); } public void writeXml(XmlSerializer out, boolean forBackup) throws IOException { @@ -619,21 +657,6 @@ public class RankingHelper implements RankingConfig { } @Override - public NotificationChannel getNotificationChannelWithFallback(String pkg, int uid, - String channelId, boolean includeDeleted) { - Record r = getOrCreateRecord(pkg, uid); - if (channelId == null) { - channelId = NotificationChannel.DEFAULT_CHANNEL_ID; - } - NotificationChannel channel = r.channels.get(channelId); - if (channel != null && (includeDeleted || !channel.isDeleted())) { - return channel; - } else { - return r.channels.get(NotificationChannel.DEFAULT_CHANNEL_ID); - } - } - - @Override public NotificationChannel getNotificationChannel(String pkg, int uid, String channelId, boolean includeDeleted) { Preconditions.checkNotNull(pkg); @@ -1057,10 +1080,9 @@ public class RankingHelper implements RankingConfig { Record fullRecord = getRecord(pkg, mPm.getPackageUidAsUser(pkg, changeUserId)); if (fullRecord != null) { - clampDefaultChannel(fullRecord); + deleteDefaultChannelIfNeeded(fullRecord); } - } catch (NameNotFoundException e) { - } + } catch (NameNotFoundException e) {} } } 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 b7b3617d7ecf..ab83b9d84747 100644 --- a/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -59,21 +59,23 @@ import com.android.server.lights.LightsManager; public class NotificationManagerServiceTest { private static final long WAIT_FOR_IDLE_TIMEOUT = 2; - private final String pkg = "com.android.server.notification"; + private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId"; private final int uid = Binder.getCallingUid(); private NotificationManagerService mNotificationManagerService; private INotificationManager mBinderService; private IPackageManager mPackageManager = mock(IPackageManager.class); - final PackageManager mPackageManagerClient = mock(PackageManager.class); - private Context mContext; + private final PackageManager mPackageManagerClient = mock(PackageManager.class); + private Context mContext = InstrumentationRegistry.getTargetContext(); + private final String PKG = mContext.getPackageName(); private HandlerThread mThread; - final RankingHelper mRankingHelper = mock(RankingHelper.class); + private final RankingHelper mRankingHelper = mock(RankingHelper.class); + private NotificationChannel mTestNotificationChannel = new NotificationChannel( + TEST_CHANNEL_ID, TEST_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT); @Before @Test @UiThreadTest public void setUp() throws Exception { - mContext = InstrumentationRegistry.getTargetContext(); mNotificationManagerService = new NotificationManagerService(mContext); // MockPackageManager - default returns ApplicationInfo with matching calling UID @@ -93,13 +95,16 @@ public class NotificationManagerServiceTest { mock(NotificationManagerService.NotificationListeners.class); when(mockNotificationListeners.checkServiceTokenLocked(any())).thenReturn( mockNotificationListeners.new ManagedServiceInfo(null, - new ComponentName(pkg, "test_class"), uid, true, null, 0)); + new ComponentName(PKG, "test_class"), uid, true, null, 0)); mNotificationManagerService.init(mThread.getLooper(), mPackageManager, mPackageManagerClient, mockLightsManager, mockNotificationListeners); // Tests call directly into the Binder. mBinderService = mNotificationManagerService.getBinderService(); + + mBinderService.createNotificationChannels( + PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); } public void waitForIdle() throws Exception { @@ -127,7 +132,7 @@ public class NotificationManagerServiceTest { private NotificationRecord generateNotificationRecord(NotificationChannel channel, Notification.TvExtender extender) { if (channel == null) { - channel = new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); + channel = mTestNotificationChannel; } Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) .setContentTitle("foo") @@ -135,8 +140,7 @@ public class NotificationManagerServiceTest { if (extender != null) { nb.extend(extender); } - StatusBarNotification sbn = new StatusBarNotification(mContext.getPackageName(), - mContext.getPackageName(), 1, "tag", uid, 0, + StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", uid, 0, nb.build(), new UserHandle(uid), null, 0); return new NotificationRecord(mContext, sbn, channel); } @@ -256,38 +260,38 @@ public class NotificationManagerServiceTest { @Test @UiThreadTest public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception { - mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, + mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, generateNotificationRecord(null).getNotification(), new int[1], 0); waitForIdle(); StatusBarNotification[] notifs = - mBinderService.getActiveNotifications(mContext.getPackageName()); + mBinderService.getActiveNotifications(PKG); assertEquals(1, notifs.length); } @Test @UiThreadTest public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception { - mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, + mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, generateNotificationRecord(null).getNotification(), new int[1], 0); - mBinderService.cancelNotificationWithTag(mContext.getPackageName(), "tag", 0, 0); + mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0); waitForIdle(); StatusBarNotification[] notifs = - mBinderService.getActiveNotifications(mContext.getPackageName()); + mBinderService.getActiveNotifications(PKG); assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelNotificationWhilePostedAndEnqueued() throws Exception { - mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, + mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, generateNotificationRecord(null).getNotification(), new int[1], 0); waitForIdle(); - mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, + mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, generateNotificationRecord(null).getNotification(), new int[1], 0); - mBinderService.cancelNotificationWithTag(mContext.getPackageName(), "tag", 0, 0); + mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0); waitForIdle(); StatusBarNotification[] notifs = - mBinderService.getActiveNotifications(mContext.getPackageName()); + mBinderService.getActiveNotifications(PKG); assertEquals(0, notifs.length); } @@ -295,7 +299,7 @@ public class NotificationManagerServiceTest { @UiThreadTest public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; - mBinderService.enqueueNotificationWithTag(sbn.getPackageName(), "opPkg", "tag", + mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", sbn.getId(), sbn.getNotification(), new int[1], sbn.getUserId()); mBinderService.cancelNotificationsFromListener(null, null); waitForIdle(); @@ -308,9 +312,9 @@ public class NotificationManagerServiceTest { @UiThreadTest public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; - mBinderService.enqueueNotificationWithTag(sbn.getPackageName(), "opPkg", "tag", + mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", sbn.getId(), sbn.getNotification(), new int[1], sbn.getUserId()); - mBinderService.cancelAllNotifications(sbn.getPackageName(), sbn.getUserId()); + mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); waitForIdle(); StatusBarNotification[] notifs = mBinderService.getActiveNotifications(sbn.getPackageName()); @@ -322,9 +326,9 @@ public class NotificationManagerServiceTest { public void testCancelAllNotifications_IgnoreForegroundService() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; - mBinderService.enqueueNotificationWithTag(sbn.getPackageName(), "opPkg", "tag", + mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", sbn.getId(), sbn.getNotification(), new int[1], sbn.getUserId()); - mBinderService.cancelAllNotifications(sbn.getPackageName(), sbn.getUserId()); + mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); waitForIdle(); StatusBarNotification[] notifs = mBinderService.getActiveNotifications(sbn.getPackageName()); @@ -336,7 +340,7 @@ public class NotificationManagerServiceTest { public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; - mBinderService.enqueueNotificationWithTag(sbn.getPackageName(), "opPkg", "tag", + mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", sbn.getId(), sbn.getNotification(), new int[1], sbn.getUserId()); mBinderService.cancelAllNotifications("other_pkg_name", sbn.getUserId()); waitForIdle(); @@ -349,7 +353,7 @@ public class NotificationManagerServiceTest { @UiThreadTest public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; - mBinderService.enqueueNotificationWithTag(sbn.getPackageName(), "opPkg", "tag", + mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", sbn.getId(), sbn.getNotification(), new int[1], sbn.getUserId()); mBinderService.cancelAllNotifications(null, sbn.getUserId()); waitForIdle(); @@ -362,7 +366,7 @@ public class NotificationManagerServiceTest { @UiThreadTest public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; - mBinderService.enqueueNotificationWithTag(sbn.getPackageName(), "opPkg", "tag", + mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", sbn.getId(), sbn.getNotification(), new int[1], UserHandle.USER_ALL); // Null pkg is how we signal a user switch. mBinderService.cancelAllNotifications(null, sbn.getUserId()); @@ -377,14 +381,14 @@ public class NotificationManagerServiceTest { public void testTvExtenderChannelOverride_onTv() throws Exception { mNotificationManagerService.setIsTelevision(true); mNotificationManagerService.setRankingHelper(mRankingHelper); - when(mRankingHelper.getNotificationChannelWithFallback( + when(mRankingHelper.getNotificationChannel( anyString(), anyInt(), eq("foo"), anyBoolean())).thenReturn( new NotificationChannel("foo", "foo", NotificationManager.IMPORTANCE_HIGH)); Notification.TvExtender tv = new Notification.TvExtender().setChannel("foo"); - mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, + mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, generateNotificationRecord(null, tv).getNotification(), new int[1], 0); - verify(mRankingHelper, times(1)).getNotificationChannelWithFallback( + verify(mRankingHelper, times(1)).getNotificationChannel( anyString(), anyInt(), eq("foo"), anyBoolean()); } @@ -393,14 +397,14 @@ public class NotificationManagerServiceTest { public void testTvExtenderChannelOverride_notOnTv() throws Exception { mNotificationManagerService.setIsTelevision(false); mNotificationManagerService.setRankingHelper(mRankingHelper); - when(mRankingHelper.getNotificationChannelWithFallback( + when(mRankingHelper.getNotificationChannel( anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( - new NotificationChannel("id", "id", NotificationManager.IMPORTANCE_HIGH)); + mTestNotificationChannel); Notification.TvExtender tv = new Notification.TvExtender().setChannel("foo"); - mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, + mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, generateNotificationRecord(null, tv).getNotification(), new int[1], 0); - verify(mRankingHelper, times(1)).getNotificationChannelWithFallback( - anyString(), anyInt(), eq("id"), anyBoolean()); + verify(mRankingHelper, times(1)).getNotificationChannel( + anyString(), anyInt(), eq(mTestNotificationChannel.getId()), anyBoolean()); } } 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 fab843425d4a..5a94018c6eb1 100644 --- a/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java +++ b/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java @@ -78,12 +78,15 @@ import static org.mockito.Mockito.when; @SmallTest @RunWith(AndroidJUnit4.class) public class RankingHelperTest { - @Mock - NotificationUsageStats mUsageStats; - @Mock - RankingHandler handler; - @Mock - PackageManager mPm; + private static final String PKG = "com.android.server.notification"; + private static final int UID = 0; + private static final String UPDATED_PKG = "updatedPkg"; + private static final int UID2 = 1111111; + private static final String TEST_CHANNEL_ID = "test_channel_id"; + + @Mock NotificationUsageStats mUsageStats; + @Mock RankingHandler mHandler; + @Mock PackageManager mPm; private Notification mNotiGroupGSortA; private Notification mNotiGroupGSortB; @@ -96,11 +99,6 @@ public class RankingHelperTest { private NotificationRecord mRecordNoGroup2; private NotificationRecord mRecordNoGroupSortA; private RankingHelper mHelper; - private final String pkg = "com.android.server.notification"; - private final int uid = 0; - private final String pkg2 = "pkg2"; - private final int uid2 = 1111111; - private static final String TEST_CHANNEL_ID = "test_channel_id"; private AudioAttributes mAudioAttributes; private Context getContext() { @@ -108,12 +106,12 @@ public class RankingHelperTest { } @Before - public void setUp() { + public void setUp() throws Exception { MockitoAnnotations.initMocks(this); UserHandle user = UserHandle.ALL; - mHelper = new RankingHelper(getContext(), mPm, handler, mUsageStats, - new String[]{ImportanceExtractor.class.getName()}); + mHelper = new RankingHelper(getContext(), mPm, mHandler, mUsageStats, + new String[] {ImportanceExtractor.class.getName()}); mNotiGroupGSortA = new Notification.Builder(getContext(), TEST_CHANNEL_ID) .setContentTitle("A") @@ -170,12 +168,9 @@ public class RankingHelperTest { legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; final ApplicationInfo upgrade = new ApplicationInfo(); 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); - when(mPm.getPackageUidAsUser(eq(pkg), anyInt())).thenReturn(uid); - } catch (PackageManager.NameNotFoundException e) { - } + when(mPm.getApplicationInfoAsUser(eq(PKG), anyInt(), anyInt())).thenReturn(legacy); + when(mPm.getApplicationInfoAsUser(eq(UPDATED_PKG), anyInt(), anyInt())).thenReturn(upgrade); + when(mPm.getPackageUidAsUser(eq(PKG), anyInt())).thenReturn(UID); } private NotificationChannel getDefaultChannel() { @@ -202,6 +197,14 @@ public class RankingHelperTest { return baos; } + private void loadStreamXml(ByteArrayOutputStream stream) throws Exception { + XmlPullParser parser = Xml.newPullParser(); + parser.setInput(new BufferedInputStream(new ByteArrayInputStream(stream.toByteArray())), + null); + parser.nextTag(); + mHelper.readXml(parser, false); + } + private void compareChannels(NotificationChannel expected, NotificationChannel actual) { assertEquals(expected.getId(), actual.getId()); assertEquals(expected.getName(), actual.getName()); @@ -289,34 +292,28 @@ public class RankingHelperTest { channel2.setVibrationPattern(new long[]{100, 67, 145, 156}); channel2.setLightColor(Color.BLUE); - mHelper.createNotificationChannelGroup(pkg, uid, ncg, true); - mHelper.createNotificationChannelGroup(pkg, uid, ncg2, true); - mHelper.createNotificationChannel(pkg, uid, channel1, true); - mHelper.createNotificationChannel(pkg, uid, channel2, false); + mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); + mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true); + mHelper.createNotificationChannel(PKG, UID, channel1, true); + mHelper.createNotificationChannel(PKG, UID, channel2, false); - mHelper.setShowBadge(pkg, uid, true); - mHelper.setShowBadge(pkg2, uid2, false); + mHelper.setShowBadge(PKG, UID, true); - ByteArrayOutputStream baos = writeXmlAndPurge(pkg, uid, false, channel1.getId(), + ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false, channel1.getId(), channel2.getId(), NotificationChannel.DEFAULT_CHANNEL_ID); - mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{pkg}, new int[]{uid}); + mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG}, new int[]{UID}); - XmlPullParser parser = Xml.newPullParser(); - parser.setInput(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())), - null); - parser.nextTag(); - mHelper.readXml(parser, false); + loadStreamXml(baos); - assertFalse(mHelper.canShowBadge(pkg2, uid2)); - assertTrue(mHelper.canShowBadge(pkg, uid)); - assertEquals(channel1, mHelper.getNotificationChannel(pkg, uid, channel1.getId(), false)); + assertTrue(mHelper.canShowBadge(PKG, UID)); + assertEquals(channel1, mHelper.getNotificationChannel(PKG, UID, channel1.getId(), false)); compareChannels(channel2, - mHelper.getNotificationChannel(pkg, uid, channel2.getId(), false)); + mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false)); assertNotNull(mHelper.getNotificationChannel( - pkg, uid, NotificationChannel.DEFAULT_CHANNEL_ID, false)); + PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false)); List<NotificationChannelGroup> actualGroups = - mHelper.getNotificationChannelGroups(pkg, uid, false).getList(); + mHelper.getNotificationChannelGroups(PKG, UID, false).getList(); boolean foundNcg = false; for (NotificationChannelGroup actual : actualGroups) { if (ncg.getId().equals(actual.getId())) { @@ -350,19 +347,19 @@ public class RankingHelperTest { new NotificationChannel("id3", "name3", IMPORTANCE_LOW); channel3.setGroup(ncg.getId()); - mHelper.createNotificationChannelGroup(pkg, uid, ncg, true); - mHelper.createNotificationChannelGroup(pkg, uid, ncg2, true); - mHelper.createNotificationChannel(pkg, uid, channel1, true); - mHelper.createNotificationChannel(pkg, uid, channel2, false); - mHelper.createNotificationChannel(pkg, uid, channel3, true); + mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); + mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true); + mHelper.createNotificationChannel(PKG, UID, channel1, true); + mHelper.createNotificationChannel(PKG, UID, channel2, false); + mHelper.createNotificationChannel(PKG, UID, channel3, true); - mHelper.deleteNotificationChannel(pkg, uid, channel1.getId()); - mHelper.deleteNotificationChannelGroup(pkg, uid, ncg.getId()); - assertEquals(channel2, mHelper.getNotificationChannel(pkg, uid, channel2.getId(), false)); + mHelper.deleteNotificationChannel(PKG, UID, channel1.getId()); + mHelper.deleteNotificationChannelGroup(PKG, UID, ncg.getId()); + assertEquals(channel2, mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false)); - ByteArrayOutputStream baos = writeXmlAndPurge(pkg, uid, true, channel1.getId(), + ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel1.getId(), channel2.getId(), channel3.getId(), NotificationChannel.DEFAULT_CHANNEL_ID); - mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{pkg}, new int[]{uid}); + mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG}, new int[]{UID}); XmlPullParser parser = Xml.newPullParser(); parser.setInput(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())), @@ -370,11 +367,11 @@ public class RankingHelperTest { parser.nextTag(); mHelper.readXml(parser, true); - 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(channel2, mHelper.getNotificationChannel(pkg, uid, channel2.getId(), false)); + 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(channel2, mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false)); } @Test @@ -382,19 +379,15 @@ public class RankingHelperTest { NotificationChannel channel1 = new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_DEFAULT); - mHelper.createNotificationChannel(pkg, uid, channel1, true); + mHelper.createNotificationChannel(PKG, UID, channel1, true); - ByteArrayOutputStream baos = writeXmlAndPurge(pkg, uid,false, channel1.getId(), + ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false, channel1.getId(), NotificationChannel.DEFAULT_CHANNEL_ID); - XmlPullParser parser = Xml.newPullParser(); - parser.setInput(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())), - null); - parser.nextTag(); - mHelper.readXml(parser, false); + loadStreamXml(baos); - final NotificationChannel updated = mHelper.getNotificationChannel( - pkg, uid, NotificationChannel.DEFAULT_CHANNEL_ID, false); + final NotificationChannel updated = mHelper.getNotificationChannel(PKG, UID, + NotificationChannel.DEFAULT_CHANNEL_ID, false); assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, updated.getImportance()); assertFalse(updated.canBypassDnd()); assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, updated.getLockscreenVisibility()); @@ -405,34 +398,30 @@ public class RankingHelperTest { public void testChannelXml_defaultChannelUpdatedApp_userSettings() throws Exception { NotificationChannel channel1 = new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_MIN); - mHelper.createNotificationChannel(pkg, uid, channel1, true); + mHelper.createNotificationChannel(PKG, UID, channel1, true); - final NotificationChannel defaultChannel = mHelper.getNotificationChannel( - pkg, uid, NotificationChannel.DEFAULT_CHANNEL_ID, false); - defaultChannel.setImportance(IMPORTANCE_LOW); - mHelper.updateNotificationChannel(pkg, uid, defaultChannel); + final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG, UID, + NotificationChannel.DEFAULT_CHANNEL_ID, false); + defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW); + mHelper.updateNotificationChannel(PKG, UID, defaultChannel); - ByteArrayOutputStream baos = writeXmlAndPurge(pkg, uid, false, channel1.getId(), + ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false, channel1.getId(), NotificationChannel.DEFAULT_CHANNEL_ID); - XmlPullParser parser = Xml.newPullParser(); - parser.setInput(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())), - null); - parser.nextTag(); - mHelper.readXml(parser, false); + loadStreamXml(baos); - assertEquals(IMPORTANCE_LOW, mHelper.getNotificationChannel( - pkg, uid, NotificationChannel.DEFAULT_CHANNEL_ID, false).getImportance()); + assertEquals(NotificationManager.IMPORTANCE_LOW, mHelper.getNotificationChannel( + PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false).getImportance()); } @Test public void testChannelXml_upgradeCreateDefaultChannel() throws Exception { final String preupgradeXml = "<ranking version=\"1\">\n" - + "<package name=\"" + pkg + "\" importance=\"" - + NotificationManager.IMPORTANCE_HIGH + + "<package name=\"" + PKG + + "\" importance=\"" + NotificationManager.IMPORTANCE_HIGH + "\" priority=\"" + Notification.PRIORITY_MAX + "\" visibility=\"" - + Notification.VISIBILITY_SECRET + "\"" + " uid=\"" + uid + "\" />\n" - + "<package name=\"" + pkg2 + "\" uid=\"" + uid2 + "\" visibility=\"" + + Notification.VISIBILITY_SECRET + "\"" +" uid=\"" + UID + "\" />\n" + + "<package name=\"" + UPDATED_PKG + "\" uid=\"" + UID2 + "\" visibility=\"" + Notification.VISIBILITY_PRIVATE + "\" />\n" + "</ranking>"; XmlPullParser parser = Xml.newPullParser(); @@ -441,30 +430,69 @@ public class RankingHelperTest { parser.nextTag(); mHelper.readXml(parser, false); - final NotificationChannel updated1 = mHelper.getNotificationChannel( - pkg, uid, NotificationChannel.DEFAULT_CHANNEL_ID, false); + final NotificationChannel updated1 = + mHelper.getNotificationChannel(PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false); assertEquals(NotificationManager.IMPORTANCE_HIGH, updated1.getImportance()); assertTrue(updated1.canBypassDnd()); assertEquals(Notification.VISIBILITY_SECRET, updated1.getLockscreenVisibility()); assertEquals(NotificationChannel.USER_LOCKED_IMPORTANCE | NotificationChannel.USER_LOCKED_PRIORITY - | NotificationChannel.USER_LOCKED_VISIBILITY, updated1.getUserLockedFields()); - - final NotificationChannel updated2 = mHelper.getNotificationChannel( - pkg2, uid2, NotificationChannel.DEFAULT_CHANNEL_ID, false); - // clamped - assertEquals(IMPORTANCE_LOW, updated2.getImportance()); - assertFalse(updated2.canBypassDnd()); - assertEquals(Notification.VISIBILITY_PRIVATE, updated2.getLockscreenVisibility()); - assertEquals(NotificationChannel.USER_LOCKED_VISIBILITY, updated2.getUserLockedFields()); + | NotificationChannel.USER_LOCKED_VISIBILITY, + updated1.getUserLockedFields()); + + // STOPSHIP - this should be reversed after the STOPSHIP is removed in the tested code. + // No Default Channel created for updated packages + // assertEquals(null, mHelper.getNotificationChannel(UPDATED_PKG, UID2, + // NotificationChannel.DEFAULT_CHANNEL_ID, false)); + assertTrue(mHelper.getNotificationChannel(UPDATED_PKG, UID2, + NotificationChannel.DEFAULT_CHANNEL_ID, false) != null); + } + + @Test + public void testChannelXml_upgradeDeletesDefaultChannel() throws Exception { + final NotificationChannel defaultChannel = mHelper.getNotificationChannel( + PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false); + assertTrue(defaultChannel != null); + ByteArrayOutputStream baos = + writeXmlAndPurge(PKG, UID, false, NotificationChannel.DEFAULT_CHANNEL_ID); + // Load package at higher sdk. + final ApplicationInfo upgraded = new ApplicationInfo(); + upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1; + when(mPm.getApplicationInfoAsUser(eq(PKG), anyInt(), anyInt())).thenReturn(upgraded); + loadStreamXml(baos); + + // STOPSHIP - this should be reversed after the STOPSHIP is removed in the tested code. + // Default Channel should be gone. + // assertEquals(null, mHelper.getNotificationChannel(PKG, UID, + // NotificationChannel.DEFAULT_CHANNEL_ID, false)); + assertTrue(mHelper.getNotificationChannel(UPDATED_PKG, UID2, + NotificationChannel.DEFAULT_CHANNEL_ID, false) != null); + } + + @Test + public void testDeletesDefaultChannelAfterChannelIsCreated() throws Exception { + mHelper.createNotificationChannel(PKG, UID, + new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true); + ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false, + NotificationChannel.DEFAULT_CHANNEL_ID, "bananas"); + + // Load package at higher sdk. + final ApplicationInfo upgraded = new ApplicationInfo(); + upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1; + when(mPm.getApplicationInfoAsUser(eq(PKG), anyInt(), anyInt())).thenReturn(upgraded); + loadStreamXml(baos); + + // Default Channel should be gone. + assertEquals(null, mHelper.getNotificationChannel(PKG, UID, + NotificationChannel.DEFAULT_CHANNEL_ID, false)); } @Test public void testCreateChannel_blocked() throws Exception { - mHelper.setImportance(pkg, uid, NotificationManager.IMPORTANCE_NONE); + mHelper.setImportance(PKG, UID, NotificationManager.IMPORTANCE_NONE); - mHelper.createNotificationChannel(pkg, uid, - new NotificationChannel(pkg, "bananas", IMPORTANCE_LOW), true); + mHelper.createNotificationChannel(PKG, UID, + new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true); } @Test @@ -474,16 +502,16 @@ public class RankingHelperTest { new NotificationChannel("id2", "name2", IMPORTANCE_LOW); channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); - mHelper.createNotificationChannel(pkg, uid, channel, false); + mHelper.createNotificationChannel(PKG, UID, channel, false); // same id, try to update final NotificationChannel channel2 = new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH); - mHelper.updateNotificationChannelFromAssistant(pkg, uid, channel2); + mHelper.updateNotificationChannelFromAssistant(PKG, UID, channel2); // no fields should be changed - assertEquals(channel, mHelper.getNotificationChannel(pkg, uid, channel.getId(), false)); + assertEquals(channel, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false)); } @Test @@ -494,17 +522,17 @@ public class RankingHelperTest { channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); channel.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY); - mHelper.createNotificationChannel(pkg, uid, channel, false); + mHelper.createNotificationChannel(PKG, UID, channel, false); // same id, try to update final NotificationChannel channel2 = new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH); channel2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); - mHelper.updateNotificationChannelFromAssistant(pkg, uid, channel2); + mHelper.updateNotificationChannelFromAssistant(PKG, UID, channel2); // no fields should be changed - assertEquals(channel, mHelper.getNotificationChannel(pkg, uid, channel.getId(), false)); + assertEquals(channel, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false)); } @Test @@ -515,7 +543,7 @@ public class RankingHelperTest { channel.enableLights(false); channel.lockFields(NotificationChannel.USER_LOCKED_VIBRATION); - mHelper.createNotificationChannel(pkg, uid, channel, false); + mHelper.createNotificationChannel(PKG, UID, channel, false); // same id, try to update final NotificationChannel channel2 = @@ -523,10 +551,10 @@ public class RankingHelperTest { channel2.enableVibration(true); channel2.setVibrationPattern(new long[]{100}); - mHelper.updateNotificationChannelFromAssistant(pkg, uid, channel2); + mHelper.updateNotificationChannelFromAssistant(PKG, UID, channel2); // no fields should be changed - assertEquals(channel, mHelper.getNotificationChannel(pkg, uid, channel.getId(), false)); + assertEquals(channel, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false)); } @Test @@ -537,17 +565,17 @@ public class RankingHelperTest { channel.enableLights(false); channel.lockFields(NotificationChannel.USER_LOCKED_LIGHTS); - mHelper.createNotificationChannel(pkg, uid, channel, false); + mHelper.createNotificationChannel(PKG, UID, channel, false); // same id, try to update final NotificationChannel channel2 = new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH); channel2.enableLights(true); - mHelper.updateNotificationChannelFromAssistant(pkg, uid, channel2); + mHelper.updateNotificationChannelFromAssistant(PKG, UID, channel2); // no fields should be changed - assertEquals(channel, mHelper.getNotificationChannel(pkg, uid, channel.getId(), false)); + assertEquals(channel, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false)); } @Test @@ -558,17 +586,17 @@ public class RankingHelperTest { channel.setBypassDnd(true); channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); - mHelper.createNotificationChannel(pkg, uid, channel, false); + mHelper.createNotificationChannel(PKG, UID, channel, false); // same id, try to update all fields final NotificationChannel channel2 = new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH); channel2.setBypassDnd(false); - mHelper.updateNotificationChannelFromAssistant(pkg, uid, channel2); + mHelper.updateNotificationChannelFromAssistant(PKG, UID, channel2); // no fields should be changed - assertEquals(channel, mHelper.getNotificationChannel(pkg, uid, channel.getId(), false)); + assertEquals(channel, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false)); } @Test @@ -579,17 +607,17 @@ public class RankingHelperTest { channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); channel.lockFields(NotificationChannel.USER_LOCKED_SOUND); - mHelper.createNotificationChannel(pkg, uid, channel, false); + mHelper.createNotificationChannel(PKG, UID, channel, false); // same id, try to update all fields final NotificationChannel channel2 = new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH); channel2.setSound(new Uri.Builder().scheme("test2").build(), mAudioAttributes); - mHelper.updateNotificationChannelFromAssistant(pkg, uid, channel2); + mHelper.updateNotificationChannelFromAssistant(PKG, UID, channel2); // no fields should be changed - assertEquals(channel, mHelper.getNotificationChannel(pkg, uid, channel.getId(), false)); + assertEquals(channel, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false)); } @Test @@ -599,16 +627,16 @@ public class RankingHelperTest { channel.setShowBadge(true); channel.lockFields(NotificationChannel.USER_LOCKED_SHOW_BADGE); - mHelper.createNotificationChannel(pkg, uid, channel, false); + mHelper.createNotificationChannel(PKG, UID, channel, false); final NotificationChannel channel2 = new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH); channel2.setShowBadge(false); - mHelper.updateNotificationChannelFromAssistant(pkg, uid, channel2); + mHelper.updateNotificationChannelFromAssistant(PKG, UID, channel2); // no fields should be changed - assertEquals(channel, mHelper.getNotificationChannel(pkg, uid, channel.getId(), false)); + assertEquals(channel, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false)); } @Test @@ -621,7 +649,7 @@ public class RankingHelperTest { channel.setBypassDnd(true); channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); - mHelper.createNotificationChannel(pkg, uid, channel, false); + mHelper.createNotificationChannel(PKG, UID, channel, false); // same id, try to update all fields final NotificationChannel channel2 = @@ -631,17 +659,15 @@ public class RankingHelperTest { channel2.setBypassDnd(false); channel2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); - mHelper.updateNotificationChannel(pkg, uid, channel2); + mHelper.updateNotificationChannel(PKG, UID, channel2); // all fields should be changed - assertEquals(channel2, mHelper.getNotificationChannel(pkg, uid, channel.getId(), false)); + assertEquals(channel2, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false)); } @Test - public void testGetChannelWithFallback() throws Exception { - NotificationChannel channel = - mHelper.getNotificationChannelWithFallback(pkg, uid, "garbage", false); - assertEquals(NotificationChannel.DEFAULT_CHANNEL_ID, channel.getId()); + public void testGetNotificationChannel_ReturnsNullForUnknownChannel() throws Exception { + assertEquals(null, mHelper.getNotificationChannel(PKG, UID, "garbage", false)); } @Test @@ -659,10 +685,10 @@ public class RankingHelperTest { } channel.lockFields(lockMask); - mHelper.createNotificationChannel(pkg, uid, channel, true); + mHelper.createNotificationChannel(PKG, UID, channel, true); NotificationChannel savedChannel = - mHelper.getNotificationChannel(pkg, uid, channel.getId(), false); + mHelper.getNotificationChannel(PKG, UID, channel.getId(), false); assertEquals(channel.getName(), savedChannel.getName()); assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights()); @@ -686,10 +712,10 @@ public class RankingHelperTest { } channel.lockFields(lockMask); - mHelper.createNotificationChannel(pkg, uid, channel, true); + mHelper.createNotificationChannel(PKG, UID, channel, true); NotificationChannel savedChannel = - mHelper.getNotificationChannel(pkg, uid, channel.getId(), false); + mHelper.getNotificationChannel(PKG, UID, channel.getId(), false); assertEquals(channel.getName(), savedChannel.getName()); assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights()); @@ -709,16 +735,16 @@ public class RankingHelperTest { channel.enableVibration(true); channel.setVibrationPattern(new long[]{100, 67, 145, 156}); - mHelper.createNotificationChannel(pkg, uid, channel, true); - mHelper.deleteNotificationChannel(pkg, uid, channel.getId()); + mHelper.createNotificationChannel(PKG, UID, channel, true); + mHelper.deleteNotificationChannel(PKG, UID, channel.getId()); // Does not return deleted channel NotificationChannel response = - mHelper.getNotificationChannel(pkg, uid, channel.getId(), false); + mHelper.getNotificationChannel(PKG, UID, channel.getId(), false); assertNull(response); // Returns deleted channel - response = mHelper.getNotificationChannel(pkg, uid, channel.getId(), true); + response = mHelper.getNotificationChannel(PKG, UID, channel.getId(), true); compareChannels(channel, response); assertTrue(response.isDeleted()); } @@ -738,14 +764,14 @@ public class RankingHelperTest { NotificationChannel channel2 = new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH); channelMap.put(channel2.getId(), channel2); - mHelper.createNotificationChannel(pkg, uid, channel, true); - mHelper.createNotificationChannel(pkg, uid, channel2, true); + mHelper.createNotificationChannel(PKG, UID, channel, true); + mHelper.createNotificationChannel(PKG, UID, channel2, true); - mHelper.deleteNotificationChannel(pkg, uid, channel.getId()); + mHelper.deleteNotificationChannel(PKG, UID, channel.getId()); // Returns only non-deleted channels List<NotificationChannel> channels = - mHelper.getNotificationChannels(pkg, uid, false).getList(); + 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())) { @@ -754,7 +780,7 @@ public class RankingHelperTest { } // Returns deleted channels too - channels = mHelper.getNotificationChannels(pkg, uid, true).getList(); + channels = mHelper.getNotificationChannels(PKG, UID, true).getList(); assertEquals(3, channels.size()); // Includes default channel for (NotificationChannel nc : channels) { if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) { @@ -771,35 +797,35 @@ public class RankingHelperTest { new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH); NotificationChannel channel3 = new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH); - mHelper.createNotificationChannel(pkg, uid, channel, true); - mHelper.createNotificationChannel(pkg, uid, channel2, true); - mHelper.createNotificationChannel(pkg, uid, channel3, true); + mHelper.createNotificationChannel(PKG, UID, channel, true); + mHelper.createNotificationChannel(PKG, UID, channel2, true); + mHelper.createNotificationChannel(PKG, UID, channel3, true); - mHelper.deleteNotificationChannel(pkg, uid, channel.getId()); - mHelper.deleteNotificationChannel(pkg, uid, channel3.getId()); + mHelper.deleteNotificationChannel(PKG, UID, channel.getId()); + mHelper.deleteNotificationChannel(PKG, UID, channel3.getId()); - assertEquals(2, mHelper.getDeletedChannelCount(pkg, uid)); - assertEquals(0, mHelper.getDeletedChannelCount(pkg2, uid2)); + assertEquals(2, mHelper.getDeletedChannelCount(PKG, UID)); + assertEquals(0, mHelper.getDeletedChannelCount("pkg2", UID2)); } @Test public void testUpdateDeletedChannels() throws Exception { NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); - mHelper.createNotificationChannel(pkg, uid, channel, true); + mHelper.createNotificationChannel(PKG, UID, channel, true); - mHelper.deleteNotificationChannel(pkg, uid, channel.getId()); + mHelper.deleteNotificationChannel(PKG, UID, channel.getId()); channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); try { - mHelper.updateNotificationChannel(pkg, uid, channel); + mHelper.updateNotificationChannel(PKG, UID, channel); fail("Updated deleted channel"); } catch (IllegalArgumentException e) { // :) } try { - mHelper.updateNotificationChannelFromAssistant(pkg, uid, channel); + mHelper.updateNotificationChannelFromAssistant(PKG, UID, channel); fail("Updated deleted channel"); } catch (IllegalArgumentException e) { // :) @@ -813,24 +839,24 @@ public class RankingHelperTest { new NotificationChannel("id2", "name2", IMPORTANCE_LOW); channel.setVibrationPattern(vibration); - mHelper.createNotificationChannel(pkg, uid, channel, true); - mHelper.deleteNotificationChannel(pkg, uid, channel.getId()); + mHelper.createNotificationChannel(PKG, UID, channel, true); + mHelper.deleteNotificationChannel(PKG, UID, channel.getId()); NotificationChannel newChannel = new NotificationChannel( channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH); newChannel.setVibrationPattern(new long[]{100}); - mHelper.createNotificationChannel(pkg, uid, newChannel, true); + mHelper.createNotificationChannel(PKG, UID, newChannel, true); // No long deleted, using old settings compareChannels(channel, - mHelper.getNotificationChannel(pkg, uid, newChannel.getId(), false)); + mHelper.getNotificationChannel(PKG, UID, newChannel.getId(), false)); } @Test public void testCreateChannel_defaultChannelId() throws Exception { try { - mHelper.createNotificationChannel(pkg2, uid2, new NotificationChannel( + mHelper.createNotificationChannel(PKG, UID, new NotificationChannel( NotificationChannel.DEFAULT_CHANNEL_ID, "ha", IMPORTANCE_HIGH), true); fail("Allowed to create default channel"); } catch (IllegalArgumentException e) { @@ -845,26 +871,26 @@ public class RankingHelperTest { new NotificationChannel("id2", "name2", IMPORTANCE_LOW); channel.setVibrationPattern(vibration); - mHelper.createNotificationChannel(pkg, uid, channel, true); + mHelper.createNotificationChannel(PKG, UID, channel, true); NotificationChannel newChannel = new NotificationChannel( channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH); newChannel.setVibrationPattern(new long[]{100}); - mHelper.createNotificationChannel(pkg, uid, newChannel, true); + mHelper.createNotificationChannel(PKG, UID, newChannel, true); // Old settings not overridden compareChannels(channel, - mHelper.getNotificationChannel(pkg, uid, newChannel.getId(), false)); + mHelper.getNotificationChannel(PKG, UID, newChannel.getId(), false)); } @Test public void testCreateChannel_addMissingSound() throws Exception { final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); - mHelper.createNotificationChannel(pkg, uid, channel, true); + mHelper.createNotificationChannel(PKG, UID, channel, true); assertNotNull(mHelper.getNotificationChannel( - pkg, uid, channel.getId(), false).getSound()); + PKG, UID, channel.getId(), false).getSound()); } @Test @@ -873,9 +899,9 @@ public class RankingHelperTest { final NotificationChannel channel = new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_DEFAULT); channel.setSound(sound, mAudioAttributes); - mHelper.createNotificationChannel(pkg, uid, channel, true); + mHelper.createNotificationChannel(PKG, UID, channel, true); assertEquals(sound, mHelper.getNotificationChannel( - pkg, uid, channel.getId(), false).getSound()); + PKG, UID, channel.getId(), false).getSound()); } @Test @@ -885,13 +911,13 @@ public class RankingHelperTest { NotificationChannel channel2 = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); - mHelper.createNotificationChannel(pkg, uid, channel1, true); - mHelper.createNotificationChannel(pkg, uid, channel2, false); + mHelper.createNotificationChannel(PKG, UID, channel1, true); + mHelper.createNotificationChannel(PKG, UID, channel2, false); - mHelper.permanentlyDeleteNotificationChannels(pkg, uid); + mHelper.permanentlyDeleteNotificationChannels(PKG, UID); // Only default channel remains - assertEquals(1, mHelper.getNotificationChannels(pkg, uid, true).getList().size()); + assertEquals(1, mHelper.getNotificationChannels(PKG, UID, true).getList().size()); } @Test @@ -907,28 +933,28 @@ public class RankingHelperTest { new NotificationChannel("deleted", "belongs to deleted", IMPORTANCE_DEFAULT); groupedAndDeleted.setGroup("totally"); - mHelper.createNotificationChannelGroup(pkg, uid, notDeleted, true); - mHelper.createNotificationChannelGroup(pkg, uid, deleted, true); - mHelper.createNotificationChannel(pkg, uid, nonGroupedNonDeletedChannel, true); - mHelper.createNotificationChannel(pkg, uid, groupedAndDeleted, true); - mHelper.createNotificationChannel(pkg, uid, groupedButNotDeleted, true); + mHelper.createNotificationChannelGroup(PKG, UID, notDeleted, true); + mHelper.createNotificationChannelGroup(PKG, UID, deleted, true); + mHelper.createNotificationChannel(PKG, UID, nonGroupedNonDeletedChannel, true); + mHelper.createNotificationChannel(PKG, UID, groupedAndDeleted, true); + mHelper.createNotificationChannel(PKG, UID, groupedButNotDeleted, true); - mHelper.deleteNotificationChannelGroup(pkg, uid, deleted.getId()); + mHelper.deleteNotificationChannelGroup(PKG, UID, deleted.getId()); - assertNull(mHelper.getNotificationChannelGroup(deleted.getId(), pkg, uid)); - assertNotNull(mHelper.getNotificationChannelGroup(notDeleted.getId(), pkg, uid)); + assertNull(mHelper.getNotificationChannelGroup(deleted.getId(), PKG, UID)); + assertNotNull(mHelper.getNotificationChannelGroup(notDeleted.getId(), PKG, UID)); - assertNull(mHelper.getNotificationChannel(pkg, uid, groupedAndDeleted.getId(), false)); + assertNull(mHelper.getNotificationChannel(PKG, UID, groupedAndDeleted.getId(), false)); compareChannels(groupedAndDeleted, - mHelper.getNotificationChannel(pkg, uid, groupedAndDeleted.getId(), true)); + mHelper.getNotificationChannel(PKG, UID, groupedAndDeleted.getId(), true)); compareChannels(groupedButNotDeleted, - mHelper.getNotificationChannel(pkg, uid, groupedButNotDeleted.getId(), false)); + mHelper.getNotificationChannel(PKG, UID, groupedButNotDeleted.getId(), false)); compareChannels(nonGroupedNonDeletedChannel, mHelper.getNotificationChannel( - pkg, uid, nonGroupedNonDeletedChannel.getId(), false)); + PKG, UID, nonGroupedNonDeletedChannel.getId(), false)); // notDeleted - assertEquals(1, mHelper.getNotificationChannelGroups(pkg, uid).size()); + assertEquals(1, mHelper.getNotificationChannelGroups(PKG, UID).size()); } @Test @@ -936,52 +962,52 @@ public class RankingHelperTest { // Deleted NotificationChannel channel1 = new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); - mHelper.createNotificationChannel(pkg, uid, channel1, true); + mHelper.createNotificationChannel(PKG, UID, channel1, true); - mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{pkg}, new int[]{uid}); + mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID}); - assertEquals(0, mHelper.getNotificationChannels(pkg, uid, true).getList().size()); + assertEquals(0, mHelper.getNotificationChannels(PKG, UID, true).getList().size()); // Not deleted - mHelper.createNotificationChannel(pkg, uid, channel1, true); + 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()); + mHelper.onPackagesChanged(false, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID}); + assertEquals(2, mHelper.getNotificationChannels(PKG, UID, false).getList().size()); } @Test public void testOnPackageChanged_packageRemoval_importance() throws Exception { - mHelper.setImportance(pkg, uid, NotificationManager.IMPORTANCE_HIGH); + mHelper.setImportance(PKG, UID, NotificationManager.IMPORTANCE_HIGH); - mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{pkg}, new int[]{uid}); + mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID}); - assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(pkg, uid)); + assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG, UID)); } @Test public void testOnPackageChanged_packageRemoval_groups() throws Exception { NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); - mHelper.createNotificationChannelGroup(pkg, uid, ncg, true); + mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2"); - mHelper.createNotificationChannelGroup(pkg, uid, ncg2, true); + mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true); - mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{pkg}, new int[]{uid}); + mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID}); - assertEquals(0, mHelper.getNotificationChannelGroups(pkg, uid).size()); + assertEquals(0, mHelper.getNotificationChannelGroups(PKG, UID, true).getList().size()); } @Test public void testRecordDefaults() throws Exception { - assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(pkg, uid)); - assertEquals(true, mHelper.canShowBadge(pkg, uid)); - assertEquals(1, mHelper.getNotificationChannels(pkg, uid, false).getList().size()); + assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG, UID)); + assertEquals(true, mHelper.canShowBadge(PKG, UID)); + assertEquals(1, mHelper.getNotificationChannels(PKG, UID, false).getList().size()); } @Test public void testCreateGroup() throws Exception { NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); - mHelper.createNotificationChannelGroup(pkg, uid, ncg, true); - assertEquals(ncg, mHelper.getNotificationChannelGroups(pkg, uid).iterator().next()); + mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); + assertEquals(ncg, mHelper.getNotificationChannelGroups(PKG, UID).iterator().next()); } @Test @@ -990,7 +1016,7 @@ public class RankingHelperTest { new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); channel1.setGroup("garbage"); try { - mHelper.createNotificationChannel(pkg, uid, channel1, true); + mHelper.createNotificationChannel(PKG, UID, channel1, true); fail("Created a channel with a bad group"); } catch (IllegalArgumentException e) { } @@ -999,45 +1025,45 @@ public class RankingHelperTest { @Test public void testCannotCreateChannel_goodGroup() throws Exception { NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); - mHelper.createNotificationChannelGroup(pkg, uid, ncg, true); + mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); NotificationChannel channel1 = new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); channel1.setGroup(ncg.getId()); - mHelper.createNotificationChannel(pkg, uid, channel1, true); + mHelper.createNotificationChannel(PKG, UID, channel1, true); assertEquals(ncg.getId(), - mHelper.getNotificationChannel(pkg, uid, channel1.getId(), false).getGroup()); + mHelper.getNotificationChannel(PKG, UID, channel1.getId(), false).getGroup()); } @Test public void testGetChannelGroups() throws Exception { NotificationChannelGroup unused = new NotificationChannelGroup("unused", "s"); - mHelper.createNotificationChannelGroup(pkg, uid, unused, true); + mHelper.createNotificationChannelGroup(PKG, UID, unused, true); NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); - mHelper.createNotificationChannelGroup(pkg, uid, ncg, true); + mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2"); - mHelper.createNotificationChannelGroup(pkg, uid, ncg2, true); + mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true); NotificationChannel channel1 = new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); channel1.setGroup(ncg.getId()); - mHelper.createNotificationChannel(pkg, uid, channel1, true); + mHelper.createNotificationChannel(PKG, UID, channel1, true); NotificationChannel channel1a = new NotificationChannel("id1a", "name1", NotificationManager.IMPORTANCE_HIGH); channel1a.setGroup(ncg.getId()); - mHelper.createNotificationChannel(pkg, uid, channel1a, true); + mHelper.createNotificationChannel(PKG, UID, channel1a, true); NotificationChannel channel2 = new NotificationChannel("id2", "name1", NotificationManager.IMPORTANCE_HIGH); channel2.setGroup(ncg2.getId()); - mHelper.createNotificationChannel(pkg, uid, channel2, true); + mHelper.createNotificationChannel(PKG, UID, channel2, true); NotificationChannel channel3 = new NotificationChannel("id3", "name1", NotificationManager.IMPORTANCE_HIGH); - mHelper.createNotificationChannel(pkg, uid, channel3, true); + mHelper.createNotificationChannel(PKG, UID, channel3, true); List<NotificationChannelGroup> actual = - mHelper.getNotificationChannelGroups(pkg, uid, true).getList(); + mHelper.getNotificationChannelGroups(PKG, UID, true).getList(); assertEquals(3, actual.size()); for (NotificationChannelGroup group : actual) { if (group.getId() == null) { @@ -1063,19 +1089,19 @@ public class RankingHelperTest { @Test public void testGetChannelGroups_noSideEffects() throws Exception { NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); - mHelper.createNotificationChannelGroup(pkg, uid, ncg, true); + mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); NotificationChannel channel1 = new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); channel1.setGroup(ncg.getId()); - mHelper.createNotificationChannel(pkg, uid, channel1, true); - mHelper.getNotificationChannelGroups(pkg, uid, true).getList(); + mHelper.createNotificationChannel(PKG, UID, channel1, true); + mHelper.getNotificationChannelGroups(PKG, UID, true).getList(); channel1.setImportance(IMPORTANCE_LOW); - mHelper.updateNotificationChannel(pkg, uid, channel1); + mHelper.updateNotificationChannel(PKG, UID, channel1); List<NotificationChannelGroup> actual = - mHelper.getNotificationChannelGroups(pkg, uid, true).getList(); + mHelper.getNotificationChannelGroups(PKG, UID, true).getList(); assertEquals(2, actual.size()); for (NotificationChannelGroup group : actual) { @@ -1088,14 +1114,14 @@ public class RankingHelperTest { @Test public void testCreateChannel_updateName() throws Exception { NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT); - mHelper.createNotificationChannel(pkg, uid, nc, true); - NotificationChannel actual = mHelper.getNotificationChannel(pkg, uid, "id", false); + mHelper.createNotificationChannel(PKG, UID, nc, true); + NotificationChannel actual = mHelper.getNotificationChannel(PKG, UID, "id", false); assertEquals("hello", actual.getName()); nc = new NotificationChannel("id", "goodbye", IMPORTANCE_HIGH); - mHelper.createNotificationChannel(pkg, uid, nc, true); + mHelper.createNotificationChannel(PKG, UID, nc, true); - actual = mHelper.getNotificationChannel(pkg, uid, "id", false); + actual = mHelper.getNotificationChannel(PKG, UID, "id", false); assertEquals("goodbye", actual.getName()); assertEquals(IMPORTANCE_DEFAULT, actual.getImportance()); } @@ -1115,7 +1141,7 @@ public class RankingHelperTest { String pkgName = "pkg" + i; int numChannels = ThreadLocalRandom.current().nextInt(1, 10); for (int j = 0; j < numChannels; j++) { - mHelper.createNotificationChannel(pkgName, uid, + mHelper.createNotificationChannel(pkgName, UID, new NotificationChannel("" + j, "a", IMPORTANCE_HIGH), true); } expectedChannels.put(pkgName, numChannels); @@ -1123,7 +1149,7 @@ public class RankingHelperTest { // delete the first channel of the first package String pkg = expectedChannels.keyAt(0); - mHelper.deleteNotificationChannel("pkg" + 0, uid, "0"); + mHelper.deleteNotificationChannel("pkg" + 0, UID, "0"); // dump should not include deleted channels int count = expectedChannels.get(pkg); expectedChannels.put(pkg, count - 1); |