Merge "Apply min aspect ratio override only for portrait only activities" into sc-v2-dev

This commit is contained in:
Tom Natan
2021-10-25 09:16:38 +00:00
committed by Android (Google) Code Review
5 changed files with 132 additions and 21 deletions

View File

@@ -994,9 +994,10 @@ public class ActivityInfo extends ComponentInfo implements Parcelable {
* OVERRIDE_MIN_ASPECT_RATIO_MEDIUM * OVERRIDE_MIN_ASPECT_RATIO_MEDIUM
* OVERRIDE_MIN_ASPECT_RATIO_LARGE * OVERRIDE_MIN_ASPECT_RATIO_LARGE
* *
* If OVERRIDE_MIN_ASPECT_RATIO is applied, the min aspect ratio given in the app's * If OVERRIDE_MIN_ASPECT_RATIO is applied, and the activity's orientation is fixed to
* manifest will be overridden to the largest enabled aspect ratio treatment unless the app's * portrait, the min aspect ratio given in the app's manifest will be overridden to the
* manifest value is higher. * largest enabled aspect ratio treatment unless the app's manifest value is higher.
* TODO(b/203647190): add OVERRIDE_MIN_ASPECT_RATIO_PORTRAIT_ONLY instead of portrait by default
* @hide * @hide
*/ */
@ChangeId @ChangeId
@@ -1232,8 +1233,8 @@ public class ActivityInfo extends ComponentInfo implements Parcelable {
* Returns true if the activity has maximum or minimum aspect ratio. * Returns true if the activity has maximum or minimum aspect ratio.
* @hide * @hide
*/ */
public boolean hasFixedAspectRatio() { public boolean hasFixedAspectRatio(@ScreenOrientation int orientation) {
return getMaxAspectRatio() != 0 || getMinAspectRatio() != 0; return getMaxAspectRatio() != 0 || getMinAspectRatio(orientation) != 0;
} }
/** /**
@@ -1392,10 +1393,14 @@ public class ActivityInfo extends ComponentInfo implements Parcelable {
* {@code getManifestMinAspectRatio}. * {@code getManifestMinAspectRatio}.
* @hide * @hide
*/ */
public float getMinAspectRatio() { public float getMinAspectRatio(@ScreenOrientation int orientation) {
// TODO(b/203647190): check orientation only if OVERRIDE_MIN_ASPECT_RATIO_PORTRAIT_ONLY
// In case the activity's orientation isn't fixed to portrait, OVERRIDE_MIN_ASPECT_RATIO
// shouldn't be applied.
if (applicationInfo == null || !CompatChanges.isChangeEnabled(OVERRIDE_MIN_ASPECT_RATIO, if (applicationInfo == null || !CompatChanges.isChangeEnabled(OVERRIDE_MIN_ASPECT_RATIO,
applicationInfo.packageName, applicationInfo.packageName,
UserHandle.getUserHandleForUid(applicationInfo.uid))) { UserHandle.getUserHandleForUid(applicationInfo.uid))
|| !isFixedOrientationPortrait(orientation)) {
return mMinAspectRatio; return mMinAspectRatio;
} }
@@ -1521,9 +1526,10 @@ public class ActivityInfo extends ComponentInfo implements Parcelable {
if (getMaxAspectRatio() != 0) { if (getMaxAspectRatio() != 0) {
pw.println(prefix + "maxAspectRatio=" + getMaxAspectRatio()); pw.println(prefix + "maxAspectRatio=" + getMaxAspectRatio());
} }
if (getMinAspectRatio() != 0) { final float minAspectRatio = getMinAspectRatio(screenOrientation);
pw.println(prefix + "minAspectRatio=" + getMinAspectRatio()); if (minAspectRatio != 0) {
if (getManifestMinAspectRatio() != getMinAspectRatio()) { pw.println(prefix + "minAspectRatio=" + minAspectRatio);
if (getManifestMinAspectRatio() != minAspectRatio) {
pw.println(prefix + "getManifestMinAspectRatio=" + getManifestMinAspectRatio()); pw.println(prefix + "getManifestMinAspectRatio=" + getManifestMinAspectRatio());
} }
} }

View File

@@ -1146,10 +1146,11 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
if (info.getMaxAspectRatio() != 0) { if (info.getMaxAspectRatio() != 0) {
pw.println(prefix + "maxAspectRatio=" + info.getMaxAspectRatio()); pw.println(prefix + "maxAspectRatio=" + info.getMaxAspectRatio());
} }
if (info.getMinAspectRatio() != 0) { final float minAspectRatio = getMinAspectRatio();
pw.println(prefix + "minAspectRatio=" + info.getMinAspectRatio()); if (minAspectRatio != 0) {
pw.println(prefix + "minAspectRatio=" + minAspectRatio);
} }
if (info.getMinAspectRatio() != info.getManifestMinAspectRatio()) { if (minAspectRatio != info.getManifestMinAspectRatio()) {
// Log the fact that we've overridden the min aspect ratio from the manifest // Log the fact that we've overridden the min aspect ratio from the manifest
pw.println(prefix + "manifestMinAspectRatio=" pw.println(prefix + "manifestMinAspectRatio="
+ info.getManifestMinAspectRatio()); + info.getManifestMinAspectRatio());
@@ -7277,7 +7278,7 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
return false; return false;
} }
} }
return !isResizeable() && (info.isFixedOrientation() || info.hasFixedAspectRatio()) return !isResizeable() && (info.isFixedOrientation() || hasFixedAspectRatio())
// The configuration of non-standard type should be enforced by system. // The configuration of non-standard type should be enforced by system.
// {@link WindowConfiguration#ACTIVITY_TYPE_STANDARD} is set when this activity is // {@link WindowConfiguration#ACTIVITY_TYPE_STANDARD} is set when this activity is
// added to a task, but this function is called when resolving the launch params, at // added to a task, but this function is called when resolving the launch params, at
@@ -7948,13 +7949,14 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
return false; return false;
} }
} }
if (info.getMinAspectRatio() > 0) { final float minAspectRatio = getMinAspectRatio();
if (minAspectRatio > 0) {
// The activity should have at least the min aspect ratio, so this checks if the // The activity should have at least the min aspect ratio, so this checks if the
// container still has available space to provide larger aspect ratio. // container still has available space to provide larger aspect ratio.
final float containerAspectRatio = final float containerAspectRatio =
(0.5f + Math.max(containerAppWidth, containerAppHeight)) (0.5f + Math.max(containerAppWidth, containerAppHeight))
/ Math.min(containerAppWidth, containerAppHeight); / Math.min(containerAppWidth, containerAppHeight);
if (containerAspectRatio <= info.getMinAspectRatio()) { if (containerAspectRatio <= minAspectRatio) {
// The long side has reached the parent. // The long side has reached the parent.
return false; return false;
} }
@@ -8165,8 +8167,7 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
Rect containingBounds, float desiredAspectRatio, boolean fixedOrientationLetterboxed) { Rect containingBounds, float desiredAspectRatio, boolean fixedOrientationLetterboxed) {
final float maxAspectRatio = info.getMaxAspectRatio(); final float maxAspectRatio = info.getMaxAspectRatio();
final Task rootTask = getRootTask(); final Task rootTask = getRootTask();
final float minAspectRatio = info.getMinAspectRatio(); final float minAspectRatio = getMinAspectRatio();
if (task == null || rootTask == null if (task == null || rootTask == null
|| (inMultiWindowMode() && !shouldCreateCompatDisplayInsets() || (inMultiWindowMode() && !shouldCreateCompatDisplayInsets()
&& !fixedOrientationLetterboxed) && !fixedOrientationLetterboxed)
@@ -8269,6 +8270,20 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
return true; return true;
} }
/**
* Returns the min aspect ratio of this activity.
*/
private float getMinAspectRatio() {
return info.getMinAspectRatio(getRequestedOrientation());
}
/**
* Returns true if the activity has maximum or minimum aspect ratio.
*/
private boolean hasFixedAspectRatio() {
return info.hasFixedAspectRatio(getRequestedOrientation());
}
/** /**
* Returns the aspect ratio of the given {@code rect}. * Returns the aspect ratio of the given {@code rect}.
*/ */

View File

@@ -818,7 +818,7 @@ class TaskLaunchParamsModifier implements LaunchParamsModifier {
final int layoutMinHeight = (layout == null) ? -1 : layout.minHeight; final int layoutMinHeight = (layout == null) ? -1 : layout.minHeight;
// Aspect ratio requirements. // Aspect ratio requirements.
final float minAspectRatio = info.getMinAspectRatio(); final float minAspectRatio = info.getMinAspectRatio(orientation);
final float maxAspectRatio = info.getMaxAspectRatio(); final float maxAspectRatio = info.getMaxAspectRatio();
final int width = Math.min(defaultWidth, Math.max(phoneWidth, layoutMinWidth)); final int width = Math.min(defaultWidth, Math.max(phoneWidth, layoutMinWidth));

View File

@@ -341,10 +341,8 @@ open class AndroidPackageParsingTestBase {
launchToken=${this.launchToken} launchToken=${this.launchToken}
lockTaskLaunchMode=${this.lockTaskLaunchMode} lockTaskLaunchMode=${this.lockTaskLaunchMode}
logo=${this.logo} logo=${this.logo}
maxAspectRatio=${this.maxAspectRatio}
maxRecents=${this.maxRecents} maxRecents=${this.maxRecents}
metaData=${this.metaData.dumpToString()} metaData=${this.metaData.dumpToString()}
minAspectRatio=${this.minAspectRatio}
name=${this.name} name=${this.name}
nonLocalizedLabel=${ nonLocalizedLabel=${
// Per b/184574333, v1 mistakenly trimmed the label. v2 fixed this, but for test // Per b/184574333, v1 mistakenly trimmed the label. v2 fixed this, but for test

View File

@@ -1122,6 +1122,98 @@ public class SizeCompatTests extends WindowTestsBase {
activity.getBounds().width(), 0.5); activity.getBounds().width(), 0.5);
} }
@Test
@EnableCompatChanges({ActivityInfo.OVERRIDE_MIN_ASPECT_RATIO,
ActivityInfo.OVERRIDE_MIN_ASPECT_RATIO_MEDIUM})
public void testOverrideMinAspectRatioScreenOrientationNotSetThenChangedToPortrait() {
// In this test, the activity's orientation isn't fixed to portrait, therefore the override
// isn't applied.
setUpDisplaySizeWithApp(1000, 1200);
// Create a size compat activity on the same task.
final ActivityRecord activity = new ActivityBuilder(mAtm)
.setTask(mTask)
.setComponent(ComponentName.createRelative(mContext,
SizeCompatTests.class.getName()))
.setUid(android.os.Process.myUid())
.build();
// The per-package override should have no effect
assertEquals(1200, activity.getBounds().height());
assertEquals(1000, activity.getBounds().width());
// After changing the orientation to portrait the override should be applied.
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
activity.clearSizeCompatMode();
// The per-package override forces the activity into a 3:2 aspect ratio
assertEquals(1200, activity.getBounds().height());
assertEquals(1200 / ActivityInfo.OVERRIDE_MIN_ASPECT_RATIO_MEDIUM_VALUE,
activity.getBounds().width(), 0.5);
}
@Test
@EnableCompatChanges({ActivityInfo.OVERRIDE_MIN_ASPECT_RATIO,
ActivityInfo.OVERRIDE_MIN_ASPECT_RATIO_MEDIUM})
public void testOverrideMinAspectRatioScreenOrientationLandscapeThenChangedToPortrait() {
// In this test, the activity's orientation isn't fixed to portrait, therefore the override
// isn't applied.
setUpDisplaySizeWithApp(1000, 1200);
// Create a size compat activity on the same task.
final ActivityRecord activity = new ActivityBuilder(mAtm)
.setTask(mTask)
.setScreenOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
.setComponent(ComponentName.createRelative(mContext,
SizeCompatTests.class.getName()))
.setUid(android.os.Process.myUid())
.build();
// The per-package override should have no effect
assertEquals(1200, activity.getBounds().height());
assertEquals(1000, activity.getBounds().width());
// After changing the orientation to portrait the override should be applied.
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
activity.clearSizeCompatMode();
// The per-package override forces the activity into a 3:2 aspect ratio
assertEquals(1200, activity.getBounds().height());
assertEquals(1200 / ActivityInfo.OVERRIDE_MIN_ASPECT_RATIO_MEDIUM_VALUE,
activity.getBounds().width(), 0.5);
}
@Test
@EnableCompatChanges({ActivityInfo.OVERRIDE_MIN_ASPECT_RATIO,
ActivityInfo.OVERRIDE_MIN_ASPECT_RATIO_MEDIUM})
public void testOverrideMinAspectRatioScreenOrientationPortraitThenChangedToUnspecified() {
setUpDisplaySizeWithApp(1000, 1200);
// Create a size compat activity on the same task.
final ActivityRecord activity = new ActivityBuilder(mAtm)
.setTask(mTask)
.setScreenOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
.setComponent(ComponentName.createRelative(mContext,
SizeCompatTests.class.getName()))
.setUid(android.os.Process.myUid())
.build();
// The per-package override forces the activity into a 3:2 aspect ratio
assertEquals(1200, activity.getBounds().height());
assertEquals(1200 / ActivityInfo.OVERRIDE_MIN_ASPECT_RATIO_MEDIUM_VALUE,
activity.getBounds().width(), 0.5);
// After changing the orientation to landscape the override shouldn't be applied.
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
activity.clearSizeCompatMode();
// The per-package override should have no effect
assertEquals(1200, activity.getBounds().height());
assertEquals(1000, activity.getBounds().width());
}
@Test @Test
@EnableCompatChanges({ActivityInfo.OVERRIDE_MIN_ASPECT_RATIO_MEDIUM}) @EnableCompatChanges({ActivityInfo.OVERRIDE_MIN_ASPECT_RATIO_MEDIUM})
public void testOverrideMinAspectRatioWithoutGlobalOverride() { public void testOverrideMinAspectRatioWithoutGlobalOverride() {