OSDN Git Service

Introduce default value to fixed to user rotation.
authorGarfield Tan <xutan@google.com>
Tue, 19 Feb 2019 18:45:35 +0000 (10:45 -0800)
committerGarfield Tan <xutan@google.com>
Thu, 21 Feb 2019 23:52:02 +0000 (15:52 -0800)
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

core/res/res/values/config.xml
core/res/res/values/symbols.xml
services/core/java/com/android/server/wm/DisplayRotation.java
services/core/java/com/android/server/wm/DisplayWindowSettings.java
services/core/java/com/android/server/wm/WindowManagerService.java
services/core/java/com/android/server/wm/WindowManagerShellCommand.java
services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java
services/tests/wmtests/src/com/android/server/wm/DisplayRotationTests.java
services/tests/wmtests/src/com/android/server/wm/DisplayWindowSettingsTests.java

index 6571cd2..eb69535 100644 (file)
 
     </string-array>
 
-    <!-- Flag indicating that this device does not rotate and will always remain in its default
-         orientation. Activities that desire to run in a non-compatible orientation will be run
-         from an emulated display within the physical display. -->
-    <bool name="config_forceDefaultOrientation">false</bool>
-
     <!-- Default Gravity setting for the system Toast view. Equivalent to: Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM -->
     <integer name="config_toastDefaultGravity">0x00000051</integer>
 
index 4ae239e..8df0e19 100644 (file)
   <java-symbol type="bool" name="config_requireRadioPowerOffOnSimRefreshReset" />
   <java-symbol type="bool" name="config_speed_up_audio_on_mt_calls" />
   <java-symbol type="bool" name="config_useFixedVolume" />
-  <java-symbol type="bool" name="config_forceDefaultOrientation" />
   <java-symbol type="bool" name="config_wifi_batched_scan_supported" />
   <java-symbol type="bool" name="config_wifi_softap_acs_supported" />
   <java-symbol type="string" name="config_wifi_softap_acs_supported_channel_list" />
index 543f196..c6b7060 100644 (file)
@@ -21,6 +21,7 @@ import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
 
+import android.annotation.IntDef;
 import android.annotation.UserIdInt;
 import android.app.ActivityManager;
 import android.content.ContentResolver;
@@ -49,6 +50,8 @@ import com.android.server.policy.WindowOrientationListener;
 import com.android.server.statusbar.StatusBarManagerInternal;
 
 import java.io.PrintWriter;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
 
 /**
  * Defines the mapping between orientation and rotation of a display.
@@ -96,11 +99,36 @@ public class DisplayRotation {
     private int mUserRotation = Surface.ROTATION_0;
 
     /**
+     * Flag that indicates this is a display that may run better when fixed to user rotation.
+     */
+    private boolean mDefaultFixedToUserRotation;
+
+    /**
+     * No overridden behavior is provided in terms of fixing rotation to user rotation. Use other
+     * flags to derive the default behavior, such as {@link WindowManagerService#mIsPc} and
+     * {@link WindowManagerService#mForceDesktopModeOnExternalDisplays}.
+     */
+    static final int FIXED_TO_USER_ROTATION_DEFAULT = 0;
+    /**
+     * Don't fix display rotation to {@link #mUserRotation} only. Always allow other factors to play
+     * a role in deciding display rotation.
+     */
+    static final int FIXED_TO_USER_ROTATION_DISABLED = 1;
+    /**
+     * Only use {@link #mUserRotation} as the display rotation.
+     */
+    static final int FIXED_TO_USER_ROTATION_ENABLED = 2;
+    @IntDef({ FIXED_TO_USER_ROTATION_DEFAULT, FIXED_TO_USER_ROTATION_DISABLED,
+            FIXED_TO_USER_ROTATION_ENABLED })
+    @Retention(RetentionPolicy.SOURCE)
+    @interface FixedToUserRotation {}
+
+    /**
      * A flag to indicate if the display rotation should be fixed to user specified rotation
      * regardless of all other states (including app requrested orientation). {@code true} the
      * display rotation should be fixed to user specified rotation, {@code false} otherwise.
      */
-    private boolean mFixedToUserRotation;
+    private int mFixedToUserRotation = FIXED_TO_USER_ROTATION_DEFAULT;
 
     private int mDemoHdmiRotation;
     private int mDemoRotation;
@@ -220,19 +248,14 @@ public class DisplayRotation {
                 PackageManager.FEATURE_LEANBACK);
         final boolean isCloseToSquare =
                 isNonDecorDisplayCloseToSquare(Surface.ROTATION_0, width, height);
-        final boolean forceDefaultOrientationInRes =
-                res.getBoolean(com.android.internal.R.bool.config_forceDefaultOrientation);
-        final boolean forceDefaultOrienation =
-                ((longSizeDp >= 960 && shortSizeDp >= 720) || isCar || isTv || isCloseToSquare)
-                        && forceDefaultOrientationInRes
-                        // For debug purposes the next line turns this feature off with:
-                        // $ adb shell setprop config.override_forced_orient true
-                        // $ adb shell wm size reset
-                        && !"true".equals(SystemProperties.get("config.override_forced_orient"));
-        // Configuration says we force to use the default orientation. We can fall back to fix
-        // rotation to only user rotation. As long as OEM doesn't change user rotation then the
-        // rotation of this display is effectively stuck at 0 deg.
-        setFixedToUserRotation(forceDefaultOrienation);
+        final boolean forceDesktopMode =
+                mService.mForceDesktopModeOnExternalDisplays && !isDefaultDisplay;
+        mDefaultFixedToUserRotation =
+                (isCar || isTv || mService.mIsPc || forceDesktopMode || isCloseToSquare)
+                // For debug purposes the next line turns this feature off with:
+                // $ adb shell setprop config.override_forced_orient true
+                // $ adb shell wm size reset
+                && !"true".equals(SystemProperties.get("config.override_forced_orient"));
     }
 
     private boolean isNonDecorDisplayCloseToSquare(int rotation, int width, int height) {
@@ -263,7 +286,7 @@ public class DisplayRotation {
     }
 
     void restoreSettings(int userRotationMode, int userRotation,
-            boolean fixedToUserRotation) {
+            @FixedToUserRotation int fixedToUserRotation) {
         mFixedToUserRotation = fixedToUserRotation;
 
         // We will retrieve user rotation and user rotation mode from settings for default display.
@@ -285,14 +308,13 @@ public class DisplayRotation {
         mUserRotation = userRotation;
     }
 
-    void setFixedToUserRotation(boolean fixedToUserRotation) {
+    void setFixedToUserRotation(@FixedToUserRotation int fixedToUserRotation) {
         if (mFixedToUserRotation == fixedToUserRotation) {
             return;
         }
 
         mFixedToUserRotation = fixedToUserRotation;
-        mDisplayWindowSettings.setFixedToUserRotation(mDisplayContent,
-                fixedToUserRotation);
+        mDisplayWindowSettings.setFixedToUserRotation(mDisplayContent, fixedToUserRotation);
         mService.updateRotation(true /* alwaysSendConfiguration */,
                 false /* forceRelayout */);
     }
@@ -346,7 +368,14 @@ public class DisplayRotation {
     }
 
     boolean isFixedToUserRotation() {
-        return mFixedToUserRotation;
+        switch (mFixedToUserRotation) {
+            case FIXED_TO_USER_ROTATION_DISABLED:
+                return false;
+            case FIXED_TO_USER_ROTATION_ENABLED:
+                return true;
+            default:
+                return mDefaultFixedToUserRotation;
+        }
     }
 
     /**
@@ -355,7 +384,7 @@ public class DisplayRotation {
      * false} is when {@link #isFixedToUserRotation()} is {@code true}.
      */
     boolean respectAppRequestedOrientation() {
-        return !mFixedToUserRotation;
+        return !isFixedToUserRotation();
     }
 
     public int getLandscapeRotation() {
@@ -461,7 +490,7 @@ public class DisplayRotation {
      * screen is switched off.
      */
     private boolean needSensorRunning() {
-        if (mFixedToUserRotation) {
+        if (isFixedToUserRotation()) {
             // We are sure we only respect user rotation settings, so we are sure we will not
             // support sensor rotation.
             return false;
@@ -527,7 +556,7 @@ public class DisplayRotation {
                         );
         }
 
-        if (mFixedToUserRotation) {
+        if (isFixedToUserRotation()) {
             return mUserRotation;
         }
 
@@ -739,7 +768,7 @@ public class DisplayRotation {
         // demo, hdmi, vr, etc mode.
 
         // Determine if the rotation is currently forced.
-        if (mFixedToUserRotation) {
+        if (isFixedToUserRotation()) {
             return false; // Rotation is forced to user settings.
         }
 
@@ -899,7 +928,7 @@ public class DisplayRotation {
         pw.print(" mDemoHdmiRotationLock=" + mDemoHdmiRotationLock);
         pw.println(" mUndockedHdmiRotation=" + Surface.rotationToString(mUndockedHdmiRotation));
         pw.println(prefix + "  mLidOpenRotation=" + Surface.rotationToString(mLidOpenRotation));
-        pw.println(prefix + "  mFixedToUserRotation=" + mFixedToUserRotation);
+        pw.println(prefix + "  mFixedToUserRotation=" + isFixedToUserRotation());
     }
 
     private class OrientationListener extends WindowOrientationListener {
index 5cfa7de..4617890 100644 (file)
@@ -22,6 +22,7 @@ import static android.view.WindowManager.REMOVE_CONTENT_MODE_UNDEFINED;
 
 import static com.android.server.wm.DisplayContent.FORCE_SCALING_MODE_AUTO;
 import static com.android.server.wm.DisplayContent.FORCE_SCALING_MODE_DISABLED;
+import static com.android.server.wm.DisplayRotation.FIXED_TO_USER_ROTATION_DEFAULT;
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
 
@@ -80,7 +81,8 @@ class DisplayWindowSettings {
         private boolean mShouldShowWithInsecureKeyguard = false;
         private boolean mShouldShowSystemDecors = false;
         private boolean mShouldShowIme = false;
-        private boolean mFixedToUserRotation;
+        private @DisplayRotation.FixedToUserRotation int mFixedToUserRotation =
+                FIXED_TO_USER_ROTATION_DEFAULT;
 
         private Entry(String name) {
             mName = name;
@@ -99,7 +101,7 @@ class DisplayWindowSettings {
                     && !mShouldShowWithInsecureKeyguard
                     && !mShouldShowSystemDecors
                     && !mShouldShowIme
-                    && !mFixedToUserRotation;
+                    && mFixedToUserRotation == FIXED_TO_USER_ROTATION_DEFAULT;
         }
     }
 
@@ -188,7 +190,8 @@ class DisplayWindowSettings {
         writeSettingsIfNeeded(entry, displayInfo);
     }
 
-    void setFixedToUserRotation(DisplayContent displayContent, boolean fixedToUserRotation) {
+    void setFixedToUserRotation(DisplayContent displayContent,
+            @DisplayRotation.FixedToUserRotation int fixedToUserRotation) {
         final DisplayInfo displayInfo = displayContent.getDisplayInfo();
         final Entry entry = getOrCreateEntry(displayInfo);
         entry.mFixedToUserRotation = fixedToUserRotation;
@@ -464,8 +467,7 @@ class DisplayWindowSettings {
                     "shouldShowWithInsecureKeyguard");
             entry.mShouldShowSystemDecors = getBooleanAttribute(parser, "shouldShowSystemDecors");
             entry.mShouldShowIme = getBooleanAttribute(parser, "shouldShowIme");
-            entry.mFixedToUserRotation = getBooleanAttribute(parser,
-                    "fixedToUserRotation");
+            entry.mFixedToUserRotation = getIntAttribute(parser, "fixedToUserRotation");
             mEntries.put(name, entry);
         }
         XmlUtils.skipCurrentTag(parser);
@@ -549,9 +551,9 @@ class DisplayWindowSettings {
                 if (entry.mShouldShowIme) {
                     out.attribute(null, "shouldShowIme", Boolean.toString(entry.mShouldShowIme));
                 }
-                if (entry.mFixedToUserRotation) {
+                if (entry.mFixedToUserRotation != FIXED_TO_USER_ROTATION_DEFAULT) {
                     out.attribute(null, "fixedToUserRotation",
-                            Boolean.toString(entry.mFixedToUserRotation));
+                            Integer.toString(entry.mFixedToUserRotation));
                 }
                 out.endTag(null, "display");
             }
index 39df0e4..7efd1a7 100644 (file)
@@ -3503,14 +3503,15 @@ public class WindowManagerService extends IWindowManager.Stub
         }
     }
 
-    void setRotateForApp(int displayId, boolean enabled) {
+    void setRotateForApp(int displayId,
+            @DisplayRotation.FixedToUserRotation int fixedToUserRotation) {
         synchronized (mGlobalLock) {
             final DisplayContent display = mRoot.getDisplayContent(displayId);
             if (display == null) {
                 Slog.w(TAG, "Trying to set rotate for app for a missing display.");
                 return;
             }
-            display.getDisplayRotation().setFixedToUserRotation(enabled);
+            display.getDisplayRotation().setFixedToUserRotation(fixedToUserRotation);
         }
     }
 
index d13ee45..7384bb7 100644 (file)
@@ -342,21 +342,24 @@ public class WindowManagerShellCommand extends ShellCommand {
             arg = getNextArgRequired();
         }
 
-        final boolean enabled;
+        final @DisplayRotation.FixedToUserRotation  int fixedToUserRotation;
         switch (arg) {
             case "enabled":
-                enabled = true;
+                fixedToUserRotation = DisplayRotation.FIXED_TO_USER_ROTATION_ENABLED;
                 break;
             case "disabled":
-                enabled = false;
+                fixedToUserRotation = DisplayRotation.FIXED_TO_USER_ROTATION_DISABLED;
+                break;
+            case "default":
+                fixedToUserRotation = DisplayRotation.FIXED_TO_USER_ROTATION_DISABLED;
                 break;
             default:
-                getErrPrintWriter().println("Error: expecting enabled or disabled, but we get "
-                        + arg);
+                getErrPrintWriter().println("Error: expecting enabled, disabled or default, but we "
+                        + "get " + arg);
                 return -1;
         }
 
-        mInternal.setRotateForApp(displayId, enabled);
+        mInternal.setRotateForApp(displayId, fixedToUserRotation);
         return 0;
     }
 
index b1b8e8c..69f7ced 100644 (file)
@@ -621,7 +621,8 @@ public class DisplayContentTests extends WindowTestsBase {
     @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
index 8733674..1c10ffb 100644 (file)
@@ -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.same;
 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 @@ public class DisplayRotationTests {
 
     @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 @@ public class DisplayRotationTests {
 
     @Test
     public void testPersistsUserRotation_UnlockRotation_NonDefaultDisplay() throws Exception {
-        mBuilder.mIsDefaultDisplay = false;
-
-        mBuilder.build();
+        mBuilder.setIsDefaultDisplay(false).build();
 
         thawRotation();
 
@@ -203,14 +203,22 @@ public class DisplayRotationTests {
     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,17 +363,15 @@ public class DisplayRotationTests {
         when(mMockDisplayPolicy.isAwake()).thenReturn(true);
         when(mMockDisplayPolicy.isKeyguardDrawComplete()).thenReturn(true);
         when(mMockDisplayPolicy.isWindowManagerDrawComplete()).thenReturn(true);
-        mTarget.setFixedToUserRotation(true);
+        mTarget.setFixedToUserRotation(FIXED_TO_USER_ROTATION_ENABLED);
         mTarget.updateOrientationListener();
         verifyOrientationListenerRegistration(0);
     }
 
     @Test
-    public void testNotEnablesSensor_ForceDefaultRotation() throws Exception {
+    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, false, false);
+        configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, true, false);
 
         when(mMockDisplayPolicy.isScreenOnEarly()).thenReturn(true);
         when(mMockDisplayPolicy.isAwake()).thenReturn(true);
@@ -376,11 +382,9 @@ public class DisplayRotationTests {
     }
 
     @Test
-    public void testNotEnablesSensor_ForceDefaultRotation_Car() throws Exception {
+    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, true, false);
+        configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, false, true);
 
         when(mMockDisplayPolicy.isScreenOnEarly()).thenReturn(true);
         when(mMockDisplayPolicy.isAwake()).thenReturn(true);
@@ -391,11 +395,9 @@ public class DisplayRotationTests {
     }
 
     @Test
-    public void testNotEnablesSensor_ForceDefaultRotation_Tv() throws Exception {
+    public void testNotEnablesSensor_ForceDefaultRotation_Squared() throws Exception {
         mBuilder.build();
-        when(mMockRes.getBoolean(com.android.internal.R.bool.config_forceDefaultOrientation))
-                .thenReturn(true);
-        configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, false, true);
+        configureDisplayRotation(SCREEN_ORIENTATION_LOCKED, false, false);
 
         when(mMockDisplayPolicy.isScreenOnEarly()).thenReturn(true);
         when(mMockDisplayPolicy.isAwake()).thenReturn(true);
@@ -513,33 +515,27 @@ public class DisplayRotationTests {
     // Tests for Policy based Rotation
     // =================================
     @Test
-    public void testReturnsUserRotation_ForceDefaultRotation() throws Exception {
+    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, false, false);
+        configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, true, false);
 
         assertEquals(Surface.ROTATION_0, mTarget.rotationForOrientation(SCREEN_ORIENTATION_PORTRAIT,
                 Surface.ROTATION_180));
     }
 
     @Test
-    public void testReturnsUserRotation_ForceDefaultRotation_Car() throws Exception {
+    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, true, false);
+        configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, false, true);
 
         assertEquals(Surface.ROTATION_0, mTarget.rotationForOrientation(SCREEN_ORIENTATION_PORTRAIT,
                 Surface.ROTATION_180));
     }
 
     @Test
-    public void testReturnsUserRotation_ForceDefaultRotation_Tv() throws Exception {
+    public void testReturnsUserRotation_ForceDefaultRotation_Squared() throws Exception {
         mBuilder.build();
-        when(mMockRes.getBoolean(com.android.internal.R.bool.config_forceDefaultOrientation))
-                .thenReturn(true);
-        configureDisplayRotation(SCREEN_ORIENTATION_LANDSCAPE, false, true);
+        configureDisplayRotation(SCREEN_ORIENTATION_LOCKED, false, false);
 
         assertEquals(Surface.ROTATION_0, mTarget.rotationForOrientation(SCREEN_ORIENTATION_PORTRAIT,
                 Surface.ROTATION_180));
@@ -591,7 +587,7 @@ public class DisplayRotationTests {
         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 @@ public class DisplayRotationTests {
     @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 @@ public class DisplayRotationTests {
                 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 @@ public class DisplayRotationTests {
                 .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 @@ public class DisplayRotationTests {
                     .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))
index 992d017..2dad187 100644 (file)
@@ -26,6 +26,9 @@ import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
 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 @@ public class DisplayWindowSettingsTests extends WindowTestsBase {
     }
 
     @Test
-    public void testNotFixedToUserRotationByDefault() {
+    public void testFixedToUserRotationDefault() {
         mTarget.setUserRotation(mPrimaryDisplay, WindowManagerPolicy.USER_ROTATION_LOCKED,
                 Surface.ROTATION_0);
 
@@ -419,13 +422,14 @@ public class DisplayWindowSettingsTests extends WindowTestsBase {
 
         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 @@ public class DisplayWindowSettingsTests extends WindowTestsBase {
 
         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();
     }