diff options
author | Jeff Brown <jeffbrown@google.com> | 2013-09-10 17:13:28 -0700 |
---|---|---|
committer | Jeff Brown <jeffbrown@google.com> | 2013-09-10 17:17:08 -0700 |
commit | 20e287534396655af0d5912d8b272070ad24a93a (patch) | |
tree | f005b2a26dae5dffae7f3aec765cd909a42d34eb /services | |
parent | bd181bb83282571eb793d2e4a48b4d19585de7f2 (diff) | |
download | frameworks_base-20e287534396655af0d5912d8b272070ad24a93a.zip frameworks_base-20e287534396655af0d5912d8b272070ad24a93a.tar.gz frameworks_base-20e287534396655af0d5912d8b272070ad24a93a.tar.bz2 |
WirelessChargerDetector: Work around bad sensor timestamps.
It turns out that on some devices the sensor event timestamp
may be non-monotonic. This may cause the detector to hold
a wakelock indefinitely if the time jumps backwards. These
timestamps are not well tested in general so there may be
other artifacts as well. Use elapsed realtime instead.
Bug: 9926451
Change-Id: Idb0b316e22b23aac86837bd25b953daf49f9b758
Diffstat (limited to 'services')
-rw-r--r-- | services/java/com/android/server/power/WirelessChargerDetector.java | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/services/java/com/android/server/power/WirelessChargerDetector.java b/services/java/com/android/server/power/WirelessChargerDetector.java index ac6dc3e..35920f7 100644 --- a/services/java/com/android/server/power/WirelessChargerDetector.java +++ b/services/java/com/android/server/power/WirelessChargerDetector.java @@ -21,6 +21,7 @@ import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.BatteryManager; +import android.os.SystemClock; import android.util.Slog; import java.io.PrintWriter; @@ -130,6 +131,10 @@ final class WirelessChargerDetector { private long mFirstSampleTime; private float mFirstSampleX, mFirstSampleY, mFirstSampleZ; + // The time and value of the last sample that was collected (for debugging only). + private long mLastSampleTime; + private float mLastSampleX, mLastSampleY, mLastSampleZ; + public WirelessChargerDetector(SensorManager sensorManager, SuspendBlocker suspendBlocker) { mSensorManager = sensorManager; @@ -153,6 +158,9 @@ final class WirelessChargerDetector { pw.println(" mFirstSampleTime=" + mFirstSampleTime); pw.println(" mFirstSampleX=" + mFirstSampleX + ", mFirstSampleY=" + mFirstSampleY + ", mFirstSampleZ=" + mFirstSampleZ); + pw.println(" mLastSampleTime=" + mLastSampleTime); + pw.println(" mLastSampleX=" + mLastSampleX + + ", mLastSampleY=" + mLastSampleY + ", mLastSampleZ=" + mLastSampleZ); } } @@ -224,6 +232,11 @@ final class WirelessChargerDetector { return; } + mLastSampleTime = timeNanos; + mLastSampleX = x; + mLastSampleY = y; + mLastSampleZ = z; + mTotalSamples += 1; if (mTotalSamples == 1) { // Save information about the first sample collected. @@ -310,7 +323,10 @@ final class WirelessChargerDetector { private final SensorEventListener mListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { - processSample(event.timestamp, event.values[0], event.values[1], event.values[2]); + // We use SystemClock.elapsedRealtimeNanos() instead of event.timestamp because + // on some devices the sensor HAL may produce timestamps that are not monotonic. + processSample(SystemClock.elapsedRealtimeNanos(), + event.values[0], event.values[1], event.values[2]); } @Override |