Introduce default value to fixed to user rotation.

The default value is calculated based on if it's PC and if it's forced
desktop mode for external displays.

Bug: 124420570
Test: WmTests pass. Manual tests.
Change-Id: I28148e0e2fb041a862eefd1b8969b3fae58b7ed4
diff --git a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
index b1b8e8c..69f7ced 100644
--- a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
@@ -621,7 +621,8 @@
     @Test
     public void testOnDescendantOrientationRequestChanged_FrozenToUserRotation() {
         final DisplayContent dc = createNewDisplay();
-        dc.getDisplayRotation().setFixedToUserRotation(true);
+        dc.getDisplayRotation().setFixedToUserRotation(
+                DisplayRotation.FIXED_TO_USER_ROTATION_ENABLED);
         mWm.mAtmService.mRootActivityContainer = mock(RootActivityContainer.class);
         final int newOrientation = dc.getLastOrientation() == SCREEN_ORIENTATION_LANDSCAPE
                 ? SCREEN_ORIENTATION_PORTRAIT
diff --git a/services/tests/wmtests/src/com/android/server/wm/DisplayRotationTests.java b/services/tests/wmtests/src/com/android/server/wm/DisplayRotationTests.java
index 8733674..1c10ffb0 100644
--- a/services/tests/wmtests/src/com/android/server/wm/DisplayRotationTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/DisplayRotationTests.java
@@ -17,6 +17,7 @@
 package com.android.server.wm;
 
 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
+import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LOCKED;
 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
 
@@ -31,6 +32,9 @@
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.times;
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
+import static com.android.server.wm.DisplayRotation.FIXED_TO_USER_ROTATION_DEFAULT;
+import static com.android.server.wm.DisplayRotation.FIXED_TO_USER_ROTATION_DISABLED;
+import static com.android.server.wm.DisplayRotation.FIXED_TO_USER_ROTATION_ENABLED;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -160,9 +164,7 @@
 
     @Test
     public void testPersistsUserRotation_LockRotation_NonDefaultDisplay() throws Exception {
-        mBuilder.mIsDefaultDisplay = false;
-
-        mBuilder.build();
+        mBuilder.setIsDefaultDisplay(false).build();
 
         freezeRotation(Surface.ROTATION_180);
 
@@ -187,9 +189,7 @@
 
     @Test
     public void testPersistsUserRotation_UnlockRotation_NonDefaultDisplay() throws Exception {
-        mBuilder.mIsDefaultDisplay = false;
-
-        mBuilder.build();
+        mBuilder.setIsDefaultDisplay(false).build();
 
         thawRotation();
 
@@ -203,14 +203,22 @@
     public void testPersistsFixedToUserRotation() throws Exception {
         mBuilder.build();
 
-        mTarget.setFixedToUserRotation(true);
+        mTarget.setFixedToUserRotation(FIXED_TO_USER_ROTATION_ENABLED);
 
-        verify(mMockDisplayWindowSettings).setFixedToUserRotation(mMockDisplayContent, true);
+        verify(mMockDisplayWindowSettings).setFixedToUserRotation(mMockDisplayContent,
+                FIXED_TO_USER_ROTATION_ENABLED);
 
         reset(mMockDisplayWindowSettings);
-        mTarget.setFixedToUserRotation(false);
+        mTarget.setFixedToUserRotation(FIXED_TO_USER_ROTATION_DISABLED);
 
-        verify(mMockDisplayWindowSettings).setFixedToUserRotation(mMockDisplayContent, false);
+        verify(mMockDisplayWindowSettings).setFixedToUserRotation(mMockDisplayContent,
+                FIXED_TO_USER_ROTATION_DISABLED);
+
+        reset(mMockDisplayWindowSettings);
+        mTarget.setFixedToUserRotation(FIXED_TO_USER_ROTATION_DEFAULT);
+
+        verify(mMockDisplayWindowSettings).setFixedToUserRotation(mMockDisplayContent,
+                FIXED_TO_USER_ROTATION_DEFAULT);
     }
 
     // ========================================
@@ -355,22 +363,7 @@
         when(mMockDisplayPolicy.isAwake()).thenReturn(true);
         when(mMockDisplayPolicy.isKeyguardDrawComplete()).thenReturn(true);
         when(mMockDisplayPolicy.isWindowManagerDrawComplete()).thenReturn(true);
-        mTarget.setFixedToUserRotation(true);
-        mTarget.updateOrientationListener();
-        verifyOrientationListenerRegistration(0);
-    }
-
-    @Test
-    public void testNotEnablesSensor_ForceDefaultRotation() throws Exception {
-        mBuilder.build();
-        when(mMockRes.getBoolean(com.android.internal.R.bool.config_forceDefaultOrientation))
-                .thenReturn(true);
-        configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, false, false);
-
-        when(mMockDisplayPolicy.isScreenOnEarly()).thenReturn(true);
-        when(mMockDisplayPolicy.isAwake()).thenReturn(true);
-        when(mMockDisplayPolicy.isKeyguardDrawComplete()).thenReturn(true);
-        when(mMockDisplayPolicy.isWindowManagerDrawComplete()).thenReturn(true);
+        mTarget.setFixedToUserRotation(FIXED_TO_USER_ROTATION_ENABLED);
         mTarget.updateOrientationListener();
         verifyOrientationListenerRegistration(0);
     }
@@ -378,8 +371,6 @@
     @Test
     public void testNotEnablesSensor_ForceDefaultRotation_Car() throws Exception {
         mBuilder.build();
-        when(mMockRes.getBoolean(com.android.internal.R.bool.config_forceDefaultOrientation))
-                .thenReturn(true);
         configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, true, false);
 
         when(mMockDisplayPolicy.isScreenOnEarly()).thenReturn(true);
@@ -393,8 +384,6 @@
     @Test
     public void testNotEnablesSensor_ForceDefaultRotation_Tv() throws Exception {
         mBuilder.build();
-        when(mMockRes.getBoolean(com.android.internal.R.bool.config_forceDefaultOrientation))
-                .thenReturn(true);
         configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, false, true);
 
         when(mMockDisplayPolicy.isScreenOnEarly()).thenReturn(true);
@@ -405,6 +394,19 @@
         verifyOrientationListenerRegistration(0);
     }
 
+    @Test
+    public void testNotEnablesSensor_ForceDefaultRotation_Squared() throws Exception {
+        mBuilder.build();
+        configureDisplayRotation(SCREEN_ORIENTATION_LOCKED, false, false);
+
+        when(mMockDisplayPolicy.isScreenOnEarly()).thenReturn(true);
+        when(mMockDisplayPolicy.isAwake()).thenReturn(true);
+        when(mMockDisplayPolicy.isKeyguardDrawComplete()).thenReturn(true);
+        when(mMockDisplayPolicy.isWindowManagerDrawComplete()).thenReturn(true);
+        mTarget.updateOrientationListener();
+        verifyOrientationListenerRegistration(0);
+    }
+
     private void enableOrientationSensor() {
         when(mMockDisplayPolicy.isScreenOnEarly()).thenReturn(true);
         when(mMockDisplayPolicy.isAwake()).thenReturn(true);
@@ -513,21 +515,8 @@
     // Tests for Policy based Rotation
     // =================================
     @Test
-    public void testReturnsUserRotation_ForceDefaultRotation() throws Exception {
-        mBuilder.build();
-        when(mMockRes.getBoolean(com.android.internal.R.bool.config_forceDefaultOrientation))
-                .thenReturn(true);
-        configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, false, false);
-
-        assertEquals(Surface.ROTATION_0, mTarget.rotationForOrientation(SCREEN_ORIENTATION_PORTRAIT,
-                Surface.ROTATION_180));
-    }
-
-    @Test
     public void testReturnsUserRotation_ForceDefaultRotation_Car() throws Exception {
         mBuilder.build();
-        when(mMockRes.getBoolean(com.android.internal.R.bool.config_forceDefaultOrientation))
-                .thenReturn(true);
         configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, true, false);
 
         assertEquals(Surface.ROTATION_0, mTarget.rotationForOrientation(SCREEN_ORIENTATION_PORTRAIT,
@@ -537,8 +526,6 @@
     @Test
     public void testReturnsUserRotation_ForceDefaultRotation_Tv() throws Exception {
         mBuilder.build();
-        when(mMockRes.getBoolean(com.android.internal.R.bool.config_forceDefaultOrientation))
-                .thenReturn(true);
         configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, false, true);
 
         assertEquals(Surface.ROTATION_0, mTarget.rotationForOrientation(SCREEN_ORIENTATION_PORTRAIT,
@@ -546,6 +533,15 @@
     }
 
     @Test
+    public void testReturnsUserRotation_ForceDefaultRotation_Squared() throws Exception {
+        mBuilder.build();
+        configureDisplayRotation(SCREEN_ORIENTATION_LOCKED, false, false);
+
+        assertEquals(Surface.ROTATION_0, mTarget.rotationForOrientation(SCREEN_ORIENTATION_PORTRAIT,
+                Surface.ROTATION_180));
+    }
+
+    @Test
     public void testReturnsLidOpenRotation_LidOpen() throws Exception {
         mBuilder.setLidOpenRotation(Surface.ROTATION_90).build();
         configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, false, false);
@@ -591,7 +587,7 @@
         mBuilder.build();
         configureDisplayRotation(SCREEN_ORIENTATION_PORTRAIT, false, false);
 
-        mTarget.setFixedToUserRotation(true);
+        mTarget.setFixedToUserRotation(FIXED_TO_USER_ROTATION_ENABLED);
 
         freezeRotation(Surface.ROTATION_180);
 
@@ -625,7 +621,7 @@
     @Test
     public void testNotRespectAppRequestedOrientation_FixedToUserRotation() throws Exception {
         mBuilder.build();
-        mTarget.setFixedToUserRotation(true);
+        mTarget.setFixedToUserRotation(FIXED_TO_USER_ROTATION_ENABLED);
 
         assertFalse("Display rotation shouldn't respect app requested orientation if"
                 + " fixed to user rotation.", mTarget.respectAppRequestedOrientation());
@@ -647,9 +643,14 @@
                 width = 1080;
                 height = 1920;
                 break;
+            case SCREEN_ORIENTATION_LOCKED:
+                // We use locked for squared display.
+                width = 1080;
+                height = 1080;
+                break;
             default:
-                throw new IllegalArgumentException("displayOrientation needs to be either landscape"
-                        + " or portrait, but we got "
+                throw new IllegalArgumentException("displayOrientation needs to be landscape, "
+                        + "portrait or locked, but we got "
                         + ActivityInfo.screenOrientationToString(displayOrientation));
         }
 
@@ -659,6 +660,10 @@
                 .thenReturn(isCar);
         when(mockPackageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK))
                 .thenReturn(isTv);
+        when(mMockDisplayPolicy.getNonDecorDisplayWidth(anyInt(), anyInt(), anyInt(), anyInt(),
+                any())).thenReturn(width);
+        when(mMockDisplayPolicy.getNonDecorDisplayHeight(anyInt(), anyInt(), anyInt(), anyInt(),
+                any())).thenReturn(height);
 
         final int shortSizeDp = (isCar || isTv) ? 540 : 720;
         final int longSizeDp = 960;
@@ -826,6 +831,9 @@
                     .thenReturn(convertRotationToDegrees(mDeskDockRotation));
             when(mMockRes.getInteger(com.android.internal.R.integer.config_undockedHdmiRotation))
                     .thenReturn(convertRotationToDegrees(mUndockedHdmiRotation));
+            when(mMockRes.getFloat(
+                    com.android.internal.R.dimen.config_closeToSquareDisplayMaxAspectRatio))
+                    .thenReturn(1.33f);
 
             mMockSensorManager = mock(SensorManager.class);
             when(mMockContext.getSystemService(Context.SENSOR_SERVICE))
diff --git a/services/tests/wmtests/src/com/android/server/wm/DisplayWindowSettingsTests.java b/services/tests/wmtests/src/com/android/server/wm/DisplayWindowSettingsTests.java
index 992d017..2dad187 100644
--- a/services/tests/wmtests/src/com/android/server/wm/DisplayWindowSettingsTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/DisplayWindowSettingsTests.java
@@ -26,6 +26,9 @@
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
+import static com.android.server.wm.DisplayRotation.FIXED_TO_USER_ROTATION_DEFAULT;
+import static com.android.server.wm.DisplayRotation.FIXED_TO_USER_ROTATION_DISABLED;
+import static com.android.server.wm.DisplayRotation.FIXED_TO_USER_ROTATION_ENABLED;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -407,7 +410,7 @@
     }
 
     @Test
-    public void testNotFixedToUserRotationByDefault() {
+    public void testFixedToUserRotationDefault() {
         mTarget.setUserRotation(mPrimaryDisplay, WindowManagerPolicy.USER_ROTATION_LOCKED,
                 Surface.ROTATION_0);
 
@@ -419,13 +422,14 @@
 
         mTarget.applySettingsToDisplayLocked(mPrimaryDisplay);
 
-        verify(displayRotation).restoreSettings(anyInt(), anyInt(), eq(false));
+        verify(displayRotation).restoreSettings(anyInt(), anyInt(),
+                eq(FIXED_TO_USER_ROTATION_DEFAULT));
         mockitoSession.finishMocking();
     }
 
     @Test
-    public void testSetFixedToUserRotation() {
-        mTarget.setFixedToUserRotation(mPrimaryDisplay, true);
+    public void testSetFixedToUserRotationDisabled() {
+        mTarget.setFixedToUserRotation(mPrimaryDisplay, FIXED_TO_USER_ROTATION_DISABLED);
 
         final MockitoSession mockitoSession = ExtendedMockito.mockitoSession()
                 .startMocking();
@@ -435,7 +439,25 @@
 
         applySettingsToDisplayByNewInstance(mPrimaryDisplay);
 
-        verify(displayRotation).restoreSettings(anyInt(), anyInt(), eq(true));
+        verify(displayRotation).restoreSettings(anyInt(), anyInt(),
+                eq(FIXED_TO_USER_ROTATION_DISABLED));
+        mockitoSession.finishMocking();
+    }
+
+    @Test
+    public void testSetFixedToUserRotationEnabled() {
+        mTarget.setFixedToUserRotation(mPrimaryDisplay, FIXED_TO_USER_ROTATION_ENABLED);
+
+        final MockitoSession mockitoSession = ExtendedMockito.mockitoSession()
+                .startMocking();
+        final DisplayRotation displayRotation = mock(DisplayRotation.class);
+        spyOn(mPrimaryDisplay);
+        doReturn(displayRotation).when(mPrimaryDisplay).getDisplayRotation();
+
+        applySettingsToDisplayByNewInstance(mPrimaryDisplay);
+
+        verify(displayRotation).restoreSettings(anyInt(), anyInt(),
+                eq(FIXED_TO_USER_ROTATION_ENABLED));
         mockitoSession.finishMocking();
     }