Foldable rotation settings: persist postures instead of device states
Device state ids can change, and therefore shouldn't be persisted. We
define our custom ids for device postures, which will not change.
1. We define our custom posture ids in Settings.Secure. These match
the current device state ids. This way we don't have to wipe users'
settings.
2. We convert device ids to postures, based on the config arrays
"config_foldedDeviceStates", "config_halfFoldedDeviceStates", and
"config_openDeviceStates".
Test: unit tests
Test: manually by trying the different auto-rotation scenarios in
different postures.
Fixes: 261968395
Change-Id: I396e9c92230e75e186b6a70198ee16b41e508a1e
diff --git a/packages/SettingsLib/DeviceStateRotationLock/Android.bp b/packages/SettingsLib/DeviceStateRotationLock/Android.bp
index c642bd1..103309a 100644
--- a/packages/SettingsLib/DeviceStateRotationLock/Android.bp
+++ b/packages/SettingsLib/DeviceStateRotationLock/Android.bp
@@ -10,7 +10,10 @@
android_library {
name: "SettingsLibDeviceStateRotationLock",
- srcs: ["src/**/*.java"],
+ srcs: [
+ "src/**/*.java",
+ "src/**/*.kt",
+ ],
min_sdk_version: "21",
}
diff --git a/packages/SettingsLib/DeviceStateRotationLock/src/com.android.settingslib.devicestate/DeviceStateRotationLockSettingsManager.java b/packages/SettingsLib/DeviceStateRotationLock/src/com.android.settingslib.devicestate/DeviceStateRotationLockSettingsManager.java
index 10b004e..76e1df1 100644
--- a/packages/SettingsLib/DeviceStateRotationLock/src/com.android.settingslib.devicestate/DeviceStateRotationLockSettingsManager.java
+++ b/packages/SettingsLib/DeviceStateRotationLock/src/com.android.settingslib.devicestate/DeviceStateRotationLockSettingsManager.java
@@ -57,17 +57,19 @@
private final Handler mMainHandler = new Handler(Looper.getMainLooper());
private final Set<DeviceStateRotationLockSettingsListener> mListeners = new HashSet<>();
private final SecureSettings mSecureSettings;
- private String[] mDeviceStateRotationLockDefaults;
- private SparseIntArray mDeviceStateRotationLockSettings;
- private SparseIntArray mDeviceStateDefaultRotationLockSettings;
- private SparseIntArray mDeviceStateRotationLockFallbackSettings;
+ private final PosturesHelper mPosturesHelper;
+ private String[] mPostureRotationLockDefaults;
+ private SparseIntArray mPostureRotationLockSettings;
+ private SparseIntArray mPostureDefaultRotationLockSettings;
+ private SparseIntArray mPostureRotationLockFallbackSettings;
private String mLastSettingValue;
private List<SettableDeviceState> mSettableDeviceStates;
@VisibleForTesting
DeviceStateRotationLockSettingsManager(Context context, SecureSettings secureSettings) {
- this.mSecureSettings = secureSettings;
- mDeviceStateRotationLockDefaults =
+ mSecureSettings = secureSettings;
+ mPosturesHelper = new PosturesHelper(context);
+ mPostureRotationLockDefaults =
context.getResources()
.getStringArray(R.array.config_perDeviceStateRotationLockDefaults);
loadDefaults();
@@ -134,13 +136,14 @@
/** Updates the rotation lock setting for a specified device state. */
public void updateSetting(int deviceState, boolean rotationLocked) {
- if (mDeviceStateRotationLockFallbackSettings.indexOfKey(deviceState) >= 0) {
- // The setting for this device state is IGNORED, and has a fallback device state.
- // The setting for that fallback device state should be the changed in this case.
- deviceState = mDeviceStateRotationLockFallbackSettings.get(deviceState);
+ int posture = mPosturesHelper.deviceStateToPosture(deviceState);
+ if (mPostureRotationLockFallbackSettings.indexOfKey(posture) >= 0) {
+ // The setting for this device posture is IGNORED, and has a fallback posture.
+ // The setting for that fallback posture should be the changed in this case.
+ posture = mPostureRotationLockFallbackSettings.get(posture);
}
- mDeviceStateRotationLockSettings.put(
- deviceState,
+ mPostureRotationLockSettings.put(
+ posture,
rotationLocked
? DEVICE_STATE_ROTATION_LOCK_LOCKED
: DEVICE_STATE_ROTATION_LOCK_UNLOCKED);
@@ -159,22 +162,23 @@
*/
@Settings.Secure.DeviceStateRotationLockSetting
public int getRotationLockSetting(int deviceState) {
- int rotationLockSetting = mDeviceStateRotationLockSettings.get(
- deviceState, /* valueIfKeyNotFound= */ DEVICE_STATE_ROTATION_LOCK_IGNORED);
+ int devicePosture = mPosturesHelper.deviceStateToPosture(deviceState);
+ int rotationLockSetting = mPostureRotationLockSettings.get(
+ devicePosture, /* valueIfKeyNotFound= */ DEVICE_STATE_ROTATION_LOCK_IGNORED);
if (rotationLockSetting == DEVICE_STATE_ROTATION_LOCK_IGNORED) {
- rotationLockSetting = getFallbackRotationLockSetting(deviceState);
+ rotationLockSetting = getFallbackRotationLockSetting(devicePosture);
}
return rotationLockSetting;
}
- private int getFallbackRotationLockSetting(int deviceState) {
- int indexOfFallbackState = mDeviceStateRotationLockFallbackSettings.indexOfKey(deviceState);
- if (indexOfFallbackState < 0) {
+ private int getFallbackRotationLockSetting(int devicePosture) {
+ int indexOfFallback = mPostureRotationLockFallbackSettings.indexOfKey(devicePosture);
+ if (indexOfFallback < 0) {
Log.w(TAG, "Setting is ignored, but no fallback was specified.");
return DEVICE_STATE_ROTATION_LOCK_IGNORED;
}
- int fallbackState = mDeviceStateRotationLockFallbackSettings.valueAt(indexOfFallbackState);
- return mDeviceStateRotationLockSettings.get(fallbackState,
+ int fallbackPosture = mPostureRotationLockFallbackSettings.valueAt(indexOfFallback);
+ return mPostureRotationLockSettings.get(fallbackPosture,
/* valueIfKeyNotFound= */ DEVICE_STATE_ROTATION_LOCK_IGNORED);
}
@@ -189,8 +193,8 @@
* DEVICE_STATE_ROTATION_LOCK_UNLOCKED}.
*/
public boolean isRotationLockedForAllStates() {
- for (int i = 0; i < mDeviceStateRotationLockSettings.size(); i++) {
- if (mDeviceStateRotationLockSettings.valueAt(i)
+ for (int i = 0; i < mPostureRotationLockSettings.size(); i++) {
+ if (mPostureRotationLockSettings.valueAt(i)
== DEVICE_STATE_ROTATION_LOCK_UNLOCKED) {
return false;
}
@@ -221,7 +225,7 @@
fallbackOnDefaults();
return;
}
- mDeviceStateRotationLockSettings = new SparseIntArray(values.length / 2);
+ mPostureRotationLockSettings = new SparseIntArray(values.length / 2);
int key;
int value;
@@ -230,7 +234,7 @@
key = Integer.parseInt(values[i++]);
value = Integer.parseInt(values[i++]);
boolean isPersistedValueIgnored = value == DEVICE_STATE_ROTATION_LOCK_IGNORED;
- boolean isDefaultValueIgnored = mDeviceStateDefaultRotationLockSettings.get(key)
+ boolean isDefaultValueIgnored = mPostureDefaultRotationLockSettings.get(key)
== DEVICE_STATE_ROTATION_LOCK_IGNORED;
if (isPersistedValueIgnored != isDefaultValueIgnored) {
Log.w(TAG, "Conflict for ignored device state " + key
@@ -238,7 +242,7 @@
fallbackOnDefaults();
return;
}
- mDeviceStateRotationLockSettings.put(key, value);
+ mPostureRotationLockSettings.put(key, value);
} catch (NumberFormatException e) {
Log.wtf(TAG, "Error deserializing one of the saved settings", e);
fallbackOnDefaults();
@@ -253,7 +257,7 @@
*/
@VisibleForTesting
public void resetStateForTesting(Resources resources) {
- mDeviceStateRotationLockDefaults =
+ mPostureRotationLockDefaults =
resources.getStringArray(R.array.config_perDeviceStateRotationLockDefaults);
fallbackOnDefaults();
}
@@ -264,23 +268,23 @@
}
private void persistSettings() {
- if (mDeviceStateRotationLockSettings.size() == 0) {
+ if (mPostureRotationLockSettings.size() == 0) {
persistSettingIfChanged(/* newSettingValue= */ "");
return;
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder
- .append(mDeviceStateRotationLockSettings.keyAt(0))
+ .append(mPostureRotationLockSettings.keyAt(0))
.append(SEPARATOR_REGEX)
- .append(mDeviceStateRotationLockSettings.valueAt(0));
+ .append(mPostureRotationLockSettings.valueAt(0));
- for (int i = 1; i < mDeviceStateRotationLockSettings.size(); i++) {
+ for (int i = 1; i < mPostureRotationLockSettings.size(); i++) {
stringBuilder
.append(SEPARATOR_REGEX)
- .append(mDeviceStateRotationLockSettings.keyAt(i))
+ .append(mPostureRotationLockSettings.keyAt(i))
.append(SEPARATOR_REGEX)
- .append(mDeviceStateRotationLockSettings.valueAt(i));
+ .append(mPostureRotationLockSettings.valueAt(i));
}
persistSettingIfChanged(stringBuilder.toString());
}
@@ -300,22 +304,20 @@
}
private void loadDefaults() {
- mSettableDeviceStates = new ArrayList<>(mDeviceStateRotationLockDefaults.length);
- mDeviceStateDefaultRotationLockSettings = new SparseIntArray(
- mDeviceStateRotationLockDefaults.length);
- mDeviceStateRotationLockSettings = new SparseIntArray(
- mDeviceStateRotationLockDefaults.length);
- mDeviceStateRotationLockFallbackSettings = new SparseIntArray(1);
- for (String entry : mDeviceStateRotationLockDefaults) {
+ mSettableDeviceStates = new ArrayList<>(mPostureRotationLockDefaults.length);
+ mPostureDefaultRotationLockSettings = new SparseIntArray(
+ mPostureRotationLockDefaults.length);
+ mPostureRotationLockSettings = new SparseIntArray(mPostureRotationLockDefaults.length);
+ mPostureRotationLockFallbackSettings = new SparseIntArray(1);
+ for (String entry : mPostureRotationLockDefaults) {
String[] values = entry.split(SEPARATOR_REGEX);
try {
- int deviceState = Integer.parseInt(values[0]);
+ int posture = Integer.parseInt(values[0]);
int rotationLockSetting = Integer.parseInt(values[1]);
if (rotationLockSetting == DEVICE_STATE_ROTATION_LOCK_IGNORED) {
if (values.length == 3) {
- int fallbackDeviceState = Integer.parseInt(values[2]);
- mDeviceStateRotationLockFallbackSettings.put(deviceState,
- fallbackDeviceState);
+ int fallbackPosture = Integer.parseInt(values[2]);
+ mPostureRotationLockFallbackSettings.put(posture, fallbackPosture);
} else {
Log.w(TAG,
"Rotation lock setting is IGNORED, but values have unexpected "
@@ -324,9 +326,14 @@
}
}
boolean isSettable = rotationLockSetting != DEVICE_STATE_ROTATION_LOCK_IGNORED;
- mSettableDeviceStates.add(new SettableDeviceState(deviceState, isSettable));
- mDeviceStateRotationLockSettings.put(deviceState, rotationLockSetting);
- mDeviceStateDefaultRotationLockSettings.put(deviceState, rotationLockSetting);
+ Integer deviceState = mPosturesHelper.postureToDeviceState(posture);
+ if (deviceState != null) {
+ mSettableDeviceStates.add(new SettableDeviceState(deviceState, isSettable));
+ } else {
+ Log.wtf(TAG, "No matching device state for posture: " + posture);
+ }
+ mPostureRotationLockSettings.put(posture, rotationLockSetting);
+ mPostureDefaultRotationLockSettings.put(posture, rotationLockSetting);
} catch (NumberFormatException e) {
Log.wtf(TAG, "Error parsing settings entry. Entry was: " + entry, e);
return;
@@ -338,13 +345,11 @@
public void dump(IndentingPrintWriter pw) {
pw.println("DeviceStateRotationLockSettingsManager");
pw.increaseIndent();
- pw.println("mDeviceStateRotationLockDefaults: " + Arrays.toString(
- mDeviceStateRotationLockDefaults));
- pw.println("mDeviceStateDefaultRotationLockSettings: "
- + mDeviceStateDefaultRotationLockSettings);
- pw.println("mDeviceStateRotationLockSettings: " + mDeviceStateRotationLockSettings);
- pw.println("mDeviceStateRotationLockFallbackSettings: "
- + mDeviceStateRotationLockFallbackSettings);
+ pw.println("mPostureRotationLockDefaults: "
+ + Arrays.toString(mPostureRotationLockDefaults));
+ pw.println("mPostureDefaultRotationLockSettings: " + mPostureDefaultRotationLockSettings);
+ pw.println("mDeviceStateRotationLockSettings: " + mPostureRotationLockSettings);
+ pw.println("mPostureRotationLockFallbackSettings: " + mPostureRotationLockFallbackSettings);
pw.println("mSettableDeviceStates: " + mSettableDeviceStates);
pw.println("mLastSettingValue: " + mLastSettingValue);
pw.decreaseIndent();
diff --git a/packages/SettingsLib/DeviceStateRotationLock/src/com.android.settingslib.devicestate/PosturesHelper.kt b/packages/SettingsLib/DeviceStateRotationLock/src/com.android.settingslib.devicestate/PosturesHelper.kt
new file mode 100644
index 0000000..9c70be9
--- /dev/null
+++ b/packages/SettingsLib/DeviceStateRotationLock/src/com.android.settingslib.devicestate/PosturesHelper.kt
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settingslib.devicestate
+
+import android.content.Context
+import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_FOLDED
+import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_HALF_FOLDED
+import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_UNFOLDED
+import android.provider.Settings.Secure.DEVICE_STATE_ROTATION_KEY_UNKNOWN
+import android.provider.Settings.Secure.DeviceStateRotationLockKey
+import com.android.internal.R
+
+/** Helps to convert between device state and posture. */
+class PosturesHelper(context: Context) {
+
+ private val foldedDeviceStates =
+ context.resources.getIntArray(R.array.config_foldedDeviceStates)
+ private val halfFoldedDeviceStates =
+ context.resources.getIntArray(R.array.config_halfFoldedDeviceStates)
+ private val unfoldedDeviceStates =
+ context.resources.getIntArray(R.array.config_openDeviceStates)
+
+ @DeviceStateRotationLockKey
+ fun deviceStateToPosture(deviceState: Int): Int {
+ return when (deviceState) {
+ in foldedDeviceStates -> DEVICE_STATE_ROTATION_KEY_FOLDED
+ in halfFoldedDeviceStates -> DEVICE_STATE_ROTATION_KEY_HALF_FOLDED
+ in unfoldedDeviceStates -> DEVICE_STATE_ROTATION_KEY_UNFOLDED
+ else -> DEVICE_STATE_ROTATION_KEY_UNKNOWN
+ }
+ }
+
+ fun postureToDeviceState(@DeviceStateRotationLockKey posture: Int): Int? {
+ return when (posture) {
+ DEVICE_STATE_ROTATION_KEY_FOLDED -> foldedDeviceStates.firstOrNull()
+ DEVICE_STATE_ROTATION_KEY_HALF_FOLDED -> halfFoldedDeviceStates.firstOrNull()
+ DEVICE_STATE_ROTATION_KEY_UNFOLDED -> unfoldedDeviceStates.firstOrNull()
+ else -> null
+ }
+ }
+}