Change CTS to restore fixed-to-user-rotation state

Bug: 170159753
Test: atest AppConfigurationTests#testFixedOrientationWhenRotating
passed and restored fixed-to-user-rotation when enabled
Test: atest PixelCopyTest passed and restored fixed-to-user-rotation
when enabled

Change-Id: I2abf6b84c6b140f85ec9608b9030d3377f459027
diff --git a/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityManagerTestBase.java b/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityManagerTestBase.java
index 492e611..0fd9b19 100644
--- a/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityManagerTestBase.java
+++ b/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityManagerTestBase.java
@@ -407,7 +407,7 @@
         }
 
         /**
-         * Launches an {@link Activity} synchronously on a target display. The class name needs to 
+         * Launches an {@link Activity} synchronously on a target display. The class name needs to
          * be provided either implicitly through the {@link Intent} or explicitly as a parameter
          *
          * @param className Optional class name of expected activity
@@ -1488,13 +1488,14 @@
 
     /** Helper class to save, set & wait, and restore rotation related preferences. */
     protected class RotationSession extends SettingsSession<Integer> {
-        private final String SET_FIX_TO_USER_ROTATION_COMMAND =
-                "cmd window set-fix-to-user-rotation ";
+        private final String FIXED_TO_USER_ROTATION_COMMAND =
+                "cmd window fixed-to-user-rotation ";
         private final SettingsSession<Integer> mAccelerometerRotation;
         private final HandlerThread mThread;
         private final Handler mRunnableHandler;
         private final SettingsObserver mRotationObserver;
         private int mPreviousDegree;
+        private String mPreviousFixedToUserRotationMode;
 
         public RotationSession() {
             // Save user_rotation and accelerometer_rotation preferences.
@@ -1510,7 +1511,8 @@
             mRotationObserver = new SettingsObserver(mRunnableHandler);
 
             // Disable fixed to user rotation
-            executeShellCommand(SET_FIX_TO_USER_ROTATION_COMMAND + "disabled");
+            mPreviousFixedToUserRotationMode = executeShellCommand(FIXED_TO_USER_ROTATION_COMMAND);
+            executeShellCommand(FIXED_TO_USER_ROTATION_COMMAND + "disabled");
 
             mPreviousDegree = get();
             // Disable accelerometer_rotation.
@@ -1567,8 +1569,8 @@
 
         @Override
         public void close() {
-            // Set fixed to user rotation to default
-            executeShellCommand(SET_FIX_TO_USER_ROTATION_COMMAND + "default");
+            // Restore fixed to user rotation to default
+            executeShellCommand(FIXED_TO_USER_ROTATION_COMMAND + mPreviousFixedToUserRotationMode);
             mThread.quitSafely();
             super.close();
             // Restore accelerometer_rotation preference.
diff --git a/tests/tests/view/src/android/view/cts/PixelCopyTest.java b/tests/tests/view/src/android/view/cts/PixelCopyTest.java
index 99e4da8..fd3d174 100644
--- a/tests/tests/view/src/android/view/cts/PixelCopyTest.java
+++ b/tests/tests/view/src/android/view/cts/PixelCopyTest.java
@@ -42,7 +42,7 @@
 import android.view.View;
 import android.view.Window;
 import android.view.WindowManager;
-import android.view.cts.util.DisableFixToUserRotationRule;
+import android.view.cts.util.DisableFixedToUserRotationRule;
 
 import androidx.test.InstrumentationRegistry;
 import androidx.test.filters.LargeTest;
@@ -71,8 +71,8 @@
     private static final String TAG = "PixelCopyTests";
 
     @Rule
-    public DisableFixToUserRotationRule mDisableFixToUserRotationRule =
-            new DisableFixToUserRotationRule();
+    public DisableFixedToUserRotationRule mDisableFixedToUserRotationRule =
+            new DisableFixedToUserRotationRule();
 
     @Rule
     public ActivityTestRule<PixelCopyGLProducerCtsActivity> mGLSurfaceViewActivityRule =
diff --git a/tests/tests/view/src/android/view/cts/util/DisableFixToUserRotationRule.java b/tests/tests/view/src/android/view/cts/util/DisableFixedToUserRotationRule.java
similarity index 80%
rename from tests/tests/view/src/android/view/cts/util/DisableFixToUserRotationRule.java
rename to tests/tests/view/src/android/view/cts/util/DisableFixedToUserRotationRule.java
index 43bc27c..17a50ff 100644
--- a/tests/tests/view/src/android/view/cts/util/DisableFixToUserRotationRule.java
+++ b/tests/tests/view/src/android/view/cts/util/DisableFixedToUserRotationRule.java
@@ -30,13 +30,15 @@
 import java.io.InputStreamReader;
 import java.io.Reader;
 
-public class DisableFixToUserRotationRule implements TestRule {
+public class DisableFixedToUserRotationRule implements TestRule {
     private static final String TAG = "DisableFixToUserRotationRule";
-    private static final String COMMAND = "cmd window set-fix-to-user-rotation ";
+    private static final String COMMAND = "cmd window fixed-to-user-rotation ";
 
     private final UiAutomation mUiAutomation;
 
-    public DisableFixToUserRotationRule() {
+    private String mOriginalValue;
+
+    public DisableFixedToUserRotationRule() {
         mUiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
     }
 
@@ -45,17 +47,18 @@
         return new Statement() {
             @Override
             public void evaluate() throws Throwable {
+                mOriginalValue = executeShellCommand(COMMAND);
                 executeShellCommandAndPrint(COMMAND + "disabled");
                 try {
                     base.evaluate();
                 } finally {
-                    executeShellCommandAndPrint(COMMAND + "default");
+                    executeShellCommandAndPrint(COMMAND + mOriginalValue);
                 }
             }
         };
     }
 
-    private void executeShellCommandAndPrint(String cmd) {
+    private String executeShellCommand(String cmd) {
         ParcelFileDescriptor pfd = mUiAutomation.executeShellCommand(cmd);
         StringBuilder builder = new StringBuilder();
         char[] buffer = new char[256];
@@ -68,8 +71,11 @@
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
+        return builder.toString();
+    }
 
-        Log.i(TAG, "Command: " + cmd + " Output: " + builder);
+    private void executeShellCommandAndPrint(String cmd) {
+        Log.i(TAG, "Command: " + cmd + " Output: " + executeShellCommand(cmd));
     }
 
 }