Use the orientation from the top fullscreen window.

Previously we explicitly ignore orientation from all windows when any
freeform stack is visible. That may lead to stretched content in the
topmost fullscreen application on phone's primary display. Therefore we
allow the topmost fullscreen window specify display rotation even when
some freeform windows are visible.

This CL may lead to unexpected display rotations on external displays
connected to phones with freeform window management support. They need
to explicitly set fixed to user rotation if they want to fix rotations
on external displays. Therefore we should expose that through hidden API
to SysUI as well.

Bug: 144185986
Test: Display rotation can be set by the topmost fullscreen window.
Test: atest DisplayContentTest#testAllowsTopmostFullscreenOrientation
Change-Id: Idbe3daa7500c603c19232d01fb42f1d1f60d9c5e
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 571f214..f875c33 100644
--- a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
@@ -16,6 +16,7 @@
 
 package com.android.server.wm;
 
+import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
 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;
@@ -700,6 +701,37 @@
     }
 
     @Test
+    public void testAllowsTopmostFullscreenOrientation() {
+        final DisplayContent dc = createNewDisplay();
+        dc.getDisplayRotation().setFixedToUserRotation(
+                DisplayRotation.FIXED_TO_USER_ROTATION_DISABLED);
+
+        final ActivityStack stack =
+                new ActivityTestsBase.StackBuilder(mWm.mAtmService.mRootActivityContainer)
+                        .setDisplay(dc.mActivityDisplay)
+                        .build();
+        doReturn(true).when(stack).isVisible();
+
+        final ActivityStack freeformStack =
+                new ActivityTestsBase.StackBuilder(mWm.mAtmService.mRootActivityContainer)
+                        .setDisplay(dc.mActivityDisplay)
+                        .setWindowingMode(WINDOWING_MODE_FREEFORM)
+                        .build();
+        doReturn(true).when(freeformStack).isVisible();
+        freeformStack.getTopChild().setBounds(100, 100, 300, 400);
+
+        assertTrue(dc.isStackVisible(WINDOWING_MODE_FREEFORM));
+
+        freeformStack.getTopNonFinishingActivity().setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
+        stack.getTopNonFinishingActivity().setOrientation(SCREEN_ORIENTATION_PORTRAIT);
+        assertEquals(SCREEN_ORIENTATION_PORTRAIT, dc.getOrientation());
+
+        stack.getTopNonFinishingActivity().setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
+        freeformStack.getTopNonFinishingActivity().setOrientation(SCREEN_ORIENTATION_PORTRAIT);
+        assertEquals(SCREEN_ORIENTATION_LANDSCAPE, dc.getOrientation());
+    }
+
+    @Test
     public void testOnDescendantOrientationRequestChanged() {
         final DisplayContent dc = createNewDisplay();
         dc.getDisplayRotation().setFixedToUserRotation(