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

Commit 1a12af54 authored by Nick Chameyev's avatar Nick Chameyev
Browse files

[FoldableDeviceStateProvider] Do not listen to hall sensor when not used

Disables listening to hall sensor when the new
posture based closed state flag is enabled.

When this flag is enabled we do not rely on hall
sensor and use hinge angle sensor instead.

Hall sensor is a wake-up sensor and it might
waste power by waking up CPU when not needed.

Test: manual check in sensor service dumpsys
  that we don't listen to hall sensor anymore
Test: atest BookStyleDeviceStatePolicyTest
Bug: 335386446
Change-Id: I89406859d5df23d17eea73113d4c46b273dd778b
parent bab84f32
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -90,6 +90,12 @@ public class BookStyleDeviceStatePolicy extends DeviceStatePolicy implements
        final DisplayManager displayManager = mContext.getSystemService(DisplayManager.class);

        mEnablePostureBasedClosedState = featureFlags.enableFoldablesPostureBasedClosedState();
        if (mEnablePostureBasedClosedState) {
            // This configuration doesn't require listening to hall sensor, it solely relies
            // on the fused hinge angle sensor
            hallSensor = null;
        }

        mIsDualDisplayBlockingEnabled = featureFlags.enableDualDisplayBlocking();

        final DeviceStateConfiguration[] configuration = createConfiguration(
+9 −6
Original line number Diff line number Diff line
@@ -96,6 +96,7 @@ public final class FoldableDeviceStateProvider implements DeviceStateProvider,

    private final Sensor mHingeAngleSensor;
    private final DisplayManager mDisplayManager;
    @Nullable
    private final Sensor mHallSensor;
    private static final Predicate<FoldableDeviceStateProvider> ALLOWED = p -> true;

@@ -123,7 +124,7 @@ public final class FoldableDeviceStateProvider implements DeviceStateProvider,
            @NonNull Context context,
            @NonNull SensorManager sensorManager,
            @NonNull Sensor hingeAngleSensor,
            @NonNull Sensor hallSensor,
            @Nullable Sensor hallSensor,
            @NonNull DisplayManager displayManager,
            @NonNull DeviceStateConfiguration[] deviceStateConfigurations) {
        this(new FeatureFlagsImpl(), context, sensorManager, hingeAngleSensor, hallSensor,
@@ -136,7 +137,7 @@ public final class FoldableDeviceStateProvider implements DeviceStateProvider,
            @NonNull Context context,
            @NonNull SensorManager sensorManager,
            @NonNull Sensor hingeAngleSensor,
            @NonNull Sensor hallSensor,
            @Nullable Sensor hallSensor,
            @NonNull DisplayManager displayManager,
            @NonNull DeviceStateConfiguration[] deviceStateConfigurations) {

@@ -150,7 +151,9 @@ public final class FoldableDeviceStateProvider implements DeviceStateProvider,
        mIsDualDisplayBlockingEnabled = featureFlags.enableDualDisplayBlocking();

        sensorManager.registerListener(this, mHingeAngleSensor, SENSOR_DELAY_FASTEST);
        if (hallSensor != null) {
            sensorManager.registerListener(this, mHallSensor, SENSOR_DELAY_FASTEST);
        }

        mOrderedStates = new DeviceState[deviceStateConfigurations.length];
        for (int i = 0; i < deviceStateConfigurations.length; i++) {
@@ -326,7 +329,7 @@ public final class FoldableDeviceStateProvider implements DeviceStateProvider,
    @Override
    public void onSensorChanged(SensorEvent event) {
        synchronized (mLock) {
            if (event.sensor == mHallSensor) {
            if (mHallSensor != null && event.sensor == mHallSensor) {
                mLastHallSensorEvent = event;
            } else if (event.sensor == mHingeAngleSensor) {
                mLastHingeAngleSensorEvent = event;
@@ -361,11 +364,11 @@ public final class FoldableDeviceStateProvider implements DeviceStateProvider,
    }

    @GuardedBy("mLock")
    private void dumpSensorValues(Sensor sensor, @Nullable SensorEvent event) {
    private void dumpSensorValues(@Nullable Sensor sensor, @Nullable SensorEvent event) {
        Slog.i(TAG, toSensorValueString(sensor, event));
    }

    private String toSensorValueString(Sensor sensor, @Nullable SensorEvent event) {
    private String toSensorValueString(@Nullable Sensor sensor, @Nullable SensorEvent event) {
        String sensorString = sensor == null ? "null" : sensor.getName();
        String eventValues = event == null ? "null" : Arrays.toString(event.values);
        return sensorString + " : " + eventValues;
+20 −0
Original line number Diff line number Diff line
@@ -218,6 +218,26 @@ public final class BookStyleDeviceStatePolicyTest {
        setScreenOn(true);
    }

    @Test
    public void test_postureBasedClosedState_createPolicy_doesNotRegisterHallSensor() {
        mFakeFeatureFlags.setFlag(Flags.FLAG_ENABLE_FOLDABLES_POSTURE_BASED_CLOSED_STATE, true);
        clearInvocations(mSensorManager);

        mInstrumentation.runOnMainSync(() -> mProvider = createProvider());

        verify(mSensorManager, never()).registerListener(any(), eq(mHallSensor), anyInt());
    }

    @Test
    public void test_postureBasedClosedStateDisabled_createPolicy_registersHallSensor() {
        mFakeFeatureFlags.setFlag(Flags.FLAG_ENABLE_FOLDABLES_POSTURE_BASED_CLOSED_STATE, false);
        clearInvocations(mSensorManager);

        mInstrumentation.runOnMainSync(() -> mProvider = createProvider());

        verify(mSensorManager).registerListener(any(), eq(mHallSensor), anyInt());
    }

    @Test
    public void test_noSensorEventsYet_reportOpenedState() {
        mProvider.setListener(mListener);