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

Commit 8af73ff2 authored by Evan Rosky's avatar Evan Rosky
Browse files

Move some more task-view management out of taskview impl [2/N]

Continuing to move high-level control out of taskview impl.

This focuses on combining removeTaskView with closeTaskView
since the usages were the same but obfuscated a little. In
doing so, also renamed the existing add/remove TaskView in
TaskViewTransitions to register/unregister since their only
responsibility was registering/unregistering with the
repository.

Also moved moveTaskViewToFullscreen (which is behind a flag
anyways)

This is also mechanical, but not verbatim (required a little bit
of re-arrangement, so separated it to make it easier to verify
that code was just moved.

Bug: 384976265
Test: refactor only, so existing tests
Flag: EXEMPT mechanical refactor
Change-Id: I6849311e07dc2bc2dfcd361ac81b4a3cc43d0114
parent d03a47b1
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -127,7 +127,7 @@ public class TaskView extends SurfaceView implements SurfaceHolder.Callback,
     * Moves the current task in taskview out of the view and back to fullscreen.
     */
    public void moveToFullscreen() {
        mTaskViewTaskController.moveToFullscreen();
        mTaskViewTransitions.moveTaskViewToFullscreen(mTaskViewTaskController);
    }

    @Override
@@ -234,7 +234,7 @@ public class TaskView extends SurfaceView implements SurfaceHolder.Callback,
     * Call to remove the task from window manager. This task will not appear in recents.
     */
    public void removeTask() {
        mTaskViewTaskController.removeTask();
        mTaskViewTransitions.removeTaskView(mTaskViewTaskController, null /* token */);
    }

    /**
+10 −57
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package com.android.wm.shell.taskview;

import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ActivityManager;
@@ -27,14 +25,12 @@ import android.graphics.Rect;
import android.gui.TrustedOverlay;
import android.os.Binder;
import android.util.CloseGuard;
import android.util.Slog;
import android.view.SurfaceControl;
import android.view.WindowInsets;
import android.window.WindowContainerToken;
import android.window.WindowContainerTransaction;

import com.android.internal.annotations.VisibleForTesting;
import com.android.wm.shell.Flags;
import com.android.wm.shell.ShellTaskOrganizer;
import com.android.wm.shell.common.SyncTransactionQueue;

@@ -93,7 +89,7 @@ public class TaskViewTaskController implements ShellTaskOrganizer.TaskListener {
        mTaskViewTransitions = taskViewTransitions;
        mShellExecutor.execute(() -> {
            if (mTaskViewTransitions != null) {
                mTaskViewTransitions.addTaskView(this);
                mTaskViewTransitions.registerTaskView(this);
            }
        });
        mGuard.open("release");
@@ -151,24 +147,6 @@ public class TaskViewTaskController implements ShellTaskOrganizer.TaskListener {
        mListenerExecutor = executor;
    }

    /**
     * Moves the current task in TaskView out of the view and back to fullscreen.
     */
    public void moveToFullscreen() {
        if (mTaskToken == null) return;
        mShellExecutor.execute(() -> {
            WindowContainerTransaction wct = new WindowContainerTransaction();
            wct.setWindowingMode(mTaskToken, WINDOWING_MODE_UNDEFINED);
            wct.setAlwaysOnTop(mTaskToken, false);
            mTaskOrganizer.setInterceptBackPressedOnTaskRoot(mTaskToken, false);
            mTaskViewTransitions.moveTaskViewToFullscreen(wct, this);
            if (mListener != null) {
                // Task is being "removed" from the clients perspective
                mListener.onTaskRemovalStarted(mTaskInfo.taskId);
            }
        });
    }

    /**
     * Release this container if it is initialized.
     */
@@ -191,7 +169,7 @@ public class TaskViewTaskController implements ShellTaskOrganizer.TaskListener {
    private void performRelease() {
        mShellExecutor.execute(() -> {
            if (mTaskViewTransitions != null) {
                mTaskViewTransitions.removeTaskView(this);
                mTaskViewTransitions.unregisterTaskView(this);
            }
            mTaskOrganizer.removeListener(this);
            resetTaskInfo();
@@ -377,30 +355,6 @@ public class TaskViewTaskController implements ShellTaskOrganizer.TaskListener {
        });
    }

    /**
     * Call to remove the task from window manager. This task will not appear in recents.
     */
    void removeTask() {
        if (mTaskToken == null) {
            if (Flags.enableTaskViewControllerCleanup()) {
                // We don't have a task yet. Only clean up the controller
                mTaskViewTransitions.removeTaskView(this);
            } else {
                // Call to remove task before we have one, do nothing
                Slog.w(TAG, "Trying to remove a task that was never added? (no taskToken)");
            }
            return;
        }
        // Cache it to avoid NPE and make sure to remove it from recents history.
        // mTaskToken can be cleared in onTaskVanished() when the task is removed.
        final WindowContainerToken taskToken = mTaskToken;
        mShellExecutor.execute(() -> {
            WindowContainerTransaction wct = new WindowContainerTransaction();
            wct.removeTask(taskToken);
            mTaskViewTransitions.closeTaskView(wct, this);
        });
    }

    /**
     * Sets a region of the task to inset to allow for a caption bar.
     *
@@ -459,15 +413,16 @@ public class TaskViewTaskController implements ShellTaskOrganizer.TaskListener {
        }
    }

    void notifyTaskRemovalStarted(@NonNull ActivityManager.RunningTaskInfo taskInfo) {
        if (mListener == null) return;
        final int taskId = taskInfo.taskId;
        mListenerExecutor.execute(() -> mListener.onTaskRemovalStarted(taskId));
    }

    /** Notifies listeners of a task being removed and stops intercepting back presses on it. */
    private void handleAndNotifyTaskRemoval(ActivityManager.RunningTaskInfo taskInfo) {
        if (taskInfo != null) {
            if (mListener != null) {
                final int taskId = taskInfo.taskId;
                mListenerExecutor.execute(() -> {
                    mListener.onTaskRemovalStarted(taskId);
                });
            }
            notifyTaskRemovalStarted(taskInfo);
            mTaskViewBase.onTaskVanished(taskInfo);
        }
    }
@@ -506,9 +461,7 @@ public class TaskViewTaskController implements ShellTaskOrganizer.TaskListener {
            handleAndNotifyTaskRemoval(pendingInfo);

            // Make sure the task is removed
            WindowContainerTransaction wct = new WindowContainerTransaction();
            wct.removeTask(pendingInfo.token);
            mTaskViewTransitions.closeTaskView(wct, this);
            mTaskViewTransitions.removeTaskView(this, pendingInfo.token);
        }
        resetTaskInfo();
    }
+42 −10
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.wm.shell.taskview;

import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
import static android.view.WindowManager.TRANSIT_CHANGE;
import static android.view.WindowManager.TRANSIT_CLOSE;
import static android.view.WindowManager.TRANSIT_OPEN;
@@ -41,6 +42,7 @@ import android.view.SurfaceControl;
import android.view.WindowManager;
import android.window.TransitionInfo;
import android.window.TransitionRequestInfo;
import android.window.WindowContainerToken;
import android.window.WindowContainerTransaction;

import androidx.annotation.VisibleForTesting;
@@ -155,7 +157,7 @@ public class TaskViewTransitions implements Transitions.TransitionHandler {
        return mTaskViewRepo;
    }

    void addTaskView(TaskViewTaskController tv) {
    void registerTaskView(TaskViewTaskController tv) {
        synchronized (mRegistered) {
            if (!mRegistered[0]) {
                mRegistered[0] = true;
@@ -169,7 +171,7 @@ public class TaskViewTransitions implements Transitions.TransitionHandler {
        }
    }

    void removeTaskView(TaskViewTaskController tv) {
    void unregisterTaskView(TaskViewTaskController tv) {
        if (useRepo()) {
            mTaskViewRepo.remove(tv);
        } else {
@@ -397,17 +399,47 @@ public class TaskViewTransitions implements Transitions.TransitionHandler {
        startNextTransition();
    }

    void closeTaskView(@NonNull WindowContainerTransaction wct,
            @NonNull TaskViewTaskController taskView) {
    /**
     * Removes a taskview and also removes it's task from window manager. This task will not appear
     * in recents.
     */
    public void removeTaskView(@NonNull TaskViewTaskController taskView,
            @Nullable WindowContainerToken taskToken) {
        final WindowContainerToken token = taskToken != null ? taskToken : taskView.getTaskToken();
        if (token == null) {
            // We don't have a task yet, so just clean up records
            if (!Flags.enableTaskViewControllerCleanup()) {
                // Call to remove task before we have one, do nothing
                Slog.w(TAG, "Trying to remove a task that was never added? (no taskToken)");
                return;
            }
            unregisterTaskView(taskView);
            return;
        }
        final WindowContainerTransaction wct = new WindowContainerTransaction();
        wct.removeTask(token);
        updateVisibilityState(taskView, false /* visible */);
        mShellExecutor.execute(() -> {
            mPending.add(new PendingTransition(TRANSIT_CLOSE, wct, taskView, null /* cookie */));
            startNextTransition();
        });
    }

    void moveTaskViewToFullscreen(@NonNull WindowContainerTransaction wct,
            @NonNull TaskViewTaskController taskView) {
    /**
     * Moves the current task in TaskView out of the view and back to fullscreen.
     */
    public void moveTaskViewToFullscreen(@NonNull TaskViewTaskController taskView) {
        final WindowContainerToken taskToken = taskView.getTaskToken();
        if (taskToken == null) return;
        final WindowContainerTransaction wct = new WindowContainerTransaction();
        wct.setWindowingMode(taskToken, WINDOWING_MODE_UNDEFINED);
        wct.setAlwaysOnTop(taskToken, false);
        mShellExecutor.execute(() -> {
            mTaskOrganizer.setInterceptBackPressedOnTaskRoot(taskToken, false);
            mPending.add(new PendingTransition(TRANSIT_CHANGE, wct, taskView, null /* cookie */));
            startNextTransition();
            taskView.notifyTaskRemovalStarted(taskView.getTaskInfo());
        });
    }

    /** Starts a new transition to make the given {@code taskView} visible. */
+6 −6
Original line number Diff line number Diff line
@@ -424,7 +424,7 @@ public class TaskViewTest extends ShellTestCase {
        verify(mOrganizer).removeListener(eq(mTaskViewTaskController));
        verify(mViewListener).onReleased();
        assertThat(mTaskView.isInitialized()).isFalse();
        verify(mTaskViewTransitions).removeTaskView(eq(mTaskViewTaskController));
        verify(mTaskViewTransitions).unregisterTaskView(eq(mTaskViewTaskController));
    }

    @Test
@@ -620,7 +620,7 @@ public class TaskViewTest extends ShellTestCase {
        assumeTrue(Transitions.ENABLE_SHELL_TRANSITIONS);

        mTaskView.removeTask();
        verify(mTaskViewTransitions, never()).closeTaskView(any(), any());
        assertFalse(mTaskViewTransitions.hasPending());
    }

    @Test
@@ -636,7 +636,7 @@ public class TaskViewTest extends ShellTestCase {
        verify(mViewListener).onTaskCreated(eq(mTaskInfo.taskId), any());

        mTaskView.removeTask();
        verify(mTaskViewTransitions).closeTaskView(any(), eq(mTaskViewTaskController));
        verify(mTaskViewTransitions).removeTaskView(eq(mTaskViewTaskController), any());
    }

    @Test
@@ -647,7 +647,7 @@ public class TaskViewTest extends ShellTestCase {
        mTaskViewTaskController.onTaskAppeared(mTaskInfo, mLeash);

        assertNull(mTaskViewTaskController.getTaskInfo());
        verify(mTaskViewTransitions).closeTaskView(any(), eq(mTaskViewTaskController));
        verify(mTaskViewTransitions).removeTaskView(eq(mTaskViewTaskController), any());
    }

    @Test
@@ -656,7 +656,7 @@ public class TaskViewTest extends ShellTestCase {

        mTaskViewTaskController.onTaskAppeared(mTaskInfo, mLeash);
        assertEquals(mTaskInfo, mTaskViewTaskController.getPendingInfo());
        verify(mTaskViewTransitions, never()).closeTaskView(any(), any());
        verify(mTaskViewTransitions, never()).removeTaskView(any(), any());
    }

    @Test
@@ -778,7 +778,7 @@ public class TaskViewTest extends ShellTestCase {
                new SurfaceControl.Transaction(), new SurfaceControl.Transaction(), mTaskInfo,
                mLeash, wct);
        mTaskView.surfaceCreated(mock(SurfaceHolder.class));
        mTaskViewTaskController.moveToFullscreen();
        mTaskViewTransitions.moveTaskViewToFullscreen(mTaskViewTaskController);

        verify(mViewListener).onTaskRemovalStarted(eq(mTaskInfo.taskId));
    }
+3 −3
Original line number Diff line number Diff line
@@ -116,7 +116,7 @@ public class TaskViewTransitionsTest extends ShellTestCase {
        when(mOrganizer.getExecutor()).thenReturn(mExecutor);
        mTaskViewTransitions = spy(new TaskViewTransitions(mTransitions, mTaskViewRepository,
                mOrganizer, mSyncQueue));
        mTaskViewTransitions.addTaskView(mTaskViewTaskController);
        mTaskViewTransitions.registerTaskView(mTaskViewTaskController);
        when(mTaskViewTaskController.getTaskInfo()).thenReturn(mTaskInfo);
        when(mTaskViewTaskController.getTaskToken()).thenReturn(mToken);
    }
@@ -224,7 +224,7 @@ public class TaskViewTransitionsTest extends ShellTestCase {

    @Test
    public void testSetTaskVisibility_taskRemoved_noNPE() {
        mTaskViewTransitions.removeTaskView(mTaskViewTaskController);
        mTaskViewTransitions.unregisterTaskView(mTaskViewTaskController);

        assumeTrue(Transitions.ENABLE_SHELL_TRANSITIONS);

@@ -233,7 +233,7 @@ public class TaskViewTransitionsTest extends ShellTestCase {

    @Test
    public void testSetTaskBounds_taskRemoved_noNPE() {
        mTaskViewTransitions.removeTaskView(mTaskViewTaskController);
        mTaskViewTransitions.unregisterTaskView(mTaskViewTaskController);

        assumeTrue(Transitions.ENABLE_SHELL_TRANSITIONS);