From d48954fb6bd5fb3170ea44b95c87ed2b7a70d535 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Wed, 22 Jul 2015 17:20:33 -0700 Subject: [PATCH] Fix issue #22414729: Checkin start clock time is different from hsitory reset time There was a mistake in the code that was supposed to recover from the initial time on a new device being bad until the real time ultimately gets set, which was causing us to update the start clock time every time there was a time change (instead of just when the original start time appears bad). Rework all of this, so we now count the start time as bad if it is more than one year before the current time, only modifying it in that case. Also when modifying it, adjust the time we set it to take in to account how much realtime has actually elapsed so far in the battery stats. Change-Id: If74bd711d9b7618c8f6148a9935c452aaaa7e257 --- .../android/internal/os/BatteryStatsImpl.java | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java index 60f47d673c180..e7c58f45c18c7 100644 --- a/core/java/com/android/internal/os/BatteryStatsImpl.java +++ b/core/java/com/android/internal/os/BatteryStatsImpl.java @@ -2560,14 +2560,24 @@ public final class BatteryStatsImpl extends BatteryStats { addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid); } + boolean ensureStartClockTime(final long currentTime) { + final long ABOUT_ONE_YEAR = 365*24*60*60*1000L; + if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) { + // If the start clock time has changed by more than a year, then presumably + // the previous time was completely bogus. So we are going to figure out a + // new time based on how much time has elapsed since we started counting. + mStartClockTime = currentTime - (SystemClock.elapsedRealtime()-(mRealtimeStart/1000)); + return true; + } + return false; + } + public void noteCurrentTimeChangedLocked() { final long currentTime = System.currentTimeMillis(); final long elapsedRealtime = SystemClock.elapsedRealtime(); final long uptime = SystemClock.uptimeMillis(); recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime); - if (isStartClockTimeValid()) { - mStartClockTime = currentTime; - } + ensureStartClockTime(currentTime); } public void noteProcessStartLocked(String name, int uid) { @@ -4306,19 +4316,11 @@ public final class BatteryStatsImpl extends BatteryStats { } } - boolean isStartClockTimeValid() { - return mStartClockTime > 365*24*60*60*1000L; - } - @Override public long getStartClockTime() { - if (!isStartClockTimeValid()) { - // If the last clock time we got was very small, then we hadn't had a real - // time yet, so try to get it again. - mStartClockTime = System.currentTimeMillis(); - if (isStartClockTimeValid()) { - recordCurrentTimeChangeLocked(mStartClockTime, SystemClock.elapsedRealtime(), - SystemClock.uptimeMillis()); - } + final long currentTime = System.currentTimeMillis(); + if (ensureStartClockTime(currentTime)) { + recordCurrentTimeChangeLocked(currentTime, SystemClock.elapsedRealtime(), + SystemClock.uptimeMillis()); } return mStartClockTime; }