2.2 Launcher 启动
接上边,我们直接跳到SystemServer类AMS相关代码,它会执行下面这行代码:
ActivityManagerService.self().systemReady(new Runnable() {});
然后再看systemReady函数的实现代码:
public void systemReady(final Runnable goingCallback) {
synchronized(this) {
......//此处省略n行代码
mStackSupervisor.resumeTopActivitiesLocked();
}
}
上面的关键函数就是resumeTopActivitiesLocked,接下来会进行一系列的函数跳转,请系紧安全带,要开车了:
boolean resumeTopActivitiesLocked() {
return resumeTopActivitiesLocked(null, null, null);
}
boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
Bundle targetOptions) {
if (targetStack == null) {
targetStack = getFocusedStack();
}
boolean result = false;
for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
final ActivityStack stack = mStacks.get(stackNdx);
if (isFrontStack(stack)) {
if (stack == targetStack) {
result = stack.resumeTopActivityLocked(target, targetOptions);
} else {
stack.resumeTopActivityLocked(null);
}
}
}
return result;
}
final boolean resumeTopActivityLocked(ActivityRecord prev) {
return resumeTopActivityLocked(prev, null);
}
final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
......\\此处省略n行代码
mStackSupervisor.resumeHomeActivity(prev);
}
boolean resumeHomeActivity(ActivityRecord prev) {
moveHomeToTop();
if (prev != null) {
prev.task.mOnTopOfHome = false;
}
ActivityRecord r = mHomeStack.topRunningActivityLocked(null);
if (r != null && r.isHomeActivity()) {
mService.setFocusedActivityLocked(r);
return resumeTopActivitiesLocked(mHomeStack, prev, null);
}
// For fastplay & boot video
Slog.i(TAG, "resumeHomeActivity(): mIsHomeActivityStarted=" + mIsHomeActivityStarted);
if (!mIsHomeActivityStarted) {
Slog.i(TAG, "resumeHomeActivity(): mProbeThread.getState()=" + mProbeThread.getState());
if (!mProbeThread.isAlive()
&& (mProbeThread.getState() != Thread.State.TERMINATED)) {
mProbeThread.start();
return true;
} else {
Slog.w(TAG, "resumeHomeActivity(): mProbeThread is not needed to start again");
return false;
}
}
return mService.startHomeActivityLocked(mCurrentUser);
}
函数resumeTopActivitiesLocked经过上面一系列的函数调用,会走到startHomeActivityLocked这个函数:
boolean startHomeActivityLocked(int userId) {
if (mHeadless) {
// Added because none of the other calls to ensureBootCompleted seem to fire
// when running headless.
ensureBootCompleted();
return false;
}
if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
&& mTopAction == null) {
// We are running in factory test mode, but unable to find
// the factory test app, so just sit around displaying the
// error message and don't try to start anything.
return false;
}
Intent intent = getHomeIntent(); //1
ActivityInfo aInfo =
resolveActivityInfo(intent, STOCK_PM_FLAGS, userId); //2
if (aInfo != null) {
intent.setComponent(new ComponentName(
aInfo.applicationInfo.packageName, aInfo.name));
// Don't do this if the home app is currently being
// instrumented.
aInfo = new ActivityInfo(aInfo);
aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
ProcessRecord app = getProcessRecordLocked(aInfo.processName, //3
aInfo.applicationInfo.uid, true);
if (app == null || app.instrumentationClass == null) { //4
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mStackSupervisor.startHomeActivity(intent, aInfo); //5
}
}
return true;
}
注释1处的getHomeIntent函数看一下做了什么事:
Intent getHomeIntent() {
Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
intent.setComponent(mTopComponent);
if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
intent.addCategory(Intent.CATEGORY_HOME);
}
return intent;
}
可以看到他有添加一个Category属性为Intent.CATEGORY_HOME
而注释3是去查看是否有这个app的进程信息,没有的话才会才会走注释5处的代码启动Launcher,此时的aInfo为com.android.launcher2.Launcher。