Only apply fixed orientation letterbox ratio if orientation mismatch
The fixed orientation letterbox aspect ration should only be applied to
fixed orientation activity where their orientation does not match the
screen orientation.
Flag: EXEMPT bug fix
Test: atest WmTests:DesktopAppCompatAspectRatioPolicyTests,
atest WmTests:DesktopModeLaunchParamsModifierTests
Fixes: 401322348
Change-Id: I135156ee167d42dbda92907f31c665db92e95892
diff --git a/services/core/java/com/android/server/wm/DesktopAppCompatAspectRatioPolicy.java b/services/core/java/com/android/server/wm/DesktopAppCompatAspectRatioPolicy.java
index fee5566..d808726 100644
--- a/services/core/java/com/android/server/wm/DesktopAppCompatAspectRatioPolicy.java
+++ b/services/core/java/com/android/server/wm/DesktopAppCompatAspectRatioPolicy.java
@@ -69,11 +69,11 @@
* Calculates the final aspect ratio of an launching activity based on the task it will be
* launched in. Takes into account any min or max aspect ratio constraints.
*/
- float calculateAspectRatio(@NonNull Task task) {
+ float calculateAspectRatio(@NonNull Task task, boolean hasOrientationMismatch) {
final float maxAspectRatio = getMaxAspectRatio();
final float minAspectRatio = getMinAspectRatio(task);
float desiredAspectRatio = 0;
- desiredAspectRatio = getDesiredAspectRatio(task);
+ desiredAspectRatio = getDesiredAspectRatio(task, hasOrientationMismatch);
if (maxAspectRatio >= 1 && desiredAspectRatio > maxAspectRatio) {
desiredAspectRatio = maxAspectRatio;
} else if (minAspectRatio >= 1 && desiredAspectRatio < minAspectRatio) {
@@ -87,13 +87,14 @@
* any min or max aspect ratio constraints.
*/
@VisibleForTesting
- float getDesiredAspectRatio(@NonNull Task task) {
+ float getDesiredAspectRatio(@NonNull Task task, boolean hasOrientationMismatch) {
final float letterboxAspectRatioOverride = getFixedOrientationLetterboxAspectRatio(task);
// Aspect ratio as suggested by the system. Apps requested mix/max aspect ratio will
// be respected in #calculateAspectRatio.
if (isDefaultMultiWindowLetterboxAspectRatioDesired(task)) {
return DEFAULT_LETTERBOX_ASPECT_RATIO_FOR_MULTI_WINDOW;
- } else if (letterboxAspectRatioOverride > MIN_FIXED_ORIENTATION_LETTERBOX_ASPECT_RATIO) {
+ } else if (hasOrientationMismatch
+ && letterboxAspectRatioOverride > MIN_FIXED_ORIENTATION_LETTERBOX_ASPECT_RATIO) {
return letterboxAspectRatioOverride;
}
return AppCompatUtils.computeAspectRatio(task.getDisplayArea().getBounds());
diff --git a/services/core/java/com/android/server/wm/DesktopModeBoundsCalculator.java b/services/core/java/com/android/server/wm/DesktopModeBoundsCalculator.java
index d935432..83ca5f6 100644
--- a/services/core/java/com/android/server/wm/DesktopModeBoundsCalculator.java
+++ b/services/core/java/com/android/server/wm/DesktopModeBoundsCalculator.java
@@ -17,7 +17,6 @@
package com.android.server.wm;
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LOCKED;
-import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
import static android.content.pm.ActivityInfo.isFixedOrientation;
import static android.content.pm.ActivityInfo.isFixedOrientationLandscape;
import static android.content.pm.ActivityInfo.isFixedOrientationPortrait;
@@ -32,8 +31,8 @@
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ActivityOptions;
-import android.content.pm.ActivityInfo.ScreenOrientation;
import android.content.pm.ActivityInfo.WindowLayout;
+import android.content.res.Configuration;
import android.graphics.Rect;
import android.os.SystemProperties;
import android.util.Size;
@@ -152,19 +151,25 @@
}
final DesktopAppCompatAspectRatioPolicy desktopAppCompatAspectRatioPolicy =
activity.mAppCompatController.getDesktopAspectRatioPolicy();
- float appAspectRatio = desktopAppCompatAspectRatioPolicy.calculateAspectRatio(task);
+ final int stableBoundsOrientation = stableBounds.height() >= stableBounds.width()
+ ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
+ int activityOrientation = getActivityConfigurationOrientation(
+ activity, task, stableBoundsOrientation);
+ // Use orientation mismatch to resolve aspect ratio to match fixed orientation letterboxing
+ // policy in {@link ActivityRecord.resolveFixedOrientationConfiguration}
+ final boolean hasOrientationMismatch = stableBoundsOrientation != activityOrientation;
+ float appAspectRatio = desktopAppCompatAspectRatioPolicy.calculateAspectRatio(
+ task, hasOrientationMismatch);
final float tdaWidth = stableBounds.width();
final float tdaHeight = stableBounds.height();
- final int taskConfigOrientation = task.getConfiguration().orientation;
- final int activityOrientation = getActivityOrientation(activity, task);
- final Size initialSize = switch (taskConfigOrientation) {
+ final Size initialSize = switch (stableBoundsOrientation) {
case ORIENTATION_LANDSCAPE -> {
// Device in landscape orientation.
if (appAspectRatio == 0) {
appAspectRatio = 1;
}
if (canChangeAspectRatio(desktopAppCompatAspectRatioPolicy, task)) {
- if (isFixedOrientationPortrait(activityOrientation)) {
+ if (hasOrientationMismatch) {
// For portrait resizeable activities, respect apps fullscreen width but
// apply ideal size height.
yield new Size((int) ((tdaHeight / appAspectRatio) + 0.5f),
@@ -183,7 +188,7 @@
final int customPortraitWidthForLandscapeApp = screenBounds.width()
- (DESKTOP_MODE_LANDSCAPE_APP_PADDING * 2);
if (canChangeAspectRatio(desktopAppCompatAspectRatioPolicy, task)) {
- if (isFixedOrientationLandscape(activityOrientation)) {
+ if (hasOrientationMismatch) {
if (appAspectRatio == 0) {
appAspectRatio = tdaWidth / (tdaWidth - 1);
}
@@ -198,7 +203,7 @@
if (appAspectRatio == 0) {
appAspectRatio = 1;
}
- if (isFixedOrientationLandscape(activityOrientation)) {
+ if (hasOrientationMismatch) {
// For landscape unresizeable activities, apply custom app width to ideal size
// and calculate maximum size with this area while maintaining original aspect
// ratio.
@@ -230,19 +235,23 @@
&& !desktopAppCompatAspectRatioPolicy.hasMinAspectRatioOverride(task);
}
- private static @ScreenOrientation int getActivityOrientation(
- @NonNull ActivityRecord activity, @NonNull Task task) {
+ private static @Configuration.Orientation int getActivityConfigurationOrientation(
+ @NonNull ActivityRecord activity, @NonNull Task task,
+ @Configuration.Orientation int stableBoundsOrientation) {
final int activityOrientation = activity.getOverrideOrientation();
final DesktopAppCompatAspectRatioPolicy desktopAppCompatAspectRatioPolicy =
activity.mAppCompatController.getDesktopAspectRatioPolicy();
- if (desktopAppCompatAspectRatioPolicy.shouldApplyUserMinAspectRatioOverride(task)
+ if ((desktopAppCompatAspectRatioPolicy.shouldApplyUserMinAspectRatioOverride(task)
&& (!isFixedOrientation(activityOrientation)
- || activityOrientation == SCREEN_ORIENTATION_LOCKED)) {
+ || activityOrientation == SCREEN_ORIENTATION_LOCKED))
+ || isFixedOrientationPortrait(activityOrientation)) {
// If a user aspect ratio override should be applied, treat the activity as portrait if
// it has not specified a fix orientation.
- return SCREEN_ORIENTATION_PORTRAIT;
+ return ORIENTATION_PORTRAIT;
}
- return activityOrientation;
+ // If activity orientation is undefined inherit task orientation.
+ return isFixedOrientationLandscape(activityOrientation)
+ ? ORIENTATION_LANDSCAPE : stableBoundsOrientation;
}
/**
@@ -252,7 +261,7 @@
// TODO(b/400617906): Merge duplicate initial bounds calculations to shared class.
@NonNull
private static Size maximizeSizeGivenAspectRatio(
- @ScreenOrientation int orientation,
+ @Configuration.Orientation int orientation,
@NonNull Size targetArea,
float aspectRatio,
int captionHeight
@@ -261,7 +270,7 @@
final int targetWidth = targetArea.getWidth();
final int finalHeight;
final int finalWidth;
- if (isFixedOrientationPortrait(orientation)) {
+ if (orientation == ORIENTATION_PORTRAIT) {
// Portrait activity.
// Calculate required width given ideal height and aspect ratio.
int tempWidth = (int) (targetHeight / aspectRatio);
diff --git a/services/tests/wmtests/src/com/android/server/wm/DesktopAppCompatAspectRatioPolicyTests.java b/services/tests/wmtests/src/com/android/server/wm/DesktopAppCompatAspectRatioPolicyTests.java
index fa7dcc8..81d753b 100644
--- a/services/tests/wmtests/src/com/android/server/wm/DesktopAppCompatAspectRatioPolicyTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/DesktopAppCompatAspectRatioPolicyTests.java
@@ -35,6 +35,7 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
import android.compat.testing.PlatformCompatChangeRule;
import android.content.pm.ActivityInfo;
@@ -413,7 +414,7 @@
void setDesiredAspectRatio(float aspectRatio) {
doReturn(aspectRatio).when(getDesktopAppCompatAspectRatioPolicy())
- .getDesiredAspectRatio(any());
+ .getDesiredAspectRatio(any(), anyBoolean());
}
DesktopAppCompatAspectRatioPolicy getDesktopAppCompatAspectRatioPolicy() {
@@ -422,7 +423,7 @@
float calculateAspectRatio() {
return getDesktopAppCompatAspectRatioPolicy().calculateAspectRatio(
- getTopActivity().getTask());
+ getTopActivity().getTask(), /* hasOrientationMismatch */ true);
}
ActivityRecord getTopActivity() {
diff --git a/services/tests/wmtests/src/com/android/server/wm/DesktopModeLaunchParamsModifierTests.java b/services/tests/wmtests/src/com/android/server/wm/DesktopModeLaunchParamsModifierTests.java
index 00b617e..f587d6e 100644
--- a/services/tests/wmtests/src/com/android/server/wm/DesktopModeLaunchParamsModifierTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/DesktopModeLaunchParamsModifierTests.java
@@ -52,6 +52,7 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@@ -437,7 +438,7 @@
spyOn(activity.mAppCompatController.getDesktopAspectRatioPolicy());
doReturn(LETTERBOX_ASPECT_RATIO).when(activity.mAppCompatController
- .getDesktopAspectRatioPolicy()).calculateAspectRatio(any());
+ .getDesktopAspectRatioPolicy()).calculateAspectRatio(any(), anyBoolean());
final int desiredWidth =
(int) ((LANDSCAPE_DISPLAY_BOUNDS.height() / LETTERBOX_ASPECT_RATIO) + 0.5f);
@@ -933,7 +934,7 @@
spyOn(activity.mAppCompatController.getDesktopAspectRatioPolicy());
doReturn(LETTERBOX_ASPECT_RATIO).when(activity.mAppCompatController
- .getDesktopAspectRatioPolicy()).calculateAspectRatio(any());
+ .getDesktopAspectRatioPolicy()).calculateAspectRatio(any(), anyBoolean());
final int desiredHeight =
(int) (LANDSCAPE_DISPLAY_BOUNDS.height() * DESKTOP_MODE_INITIAL_BOUNDS_SCALE);
@@ -1060,7 +1061,7 @@
spyOn(activity.mAppCompatController.getDesktopAspectRatioPolicy());
doReturn(LETTERBOX_ASPECT_RATIO).when(activity.mAppCompatController
- .getDesktopAspectRatioPolicy()).calculateAspectRatio(any());
+ .getDesktopAspectRatioPolicy()).calculateAspectRatio(any(), anyBoolean());
final int desiredWidth = PORTRAIT_DISPLAY_BOUNDS.width()
- (DESKTOP_MODE_LANDSCAPE_APP_PADDING * 2);
@@ -1115,7 +1116,7 @@
spyOn(activity.mAppCompatController.getDesktopAspectRatioPolicy());
doReturn(LETTERBOX_ASPECT_RATIO).when(activity.mAppCompatController
- .getDesktopAspectRatioPolicy()).calculateAspectRatio(any());
+ .getDesktopAspectRatioPolicy()).calculateAspectRatio(any(), anyBoolean());
final int desiredWidth = PORTRAIT_DISPLAY_BOUNDS.width()
- (DESKTOP_MODE_LANDSCAPE_APP_PADDING * 2);
@@ -1569,7 +1570,7 @@
activity.mAppCompatController.getDesktopAspectRatioPolicy();
spyOn(desktopAppCompatAspectRatioPolicy);
doReturn(aspectRatio).when(desktopAppCompatAspectRatioPolicy)
- .getDesiredAspectRatio(any());
+ .getDesiredAspectRatio(any(), anyBoolean());
}
private void applyUserMinAspectRatioOverride(ActivityRecord activity, int overrideCode,
@@ -1579,7 +1580,7 @@
activity.mAppCompatController.getDesktopAspectRatioPolicy();
spyOn(desktopAppCompatAspectRatioPolicy);
doReturn(1f).when(desktopAppCompatAspectRatioPolicy)
- .getDesiredAspectRatio(any());
+ .getDesiredAspectRatio(any(), anyBoolean());
// Enable user aspect ratio settings
final AppCompatConfiguration appCompatConfiguration =