轉載請注明原位址:https://www.jianshu.com/p/35e66fe56a58
現在網上能搜到的關于Android原生Launcher的一些文章,大多數是基于Android 7.0或者更早之前的,比較老舊。雖說依然可以作為參考,但是經過幾個大版本的演進,還是多多少少有些不同的。于是打算自己寫一寫基于最新AOSP的Launcher3代碼分析,供大家參考。
Android開機過程中,會把各種系統服務拉起,并且調用其systemReady()函數。其中最關鍵的ActivityManagerService拉起後,systemReady()中調用了一個函數startHomeActivityLocked(),
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
...
startHomeActivityLocked(currentUserId, "systemReady");
...
}
看一下這個函數
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() | 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);
}
} else {
Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
}
return true;
}
這裡會首先構造一個帶有HOME category的Intent,用此intent來從PackageManagerService查詢對應的ActivityInfo。
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的應用了。而系統的Launcher3應用就是在Manifest中聲明了CATEGORY_HOME的,那麼是不是這樣一來就可以拉起Launcher了呢?沒那麼簡單,這中間還有一點彎彎繞繞。
搜尋系統源碼,可以發現Settings中有一個Activity同樣聲明了CATEGORY_HOME,即FallbackHome(其實還有另外一個,不過正常啟動系統是不會出現的)。如果把鎖屏方式設定為無,重新開機之後可能是可以先看到一個“android正在啟動”的頁面,然後才真正出現了Launcher。那既然有兩個聲明了CATEGORY_HOME的頁面,為什麼不是開機後出現一個選擇框讓使用者選擇啟動哪個呢?
我們知道Android 7.0之後增加了一個DirectBoot模式,詳見https://developer.android.google.cn/training/articles/direct-boot.html?hl=zh-cn
對比兩個應用,Settings是在Manifest中聲明了android:directBootAware=“true”,而Launcher沒有,是以在此情況下,是以剛開機時,其實隻有FallbackHome這個頁面會被檢索到,也就不會出現應用選擇的對話框了。然後FallbackHome這個界面會一直檢測目前是否已經具備喚醒正常Launcher的條件,如果OK,就finish掉自己。見下面這段代碼(來自FallbackHome.java)
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();
}
}
}
finish之後系統會再次調用到AMS的startHomeActivityLocked,這時候就已經可以查詢到兩個聲明CATEGORY_HOME的activity了。但此時依然不會有應用選擇對話框。這是因為FallbackHome在manifest中聲明了自己的優先級為-1000,PackageManagerService裡面對這樣的情況是做了處理的。我們可以看一下PMS裡的resolveIntent來檢索符合CATEGORY_HOME條件的應用時執行的邏輯邏輯。resolveIntent會調用到resolveIntentInternal。
private ResolveInfo resolveIntentInternal(Intent intent, String resolvedType,
int flags, int userId, boolean resolveForStart, int filterCallingUid) {
try {
Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "resolveIntent");
if (!sUserManager.exists(userId)) return null;
final int callingUid = Binder.getCallingUid();
flags = updateFlagsForResolve(flags, userId, intent, filterCallingUid, resolveForStart);
mPermissionManager.enforceCrossUserPermission(callingUid, userId,
false /*requireFullPermission*/, false /*checkShell*/, "resolve intent");
Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "queryIntentActivities");
final List<ResolveInfo> query = queryIntentActivitiesInternal(intent, resolvedType,
flags, filterCallingUid, 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);
}
}
resolveIntentInternal中,query這個List就是符合查詢條件的所有元件,在此場景下也就是FallbackHome與Launcher3兩個應用Activity了。然後在chooseBestActivity中,當出現一些優先級不同的情況時,系統會傳回query List中的第一個元素(第一個元素在此情況下就是Launcher3的Activity,有興趣的讀者可以看下PMS的mResolvePrioritySorter,這裡說明了傳回List的排序規則)。
private ResolveInfo chooseBestActivity(Intent intent, String resolvedType,
int flags, List<ResolveInfo> query, int userId) {
...
} else if (N > 1) {
final boolean debug = ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
// If there is more than one activity with the same priority,
// then let the user decide between them.
ResolveInfo r0 = query.get(0);
ResolveInfo r1 = query.get(1);
if (DEBUG_INTENT_MATCHING || debug) {
Slog.v(TAG, r0.activityInfo.name + "=" + r0.priority + " vs "
+ r1.activityInfo.name + "=" + r1.priority);
}
// If the first activity has a higher priority, or a different
// default, then it is always desirable to pick it.
if (r0.priority != r1.priority
|| r0.preferredOrder != r1.preferredOrder
|| r0.isDefault != r1.isDefault) {
return query.get(0);
}
...
}
這裡進行另一個實踐,把FallbackHome在Manifest中聲明的priority去除,使其與Launcher3同等優先級,然後重新開機。這下就會出現應用選擇對話框了。
于是經過前面所述的邏輯後,系統桌面Launcher3就正式登場了。下一篇将分析Launcher3的啟動流程。
作者:當心你的背後
連結:https://www.jianshu.com/p/35e66fe56a58
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。