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

Commit 4421532a authored by Eghosa Ewansiha-Vlachavas's avatar Eghosa Ewansiha-Vlachavas Committed by Automerger Merge Worker
Browse files

Merge "[1/n] Optimize user aspect ratio button heuristic" into udc-qpr-dev am:...

Merge "[1/n] Optimize user aspect ratio button heuristic" into udc-qpr-dev am: 4b539568 am: 88c32769

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



Change-Id: I746ed93a7dcf8836eaa6f486cbb2a50a0bd35f42
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 3ef3a7da 88c32769
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -583,6 +583,7 @@ public class TaskInfo {
                    == that.configuration.getLayoutDirection())
                && (!hasCompatUI() || configuration.uiMode == that.configuration.uiMode)
                && (!hasCompatUI() || isVisible == that.isVisible)
                && isFocused == that.isFocused
                && isUserFullscreenOverrideEnabled == that.isUserFullscreenOverrideEnabled;
    }

+58 −1
Original line number Diff line number Diff line
@@ -176,6 +176,18 @@ public class CompatUIController implements OnDisplaysChangedListener,
    // be shown.
    private boolean mKeyguardShowing;

    /**
     * The id of the task for the application we're currently attempting to show the user aspect
     * ratio settings button for, or have most recently shown the button for.
     */
    private int mTopActivityTaskId;

    /**
     * Whether the user aspect ratio settings button has been shown for the current application
     * associated with the task id stored in {@link CompatUIController#mTopActivityTaskId}.
     */
    private boolean mHasShownUserAspectRatioSettingsButton = false;

    public CompatUIController(@NonNull Context context,
            @NonNull ShellInit shellInit,
            @NonNull ShellController shellController,
@@ -230,6 +242,11 @@ public class CompatUIController implements OnDisplaysChangedListener,
        if (taskInfo != null && !taskInfo.topActivityInSizeCompat) {
            mSetOfTaskIdsShowingRestartDialog.remove(taskInfo.taskId);
        }

        if (taskInfo != null && taskListener != null) {
            updateActiveTaskInfo(taskInfo);
        }

        if (taskInfo.configuration == null || taskListener == null) {
            // Null token means the current foreground activity is not in compatibility mode.
            removeLayouts(taskInfo.taskId);
@@ -322,6 +339,45 @@ public class CompatUIController implements OnDisplaysChangedListener,
        forAllLayouts(layout -> layout.updateVisibility(showOnDisplay(layout.getDisplayId())));
    }

    /**
     * Invoked when a new task is created or the info of an existing task has changed. Updates the
     * shown status of the user aspect ratio settings button and the task id it relates to.
     */
    void updateActiveTaskInfo(@NonNull TaskInfo taskInfo) {
        // If the activity belongs to the task we are currently tracking, don't update any variables
        // as they are still relevant. Else, if the activity is visible and focused (the one the
        // user can see and is using), the user aspect ratio button can potentially be displayed so
        // start tracking the buttons visibility for this task.
        if (mTopActivityTaskId != taskInfo.taskId && taskInfo.isVisible && taskInfo.isFocused) {
            mTopActivityTaskId = taskInfo.taskId;
            setHasShownUserAspectRatioSettingsButton(false);
        }
    }

    /**
     * Informs the system that the user aspect ratio button has been displayed for the application
     * associated with the task id in {@link CompatUIController#mTopActivityTaskId}.
     */
    void setHasShownUserAspectRatioSettingsButton(boolean state) {
        mHasShownUserAspectRatioSettingsButton = state;
    }

    /**
     * Returns whether the user aspect ratio settings button has been show for the application
     * associated with the task id in {@link CompatUIController#mTopActivityTaskId}.
     */
    boolean hasShownUserAspectRatioSettingsButton() {
        return mHasShownUserAspectRatioSettingsButton;
    }

    /**
     * Returns the task id of the application we are currently attempting to show, of have most
     * recently shown, the user aspect ratio settings button for.
     */
    int getTopActivityTaskId() {
        return mTopActivityTaskId;
    }

    private boolean showOnDisplay(int displayId) {
        return !mKeyguardShowing && !isImeShowingOnDisplay(displayId);
    }
@@ -572,7 +628,8 @@ public class CompatUIController implements OnDisplaysChangedListener,
        return new UserAspectRatioSettingsWindowManager(context, taskInfo, mSyncQueue,
                taskListener, mDisplayController.getDisplayLayout(taskInfo.displayId),
                mCompatUIHintsState, this::launchUserAspectRatioSettings, mMainExecutor,
                mDisappearTimeSupplier);
                mDisappearTimeSupplier, this::hasShownUserAspectRatioSettingsButton,
                this::setHasShownUserAspectRatioSettingsButton);
    }

    private void launchUserAspectRatioSettings(
+16 −2
Original line number Diff line number Diff line
@@ -37,7 +37,9 @@ import com.android.wm.shell.common.SyncTransactionQueue;
import com.android.wm.shell.compatui.CompatUIController.CompatUIHintsState;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * Window manager for the user aspect ratio settings button which allows users to go to
@@ -55,6 +57,12 @@ class UserAspectRatioSettingsWindowManager extends CompatUIWindowManagerAbstract

    private final ShellExecutor mShellExecutor;

    @NonNull
    private final Supplier<Boolean> mUserAspectRatioButtonShownChecker;

    @NonNull
    private final Consumer<Boolean> mUserAspectRatioButtonStateConsumer;

    @VisibleForTesting
    @NonNull
    final CompatUIHintsState mCompatUIHintsState;
@@ -72,9 +80,13 @@ class UserAspectRatioSettingsWindowManager extends CompatUIWindowManagerAbstract
            @NonNull DisplayLayout displayLayout, @NonNull CompatUIHintsState compatUIHintsState,
            @NonNull BiConsumer<TaskInfo, ShellTaskOrganizer.TaskListener> onButtonClicked,
            @NonNull ShellExecutor shellExecutor,
            @NonNull Function<Integer, Integer> disappearTimeSupplier) {
            @NonNull Function<Integer, Integer> disappearTimeSupplier,
            @NonNull Supplier<Boolean> userAspectRatioButtonStateChecker,
            @NonNull Consumer<Boolean> userAspectRatioButtonShownConsumer) {
        super(context, taskInfo, syncQueue, taskListener, displayLayout);
        mShellExecutor = shellExecutor;
        mUserAspectRatioButtonShownChecker = userAspectRatioButtonStateChecker;
        mUserAspectRatioButtonStateConsumer = userAspectRatioButtonShownConsumer;
        mHasUserAspectRatioSettingsButton = getHasUserAspectRatioSettingsButton(taskInfo);
        mCompatUIHintsState = compatUIHintsState;
        mOnButtonClicked = onButtonClicked;
@@ -185,6 +197,7 @@ class UserAspectRatioSettingsWindowManager extends CompatUIWindowManagerAbstract
            return;
        }
        mLayout.setUserAspectRatioButtonVisibility(true);
        mUserAspectRatioButtonStateConsumer.accept(true);
        // Only show by default for the first time.
        if (!mCompatUIHintsState.mHasShownUserAspectRatioSettingsButtonHint) {
            mLayout.setUserAspectRatioSettingsHintVisibility(/* show= */ true);
@@ -210,7 +223,8 @@ class UserAspectRatioSettingsWindowManager extends CompatUIWindowManagerAbstract
    private boolean getHasUserAspectRatioSettingsButton(@NonNull TaskInfo taskInfo) {
        return taskInfo.topActivityEligibleForUserAspectRatioButton
                && (taskInfo.topActivityBoundsLetterboxed
                    || taskInfo.isUserFullscreenOverrideEnabled);
                    || taskInfo.isUserFullscreenOverrideEnabled)
                && !mUserAspectRatioButtonShownChecker.get();
    }

    private long getDisappearTimeMs() {
+119 −0
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ import com.android.wm.shell.transition.Transitions;

import dagger.Lazy;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -523,13 +524,131 @@ public class CompatUIControllerTest extends ShellTestCase {
                .createLayout(anyBoolean());
    }

    @Test
    public void testUpdateActiveTaskInfo_newTask_visibleAndFocused_updated() {
        // Simulate user aspect ratio button being shown for previous task
        mController.setHasShownUserAspectRatioSettingsButton(true);
        Assert.assertTrue(mController.hasShownUserAspectRatioSettingsButton());

        // Create new task
        final TaskInfo taskInfo = createTaskInfo(DISPLAY_ID, TASK_ID,
                /* hasSizeCompat= */ true, CAMERA_COMPAT_CONTROL_HIDDEN, /* isVisible */ true,
                /* isFocused */ true);

        // Simulate new task being shown
        mController.updateActiveTaskInfo(taskInfo);

        // Check topActivityTaskId is updated to the taskId of the new task and
        // hasShownUserAspectRatioSettingsButton has been reset to false
        Assert.assertEquals(TASK_ID, mController.getTopActivityTaskId());
        Assert.assertFalse(mController.hasShownUserAspectRatioSettingsButton());
    }

    @Test
    public void testUpdateActiveTaskInfo_newTask_notVisibleOrFocused_notUpdated() {
        // Create new task
        final TaskInfo taskInfo = createTaskInfo(DISPLAY_ID, TASK_ID,
                /* hasSizeCompat= */ true, CAMERA_COMPAT_CONTROL_HIDDEN, /* isVisible */ true,
                /* isFocused */ true);

        // Simulate task being shown
        mController.updateActiveTaskInfo(taskInfo);

        // Check topActivityTaskId is updated to the taskId of the new task and
        // hasShownUserAspectRatioSettingsButton has been reset to false
        Assert.assertEquals(TASK_ID, mController.getTopActivityTaskId());
        Assert.assertFalse(mController.hasShownUserAspectRatioSettingsButton());

        // Simulate user aspect ratio button being shown
        mController.setHasShownUserAspectRatioSettingsButton(true);
        Assert.assertTrue(mController.hasShownUserAspectRatioSettingsButton());

        final int newTaskId = TASK_ID + 1;

        // Create visible but NOT focused task
        final TaskInfo taskInfo1 = createTaskInfo(DISPLAY_ID, newTaskId,
                /* hasSizeCompat= */ true, CAMERA_COMPAT_CONTROL_HIDDEN, /* isVisible */ true,
                /* isFocused */ false);

        // Simulate new task being shown
        mController.updateActiveTaskInfo(taskInfo1);

        // Check topActivityTaskId is NOT updated and hasShownUserAspectRatioSettingsButton
        // remains true
        Assert.assertEquals(TASK_ID, mController.getTopActivityTaskId());
        Assert.assertTrue(mController.hasShownUserAspectRatioSettingsButton());

        // Create focused but NOT visible task
        final TaskInfo taskInfo2 = createTaskInfo(DISPLAY_ID, newTaskId,
                /* hasSizeCompat= */ true, CAMERA_COMPAT_CONTROL_HIDDEN, /* isVisible */ false,
                /* isFocused */ true);

        // Simulate new task being shown
        mController.updateActiveTaskInfo(taskInfo2);

        // Check topActivityTaskId is NOT updated and hasShownUserAspectRatioSettingsButton
        // remains true
        Assert.assertEquals(TASK_ID, mController.getTopActivityTaskId());
        Assert.assertTrue(mController.hasShownUserAspectRatioSettingsButton());

        // Create NOT focused but NOT visible task
        final TaskInfo taskInfo3 = createTaskInfo(DISPLAY_ID, newTaskId,
                /* hasSizeCompat= */ true, CAMERA_COMPAT_CONTROL_HIDDEN, /* isVisible */ false,
                /* isFocused */ false);

        // Simulate new task being shown
        mController.updateActiveTaskInfo(taskInfo3);

        // Check topActivityTaskId is NOT updated and hasShownUserAspectRatioSettingsButton
        // remains true
        Assert.assertEquals(TASK_ID, mController.getTopActivityTaskId());
        Assert.assertTrue(mController.hasShownUserAspectRatioSettingsButton());
    }

    @Test
    public void testUpdateActiveTaskInfo_sameTask_notUpdated() {
        // Create new task
        final TaskInfo taskInfo = createTaskInfo(DISPLAY_ID, TASK_ID,
                /* hasSizeCompat= */ true, CAMERA_COMPAT_CONTROL_HIDDEN, /* isVisible */ true,
                /* isFocused */ true);

        // Simulate new task being shown
        mController.updateActiveTaskInfo(taskInfo);

        // Check topActivityTaskId is updated to the taskId of the new task and
        // hasShownUserAspectRatioSettingsButton has been reset to false
        Assert.assertEquals(TASK_ID, mController.getTopActivityTaskId());
        Assert.assertFalse(mController.hasShownUserAspectRatioSettingsButton());

        // Simulate user aspect ratio button being shown
        mController.setHasShownUserAspectRatioSettingsButton(true);
        Assert.assertTrue(mController.hasShownUserAspectRatioSettingsButton());

        // Simulate same task being re-shown
        mController.updateActiveTaskInfo(taskInfo);

        // Check topActivityTaskId is NOT updated and hasShownUserAspectRatioSettingsButton
        // remains true
        Assert.assertEquals(TASK_ID, mController.getTopActivityTaskId());
        Assert.assertTrue(mController.hasShownUserAspectRatioSettingsButton());
    }

    private static TaskInfo createTaskInfo(int displayId, int taskId, boolean hasSizeCompat,
            @CameraCompatControlState int cameraCompatControlState) {
        return createTaskInfo(displayId, taskId, hasSizeCompat, cameraCompatControlState,
                /* isVisible */ false, /* isFocused */ false);
    }

    private static TaskInfo createTaskInfo(int displayId, int taskId, boolean hasSizeCompat,
            @CameraCompatControlState int cameraCompatControlState, boolean isVisible,
            boolean isFocused) {
        RunningTaskInfo taskInfo = new RunningTaskInfo();
        taskInfo.taskId = taskId;
        taskInfo.displayId = displayId;
        taskInfo.topActivityInSizeCompat = hasSizeCompat;
        taskInfo.cameraCompatControlState = cameraCompatControlState;
        taskInfo.isVisible = isVisible;
        taskInfo.isFocused = isFocused;
        return taskInfo;
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -93,7 +93,8 @@ public class UserAspectRatioSettingsLayoutTest extends ShellTestCase {
        mWindowManager = new UserAspectRatioSettingsWindowManager(mContext, mTaskInfo,
                mSyncTransactionQueue, mTaskListener, new DisplayLayout(),
                new CompatUIController.CompatUIHintsState(),
                mOnUserAspectRatioSettingsButtonClicked, new TestShellExecutor(), flags -> 0);
                mOnUserAspectRatioSettingsButtonClicked, new TestShellExecutor(), flags -> 0,
                () -> false, s -> {});

        mLayout = (UserAspectRatioSettingsLayout) LayoutInflater.from(mContext).inflate(
                R.layout.user_aspect_ratio_settings_layout, null);
Loading