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

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

Merge "Fix notification ordering for groups"

parents 11432662 bbc469ce
Loading
Loading
Loading
Loading
+6 −4
Original line number Original line Diff line number Diff line
@@ -16,14 +16,14 @@


package com.android.server.notification;
package com.android.server.notification;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Context;
import android.net.Uri;
import android.net.Uri;
import android.service.notification.NotificationListenerService;
import android.util.Log;
import android.util.Log;
import android.util.Slog;
import android.util.Slog;


import com.android.internal.annotations.VisibleForTesting;

/**
/**
 * This {@link com.android.server.notification.NotificationSignalExtractor} notices noisy
 * This {@link com.android.server.notification.NotificationSignalExtractor} notices noisy
 * notifications and marks them to get a temporary ranking bump.
 * notifications and marks them to get a temporary ranking bump.
@@ -34,7 +34,8 @@ public class NotificationIntrusivenessExtractor implements NotificationSignalExt


    /** Length of time (in milliseconds) that an intrusive or noisy notification will stay at
    /** Length of time (in milliseconds) that an intrusive or noisy notification will stay at
    the top of the ranking order, before it falls back to its natural position. */
    the top of the ranking order, before it falls back to its natural position. */
    private static final long HANG_TIME_MS = 10000;
    @VisibleForTesting
    static final long HANG_TIME_MS = 10000;


    public void initialize(Context ctx, NotificationUsageStats usageStats) {
    public void initialize(Context ctx, NotificationUsageStats usageStats) {
        if (DBG) Slog.d(TAG, "Initializing  " + getClass().getSimpleName() + ".");
        if (DBG) Slog.d(TAG, "Initializing  " + getClass().getSimpleName() + ".");
@@ -46,7 +47,8 @@ public class NotificationIntrusivenessExtractor implements NotificationSignalExt
            return null;
            return null;
        }
        }


        if (record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT) {
        if (record.getFreshnessMs(System.currentTimeMillis()) < HANG_TIME_MS
                && record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT) {
            if (record.getSound() != null && record.getSound() != Uri.EMPTY) {
            if (record.getSound() != null && record.getSound() != Uri.EMPTY) {
                record.setRecentlyIntrusive(true);
                record.setRecentlyIntrusive(true);
            }
            }
+10 −6
Original line number Original line Diff line number Diff line
@@ -3002,10 +3002,12 @@ public class NotificationManagerService extends SystemService {
        if (r == null) {
        if (r == null) {
            return;
            return;
        }
        }
        if (r.sbn.getOverrideGroupKey() == null) {
            addAutoGroupAdjustment(r, GroupHelper.AUTOGROUP_KEY);
            addAutoGroupAdjustment(r, GroupHelper.AUTOGROUP_KEY);
            EventLogTags.writeNotificationAutogrouped(key);
            EventLogTags.writeNotificationAutogrouped(key);
            mRankingHandler.requestSort();
            mRankingHandler.requestSort();
        }
        }
    }


    @GuardedBy("mNotificationLock")
    @GuardedBy("mNotificationLock")
    void removeAutogroupKeyLocked(String key) {
    void removeAutogroupKeyLocked(String key) {
@@ -3013,10 +3015,12 @@ public class NotificationManagerService extends SystemService {
        if (r == null) {
        if (r == null) {
            return;
            return;
        }
        }
        if (r.sbn.getOverrideGroupKey() != null) {
            addAutoGroupAdjustment(r, null);
            addAutoGroupAdjustment(r, null);
            EventLogTags.writeNotificationUnautogrouped(key);
            EventLogTags.writeNotificationUnautogrouped(key);
            mRankingHandler.requestSort();
            mRankingHandler.requestSort();
        }
        }
    }


    private void addAutoGroupAdjustment(NotificationRecord r, String overrideGroupKey) {
    private void addAutoGroupAdjustment(NotificationRecord r, String overrideGroupKey) {
        Bundle signals = new Bundle();
        Bundle signals = new Bundle();
+1 −2
Original line number Original line Diff line number Diff line
@@ -427,8 +427,7 @@ public class RankingHelper implements RankingConfig {
                record.setAuthoritativeRank(i);
                record.setAuthoritativeRank(i);
                final String groupKey = record.getGroupKey();
                final String groupKey = record.getGroupKey();
                NotificationRecord existingProxy = mProxyByGroupTmp.get(groupKey);
                NotificationRecord existingProxy = mProxyByGroupTmp.get(groupKey);
                if (existingProxy == null
                if (existingProxy == null) {
                        || record.getImportance() > existingProxy.getImportance()) {
                    mProxyByGroupTmp.put(groupKey, record);
                    mProxyByGroupTmp.put(groupKey, record);
                }
                }
            }
            }
+24 −1
Original line number Original line Diff line number Diff line
@@ -19,6 +19,9 @@ package com.android.server.notification;
import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static android.app.NotificationManager.IMPORTANCE_LOW;
import static android.app.NotificationManager.IMPORTANCE_LOW;


import static com.android.server.notification.NotificationIntrusivenessExtractor.HANG_TIME_MS;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertNull;


@@ -59,9 +62,29 @@ public class NotificationIntrusivenessExtractorTest extends NotificationTestCase


        Notification n = builder.build();
        Notification n = builder.build();
        StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0,
        StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0,
                0, n, UserHandle.ALL, null, System.currentTimeMillis());
                0, n, UserHandle.ALL, null,
                System.currentTimeMillis());
        NotificationRecord r = new NotificationRecord(getContext(), sbn, channel);
        NotificationRecord r = new NotificationRecord(getContext(), sbn, channel);


        assertNotNull(new NotificationIntrusivenessExtractor().process(r));
        assertNotNull(new NotificationIntrusivenessExtractor().process(r));
    }
    }

    @Test
    public void testOldNotificationsNotIntrusive() {
        NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
        final Notification.Builder builder = new Notification.Builder(getContext())
                .setContentTitle("foo")
                .setFullScreenIntent(PendingIntent.getActivity(
                        getContext(), 0, new Intent(""), 0), true)
                .setSmallIcon(android.R.drawable.sym_def_app_icon);

        Notification n = builder.build();
        StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0,
                0, n, UserHandle.ALL, null,
                System.currentTimeMillis() - HANG_TIME_MS);

        NotificationRecord r = new NotificationRecord(getContext(), sbn, channel);
        assertNull(new NotificationIntrusivenessExtractor().process(r));
        assertFalse(r.isRecentlyIntrusive());
    }
}
}
+27 −2
Original line number Original line Diff line number Diff line
@@ -1391,16 +1391,41 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
    }
    }


    @Test
    @Test
    public void testModifyAutogroup_requestsSort() throws Exception {
    public void testAddAutogroup_requestsSort() throws Exception {
        RankingHandler rh = mock(RankingHandler.class);
        RankingHandler rh = mock(RankingHandler.class);
        mNotificationManagerService.setRankingHandler(rh);
        mNotificationManagerService.setRankingHandler(rh);


        final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
        final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
        mNotificationManagerService.addNotification(r);
        mNotificationManagerService.addNotification(r);
        mNotificationManagerService.addAutogroupKeyLocked(r.getKey());
        mNotificationManagerService.addAutogroupKeyLocked(r.getKey());

        verify(rh, times(1)).requestSort();
    }

    @Test
    public void testRemoveAutogroup_requestsSort() throws Exception {
        RankingHandler rh = mock(RankingHandler.class);
        mNotificationManagerService.setRankingHandler(rh);

        final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
        r.setOverrideGroupKey("TEST");
        mNotificationManagerService.addNotification(r);
        mNotificationManagerService.removeAutogroupKeyLocked(r.getKey());
        mNotificationManagerService.removeAutogroupKeyLocked(r.getKey());


        verify(rh, times(2)).requestSort();
        verify(rh, times(1)).requestSort();
    }

    @Test
    public void testReaddAutogroup_noSort() throws Exception {
        RankingHandler rh = mock(RankingHandler.class);
        mNotificationManagerService.setRankingHandler(rh);

        final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
        r.setOverrideGroupKey("TEST");
        mNotificationManagerService.addNotification(r);
        mNotificationManagerService.addAutogroupKeyLocked(r.getKey());

        verify(rh, never()).requestSort();
    }
    }


    @Test
    @Test