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

Commit d7f0c15d authored by Jay Aliomer's avatar Jay Aliomer
Browse files

Make sure life extended HUNs get removed

Fixes: 213194601
Test: HeadsUpCoordinatorTest.java
Change-Id: I8866572934183a23a4f8b1bfcb263c8c790cfe53
parent 30530053
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -238,7 +238,7 @@ public abstract class AlertingNotificationManager implements NotificationLifetim
     * @param key the key to check if removable
     * @return true if the alert entry can be removed
     */
    protected boolean canRemoveImmediately(String key) {
    public boolean canRemoveImmediately(String key) {
        AlertEntry alertEntry = mAlertEntries.get(key);
        return alertEntry == null || alertEntry.wasShownLongEnough()
                || alertEntry.mEntry.isRowDismissed();
+12 −9
Original line number Diff line number Diff line
@@ -179,23 +179,26 @@ public class HeadsUpCoordinator implements Coordinator {

        @Override
        public boolean shouldExtendLifetime(@NonNull NotificationEntry entry, int reason) {
            boolean isShowingHun = isCurrentlyShowingHun(entry);
            if (isShowingHun) {
            boolean extend = !mHeadsUpManager.canRemoveImmediately(entry.getKey());
            if (extend) {
                if (isSticky(entry)) {
                    long removeAfterMillis = mHeadsUpManager.getEarliestRemovalTime(entry.getKey());
                    if (removeAfterMillis <= 0) return false;
                    mExecutor.executeDelayed(() -> {
                        // make sure that the entry was not updated
                        long removeAfterMillis2 =
                                mHeadsUpManager.getEarliestRemovalTime(entry.getKey());
                        if (mNotifsExtendingLifetime.contains(entry) && removeAfterMillis2 <= 0) {
                            mHeadsUpManager.removeNotification(entry.getKey(), true);
                        if (mNotifsExtendingLifetime.contains(entry)
                                && mHeadsUpManager.canRemoveImmediately(entry.getKey())) {
                            mHeadsUpManager.removeNotification(
                                    entry.getKey(), /* releaseImmediately */  true);
                        }
                    }, removeAfterMillis);
                } else {
                    // remove as early as possible
                    mExecutor.execute(
                            () -> mHeadsUpManager.removeNotification(
                                    entry.getKey(), /* releaseImmediately */  false));
                }
                mNotifsExtendingLifetime.add(entry);
            }
            return isShowingHun;
            return extend;
        }

        @Override
+1 −1
Original line number Diff line number Diff line
@@ -412,7 +412,7 @@ public class HeadsUpManagerPhone extends HeadsUpManager implements Dumpable,
    }

    @Override
    protected boolean canRemoveImmediately(@NonNull String key) {
    public boolean canRemoveImmediately(@NonNull String key) {
        if (mSwipedOutKeys.contains(key)) {
            // We always instantly dismiss views being manually swiped out.
            mSwipedOutKeys.remove(key);
+26 −0
Original line number Diff line number Diff line
@@ -140,10 +140,13 @@ public class HeadsUpCoordinatorTest extends SysuiTestCase {
    public void testCancelStickyNotification() {
        when(mHeadsUpManager.isSticky(anyString())).thenReturn(true);
        addHUN(mEntry);
        when(mHeadsUpManager.canRemoveImmediately(anyString())).thenReturn(false, true);
        when(mHeadsUpManager.getEarliestRemovalTime(anyString())).thenReturn(1000L, 0L);
        assertTrue(mNotifLifetimeExtender.shouldExtendLifetime(mEntry, 0));
        mClock.advanceTime(1000L);
        mExecutor.runAllReady();
        verify(mHeadsUpManager, times(0))
                .removeNotification(anyString(), eq(false));
        verify(mHeadsUpManager, times(1))
                .removeNotification(anyString(), eq(true));
    }
@@ -156,6 +159,22 @@ public class HeadsUpCoordinatorTest extends SysuiTestCase {
        assertTrue(mNotifLifetimeExtender.shouldExtendLifetime(mEntry, 0));
        mClock.advanceTime(1000L);
        mExecutor.runAllReady();
        verify(mHeadsUpManager, times(0))
                .removeNotification(anyString(), eq(false));
        verify(mHeadsUpManager, times(0))
                .removeNotification(anyString(), eq(true));
    }

    @Test
    public void testCancelNotification() {
        when(mHeadsUpManager.isSticky(anyString())).thenReturn(false);
        addHUN(mEntry);
        when(mHeadsUpManager.getEarliestRemovalTime(anyString())).thenReturn(1000L, 500L);
        assertTrue(mNotifLifetimeExtender.shouldExtendLifetime(mEntry, 0));
        mClock.advanceTime(1000L);
        mExecutor.runAllReady();
        verify(mHeadsUpManager, times(1))
                .removeNotification(anyString(), eq(false));
        verify(mHeadsUpManager, times(0))
                .removeNotification(anyString(), eq(true));
    }
@@ -189,6 +208,13 @@ public class HeadsUpCoordinatorTest extends SysuiTestCase {
        // GIVEN there is a HUN, mEntry
        addHUN(mEntry);

        given(mHeadsUpManager.canRemoveImmediately(anyString())).willAnswer(i -> {
            String key = i.getArgument(0);
            for (NotificationEntry entry : mHuns) {
                if (entry.getKey().equals(key)) return false;
            }
            return true;
        });
        // THEN only the current HUN, mEntry, should be lifetimeExtended
        assertTrue(mNotifLifetimeExtender.shouldExtendLifetime(mEntry, /* cancellationReason */ 0));
        assertFalse(mNotifLifetimeExtender.shouldExtendLifetime(