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

Commit 528d8d90 authored by James Lemieux's avatar James Lemieux
Browse files

Avoid applying negative time deltas - DO NOT MERGE

Bug: 32194582

After a reboot, time can be reported as wonky. Android system time
can appear to have moved backward in some cases. This change
defends the integrity of timer and stopwatch data against such cases.

Change-Id: Idf467f4ff5a9634e3a268467730412fc0c15aad7
parent b2c693e8
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import static com.android.deskclock.Utils.wallClock;
import static com.android.deskclock.data.Stopwatch.State.PAUSED;
import static com.android.deskclock.data.Stopwatch.State.RESET;
import static com.android.deskclock.data.Stopwatch.State.RUNNING;
import static com.android.deskclock.provider.ClockContract.AUTHORITY;

/**
 * A read-only domain object representing a stopwatch.
@@ -118,7 +117,9 @@ public final class Stopwatch {
        }
        final long timeSinceBoot = now();
        final long wallClockTime = wallClock();
        final long delta = wallClockTime - mLastStartWallClockTime;
        // Avoid negative time deltas. They can happen in practice, but they can't be used. Simply
        // update the recorded times and proceed with no change in accumulated time.
        final long delta = Math.max(0, wallClockTime - mLastStartWallClockTime);
        return new Stopwatch(mState, timeSinceBoot, wallClockTime, mAccumulatedTime + delta);
    }

@@ -130,6 +131,9 @@ public final class Stopwatch {
        final long wallClockTime = wallClock();
        final long delta = timeSinceBoot - mLastStartTime;
        if (delta < 0) {
            // Avoid negative time deltas. They typically happen following reboots when TIME_SET is
            // broadcast before BOOT_COMPLETED. Simply ignore the time update and hope
            // updateAfterReboot() can successfully correct the data at a later time.
            return this;
        }
        return new Stopwatch(mState, timeSinceBoot, wallClockTime, mAccumulatedTime + delta);
+6 −1
Original line number Diff line number Diff line
@@ -258,7 +258,9 @@ public final class Timer {

        final long timeSinceBoot = now();
        final long wallClockTime = wallClock();
        final long delta = wallClockTime - mLastStartWallClockTime;
        // Avoid negative time deltas. They can happen in practice, but they can't be used. Simply
        // update the recorded times and proceed with no change in accumulated time.
        final long delta = Math.max(0, wallClockTime - mLastStartWallClockTime);
        final long remainingTime = mRemainingTime - delta;
        return new Timer(mId, mState, mLength, mTotalLength, timeSinceBoot, wallClockTime,
                remainingTime, mLabel, mDeleteAfterUse);
@@ -277,6 +279,9 @@ public final class Timer {
        final long delta = timeSinceBoot - mLastStartTime;
        final long remainingTime = mRemainingTime - delta;
        if (delta < 0) {
            // Avoid negative time deltas. They typically happen following reboots when TIME_SET is
            // broadcast before BOOT_COMPLETED. Simply ignore the time update and hope
            // updateAfterReboot() can successfully correct the data at a later time.
            return this;
        }
        return new Timer(mId, mState, mLength, mTotalLength, timeSinceBoot, wallClockTime,