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

Commit 90846a0c authored by dshivangi's avatar dshivangi
Browse files

Prevent stale in-memory state for device auto-rotate settings

Updates in-memory device auto-rotate settings map to always reflect the fully resolved value written to persisted setting, preventing stale partial states.

Fixes: 421165907
Test: atest DeviceStateAutoRotateSettingControllerTests DeviceStateAutoRotateSettingManagerImplTest
Flag: com.android.window.flags.enable_device_state_auto_rotate_setting_refactor
Change-Id: I86c0a33087a8cebe60c7e947274e2c96f2898e69
parent 9b6e831d
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -68,11 +68,14 @@ public interface DeviceStateAutoRotateSettingManager extends Dumpable {
     *
     * @param proposedSetting Settings maps desired to be written into persisted setting.
     * @param currentSetting  Current settings map
     * @return Resolved proposedSetting map
     */
    @Discouraged(message = "This method is exclusively for internal use. The designated API for "
            + "updating settings is #updateSetting(int, boolean) in com.android.settingslib"
            + ".devicestate.DeviceStateAutoRotateSettingManager. Please use that method.")
    void updateSetting(SparseIntArray proposedSetting, SparseIntArray currentSetting);
    @NonNull
    SparseIntArray updateSetting(@NonNull SparseIntArray proposedSetting,
            @NonNull SparseIntArray currentSetting);

    /**
     * Get {@link DEVICE_STATE_ROTATION_LOCK} setting value for {@code deviceState}.
+6 −1
Original line number Diff line number Diff line
@@ -162,8 +162,10 @@ public class DeviceStateAutoRotateSettingManagerImpl implements
        RotationPolicy.requestDeviceStateAutoRotateSettingChange(deviceState, !rotationLock);
    }

    @NonNull
    @Override
    public void updateSetting(SparseIntArray proposedSetting, SparseIntArray currentSetting) {
    public SparseIntArray updateSetting(@NonNull SparseIntArray proposedSetting,
            @NonNull SparseIntArray currentSetting) {
        if (!areAllDefaultsPresent(proposedSetting) || !areAllDefaultsPresent(currentSetting)) {
            // Either the postures in proposed setting or current setting map do not match with
            // device postures defined in the default in configuration. We should still go ahead
@@ -221,6 +223,9 @@ public class DeviceStateAutoRotateSettingManagerImpl implements
                convertIntArrayToSerializedSetting(proposedSetting);
        mSecureSettings.putStringForUser(DEVICE_STATE_ROTATION_LOCK,
                serializedDeviceStateAutoRotateSetting, UserHandle.USER_CURRENT);

        resolveIgnoredAutoRotateStates(proposedSetting);
        return proposedSetting;
    }

    @Override
+3 −2
Original line number Diff line number Diff line
@@ -140,8 +140,9 @@ public final class DeviceStateRotationLockSettingsManager implements
    }

    @Override
    public void updateSetting(SparseIntArray proposedSetting,
            SparseIntArray currentSetting) {
    @NonNull
    public SparseIntArray updateSetting(@NonNull SparseIntArray proposedSetting,
            @NonNull SparseIntArray currentSetting) {
        // This method is not supported in this implementation. Use updateSetting(int, boolean)
        // instead. This overload is intended for a refactored settings manager.
        throw new UnsupportedOperationException(
+83 −4
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ class DeviceStateAutoRotateSettingManagerImplTest {
                "$DEVICE_STATE_ROTATION_KEY_UNFOLDED",
        "$DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY:" +
                "$DEVICE_STATE_ROTATION_LOCK_IGNORED:" +
                "$DEVICE_STATE_ROTATION_KEY_UNFOLDED",
                "$DEVICE_STATE_ROTATION_KEY_FOLDED",
        "$DEVICE_STATE_ROTATION_KEY_UNFOLDED:$DEVICE_STATE_ROTATION_LOCK_UNLOCKED",
        "$DEVICE_STATE_ROTATION_KEY_FOLDED:$DEVICE_STATE_ROTATION_LOCK_LOCKED",
    )
@@ -299,7 +299,7 @@ class DeviceStateAutoRotateSettingManagerImplTest {
        expectedPairs[DEVICE_STATE_ROTATION_KEY_HALF_FOLDED] =
            DEVICE_STATE_ROTATION_LOCK_LOCKED
        expectedPairs[DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY] =
            DEVICE_STATE_ROTATION_LOCK_LOCKED
            DEVICE_STATE_ROTATION_LOCK_UNLOCKED

        val deviceStateAutoRotateSetting = settingManager.getRotationLockSetting()

@@ -435,6 +435,72 @@ class DeviceStateAutoRotateSettingManagerImplTest {
        assertThat(persistedSetting).isEqualTo(expectedPairs)
    }

    @Test
    fun updateSettingMap_sendsMapsWithOneUpdatedSetting_returnsResolvedMap() {
        val currentSettingMap = getDefaultResolvedMap()
        val proposedSettingMap = getDefaultResolvedMap()
        proposedSettingMap[DEVICE_STATE_ROTATION_KEY_FOLDED] =
            DEVICE_STATE_ROTATION_LOCK_UNLOCKED
        val expectedSettingIntArray = convertMapToSparseIntArray(proposedSettingMap)
        expectedSettingIntArray.put(
            DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY,
            DEVICE_STATE_ROTATION_LOCK_UNLOCKED
        )

        val actualProposedSettingIntArray = settingManager.updateSetting(
            convertMapToSparseIntArray(proposedSettingMap),
            convertMapToSparseIntArray(currentSettingMap)
        )

        assertIntArrayEqual(expectedSettingIntArray, actualProposedSettingIntArray)
    }

    @Test
    fun updateSettingMap_sendsMapsWithTwoUpdatedSettings_returnsResolvedMap() {
        val currentSettingMap = getDefaultResolvedMap()
        val proposedSettingMap = getDefaultResolvedMap()
        proposedSettingMap[DEVICE_STATE_ROTATION_KEY_FOLDED] =
            DEVICE_STATE_ROTATION_LOCK_UNLOCKED
        proposedSettingMap[DEVICE_STATE_ROTATION_KEY_UNFOLDED] =
            DEVICE_STATE_ROTATION_LOCK_LOCKED
        val expectedSettingIntArray = convertMapToSparseIntArray(proposedSettingMap)
        expectedSettingIntArray.put(
            DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY,
            DEVICE_STATE_ROTATION_LOCK_UNLOCKED
        )
        expectedSettingIntArray.put(
            DEVICE_STATE_ROTATION_KEY_HALF_FOLDED,
            DEVICE_STATE_ROTATION_LOCK_LOCKED
        )

        val actualProposedSettingIntArray = settingManager.updateSetting(
            convertMapToSparseIntArray(proposedSettingMap),
            convertMapToSparseIntArray(currentSettingMap)
        )

        assertIntArrayEqual(expectedSettingIntArray, actualProposedSettingIntArray)
    }

    @Test
    fun updateSettingMap_settingValueForIgnoredChanged_returnsResolvedMap() {
        val currentSettingMap = getDefaultResolvedMap()
        val proposedSettingMap = getDefaultResolvedMap()
        proposedSettingMap[DEVICE_STATE_ROTATION_KEY_HALF_FOLDED] =
            DEVICE_STATE_ROTATION_LOCK_LOCKED
        val expectedSettingIntArray = convertMapToSparseIntArray(proposedSettingMap)
        expectedSettingIntArray.put(
            DEVICE_STATE_ROTATION_KEY_UNFOLDED,
            DEVICE_STATE_ROTATION_LOCK_LOCKED
        )

        val actualProposedSettingIntArray = settingManager.updateSetting(
            convertMapToSparseIntArray(proposedSettingMap),
            convertMapToSparseIntArray(currentSettingMap)
        )

        assertIntArrayEqual(expectedSettingIntArray, actualProposedSettingIntArray)
    }

    @Test
    fun updateSettingMap_currentSettingMissingValueForFallbackPosture_throwsIllegalStateException() {
        val currentSettingMap = getDefaultResolvedMap()
@@ -577,7 +643,7 @@ class DeviceStateAutoRotateSettingManagerImplTest {
        defaultSettingMap[DEVICE_STATE_ROTATION_KEY_HALF_FOLDED] =
            DEVICE_STATE_ROTATION_LOCK_UNLOCKED
        defaultSettingMap[DEVICE_STATE_ROTATION_KEY_REAR_DISPLAY] =
            DEVICE_STATE_ROTATION_LOCK_UNLOCKED
            DEVICE_STATE_ROTATION_LOCK_LOCKED

        return defaultSettingMap
    }
@@ -643,6 +709,19 @@ class DeviceStateAutoRotateSettingManagerImplTest {
        ).thenReturn(DEVICE_STATE_REAR_DISPLAY)
    }

    private fun assertIntArrayEqual(
        expectedIntArray: SparseIntArray,
        actualIntArray: SparseIntArray
    ) {
        assertThat(expectedIntArray.size()).isEqualTo(actualIntArray.size())
        for (i in 0 until expectedIntArray.size()) {
            val expectedKey = expectedIntArray.keyAt(i)
            val expectedValue = expectedIntArray.valueAt(i)
            assertThat(actualIntArray.indexOfKey(expectedKey)).isGreaterThan(-1)
            assertThat(actualIntArray[expectedKey]).isEqualTo(expectedValue)
        }
    }

    private companion object {
        const val DEVICE_STATE_FOLDED = 0
        const val DEVICE_STATE_HALF_FOLDED = 1
@@ -654,7 +733,7 @@ class DeviceStateAutoRotateSettingManagerImplTest {
        const val FOLDED_LOCKED_OPEN_UNLOCKED_SETTING_UNRESOLVED: String = "0:1:1:0:2:2:3:0"
        const val FOLDED_LOCKED_OPEN_LOCKED_SETTING_UNRESOLVED: String = "0:1:1:0:2:1:3:0"
        const val FOLDED_UNLOCKED_OPEN_LOCKED_SETTING_UNRESOLVED: String = "0:2:1:0:2:1:3:0"
        const val FOLDED_UNLOCKED_OPEN_UNLOCKED_SETTING_RESOLVED: String = "0:1:1:2:2:2:3:0"
        const val FOLDED_UNLOCKED_OPEN_UNLOCKED_SETTING_RESOLVED: String = "0:2:1:2:2:2:3:2"
        const val INVALID_AUTO_ROTATE_VALUE_FOR_FOLDED_SETTING: String = "0:4:1:0:2:2:3:0"
        const val MISSING_FOLDED_KEY_AND_VALUE_SETTING: String = "1:0:2:2:3:0"
        const val MISSING_VALUE_FOR_FOLDED_SETTING: String = "0:1:0:2:2:3:0"
+5 −3
Original line number Diff line number Diff line
@@ -331,7 +331,7 @@ public class DeviceStateAutoRotateSettingController {
        }

        if (!equals(mDeviceStateAutoRotateSetting, persistedDeviceStateAutoRotateSetting)) {
            mDeviceStateAutoRotateSettingManager.updateSetting(
            mDeviceStateAutoRotateSetting = mDeviceStateAutoRotateSettingManager.updateSetting(
                    mDeviceStateAutoRotateSetting.clone(),
                    persistedDeviceStateAutoRotateSetting == null
                            ? getDefaultDeviceStateAutoRotateSetting()
@@ -437,7 +437,8 @@ public class DeviceStateAutoRotateSettingController {
         * setting(DEVICE_STATE_ROTATION_LOCK) for a specific device posture.
         */
        static final class UpdateDeviceStateAutoRotateSetting extends Event {
            @DeviceStateRotationLockKey final int mDevicePosture;
            @DeviceStateRotationLockKey
            final int mDevicePosture;
            final boolean mAutoRotate;

            /**
@@ -455,7 +456,8 @@ public class DeviceStateAutoRotateSettingController {
         * Event sent when the device posture changes.
         */
        static final class UpdateDevicePosture extends Event {
            @DeviceStateRotationLockKey final int mDevicePosture;
            @DeviceStateRotationLockKey
            final int mDevicePosture;

            /**
             * @param devicePosture New device posture.
Loading