Implement backup & restore for the device state based auto rotation setting

- Adds the setting to DeviceSpecificSettings so that it is only restored on the same device
- Prevents the non-device state based setting to be restored if device stated based settings are enabled

Fixes: 196933725
Test: atest SettingsHelperTest
Change-Id: I58163cb70e8136104370ae7c1cdaaa8b7124b853
This commit is contained in:
Christian Göllner
2022-04-29 14:10:31 +00:00
parent afb021d75e
commit c562b38cdc
4 changed files with 72 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ android_app {
],
static_libs: [
"junit",
"SettingsLibDeviceStateRotationLock",
"SettingsLibDisplayDensityUtils",
],
platform_apis: true,
@@ -55,6 +56,7 @@ android_test {
static_libs: [
"androidx.test.rules",
"mockito-target-minus-junit4",
"SettingsLibDeviceStateRotationLock",
"SettingsLibDisplayDensityUtils",
"platform-test-annotations",
"truth-prebuilt",

View File

@@ -32,5 +32,6 @@ public class DeviceSpecificSettings {
*/
public static final String[] DEVICE_SPECIFIC_SETTINGS_TO_BACKUP = {
Settings.Secure.DISPLAY_DENSITY_FORCED,
Settings.Secure.DEVICE_STATE_ROTATION_LOCK
};
}

View File

@@ -41,6 +41,7 @@ import android.util.ArraySet;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.LocalePicker;
import com.android.settingslib.devicestate.DeviceStateRotationLockSettingsManager;
import java.util.ArrayList;
import java.util.HashMap;
@@ -200,6 +201,9 @@ public class SettingsHelper {
} else if (Settings.Global.POWER_BUTTON_LONG_PRESS.equals(name)) {
setLongPressPowerBehavior(cr, value);
return;
} else if (Settings.System.ACCELEROMETER_ROTATION.equals(name)
&& shouldSkipAutoRotateRestore()) {
return;
}
// 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) {
// Special processing for backing up ringtones & notification sounds
if (Settings.System.RINGTONE.equals(name)

View File

@@ -73,6 +73,8 @@ public class SettingsHelperTest {
when(mContext.getSystemService(eq(Context.TELEPHONY_SERVICE))).thenReturn(
mTelephonyManager);
when(mContext.getResources()).thenReturn(mResources);
when(mContext.getApplicationContext()).thenReturn(mContext);
when(mContext.getContentResolver()).thenReturn(getContentResolver());
mSettingsHelper = spy(new SettingsHelper(mContext));
}
@@ -305,6 +307,63 @@ public class SettingsHelperTest {
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() {
ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext()
.getContentResolver();