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

Commit 6b9edc42 authored by Eghosa Ewansiha-Vlachavas's avatar Eghosa Ewansiha-Vlachavas
Browse files

[2/n] Preserve exisitng orienation and aspect ratio from recents tasks relaunch

If task from recents with a running activity is being relaunched,
preserve the aspect ratio and orientation of the top activity to prevent
letterboxing.

Flag: com.android.window.flags.preserve_recents_task_configuration_on_relaunch
Test: atest WmTests:DesktopModeLaunchParamsModifierTests
Fixes: 405141594
Change-Id: I27a3710d9a49db3141fec274be91e34c1f715eb7
parent d5866cb5
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
package com.android.server.wm;

import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
import static android.content.res.Configuration.UI_MODE_TYPE_MASK;
import static android.content.res.Configuration.UI_MODE_TYPE_VR_HEADSET;

@@ -81,6 +83,14 @@ final class AppCompatUtils {
        return Math.max(width, height) / (float) Math.min(width, height);
    }

    /**
     * Returns the orientation of the given {@code rect}.
     */
    static @Configuration.Orientation int computeConfigOrientation(@NonNull Rect rect) {
        return rect.height() >= rect.width()
                ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
    }

    /**
     * @param config The current {@link Configuration}
     * @return {@code true} if using a VR headset.
+10 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.annotation.NonNull;
import android.app.WindowConfiguration;
import android.content.pm.ActivityInfo;
import android.graphics.Rect;
import android.window.DesktopModeFlags;

import com.android.internal.R;
import com.android.internal.annotations.VisibleForTesting;
@@ -70,6 +71,15 @@ public class DesktopAppCompatAspectRatioPolicy {
     * launched in. Takes into account any min or max aspect ratio constraints.
     */
    float calculateAspectRatio(@NonNull Task task, boolean hasOrientationMismatch) {
        if (DesktopModeFlags.PRESERVE_RECENTS_TASK_CONFIGURATION_ON_RELAUNCH.isTrue()
                && task.inRecents && task.topRunningActivity() != null) {
            // If task in resents with running activity, inherit existing activity aspect ratio.
            final WindowConfiguration windowConfiguration =
                    task.topRunningActivity().getWindowConfiguration();
            final Rect existingBounds = windowConfiguration.getAppBounds() != null
                    ? windowConfiguration.getAppBounds() : windowConfiguration.getBounds();
            return AppCompatUtils.computeAspectRatio(existingBounds);
        }
        final float maxAspectRatio = getMaxAspectRatio();
        final float minAspectRatio = getMinAspectRatio(task);
        float desiredAspectRatio = 0;
+11 −2
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import static com.android.server.wm.LaunchParamsUtil.calculateLayoutBounds;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ActivityOptions;
import android.app.WindowConfiguration;
import android.content.pm.ActivityInfo.WindowLayout;
import android.content.res.Configuration;
import android.graphics.Rect;
@@ -151,8 +152,7 @@ public final class DesktopModeBoundsCalculator {
        }
        final DesktopAppCompatAspectRatioPolicy desktopAppCompatAspectRatioPolicy =
                activity.mAppCompatController.getDesktopAspectRatioPolicy();
        final int stableBoundsOrientation = stableBounds.height() >= stableBounds.width()
                ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
        final int stableBoundsOrientation = AppCompatUtils.computeConfigOrientation(stableBounds);
        int activityOrientation = getActivityConfigurationOrientation(
                activity, task, stableBoundsOrientation);
        // Use orientation mismatch to resolve aspect ratio to match fixed orientation letterboxing
@@ -238,6 +238,15 @@ public final class DesktopModeBoundsCalculator {
    private static @Configuration.Orientation int getActivityConfigurationOrientation(
            @NonNull ActivityRecord activity, @NonNull Task task,
            @Configuration.Orientation int stableBoundsOrientation) {
        if (DesktopModeFlags.PRESERVE_RECENTS_TASK_CONFIGURATION_ON_RELAUNCH.isTrue()
                && task.inRecents && task.topRunningActivity() != null) {
            // If task in resents with running activity, inherit existing activity orientation.
            final WindowConfiguration windowConfiguration =
                    task.topRunningActivity().getWindowConfiguration();
            final Rect existingBounds = windowConfiguration.getAppBounds() != null
                    ? windowConfiguration.getAppBounds() : windowConfiguration.getBounds();
            return AppCompatUtils.computeConfigOrientation(existingBounds);
        }
        final int activityOrientation = activity.getOverrideOrientation();
        final DesktopAppCompatAspectRatioPolicy desktopAppCompatAspectRatioPolicy =
                activity.mAppCompatController.getDesktopAspectRatioPolicy();
+28 −0
Original line number Diff line number Diff line
@@ -270,6 +270,34 @@ public class DesktopModeLaunchParamsModifierTests extends
        mCurrent.mBounds.set(/* left */ 0, /* top */ 0, /* right */ 100, /* bottom */ 100);
        assertEquals(RESULT_SKIP, new CalculateRequestBuilder().setTask(task).calculate());
    }
    @Test
    @EnableFlags({Flags.FLAG_ENABLE_DESKTOP_WINDOWING_MODE,
            Flags.FLAG_PRESERVE_RECENTS_TASK_CONFIGURATION_ON_RELAUNCH})
    public void testPreserveOrientationAndAspectRatioFromRecentsTaskRelaunch() {
        setupDesktopModeLaunchParamsModifier();
        final int captionHeight = getDesktopViewAppHeaderHeightPx(mContext);
        final float fullscreenAspectRatio =
                AppCompatUtils.computeAspectRatio(PORTRAIT_DISPLAY_BOUNDS);
        final int desiredHeight =
                (int) (LANDSCAPE_DISPLAY_BOUNDS.height() * DESKTOP_MODE_INITIAL_BOUNDS_SCALE);
        final float expectedAspectRatio = desiredHeight
                / ((desiredHeight - captionHeight) / fullscreenAspectRatio);

        final TestDisplayContent display = createDisplayContent(ORIENTATION_LANDSCAPE,
                LANDSCAPE_DISPLAY_BOUNDS);
        final Task task = createTask(display,  /* isResizeable */ false);
        final ActivityRecord activity = createActivity(display, SCREEN_ORIENTATION_UNSPECIFIED,
                task, /* ignoreOrientationRequest */ true);
        activity.getWindowConfiguration().setAppBounds(PORTRAIT_DISPLAY_BOUNDS);
        task.inRecents = true;

        assertEquals(RESULT_CONTINUE, new CalculateRequestBuilder().setTask(task)
                .setActivity(activity).calculate());
        assertEquals(ORIENTATION_PORTRAIT,
                AppCompatUtils.computeConfigOrientation(mResult.mBounds));
        assertEquals(expectedAspectRatio,
                AppCompatUtils.computeAspectRatio(mResult.mBounds), /* delta */ 0.05);
    }

    @Test
    @EnableFlags({Flags.FLAG_ENABLE_DESKTOP_WINDOWING_MODE,