Activity 启动流程(二)—— AMS 处理阶段

210 阅读13分钟

前言

Activity 启动过程文章系列中,首先会先对启动流程从头到尾进行简要梳理,后续文章会对其中重要细节进行详细展开。

启动流程梳理:

  1. Activity 启动流程(一)—— Launcher 阶段
  2. Activity 启动流程(二)—— AMS 处理阶段
  3. Activity 启动流程(三)—— 应用程序进程启动阶段
  4. Activity 启动流程(四)—— ActivityThread 初始化阶段
  5. Activity 启动流程(五)—— Activity 启动阶段

经过 Launcher 阶段后,进入到了系统服务处理阶段,AMS 校验权限,创建处理对应的 ActivityRecord 和 Task ,并挂载到窗口层级树中。

1. AMS 处理阶段

1.1 ATMS 创建 ActivityStarter

ActivityTaskManagerService 的 startActivity 方法:

// services/core/java/com/android/server/wm/ActivityTaskManagerService.java
@Override
public final int startActivity(IApplicationThread caller, String callingPackage,
        String callingFeatureId, Intent intent, String resolvedType, IBinder resultTo,
        String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo,
        Bundle bOptions) {
    return startActivityAsUser(caller, callingPackage, callingFeatureId, intent, resolvedType,
            resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions,
            UserHandle.getCallingUserId());
}

UserHandle.getCallingUserId() 获得调用者的 UserId,根据 UserId 来确定调用者的权限。

// services/core/java/com/android/server/wm/ActivityTaskManagerService.java
@Override
public int startActivityAsUser(IApplicationThread caller, String callingPackage,
        String callingFeatureId, Intent intent, String resolvedType, IBinder resultTo,
        String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo,
        Bundle bOptions, int userId) {
    return startActivityAsUser(caller, callingPackage, callingFeatureId, intent, resolvedType,
            resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions, userId,
            true /*validateIncomingUser*/);
}

private int startActivityAsUser(IApplicationThread caller, String callingPackage,
        @Nullable String callingFeatureId, Intent intent, String resolvedType,
        IBinder resultTo, String resultWho, int requestCode, int startFlags,
        ProfilerInfo profilerInfo, Bundle bOptions, int userId, boolean validateIncomingUser) {
    ...
    // 判断调用者进程是否被隔离
    enforceNotIsolatedCaller("startActivityAsUser");
    ...
    // 检查调用者权限
    userId = getActivityStartController().checkTargetUser(userId, validateIncomingUser,
            Binder.getCallingPid(), Binder.getCallingUid(), "startActivityAsUser");

    // TODO: Switch to user app stacks here.
    // 获取 ActivityStarter 并执行
    return getActivityStartController().obtainStarter(intent, "startActivityAsUser")
            .setCaller(caller)
            .setCallingPackage(callingPackage)
            .setCallingFeatureId(callingFeatureId)
            .setResolvedType(resolvedType)
            .setResultTo(resultTo)
            .setResultWho(resultWho)
            .setRequestCode(requestCode)
            .setStartFlags(startFlags)
            .setProfilerInfo(profilerInfo)
            .setActivityOptions(bOptions)
            .setUserId(userId)
            .execute();

}

static void enforceNotIsolatedCaller(String caller) {
    if (UserHandle.isIsolated(Binder.getCallingUid())) {
        throw new SecurityException("Isolated process not allowed to call " + caller);
    }
}

调用者进程如果被隔离或者无权限会抛出 SecurityException 异常。最后调用 ActivityStartController 的 obtainStarter 方法获取一个 ActivityStarter 对象。

1.2 ActivityStarter execute 流程

ActivityStarter 是启动 Activity 的控制类,根据设置的参数来决定如何根据 Intent 和 Flags 来启动 Activity,并将 Activity 和 Task 以及 Stack 相关联。它在调用 ActivityStarter 的 execute 方法之前一直有效。

// services/core/java/com/android/server/wm/ActivityStartController.java
ActivityStarter obtainStarter(Intent intent, String reason) {
    return mFactory.obtain().setIntent(intent).setReason(reason);
}

最后执行 ActivityStarter 的 execute 方法,根据前面提供的请求参数解析必要的信息,并且执行启动 Activity 的请求:

// services/core/java/com/android/server/wm/ActivityStarter.java
int execute() {
    ...
    res = executeRequest(mRequest);
    ...
}

executeRequest 执行 Activity 启动请求,这里首先执几项初步检查,并创建了 ActivityRecord:

// services/core/java/com/android/server/wm/ActivityStarter.java
private int executeRequest(Request request) {
    ...
    final ActivityRecord r = new ActivityRecord.Builder(mService)
                    .setCaller(callerApp)
                    .setLaunchedFromPid(callingPid)
                    .setLaunchedFromUid(callingUid)
                    .setLaunchedFromPackage(callingPackage)
                    .setLaunchedFromFeature(callingFeatureId)
                    .setIntent(intent)
                    .setResolvedType(resolvedType)
                    .setActivityInfo(aInfo)
                    .setConfiguration(mService.getGlobalConfiguration())
                    .setResultTo(resultRecord)
                    .setResultWho(resultWho)
                    .setRequestCode(requestCode)
                    .setComponentSpecified(request.componentSpecified)
                    .setRootVoiceInteraction(voiceSession != null)
                    .setActivityOptions(checkedOptions)
                    .setSourceRecord(sourceRecord)
                    .build();
    ...
    mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
            request.voiceInteractor, startFlags, true /* doResume */, checkedOptions, inTask,
            restrictedBgActivity, intentGrants);
    ...
    return mLastStartActivityResult;
}

ActivityRecord 用于维护一个 Activity 的相关信息,如任务栈、生命周期等。

startActivityUnchecked 在完成大部分初步检查并确认调用方拥有执行此操作的必要权限时启动 Activity,还确保在启动不成功时删除正在启动的 Activity:

// services/core/java/com/android/server/wm/ActivityStarter.java
private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
        IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
        int startFlags, ActivityOptions options, Task inTask,
        TaskFragment inTaskFragment,
        BalVerdict balVerdict,
        NeededUriGrants intentGrants, int realCallingUid) {
    int result = START_CANCELED;
    final Task startedActivityRootTask;
    final TransitionController transitionController = r.mTransitionController;
    Transition newTransition = transitionController.isShellTransitionsEnabled()
            ? transitionController.createAndStartCollecting(TRANSIT_OPEN) : null;
    RemoteTransition remoteTransition = r.takeRemoteTransition();
    try {
        mService.deferWindowLayout();
        transitionController.collect(r);
        try {
            Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "startActivityInner");
            result = startActivityInner(r, sourceRecord, voiceSession, voiceInteractor,
                    startFlags, options, inTask, inTaskFragment, balVerdict,
                    intentGrants, realCallingUid);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
            startedActivityRootTask = handleStartResult(r, options, result, newTransition,
                    remoteTransition);
        }
    } finally {
        mService.continueWindowLayout();
    }
    postStartActivityProcessing(r, result, startedActivityRootTask);

    return result;
}

startActivityInner 启动 Activity 并确定该 Activity 是否应添加到已经存在的 task 的顶部或向现有 activity 传递新 Intent:

// services/core/java/com/android/server/wm/ActivityStarter.java
int startActivityInner(final ActivityRecord r, ActivityRecord sourceRecord,
        IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
        int startFlags, ActivityOptions options, Task inTask,
        TaskFragment inTaskFragment, BalVerdict balVerdict,
        NeededUriGrants intentGrants, int realCallingUid) {
    setInitialState(r, options, inTask, inTaskFragment, startFlags, sourceRecord,
            voiceSession, voiceInteractor, balVerdict.getCode(), realCallingUid);

    ...
    // Compute if there is an existing task that should be used for.
    // Flag 为 FLAG_ACTIVITY_NEW_TASK,根据 computeTargetTask 计算需要返回 null,表示需要新的 Task
    final Task targetTask = reusedTask != null ? reusedTask : computeTargetTask();
    final boolean newTask = targetTask == null;// true
    mTargetTask = targetTask;

    computeLaunchParams(r, sourceRecord, targetTask);
    ...
    
    // Task 为 null,创建新的 ActivityRecord 为 null
    final ActivityRecord targetTaskTop = newTask
            ? null : targetTask.getTopNonFinishingActivity();
    if (targetTaskTop != null) {
        ...
    } else {
        mAddingToTask = true;
    }

    ...

    if (mTargetRootTask == null) {
        // 创建 Task
        mTargetRootTask = getOrCreateRootTask(mStartActivity, mLaunchFlags, targetTask,
                mOptions);
    }
    if (newTask) {
        final Task taskToAffiliate = (mLaunchTaskBehind && mSourceRecord != null)
                ? mSourceRecord.getTask() : null;
        // 将 ActivityRecord 与 Task 关联
        setNewTask(taskToAffiliate);
    } 
    ...

    if (!mAvoidMoveToFront && mDoResume) {
        logOnlyCreatorAllowsBAL(balVerdict, realCallingUid, newTask);
        // 移动到栈顶
        mTargetRootTask.getRootTask().moveToFront("reuseOrNewTask", targetTask);
        ...
    }

    ...
    if (mDoResume) {
        final com.android.server.wm.ActivityRecord topTaskActivity = startedTask.topRunningActivityLocked();
        if (!mTargetRootTask.isTopActivityFocusable()
                ...
        } else {
            ...
            // Activity resume
            mRootWindowContainer.resumeFocusedTasksTopActivities(
                    mTargetRootTask, mStartActivity, mOptions, mTransientLaunch);
        }
    }
    mRootWindowContainer.updateUserRootTask(mStartActivity.mUserId, mTargetRootTask);

    // Update the recent tasks list immediately when the activity starts
    mSupervisor.mRecentTasks.add(startedTask);
    mSupervisor.handleNonResizableTaskIfNeeded(startedTask,
            mPreferredWindowingMode, mPreferredTaskDisplayArea, mTargetRootTask);

    // If Activity's launching into PiP, move the mStartActivity immediately to pinned mode.
    // Note that mStartActivity and source should be in the same Task at this point.
    if (mOptions != null && mOptions.isLaunchIntoPip()
            && sourceRecord != null && sourceRecord.getTask() == mStartActivity.getTask()
            && balVerdict.allows()) {
        mRootWindowContainer.moveActivityToPinnedRootTask(mStartActivity,
                sourceRecord, "launch-into-pip");
    }

    mSupervisor.getBackgroundActivityLaunchController()
            .onNewActivityLaunched(mStartActivity);

    return START_SUCCESS;
}

startActivityInner 首先创建了 Task,接着将 ActivityRecord 挂载到创建的 Task 下,并移动 Task 到栈顶,最后调用 RootWindowContainer resumeFocusedTasksTopActivities 方法显示顶部的 Activity。

1.3 RootWindowContainer 显示顶部 Acitivity

RootWindowContainer 是窗口管理树的根节点,负责管理整个系统中所有窗口的层级关系和布局,以及处理窗口的生命周期和多屏幕支持。

看一下 RootWindowContainer 的 resumeFocusedTasksTopActivities,是实现 Activity 显示和切换的关键方法:

// services/core/java/com/android/server/wm/RootWindowContainer.java
boolean resumeFocusedTasksTopActivities(
        Task targetRootTask, ActivityRecord target, ActivityOptions targetOptions,
        boolean deferPause) {
    if (!mTaskSupervisor.readyToResume()) {
        return false;
    }

    boolean result = false;
    if (targetRootTask != null && (targetRootTask.isTopRootTaskInDisplayArea()
            || getTopDisplayFocusedRootTask() == targetRootTask)) {
        // targetRootTask 就是刚创建的目标 Task
        result = targetRootTask.resumeTopActivityUncheckedLocked(target, targetOptions,
                deferPause);
    }

    for (int displayNdx = getChildCount() - 1; displayNdx >= 0; --displayNdx) {
        final DisplayContent display = getChildAt(displayNdx);
        final boolean curResult = result;
        boolean[] resumedOnDisplay = new boolean[1];
        display.forAllRootTasks(rootTask -> {
            final ActivityRecord topRunningActivity = rootTask.topRunningActivity();
            if (!rootTask.isFocusableAndVisible() || topRunningActivity == null) {
                return;
            }
            if (rootTask == targetRootTask) {
                // Simply update the result for targetRootTask because the targetRootTask
                // had already resumed in above. We don't want to resume it again,
                // especially in some cases, it would cause a second launch failure
                // if app process was dead.
                resumedOnDisplay[0] |= curResult;
                return;
            }
            if (topRunningActivity.isState(RESUMED)
                    && topRunningActivity == rootTask.getDisplayArea().topRunningActivity()) {
                // Kick off any lingering app transitions form the MoveTaskToFront operation,
                // but only consider the top activity on that display.
                rootTask.executeAppTransition(targetOptions);
            } else {
                resumedOnDisplay[0] |= topRunningActivity.makeActiveIfNeeded(target);
            }
        });
        result |= resumedOnDisplay[0];
        if (!resumedOnDisplay[0]) {
            // In cases when there are no valid activities (e.g. device just booted or launcher
            // crashed) it's possible that nothing was resumed on a display. Requesting resume
            // of top activity in focused root task explicitly will make sure that at least home
            // activity is started and resumed, and no recursion occurs.
            final com.android.server.wm.Task focusedRoot = display.getFocusedRootTask();
            if (focusedRoot != null) {
                result |= focusedRoot.resumeTopActivityUncheckedLocked(target, targetOptions);
            } else if (targetRootTask == null) {
                result |= resumeHomeActivity(null /* prev */, "no-focusable-task",
                        display.getDefaultTaskDisplayArea());
            }
        }
    }

    return result;
}

继续调用 Task 的 resumeTopActivityUncheckedLocked 方法:

// services/core/java/com/android/server/wm/Task.java
@GuardedBy("mService")
boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options,
        boolean deferPause) {
    if (mInResumeTopActivity) {
        // Don't even start recursing.
        return false;
    }

    boolean someActivityResumed = false;
    try {
        // Protect against recursion.
        mInResumeTopActivity = true;

        if (isLeafTask()) {
            if (isFocusableAndVisible()) {
                someActivityResumed = resumeTopActivityInnerLocked(prev, options, deferPause);
            }
        } else {
            int idx = mChildren.size() - 1;
            while (idx >= 0) {
                final Task child = (Task) getChildAt(idx--);
                if (!child.isTopActivityFocusable()) {
                    continue;
                }
                if (child.getVisibility(null /* starting */)
                        != TASK_FRAGMENT_VISIBILITY_VISIBLE) {
                    if (child.topRunningActivity() == null) {
                        // Skip the task if no running activity and continue resuming next task.
                        continue;
                    }
                    // Otherwise, assuming everything behind this task should also be invisible.
                    break;
                }

                someActivityResumed |= child.resumeTopActivityUncheckedLocked(prev, options,
                        deferPause);
                // Doing so in order to prevent IndexOOB since hierarchy might changes while
                // resuming activities, for example dismissing split-screen while starting
                // non-resizeable activity.
                if (idx >= mChildren.size()) {
                    idx = mChildren.size() - 1;
                }
            }
        }

        // When resuming the top activity, it may be necessary to pause the top activity (for
        // example, returning to the lock screen. We suppress the normal pause logic in
        // {@link #resumeTopActivityUncheckedLocked}, since the top activity is resumed at the
        // end. We call the {@link ActivityTaskSupervisor#checkReadyForSleepLocked} again here
        // to ensure any necessary pause logic occurs. In the case where the Activity will be
        // shown regardless of the lock screen, the call to
        // {@link ActivityTaskSupervisor#checkReadyForSleepLocked} is skipped.
        final ActivityRecord next = topRunningActivity(true /* focusableOnly */);
        if (next == null || !next.canTurnScreenOn()) {
            checkReadyForSleep();
        }
    } finally {
        mInResumeTopActivity = false;
    }

    return someActivityResumed;
}
@GuardedBy("mService")
private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options,
        boolean deferPause) {
    if (!mAtmService.isBooting() && !mAtmService.isBooted()) {
        // Not ready yet!
        return false;
    }

    final ActivityRecord topActivity = topRunningActivity(true /* focusableOnly */);
    if (topActivity == null) {
        // There are no activities left in this task, let's look somewhere else.
        return resumeNextFocusableActivityWhenRootTaskIsEmpty(prev, options);
    }

    final boolean[] resumed = new boolean[1];
    final com.android.server.wm.TaskFragment topFragment = topActivity.getTaskFragment();
    resumed[0] = topFragment.resumeTopActivity(prev, options, deferPause);
    forAllLeafTaskFragments(f -> {
        if (topFragment == f) {
            return;
        }
        if (!f.canBeResumed(null /* starting */)) {
            return;
        }
        resumed[0] |= f.resumeTopActivity(prev, options, deferPause);
    }, true);
    return resumed[0];
}

接着调用了 Task 的 resumeTopActivity 方法:

final boolean resumeTopActivity(ActivityRecord prev, ActivityOptions options,
        boolean skipPause) {
    com.android.server.wm.ActivityRecord next = topRunningActivity(true /* focusableOnly */);
    if (next == null || !next.canResumeByCompat()) {
        return false;
    }

    next.delayedResume = false;

    if (!skipPause && !mRootWindowContainer.allPausedActivitiesComplete()) {
        // If we aren't skipping pause, then we have to wait for currently pausing activities.
        ProtoLog.v(WM_DEBUG_STATES, "resumeTopActivity: Skip resume: some activity pausing.");
        return false;
    }

    final com.android.server.wm.TaskDisplayArea taskDisplayArea = getDisplayArea();
    // If the top activity is the resumed one, nothing to do.
    if (mResumedActivity == next && next.isState(RESUMED)
            && taskDisplayArea.allResumedActivitiesComplete()) {
        // Ensure the visibility gets updated before execute app transition.
        taskDisplayArea.ensureActivitiesVisible(null /* starting */, 0 /* configChanges */,
                false /* preserveWindows */, true /* notifyClients */);
        // Make sure we have executed any pending transitions, since there
        // should be nothing left to do at this point.
        executeAppTransition(options);

        // In a multi-resumed environment, like in a freeform device, the top
        // activity can be resumed, but it might not be the focused app.
        // Set focused app when top activity is resumed. However, we shouldn't do it for a
        // same task because it can break focused state. (e.g. activity embedding)
        if (taskDisplayArea.inMultiWindowMode() && taskDisplayArea.mDisplayContent != null) {
            final ActivityRecord focusedApp = taskDisplayArea.mDisplayContent.mFocusedApp;
            if (focusedApp == null || focusedApp.getTask() != next.getTask()) {
                taskDisplayArea.mDisplayContent.setFocusedApp(next);
            }
        }
        ProtoLog.d(WM_DEBUG_STATES, "resumeTopActivity: Top activity "
                + "resumed %s", next);
        return false;
    }

    // If we are sleeping, and there is no resumed activity, and the top activity is paused,
    // well that is the state we want.
    if (mLastPausedActivity == next && shouldSleepOrShutDownActivities()) {
        // Make sure we have executed any pending transitions, since there
        // should be nothing left to do at this point.
        executeAppTransition(options);
        ProtoLog.d(WM_DEBUG_STATES, "resumeTopActivity: Going to sleep and"
                + " all paused");
        return false;
    }

    // Make sure that the user who owns this activity is started.  If not,
    // we will just leave it as is because someone should be bringing
    // another user's activities to the top of the stack.
    if (!mAtmService.mAmInternal.hasStartedUserState(next.mUserId)) {
        Slog.w(TAG, "Skipping resume of top activity " + next
                + ": user " + next.mUserId + " is stopped");
        return false;
    }

    // The activity may be waiting for stop, but that is no longer
    // appropriate for it.
    mTaskSupervisor.mStoppingActivities.remove(next);

    if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming " + next);

    mTaskSupervisor.setLaunchSource(next.info.applicationInfo.uid);

    ActivityRecord lastResumed = null;
    final Task lastFocusedRootTask = taskDisplayArea.getLastFocusedRootTask();
    if (lastFocusedRootTask != null && lastFocusedRootTask != getRootTaskFragment().asTask()) {
        // So, why aren't we using prev here??? See the param comment on the method. prev
        // doesn't represent the last resumed activity. However, the last focus stack does if
        // it isn't null.
        lastResumed = lastFocusedRootTask.getTopResumedActivity();
    }

    boolean pausing = !skipPause && taskDisplayArea.pauseBackTasks(next);
    if (mResumedActivity != null) {
        ProtoLog.d(WM_DEBUG_STATES, "resumeTopActivity: Pausing %s", mResumedActivity);
        pausing |= startPausing(mTaskSupervisor.mUserLeaving, false /* uiSleeping */,
                next, "resumeTopActivity");
    }
    if (pausing) {
        ProtoLog.v(WM_DEBUG_STATES, "resumeTopActivity: Skip resume: need to"
                + " start pausing");
        // At this point we want to put the upcoming activity's process
        // at the top of the LRU list, since we know we will be needing it
        // very soon and it would be a waste to let it get killed if it
        // happens to be sitting towards the end.
        if (next.attachedToProcess()) {
            next.app.updateProcessInfo(false /* updateServiceConnectionActivities */,
                    true /* activityChange */, false /* updateOomAdj */,
                    false /* addPendingTopUid */);
        } else if (!next.isProcessRunning()) {
            // Since the start-process is asynchronous, if we already know the process of next
            // activity isn't running, we can start the process earlier to save the time to wait
            // for the current activity to be paused.
            // 如果我们已经知道下一个活动的进程没有运行,我们可以提前启动该进程以节省等待当前活动暂停的时间。
            final boolean isTop = this == taskDisplayArea.getFocusedRootTask();
            mAtmService.startProcessAsync(next, false /* knownToBeDead */, isTop,
                    isTop ? HostingRecord.HOSTING_TYPE_NEXT_TOP_ACTIVITY
                            : HostingRecord.HOSTING_TYPE_NEXT_ACTIVITY);
        }
        if (lastResumed != null) {
            lastResumed.setWillCloseOrEnterPip(true);
        }
        return true;
    } else if (mResumedActivity == next && next.isState(RESUMED)
            && taskDisplayArea.allResumedActivitiesComplete()) {
        // It is possible for the activity to be resumed when we paused back stacks above if the
        // next activity doesn't have to wait for pause to complete.
        // So, nothing else to-do except:
        // Make sure we have executed any pending transitions, since there
        // should be nothing left to do at this point.
        executeAppTransition(options);
        ProtoLog.d(WM_DEBUG_STATES, "resumeTopActivity: Top activity resumed "
                + "(dontWaitForPause) %s", next);
        return true;
    }

    // If the most recent activity was noHistory but was only stopped rather
    // than stopped+finished because the device went to sleep, we need to make
    // sure to finish it as we're making a new activity topmost.
    if (shouldSleepActivities()) {
        mTaskSupervisor.finishNoHistoryActivitiesIfNeeded(next);
    }

    if (prev != null && prev != next && next.nowVisible) {
        // The next activity is already visible, so hide the previous
        // activity's windows right now so we can show the new one ASAP.
        // We only do this if the previous is finishing, which should mean
        // it is on top of the one being resumed so hiding it quickly
        // is good.  Otherwise, we want to do the normal route of allowing
        // the resumed activity to be shown so we can decide if the
        // previous should actually be hidden depending on whether the
        // new one is found to be full-screen or not.
        if (prev.finishing) {
            prev.setVisibility(false);
            if (DEBUG_SWITCH) {
                Slog.v(TAG_SWITCH, "Not waiting for visible to hide: " + prev
                        + ", nowVisible=" + next.nowVisible);
            }
        } else {
            if (DEBUG_SWITCH) {
                Slog.v(TAG_SWITCH, "Previous already visible but still waiting to hide: " + prev
                        + ", nowVisible=" + next.nowVisible);
            }
        }
    }

    try {
        mTaskSupervisor.getActivityMetricsLogger()
                .notifyBeforePackageUnstopped(next.packageName);
        mAtmService.getPackageManagerInternalLocked().notifyComponentUsed(
                next.packageName, next.mUserId,
                next.packageName, next.toString()); /* TODO: Verify if correct userid */
    } catch (IllegalArgumentException e) {
        Slog.w(TAG, "Failed trying to unstop package "
                + next.packageName + ": " + e);
    }

    // We are starting up the next activity, so tell the window manager
    // that the previous one will be hidden soon.  This way it can know
    // to ignore it when computing the desired screen orientation.
    boolean anim = true;
    final DisplayContent dc = taskDisplayArea.mDisplayContent;
    if (prev != null) {
        if (prev.finishing) {
            if (DEBUG_TRANSITION) {
                Slog.v(TAG_TRANSITION, "Prepare close transition: prev=" + prev);
            }
            if (mTaskSupervisor.mNoAnimActivities.contains(prev)) {
                anim = false;
                dc.prepareAppTransition(TRANSIT_NONE);
            } else {
                dc.prepareAppTransition(TRANSIT_CLOSE);
            }
            prev.setVisibility(false);
        } else {
            if (DEBUG_TRANSITION) {
                Slog.v(TAG_TRANSITION, "Prepare open transition: prev=" + prev);
            }
            if (mTaskSupervisor.mNoAnimActivities.contains(next)) {
                anim = false;
                dc.prepareAppTransition(TRANSIT_NONE);
            } else {
                dc.prepareAppTransition(TRANSIT_OPEN,
                        next.mLaunchTaskBehind ? TRANSIT_FLAG_OPEN_BEHIND : 0);
            }
        }
    } else {
        if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
        if (mTaskSupervisor.mNoAnimActivities.contains(next)) {
            anim = false;
            dc.prepareAppTransition(TRANSIT_NONE);
        } else {
            dc.prepareAppTransition(TRANSIT_OPEN);
        }
    }

    if (anim) {
        next.applyOptionsAnimation();
    } else {
        next.abortAndClearOptionsAnimation();
    }

    mTaskSupervisor.mNoAnimActivities.clear();

    if (next.attachedToProcess()) {
        if (DEBUG_SWITCH) {
            Slog.v(TAG_SWITCH, "Resume running: " + next + " stopped=" + next.mAppStopped
                    + " visibleRequested=" + next.isVisibleRequested());
        }

        // If the previous activity is translucent, force a visibility update of
        // the next activity, so that it's added to WM's opening app list, and
        // transition animation can be set up properly.
        // For example, pressing Home button with a translucent activity in focus.
        // Launcher is already visible in this case. If we don't add it to opening
        // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a
        // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.
        final boolean lastActivityTranslucent = inMultiWindowMode()
                || mLastPausedActivity != null && !mLastPausedActivity.occludesParent();

        // This activity is now becoming visible.
        if (!next.isVisibleRequested() || next.mAppStopped || lastActivityTranslucent) {
            next.app.addToPendingTop();
            next.setVisibility(true);
        }

        // schedule launch ticks to collect information about slow apps.
        next.startLaunchTickingLocked();

        ActivityRecord lastResumedActivity =
                lastFocusedRootTask == null ? null
                        : lastFocusedRootTask.getTopResumedActivity();
        final ActivityRecord.State lastState = next.getState();

        mAtmService.updateCpuStats();

        ProtoLog.v(WM_DEBUG_STATES, "Moving to RESUMED: %s (in existing)", next);

        next.setState(RESUMED, "resumeTopActivity");

        // Have the window manager re-evaluate the orientation of
        // the screen based on the new activity order.
        boolean notUpdated = true;

        // Activity should also be visible if set mLaunchTaskBehind to true (see
        // ActivityRecord#shouldBeVisibleIgnoringKeyguard()).
        if (shouldBeVisible(next)) {
            // We have special rotation behavior when here is some active activity that
            // requests specific orientation or Keyguard is locked. Make sure all activity
            // visibilities are set correctly as well as the transition is updated if needed
            // to get the correct rotation behavior. Otherwise the following call to update
            // the orientation may cause incorrect configurations delivered to client as a
            // result of invisible window resize.
            // TODO: Remove this once visibilities are set correctly immediately when
            // starting an activity.
            notUpdated = !mRootWindowContainer.ensureVisibilityAndConfig(next, getDisplayId(),
                    false /* deferResume */);
        }

        if (notUpdated) {
            // The configuration update wasn't able to keep the existing
            // instance of the activity, and instead started a new one.
            // We should be all done, but let's just make sure our activity
            // is still at the top and schedule another run if something
            // weird happened.
            ActivityRecord nextNext = topRunningActivity();
            ProtoLog.i(WM_DEBUG_STATES, "Activity config changed during resume: "
                    + "%s, new next: %s", next, nextNext);
            if (nextNext != next) {
                // Do over!
                mTaskSupervisor.scheduleResumeTopActivities();
            }
            if (!next.isVisibleRequested() || next.mAppStopped) {
                next.setVisibility(true);
            }
            next.completeResumeLocked();
            return true;
        }

        try {
            final IApplicationThread appThread = next.app.getThread();
            final ClientTransaction transaction = Flags.bundleClientTransactionFlag()
                    ? null
                    : ClientTransaction.obtain(appThread);
            // Deliver all pending results.
            final ArrayList<ResultInfo> a = next.results;
            if (a != null) {
                final int size = a.size();
                if (!next.finishing && size > 0) {
                    if (DEBUG_RESULTS) {
                        Slog.v(TAG_RESULTS, "Delivering results to " + next + ": " + a);
                    }
                    final ActivityResultItem activityResultItem = ActivityResultItem.obtain(
                            next.token, a);
                    if (transaction == null) {
                        mAtmService.getLifecycleManager().scheduleTransactionItem(
                                appThread, activityResultItem);
                    } else {
                        transaction.addCallback(activityResultItem);
                    }
                }
            }

            if (next.newIntents != null) {
                final NewIntentItem newIntentItem = NewIntentItem.obtain(
                        next.token, next.newIntents, true /* resume */);
                if (transaction == null) {
                    mAtmService.getLifecycleManager().scheduleTransactionItem(
                            appThread, newIntentItem);
                } else {
                    transaction.addCallback(newIntentItem);
                }
            }

            // Well the app will no longer be stopped.
            // Clear app token stopped state in window manager if needed.
            next.notifyAppResumed();

            EventLogTags.writeWmResumeActivity(next.mUserId, System.identityHashCode(next),
                    next.getTask().mTaskId, next.shortComponentName);

            mAtmService.getAppWarningsLocked().onResumeActivity(next);
            final int topProcessState = mAtmService.mTopProcessState;
            next.app.setPendingUiCleanAndForceProcessStateUpTo(topProcessState);
            next.abortAndClearOptionsAnimation();
            final ResumeActivityItem resumeActivityItem = ResumeActivityItem.obtain(
                    next.token, topProcessState, dc.isNextTransitionForward(),
                    next.shouldSendCompatFakeFocus());
            if (transaction == null) {
                mAtmService.getLifecycleManager().scheduleTransactionItem(
                        appThread, resumeActivityItem);
            } else {
                transaction.setLifecycleStateRequest(resumeActivityItem);
                mAtmService.getLifecycleManager().scheduleTransaction(transaction);
            }

            ProtoLog.d(WM_DEBUG_STATES, "resumeTopActivity: Resumed %s", next);
        } catch (Exception e) {
            // Whoops, need to restart this activity!
            ProtoLog.v(WM_DEBUG_STATES, "Resume failed; resetting state to %s: "
                    + "%s", lastState, next);
            next.setState(lastState, "resumeTopActivityInnerLocked");

            // lastResumedActivity being non-null implies there is a lastStack present.
            if (lastResumedActivity != null) {
                lastResumedActivity.setState(RESUMED, "resumeTopActivityInnerLocked");
            }

            Slog.i(TAG, "Restarting because process died: " + next);
            if (!next.hasBeenLaunched) {
                next.hasBeenLaunched = true;
            } else if (SHOW_APP_STARTING_PREVIEW && lastFocusedRootTask != null
                    && lastFocusedRootTask.isTopRootTaskInDisplayArea()) {
                next.showStartingWindow(false /* taskSwitch */);
            }
            mTaskSupervisor.startSpecificActivity(next, true, false);
            return true;
        }

        // From this point on, if something goes wrong there is no way
        // to recover the activity.
        try {
            next.completeResumeLocked();
        } catch (Exception e) {
            // If any exception gets thrown, toss away this
            // activity and try the next one.
            Slog.w(TAG, "Exception thrown during resume of " + next, e);
            next.finishIfPossible("resume-exception", true /* oomAdj */);
            return true;
        }
    } else {
        // Whoops, need to restart this activity!
        if (!next.hasBeenLaunched) {
            next.hasBeenLaunched = true;
        } else {
            if (SHOW_APP_STARTING_PREVIEW) {
                next.showStartingWindow(false /* taskSwich */);
            }
            if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting: " + next);
        }
        ProtoLog.d(WM_DEBUG_STATES, "resumeTopActivity: Restarting %s", next);
        mTaskSupervisor.startSpecificActivity(next, true, true);
    }

    return true;
}

resumeTopActivityInnerLocked() 中先对上一个 Activity 执行了 pause 操作,然后判断当前要启动的 Activity 是否已经启动了,如果已经启动了,发送 ResumeActivityItem 事务给客户端,执行 resume 流程,如果没有启动,继续调用 ActivityStackSupervisor 的 startSpecificActivity() 方法。这里注意下,调用 startSpecificActivity() 方法前调用了 next.showStartingWindow() 来展示一个 window,这就是冷启动时出现白屏的原因。我们继续来看 ActivityStackSupervisor 的 startSpecificActivity() 方法:

void startSpecificActivity(ActivityRecord r, boolean andResume, boolean checkConfig) {
    // Is this activity's application already running?
    final WindowProcessController wpc =
            mService.getProcessController(r.processName, r.info.applicationInfo.uid);

    boolean knownToBeDead = false;
    if (wpc != null && wpc.hasThread()) {
        try {
            realStartActivityLocked(r, wpc, andResume, checkConfig);
            return;
        } catch (RemoteException e) {
            Slog.w(TAG, "Exception when starting activity "
                    + r.intent.getComponent().flattenToShortString(), e);
        }

        // If a dead object exception was thrown -- fall through to
        // restart the application.
        knownToBeDead = true;
        // Remove the process record so it won't be considered as alive.
        mService.mProcessNames.remove(wpc.mName, wpc.mUid);
        mService.mProcessMap.remove(wpc.getPid());
    } else if (ActivityTaskManagerService.isSdkSandboxActivityIntent(
            mService.mContext, r.intent)) {
        Slog.e(TAG, "Abort sandbox activity launching as no sandbox process to host it.");
        r.finishIfPossible("No sandbox process for the activity", false /* oomAdj */);
        r.launchFailed = true;
        r.detachFromProcess();
        return;
    }

    r.notifyUnknownVisibilityLaunchedForKeyguardTransition();

    final boolean isTop = andResume && r.isTopRunningActivity();
    mService.startProcessAsync(r, knownToBeDead, isTop,
            isTop ? HostingRecord.HOSTING_TYPE_TOP_ACTIVITY
                    : HostingRecord.HOSTING_TYPE_ACTIVITY);
}
// java/com/android/server/wm/TaskFragment.java
void startProcessAsync(ActivityRecord activity, boolean knownToBeDead, boolean isTop,
        String hostingType) {
    try {
        if (Trace.isTagEnabled(TRACE_TAG_WINDOW_MANAGER)) {
            Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "dispatchingStartProcess:"
                    + activity.processName);
        }
        // Post message to start process to avoid possible deadlock of calling into AMS with the
        // ATMS lock held.
        // 通过发送消息来启动进程,避免在持有ATMS锁的情况下调用AMS可能发生的死锁
        final Message m = PooledLambda.obtainMessage(ActivityManagerInternal::startProcess,
                mAmInternal, activity.processName, activity.info.applicationInfo, knownToBeDead,
                isTop, hostingType, activity.intent.getComponent());
        mH.sendMessage(m);
    } finally {
        Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
    }
}

startSpecificActivity() 方法中通过 wpc.hasThread() 来判断 activity 所在的应用是否已经在运行,其内部是通过 IApplicationThread 是否已经被赋值来判断的,如果为 true,走 ActivityStackSupervisor 的realStartActivityLocked() 方法,即普通 Activity 的启动流程;否则需要调用 ATMS 的 startProcessAsync() 来创建应用进程。

此时应用进程还不存在,需要调用 ATMS 的 startProcessAsync() 来创建应用进程:

这里使用Handler发送消息来启动进程,获取Message对象跟我们平时的使用方式不太一样,这里用到PooledLambda的obtainMessage()方法,实际就是调用了ActivityManagerInternal的startProcess()方法。

ActivityManagerInternal是一个抽象类,它是ActivityManager本地系统服务接口,代码如下:

public abstract class ActivityManagerInternal {

    /** Starts a given process. */
    public abstract void startProcess(String processName, ApplicationInfo info,
            boolean knownToBeDead, boolean isTop, String hostingType, ComponentName hostingName);

}

ActivityManagerInternal的实现类为AMS的内部类LocalService,这样就把创建进程的请求传递给了AMS去处理。

LocalServices可以理解为是一个公开缓存池,内部使用ArrayMap来存储本地服务对象。SystemServer进程中每个服务都可以通过LocalServices的addService()方法注册到LocalServices中,需要使用时通过LocalServices的getService()获取注册的本地服务。