Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 727909a7 authored by Jorge Gil's avatar Jorge Gil Committed by Automerger Merge Worker
Browse files

Merge "Respect min/max aspect ratio in freeform launch bounds" into sc-v2-dev am: 05dde516

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15517012

Change-Id: I332046b73df14718c542dda61550b066f9902702
parents 643ea04e 05dde516
Loading
Loading
Loading
Loading
+32 −4
Original line number Original line Diff line number Diff line
@@ -717,7 +717,7 @@ class TaskLaunchParamsModifier implements LaunchParamsModifier {
        }
        }


        // First we get the default size we want.
        // First we get the default size we want.
        getDefaultFreeformSize(displayArea, layout, orientation, mTmpBounds);
        getDefaultFreeformSize(root.info, displayArea, layout, orientation, mTmpBounds);
        if (hasInitialBounds || sizeMatches(inOutBounds, mTmpBounds)) {
        if (hasInitialBounds || sizeMatches(inOutBounds, mTmpBounds)) {
            // We're here because either input parameters specified initial bounds, or the suggested
            // We're here because either input parameters specified initial bounds, or the suggested
            // bounds have the same size of the default freeform size. We should use the suggested
            // bounds have the same size of the default freeform size. We should use the suggested
@@ -785,7 +785,8 @@ class TaskLaunchParamsModifier implements LaunchParamsModifier {
        return orientation;
        return orientation;
    }
    }


    private void getDefaultFreeformSize(@NonNull TaskDisplayArea displayArea,
    private void getDefaultFreeformSize(@NonNull ActivityInfo info,
            @NonNull TaskDisplayArea displayArea,
            @NonNull ActivityInfo.WindowLayout layout, int orientation, @NonNull Rect bounds) {
            @NonNull ActivityInfo.WindowLayout layout, int orientation, @NonNull Rect bounds) {
        // Default size, which is letterboxing/pillarboxing in displayArea. That's to say the large
        // Default size, which is letterboxing/pillarboxing in displayArea. That's to say the large
        // dimension of default size is the small dimension of displayArea size, and the small
        // dimension of default size is the small dimension of displayArea size, and the small
@@ -816,11 +817,38 @@ class TaskLaunchParamsModifier implements LaunchParamsModifier {
        final int layoutMinWidth = (layout == null) ? -1 : layout.minWidth;
        final int layoutMinWidth = (layout == null) ? -1 : layout.minWidth;
        final int layoutMinHeight = (layout == null) ? -1 : layout.minHeight;
        final int layoutMinHeight = (layout == null) ? -1 : layout.minHeight;


        // Final result.
        // Aspect ratio requirements.
        final float minAspectRatio = info.getMinAspectRatio();
        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));
        final int height = Math.min(defaultHeight, Math.max(phoneHeight, layoutMinHeight));
        final int height = Math.min(defaultHeight, Math.max(phoneHeight, layoutMinHeight));
        final float aspectRatio = (float) Math.max(width, height) / (float) Math.min(width, height);

        // Adjust the width and height to the aspect ratio requirements.
        int adjWidth = width;
        int adjHeight = height;
        if (minAspectRatio >= 1 && aspectRatio < minAspectRatio) {
            // The aspect ratio is below the minimum, adjust it to the minimum.
            if (orientation == SCREEN_ORIENTATION_LANDSCAPE) {
                // Fix the width, scale the height.
                adjHeight = (int) (adjWidth / minAspectRatio + 0.5f);
            } else {
                // Fix the height, scale the width.
                adjWidth = (int) (adjHeight / minAspectRatio + 0.5f);
            }
        } else if (maxAspectRatio >= 1 && aspectRatio > maxAspectRatio) {
            // The aspect ratio exceeds the maximum, adjust it to the maximum.
            if (orientation == SCREEN_ORIENTATION_LANDSCAPE) {
                // Fix the width, scale the height.
                adjHeight = (int) (adjWidth / maxAspectRatio + 0.5f);
            } else {
                // Fix the height, scale the width.
                adjWidth = (int) (adjHeight / maxAspectRatio + 0.5f);
            }
        }


        bounds.set(0, 0, width, height);
        bounds.set(0, 0, adjWidth, adjHeight);
        bounds.offset(stableBounds.left, stableBounds.top);
        bounds.offset(stableBounds.left, stableBounds.top);
    }
    }


+110 −0
Original line number Original line Diff line number Diff line
@@ -1320,6 +1320,50 @@ public class TaskLaunchParamsModifierTests extends WindowTestsBase {
                DISPLAY_STABLE_BOUNDS.bottom - mResult.mBounds.bottom, /* delta */ 1);
                DISPLAY_STABLE_BOUNDS.bottom - mResult.mBounds.bottom, /* delta */ 1);
    }
    }


    @Test
    public void testDefaultFreeformSizeRespectsMinAspectRatio() {
        final TestDisplayContent freeformDisplay = createNewDisplayContent(
                WINDOWING_MODE_FREEFORM);

        final ActivityOptions options = ActivityOptions.makeBasic();
        options.setLaunchDisplayId(freeformDisplay.mDisplayId);

        mActivity.info.applicationInfo.targetSdkVersion = Build.VERSION_CODES.LOLLIPOP;
        mActivity.info.setMinAspectRatio(5f);

        assertEquals(RESULT_CONTINUE,
                new CalculateRequestBuilder()
                        .setOptions(options).calculate());

        final float aspectRatio =
                (float) Math.max(mResult.mBounds.width(), mResult.mBounds.height())
                        / (float) Math.min(mResult.mBounds.width(), mResult.mBounds.height());
        assertTrue("Bounds aspect ratio should be at least 5.0, but was " + aspectRatio,
                aspectRatio >= 5f);
    }

    @Test
    public void testDefaultFreeformSizeRespectsMaxAspectRatio() {
        final TestDisplayContent freeformDisplay = createNewDisplayContent(
                WINDOWING_MODE_FREEFORM);

        final ActivityOptions options = ActivityOptions.makeBasic();
        options.setLaunchDisplayId(freeformDisplay.mDisplayId);

        mActivity.info.applicationInfo.targetSdkVersion = Build.VERSION_CODES.LOLLIPOP;
        mActivity.info.setMaxAspectRatio(1.5f);

        assertEquals(RESULT_CONTINUE,
                new CalculateRequestBuilder()
                        .setOptions(options).calculate());

        final float aspectRatio =
                (float) Math.max(mResult.mBounds.width(), mResult.mBounds.height())
                        / (float) Math.min(mResult.mBounds.width(), mResult.mBounds.height());
        assertTrue("Bounds aspect ratio should be at most 1.5, but was " + aspectRatio,
                aspectRatio <= 1.5f);
    }

    @Test
    @Test
    public void testCascadesToSourceSizeForFreeform() {
    public void testCascadesToSourceSizeForFreeform() {
        final TestDisplayContent freeformDisplay = createNewDisplayContent(
        final TestDisplayContent freeformDisplay = createNewDisplayContent(
@@ -1347,6 +1391,72 @@ public class TaskLaunchParamsModifierTests extends WindowTestsBase {
                mResult.mBounds.centerY() < displayBounds.centerY());
                mResult.mBounds.centerY() < displayBounds.centerY());
    }
    }


    @Test
    public void testCascadesToSourceSizeForFreeformRespectingMinAspectRatio() {
        final TestDisplayContent freeformDisplay = createNewDisplayContent(
                WINDOWING_MODE_FREEFORM);

        final ActivityOptions options = ActivityOptions.makeBasic();
        options.setLaunchDisplayId(freeformDisplay.mDisplayId);

        final ActivityRecord source = createSourceActivity(freeformDisplay);
        source.setBounds(0, 0, 412, 732);

        mActivity.info.applicationInfo.targetSdkVersion = Build.VERSION_CODES.LOLLIPOP;
        mActivity.info.setMinAspectRatio(5f);

        assertEquals(RESULT_CONTINUE,
                new CalculateRequestBuilder().setSource(source).setOptions(options).calculate());

        final Rect displayBounds = freeformDisplay.getBounds();
        assertTrue("Left bounds should be larger than 0.", mResult.mBounds.left > 0);
        assertTrue("Top bounds should be larger than 0.", mResult.mBounds.top > 0);
        assertTrue("Bounds should be centered at somewhere in the left half, but it's "
                        + "centerX is " + mResult.mBounds.centerX(),
                mResult.mBounds.centerX() < displayBounds.centerX());
        assertTrue("Bounds should be centered at somewhere in the top half, but it's "
                        + "centerY is " + mResult.mBounds.centerY(),
                mResult.mBounds.centerY() < displayBounds.centerY());
        final float aspectRatio =
                (float) Math.max(mResult.mBounds.width(), mResult.mBounds.height())
                        / (float) Math.min(mResult.mBounds.width(), mResult.mBounds.height());
        assertTrue("Bounds aspect ratio should be at least 5.0, but was " + aspectRatio,
                aspectRatio >= 5f);
    }

    @Test
    public void testCascadesToSourceSizeForFreeformRespectingMaxAspectRatio() {
        final TestDisplayContent freeformDisplay = createNewDisplayContent(
                WINDOWING_MODE_FREEFORM);

        final ActivityOptions options = ActivityOptions.makeBasic();
        options.setLaunchDisplayId(freeformDisplay.mDisplayId);

        final ActivityRecord source = createSourceActivity(freeformDisplay);
        source.setBounds(0, 0, 412, 732);

        mActivity.info.applicationInfo.targetSdkVersion = Build.VERSION_CODES.LOLLIPOP;
        mActivity.info.setMaxAspectRatio(1.5f);

        assertEquals(RESULT_CONTINUE,
                new CalculateRequestBuilder().setSource(source).setOptions(options).calculate());

        final Rect displayBounds = freeformDisplay.getBounds();
        assertTrue("Left bounds should be larger than 0.", mResult.mBounds.left > 0);
        assertTrue("Top bounds should be larger than 0.", mResult.mBounds.top > 0);
        assertTrue("Bounds should be centered at somewhere in the left half, but it's "
                        + "centerX is " + mResult.mBounds.centerX(),
                mResult.mBounds.centerX() < displayBounds.centerX());
        assertTrue("Bounds should be centered at somewhere in the top half, but it's "
                        + "centerY is " + mResult.mBounds.centerY(),
                mResult.mBounds.centerY() < displayBounds.centerY());
        final float aspectRatio =
                (float) Math.max(mResult.mBounds.width(), mResult.mBounds.height())
                        / (float) Math.min(mResult.mBounds.width(), mResult.mBounds.height());
        assertTrue("Bounds aspect ratio should be at most 1.5, but was " + aspectRatio,
                aspectRatio <= 1.5f);
    }

    @Test
    @Test
    public void testAdjustBoundsToFitDisplay_TopLeftOutOfDisplay() {
    public void testAdjustBoundsToFitDisplay_TopLeftOutOfDisplay() {
        final TestDisplayContent freeformDisplay = createNewDisplayContent(
        final TestDisplayContent freeformDisplay = createNewDisplayContent(