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

Commit 63baeffd authored by Hongguang Chen's avatar Hongguang Chen Committed by Android (Google) Code Review
Browse files

Merge "Use elapsed real time for sleep duration for the wake up with home intent"

parents 4f10133f 06dea749
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -715,28 +715,28 @@ public final class PowerManager {
     * @hide
     */
    public static class WakeData {
        public WakeData(long wakeTime, @WakeReason int wakeReason, long sleepDuration) {
        public WakeData(long wakeTime, @WakeReason int wakeReason, long sleepDurationRealtime) {
            this.wakeTime = wakeTime;
            this.wakeReason = wakeReason;
            this.sleepDuration = sleepDuration;
            this.sleepDurationRealtime = sleepDurationRealtime;
        }
        public final long wakeTime;
        public final @WakeReason int wakeReason;
        public final long sleepDuration;
        public final long sleepDurationRealtime;

        @Override
        public boolean equals(@Nullable Object o) {
            if (o instanceof WakeData) {
                final WakeData other = (WakeData) o;
                return wakeTime == other.wakeTime && wakeReason == other.wakeReason
                        && sleepDuration == other.sleepDuration;
                        && sleepDurationRealtime == other.sleepDurationRealtime;
            }
            return false;
        }

        @Override
        public int hashCode() {
            return Objects.hash(wakeTime, wakeReason, sleepDuration);
            return Objects.hash(wakeTime, wakeReason, sleepDurationRealtime);
        }
    }

+4 −3
Original line number Diff line number Diff line
@@ -4585,12 +4585,13 @@ public class PhoneWindowManager implements WindowManagerPolicy {
            return false;
        }

        final long sleepDuration = mPowerManagerInternal.getLastWakeup().sleepDuration;
        final long sleepDurationRealtime =
                mPowerManagerInternal.getLastWakeup().sleepDurationRealtime;
        if (DEBUG_WAKEUP) {
            Log.i(TAG, "shouldWakeUpWithHomeIntent: sleepDuration= " + sleepDuration
            Log.i(TAG, "shouldWakeUpWithHomeIntent: sleepDurationRealtime= " + sleepDurationRealtime
                    + " mWakeUpToLastStateTimeout= " + mWakeUpToLastStateTimeout);
        }
        return sleepDuration > mWakeUpToLastStateTimeout;
        return sleepDurationRealtime > mWakeUpToLastStateTimeout;
    }

    private void wakeUpFromPowerKey(long eventTime) {
+31 −6
Original line number Diff line number Diff line
@@ -376,6 +376,10 @@ public final class PowerManagerService extends SystemService
    private long mLastGlobalWakeTime;
    private long mLastGlobalSleepTime;

    // Timestamp (in the elapsed realtime timebase) of the last time was awoken or put to sleep.
    private long mLastGlobalWakeTimeRealtime;
    private long mLastGlobalSleepTimeRealtime;

    // Last reason the device went to sleep.
    private @WakeReason int mLastGlobalWakeReason;
    private @GoToSleepReason int mLastGlobalSleepReason;
@@ -927,6 +931,11 @@ public final class PowerManagerService extends SystemService
         * Returns current time in milliseconds since boot, not counting time spent in deep sleep.
         */
        long uptimeMillis();

        /**
         * Returns milliseconds since boot, including time spent in sleep.
         */
        long elapsedRealtime();
    }

    @VisibleForTesting
@@ -1005,7 +1014,18 @@ public final class PowerManagerService extends SystemService
        }

        Clock createClock() {
            return SystemClock::uptimeMillis;
            return new Clock() {
                @Override
                public long uptimeMillis() {
                    return SystemClock.uptimeMillis();
                }

                @Override
                public long elapsedRealtime() {
                    return SystemClock.elapsedRealtime();
                }
            };

        }

        /**
@@ -2141,6 +2161,7 @@ public final class PowerManagerService extends SystemService
                        + ")...");
                mLastGlobalWakeTime = eventTime;
                mLastGlobalWakeReason = reason;
                mLastGlobalWakeTimeRealtime = mClock.elapsedRealtime();
                break;

            case WAKEFULNESS_DREAMING:
@@ -2152,9 +2173,9 @@ public final class PowerManagerService extends SystemService
                traceMethodName = "goToSleep";
                Slog.i(TAG, "Going to sleep due to " + PowerManager.sleepReasonToString(reason)
                        + " (uid " + uid + ")...");

                mLastGlobalSleepTime = eventTime;
                mLastGlobalSleepReason = reason;
                mLastGlobalSleepTimeRealtime = mClock.elapsedRealtime();
                mDozeStartInProgress = true;
                break;

@@ -4404,6 +4425,10 @@ public final class PowerManagerService extends SystemService
            pw.println("  mLastSleepTime=" + TimeUtils.formatUptime(mLastGlobalSleepTime));
            pw.println("  mLastSleepReason=" + PowerManager.sleepReasonToString(
                    mLastGlobalSleepReason));
            pw.println("  mLastGlobalWakeTimeRealtime="
                    + TimeUtils.formatUptime(mLastGlobalWakeTimeRealtime));
            pw.println("  mLastGlobalSleepTimeRealtime="
                    + TimeUtils.formatUptime(mLastGlobalSleepTimeRealtime));
            pw.println("  mLastInteractivePowerHintTime="
                    + TimeUtils.formatUptime(mLastInteractivePowerHintTime));
            pw.println("  mLastScreenBrightnessBoostTime="
@@ -5886,7 +5911,7 @@ public final class PowerManagerService extends SystemService
                boolean isPersonalized) {
            // Get current time before acquiring the lock so that the calculated end time is as
            // accurate as possible.
            final long nowElapsed = SystemClock.elapsedRealtime();
            final long nowElapsed = mClock.elapsedRealtime();
            if (mContext.checkCallingOrSelfPermission(
                    android.Manifest.permission.BATTERY_PREDICTION)
                    != PackageManager.PERMISSION_GRANTED) {
@@ -5950,7 +5975,7 @@ public final class PowerManagerService extends SystemService
                synchronized (mEnhancedDischargeTimeLock) {
                    // Get current time after acquiring the lock so that the calculated duration
                    // is as accurate as possible.
                    final long nowElapsed = SystemClock.elapsedRealtime();
                    final long nowElapsed = mClock.elapsedRealtime();
                    if (isEnhancedDischargePredictionValidLocked(nowElapsed)) {
                        return new ParcelDuration(mEnhancedDischargeTimeElapsed - nowElapsed);
                    }
@@ -5969,7 +5994,7 @@ public final class PowerManagerService extends SystemService
            final long ident = Binder.clearCallingIdentity();
            try {
                synchronized (mEnhancedDischargeTimeLock) {
                    return isEnhancedDischargePredictionValidLocked(SystemClock.elapsedRealtime())
                    return isEnhancedDischargePredictionValidLocked(mClock.elapsedRealtime())
                            && mEnhancedDischargePredictionIsPersonalized;
                }
            } finally {
@@ -6459,7 +6484,7 @@ public final class PowerManagerService extends SystemService
    private PowerManager.WakeData getLastWakeupInternal() {
        synchronized (mLock) {
            return new PowerManager.WakeData(mLastGlobalWakeTime, mLastGlobalWakeReason,
                    mLastGlobalWakeTime - mLastGlobalSleepTime);
                    mLastGlobalWakeTimeRealtime - mLastGlobalSleepTimeRealtime);
        }
    }

+11 −1
Original line number Diff line number Diff line
@@ -223,7 +223,17 @@ public class PowerManagerServiceMockingTest {

            @Override
            PowerManagerService.Clock createClock() {
                return () -> mClock.now();
                return new PowerManagerService.Clock() {
                    @Override
                    public long uptimeMillis() {
                        return mClock.now();
                    }

                    @Override
                    public long elapsedRealtime() {
                        return mClock.now();
                    }
                };
            }

            @Override
+19 −5
Original line number Diff line number Diff line
@@ -158,6 +158,7 @@ public class PowerManagerServiceTest {
    private UserSwitchedReceiver mUserSwitchedReceiver;
    private Resources mResourcesSpy;
    private OffsettableClock mClock;
    private long mLastElapsedRealtime;
    private TestLooper mTestLooper;

    private static class IntentFilterMatcher implements ArgumentMatcher<IntentFilter> {
@@ -286,7 +287,18 @@ public class PowerManagerServiceTest {

            @Override
            PowerManagerService.Clock createClock() {
                return () -> mClock.now();
                return new PowerManagerService.Clock() {
                    @Override
                    public long uptimeMillis() {
                        return mClock.now();
                    }

                    @Override
                    public long elapsedRealtime() {
                        mLastElapsedRealtime = mClock.now();
                        return mLastElapsedRealtime;
                    }
                };
            }

            @Override
@@ -1686,6 +1698,7 @@ public class PowerManagerServiceTest {

        mService.setWakefulnessLocked(nonDefaultPowerGroupId, WAKEFULNESS_DOZING, eventTime2,
                0, PowerManager.GO_TO_SLEEP_REASON_APPLICATION, 0, null, null);
        long eventElapsedRealtime1 = mLastElapsedRealtime;
        assertThat(mService.getGlobalWakefulnessLocked()).isEqualTo(WAKEFULNESS_DOZING);
        assertThat(mService.getBinderServiceInstance().getLastSleepReason()).isEqualTo(
                PowerManager.GO_TO_SLEEP_REASON_APPLICATION);
@@ -1693,19 +1706,20 @@ public class PowerManagerServiceTest {
        mService.setWakefulnessLocked(Display.DEFAULT_DISPLAY_GROUP, WAKEFULNESS_AWAKE,
                eventTime3, /* uid= */ 0, PowerManager.WAKE_REASON_PLUGGED_IN, /* opUid= */
                0, /* opPackageName= */ null, /* details= */ null);
        long eventElapsedRealtime2 = mLastElapsedRealtime;
        PowerManager.WakeData wakeData = mService.getLocalServiceInstance().getLastWakeup();
        assertThat(wakeData.wakeTime).isEqualTo(eventTime3);
        assertThat(wakeData.wakeReason).isEqualTo(PowerManager.WAKE_REASON_PLUGGED_IN);
        assertThat(wakeData.sleepDuration).isEqualTo(eventTime3 - eventTime2);
        assertThat(wakeData.sleepDurationRealtime)
                .isEqualTo(eventElapsedRealtime2 - eventElapsedRealtime1);

        // The global wake time and reason as well as sleep duration shouldn't change when another
        // PowerGroup wakes up.
        mService.setWakefulnessLocked(nonDefaultPowerGroupId, WAKEFULNESS_AWAKE,
                eventTime4, /* uid= */ 0, PowerManager.WAKE_REASON_CAMERA_LAUNCH, /* opUid= */
                0, /* opPackageName= */ null, /* details= */ null);
        assertThat(wakeData.wakeTime).isEqualTo(eventTime3);
        assertThat(wakeData.wakeReason).isEqualTo(PowerManager.WAKE_REASON_PLUGGED_IN);
        assertThat(wakeData.sleepDuration).isEqualTo(eventTime3 - eventTime2);
        PowerManager.WakeData wakeData2 = mService.getLocalServiceInstance().getLastWakeup();
        assertThat(wakeData2).isEqualTo(wakeData);
    }

    @Test