一.launcher启动流程
对应的 apk为Launcher3QuickStep.apk ,对应的路径为packages/app/Launcher
打开AndroidManifest.xml,主界面为com.android.launcher3.Launcher
查看开机log: Loaded persisted task位于frameworks\base\services\core\java\com\android\server\am\RecentTasks.java中的loadPersistedTaskIdsForUserLocked,在此处添加堆栈打印Log.i(TAG, Log.getStackTraceString(new Throwable()));
此时log打印如下
1.为啥第一次获取的是FallbackHome
根据这个逻辑分析代码流程
在am.ActivityManagerService.systemReady中传进的参数如下
startHomeActivityLocked(currentUserId, “systemReady”);
查看startHomeActivityLocked, 会通过Intent intent = getHomeIntent();获取定义为home类型的activity,第一次获取的是FallbackHome, ,第二次为launcher
boolean startHomeActivityLocked(int userId, String reason) {
if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
&& mTopAction == null) {
return false;
}
Intent intent = getHomeIntent();//筛选 <category android:name="android.intent.category.HOME"的acticity
ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);//具体选择
mActivityStartController.startHomeActivity(intent, aInfo, myReason);
}
} else {
Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
}
return true;
}
添加的log打印如下,可以知道getHomeIntent并没有获取到具体的intent ,而是通过resolveActivityInfo筛选
resolveActivityInfo
–》AppGlobals.getPackageManager().resolveIntent //PackageManagerService
–》resolveIntentInternal
核心代码如下
private ResolveInfo resolveIntentInternal(Intent intent, String resolvedType,
int flags, int userId, boolean resolveForStart, int filterCallingUid) {
final List<ResolveInfo> query = queryIntentActivitiesInternal(intent, resolvedType,
flags, filterCallingUid, userId, resolveForStart, true /*allowDynamicSplits*/);
final ResolveInfo bestChoice =
chooseBestActivity(intent, resolvedType, flags, query, userId);
return bestChoice;
} finally {
Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
}
}
在此处添加打印,打印获取到的query和返回的bestChoice,其结果如下:,选择FallbackHome时只查询到了FallbackHome,Launcher还没起来
2.FallbackHome中逻辑
在FallbackHome中大致逻辑如下,会对ACTION_USER_UNLOCKED进行监听,在maybeFinish中监听launcher是否启动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_USER_UNLOCKED)); //对ACTION_USER_UNLOCKED监听
maybeFinish(); //对launcher是否起来监听
}
protected void onResume() {
super.onResume();
if (mProvisioned) {
mHandler.postDelayed(mProgressTimeoutRunnable, PROGRESS_TIMEOUT);//mProgressTimeoutRunnable显示phone is starting 动画
}
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) { //收到ACTION_USER_UNLOCKED
maybeFinish();
}
};
private void maybeFinish() {
if (getSystemService(UserManager.class).isUserUnlocked()) {
final Intent homeIntent = new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME);
final ResolveInfo homeInfo = getPackageManager().resolveActivity(homeIntent, 0);//找到home类型的activity
if (Objects.equals(getPackageName(), homeInfo.activityInfo.packageName)) {//是自己本身
if (UserManager.isSplitSystemUser()
&& UserHandle.myUserId() == UserHandle.USER_SYSTEM) {
// This avoids the situation where the system user has no home activity after
// SUW and this activity continues to throw out warnings. See b/28870689.
return;
}
Log.d(TAG, "User unlocked but no home; let's hope someone enables one soon?");
mHandler.sendEmptyMessageDelayed(0, 500);//每隔0.5s发一次消息监听launcher是否起来了
} else {
Log.d(TAG, "User unlocked and real home found; let's go!");
getSystemService(PowerManager.class).userActivity(
SystemClock.uptimeMillis(), false);//找到launcher开始启动
finish();
}
}
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {//处理自己发送的消息
maybeFinish();
}
};
谁会发送ACTION_USER_UNLOCKED呢,全搜发现在UserController.java中的finishUserUnlocked中发送,在此处加上堆栈打印,可以知道UserController收到了消息SYSTEM_USER_UNLOCK_MSG会处理,该消息来自finishUserUnlocking
finishUserUnlocking来自unlockUserCleared,在unlockUserCleared添加打印,其结果如下,在WindowManagerService中处理CHECK_IF_BOOT_ANIMATION_FINISHED消息时调用
具体分析CHECK_IF_BOOT_ANIMATION_FINISHED,该消息是checkBootAnimationCompleteLocked发送,继续添加打印,发现是处理消息ENABLE_SCREEN
消息ENABLE_SCREEN来自enableScreenIfNeededLocked,继续添加打印,发现是PhoneWindowManager 处理消息MSG_WINDOW_MANAGER_DRAWN_COMPLETE,位于PhoneWindowManager的run方法
继续添加堆栈打印,发现是WindowManagerService处理消息ALL_WINDOWS_DRAWN
ALL_WINDOWS_DRAWN来自checkDrawnWindowsLocked,继续添加打印,发现来自WindowSurfacePlacer的构造方法
而WindowSurfacePlacer由WindowManagerService的构造方法创建,WindowManagerService由SystemServer中的startOtherServices创建
总结,先启动FallbackHome,在WindowManagerService中监听开机动画是否完成,当开机动画完成,通过一系列步骤发送ACTION_USER_UNLOCKED到FallbackHome,FallbackHome再监听launcher是否已经Create,若是则启动launcher主界面