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

Commit 40dde01a authored by Riddle Hsu's avatar Riddle Hsu
Browse files

Use pre-computed top process for updating previous process

There was a computed field with the same meaning, so it is
unnecessary to traverse the entire hierarchy to find again.
The traversal method is also removed because no one uses it
anymore.

Moved updatePreviousProcess from RWC to ATMS because it
updates the fields of ATMS.

Bug: 163976519
Test: CtsWindowManagerDeviceTestCases
Change-Id: I9177cb34b0456e5c60542fd1bac40b884d8db3a7
parent 05c87f76
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -5695,7 +5695,7 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
                    destroyImmediately("stop-config");
                    mRootWindowContainer.resumeFocusedTasksTopActivities();
                } else {
                    mRootWindowContainer.updatePreviousProcess(this);
                    mAtmService.updatePreviousProcess(this);
                }
            }
            mTaskSupervisor.checkReadyForSleepLocked(true /* allowDelay */);
+18 −1
Original line number Diff line number Diff line
@@ -398,7 +398,7 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
     */
    volatile WindowProcessController mPreviousProcess;
    /** The time at which the previous process was last visible. */
    long mPreviousProcessVisibleTime;
    private long mPreviousProcessVisibleTime;

    /** List of intents that were used to start the most recent tasks. */
    private RecentTasks mRecentTasks;
@@ -4634,6 +4634,23 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
        mTopApp = top != null ? top.app : null;
    }

    /**
     * The process state of previous activity is more important than the regular background to keep
     * around in case the user wants to return to it.
     */
    void updatePreviousProcess(ActivityRecord stoppedActivity) {
        if (stoppedActivity.app != null && mTopApp != null
                // Don't replace the previous process if the stopped one is the top, e.g. sleeping.
                && stoppedActivity.app != mTopApp
                // The stopped activity must have been visible later than the previous.
                && stoppedActivity.lastVisibleTime > mPreviousProcessVisibleTime
                // Home has its own retained state, so don't let it occupy the previous.
                && stoppedActivity.app != mHomeProcess) {
            mPreviousProcess = stoppedActivity.app;
            mPreviousProcessVisibleTime = stoppedActivity.lastVisibleTime;
        }
    }

    void updateActivityUsageStats(ActivityRecord activity, int event) {
        ComponentName taskRoot = null;
        final Task task = activity.getTask();
+0 −28
Original line number Diff line number Diff line
@@ -1894,34 +1894,6 @@ class RootWindowContainer extends WindowContainer<DisplayContent>
        return task != null && task == getTopDisplayFocusedRootTask();
    }

    void updatePreviousProcess(ActivityRecord r) {
        // Now that this process has stopped, we may want to consider it to be the previous app to
        // try to keep around in case the user wants to return to it.

        // First, found out what is currently the foreground app, so that we don't blow away the
        // previous app if this activity is being hosted by the process that is actually still the
        // foreground.
        WindowProcessController fgApp = getItemFromRootTasks(rootTask -> {
            if (isTopDisplayFocusedRootTask(rootTask)) {
                final ActivityRecord resumedActivity = rootTask.getTopResumedActivity();
                if (resumedActivity != null) {
                    return resumedActivity.app;
                } else if (rootTask.getTopPausingActivity() != null) {
                    return rootTask.getTopPausingActivity().app;
                }
            }
            return null;
        });

        // Now set this one as the previous process, only if that really makes sense to.
        if (r.hasProcess() && fgApp != null && r.app != fgApp
                && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
                && r.app != mService.mHomeProcess) {
            mService.mPreviousProcess = r.app;
            mService.mPreviousProcessVisibleTime = r.lastVisibleTime;
        }
    }

    boolean attachApplication(WindowProcessController app) throws RemoteException {
        try {
            return mAttachApplicationHelper.process(app);
+0 −7
Original line number Diff line number Diff line
@@ -214,7 +214,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
@@ -3199,12 +3198,6 @@ class Task extends TaskFragment {
        return isRootTask() && callback.test(this) ? this : null;
    }

    @Nullable
    @Override
    <R> R getItemFromRootTasks(Function<Task, R> callback, boolean traverseTopToBottom) {
        return isRootTask() ? callback.apply(this) : null;
    }

    /**
     * @param canAffectSystemUiFlags If false, all windows in this task can not affect SystemUI
     *                               flags. See {@link WindowState#canAffectSystemUiFlags()}.
+0 −41
Original line number Diff line number Diff line
@@ -2115,47 +2115,6 @@ class WindowContainer<E extends WindowContainer> extends ConfigurationContainer<
        return getItemFromTaskDisplayAreas(callback, true /* traverseTopToBottom */);
    }

    /**
     * Finds the first non {@code null} return value from calling the callback on all root
     * {@link Task} at or below this container.
     * @param callback Applies on each root {@link Task} found and stops the search if it
     *                 returns non {@code null}.
     * @param traverseTopToBottom If {@code true}, traverses the hierarchy from top-to-bottom in
     *                            terms of z-order, else from bottom-to-top.
     * @return the first returned object that is not {@code null}. Returns {@code null} if not
     *         found.
     */
    @Nullable
    <R> R getItemFromRootTasks(Function<Task, R> callback, boolean traverseTopToBottom) {
        int count = mChildren.size();
        if (traverseTopToBottom) {
            for (int i = count - 1; i >= 0; --i) {
                R result = (R) mChildren.get(i).getItemFromRootTasks(callback, traverseTopToBottom);
                if (result != null) {
                    return result;
                }
            }
        } else {
            for (int i = 0; i < count; i++) {
                R result = (R) mChildren.get(i).getItemFromRootTasks(callback, traverseTopToBottom);
                if (result != null) {
                    return result;
                }
                // Root tasks may be removed from this display. Ensure each task will be processed
                // and the loop will end.
                int newCount = mChildren.size();
                i -= count - newCount;
                count = newCount;
            }
        }
        return null;
    }

    @Nullable
    <R> R getItemFromRootTasks(Function<Task, R> callback) {
        return getItemFromRootTasks(callback, true /* traverseTopToBottom */);
    }

    /**
     * Returns 1, 0, or -1 depending on if this container is greater than, equal to, or lesser than
     * the input container in terms of z-order.