summaryrefslogtreecommitdiffstats
path: root/src/com
diff options
context:
space:
mode:
authorJames Lemieux <jplemieux@google.com>2017-01-26 17:32:55 -0800
committerJames Lemieux <jplemieux@google.com>2017-01-26 17:32:55 -0800
commit4eabf334b12b04ba941fa242d28a15a9ee8e172f (patch)
tree06de5bb64d3578064c328b9f8e9e6e6c3e529390 /src/com
parentbc846e2eee129219f87f529a8a687ff851b0cabd (diff)
downloadandroid_packages_apps_DeskClock-4eabf334b12b04ba941fa242d28a15a9ee8e172f.tar.gz
android_packages_apps_DeskClock-4eabf334b12b04ba941fa242d28a15a9ee8e172f.tar.bz2
android_packages_apps_DeskClock-4eabf334b12b04ba941fa242d28a15a9ee8e172f.zip
Reset stopwatches with bad data on first load
Bug: 34742962 Test: Manually create There was a time when it was possible for a stopwatch with negative elapsed time could be recorded. This bad data can cause formatting exceptions in the new code. To avoid these exceptions, the stopwatch is reset if it loaded in this bad state. Change-Id: I5cdd287c96bd87a82876b7a8cb3643313045f667
Diffstat (limited to 'src/com')
-rw-r--r--src/com/android/deskclock/data/StopwatchDAO.java9
-rw-r--r--src/com/android/deskclock/data/Timer.java12
2 files changed, 16 insertions, 5 deletions
diff --git a/src/com/android/deskclock/data/StopwatchDAO.java b/src/com/android/deskclock/data/StopwatchDAO.java
index 823ab63fd..541390148 100644
--- a/src/com/android/deskclock/data/StopwatchDAO.java
+++ b/src/com/android/deskclock/data/StopwatchDAO.java
@@ -61,7 +61,14 @@ final class StopwatchDAO {
final long lastStartTime = prefs.getLong(LAST_START_TIME, Stopwatch.UNUSED);
final long lastWallClockTime = prefs.getLong(LAST_WALL_CLOCK_TIME, Stopwatch.UNUSED);
final long accumulatedTime = prefs.getLong(ACCUMULATED_TIME, 0);
- return new Stopwatch(state, lastStartTime, lastWallClockTime, accumulatedTime);
+ Stopwatch s = new Stopwatch(state, lastStartTime, lastWallClockTime, accumulatedTime);
+
+ // If the stopwatch reports an illegal (negative) amount of time, remove the bad data.
+ if (s.getTotalTime() < 0) {
+ s = s.reset();
+ setStopwatch(prefs, s);
+ }
+ return s;
}
/**
diff --git a/src/com/android/deskclock/data/Timer.java b/src/com/android/deskclock/data/Timer.java
index 816d58786..71716a375 100644
--- a/src/com/android/deskclock/data/Timer.java
+++ b/src/com/android/deskclock/data/Timer.java
@@ -139,14 +139,18 @@ public final class Timer {
/**
* @return the total amount of time remaining up to this moment; expired and missed timers will
- * return a negative amount
+ * return a negative amount
*/
public long getRemainingTime() {
- if (mState == RUNNING || mState == EXPIRED || mState == MISSED) {
- return mRemainingTime - (now() - mLastStartTime);
+ if (mState == PAUSED || mState == RESET) {
+ return mRemainingTime;
}
- return mRemainingTime;
+ // In practice, "now" can be any value due to device reboots. When the real-time clock
+ // is reset, there is no more guarantee that "now" falls after the last start time. To
+ // ensure the timer is monotonically decreasing, normalize negative time segments to 0,
+ final long timeSinceStart = now() - mLastStartTime;
+ return mRemainingTime - Math.max(0, timeSinceStart);
}
/**