From 4162d60f3afad04fe27c3289b1f949d94d1f68e1 Mon Sep 17 00:00:00 2001 From: Kevin Chyn Date: Fri, 21 Apr 2023 22:00:11 +0000 Subject: [PATCH] Also reverse rotation for #freezeRotation path When auto-rotation is disabled, users can apply the suggested rotation using the rotation icon from SysUI. The sensor value passed to DisplayRotation#freezeRotation(int) that comes from WindowManagerService#freezeDisplayRotation also needs to apply the same reversing logic as DisplayRotation#rotationForOrientation. Fixes: 275286150 Test: atest DisplayRotationTests Test: atest RotationUtilsTest Change-Id: I985ae560d8eff6ea063206c1e39bd694ab576fde --- .../src/android/util/RotationUtilsTest.java | 16 ++++++++++++++++ .../com/android/server/wm/DisplayRotation.java | 4 ++++ .../server/wm/DisplayRotationTests.java | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/core/tests/coretests/src/android/util/RotationUtilsTest.java b/core/tests/coretests/src/android/util/RotationUtilsTest.java index 1b1ee4fb1a1c9..06b28f5b58ef0 100644 --- a/core/tests/coretests/src/android/util/RotationUtilsTest.java +++ b/core/tests/coretests/src/android/util/RotationUtilsTest.java @@ -19,6 +19,7 @@ package android.util; import static android.util.RotationUtils.rotateBounds; import static android.util.RotationUtils.rotatePoint; import static android.util.RotationUtils.rotatePointF; +import static android.view.Surface.ROTATION_0; import static android.view.Surface.ROTATION_180; import static android.view.Surface.ROTATION_270; import static android.view.Surface.ROTATION_90; @@ -103,4 +104,19 @@ public class RotationUtilsTest { assertEquals(560f, testResult.x, .1f); assertEquals(60f, testResult.y, .1f); } + + @Test + public void testReverseRotationDirectionAroundZAxis() { + assertEquals(ROTATION_90, + RotationUtils.reverseRotationDirectionAroundZAxis(ROTATION_270)); + assertEquals(ROTATION_270, + RotationUtils.reverseRotationDirectionAroundZAxis(ROTATION_90)); + assertEquals(ROTATION_0, + RotationUtils.reverseRotationDirectionAroundZAxis(ROTATION_0)); + assertEquals(ROTATION_180, + RotationUtils.reverseRotationDirectionAroundZAxis(ROTATION_180)); + + assertEquals(-1, + RotationUtils.reverseRotationDirectionAroundZAxis(-1)); + } } diff --git a/services/core/java/com/android/server/wm/DisplayRotation.java b/services/core/java/com/android/server/wm/DisplayRotation.java index 20048ce543f31..a5e652cce41ee 100644 --- a/services/core/java/com/android/server/wm/DisplayRotation.java +++ b/services/core/java/com/android/server/wm/DisplayRotation.java @@ -946,6 +946,10 @@ public class DisplayRotation { } void freezeRotation(int rotation) { + if (mDeviceStateController.shouldReverseRotationDirectionAroundZAxis()) { + rotation = RotationUtils.reverseRotationDirectionAroundZAxis(rotation); + } + rotation = (rotation == -1) ? mRotation : rotation; setUserRotation(WindowManagerPolicy.USER_ROTATION_LOCKED, rotation); } 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 4b2d1071d1131..8e80485bca7c6 100644 --- a/services/tests/wmtests/src/com/android/server/wm/DisplayRotationTests.java +++ b/services/tests/wmtests/src/com/android/server/wm/DisplayRotationTests.java @@ -541,6 +541,24 @@ public class DisplayRotationTests { SCREEN_ORIENTATION_UNSPECIFIED, Surface.ROTATION_180)); } + @Test + public void testFreezeRotation_reverseRotationDirectionAroundZAxis_yes() throws Exception { + mBuilder.build(); + when(mDeviceStateController.shouldReverseRotationDirectionAroundZAxis()).thenReturn(true); + + freezeRotation(Surface.ROTATION_90); + assertEquals(Surface.ROTATION_270, mTarget.getUserRotation()); + } + + @Test + public void testFreezeRotation_reverseRotationDirectionAroundZAxis_no() throws Exception { + mBuilder.build(); + when(mDeviceStateController.shouldReverseRotationDirectionAroundZAxis()).thenReturn(false); + + freezeRotation(Surface.ROTATION_90); + assertEquals(Surface.ROTATION_90, mTarget.getUserRotation()); + } + private boolean waitForUiHandler() { final CountDownLatch latch = new CountDownLatch(1); UiThread.getHandler().post(latch::countDown);