1、Launcher大體佈局架構
2、Launcher3 主要目錄解析
allapps 目錄:主要存放主菜單界面相關代碼。
anim目錄:存放動畫相關代碼,主要是動畫基類代碼。
badge目錄:主要存放圖標標識相關代碼,例如通知小圓點等
compat目錄:主要存放解決兼容性相關的代碼。
config目錄:主要配置Launcher相關功能的宏開關,目前Launcher原生新增的功能宏開關都在這個目錄。
dragndrop目錄:主要存放拖拽相關操作的代碼
dynamicui目錄:主要存放新增功能桌面主題效果跟隨壁紙顏色自適應相關的代碼。
graphics目錄:主要存放處理圖標大小、顏色、自適應等相關的代碼
model目錄:存放Launcher加載流程相關模塊化的代碼
notification目錄:存放通知相關的代碼
pageindicators目錄:存放桌面頁面指示器相關的代碼
popu目錄:存放長按圖標顯示彈出框相關的代碼
provider目錄:存放Launcher數據庫相關的代碼
qsb目錄:存放搜索功能相關的代碼
shortcuts目錄:存放桌面所屬應用某些功能的快捷圖標相關的代碼。
3、Launcher3 通電後啓動流程分析
Launcher的啓動是在ActivityManagerService中完成的
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
...
startHomeActivityLocked(currentUserId, "systemReady");
.....
}
在systemReady中會執行startHomeActivityLocked方法
boolean startHomeActivityLocked(int userId, String reason) {
if (mFactoryTest == FactoryTest.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();
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() | Intent.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;
mActivityStarter.startHomeActivityLocked(intent, aInfo, myReason);
}
} else {
Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
}
return true;
}
在中間會執行getHomeIntent獲取Intent
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;
}
這其中的mTopAction和mTopComponent,在工廠模式(不是設計模式的那個,而是前面代碼裏的factory test mode,應該是用於工廠生產使用的)下會被指定爲其他內容,這個我們不需要關心。普通情況下就是ACTION_MAIN和null。那麼category指定爲CATEGORY_HOME後,查詢的就是在Manifest中聲明瞭該category的應用了。Launcher應用都會在AndroidManifest文件中聲明 <category android:name="android.intent.category.HOME" />。但是在這一步時,系統並沒有直接啓動Launcher3.
在Settings中的FallbackHome同樣聲明瞭CATEGORY_HOME。這個就涉及到DirectBoot模式模式
https://developer.android.google.cn/training/articles/direct-boot.html?hl=zh-cn
對比兩個應用,Settings在AndroidManifest中聲明瞭android:directBootAware="true",而Launcher沒有,所以在剛開機時,只有FallbackHome這個頁面會被檢索到。在FallbackHome這個界面會一直檢測當前是否已經具備喚醒正常Launcher的條件。
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);
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);
} else {
Log.d(TAG, "User unlocked and real home found; let's go!");
getSystemService(PowerManager.class).userActivity(
SystemClock.uptimeMillis(), false);
finish();
}
}
}
當手機解鎖了後,FallbackHome會執行finish,系統會再次調用startHomeActivityLocked,這個時候就已經可以查詢到兩個聲明CATEGORY_HOME的activity了。但是此時依然不會有應用選擇對話框。這個是因爲FallbackHome在manifest中聲明瞭android:priority="-1000"(範圍【-1000,1000】,值越大,優先級越高)。在PackageManagerService裏面對這樣的情況做了處理。這裏可以分析一下PMS裏面的resolveIntent來檢索符合CATEGORY_HOME條件的應用是執行的邏輯。resolveIntent
會調用到resolveIntentInternal
。
private ResolveInfo resolveIntentInternal(Intent intent, String resolvedType,
int flags, int userId, boolean resolveForStart) {
try {
Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "resolveIntent");
if (!sUserManager.exists(userId)) return null;
final int callingUid = Binder.getCallingUid();
flags = updateFlagsForResolve(flags, userId, intent, callingUid, resolveForStart);
enforceCrossUserPermission(callingUid, userId,
false /*requireFullPermission*/, false /*checkShell*/, "resolve intent");
Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "queryIntentActivities");
final List<ResolveInfo> query = queryIntentActivitiesInternal(intent, resolvedType,
flags, callingUid, userId, resolveForStart, true /*allowDynamicSplits*/);
Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
final ResolveInfo bestChoice =
chooseBestActivity(intent, resolvedType, flags, query, userId);
return bestChoice;
} finally {
Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
}
}
resolvIntentInternal中,query這個List就是符合查詢條件的所有組件,在此場景下也是FallbackHome與Launcher3兩個應用Activity。然後在chooseBestActivity
中,當出現一些優先級不同的情況時,系統會返回query List中的第一個元素(第一個元素在此情況下就是Launcher3的Activity,有興趣的讀者可以看下PMS的mResolvePrioritySorter
,這裏說明了返回List的排序規則)。
有關FallbackHome的內容可以看https://blog.csdn.net/fu_kevin0606/article/details/65437594。
這裏描述的是開機後Launcher的啓動過程,其它情況的啓動過程可以看https://www.jianshu.com/p/667a1b760d9a文章。