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
diff --git a/packages/SettingsProvider/Android.bp b/packages/SettingsProvider/Android.bp
index f490c87..4a10427 100644
--- a/packages/SettingsProvider/Android.bp
+++ b/packages/SettingsProvider/Android.bp
@@ -32,6 +32,7 @@
     ],
     static_libs: [
         "junit",
+        "SettingsLibDeviceStateRotationLock",
         "SettingsLibDisplayDensityUtils",
     ],
     platform_apis: true,
@@ -55,6 +56,7 @@
     static_libs: [
         "androidx.test.rules",
         "mockito-target-minus-junit4",
+        "SettingsLibDeviceStateRotationLock",
         "SettingsLibDisplayDensityUtils",
         "platform-test-annotations",
         "truth-prebuilt",
diff --git a/packages/SettingsProvider/src/android/provider/settings/backup/DeviceSpecificSettings.java b/packages/SettingsProvider/src/android/provider/settings/backup/DeviceSpecificSettings.java
index e425790..3cb1439 100644
--- a/packages/SettingsProvider/src/android/provider/settings/backup/DeviceSpecificSettings.java
+++ b/packages/SettingsProvider/src/android/provider/settings/backup/DeviceSpecificSettings.java
@@ -32,5 +32,6 @@
      */
     public static final String[] DEVICE_SPECIFIC_SETTINGS_TO_BACKUP = {
             Settings.Secure.DISPLAY_DENSITY_FORCED,
+            Settings.Secure.DEVICE_STATE_ROTATION_LOCK
     };
 }
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
index 440bb67..808ea9e 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
@@ -41,6 +41,7 @@
 
 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 @@
             } 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 @@
         }
     }
 
+    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)
diff --git a/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsHelperTest.java b/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsHelperTest.java
index 4f7b494..ee76dbf 100644
--- a/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsHelperTest.java
+++ b/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsHelperTest.java
@@ -73,6 +73,8 @@
         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 @@
                         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();