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

Commit 2377e92d authored by Nergi Rahardi's avatar Nergi Rahardi
Browse files

Refactor isTaskMaximized to enforce caller to validate DisplayLayout

isTaskMaximized implicitly did a null check (which could lead to crash)
when taskInfo.displayId is no longer valid. As this is just a boolean
function, returning null when display is missing is not a very clear. So
it's better to enforce from the caller side to pass DisplayLayout so it
will do a null check before passing it to isTaskMaximized

Also removed the redundant overloading

Bug: 426465402
Test: m & presubmit
Flag: EXEMPT bugfix
Change-Id: I43fbc75731a6f5fae878bbeda5fa1271ced8635e
parent d55f80cc
Loading
Loading
Loading
Loading
+9 −1
Original line number Original line Diff line number Diff line
@@ -144,13 +144,21 @@ class DesktopModeKeyGestureHandler(
            KeyGestureEvent.KEY_GESTURE_TYPE_TOGGLE_MAXIMIZE_FREEFORM_WINDOW -> {
            KeyGestureEvent.KEY_GESTURE_TYPE_TOGGLE_MAXIMIZE_FREEFORM_WINDOW -> {
                logV("Key gesture TOGGLE_MAXIMIZE_FREEFORM_WINDOW is handled")
                logV("Key gesture TOGGLE_MAXIMIZE_FREEFORM_WINDOW is handled")
                getGloballyFocusedDesktopTask()?.let { taskInfo ->
                getGloballyFocusedDesktopTask()?.let { taskInfo ->
                    val displayId = taskInfo.displayId
                    val displayLayout = displayController.getDisplayLayout(displayId)
                    if (displayLayout == null) {
                        logW(
                            "Display %d is not found, task displayId might be stale", displayId
                        )
                        return
                    }
                    mainExecutor.execute {
                    mainExecutor.execute {
                        desktopTasksController
                        desktopTasksController
                            .get()
                            .get()
                            .toggleDesktopTaskSize(
                            .toggleDesktopTaskSize(
                                taskInfo,
                                taskInfo,
                                ToggleTaskSizeInteraction(
                                ToggleTaskSizeInteraction(
                                    isMaximized = isTaskMaximized(taskInfo, displayController),
                                    isMaximized = isTaskMaximized(taskInfo, displayLayout),
                                    source = ToggleTaskSizeInteraction.Source.KEYBOARD_SHORTCUT,
                                    source = ToggleTaskSizeInteraction.Source.KEYBOARD_SHORTCUT,
                                    inputMethod =
                                    inputMethod =
                                        DesktopModeEventLogger.Companion.InputMethod.KEYBOARD,
                                        DesktopModeEventLogger.Companion.InputMethod.KEYBOARD,
+1 −9
Original line number Original line Diff line number Diff line
@@ -261,17 +261,9 @@ fun calculateAspectRatio(taskInfo: TaskInfo): Float {
}
}


/** Returns whether the task is maximized. */
/** Returns whether the task is maximized. */
fun isTaskMaximized(taskInfo: RunningTaskInfo, displayController: DisplayController): Boolean {
fun isTaskMaximized(taskInfo: RunningTaskInfo, displayLayout: DisplayLayout): Boolean {
    val displayLayout =
        displayController.getDisplayLayout(taskInfo.displayId)
            ?: error("Could not get display layout for display=${taskInfo.displayId}")
    val stableBounds = Rect()
    val stableBounds = Rect()
    displayLayout.getStableBounds(stableBounds)
    displayLayout.getStableBounds(stableBounds)
    return isTaskMaximized(taskInfo, stableBounds)
}

/** Returns whether the task is maximized. */
fun isTaskMaximized(taskInfo: RunningTaskInfo, stableBounds: Rect): Boolean {
    val currentTaskBounds = taskInfo.configuration.windowConfiguration.bounds
    val currentTaskBounds = taskInfo.configuration.windowConfiguration.bounds
    return if (taskInfo.isResizeable) {
    return if (taskInfo.isResizeable) {
        isTaskBoundsEqual(currentTaskBounds, stableBounds)
        isTaskBoundsEqual(currentTaskBounds, stableBounds)
+7 −1
Original line number Original line Diff line number Diff line
@@ -2639,7 +2639,13 @@ class DesktopTasksController(
        currentDragBounds: Rect,
        currentDragBounds: Rect,
        motionEvent: MotionEvent,
        motionEvent: MotionEvent,
    ) {
    ) {
        if (isTaskMaximized(taskInfo, displayController)) {
        val displayId = taskInfo.displayId
        val displayLayout = displayController.getDisplayLayout(displayId)
        if (displayLayout == null)  {
            logW("Display %d is not found, task displayId might be stale", displayId)
            return
        }
        if (isTaskMaximized(taskInfo, displayLayout)) {
            // Handle the case where we attempt to drag-to-maximize when already maximized: the task
            // Handle the case where we attempt to drag-to-maximize when already maximized: the task
            // position won't need to change but we want to animate the surface going back to the
            // position won't need to change but we want to animate the surface going back to the
            // maximized position.
            // maximized position.
+1 −3
Original line number Original line Diff line number Diff line
@@ -804,9 +804,7 @@ public class DesktopModeWindowDecorViewModel implements WindowDecorViewModel,
        if (displayLayout == null) {
        if (displayLayout == null) {
            return null;
            return null;
        }
        }
        final Rect stableBounds = new Rect();
        boolean isMaximized = DesktopModeUtils.isTaskMaximized(taskInfo, displayLayout);
        displayLayout.getStableBounds(stableBounds);
        boolean isMaximized = DesktopModeUtils.isTaskMaximized(taskInfo, stableBounds);


        return new ToggleTaskSizeInteraction(
        return new ToggleTaskSizeInteraction(
                isMaximized
                isMaximized
+16 −6
Original line number Original line Diff line number Diff line
@@ -29,6 +29,7 @@ import static android.window.DesktopModeFlags.ENABLE_CAPTION_COMPAT_INSET_FORCE_
import static android.window.DesktopModeFlags.ENABLE_CAPTION_COMPAT_INSET_FORCE_CONSUMPTION_ALWAYS;
import static android.window.DesktopModeFlags.ENABLE_CAPTION_COMPAT_INSET_FORCE_CONSUMPTION_ALWAYS;


import static com.android.internal.policy.SystemBarUtils.getDesktopViewAppHeaderHeightId;
import static com.android.internal.policy.SystemBarUtils.getDesktopViewAppHeaderHeightId;
import static com.android.wm.shell.protolog.ShellProtoLogGroup.WM_SHELL_DESKTOP_MODE;
import static com.android.wm.shell.shared.split.SplitScreenConstants.SPLIT_POSITION_BOTTOM_OR_RIGHT;
import static com.android.wm.shell.shared.split.SplitScreenConstants.SPLIT_POSITION_BOTTOM_OR_RIGHT;
import static com.android.wm.shell.windowdecor.DragPositioningCallbackUtility.DragEventListener;
import static com.android.wm.shell.windowdecor.DragPositioningCallbackUtility.DragEventListener;
import static com.android.wm.shell.windowdecor.DragResizeWindowGeometry.DisabledEdge;
import static com.android.wm.shell.windowdecor.DragResizeWindowGeometry.DisabledEdge;
@@ -60,7 +61,6 @@ import android.os.Handler;
import android.os.Trace;
import android.os.Trace;
import android.os.UserHandle;
import android.os.UserHandle;
import android.util.Size;
import android.util.Size;
import android.util.Slog;
import android.view.Choreographer;
import android.view.Choreographer;
import android.view.Display;
import android.view.Display;
import android.view.InsetsState;
import android.view.InsetsState;
@@ -80,6 +80,7 @@ import android.window.WindowContainerTransaction;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.policy.DesktopModeCompatPolicy;
import com.android.internal.policy.DesktopModeCompatPolicy;
import com.android.internal.policy.SystemBarUtils;
import com.android.internal.policy.SystemBarUtils;
import com.android.internal.protolog.ProtoLog;
import com.android.window.flags.Flags;
import com.android.window.flags.Flags;
import com.android.wm.shell.R;
import com.android.wm.shell.R;
import com.android.wm.shell.RootTaskDisplayAreaOrganizer;
import com.android.wm.shell.RootTaskDisplayAreaOrganizer;
@@ -933,14 +934,16 @@ public class DesktopModeWindowDecoration extends WindowDecoration<WindowDecorLin
    /** Update the view holder for app header. */
    /** Update the view holder for app header. */
    private void updateAppHeaderViewHolder(boolean inFullImmersive, boolean hasGlobalFocus) {
    private void updateAppHeaderViewHolder(boolean inFullImmersive, boolean hasGlobalFocus) {
        if (!isAppHeader(mWindowDecorViewHolder)) return;
        if (!isAppHeader(mWindowDecorViewHolder)) return;
        if (mDisplayController.getDisplayLayout(mTaskInfo.displayId) == null) {
        final int displayId = mTaskInfo.displayId;
            Slog.w(TAG, "Display" + mTaskInfo.displayId
        final DisplayLayout displayLayout = mDisplayController.getDisplayLayout(displayId);
                    + " is not found, task displayId might be stale");
        if (displayLayout == null) {
            ProtoLog.w(WM_SHELL_DESKTOP_MODE,
                    "%s: Display %d is not found, task displayId might be stale", TAG, displayId);
            return;
            return;
        }
        }
        asAppHeader(mWindowDecorViewHolder).bindData(new AppHeaderViewHolder.HeaderData(
        asAppHeader(mWindowDecorViewHolder).bindData(new AppHeaderViewHolder.HeaderData(
                mTaskInfo,
                mTaskInfo,
                DesktopModeUtils.isTaskMaximized(mTaskInfo, mDisplayController),
                DesktopModeUtils.isTaskMaximized(mTaskInfo, displayLayout),
                inFullImmersive,
                inFullImmersive,
                hasGlobalFocus,
                hasGlobalFocus,
                /* maximizeHoverEnabled= */ canOpenMaximizeMenu(
                /* maximizeHoverEnabled= */ canOpenMaximizeMenu(
@@ -1969,12 +1972,19 @@ public class DesktopModeWindowDecoration extends WindowDecoration<WindowDecorLin


    void setAnimatingTaskResizeOrReposition(boolean animatingTaskResizeOrReposition) {
    void setAnimatingTaskResizeOrReposition(boolean animatingTaskResizeOrReposition) {
        if (!isAppHeader(mWindowDecorViewHolder)) return;
        if (!isAppHeader(mWindowDecorViewHolder)) return;
        final int displayId = mTaskInfo.displayId;
        final DisplayLayout displayLayout = mDisplayController.getDisplayLayout(displayId);
        if (displayLayout == null)  {
            ProtoLog.w(WM_SHELL_DESKTOP_MODE,
                    "%s: Display %d is not found, task displayId might be stale", TAG, displayId);
            return;
        }
        final boolean inFullImmersive =
        final boolean inFullImmersive =
                mDesktopUserRepositories.getProfile(mTaskInfo.userId)
                mDesktopUserRepositories.getProfile(mTaskInfo.userId)
                        .isTaskInFullImmersiveState(mTaskInfo.taskId);
                        .isTaskInFullImmersiveState(mTaskInfo.taskId);
        asAppHeader(mWindowDecorViewHolder).bindData(new AppHeaderViewHolder.HeaderData(
        asAppHeader(mWindowDecorViewHolder).bindData(new AppHeaderViewHolder.HeaderData(
                mTaskInfo,
                mTaskInfo,
                DesktopModeUtils.isTaskMaximized(mTaskInfo, mDisplayController),
                DesktopModeUtils.isTaskMaximized(mTaskInfo, displayLayout),
                inFullImmersive,
                inFullImmersive,
                isFocused(),
                isFocused(),
                /* maximizeHoverEnabled= */ canOpenMaximizeMenu(animatingTaskResizeOrReposition),
                /* maximizeHoverEnabled= */ canOpenMaximizeMenu(animatingTaskResizeOrReposition),
Loading