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

Commit 780b82dc authored by Kazuki Takise's avatar Kazuki Takise
Browse files

Move bounds adjustment logic to LaunchParamsUtil

Bug: 185427982
Test: atest WmTests:TaskLaunchParamsModifierTests
Change-Id: Ie3fd0c39f43dceff4f33edb2bd5b98e782b22675
parent b3968b81
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.annotation.NonNull;
import android.content.pm.ActivityInfo;
import android.graphics.Rect;
import android.util.Size;
import android.view.View;

/**
 * The static class that defines some utility constants and functions that are shared among launch
@@ -43,6 +44,8 @@ class LaunchParamsUtil {
    private static final int DEFAULT_LANDSCAPE_FREEFORM_WIDTH_DP = 1064;
    private static final int DEFAULT_LANDSCAPE_FREEFORM_HEIGHT_DP = 600;

    private static final int DISPLAY_EDGE_OFFSET_DP = 27;

    private LaunchParamsUtil() {}

    /**
@@ -126,4 +129,44 @@ class LaunchParamsUtil {

        return new Size(adjWidth, adjHeight);
    }

    static void adjustBoundsToFitInDisplayArea(@NonNull Rect stableBounds, int layoutDirection,
                                               @NonNull ActivityInfo.WindowLayout layout,
                                               @NonNull Rect inOutBounds) {
        if (stableBounds.width() < inOutBounds.width()
                || stableBounds.height() < inOutBounds.height()) {
            // There is no way for us to fit the bounds in the displayArea without changing width
            // or height. Just move the start to align with the displayArea.
            final int left = layoutDirection == View.LAYOUT_DIRECTION_RTL
                    ? stableBounds.right - inOutBounds.right + inOutBounds.left
                    : stableBounds.left;
            inOutBounds.offsetTo(left, stableBounds.top);
            return;
        }

        final int dx;
        if (inOutBounds.right > stableBounds.right) {
            // Right edge is out of displayArea.
            dx = stableBounds.right - inOutBounds.right;
        } else if (inOutBounds.left < stableBounds.left) {
            // Left edge is out of displayArea.
            dx = stableBounds.left - inOutBounds.left;
        } else {
            // Vertical edges are all in displayArea.
            dx = 0;
        }

        final int dy;
        if (inOutBounds.top < stableBounds.top) {
            // Top edge is out of displayArea.
            dy = stableBounds.top - inOutBounds.top;
        } else if (inOutBounds.bottom > stableBounds.bottom) {
            // Bottom edge is out of displayArea.
            dy = stableBounds.bottom - inOutBounds.bottom;
        } else {
            // Horizontal edges are all in displayArea.
            dy = 0;
        }
        inOutBounds.offset(dx, dy);
    }
}
+9 −44
Original line number Diff line number Diff line
@@ -50,7 +50,6 @@ import android.graphics.Rect;
import android.util.Size;
import android.util.Slog;
import android.view.Gravity;
import android.view.View;
import android.window.WindowContainerToken;

import com.android.internal.annotations.VisibleForTesting;
@@ -370,7 +369,7 @@ class TaskLaunchParamsModifier implements LaunchParamsModifier {
            if (resolvedMode == WINDOWING_MODE_FREEFORM) {
                // Make sure bounds are in the displayArea.
                if (currentParams.mPreferredTaskDisplayArea != taskDisplayArea) {
                    adjustBoundsToFitInDisplayArea(taskDisplayArea, outParams.mBounds);
                    adjustBoundsToFitInDisplayArea(taskDisplayArea, layout, outParams.mBounds);
                }
                // Even though we want to keep original bounds, we still don't want it to stomp on
                // an existing task.
@@ -771,7 +770,7 @@ class TaskLaunchParamsModifier implements LaunchParamsModifier {
            // we may need to move it back to the displayArea.
            LaunchParamsUtil.centerBounds(displayArea, mTmpBounds.width(), mTmpBounds.height(),
                    inOutBounds);
            adjustBoundsToFitInDisplayArea(displayArea, inOutBounds);
            adjustBoundsToFitInDisplayArea(displayArea, layout, inOutBounds);
            if (DEBUG) appendLog("freeform-size-mismatch=" + inOutBounds);
        }

@@ -818,47 +817,13 @@ class TaskLaunchParamsModifier implements LaunchParamsModifier {
    }

    private void adjustBoundsToFitInDisplayArea(@NonNull TaskDisplayArea displayArea,
                                                @NonNull ActivityInfo.WindowLayout layout,
                                                @NonNull Rect inOutBounds) {
        final Rect stableBounds = mTmpStableBounds;
        displayArea.getStableRect(stableBounds);

        if (stableBounds.width() < inOutBounds.width()
                || stableBounds.height() < inOutBounds.height()) {
            // There is no way for us to fit the bounds in the displayArea without changing width
            // or height. Just move the start to align with the displayArea.
            final int layoutDirection =
                    mSupervisor.mRootWindowContainer.getConfiguration().getLayoutDirection();
            final int left = layoutDirection == View.LAYOUT_DIRECTION_RTL
                    ? stableBounds.right - inOutBounds.right + inOutBounds.left
                    : stableBounds.left;
            inOutBounds.offsetTo(left, stableBounds.top);
            return;
        }

        final int dx;
        if (inOutBounds.right > stableBounds.right) {
            // Right edge is out of displayArea.
            dx = stableBounds.right - inOutBounds.right;
        } else if (inOutBounds.left < stableBounds.left) {
            // Left edge is out of displayArea.
            dx = stableBounds.left - inOutBounds.left;
        } else {
            // Vertical edges are all in displayArea.
            dx = 0;
        }

        final int dy;
        if (inOutBounds.top < stableBounds.top) {
            // Top edge is out of displayArea.
            dy = stableBounds.top - inOutBounds.top;
        } else if (inOutBounds.bottom > stableBounds.bottom) {
            // Bottom edge is out of displayArea.
            dy = stableBounds.bottom - inOutBounds.bottom;
        } else {
            // Horizontal edges are all in displayArea.
            dy = 0;
        }
        inOutBounds.offset(dx, dy);
        final int layoutDirection = mSupervisor.mRootWindowContainer.getConfiguration()
                .getLayoutDirection();
        displayArea.getStableRect(mTmpStableBounds);
        LaunchParamsUtil.adjustBoundsToFitInDisplayArea(mTmpStableBounds, layoutDirection, layout,
                inOutBounds);
    }

    /**