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

Commit e41e427d authored by Lyn Han's avatar Lyn Han Committed by Android (Google) Code Review
Browse files

Merge changes I47500298,I1dde9b3b into main

* changes:
  Test that bundles are sorted before notifs
  Prefix bundle keys to sort them in fixed order
parents 365cd6de 9327fb63
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -71,6 +71,8 @@ import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@SmallTest
@RunWith(AndroidJUnit4.class)
@@ -324,6 +326,35 @@ public class RankingCoordinatorTest extends SysuiTestCase {
        }
    }

    @Test
    public void testSilentSectionComparator_sortsBundlesByPrefixedKeys() {
        // This is the sorted order
        BundleEntry socialBundle = new BundleEntry(BundleSpec.Companion.getSOCIAL_MEDIA());
        BundleEntry promoBundle = new BundleEntry(BundleSpec.Companion.getPROMOTIONS());
        BundleEntry newsBundle = new BundleEntry(BundleSpec.Companion.getNEWS());
        BundleEntry recsBundle = new BundleEntry(BundleSpec.Companion.getRECOMMENDED());

        // Add them in unsorted order
        List<BundleEntry> bundles = new ArrayList<>(Arrays.asList(
                newsBundle, socialBundle, recsBundle, promoBundle
        ));
        Collections.sort(bundles, mSilentSectioner.getComparator());

        assertEquals("i=0 expected Social", socialBundle, bundles.get(0));
        assertEquals("i=1 expected Promo", promoBundle, bundles.get(1));
        assertEquals("i=2 expected News", newsBundle, bundles.get(2));
        assertEquals("i=3 expected Recs", recsBundle, bundles.get(3));
    }

    @Test
    public void testSilentSectionComparator_sortsBundlesBeforeNotifs() {
        BundleEntry bundleEntry = new BundleEntry(BundleSpec.Companion.getSOCIAL_MEDIA());
        int comparison = mSilentSectioner.getComparator().compare(bundleEntry, mEntry);
        assertTrue("Expected BundleEntry before NotifEntry (comparison < 0) but was: "
                        + comparison,
                comparison < 0);
    }

    @Test
    public void testMinimizedSectioner_rejectsBundle() {
        BundleEntry bundleEntry = new BundleEntry(BundleSpec.Companion.getNEWS());
+25 −5
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.systemui.statusbar.notification.collection.coordinator;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.NotificationChannel;

import com.android.systemui.plugins.statusbar.StatusBarStateController;
import com.android.systemui.statusbar.notification.collection.BundleEntry;
@@ -37,7 +38,9 @@ import com.android.systemui.statusbar.notification.dagger.SilentHeader;
import com.android.systemui.statusbar.notification.shared.NotificationBundleUi;
import com.android.systemui.statusbar.notification.stack.NotificationPriorityBucketKt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

@@ -59,6 +62,16 @@ public class RankingCoordinator implements Coordinator {
    private boolean mHasSilentEntries;
    private boolean mHasMinimizedEntries;

    // Define the explicit sort order for bundle keys
    private static final Map<String, Integer> BUNDLE_KEY_SORT_ORDER = new HashMap<>();
    static {
        BUNDLE_KEY_SORT_ORDER.put(NotificationChannel.SOCIAL_MEDIA_ID, 1);
        BUNDLE_KEY_SORT_ORDER.put(NotificationChannel.PROMOTIONS_ID, 2);
        BUNDLE_KEY_SORT_ORDER.put(NotificationChannel.NEWS_ID, 3);
        BUNDLE_KEY_SORT_ORDER.put(NotificationChannel.RECS_ID, 4);
        BUNDLE_KEY_SORT_ORDER.put("debug_bundle", 99);
    }

    @Inject
    public RankingCoordinator(
            StatusBarStateController statusBarStateController,
@@ -176,11 +189,18 @@ public class RankingCoordinator implements Coordinator {
                boolean isBundle1 = o1 instanceof BundleEntry;
                boolean isBundle2 = o2 instanceof BundleEntry;
                if (isBundle1 && isBundle2) {
                    // When both are bundles, order by bundle id, which is guaranteed to be in
                    // a fixed order
                    // TODO(b/399736937) prefix bundle keys to ensure fixed order
                    // TODO(b/399736937) optimize sort since this is on the main thread
                    return o1.getKey().compareTo(o2.getKey());
                    final String key1 = o1.getKey();
                    final String key2 = o2.getKey();
                    // When both are bundles, use the BUNDLE_KEY_SORT_ORDER map to get rankings for
                    // the keys, which are guaranteed to be in fixed order. Default to large value
                    // for unknown bundle keys to sort them last.
                    int rank1 = BUNDLE_KEY_SORT_ORDER.getOrDefault(key1, Integer.MAX_VALUE);
                    int rank2 = BUNDLE_KEY_SORT_ORDER.getOrDefault(key2, Integer.MAX_VALUE);
                    int rankComparison = Integer.compare(rank1, rank2);
                    if (rankComparison != 0) {
                        return rankComparison;
                    }
                    return key1.compareTo(key2);
                }
                // Order bundles before non-bundles
                return -1 * Boolean.compare(isBundle1, isBundle2);