Android Framework实战视频--SystemServer启动FallbackHome篇
Posted learnframework
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Framework实战视频--SystemServer启动FallbackHome篇相关的知识,希望对你有一定的参考价值。
课程答疑和新课信息:QQ交流群:422901085进行课程讨论
FrameWork入门课视频链接:https://edu.csdn.net/course/detail/30298
FrameWork实战课1视频链接:https://edu.csdn.net/course/detail/30275
专题博客系列:
Android 8.1 zygote 启动过程源码
Android Framework实战视频–Zygote的fork进程篇
Android Framework实战视频–SystemServer启动篇
Android Framework实战视频–SystemServer启动FallbackHome篇
Android Framework实战视频–FallbackHome进程启动及Activity启动篇
Android Framework实战视频–FallbackHome结束启动Launcher篇
android Framework实战视频–SystemServer启动FallbackHome篇
Android 8.1代码分析FallbackHome的启动:
前面已经讲解到了在startOtherService里面不仅仅会start系统服务,等系统服务都启动完成后,还会调用各个Service的systemReady方法,这里需要分析首个Ativity的启动,即常见的“Launcher”,当然是从ActivityManagerService开始分析:
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
//省略。。
synchronized(this) {
//省略。。
synchronized (this) {
//省略。。
startHomeActivityLocked(currentUserId, "systemReady");
//省略。。
mStackSupervisor.resumeFocusedStackTopActivityLocked();
//省略。。
}
}
这里对大部分不是分析的核心代码进行了省略,这里看到了startHomeActivityLocked(currentUserId, “systemReady”);被调用,从名字既可以看得出他是要启动Home类型的Activtiy,常见的Launcher就是一种Home类型的Activity,但这里其实并不是Launcher,而是设置中的FallbackHome类型的,它也是一个Home类型的Activity,这里FallbackHome是google新加入的,主要就是因为涉及整个android系统的加密等原因,系统在还没有完全解锁前,不可以启动Launcher,因为Launcher中明显和各个第三方应用耦合较多(比如桌面可能显示着一堆的各个应用的Widget),如果直接Launcher作为FallbackHome启动,相对就会要求Launcher依赖的应用也是支持直接解密类型,那就肯定不现实。所以就先启动了FallbackHome一个什么也不显示的界面来作为启动真正Launcher的一个过度。
那来分析一下,到底是怎么启动FallbackHome,又是凭什么确定就是FallbackHome?接下来继续分析startHomeActivityLocked代码:
boolean startHomeActivityLocked(int userId, String reason) {
//省略
Intent intent = getHomeIntent();//获取Home类型的Intent
ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);//通过PackageManagerService查询出目前系统中可以匹配这个intent的ActivtiyInfo
if (aInfo != null) {
intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
aInfo = new ActivityInfo(aInfo);
aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
//判断进程是否启动
ProcessRecord app = getProcessRecordLocked(aInfo.processName,
aInfo.applicationInfo.uid, true);
if (app == null || app.instr == null) {
//省略。。
mActivityStarter.startHomeActivityLocked(intent, aInfo, myReason);
}
}
//省略。。
return true;
}
Intent getHomeIntent() {
Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
intent.setComponent(mTopComponent);
intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
//这里是匹配HomeIntent的核心
intent.addCategory(Intent.CATEGORY_HOME);
}
return intent;
}
这里的getHomeIntent主要就是在Intent中增加 intent.addCategory(Intent.CATEGORY_HOME);即有category为Intent.CATEGORY_HOME的Intent
这里面比较简单,就是获取了HomeIntent后,然后利用intent进行ActivityInfo获取,然后判断是否还没有启动过该进程,没有则继续调用mActivityStarter.startHomeActivityLocked(intent, aInfo, myReason);调用ActivityStarter里面的startHomeActivityLocked
void startHomeActivityLocked(Intent intent, ActivityInfo aInfo, String reason) {
mSupervisor.moveHomeStackTaskToTop(reason);
mLastHomeActivityStartResult = startActivityLocked(null /*caller*/, intent,
null /*ephemeralIntent*/, null /*resolvedType*/, aInfo, null /*rInfo*/,
null /*voiceSession*/, null /*voiceInteractor*/, null /*resultTo*/,
null /*resultWho*/, 0 /*requestCode*/, 0 /*callingPid*/, 0 /*callingUid*/,
null /*callingPackage*/, 0 /*realCallingPid*/, 0 /*realCallingUid*/,
0 /*startFlags*/, null /*options*/, false /*ignoreTargetSecurity*/,
false /*componentSpecified*/, mLastHomeActivityStartRecord /*outActivity*/,
null /*inTask*/, "startHomeActivity: " + reason);
if (mSupervisor.inResumeTopActivity) {
// If we are in resume section already, home activity will be initialized, but not
// resumed (to avoid recursive resume) and will stay that way until something pokes it
// again. We need to schedule another resume.
mSupervisor.scheduleResumeTopActivities();
}
}
这里又调用到了startActivityLocked方法:
int startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
ActivityRecord[] outActivity, TaskRecord inTask, String reason) {
//省略
mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,
aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,
callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,
inTask);
//省略
return mLastStartActivityResult != START_ABORTED ? mLastStartActivityResult : START_SUCCESS;
}
又调用到了startActivity方法:
private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
ActivityRecord[] outActivity, TaskRecord inTask) {
//省略
ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,
callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),
resultRecord, resultWho, requestCode, componentSpecified, voiceSession != null,
mSupervisor, options, sourceRecord);
//省略
return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true,
options, inTask, outActivity);
}
又调用 startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true,options, inTask, outActivity);
private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
ActivityRecord[] outActivity) {
//省略
mService.mWindowManager.deferSurfaceLayout();
result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,
startFlags, doResume, options, inTask, outActivity);
//省略
return result;
}
这里又调用到startActivityUnchecked方法:
private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
//省略
if (mDoResume) {
//省略
} else {
//省略
mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,
mOptions);
}
}
//省略
return START_SUCCESS;
}
这里用会调用到ActivityStackSupervisor的resumeFocusedStackTopActivityLocked方法:
mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,mOptions);
boolean resumeFocusedStackTopActivityLocked(
ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
//省略
if (targetStack != null && isFocusedStack(targetStack)) {
return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
}
//省略
return false;
}
这里又会调用到targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
frameworks/base/services/core/java/com/android/server/am/ActivityStack.java
boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
//省略
try {
// Protect against recursion.
mStackSupervisor.inResumeTopActivity = true;
result = resumeTopActivityInnerLocked(prev, options);
} finally {
mStackSupervisor.inResumeTopActivity = false;
}
//省略
return result;
}
又会调用到resumeTopActivityInnerLocked:
private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
//省略
mStackSupervisor.startSpecificActivityLocked(next, true, true);
//省略
return true;
}
最后又会调回ActivityStackSupervisor的startSpecificActivityLocked(next, true, true):
void startSpecificActivityLocked(ActivityRecord r,
boolean andResume, boolean checkConfig) {
//省略
mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
"activity", r.intent.getComponent(), false, false, true);
}
这里又会调用ActivityManagerService的startProcessLocked方法,startProcessLocked也有几个重载最后调到如下方法:
final ProcessRecord startProcessLocked(String processName, ApplicationInfo info,
boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName,
boolean allowWhileBooting, boolean isolated, int isolatedUid, boolean keepIfLarge,
String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) {
long startTime = SystemClock.elapsedRealtime();
//省略
if (app == null) {
app = newProcessRecordLocked(info, processName, isolated, isolatedUid);
}
//省略
startProcessLocked(
app, hostingType, hostingNameStr, abiOverride, entryPoint, entryPointArgs);
//省略
return (app.pid != 0) ? app : null;
}
刚开始进程ProcessRecord app肯定位null所以newProcessRecordLocked会构造一个ProcessRecord,再次调用 startProcessLocked(
app, hostingType, hostingNameStr, abiOverride, entryPoint, entryPointArgs); 方法
private final void startProcessLocked(ProcessRecord app, String hostingType,
String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs) {
//省略。。
boolean isActivityProcess = (entryPoint == null);
//这里entryPoint传递的是null,所以赋值为entryPoint = "android.app.ActivityThread"
if (entryPoint == null) entryPoint = "android.app.ActivityThread";
//省略
ProcessStartResult startResult;
if (hostingType.equals("webview_service")) {
startResult = startWebView(entryPoint,
app.processName, uid, uid, gids, debugFlags, mountExternal,
app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
app.info.dataDir, null, entryPointArgs);
} else {
//调用process的start方法来启动进程
startResult = Process.start(entryPoint,
app.processName, uid, uid, gids, debugFlags, mountExternal,
app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
app.info.dataDir, invokeWith, entryPointArgs);
}
//省略
}
以上是关于Android Framework实战视频--SystemServer启动FallbackHome篇的主要内容,如果未能解决你的问题,请参考以下文章
Android Framework实战视频--SystemServer启动篇
Android Framework实战视频--FallbackHome结束启动Launcher篇
Android Framework实战视频--系统SystemServer启动篇
Android Framework实战视频--FallbackHome结束启动Launcher篇