From 03616a4072f24a635579341a55bf2ae5ff2c5469 Mon Sep 17 00:00:00 2001 From: Soonil Nagarkar Date: Wed, 5 Aug 2020 15:23:11 -0700 Subject: [PATCH] Fix a provider bug and remove redundant GNSS logic This fixes a bug where screen on/off state changing was not refreshing the active state of registrations. In addition remove code from GnssLocationProvider that is now redundant (all the throttling logic is handled in the manager now). Finally this removes a broken test from the fused provider tests. This was broken by the last change because FusedLocationServiceTest is not allowlisted for being able to use bypass requests (to be honest, I'm not sure why this test was passing previously...). FusedLocationServiceTest is not a great test, so I am removing it to fix tests for now while I consider a better approach. Bug: 162949881 Bug: 162950120 Test: manual + presubmit (atest BatterySaverLocationTest) Change-Id: Ide762b352014da4aa42959d479f2e45875ec19af --- .../fused/tests/FusedLocationServiceTest.java | 35 ------------------- .../location/LocationProviderManager.java | 2 +- .../location/gnss/GnssLocationProvider.java | 18 ---------- .../location/LocationProviderManagerTest.java | 16 +++++++++ 4 files changed, 17 insertions(+), 54 deletions(-) diff --git a/packages/FusedLocation/test/src/com/android/location/fused/tests/FusedLocationServiceTest.java b/packages/FusedLocation/test/src/com/android/location/fused/tests/FusedLocationServiceTest.java index 8efbad64f3d22..2c4545e7b2652 100644 --- a/packages/FusedLocation/test/src/com/android/location/fused/tests/FusedLocationServiceTest.java +++ b/packages/FusedLocation/test/src/com/android/location/fused/tests/FusedLocationServiceTest.java @@ -155,41 +155,6 @@ public class FusedLocationServiceTest { assertThat(mManager.getNextLocation(TIMEOUT_MS)).isEqualTo(location); } - @Test - public void testBypassRequest() throws Exception { - LocationRequest request = LocationRequest.createFromDeprecatedProvider(FUSED_PROVIDER, 1000, - 0, false).setQuality(LocationRequest.POWER_HIGH).setLocationSettingsIgnored(true); - - mProvider.setRequest( - new ProviderRequest.Builder() - .setInterval(1000) - .setLocationSettingsIgnored(true) - .setLocationRequests(Collections.singletonList(request)) - .build(), - new WorkSource()); - - boolean containsNetworkBypass = false; - for (LocationRequest iRequest : mLocationManager.getTestProviderCurrentRequests( - NETWORK_PROVIDER)) { - if (iRequest.isLocationSettingsIgnored()) { - containsNetworkBypass = true; - break; - } - } - - boolean containsGpsBypass = false; - for (LocationRequest iRequest : mLocationManager.getTestProviderCurrentRequests( - GPS_PROVIDER)) { - if (iRequest.isLocationSettingsIgnored()) { - containsGpsBypass = true; - break; - } - } - - assertThat(containsNetworkBypass).isTrue(); - assertThat(containsGpsBypass).isTrue(); - } - private static class LocationProviderManagerCapture extends ILocationProviderManager.Stub { private final LinkedBlockingQueue mLocations; diff --git a/services/core/java/com/android/server/location/LocationProviderManager.java b/services/core/java/com/android/server/location/LocationProviderManager.java index d4f8c7e855b9f..c3532a84c42df 100644 --- a/services/core/java/com/android/server/location/LocationProviderManager.java +++ b/services/core/java/com/android/server/location/LocationProviderManager.java @@ -1615,7 +1615,7 @@ class LocationProviderManager extends case LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF: // fall through case LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF: - updateService(); + updateRegistrations(registration -> true); break; default: break; diff --git a/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java b/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java index 8004ec70aaf3c..850cf7f4b7ce8 100644 --- a/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java +++ b/services/core/java/com/android/server/location/gnss/GnssLocationProvider.java @@ -49,8 +49,6 @@ import android.os.Looper; import android.os.Message; import android.os.PersistableBundle; import android.os.PowerManager; -import android.os.PowerManager.ServiceType; -import android.os.PowerSaveState; import android.os.RemoteException; import android.os.ServiceManager; import android.os.SystemClock; @@ -486,10 +484,6 @@ public class GnssLocationProvider extends AbstractLocationProvider implements deviceIdleService.unregisterStationaryListener( mDeviceIdleStationaryListener); } - // Intentional fall-through. - case PowerManager.ACTION_POWER_SAVE_MODE_CHANGED: - case Intent.ACTION_SCREEN_OFF: - case Intent.ACTION_SCREEN_ON: // Call updateLowPowerMode on handler thread so it's always called from the // same thread. mHandler.sendEmptyMessage(UPDATE_LOW_POWER_MODE); @@ -554,15 +548,6 @@ public class GnssLocationProvider extends AbstractLocationProvider implements private void updateLowPowerMode() { // Disable GPS if we are in device idle mode and the device is stationary. boolean disableGpsForPowerManager = mPowerManager.isDeviceIdleMode() && mIsDeviceStationary; - final PowerSaveState result = mPowerManager.getPowerSaveState(ServiceType.LOCATION); - switch (result.locationMode) { - case PowerManager.LOCATION_MODE_GPS_DISABLED_WHEN_SCREEN_OFF: - case PowerManager.LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF: - // If we are in battery saver mode and the screen is off, disable GPS. - disableGpsForPowerManager |= - result.batterySaverEnabled && !mPowerManager.isInteractive(); - break; - } if (disableGpsForPowerManager != mDisableGpsForPowerManager) { mDisableGpsForPowerManager = disableGpsForPowerManager; updateEnabled(); @@ -1959,10 +1944,7 @@ public class GnssLocationProvider extends AbstractLocationProvider implements IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ALARM_WAKEUP); intentFilter.addAction(ALARM_TIMEOUT); - intentFilter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED); intentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); - intentFilter.addAction(Intent.ACTION_SCREEN_OFF); - intentFilter.addAction(Intent.ACTION_SCREEN_ON); intentFilter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); intentFilter.addAction(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED); mContext.registerReceiver(mBroadcastReceiver, intentFilter, null, this); diff --git a/services/tests/mockingservicestests/src/com/android/server/location/LocationProviderManagerTest.java b/services/tests/mockingservicestests/src/com/android/server/location/LocationProviderManagerTest.java index 1cb004a6dc1ee..fdcadf3e30882 100644 --- a/services/tests/mockingservicestests/src/com/android/server/location/LocationProviderManagerTest.java +++ b/services/tests/mockingservicestests/src/com/android/server/location/LocationProviderManagerTest.java @@ -25,6 +25,7 @@ import static android.location.Criteria.ACCURACY_COARSE; import static android.location.Criteria.ACCURACY_FINE; import static android.location.Criteria.POWER_HIGH; import static android.location.LocationManager.PASSIVE_PROVIDER; +import static android.os.PowerManager.LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF; import static androidx.test.ext.truth.location.LocationSubject.assertThat; @@ -907,6 +908,21 @@ public class LocationProviderManagerTest { assertThat(mProvider.getRequest().interval).isEqualTo(5); } + @Test + public void testProviderRequest_BatterySaver_ScreenOnOff() { + mInjector.getLocationPowerSaveModeHelper().setLocationPowerSaveMode( + LOCATION_MODE_THROTTLE_REQUESTS_WHEN_SCREEN_OFF); + + ILocationListener listener = createMockLocationListener(); + LocationRequest request = LocationRequest.createFromDeprecatedProvider(NAME, 5, 0, false); + mManager.registerLocationRequest(request, IDENTITY, PERMISSION_FINE, listener); + + assertThat(mProvider.getRequest().reportLocation).isTrue(); + + mInjector.getScreenInteractiveHelper().setScreenInteractive(false); + assertThat(mProvider.getRequest().reportLocation).isFalse(); + } + private ILocationListener createMockLocationListener() { return spy(new ILocationListener.Stub() { @Override