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

Commit ff603f83 authored by Nick Chameyev's avatar Nick Chameyev Committed by Android (Google) Code Review
Browse files

Merge "[FoldableDeviceStateProvider] Do not listen to hall sensor when not used" into 24D1-dev

parents d8b44308 1a12af54
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);