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

Commit baba1a05 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add ranking object to each NotificationEntry"

parents d457ea8a ae90a9e4
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -499,10 +499,10 @@ public class NotificationEntryManager implements
        if (rankingMap == null) {
            return;
        }
        NotificationListenerService.Ranking tmpRanking = new NotificationListenerService.Ranking();
        NotificationListenerService.Ranking ranking = new NotificationListenerService.Ranking();
        for (NotificationEntry pendingNotification : mPendingNotifications.values()) {
            rankingMap.getRanking(pendingNotification.key, tmpRanking);
            pendingNotification.populateFromRanking(tmpRanking);
            rankingMap.getRanking(pendingNotification.key, ranking);
            pendingNotification.setRanking(ranking);
        }
    }

+9 −15
Original line number Diff line number Diff line
@@ -213,7 +213,7 @@ public class NotificationData {
            StatusBarNotification notification) {
        updateRanking(ranking);
        final StatusBarNotification oldNotification = entry.notification;
        entry.notification = notification;
        entry.setNotification(notification);
        mGroupManager.onEntryUpdated(entry, oldNotification);
    }

@@ -325,14 +325,6 @@ public class NotificationData {
        return NotificationManager.IMPORTANCE_UNSPECIFIED;
    }

    public String getOverrideGroupKey(String key) {
        if (mRankingMap != null) {
            getRanking(key, mTmpRanking);
            return mTmpRanking.getOverrideGroupKey();
        }
        return null;
    }

    public List<SnoozeCriterion> getSnoozeCriteria(String key) {
        if (mRankingMap != null) {
            getRanking(key, mTmpRanking);
@@ -365,23 +357,25 @@ public class NotificationData {
        return false;
    }

    private void updateRankingAndSort(RankingMap ranking) {
        if (ranking != null) {
            mRankingMap = ranking;
    private void updateRankingAndSort(RankingMap rankingMap) {
        if (rankingMap != null) {
            mRankingMap = rankingMap;
            synchronized (mEntries) {
                final int len = mEntries.size();
                for (int i = 0; i < len; i++) {
                    NotificationEntry entry = mEntries.valueAt(i);
                    if (!getRanking(entry.key, mTmpRanking)) {
                    Ranking newRanking = new Ranking();
                    if (!getRanking(entry.key, newRanking)) {
                        continue;
                    }
                    entry.setRanking(newRanking);

                    final StatusBarNotification oldSbn = entry.notification.cloneLight();
                    final String overrideGroupKey = getOverrideGroupKey(entry.key);
                    final String overrideGroupKey = newRanking.getOverrideGroupKey();
                    if (!Objects.equals(oldSbn.getOverrideGroupKey(), overrideGroupKey)) {
                        entry.notification.setOverrideGroupKey(overrideGroupKey);
                        mGroupManager.onEntryUpdated(entry, oldSbn);
                    }
                    entry.populateFromRanking(mTmpRanking);
                    entry.setIsHighPriority(isHighPriority(entry.notification));
                }
            }
+72 −11
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.SystemClock;
import android.service.notification.NotificationListenerService;
import android.service.notification.NotificationListenerService.Ranking;
import android.service.notification.SnoozeCriterion;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
@@ -88,6 +88,8 @@ public final class NotificationEntry {
    private static final int COLOR_INVALID = 1;
    public final String key;
    public StatusBarNotification notification;
    private Ranking mRanking;

    public NotificationChannel channel;
    public long lastAudiblyAlertedMs;
    public boolean noisy;
@@ -103,7 +105,7 @@ public final class NotificationEntry {
    private long lastFullScreenIntentLaunchTime = NOT_LAUNCHED_YET;
    public CharSequence remoteInputText;
    public List<SnoozeCriterion> snoozeCriteria;
    public int userSentiment = NotificationListenerService.Ranking.USER_SENTIMENT_NEUTRAL;
    public int userSentiment = Ranking.USER_SENTIMENT_NEUTRAL;
    /** Smart Actions provided by the NotificationAssistantService. */
    @NonNull
    public List<Notification.Action> systemGeneratedSmartActions = Collections.emptyList();
@@ -181,21 +183,80 @@ public final class NotificationEntry {
    private boolean mAutoHeadsUp;
    private boolean mPulseSupressed;

    public NotificationEntry(StatusBarNotification n) {
        this(n, null);
    public NotificationEntry(
            StatusBarNotification sbn,
            @NonNull Ranking ranking) {
        this(sbn, ranking, false);
    }

    public NotificationEntry(
            StatusBarNotification n,
            @Nullable NotificationListenerService.Ranking ranking) {
        this.key = n.getKey();
        this.notification = n;
    private NotificationEntry(
            StatusBarNotification sbn,
            Ranking ranking,
            boolean isTest) {
        this.key = sbn.getKey();
        this.notification = sbn;

        // TODO: Update tests to no longer need to pass null ranking
        if (ranking != null) {
            populateFromRanking(ranking);
            setRanking(ranking);
        } else if (!isTest) {
            throw new IllegalArgumentException("Ranking cannot be null");
        }
    }

    /**
     * Method for old tests that build NotificationEntries with a ranking.
     *
     * @deprecated New tests should pass a ranking object as well.
     */
    @VisibleForTesting
    @Deprecated
    public static NotificationEntry buildForTest(StatusBarNotification sbn) {
        // TODO START here this will NPE on all tests
        return new NotificationEntry(sbn, null, true);
    }

    /** The key for this notification. Guaranteed to be immutable and unique */
    public String key() {
        return key;
    }

    /**
     * The StatusBarNotification that represents one half of a NotificationEntry (the other half
     * being the Ranking). This object is swapped out whenever a notification is updated.
     */
    public StatusBarNotification sbn() {
        return notification;
    }

    /**
     * Should only be called by NotificationEntryManager and friends.
     * TODO: Make this package-private
     */
    public void setNotification(StatusBarNotification sbn) {
        if (!sbn.getKey().equals(key)) {
            throw new IllegalArgumentException("New key " + sbn.getKey()
                    + " doesn't match existing key " + key);
        }
        notification = sbn;
    }

    public void populateFromRanking(@NonNull NotificationListenerService.Ranking ranking) {
    /**
     * The Ranking that represents one half of a NotificationEntry (the other half being the
     * StatusBarNotification). This object is swapped out whenever a the ranking is updated (which
     * generally occurs whenever anything changes in the notification list).
     */
    public Ranking ranking() {
        return mRanking;
    }

    /**
     * Should only be called by NotificationEntryManager and friends.
     * TODO: Make this package-private
     */
    public void setRanking(@NonNull Ranking ranking) {
        mRanking = ranking;

        channel = ranking.getChannel();
        lastAudiblyAlertedMs = ranking.getLastAudiblyAlertedMillis();
        importance = ranking.getImportance();
+3 −3
Original line number Diff line number Diff line
@@ -392,18 +392,18 @@ public class ForegroundServiceControllerTest extends SysuiTestCase {
    }

    private void entryRemoved(StatusBarNotification notification) {
        mEntryListener.onEntryRemoved(new NotificationEntry(notification),
        mEntryListener.onEntryRemoved(NotificationEntry.buildForTest(notification),
                null, false);
    }

    private void entryAdded(StatusBarNotification notification, int importance) {
        NotificationEntry entry = new NotificationEntry(notification);
        NotificationEntry entry = NotificationEntry.buildForTest(notification);
        entry.importance = importance;
        mEntryListener.onPendingEntryAdded(entry);
    }

    private void entryUpdated(StatusBarNotification notification, int importance) {
        NotificationEntry entry = new NotificationEntry(notification);
        NotificationEntry entry = NotificationEntry.buildForTest(notification);
        entry.importance = importance;
        mEntryListener.onPostEntryUpdated(entry);
    }
+1 −1
Original line number Diff line number Diff line
@@ -878,7 +878,7 @@ public class BubbleDataTest extends SysuiTestCase {
        when(sbn.getNotification()).thenReturn(notification);

        // NotificationEntry -> StatusBarNotification -> Notification -> BubbleMetadata
        return new NotificationEntry(sbn);
        return NotificationEntry.buildForTest(sbn);
    }

    private void setCurrentTime(long time) {
Loading