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

Commit 057eb8c4 authored by Robert Horvath's avatar Robert Horvath
Browse files

Remove userActivitySummary condition for inattentive sleep

User activity with flag USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS, as
triggered when a wakelock with flag ON_AFTER_RELEASE is released,
should not extend the time the device is awake when the inattentive
sleep timeout expires.

This change removes the userActivitySummary condition in
isBeingKeptFromInattentiveSleepLocked. When this method is called, we
have already checked that since last user activity, attentive_timeout
has expired, so checking userActivitySummary is not required.
When a user activity with USER_ACTIVITY_NO_CHANGE_LIGHTS extends the dim
timeout, the userActivitySummary condition prevented the device from
going to sleep.

Bug: 202090650
Test: atest PowerManagerServiceTests
Test: Set short attentive_timeout, autoplay a series of short YouTube
      videos, confirm the device goes to sleep after attentive_timeout
      has expired
Change-Id: I2bcf2e65757118b82df52cbcf57d5017a9ce72f5
parent ad2f5e70
Loading
Loading
Loading
Loading
+7 −14
Original line number Diff line number Diff line
@@ -2701,7 +2701,7 @@ public final class PowerManagerService extends SystemService

            mHandler.removeMessages(MSG_ATTENTIVE_TIMEOUT);

            if (isBeingKeptFromShowingInattentiveSleepWarningLocked()) {
            if (isBeingKeptFromInattentiveSleepLocked()) {
                return;
            }

@@ -2717,10 +2717,6 @@ public final class PowerManagerService extends SystemService
                }
                mInattentiveSleepWarningOverlayController.show();
                nextTimeout = goToSleepTime;
            } else {
                if (DEBUG && getWakefulnessLocked() != WAKEFULNESS_ASLEEP) {
                    Slog.i(TAG, "Going to sleep now due to long user inactivity");
                }
            }

            if (nextTimeout >= 0) {
@@ -2739,7 +2735,7 @@ public final class PowerManagerService extends SystemService
        if (getWakefulnessLocked() != WAKEFULNESS_AWAKE) {
            mInattentiveSleepWarningOverlayController.dismiss(false);
            return true;
        } else if (attentiveTimeout < 0 || isBeingKeptFromShowingInattentiveSleepWarningLocked()
        } else if (attentiveTimeout < 0 || isBeingKeptFromInattentiveSleepLocked()
                || now < showWarningTime) {
            mInattentiveSleepWarningOverlayController.dismiss(true);
            return true;
@@ -2861,6 +2857,9 @@ public final class PowerManagerService extends SystemService
                        Slog.d(TAG, "updateWakefulnessLocked: Bed time for group " + id);
                    }
                    if (isAttentiveTimeoutExpired(id, time)) {
                        if (DEBUG) {
                            Slog.i(TAG, "Going to sleep now due to long user inactivity");
                        }
                        changed = sleepDisplayGroupNoUpdateLocked(id, time,
                                PowerManager.GO_TO_SLEEP_REASON_TIMEOUT,
                                PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE, Process.SYSTEM_UID);
@@ -2898,7 +2897,7 @@ public final class PowerManagerService extends SystemService

        long now = mClock.uptimeMillis();
        if (isAttentiveTimeoutExpired(groupId, now)) {
            return !isBeingKeptFromInattentiveSleepLocked(groupId);
            return !isBeingKeptFromInattentiveSleepLocked();
        } else {
            return !isBeingKeptAwakeLocked(groupId);
        }
@@ -2928,13 +2927,7 @@ public final class PowerManagerService extends SystemService
     * a phone call. This function only controls whether the device will go to sleep which is
     * independent of whether it will be allowed to suspend.
     */
    private boolean isBeingKeptFromInattentiveSleepLocked(int groupId) {
        return mStayOn || mScreenBrightnessBoostInProgress || mProximityPositive
                || (mDisplayGroupPowerStateMapper.getUserActivitySummaryLocked(groupId) & (
                USER_ACTIVITY_SCREEN_BRIGHT | USER_ACTIVITY_SCREEN_DIM)) != 0;
    }

    private boolean isBeingKeptFromShowingInattentiveSleepWarningLocked() {
    private boolean isBeingKeptFromInattentiveSleepLocked() {
        return mStayOn || mScreenBrightnessBoostInProgress || mProximityPositive || !mBootCompleted;
    }

+68 −0
Original line number Diff line number Diff line
@@ -888,6 +888,74 @@ public class PowerManagerServiceTest {
        assertThat(mService.getWakefulnessLocked()).isEqualTo(WAKEFULNESS_ASLEEP);
    }

    @Test
    public void testInattentiveSleep_wakeLockOnAfterRelease_inattentiveSleepTimeoutNotAffected()
            throws Exception {
        final DisplayInfo info = new DisplayInfo();
        info.displayGroupId = Display.DEFAULT_DISPLAY_GROUP;
        when(mDisplayManagerInternalMock.getDisplayInfo(Display.DEFAULT_DISPLAY)).thenReturn(info);

        final String pkg = mContextSpy.getOpPackageName();
        final Binder token = new Binder();
        final String tag = "testInattentiveSleep_wakeLockOnAfterRelease";

        setMinimumScreenOffTimeoutConfig(5);
        setAttentiveTimeout(2000);
        createService();
        startSystem();

        mService.getBinderServiceInstance().acquireWakeLock(token,
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, tag, pkg,
                null /* workSource */, null /* historyTag */, Display.DEFAULT_DISPLAY);

        advanceTime(1500);
        mService.getBinderServiceInstance().releaseWakeLock(token, 0 /* flags */);

        advanceTime(520);
        assertThat(mService.getWakefulnessLocked()).isEqualTo(WAKEFULNESS_ASLEEP);
    }

    @Test
    public void testInattentiveSleep_userActivityNoChangeLights_inattentiveSleepTimeoutNotAffected()
            throws Exception {
        final DisplayInfo info = new DisplayInfo();
        info.displayGroupId = Display.DEFAULT_DISPLAY_GROUP;
        when(mDisplayManagerInternalMock.getDisplayInfo(Display.DEFAULT_DISPLAY)).thenReturn(info);

        setMinimumScreenOffTimeoutConfig(5);
        setAttentiveTimeout(2000);
        createService();
        startSystem();

        advanceTime(1500);
        mService.getBinderServiceInstance().userActivity(Display.DEFAULT_DISPLAY, mClock.now(),
                PowerManager.USER_ACTIVITY_EVENT_OTHER,
                PowerManager.USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS);

        advanceTime(520);
        assertThat(mService.getWakefulnessLocked()).isEqualTo(WAKEFULNESS_ASLEEP);
    }

    @Test
    public void testInattentiveSleep_userActivity_inattentiveSleepTimeoutExtended()
            throws Exception {
        final DisplayInfo info = new DisplayInfo();
        info.displayGroupId = Display.DEFAULT_DISPLAY_GROUP;
        when(mDisplayManagerInternalMock.getDisplayInfo(Display.DEFAULT_DISPLAY)).thenReturn(info);

        setMinimumScreenOffTimeoutConfig(5);
        setAttentiveTimeout(2000);
        createService();
        startSystem();

        advanceTime(1500);
        mService.getBinderServiceInstance().userActivity(Display.DEFAULT_DISPLAY, mClock.now(),
                PowerManager.USER_ACTIVITY_EVENT_OTHER, 0 /* flags */);

        advanceTime(520);
        assertThat(mService.getWakefulnessLocked()).isNotEqualTo(WAKEFULNESS_ASLEEP);
    }

    @Test
    public void testWakeLock_affectsProperDisplayGroup() throws Exception {
        final int nonDefaultDisplayGroupId = Display.DEFAULT_DISPLAY_GROUP + 1;