diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index ae0f7ed5bdde2..b7247166855d1 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -2299,8 +2299,9 @@ public class NotificationManagerService extends SystemService { public ParceledListSlice getNotificationChannelGroups( String pkg) { checkCallerIsSystemOrSameApp(pkg); + return mRankingHelper.getNotificationChannelGroups( - pkg, Binder.getCallingUid(), false, false); + pkg, Binder.getCallingUid(), false, false, true); } @Override @@ -2376,7 +2377,9 @@ public class NotificationManagerService extends SystemService { public ParceledListSlice getNotificationChannelGroupsForPackage( String pkg, int uid, boolean includeDeleted) { checkCallerIsSystem(); - return mRankingHelper.getNotificationChannelGroups(pkg, uid, includeDeleted, true); + + return mRankingHelper.getNotificationChannelGroups( + pkg, uid, includeDeleted, true, false); } @Override diff --git a/services/core/java/com/android/server/notification/RankingConfig.java b/services/core/java/com/android/server/notification/RankingConfig.java index af6468333ba00..605348b4d52d7 100644 --- a/services/core/java/com/android/server/notification/RankingConfig.java +++ b/services/core/java/com/android/server/notification/RankingConfig.java @@ -36,7 +36,7 @@ public interface RankingConfig { void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group, boolean fromTargetApp); ParceledListSlice getNotificationChannelGroups(String pkg, - int uid, boolean includeDeleted, boolean includeNonGrouped); + int uid, boolean includeDeleted, boolean includeNonGrouped, boolean includeEmpty); void createNotificationChannel(String pkg, int uid, NotificationChannel channel, boolean fromTargetApp, boolean hasDndAccess); void updateNotificationChannel(String pkg, int uid, NotificationChannel channel, boolean fromUser); diff --git a/services/core/java/com/android/server/notification/RankingHelper.java b/services/core/java/com/android/server/notification/RankingHelper.java index 61b5415ec7a38..c96894be3f938 100644 --- a/services/core/java/com/android/server/notification/RankingHelper.java +++ b/services/core/java/com/android/server/notification/RankingHelper.java @@ -830,7 +830,7 @@ public class RankingHelper implements RankingConfig { @Override public ParceledListSlice getNotificationChannelGroups(String pkg, - int uid, boolean includeDeleted, boolean includeNonGrouped) { + int uid, boolean includeDeleted, boolean includeNonGrouped, boolean includeEmpty) { Preconditions.checkNotNull(pkg); Map groups = new ArrayMap<>(); Record r = getRecord(pkg, uid); @@ -861,6 +861,13 @@ public class RankingHelper implements RankingConfig { if (includeNonGrouped && nonGrouped.getChannels().size() > 0) { groups.put(null, nonGrouped); } + if (includeEmpty) { + for (NotificationChannelGroup group : r.groups.values()) { + if (!groups.containsKey(group.getId())) { + groups.put(group.getId(), group); + } + } + } return new ParceledListSlice<>(new ArrayList<>(groups.values())); } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/RankingHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/RankingHelperTest.java index 98c6ec42207fe..a9e713e5778b8 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/RankingHelperTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/RankingHelperTest.java @@ -399,7 +399,7 @@ public class RankingHelperTest extends UiServiceTestCase { mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false)); List actualGroups = - mHelper.getNotificationChannelGroups(PKG, UID, false, true).getList(); + mHelper.getNotificationChannelGroups(PKG, UID, false, true, false).getList(); boolean foundNcg = false; for (NotificationChannelGroup actual : actualGroups) { if (ncg.getId().equals(actual.getId())) { @@ -469,7 +469,7 @@ public class RankingHelperTest extends UiServiceTestCase { mHelper.getNotificationChannel(PKG, UID, channel3.getId(), false)); List actualGroups = - mHelper.getNotificationChannelGroups(PKG, UID, false, true).getList(); + mHelper.getNotificationChannelGroups(PKG, UID, false, true, false).getList(); boolean foundNcg = false; for (NotificationChannelGroup actual : actualGroups) { if (ncg.getId().equals(actual.getId())) { @@ -784,6 +784,31 @@ public class RankingHelperTest extends UiServiceTestCase { new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX), true, false); } + @Test + public void testGetChannelGroups_includeEmptyGroups() { + NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); + mHelper.createNotificationChannelGroup(PKG, UID, ncg, true); + NotificationChannelGroup ncgEmpty = new NotificationChannelGroup("group2", "name2"); + mHelper.createNotificationChannelGroup(PKG, UID, ncgEmpty, true); + + NotificationChannel channel1 = + new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); + channel1.setGroup(ncg.getId()); + mHelper.createNotificationChannel(PKG, UID, channel1, true, false); + + List actual = mHelper.getNotificationChannelGroups( + PKG, UID, false, false, true).getList(); + + assertEquals(2, actual.size()); + for (NotificationChannelGroup group : actual) { + if (Objects.equals(group.getId(), ncg.getId())) { + assertEquals(1, group.getChannels().size()); + } + if (Objects.equals(group.getId(), ncgEmpty.getId())) { + assertEquals(0, group.getChannels().size()); + } + } + } @Test public void testUpdate() throws Exception { @@ -1421,7 +1446,7 @@ public class RankingHelperTest extends UiServiceTestCase { mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID}); assertEquals(0, - mHelper.getNotificationChannelGroups(PKG, UID, true, true).getList().size()); + mHelper.getNotificationChannelGroups(PKG, UID, true, true, false).getList().size()); } @Test @@ -1510,7 +1535,7 @@ public class RankingHelperTest extends UiServiceTestCase { mHelper.createNotificationChannel(PKG, UID, channel3, true, false); List actual = - mHelper.getNotificationChannelGroups(PKG, UID, true, true).getList(); + mHelper.getNotificationChannelGroups(PKG, UID, true, true, false).getList(); assertEquals(3, actual.size()); for (NotificationChannelGroup group : actual) { if (group.getId() == null) { @@ -1542,13 +1567,13 @@ public class RankingHelperTest extends UiServiceTestCase { new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); channel1.setGroup(ncg.getId()); mHelper.createNotificationChannel(PKG, UID, channel1, true, false); - mHelper.getNotificationChannelGroups(PKG, UID, true, true).getList(); + mHelper.getNotificationChannelGroups(PKG, UID, true, true, false).getList(); channel1.setImportance(IMPORTANCE_LOW); mHelper.updateNotificationChannel(PKG, UID, channel1, true); List actual = - mHelper.getNotificationChannelGroups(PKG, UID, true, true).getList(); + mHelper.getNotificationChannelGroups(PKG, UID, true, true, false).getList(); assertEquals(2, actual.size()); for (NotificationChannelGroup group : actual) {