Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 15e9e779 authored by Kweku Adams's avatar Kweku Adams Committed by Automerger Merge Worker
Browse files

Merge "Properly handle disabled location prefetch." into udc-dev am: 44d9e63f

parents 80df9bce 44d9e63f
Loading
Loading
Loading
Loading
+53 −36
Original line number Original line Diff line number Diff line
@@ -319,6 +319,8 @@ public class DeviceIdleController extends SystemService
    private SensorManager mSensorManager;
    private SensorManager mSensorManager;
    private final boolean mUseMotionSensor;
    private final boolean mUseMotionSensor;
    private Sensor mMotionSensor;
    private Sensor mMotionSensor;
    private final boolean mIsLocationPrefetchEnabled;
    @Nullable
    private LocationRequest mLocationRequest;
    private LocationRequest mLocationRequest;
    private Intent mIdleIntent;
    private Intent mIdleIntent;
    private Bundle mIdleIntentOptions;
    private Bundle mIdleIntentOptions;
@@ -2460,6 +2462,11 @@ public class DeviceIdleController extends SystemService
            return null;
            return null;
        }
        }


        boolean isLocationPrefetchEnabled() {
            return mContext.getResources().getBoolean(
                   com.android.internal.R.bool.config_autoPowerModePrefetchLocation);
        }

        boolean useMotionSensor() {
        boolean useMotionSensor() {
            return mContext.getResources().getBoolean(
            return mContext.getResources().getBoolean(
                   com.android.internal.R.bool.config_autoPowerModeUseMotionSensor);
                   com.android.internal.R.bool.config_autoPowerModeUseMotionSensor);
@@ -2489,6 +2496,7 @@ public class DeviceIdleController extends SystemService
        mAppStateTracker = mInjector.getAppStateTracker(context,
        mAppStateTracker = mInjector.getAppStateTracker(context,
                AppSchedulingModuleThread.get().getLooper());
                AppSchedulingModuleThread.get().getLooper());
        LocalServices.addService(AppStateTracker.class, mAppStateTracker);
        LocalServices.addService(AppStateTracker.class, mAppStateTracker);
        mIsLocationPrefetchEnabled = mInjector.isLocationPrefetchEnabled();
        mUseMotionSensor = mInjector.useMotionSensor();
        mUseMotionSensor = mInjector.useMotionSensor();
    }
    }


@@ -2602,8 +2610,7 @@ public class DeviceIdleController extends SystemService
                    mMotionSensor = mInjector.getMotionSensor();
                    mMotionSensor = mInjector.getMotionSensor();
                }
                }


                if (getContext().getResources().getBoolean(
                if (mIsLocationPrefetchEnabled) {
                        com.android.internal.R.bool.config_autoPowerModePrefetchLocation)) {
                    mLocationRequest = new LocationRequest.Builder(/*intervalMillis=*/ 0)
                    mLocationRequest = new LocationRequest.Builder(/*intervalMillis=*/ 0)
                        .setQuality(LocationRequest.QUALITY_HIGH_ACCURACY)
                        .setQuality(LocationRequest.QUALITY_HIGH_ACCURACY)
                        .setMaxUpdates(1)
                        .setMaxUpdates(1)
@@ -3779,10 +3786,12 @@ public class DeviceIdleController extends SystemService
            case STATE_SENSING:
            case STATE_SENSING:
                cancelSensingTimeoutAlarmLocked();
                cancelSensingTimeoutAlarmLocked();
                moveToStateLocked(STATE_LOCATING, reason);
                moveToStateLocked(STATE_LOCATING, reason);
                if (mIsLocationPrefetchEnabled) {
                    scheduleAlarmLocked(mConstants.LOCATING_TIMEOUT);
                    scheduleAlarmLocked(mConstants.LOCATING_TIMEOUT);
                    LocationManager locationManager = mInjector.getLocationManager();
                    LocationManager locationManager = mInjector.getLocationManager();
                    if (locationManager != null
                    if (locationManager != null
                        && locationManager.getProvider(LocationManager.FUSED_PROVIDER) != null) {
                            && locationManager.getProvider(LocationManager.FUSED_PROVIDER)
                                    != null) {
                        locationManager.requestLocationUpdates(LocationManager.FUSED_PROVIDER,
                        locationManager.requestLocationUpdates(LocationManager.FUSED_PROVIDER,
                                mLocationRequest,
                                mLocationRequest,
                                AppSchedulingModuleThread.getExecutor(),
                                AppSchedulingModuleThread.getExecutor(),
@@ -3794,8 +3803,8 @@ public class DeviceIdleController extends SystemService
                    if (locationManager != null
                    if (locationManager != null
                            && locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
                            && locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
                        mHasGps = true;
                        mHasGps = true;
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5,
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                            mGpsLocationListener, mHandler.getLooper());
                                1000, 5, mGpsLocationListener, mHandler.getLooper());
                        mLocating = true;
                        mLocating = true;
                    } else {
                    } else {
                        mHasGps = false;
                        mHasGps = false;
@@ -3805,8 +3814,12 @@ public class DeviceIdleController extends SystemService
                    if (mLocating) {
                    if (mLocating) {
                        break;
                        break;
                    }
                    }

                    // Otherwise, we have to move from locating into idle maintenance.
                    // Otherwise, we have to move from locating into idle maintenance.
                } else {
                    mLocating = false;
                }

                // We're not doing any locating work, so move on to the next state.
            case STATE_LOCATING:
            case STATE_LOCATING:
                cancelAlarmLocked();
                cancelAlarmLocked();
                cancelLocatingLocked();
                cancelLocatingLocked();
@@ -5303,6 +5316,7 @@ public class DeviceIdleController extends SystemService
                pw.print("  "); pw.print(mStationaryListeners.size());
                pw.print("  "); pw.print(mStationaryListeners.size());
                pw.println(" stationary listeners registered");
                pw.println(" stationary listeners registered");
            }
            }
            if (mIsLocationPrefetchEnabled) {
                pw.print("  mLocating="); pw.print(mLocating);
                pw.print("  mLocating="); pw.print(mLocating);
                pw.print(" mHasGps="); pw.print(mHasGps);
                pw.print(" mHasGps="); pw.print(mHasGps);
                pw.print(" mHasFused="); pw.print(mHasFusedLocation);
                pw.print(" mHasFused="); pw.print(mHasFusedLocation);
@@ -5313,6 +5327,9 @@ public class DeviceIdleController extends SystemService
                if (mLastGpsLocation != null) {
                if (mLastGpsLocation != null) {
                    pw.print("  mLastGpsLocation="); pw.println(mLastGpsLocation);
                    pw.print("  mLastGpsLocation="); pw.println(mLastGpsLocation);
                }
                }
            } else {
                pw.println("  Location prefetching disabled");
            }
            pw.print("  mState="); pw.print(stateToString(mState));
            pw.print("  mState="); pw.print(stateToString(mState));
            pw.print(" mLightState=");
            pw.print(" mLightState=");
            pw.println(lightStateToString(mLightState));
            pw.println(lightStateToString(mLightState));
+83 −0
Original line number Original line Diff line number Diff line
@@ -154,6 +154,7 @@ public class DeviceIdleControllerTest {
        // Freeze time for testing.
        // Freeze time for testing.
        long nowElapsed;
        long nowElapsed;
        boolean useMotionSensor = true;
        boolean useMotionSensor = true;
        boolean isLocationPrefetchEnabled = true;


        InjectorForTest(Context ctx) {
        InjectorForTest(Context ctx) {
            super(ctx);
            super(ctx);
@@ -222,6 +223,11 @@ public class DeviceIdleControllerTest {
            return mMotionSensor;
            return mMotionSensor;
        }
        }


        @Override
        boolean isLocationPrefetchEnabled() {
            return isLocationPrefetchEnabled;
        }

        @Override
        @Override
        PowerManager getPowerManager() {
        PowerManager getPowerManager() {
            return mPowerManager;
            return mPowerManager;
@@ -990,6 +996,43 @@ public class DeviceIdleControllerTest {
        verifyStateConditions(STATE_IDLE_MAINTENANCE);
        verifyStateConditions(STATE_IDLE_MAINTENANCE);
    }
    }


    @Test
    public void testStepIdleStateLocked_ValidStates_LocationPrefetchDisabled() {
        mInjector.locationManager = mLocationManager;
        mInjector.isLocationPrefetchEnabled = false;
        cleanupDeviceIdleController();
        setupDeviceIdleController();
        doReturn(mock(LocationProvider.class)).when(mLocationManager).getProvider(anyString());
        // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
        setAlarmSoon(false);
        // Set state to INACTIVE.
        mDeviceIdleController.becomeActiveLocked("testing", 0);
        setChargingOn(false);
        setScreenOn(false);
        verifyStateConditions(STATE_INACTIVE);

        mDeviceIdleController.stepIdleStateLocked("testing");
        verifyStateConditions(STATE_IDLE_PENDING);

        mDeviceIdleController.stepIdleStateLocked("testing");
        verifyStateConditions(STATE_SENSING);

        mDeviceIdleController.stepIdleStateLocked("testing");
        // Prefetch location is off, so SENSING should go straight through to IDLE.
        verifyStateConditions(STATE_IDLE);

        // Should just alternate between IDLE and IDLE_MAINTENANCE now.

        mDeviceIdleController.stepIdleStateLocked("testing");
        verifyStateConditions(STATE_IDLE_MAINTENANCE);

        mDeviceIdleController.stepIdleStateLocked("testing");
        verifyStateConditions(STATE_IDLE);

        mDeviceIdleController.stepIdleStateLocked("testing");
        verifyStateConditions(STATE_IDLE_MAINTENANCE);
    }

    @Test
    @Test
    public void testStepIdleStateLocked_ValidStates_WithLocationManager_NoProviders() {
    public void testStepIdleStateLocked_ValidStates_WithLocationManager_NoProviders() {
        // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
        // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
@@ -1023,6 +1066,46 @@ public class DeviceIdleControllerTest {
        verifyStateConditions(STATE_IDLE_MAINTENANCE);
        verifyStateConditions(STATE_IDLE_MAINTENANCE);
    }
    }


    @Test
    public void testStepIdleStateLocked_ValidStates_WithLocationManager_MissingProviders() {
        mInjector.locationManager = mLocationManager;
        doReturn(null).when(mLocationManager)
                .getProvider(eq(LocationManager.FUSED_PROVIDER));
        doReturn(null).when(mLocationManager)
                .getProvider(eq(LocationManager.GPS_PROVIDER));
        doReturn(mock(LocationProvider.class)).when(mLocationManager)
                .getProvider(eq(LocationManager.NETWORK_PROVIDER));
        // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
        setAlarmSoon(false);
        // Set state to INACTIVE.
        mDeviceIdleController.becomeActiveLocked("testing", 0);
        setChargingOn(false);
        setScreenOn(false);
        verifyStateConditions(STATE_INACTIVE);

        mDeviceIdleController.stepIdleStateLocked("testing");
        verifyStateConditions(STATE_IDLE_PENDING);

        mDeviceIdleController.stepIdleStateLocked("testing");
        verifyStateConditions(STATE_SENSING);

        mDeviceIdleController.stepIdleStateLocked("testing");
        // Location manager exists, but the required providers don't exist,
        // so SENSING should go straight through to IDLE.
        verifyStateConditions(STATE_IDLE);

        // Should just alternate between IDLE and IDLE_MAINTENANCE now.

        mDeviceIdleController.stepIdleStateLocked("testing");
        verifyStateConditions(STATE_IDLE_MAINTENANCE);

        mDeviceIdleController.stepIdleStateLocked("testing");
        verifyStateConditions(STATE_IDLE);

        mDeviceIdleController.stepIdleStateLocked("testing");
        verifyStateConditions(STATE_IDLE_MAINTENANCE);
    }

    @Test
    @Test
    public void testStepIdleStateLocked_ValidStates_WithLocationManager_WithProviders() {
    public void testStepIdleStateLocked_ValidStates_WithLocationManager_WithProviders() {
        mInjector.locationManager = mLocationManager;
        mInjector.locationManager = mLocationManager;