From 1377ec6664448d0c771e923f86c77a2b656ae496 Mon Sep 17 00:00:00 2001 From: Garfield Tan Date: Fri, 19 Feb 2021 12:10:42 -0800 Subject: [PATCH] Update 2 fields when fixed to user rotation changed Orientation and orientation requesting TDA are affected by its value, so we need to make sure they are updated when the value changed. Bug: 180745199 Test: Updating fixed-to-user-rotation triggers display rotation when necessary. Test: atest DisplayContentTests Test: atest ActivityRecordTests#testReportOrientationChange on cf Change-Id: I7b0ecac9337e50ff78b3b41eba3c3342e64c04af --- .../android/server/wm/DisplayRotation.java | 9 +++- .../server/wm/ActivityRecordTests.java | 2 +- .../server/wm/DisplayContentTests.java | 43 +++++++++++++++---- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/services/core/java/com/android/server/wm/DisplayRotation.java b/services/core/java/com/android/server/wm/DisplayRotation.java index b106657dee995..d5ded97ee0b90 100644 --- a/services/core/java/com/android/server/wm/DisplayRotation.java +++ b/services/core/java/com/android/server/wm/DisplayRotation.java @@ -788,8 +788,13 @@ public class DisplayRotation { mFixedToUserRotation = fixedToUserRotation; mDisplayWindowSettings.setFixedToUserRotation(mDisplayContent, fixedToUserRotation); - mService.updateRotation(true /* alwaysSendConfiguration */, - false /* forceRelayout */); + if (mDisplayContent.mFocusedApp != null) { + // We record the last focused TDA that respects orientation request, check if this + // change may affect it. + mDisplayContent.onLastFocusedTaskDisplayAreaChanged( + mDisplayContent.mFocusedApp.getDisplayArea()); + } + mDisplayContent.updateOrientation(); } @VisibleForTesting diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java index aa1110cd55a76..488e629f5790a 100644 --- a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java +++ b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java @@ -2288,7 +2288,7 @@ public class ActivityRecordTests extends WindowTestsBase { IWindowManager.FIXED_TO_USER_ROTATION_ENABLED); reset(task); activity.reportDescendantOrientationChangeIfNeeded(); - verify(task).onConfigurationChanged(any(Configuration.class)); + verify(task, atLeast(1)).onConfigurationChanged(any(Configuration.class)); } @Test 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 58169bb1e0735..dc702e6bc5728 100644 --- a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java +++ b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java @@ -32,6 +32,7 @@ import static android.view.DisplayCutout.fromBoundingRect; import static android.view.InsetsState.ITYPE_NAVIGATION_BAR; import static android.view.InsetsState.ITYPE_STATUS_BAR; import static android.view.Surface.ROTATION_0; +import static android.view.Surface.ROTATION_180; import static android.view.Surface.ROTATION_90; import static android.view.WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE; import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW; @@ -121,6 +122,7 @@ import androidx.test.filters.SmallTest; import com.android.internal.logging.MetricsLogger; import com.android.internal.logging.nano.MetricsProto; +import com.android.server.policy.WindowManagerPolicy; import com.android.server.wm.utils.WmDisplayCutout; import org.junit.Test; @@ -955,16 +957,14 @@ public class DisplayContentTests extends WindowTestsBase { IWindowManager.FIXED_TO_USER_ROTATION_DISABLED); final int newOrientation = getRotatedOrientation(dc); - final Task stack = new TaskBuilder(mSupervisor) + final Task task = new TaskBuilder(mSupervisor) .setDisplay(dc).setCreateActivity(true).build(); - final ActivityRecord activity = stack.getTopMostTask().getTopNonFinishingActivity(); + final ActivityRecord activity = task.getTopMostTask().getTopNonFinishingActivity(); + dc.setFocusedApp(activity); activity.setRequestedOrientation(newOrientation); - final int expectedOrientation = newOrientation == SCREEN_ORIENTATION_PORTRAIT - ? Configuration.ORIENTATION_PORTRAIT - : Configuration.ORIENTATION_LANDSCAPE; - assertEquals(expectedOrientation, dc.getConfiguration().orientation); + assertTrue("The display should be rotated.", dc.getRotation() % 2 == 1); } @Test @@ -972,17 +972,42 @@ public class DisplayContentTests extends WindowTestsBase { final DisplayContent dc = createNewDisplay(); dc.getDisplayRotation().setFixedToUserRotation( IWindowManager.FIXED_TO_USER_ROTATION_ENABLED); + dc.getDisplayRotation().setUserRotation( + WindowManagerPolicy.USER_ROTATION_LOCKED, ROTATION_180); final int newOrientation = getRotatedOrientation(dc); - final Task stack = new TaskBuilder(mSupervisor) + final Task task = new TaskBuilder(mSupervisor) .setDisplay(dc).setCreateActivity(true).build(); - final ActivityRecord activity = stack.getTopMostTask().getTopNonFinishingActivity(); + final ActivityRecord activity = task.getTopMostTask().getTopNonFinishingActivity(); + dc.setFocusedApp(activity); activity.setRequestedOrientation(newOrientation); verify(dc, never()).updateDisplayOverrideConfigurationLocked(any(), eq(activity), anyBoolean(), same(null)); - assertEquals(dc.getDisplayRotation().getUserRotation(), dc.getRotation()); + assertEquals(ROTATION_180, dc.getRotation()); + } + + @Test + public void testFixedToUserRotationChanged() { + final DisplayContent dc = createNewDisplay(); + dc.getDisplayRotation().setFixedToUserRotation( + IWindowManager.FIXED_TO_USER_ROTATION_ENABLED); + dc.getDisplayRotation().setUserRotation( + WindowManagerPolicy.USER_ROTATION_LOCKED, ROTATION_0); + final int newOrientation = getRotatedOrientation(dc); + + final Task task = new TaskBuilder(mSupervisor) + .setDisplay(dc).setCreateActivity(true).build(); + final ActivityRecord activity = task.getTopMostTask().getTopNonFinishingActivity(); + dc.setFocusedApp(activity); + + activity.setRequestedOrientation(newOrientation); + + dc.getDisplayRotation().setFixedToUserRotation( + IWindowManager.FIXED_TO_USER_ROTATION_DISABLED); + + assertTrue("The display should be rotated.", dc.getRotation() % 2 == 1); } @Test