summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Lemieux <jplemieux@google.com>2016-02-17 23:38:46 +0000
committerandroid-build-merger <android-build-merger@google.com>2016-02-17 23:38:46 +0000
commit43f89c23c5f4b4a8db0064f8e3831da68860316d (patch)
tree5cb2e578cf9532fe3152c52cf220a8a21fab24af
parent079dc3af583995b1fcbd84534f73feaf10adb596 (diff)
parentf8e562f69dd6547f7386956eea2edab361913cc0 (diff)
downloadandroid_packages_apps_DeskClock-43f89c23c5f4b4a8db0064f8e3831da68860316d.tar.gz
android_packages_apps_DeskClock-43f89c23c5f4b4a8db0064f8e3831da68860316d.tar.bz2
android_packages_apps_DeskClock-43f89c23c5f4b4a8db0064f8e3831da68860316d.zip
Do not assume current stopwatch lap has positive duration
am: f8e562f69d * commit 'f8e562f69dd6547f7386956eea2edab361913cc0': Do not assume current stopwatch lap has positive duration
-rw-r--r--src/com/android/deskclock/data/Stopwatch.java11
-rw-r--r--src/com/android/deskclock/data/StopwatchModel.java20
2 files changed, 15 insertions, 16 deletions
diff --git a/src/com/android/deskclock/data/Stopwatch.java b/src/com/android/deskclock/data/Stopwatch.java
index 9eb2b9cf2..a08cc1f0d 100644
--- a/src/com/android/deskclock/data/Stopwatch.java
+++ b/src/com/android/deskclock/data/Stopwatch.java
@@ -61,7 +61,11 @@ public final class Stopwatch {
return mAccumulatedTime;
}
- return mAccumulatedTime + (now() - mLastStartTime);
+ // 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 stopwatch is monotonically increasing, normalize negative time segments to 0,
+ final long timeSinceStart = now() - mLastStartTime;
+ return mAccumulatedTime + Math.max(0, timeSinceStart);
}
/**
@@ -79,7 +83,7 @@ public final class Stopwatch {
return this;
}
- return new Stopwatch(RUNNING, now(), mAccumulatedTime);
+ return new Stopwatch(RUNNING, now(), getTotalTime());
}
/**
@@ -90,8 +94,7 @@ public final class Stopwatch {
return this;
}
- final long accumulatedTime = mAccumulatedTime + (now() - mLastStartTime);
- return new Stopwatch(PAUSED, Long.MIN_VALUE, accumulatedTime);
+ return new Stopwatch(PAUSED, Long.MIN_VALUE, getTotalTime());
}
/**
diff --git a/src/com/android/deskclock/data/StopwatchModel.java b/src/com/android/deskclock/data/StopwatchModel.java
index 217a83016..a536c1fb4 100644
--- a/src/com/android/deskclock/data/StopwatchModel.java
+++ b/src/com/android/deskclock/data/StopwatchModel.java
@@ -175,21 +175,17 @@ final class StopwatchModel {
}
/**
- * @param time a point in time after the end of the last lap
- * @return the elapsed time between the given {@code time} and the end of the previous lap
+ * In practice, {@code time} can be any value due to device reboots. When the real-time clock is
+ * reset, there is no more guarantee that this time falls after the last recorded lap.
+ *
+ * @param time a point in time expected, but not required, to be after the end of the prior lap
+ * @return the elapsed time between the given {@code time} and the end of the prior lap;
+ * negative elapsed times are normalized to {@code 0}
*/
long getCurrentLapTime(long time) {
final Lap previousLap = getLaps().get(0);
-
- final long last = previousLap.getAccumulatedTime();
- final long lapTime = time - last;
-
- if (lapTime < 0) {
- final String message = String.format("time (%d) must exceed last lap (%d)", time, last);
- throw new IllegalArgumentException(message);
- }
-
- return lapTime;
+ final long currentLapTime = time - previousLap.getAccumulatedTime();
+ return Math.max(0, currentLapTime);
}
/**