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

Commit b81fc8cf authored by Valentin Iftime's avatar Valentin Iftime Committed by Iavor-Valentin Iftime
Browse files

Remove handler callbacks correctly

 Use removeCallbacksAndEqualMessages when removing handler callbacks by notification key.
 If a notification was updated and then canceled during the DELAY_FORCE_REGROUP_TIME: the callback was not removed because Handler#removeCallbacksMessages would check for reference equality, which would fail because the update would create a new
 NotificationRecord with the same key.

 Also skip calling GroupHelper#onNotificationPostedWithDelay if the notification was removed during DELAY_FORCE_REGROUP_TIME.

Flag: android.service.notification.notification_force_grouping
Test: atest NotificationManagerServiceTest
Bug: 376222541
Change-Id: I651c6d094558c558153f0a4e667ce4c3874ca398
parent cd4bc44f
Loading
Loading
Loading
Loading
+17 −7
Original line number Diff line number Diff line
@@ -9278,10 +9278,15 @@ public class NotificationManagerService extends SystemService {
                                            // a group summary or children (complete a group)
                                            mHandler.postDelayed(() -> {
                                                synchronized (mNotificationLock) {
                                                    NotificationRecord record =
                                                            mNotificationsByKey.get(key);
                                                    if (record != null) {
                                                        mGroupHelper.onNotificationPostedWithDelay(
                                                        r, mNotificationList, mSummaryByGroupKey);
                                                                record, mNotificationList,
                                                                mSummaryByGroupKey);
                                                    }
                                                }
                                            }, r.getKey(), DELAY_FORCE_REGROUP_TIME);
                                            }, key, DELAY_FORCE_REGROUP_TIME);
                                        }
                                    }
@@ -9327,10 +9332,15 @@ public class NotificationManagerService extends SystemService {
                                    if (notificationForceGrouping()) {
                                        mHandler.postDelayed(() -> {
                                            synchronized (mNotificationLock) {
                                                mGroupHelper.onNotificationPostedWithDelay(r,
                                                        mNotificationList, mSummaryByGroupKey);
                                                NotificationRecord record =
                                                        mNotificationsByKey.get(key);
                                                if (record != null) {
                                                    mGroupHelper.onNotificationPostedWithDelay(
                                                            record, mNotificationList,
                                                            mSummaryByGroupKey);
                                                }
                                            }
                                        }, r.getKey(), DELAY_FORCE_REGROUP_TIME);
                                        }, key, DELAY_FORCE_REGROUP_TIME);
                                    }
                                }
                            }
@@ -10310,7 +10320,7 @@ public class NotificationManagerService extends SystemService {
                }
                mListeners.notifyRemovedLocked(r, reason, r.getStats());
                if (notificationForceGrouping()) {
                    mHandler.removeCallbacksAndMessages(r.getKey());
                    mHandler.removeCallbacksAndEqualMessages(r.getKey());
                    mHandler.post(() -> {
                        synchronized (NotificationManagerService.this.mNotificationLock) {
                            mGroupHelper.onNotificationRemoved(r, mNotificationList);
+38 −0
Original line number Diff line number Diff line
@@ -2879,6 +2879,44 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
        verify(mGroupHelper, never()).onNotificationPostedWithDelay(eq(r), any(), any());
    }
    @Test
    @EnableFlags({FLAG_NOTIFICATION_FORCE_GROUPING,
            android.app.Flags.FLAG_CHECK_AUTOGROUP_BEFORE_POST})
    public void testRemoveScheduledForceGroup_onNotificationCanceled() throws Exception {
        NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, "tag", null,
                false);
        when(mGroupHelper.onNotificationPosted(any(), anyBoolean())).thenReturn(false);
        mService.addEnqueuedNotification(r);
        NotificationManagerService.PostNotificationRunnable runnable =
                mService.new PostNotificationRunnable(r.getKey(), r.getSbn().getPackageName(),
                r.getUid(), mPostNotificationTrackerFactory.newTracker(null));
        runnable.run();
        waitForIdle();
        // Post an update to the notification
        NotificationRecord r_update =
                generateNotificationRecord(mTestNotificationChannel, 0, "tag", null, false);
        mService.addEnqueuedNotification(r_update);
        runnable = mService.new PostNotificationRunnable(r_update.getKey(),
                r_update.getSbn().getPackageName(), r_update.getUid(),
                mPostNotificationTrackerFactory.newTracker(null));
        runnable.run();
        waitForIdle();
        // Cancel the notification
        mBinderService.cancelNotificationWithTag(r.getSbn().getPackageName(),
                r.getSbn().getPackageName(), r.getSbn().getTag(),
                r.getSbn().getId(), r.getSbn().getUserId());
        waitForIdle();
        mTestableLooper.moveTimeForward(DELAY_FORCE_REGROUP_TIME);
        waitForIdle();
        // Check that onNotificationPostedWithDelay was canceled
        verify(mGroupHelper, times(1)).onNotificationPosted(any(), anyBoolean());
        verify(mGroupHelper, never()).onNotificationPostedWithDelay(any(), any(), any());
    }
    @Test
    @EnableFlags(FLAG_NOTIFICATION_FORCE_GROUPING)
    public void testEnqueueNotification_forceGrouped_clearsSummaryFlag() throws Exception {