From 6eecc3ce91432db9ee4f3ede0b4efd45f70885f0 Mon Sep 17 00:00:00 2001 From: Soonil Nagarkar Date: Wed, 18 Aug 2021 10:42:43 -0700 Subject: [PATCH] Only register stationary detector while idle The stationary detector takes some non-zero amount of power, this should help save power in the case where the device is not idle. Bug: 196997374 Test: manual + presubmit Change-Id: Ia1f0b85ee5ccb8a61b3f4514ccbeaf1eef36016b --- .../StationaryThrottlingLocationProvider.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/services/core/java/com/android/server/location/provider/StationaryThrottlingLocationProvider.java b/services/core/java/com/android/server/location/provider/StationaryThrottlingLocationProvider.java index 22a675ad39ab6..ad87c45308f81 100644 --- a/services/core/java/com/android/server/location/provider/StationaryThrottlingLocationProvider.java +++ b/services/core/java/com/android/server/location/provider/StationaryThrottlingLocationProvider.java @@ -105,20 +105,15 @@ public final class StationaryThrottlingLocationProvider extends DelegateLocation synchronized (mLock) { mDeviceIdleHelper.addListener(this); - mDeviceIdle = mDeviceIdleHelper.isDeviceIdle(); - mDeviceStationaryHelper.addListener(this); - mDeviceStationary = false; - mDeviceStationaryRealtimeMs = Long.MIN_VALUE; - - onThrottlingChangedLocked(false); + onDeviceIdleChanged(mDeviceIdleHelper.isDeviceIdle()); } } @Override protected void onStop() { synchronized (mLock) { - mDeviceStationaryHelper.removeListener(this); mDeviceIdleHelper.removeListener(this); + onDeviceIdleChanged(false); mIncomingRequest = ProviderRequest.EMPTY_REQUEST; mOutgoingRequest = ProviderRequest.EMPTY_REQUEST; @@ -151,13 +146,26 @@ public final class StationaryThrottlingLocationProvider extends DelegateLocation } mDeviceIdle = deviceIdle; - onThrottlingChangedLocked(false); + + if (deviceIdle) { + // device stationary helper will deliver an immediate listener update + mDeviceStationaryHelper.addListener(this); + } else { + mDeviceStationaryHelper.removeListener(this); + mDeviceStationary = false; + mDeviceStationaryRealtimeMs = Long.MIN_VALUE; + } } } @Override public void onDeviceStationaryChanged(boolean deviceStationary) { synchronized (mLock) { + if (!mDeviceIdle) { + // stationary detection is only registered while idle - ignore late notifications + return; + } + if (mDeviceStationary == deviceStationary) { return; }