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

Commit 7e5027f0 authored by Alexander Roederer's avatar Alexander Roederer Committed by Android (Google) Code Review
Browse files

Merge changes from topic "b330214226-AutogroupOnPost" into main

* changes:
  Reduce RankingUpdate calls on autogroup
  Add autogroup on post flag
parents 90ebff0e f2ec0341
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -38,6 +38,13 @@ flag {
  bug: "299448097"
}

flag {
  name: "check_autogroup_before_post"
  namespace: "systemui"
  description: "Does a check to see if notification should be autogrouped before posting, and if so groups before post."
  bug: "330214226"
}

flag {
  name: "visit_risky_uris"
  namespace: "systemui"
+30 −9
Original line number Diff line number Diff line
@@ -108,16 +108,25 @@ public class GroupHelper {
        return (flags & mask) != 0;
    }

    public void onNotificationPosted(StatusBarNotification sbn, boolean autogroupSummaryExists) {
    /**
     * Called when a notification is newly posted. Checks whether that notification, and all other
     * active notifications should be grouped or ungrouped atuomatically, and returns whether.
     * @param sbn The posted notification.
     * @param autogroupSummaryExists Whether a summary for this notification already exists.
     * @return Whether the provided notification should be autogrouped synchronously.
     */
    public boolean onNotificationPosted(StatusBarNotification sbn, boolean autogroupSummaryExists) {
        boolean sbnToBeAutogrouped = false;
        try {
            if (!sbn.isAppGroup()) {
                maybeGroup(sbn, autogroupSummaryExists);
                sbnToBeAutogrouped = maybeGroup(sbn, autogroupSummaryExists);
            } else {
                maybeUngroup(sbn, false, sbn.getUserId());
            }
        } catch (Exception e) {
            Slog.e(TAG, "Failure processing new notification", e);
        }
        return sbnToBeAutogrouped;
    }

    public void onNotificationRemoved(StatusBarNotification sbn) {
@@ -137,20 +146,22 @@ public class GroupHelper {
     *
     * And stores the list of upgrouped notifications & their flags
     */
    private void maybeGroup(StatusBarNotification sbn, boolean autogroupSummaryExists) {
    private boolean maybeGroup(StatusBarNotification sbn, boolean autogroupSummaryExists) {
        int flags = 0;
        List<String> notificationsToGroup = new ArrayList<>();
        List<NotificationAttributes> childrenAttr = new ArrayList<>();
        // Indicates whether the provided sbn should be autogrouped by the caller.
        boolean sbnToBeAutogrouped = false;
        synchronized (mUngroupedNotifications) {
            String key = generatePackageKey(sbn.getUserId(), sbn.getPackageName());
            String packageKey = generatePackageKey(sbn.getUserId(), sbn.getPackageName());
            final ArrayMap<String, NotificationAttributes> children =
                    mUngroupedNotifications.getOrDefault(key, new ArrayMap<>());
                    mUngroupedNotifications.getOrDefault(packageKey, new ArrayMap<>());

            NotificationAttributes attr = new NotificationAttributes(sbn.getNotification().flags,
                    sbn.getNotification().getSmallIcon(), sbn.getNotification().color,
                    sbn.getNotification().visibility);
            children.put(sbn.getKey(), attr);
            mUngroupedNotifications.put(key, children);
            mUngroupedNotifications.put(packageKey, children);

            if (children.size() >= mAutoGroupAtCount || autogroupSummaryExists) {
                flags = getAutogroupSummaryFlags(children);
@@ -187,10 +198,20 @@ public class GroupHelper {
                mCallback.addAutoGroupSummary(sbn.getUserId(), sbn.getPackageName(), sbn.getKey(),
                        attr);
            }
            for (String key : notificationsToGroup) {
                mCallback.addAutoGroup(key);
            for (String keyToGroup : notificationsToGroup) {
                if (android.app.Flags.checkAutogroupBeforePost()) {
                    if (keyToGroup.equals(sbn.getKey())) {
                        // Autogrouping for the provided notification is to be done synchronously.
                        sbnToBeAutogrouped = true;
                    } else {
                        mCallback.addAutoGroup(keyToGroup, /*requestSort=*/true);
                    }
                } else {
                    mCallback.addAutoGroup(keyToGroup, /*requestSort=*/true);
                }
            }
        }
        return sbnToBeAutogrouped;
    }

    /**
@@ -406,7 +427,7 @@ public class GroupHelper {
    }

    protected interface Callback {
        void addAutoGroup(String key);
        void addAutoGroup(String key, boolean requestSort);
        void removeAutoGroup(String key);

        void addAutoGroupSummary(int userId, String pkg, String triggeringKey,
+45 −17
Original line number Diff line number Diff line
@@ -2793,9 +2793,9 @@ public class NotificationManagerService extends SystemService {
        return new GroupHelper(getContext(), getContext().getPackageManager(),
                mAutoGroupAtCount, new GroupHelper.Callback() {
            @Override
            public void addAutoGroup(String key) {
            public void addAutoGroup(String key, boolean requestSort) {
                        synchronized (mNotificationLock) {
                    addAutogroupKeyLocked(key);
                            addAutogroupKeyLocked(key, requestSort);
                        }
            }
@@ -6538,7 +6538,7 @@ public class NotificationManagerService extends SystemService {
    }
    @GuardedBy("mNotificationLock")
    void addAutogroupKeyLocked(String key) {
    void addAutogroupKeyLocked(String key, boolean requestSort) {
        NotificationRecord r = mNotificationsByKey.get(key);
        if (r == null) {
            return;
@@ -6546,9 +6546,11 @@ public class NotificationManagerService extends SystemService {
        if (r.getSbn().getOverrideGroupKey() == null) {
            addAutoGroupAdjustment(r, GroupHelper.AUTOGROUP_KEY);
            EventLogTags.writeNotificationAutogrouped(key);
            if (!android.app.Flags.checkAutogroupBeforePost() || requestSort) {
                mRankingHandler.requestSort();
            }
        }
    }
    @GuardedBy("mNotificationLock")
    void removeAutogroupKeyLocked(String key) {
@@ -8609,6 +8611,29 @@ public class NotificationManagerService extends SystemService {
                        notification.flags |= FLAG_NO_CLEAR;
                    }
                    // Posts the notification if it has a small icon, and potentially autogroup
                    // the new notification.
                    if (android.app.Flags.checkAutogroupBeforePost()) {
                        if (notification.getSmallIcon() != null && !isCritical(r)) {
                            StatusBarNotification oldSbn = (old != null) ? old.getSbn() : null;
                            if (oldSbn == null || !Objects.equals(oldSbn.getGroup(), n.getGroup())
                                    || oldSbn.getNotification().flags
                                    != n.getNotification().flags) {
                                synchronized (mNotificationLock) {
                                    boolean willBeAutogrouped = mGroupHelper.onNotificationPosted(n,
                                            hasAutoGroupSummaryLocked(n));
                                    if (willBeAutogrouped) {
                                        // The newly posted notification will be autogrouped, but
                                        // was not autogrouped onPost, to avoid an unnecessary sort.
                                        // We add the autogroup key to the notification without a
                                        // sort here, and it'll be sorted below with extractSignals.
                                        addAutogroupKeyLocked(key, /*requestSort=*/false);
                                    }
                                }
                            }
                        }
                    }
                    mRankingHelper.extractSignals(r);
                    mRankingHelper.sort(mNotificationList);
                    final int position = mRankingHelper.indexOf(mNotificationList, r);
@@ -8629,10 +8654,12 @@ public class NotificationManagerService extends SystemService {
                        notifyListenersPostedAndLogLocked(r, old, mTracker, maybeReport);
                        posted = true;
                        if (!android.app.Flags.checkAutogroupBeforePost()) {
                            StatusBarNotification oldSbn = (old != null) ? old.getSbn() : null;
                            if (oldSbn == null
                                    || !Objects.equals(oldSbn.getGroup(), n.getGroup())
                                || oldSbn.getNotification().flags != n.getNotification().flags) {
                                    || oldSbn.getNotification().flags
                                        != n.getNotification().flags) {
                                if (!isCritical(r)) {
                                    mHandler.post(() -> {
                                        synchronized (mNotificationLock) {
@@ -8642,6 +8669,7 @@ public class NotificationManagerService extends SystemService {
                                    });
                                }
                            }
                        }
                    } else {
                        Slog.e(TAG, "Not posting notification without small icon: " + notification);
                        if (old != null && !old.isCanceled) {
+238 −15

File changed.

Preview size limit exceeded, changes collapsed.

+24 −4
Original line number Diff line number Diff line
@@ -311,6 +311,7 @@ import com.android.server.wm.WindowManagerInternal;
import com.google.android.collect.Lists;
import com.google.common.collect.ImmutableList;
import libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
@@ -331,8 +332,6 @@ import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
@@ -348,6 +347,9 @@ import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
import platform.test.runner.parameterized.Parameters;
@SmallTest
@RunWith(ParameterizedAndroidJunit4.class)
@RunWithLooper
@@ -5542,7 +5544,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
    public void testAddAutogroup_requestsSort() throws Exception {
        final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
        mService.addNotification(r);
        mService.addAutogroupKeyLocked(r.getKey());
        mService.addAutogroupKeyLocked(r.getKey(), true);
        verify(mRankingHandler, times(1)).requestSort();
    }
@@ -5562,11 +5564,29 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
        final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
        r.setOverrideGroupKey("TEST");
        mService.addNotification(r);
        mService.addAutogroupKeyLocked(r.getKey());
        mService.addAutogroupKeyLocked(r.getKey(), true);
        verify(mRankingHandler, never()).requestSort();
    }
    @Test
    @EnableFlags(android.app.Flags.FLAG_CHECK_AUTOGROUP_BEFORE_POST)
    public void testAutogroupSuppressSort_noSort() throws Exception {
        final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
        mService.addNotification(r);
        mService.addAutogroupKeyLocked(r.getKey(), false);
        verify(mRankingHandler, never()).requestSort();
    }
    @Test
    @EnableFlags(android.app.Flags.FLAG_CHECK_AUTOGROUP_BEFORE_POST)
    public void testAutogroupOnPost_skipManualSort() throws Exception {
        final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
        mService.addNotification(r);
        verify(mRankingHandler, never()).requestSort();
    }
    @Test
    public void testHandleRankingSort_sendsUpdateOnSignalExtractorChange() throws Exception {
        mService.setPreferencesHelper(mPreferencesHelper);