Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fa8f37aa authored by Valentin Iftime's avatar Valentin Iftime
Browse files

Add GroupHelper sections for new notification categories

 Add support for auto-grouping for the new reserved notification channels.

Flag: android.service.notification.notification_force_grouping
Flag: android.service.notification.notification_classification

Test: atest GroupHelperTest

Bug: 336488844
Change-Id: I53001b1150ce3bf5f2a5462431d9febb17fa583e
parent 7eb10467
Loading
Loading
Loading
Loading
+27 −5
Original line number Diff line number Diff line
@@ -118,11 +118,32 @@ public class GroupHelper {
    private final ArrayMap<FullyQualifiedGroupKey, ArrayMap<String, NotificationAttributes>>
            mAggregatedNotifications = new ArrayMap<>();

    private static final List<NotificationSectioner> NOTIFICATION_SHADE_SECTIONS = List.of(
    private static List<NotificationSectioner> NOTIFICATION_SHADE_SECTIONS =
            getNotificationShadeSections();

    private static List<NotificationSectioner> getNotificationShadeSections() {
        if (android.service.notification.Flags.notificationClassification()) {
            return List.of(
                new NotificationSectioner("PromotionsSection", 0, (record) ->
                    NotificationChannel.PROMOTIONS_ID.equals(record.getChannel().getId())),
                new NotificationSectioner("SocialSection", 0, (record) ->
                    NotificationChannel.SOCIAL_MEDIA_ID.equals(record.getChannel().getId())),
                new NotificationSectioner("NewsSection", 0, (record) ->
                    NotificationChannel.NEWS_ID.equals(record.getChannel().getId())),
                new NotificationSectioner("RecsSection", 0, (record) ->
                    NotificationChannel.RECS_ID.equals(record.getChannel().getId())),
                new NotificationSectioner("AlertingSection", 0, (record) ->
                    record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT),
                new NotificationSectioner("SilentSection", 1, (record) ->
                    record.getImportance() < NotificationManager.IMPORTANCE_DEFAULT));
        } else {
            return List.of(
                new NotificationSectioner("AlertingSection", 0, (record) ->
                    record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT),
                new NotificationSectioner("SilentSection", 1, (record) ->
                    record.getImportance() < NotificationManager.IMPORTANCE_DEFAULT));
        }
    }

    public GroupHelper(Context context, PackageManager packageManager, int autoGroupAtCount,
            int autoGroupSparseGroupsAtCount, Callback callback) {
@@ -131,6 +152,7 @@ public class GroupHelper {
        mContext = context;
        mPackageManager = packageManager;
        mAutogroupSparseGroupsAtCount = autoGroupSparseGroupsAtCount;
        NOTIFICATION_SHADE_SECTIONS = getNotificationShadeSections();
    }

    private String generatePackageKey(int userId, String pkg) {
+97 −13
Original line number Diff line number Diff line
@@ -31,10 +31,11 @@ import static android.app.Notification.VISIBILITY_PUBLIC;
import static android.app.Notification.VISIBILITY_SECRET;
import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static android.app.NotificationManager.IMPORTANCE_LOW;
import static android.service.notification.Flags.FLAG_NOTIFICATION_CLASSIFICATION;
import static android.service.notification.Flags.FLAG_NOTIFICATION_FORCE_GROUPING;
import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
import static android.platform.test.flag.junit.SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT;

import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL;
import static com.android.server.notification.GroupHelper.AGGREGATE_GROUP_KEY;
import static com.android.server.notification.GroupHelper.AUTOGROUP_KEY;
import static com.android.server.notification.GroupHelper.BASE_FLAGS;
@@ -2518,17 +2519,7 @@ public class GroupHelperTest extends UiServiceTestCase {
        assertThat(cachedSummary).isNull();
    }

    @Test
    @EnableFlags(FLAG_NOTIFICATION_FORCE_GROUPING)
    public void testGroupSectioners() {
        final NotificationRecord notification_alerting = getNotificationRecord(mPkg, 0, "", mUser,
            "", false, IMPORTANCE_DEFAULT);
        assertThat(GroupHelper.getSection(notification_alerting).mName).isEqualTo("AlertingSection");

        final NotificationRecord notification_silent = getNotificationRecord(mPkg, 0, "", mUser,
            "", false, IMPORTANCE_LOW);
        assertThat(GroupHelper.getSection(notification_silent).mName).isEqualTo("SilentSection");

    private void checkNonGroupableNotifications() {
        NotificationRecord notification_conversation = mock(NotificationRecord.class);
        when(notification_conversation.isConversation()).thenReturn(true);
        assertThat(GroupHelper.getSection(notification_conversation)).isNull();
@@ -2558,4 +2549,97 @@ public class GroupHelperTest extends UiServiceTestCase {
        assertThat(GroupHelper.getSection(notification_colorFg)).isNull();
    }

    @Test
    @EnableFlags(FLAG_NOTIFICATION_FORCE_GROUPING)
    @DisableFlags(FLAG_NOTIFICATION_CLASSIFICATION)
    public void testGroupSectioners() {
        final NotificationRecord notification_alerting = getNotificationRecord(mPkg, 0, "", mUser,
                "", false, IMPORTANCE_DEFAULT);
        assertThat(GroupHelper.getSection(notification_alerting).mName).isEqualTo(
                "AlertingSection");

        final NotificationRecord notification_silent = getNotificationRecord(mPkg, 0, "", mUser,
                "", false, IMPORTANCE_LOW);
        assertThat(GroupHelper.getSection(notification_silent).mName).isEqualTo("SilentSection");

        // Check that special categories are grouped by their importance
        final NotificationChannel promoChannel = new NotificationChannel(
                NotificationChannel.PROMOTIONS_ID, NotificationChannel.PROMOTIONS_ID,
                IMPORTANCE_DEFAULT);
        final NotificationRecord notification_promotion = getNotificationRecord(mPkg, 0, "", mUser,
                "", false, promoChannel);
        assertThat(GroupHelper.getSection(notification_promotion).mName).isEqualTo(
                "AlertingSection");

        final NotificationChannel newsChannel = new NotificationChannel(NotificationChannel.NEWS_ID,
                NotificationChannel.NEWS_ID, IMPORTANCE_DEFAULT);
        final NotificationRecord notification_news = getNotificationRecord(mPkg, 0, "", mUser,
                "", false, newsChannel);
        assertThat(GroupHelper.getSection(notification_news).mName).isEqualTo(
                "AlertingSection");

        final NotificationChannel socialChannel = new NotificationChannel(
                NotificationChannel.SOCIAL_MEDIA_ID, NotificationChannel.SOCIAL_MEDIA_ID,
                IMPORTANCE_DEFAULT);
        final NotificationRecord notification_social = getNotificationRecord(mPkg, 0, "", mUser,
                "", false, socialChannel);
        assertThat(GroupHelper.getSection(notification_social).mName).isEqualTo(
                "AlertingSection");

        final NotificationChannel recsChannel = new NotificationChannel(NotificationChannel.RECS_ID,
                NotificationChannel.RECS_ID, IMPORTANCE_DEFAULT);
        final NotificationRecord notification_recs = getNotificationRecord(mPkg, 0, "", mUser,
                "", false, recsChannel);
        assertThat(GroupHelper.getSection(notification_recs).mName).isEqualTo(
                "AlertingSection");

        checkNonGroupableNotifications();
    }

    @Test
    @EnableFlags({FLAG_NOTIFICATION_FORCE_GROUPING, FLAG_NOTIFICATION_CLASSIFICATION})
    public void testGroupSectioners_withClassificationSections() {
        final NotificationRecord notification_alerting = getNotificationRecord(mPkg, 0, "", mUser,
                "", false, IMPORTANCE_DEFAULT);
        assertThat(GroupHelper.getSection(notification_alerting).mName).isEqualTo(
                "AlertingSection");

        final NotificationRecord notification_silent = getNotificationRecord(mPkg, 0, "", mUser,
                "", false, IMPORTANCE_LOW);
        assertThat(GroupHelper.getSection(notification_silent).mName).isEqualTo("SilentSection");

        // Check that special categories are grouped in their own sections
        final NotificationChannel promoChannel = new NotificationChannel(
                NotificationChannel.PROMOTIONS_ID, NotificationChannel.PROMOTIONS_ID,
                IMPORTANCE_DEFAULT);
        final NotificationRecord notification_promotion = getNotificationRecord(mPkg, 0, "", mUser,
                "", false, promoChannel);
        assertThat(GroupHelper.getSection(notification_promotion).mName).isEqualTo(
                "PromotionsSection");

        final NotificationChannel newsChannel = new NotificationChannel(NotificationChannel.NEWS_ID,
                NotificationChannel.NEWS_ID, IMPORTANCE_DEFAULT);
        final NotificationRecord notification_news = getNotificationRecord(mPkg, 0, "", mUser,
                "", false, newsChannel);
        assertThat(GroupHelper.getSection(notification_news).mName).isEqualTo(
                "NewsSection");

        final NotificationChannel socialChannel = new NotificationChannel(
                NotificationChannel.SOCIAL_MEDIA_ID, NotificationChannel.SOCIAL_MEDIA_ID,
                IMPORTANCE_DEFAULT);
        final NotificationRecord notification_social = getNotificationRecord(mPkg, 0, "", mUser,
                "", false, socialChannel);
        assertThat(GroupHelper.getSection(notification_social).mName).isEqualTo(
                "SocialSection");

        final NotificationChannel recsChannel = new NotificationChannel(NotificationChannel.RECS_ID,
                NotificationChannel.RECS_ID, IMPORTANCE_DEFAULT);
        final NotificationRecord notification_recs = getNotificationRecord(mPkg, 0, "", mUser,
                "", false, recsChannel);
        assertThat(GroupHelper.getSection(notification_recs).mName).isEqualTo(
                "RecsSection");

        checkNonGroupableNotifications();
    }

}