[Android] Add timer interfaces and implementations to base/time
Currently, most code uses System#currentTimeMillis or a SystemClock
method. Measuring elapsed time is typically implemented manually. Which
of these is used is ad-hoc and often based on pattern-matching existing
code.
This can be problematic because currentTimeMillis is often a poor choice
for interval timing, and using a static method directly makes testing
harder. Manually implementing measurement is simple enough but increases
the state space of classes and also makes testing harder as a result.
Pattern matching existing code can produce bugs if that code has subtly
different requirements (e.g. if it needs a timestamp that's stable
across restarts).
This CL introduces a timer interface and a standard set of timer
implementations with documentation on when to use each and why. This
includes a test-specific timer that can be wired in unit tests.
Bug: 1194269
Change-Id: I2f1d99d32832f94a3ee12295e9f827adc8d65c98
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2767399
Reviewed-by: Tommy Nyquist <nyquist@chromium.org>
Commit-Queue: Patrick Noland <pnoland@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1001756}
CrOS-Libchrome-Original-Commit: 602692dc9d255d82e1373b4cdc7ca9a3c903c79f
8 files changed