From 5d1aca0f91c21975acf69a820731d656df81e6b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Hern=C3=A1ndez?= Date: Wed, 1 Mar 2023 19:10:57 +0100 Subject: [PATCH] Check channel group limit when creating from a NotificationListener Fixes: 268038726 Test: atest PreferencesHelperTest Change-Id: I048e221d5ebc14e856cd6e762ce80760ded3be20 --- .../server/notification/PreferencesHelper.java | 6 +++--- .../notification/PreferencesHelperTest.java | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/services/core/java/com/android/server/notification/PreferencesHelper.java b/services/core/java/com/android/server/notification/PreferencesHelper.java index c63cdddc85608..59af58fb0dfb2 100644 --- a/services/core/java/com/android/server/notification/PreferencesHelper.java +++ b/services/core/java/com/android/server/notification/PreferencesHelper.java @@ -862,11 +862,11 @@ public class PreferencesHelper implements RankingConfig { if (r == null) { throw new IllegalArgumentException("Invalid package"); } + if (r.groups.size() >= NOTIFICATION_CHANNEL_GROUP_COUNT_LIMIT) { + throw new IllegalStateException("Limit exceed; cannot create more groups"); + } if (fromTargetApp) { group.setBlocked(false); - if (r.groups.size() >= NOTIFICATION_CHANNEL_GROUP_COUNT_LIMIT) { - throw new IllegalStateException("Limit exceed; cannot create more groups"); - } } final NotificationChannelGroup oldGroup = r.groups.get(group.getId()); if (oldGroup != null) { diff --git a/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java index 1ecd4a1ffd7e0..79f69ee94a626 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java @@ -4131,17 +4131,26 @@ public class PreferencesHelperTest extends UiServiceTestCase { } @Test - public void testTooManyGroups() { + public void testTooManyGroups_fromTargetApp() { + testTooManyGroups(/* fromTargetApp= */ true); + } + + @Test + public void testTooManyGroups_fromListener() { + testTooManyGroups(/* fromTargetApp= */ false); + } + + private void testTooManyGroups(boolean fromTargetApp) { for (int i = 0; i < NOTIFICATION_CHANNEL_GROUP_COUNT_LIMIT; i++) { NotificationChannelGroup group = new NotificationChannelGroup(String.valueOf(i), String.valueOf(i)); - mHelper.createNotificationChannelGroup(PKG_O, UID_O, group, true); + mHelper.createNotificationChannelGroup(PKG_O, UID_O, group, fromTargetApp); } try { NotificationChannelGroup group = new NotificationChannelGroup( String.valueOf(NOTIFICATION_CHANNEL_GROUP_COUNT_LIMIT), String.valueOf(NOTIFICATION_CHANNEL_GROUP_COUNT_LIMIT)); - mHelper.createNotificationChannelGroup(PKG_O, UID_O, group, true); + mHelper.createNotificationChannelGroup(PKG_O, UID_O, group, fromTargetApp); fail("Allowed to create too many notification channel groups"); } catch (IllegalStateException e) { // great