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

Commit 3fa616a7 authored by Automerger Merge Worker's avatar Automerger Merge Worker Committed by Android (Google) Code Review
Browse files

Merge "Merge "Implement backup & restore for the device state based auto...

Merge "Merge "Implement backup & restore for the device state based auto rotation setting" into tm-qpr-dev am: 5b3c3301 am: 73217c16"
parents 74a45d0d f4b49db5
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -32,6 +32,7 @@ android_app {
    ],
    ],
    static_libs: [
    static_libs: [
        "junit",
        "junit",
        "SettingsLibDeviceStateRotationLock",
        "SettingsLibDisplayDensityUtils",
        "SettingsLibDisplayDensityUtils",
    ],
    ],
    platform_apis: true,
    platform_apis: true,
@@ -55,6 +56,7 @@ android_test {
    static_libs: [
    static_libs: [
        "androidx.test.rules",
        "androidx.test.rules",
        "mockito-target-minus-junit4",
        "mockito-target-minus-junit4",
        "SettingsLibDeviceStateRotationLock",
        "SettingsLibDisplayDensityUtils",
        "SettingsLibDisplayDensityUtils",
        "platform-test-annotations",
        "platform-test-annotations",
        "truth-prebuilt",
        "truth-prebuilt",
+1 −0
Original line number Original line Diff line number Diff line
@@ -32,5 +32,6 @@ public class DeviceSpecificSettings {
     */
     */
    public static final String[] DEVICE_SPECIFIC_SETTINGS_TO_BACKUP = {
    public static final String[] DEVICE_SPECIFIC_SETTINGS_TO_BACKUP = {
            Settings.Secure.DISPLAY_DENSITY_FORCED,
            Settings.Secure.DISPLAY_DENSITY_FORCED,
            Settings.Secure.DEVICE_STATE_ROTATION_LOCK
    };
    };
}
}
+10 −0
Original line number Original line Diff line number Diff line
@@ -41,6 +41,7 @@ import android.util.ArraySet;


import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.LocalePicker;
import com.android.internal.app.LocalePicker;
import com.android.settingslib.devicestate.DeviceStateRotationLockSettingsManager;


import java.util.ArrayList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashMap;
@@ -200,6 +201,9 @@ public class SettingsHelper {
            } else if (Settings.Global.POWER_BUTTON_LONG_PRESS.equals(name)) {
            } else if (Settings.Global.POWER_BUTTON_LONG_PRESS.equals(name)) {
                setLongPressPowerBehavior(cr, value);
                setLongPressPowerBehavior(cr, value);
                return;
                return;
            } else if (Settings.System.ACCELEROMETER_ROTATION.equals(name)
                    && shouldSkipAutoRotateRestore()) {
                return;
            }
            }


            // Default case: write the restored value to settings
            // Default case: write the restored value to settings
@@ -236,6 +240,12 @@ public class SettingsHelper {
        }
        }
    }
    }


    private boolean shouldSkipAutoRotateRestore() {
        // When device state based auto rotation settings are available, let's skip the restoring
        // of the standard auto rotation settings to avoid conflicting setting values.
        return DeviceStateRotationLockSettingsManager.isDeviceStateRotationLockEnabled(mContext);
    }

    public String onBackupValue(String name, String value) {
    public String onBackupValue(String name, String value) {
        // Special processing for backing up ringtones & notification sounds
        // Special processing for backing up ringtones & notification sounds
        if (Settings.System.RINGTONE.equals(name)
        if (Settings.System.RINGTONE.equals(name)
+59 −0
Original line number Original line Diff line number Diff line
@@ -73,6 +73,8 @@ public class SettingsHelperTest {
        when(mContext.getSystemService(eq(Context.TELEPHONY_SERVICE))).thenReturn(
        when(mContext.getSystemService(eq(Context.TELEPHONY_SERVICE))).thenReturn(
                mTelephonyManager);
                mTelephonyManager);
        when(mContext.getResources()).thenReturn(mResources);
        when(mContext.getResources()).thenReturn(mResources);
        when(mContext.getApplicationContext()).thenReturn(mContext);
        when(mContext.getContentResolver()).thenReturn(getContentResolver());


        mSettingsHelper = spy(new SettingsHelper(mContext));
        mSettingsHelper = spy(new SettingsHelper(mContext));
    }
    }
@@ -305,6 +307,63 @@ public class SettingsHelperTest {
                        new String[] { "he-IL", "id-ID", "yi" }));  // supported
                        new String[] { "he-IL", "id-ID", "yi" }));  // supported
    }
    }


    @Test
    public void restoreValue_autoRotation_deviceStateAutoRotationDisabled_restoresValue() {
        when(mResources.getStringArray(R.array.config_perDeviceStateRotationLockDefaults))
                .thenReturn(new String[]{});
        int previousValue = 0;
        int newValue = 1;
        setAutoRotationSettingValue(previousValue);

        restoreAutoRotationSetting(newValue);

        assertThat(getAutoRotationSettingValue()).isEqualTo(newValue);
    }

    @Test
    public void restoreValue_autoRotation_deviceStateAutoRotationEnabled_doesNotRestoreValue() {
        when(mResources.getStringArray(R.array.config_perDeviceStateRotationLockDefaults))
                .thenReturn(new String[]{"0:1", "1:1"});
        int previousValue = 0;
        int newValue = 1;
        setAutoRotationSettingValue(previousValue);

        restoreAutoRotationSetting(newValue);

        assertThat(getAutoRotationSettingValue()).isEqualTo(previousValue);
    }

    private int getAutoRotationSettingValue() {
        return Settings.System.getInt(
                getContentResolver(),
                Settings.System.ACCELEROMETER_ROTATION,
                /* default= */ -1);
    }

    private void setAutoRotationSettingValue(int value) {
        Settings.System.putInt(
                getContentResolver(),
                Settings.System.ACCELEROMETER_ROTATION,
                value
        );
    }

    private void restoreAutoRotationSetting(int newValue) {
        mSettingsHelper.restoreValue(
                mContext,
                getContentResolver(),
                new ContentValues(),
                /* destination= */ Settings.System.CONTENT_URI,
                /* name= */ Settings.System.ACCELEROMETER_ROTATION,
                /* value= */ String.valueOf(newValue),
                /* restoredFromSdkInt= */ 0);
    }

    private ContentResolver getContentResolver() {
        return InstrumentationRegistry.getInstrumentation().getTargetContext()
                .getContentResolver();
    }

    private void clearLongPressPowerValues() {
    private void clearLongPressPowerValues() {
        ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext()
        ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext()
                .getContentResolver();
                .getContentResolver();