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

Commit 0dd261a5 authored by Shivangi Dubey's avatar Shivangi Dubey Committed by Android (Google) Code Review
Browse files

Merge "Ensure fast-fail when config corrupted" into main

parents db8d9a63 aea04bde
Loading
Loading
Loading
Loading
+27 −31
Original line number Diff line number Diff line
@@ -125,27 +125,25 @@ public class DeviceStateAutoRotateSettingManagerImpl implements
                DEVICE_STATE_ROTATION_LOCK,
                UserHandle.USER_CURRENT);
        if (serializedSetting == null || serializedSetting.isEmpty()) return null;
        try {
        final String[] deserializedSettings = serializedSetting.split(SEPARATOR_REGEX);
        if (deserializedSettings.length % 2 != 0) {
                throw new IllegalStateException("Odd number of elements in the list");
            Log.e(TAG, "Invalid format in serializedSetting=" + serializedSetting
                    + "\nOdd number of elements in the list");
            return null;
        }
        final SparseIntArray deviceStateAutoRotateSetting = new SparseIntArray();
        for (int i = 0; i < deserializedSettings.length; i += 2) {
            final int key = Integer.parseInt(deserializedSettings[i]);
            final int value = Integer.parseInt(deserializedSettings[i + 1]);
            if (value < 0 || value > 2) {
                    throw new IllegalStateException(
                            "Invalid value in pair: key=" + deserializedSettings[i] + ", value="
                Log.e(TAG, "Invalid format in serializedSetting=" + serializedSetting
                        + "\nInvalid value in pair: key=" + deserializedSettings[i] + ", value="
                        + deserializedSettings[i + 1]);
                return null;
            }
            deviceStateAutoRotateSetting.put(key, value);
        }
        return deviceStateAutoRotateSetting;
        } catch (Exception e) {
            Log.w(TAG, "Invalid format in serializedSetting=" + serializedSetting, e);
            return null;
        }
    }

    @Override
@@ -213,7 +211,6 @@ public class DeviceStateAutoRotateSettingManagerImpl implements
                        R.array.config_perDeviceStateRotationLockDefaults);
        for (String entry : perDeviceStateAutoRotateDefaults) {
            final PostureEntry parsedEntry = parsePostureEntry(entry);
            if (parsedEntry == null) return;

            final int posture = parsedEntry.posture;
            final int autoRotateValue = parsedEntry.autoRotateValue;
@@ -221,28 +218,28 @@ public class DeviceStateAutoRotateSettingManagerImpl implements
            final Integer deviceState = mPostureDeviceStateConverter.postureToDeviceState(posture);

            if (deviceState == null) {
                Log.wtf(TAG, "No matching device state for posture: " + posture);
            } else {
                mSettableDeviceState.add(new SettableDeviceState(deviceState,
                        autoRotateValue != DEVICE_STATE_ROTATION_LOCK_IGNORED)
                );
                throw new IllegalStateException("No matching device state for posture: " + posture);
            }
            mSettableDeviceState.add(new SettableDeviceState(deviceState,
                    autoRotateValue != DEVICE_STATE_ROTATION_LOCK_IGNORED));

            if (autoRotateValue == DEVICE_STATE_ROTATION_LOCK_IGNORED
                    && fallbackPosture != null) {
                mFallbackPostureMap.put(posture, fallbackPosture);
            } else if (autoRotateValue == DEVICE_STATE_ROTATION_LOCK_IGNORED) {
                Log.w(TAG, "Auto rotate setting is IGNORED, but no fallback-posture defined");
                throw new IllegalStateException(
                        "Auto rotate setting is IGNORED for posture=" + posture
                                + ", but no fallback-posture defined");
            }
            mDefaultDeviceStateAutoRotateSetting.put(posture, autoRotateValue);
        }
    }

    @NonNull
    private PostureEntry parsePostureEntry(String entry) {
        final String[] values = entry.split(SEPARATOR_REGEX);
        if (values.length < 2 || values.length > 3) { // It should contain 2 or 3 values.
            Log.wtf(TAG, "Invalid number of values in entry: " + entry);
            return null;
            throw new IllegalStateException("Invalid number of values in entry: " + entry);
        }
        try {
            final int posture = Integer.parseInt(values[0]);
@@ -253,12 +250,11 @@ public class DeviceStateAutoRotateSettingManagerImpl implements
            return new PostureEntry(posture, autoRotateValue, fallbackPosture);

        } catch (NumberFormatException e) {
            Log.wtf(TAG, "Invalid number format in '" + entry + "'", e);
            return null;
            throw new IllegalStateException(
                    "Invalid number format in '" + entry + "'" + e.getMessage());
        }
    }


    private Integer extractSettingForDevicePosture(
            int devicePosture,
            SparseIntArray deviceStateAutoRotateSetting
+147 −15
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import androidx.test.filters.SmallTest
import com.android.internal.R
import com.android.settingslib.devicestate.DeviceStateAutoRotateSettingManager.DeviceStateAutoRotateSettingListener
import com.google.common.truth.Truth.assertThat
import org.junit.Assert.assertThrows
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@@ -328,6 +329,111 @@ class DeviceStateAutoRotateSettingManagerImplTest {
        assertThat(expectedPairs.size).isEqualTo(defaultDeviceStateAutoRotateSetting.size())
    }

    @Test
    fun loadAutoRotateDeviceStates_missingDeviceStateForPosture_throwsException() {
        whenever(
            mMockPostureDeviceStateConverter.postureToDeviceState(
                eq(
                    DEVICE_STATE_ROTATION_KEY_UNFOLDED
                )
            )
        ).thenReturn(null)

        val exception = assertThrows(IllegalStateException::class.java) {
            settingManager =
                DeviceStateAutoRotateSettingManagerImpl(
                    mockContext,
                    executor,
                    fakeSecureSettings,
                    mockHandler,
                    mMockPostureDeviceStateConverter,
                )
        }
        assertThat(exception.message).contains(
            "No matching device state for posture: "
                    + "$DEVICE_STATE_ROTATION_KEY_UNFOLDED"
        )
    }

    @Test
    fun loadAutoRotateDeviceStates_missingFallbackPosture_throwsException() {
        whenever(mockResources.getStringArray(R.array.config_perDeviceStateRotationLockDefaults))
            .thenReturn(
                arrayOf(
                    "$DEVICE_STATE_ROTATION_KEY_HALF_FOLDED:$DEVICE_STATE_ROTATION_LOCK_IGNORED",
                    "$DEVICE_STATE_ROTATION_KEY_UNFOLDED:$DEVICE_STATE_ROTATION_LOCK_LOCKED"
                )
            )

        val exception = assertThrows(IllegalStateException::class.java) {
            settingManager =
                DeviceStateAutoRotateSettingManagerImpl(
                    mockContext,
                    executor,
                    fakeSecureSettings,
                    mockHandler,
                    mMockPostureDeviceStateConverter,
                )
        }
        assertThat(exception.message).contains(
            "Auto rotate setting is IGNORED for posture=" + DEVICE_STATE_ROTATION_KEY_HALF_FOLDED
                    + ", but no fallback-posture defined"
        )
    }

    @Test
    fun loadAutoRotateDeviceStates_invalidNumberOfElementsInEntry_throwsException() {
        whenever(mockResources.getStringArray(R.array.config_perDeviceStateRotationLockDefaults))
            .thenReturn(
                arrayOf(
                    "$DEVICE_STATE_ROTATION_KEY_HALF_FOLDED",
                    "$DEVICE_STATE_ROTATION_KEY_UNFOLDED:$DEVICE_STATE_ROTATION_LOCK_LOCKED",
                    "$DEVICE_STATE_ROTATION_KEY_FOLDED:$DEVICE_STATE_ROTATION_LOCK_LOCKED"
                )
            )

        val exception = assertThrows(IllegalStateException::class.java) {
            settingManager =
                DeviceStateAutoRotateSettingManagerImpl(
                    mockContext,
                    executor,
                    fakeSecureSettings,
                    mockHandler,
                    mMockPostureDeviceStateConverter,
                )
        }
        assertThat(exception.message).contains(
            "Invalid number of values in entry: "
                    + "$DEVICE_STATE_ROTATION_KEY_HALF_FOLDED"
        )
    }

    @Test
    fun loadAutoRotateDeviceStates_invalidNumberFormatInEntry_throwsException() {
        whenever(mockResources.getStringArray(R.array.config_perDeviceStateRotationLockDefaults))
            .thenReturn(
                arrayOf(
                    "$DEVICE_STATE_ROTATION_KEY_HALF_FOLDED:two",
                    "$DEVICE_STATE_ROTATION_KEY_UNFOLDED:$DEVICE_STATE_ROTATION_LOCK_LOCKED",
                    "$DEVICE_STATE_ROTATION_KEY_FOLDED:$DEVICE_STATE_ROTATION_LOCK_LOCKED"
                )
            )

        val exception = assertThrows(IllegalStateException::class.java) {
            settingManager =
                DeviceStateAutoRotateSettingManagerImpl(
                    mockContext,
                    executor,
                    fakeSecureSettings,
                    mockHandler,
                    mMockPostureDeviceStateConverter,
                )
        }
        assertThat(exception.message).contains(
            "Invalid number format in '$DEVICE_STATE_ROTATION_KEY_HALF_FOLDED:two'"
        )
    }

    private fun persistSettings(devicePosture: Int, autoRotateSetting: Int) {
        persistSettings("$devicePosture:$autoRotateSetting")
    }
@@ -343,21 +449,47 @@ class DeviceStateAutoRotateSettingManagerImplTest {
            .thenReturn(DEVICE_STATE_ROTATION_KEY_UNFOLDED)
        whenever(mMockPostureDeviceStateConverter.deviceStateToPosture(eq(DEVICE_STATE_FOLDED)))
            .thenReturn(DEVICE_STATE_ROTATION_KEY_FOLDED)
        whenever(mMockPostureDeviceStateConverter.deviceStateToPosture(eq(DEVICE_STATE_HALF_FOLDED)))
            .thenReturn(DEVICE_STATE_ROTATION_KEY_HALF_FOLDED)
        whenever(mMockPostureDeviceStateConverter.deviceStateToPosture(eq(DEVICE_STATE_INVALID)))
            .thenReturn(DEVICE_STATE_ROTATION_LOCK_IGNORED)
        whenever(mMockPostureDeviceStateConverter.deviceStateToPosture(eq(DEVICE_STATE_REAR_DISPLAY)))
            .thenReturn(DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY)

        whenever(mMockPostureDeviceStateConverter.postureToDeviceState(eq(DEVICE_STATE_ROTATION_KEY_UNFOLDED)))
            .thenReturn(DEVICE_STATE_UNFOLDED)
        whenever(mMockPostureDeviceStateConverter.postureToDeviceState(eq(DEVICE_STATE_ROTATION_KEY_FOLDED)))
            .thenReturn(DEVICE_STATE_FOLDED)
        whenever(mMockPostureDeviceStateConverter.postureToDeviceState(eq(DEVICE_STATE_ROTATION_KEY_HALF_FOLDED)))
            .thenReturn(DEVICE_STATE_HALF_FOLDED)
        whenever(mMockPostureDeviceStateConverter.postureToDeviceState(eq(DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY)))
            .thenReturn(DEVICE_STATE_REAR_DISPLAY)
        whenever(
            mMockPostureDeviceStateConverter
                .deviceStateToPosture(eq(DEVICE_STATE_HALF_FOLDED))
        ).thenReturn(DEVICE_STATE_ROTATION_KEY_HALF_FOLDED)
        whenever(
            mMockPostureDeviceStateConverter
                .deviceStateToPosture(eq(DEVICE_STATE_INVALID))
        ).thenReturn(DEVICE_STATE_ROTATION_LOCK_IGNORED)
        whenever(
            mMockPostureDeviceStateConverter
                .deviceStateToPosture(eq(DEVICE_STATE_REAR_DISPLAY))
        ).thenReturn(DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY)

        whenever(
            mMockPostureDeviceStateConverter.postureToDeviceState(
                eq(
                    DEVICE_STATE_ROTATION_KEY_UNFOLDED
                )
            )
        ).thenReturn(DEVICE_STATE_UNFOLDED)
        whenever(
            mMockPostureDeviceStateConverter.postureToDeviceState(
                eq(
                    DEVICE_STATE_ROTATION_KEY_FOLDED
                )
            )
        ).thenReturn(DEVICE_STATE_FOLDED)
        whenever(
            mMockPostureDeviceStateConverter.postureToDeviceState(
                eq(
                    DEVICE_STATE_ROTATION_KEY_HALF_FOLDED
                )
            )
        ).thenReturn(DEVICE_STATE_HALF_FOLDED)
        whenever(
            mMockPostureDeviceStateConverter.postureToDeviceState(
                eq(
                    DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY
                )
            )
        ).thenReturn(DEVICE_STATE_REAR_DISPLAY)
    }

    private companion object {