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

Commit 8b42a837 authored by Alex Salo's avatar Alex Salo Committed by Android (Google) Code Review
Browse files

Merge "Prevent extra work on the same timestamp" into qt-dev

parents e7180555 cbe610ac
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -270,7 +270,7 @@ public class AttentionManagerService extends SystemService {
                return;
            }
            if (!userState.mCurrentAttentionCheck.mCallbackInternal.equals(callbackInternal)) {
                Slog.e(LOG_TAG, "Cannot cancel a non-current request");
                Slog.w(LOG_TAG, "Cannot cancel a non-current request");
                return;
            }
            cancel(userState);
+7 −1
Original line number Diff line number Diff line
@@ -75,6 +75,8 @@ public class AttentionDetector {
     */
    private final AtomicBoolean mRequested;

    private long mLastActedOnNextScreenDimming;

    /**
     * Monotonously increasing ID for the requests sent.
     */
@@ -150,6 +152,9 @@ public class AttentionDetector {
    }

    public long updateUserActivity(long nextScreenDimming) {
        if (nextScreenDimming == mLastActedOnNextScreenDimming) {
            return nextScreenDimming;
        }
        if (!mIsSettingEnabled) {
            return nextScreenDimming;
        }
@@ -190,13 +195,14 @@ public class AttentionDetector {
        // afterwards if AttentionManager couldn't deliver it.
        mRequested.set(true);
        mRequestId++;
        mLastActedOnNextScreenDimming = nextScreenDimming;
        mCallback = new AttentionCallbackInternalImpl(mRequestId);
        Slog.v(TAG, "Checking user attention, ID: " + mRequestId);
        final boolean sent = mAttentionManager.checkAttention(getAttentionTimeout(), mCallback);
        if (!sent) {
            mRequested.set(false);
        }

        Slog.v(TAG, "Checking user attention, ID: " + mRequestId);
        return whenToCheck;
    }

+14 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
@@ -181,11 +182,23 @@ public class AttentionDetectorTest extends AndroidTestCase {
        verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
    }

    @Test
    public void testOnUserActivity_ignoresIfAlreadyDoneForThatNextScreenDimming() {
        long when = registerAttention();
        verify(mAttentionManagerInternal).checkAttention(anyLong(), any());
        assertThat(when).isLessThan(mNextDimming);
        clearInvocations(mAttentionManagerInternal);

        long redundantWhen = mAttentionDetector.updateUserActivity(mNextDimming);
        assertThat(redundantWhen).isEqualTo(mNextDimming);
        verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
    }

    @Test
    public void testOnUserActivity_skipsIfAlreadyScheduled() {
        registerAttention();
        reset(mAttentionManagerInternal);
        long when = mAttentionDetector.updateUserActivity(mNextDimming);
        long when = mAttentionDetector.updateUserActivity(mNextDimming + 1);
        verify(mAttentionManagerInternal, never()).checkAttention(anyLong(), any());
        assertThat(when).isLessThan(mNextDimming);
    }