在startHomeActivityLocked()方法中,起首通过getHomeIntent()方法,获取到要启动的HomeActivity的intent对象,此中mTopAction默以为INTENT.ACTION_MAIN,并添加CATEGORY_HOME的category标记;
得到Intent对象,通过PackageManager去获取对应符合的Activity,获取对应的ActivityInfo,并获取对应的进程纪录,此时对应的进程还没启动,反面继续实行,为intent添加FLAG_ACTIVITY_NEW_TASK启动参数,开启新栈,随后调用ActivityStartController类的startHomeActivity()方法去实行启动;
boolean startHomeActivityLocked(int userId, String reason) { ... Intent intent = getHomeIntent(); ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId); 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, aInfo.applicationInfo.uid, true); if (app == null || app.instr == null) { intent.setFlags(intent.getFlags() | FLAG_ACTIVITY_NEW_TASK); final int resolvedUserId = UserHandle.getUserId(aInfo.applicationInfo.uid); // For ANR debugging to verify if the user activity is the one that actually launched. final String myReason = reason + ":" + userId + ":" + resolvedUserId; mActivityStartController.startHomeActivity(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) { intent.addCategory(Intent.CATEGORY_HOME); } return intent;}2.2.2、启动Launcher
在startHomeActivity()方法中,调用obtainStarter()方法获取到一个ActivityStarter对象,setCallingUid()方法设置当前调用的Uid=0,然后实行其execute()方法;
void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) { mSupervisor.moveHomeStackTaskToTop(reason); mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason) .setOutActivity(tmpOutRecord) .setCallingUid(0) .setActivityInfo(aInfo) .execute(); mLastHomeActivityStartRecord = tmpOutRecord[0]; 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(); }}在ActivityStarter的execute()方法中,mayWait默以为false,实行startActivity()方法;
int execute() { try { // TODO(b/64750076): Look into passing request directly to these methods to allow // for transactional diffs and preprocessing. if (mRequest.mayWait) { return startActivityMayWait(mRequest.caller, mRequest.callingUid, ...); } else { return startActivity(mRequest.caller, mRequest.intent, mRequest.ephemeralIntent, ...); } } finally { onExecutionComplete(); }}这里进入了Activity的启动流程,Launcher自己就是一个体系APP,用于表现桌面等,LauncherApp启动之后会实行其生命周期方法初始化桌面结构;
2.3、初始化桌面图标
2.3.1、实行onCreate()方法
@Overrideprotected void onCreate(Bundle savedInstanceState) { ... LauncherAppState app = LauncherAppState.getInstance(this); ...}获取LauncherAppState,通过LauncherAppState的getInstance()方法获取,该方法内里会判断当前线程是否为主线程,在主线程时还会直接new出对象,不在主线程时,通过MainThreadExecutor的submit()方法向主线程提交一个任务去获取该对象;
// We do not need any synchronization for this variable as its only written on UI thread.private static LauncherAppState INSTANCE;public static LauncherAppState getInstance(final Context context) { if (INSTANCE == null) { if (Looper.myLooper() == Looper.getMainLooper()) { INSTANCE = new LauncherAppState(context.getApplicationContext()); } else { try { return new MainThreadExecutor().submit(new Callable<LauncherAppState>() { @Override public LauncherAppState call() throws Exception { return LauncherAppState.getInstance(context); } }).get(); } catch (InterruptedException|ExecutionException e) { throw new RuntimeException(e); } } } return INSTANCE;}2.3.2、读取安装APP信息