Ignore orientation requests from apps on the close-to-square display

We don't rotate the screen for apps on the close-to-square display
because apps won't gain much benefits from that. However, We still
rotate the display for the user as long as "Auto-rotate" is on.

Bug: 129909104
Test: atest DisplayContentTests
Change-Id: I2feb5fc457c42d043440062d3aa99d2aebb9686e
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 e60e54c..bbba0a00 100644
--- a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
@@ -19,6 +19,7 @@
 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
+import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_USER;
 import static android.os.Build.VERSION_CODES.P;
 import static android.os.Build.VERSION_CODES.Q;
 import static android.view.Display.DEFAULT_DISPLAY;
@@ -29,6 +30,7 @@
 import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
+import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
 import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
 import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
 import static android.view.WindowManager.LayoutParams.TYPE_VOICE_INTERACTION;
@@ -528,6 +530,43 @@
     }
 
     @Test
+    public void testOrientationForAspectRatio() {
+        final DisplayContent dc = createNewDisplay();
+
+        // When display content is created its configuration is not yet initialized, which could
+        // cause unnecessary configuration propagation, so initialize it here.
+        final Configuration config = new Configuration();
+        dc.computeScreenConfiguration(config);
+        dc.onRequestedOverrideConfigurationChanged(config);
+
+        // Create a window that requests a fixed orientation. It will define device orientation
+        // by default.
+        final WindowState window = createWindow(null /* parent */, TYPE_APPLICATION_OVERLAY, dc,
+                "window");
+        window.mHasSurface = true;
+        window.mAttrs.screenOrientation = SCREEN_ORIENTATION_LANDSCAPE;
+
+        // --------------------------------
+        // Test non-close-to-square display
+        // --------------------------------
+        dc.mBaseDisplayWidth = 1000;
+        dc.mBaseDisplayHeight = (int) (dc.mBaseDisplayWidth * dc.mCloseToSquareMaxAspectRatio * 2f);
+        dc.configureDisplayPolicy();
+
+        assertEquals("Screen orientation must be defined by the window by default.",
+                window.mAttrs.screenOrientation, dc.getOrientation());
+
+        // ----------------------------
+        // Test close-to-square display
+        // ----------------------------
+        dc.mBaseDisplayHeight = dc.mBaseDisplayWidth;
+        dc.configureDisplayPolicy();
+
+        assertEquals("Screen orientation must be SCREEN_ORIENTATION_USER.",
+                SCREEN_ORIENTATION_USER, dc.getOrientation());
+    }
+
+    @Test
     public void testDisableDisplayInfoOverrideFromWindowManager() {
         final DisplayContent dc = createNewDisplay();
 
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 1c10ffb0..49d38c0 100644
--- a/services/tests/wmtests/src/com/android/server/wm/DisplayRotationTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/DisplayRotationTests.java
@@ -17,7 +17,6 @@
 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;
 
@@ -394,19 +393,6 @@
         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);
@@ -533,15 +519,6 @@
     }
 
     @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);
@@ -643,14 +620,9 @@
                 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 landscape, "
-                        + "portrait or locked, but we got "
+                throw new IllegalArgumentException("displayOrientation needs to be either landscape"
+                        + " or portrait, but we got "
                         + ActivityInfo.screenOrientationToString(displayOrientation));
         }
 
@@ -660,10 +632,6 @@
                 .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;
@@ -831,9 +799,6 @@
                     .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))