Launcher 概述
我們知道,Android 系統(tǒng)啟動的最后一步叠赐,就是去啟動一個桌面應(yīng)用程序痊土,這個應(yīng)用程序就是 Launcher塞帐。
Launcher 其實就是一個普通的 App 應(yīng)用程序,只是它的功能是可以顯示 Android 系統(tǒng)中所有已經(jīng)安裝的程序沪羔,然后提供用戶點擊相應(yīng)的應(yīng)用快捷圖標(biāo)就可以啟動相關(guān)應(yīng)用的功能······饥伊。
那么,Launcher 進程的具體啟動流程是怎樣的呢蔫饰?我們下面就來結(jié)合源碼進行分析琅豆。
Launcher 啟動流程解析(源碼:android-6.0.1_r81)
在前面的文章 Android 系統(tǒng)啟動流程簡析 有講過,在 System Server 進程啟動各種系統(tǒng)服務(wù)后篓吁,Android 系統(tǒng)就已經(jīng)啟動完成了茫因。Launcher 進程的創(chuàng)建,其實就存在于 System Server 啟動系統(tǒng)服務(wù)過程中杖剪,具體代碼如下:frameworks\base\services\java\com\android\server\SystemServer.java
private void run() {
...
// Start services.
startBootstrapServices();
startCoreServices();
startOtherServices();
...
}
System Server 在其run
方法內(nèi)部開啟了3類系統(tǒng)服務(wù):BootstrapServices冻押,CoreServices 和 OtherServices。
我們進入startOtherServices
方法內(nèi)部看下:
private void startOtherServices() {
...
// We now tell the activity manager it is okay to run third party
// code. It will call back into us once it has gotten to the state
// where third party code can really run (but before it has actually
// started launching the initial applications), for us to complete our
// initialization.
mActivityManagerService.systemReady(new Runnable() {
@Override
public void run() {
/*
*執(zhí)行各種SystemService的啟動方法盛嘿,各種SystemService的systemReady方法...
*/
}
});
}
在startOtherServices
方法內(nèi)調(diào)用了ActivityManagerService.systemReady(Runnable)
方法洛巢,其參數(shù)Runnable
是一個回調(diào),當(dāng)系統(tǒng)達到可以運行第三方代碼的狀態(tài)后次兆,會回調(diào)該Runnable
稿茉,執(zhí)行各種SystemService
的啟動方法,各種SystemService
的systemReady
方法。不過這個過程我們并不關(guān)心漓库,我們主要要看下ActivityManagerService.systemReady(Runnable)
這個方法的內(nèi)部實現(xiàn):frameworks\base\services\core\java\com\android\server\am\ActivityManagerService.java
public void systemReady(final Runnable goingCallback) {
...
// Start up initial activity.
mBooting = true;
startHomeActivityLocked(mCurrentUserId, "systemReady");
...
}
可以看到恃慧,在systemReady
里面調(diào)用了startHomeActivityLocked
方法,見名知意渺蒿,該方法應(yīng)該就是用來啟動 Launcher 桌面程序痢士。那么我們就接著看下這個方法的內(nèi)部實現(xiàn):
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.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mStackSupervisor.startHomeActivity(intent, aInfo, reason);
}
}
return true;
}
ComponentName mTopComponent;
String mTopAction = Intent.ACTION_MAIN;// "android.intent.action.MAIN"
String mTopData;
Intent getHomeIntent() {
Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
intent.setComponent(mTopComponent);
// 系統(tǒng)運行模式不是低級工廠模式,則添加CATEGORY_HOME
if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
intent.addCategory(Intent.CATEGORY_HOME);
}
return intent;
}
startHomeActivityLocked
主要做了3件事:
- 首先是調(diào)用
getHomeIntent
構(gòu)建一個Intent
茂装,
該Intent
的Action
為mTopAction
良瞧,ComponentName
為mTopComponent
,mTopAction
默認為Intent.ACTION_MAIN
训唱,mTopComponent
默認為空褥蚯,只有當(dāng)系統(tǒng)處于低級工廠模式時,mTopAction
和mTopComponent
才會被改變况增,具體代碼如下:
public void systemReady(final Runnable goingCallback) {
...
if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL) {
ResolveInfo ri = mContext.getPackageManager()
.resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST),
STOCK_PM_FLAGS);
CharSequence errorMsg = null;
if (ri != null) {
ActivityInfo ai = ri.activityInfo;
ApplicationInfo app = ai.applicationInfo;
if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
//設(shè)置為工廠模式Action
mTopAction = Intent.ACTION_FACTORY_TEST;
mTopData = null;
//設(shè)置工廠模式App的ComponentName
mTopComponent = new ComponentName(app.packageName,
ai.name);
} else {
errorMsg = mContext.getResources().getText(
com.android.internal.R.string.factorytest_not_system);
}
} else {
errorMsg = mContext.getResources().getText(
com.android.internal.R.string.factorytest_no_action);
}
if (errorMsg != null) {
mTopAction = null;
mTopData = null;
mTopComponent = null;
Message msg = Message.obtain();
msg.what = SHOW_FACTORY_ERROR_MSG;
msg.getData().putCharSequence("msg", errorMsg);
mUiHandler.sendMessage(msg);
}
}
...
}
因此赞庶,系統(tǒng)正常啟動時,getHomeIntent
構(gòu)建的Intent
的ComponentName
為null
澳骤,Action
為"android.intent.action.MAIN"
歧强,Category
為"android.intent.category.HOME"
,(所以为肮,Launcher 與普通 App 的區(qū)別就是多了一個<category android:name="android.intent.category.HOME">
)摊册。
- 獲取到
HomeIntent
后,就又通過resolveActivityInfo
方法結(jié)合HomeIntent
獲取到要啟動的應(yīng)用相關(guān)信息颊艳,具體代碼如下:
private ActivityInfo resolveActivityInfo(Intent intent, int flags, int userId) {
ActivityInfo ai = null;
//系統(tǒng)正常啟動時茅特,comp為null
ComponentName comp = intent.getComponent();
try {
if (comp != null) {
// Factory test.
ai = AppGlobals.getPackageManager().getActivityInfo(comp, flags, userId);
} else {
ResolveInfo info = AppGlobals.getPackageManager().resolveIntent(
intent,
intent.resolveTypeIfNeeded(mContext.getContentResolver()),
flags, userId);
if (info != null) {
ai = info.activityInfo;
}
}
} catch (RemoteException e) {
// ignore
}
return ai;
}
我們在前面分析getHomeIntent
方法時,已經(jīng)知道resolveActivityInfo
傳入的Intent
的ComponentName
為null
棋枕,因此白修,resolveActivityInfo
最終調(diào)用的是AppGlobals.getPackageManager().resolveIntent
方法:
那么我們首先來看下AppGlobals.getPackageManager()
的具體實現(xiàn):
frameworks\base\core\java\android\app\AppGlobals.java
public static IPackageManager getPackageManager() {
return ActivityThread.getPackageManager();
}
frameworks\base\core\java\android\app\ActivityThread.java
static IPackageManager sPackageManager;
public static IPackageManager getPackageManager() {
if (sPackageManager != null) {
//Slog.v("PackageManager", "returning cur default = " + sPackageManager);
return sPackageManager;
}
IBinder b = ServiceManager.getService("package");
//Slog.v("PackageManager", "default service binder = " + b);
sPackageManager = IPackageManager.Stub.asInterface(b);
//Slog.v("PackageManager", "default service = " + sPackageManager);
return sPackageManager;
}
所以AppGlobals.getPackageManager()
最終返回的其實就是一個IPackageManager
實例,從其內(nèi)部實現(xiàn)ActivityThread.getPackageManager()
可以很明顯地知道該IPackageManager
實例是一個遠程Binder
代理重斑,對應(yīng)的服務(wù)端其實就是PackageManagerService
:
public class PackageManagerService extends IPackageManager.Stub{···}
因此兵睛,AppGlobals.getPackageManager().resolveIntent
最終調(diào)用的就是PackageManagerService.resolveIntent
方法,我們來查看下該方法具體實現(xiàn):
frameworks\base\services\core\java\com\android\server\pm\PackageManagerService.java
@Override
public ResolveInfo resolveIntent(Intent intent, String resolvedType,
int flags, int userId) {
if (!sUserManager.exists(userId)) return null;
//權(quán)限檢查
enforceCrossUserPermission(Binder.getCallingUid(), userId, false, false, "resolve intent");
//查找符合Intent的Activity相關(guān)信息
List<ResolveInfo> query = queryIntentActivities(intent, resolvedType, flags, userId);
//選擇一個最符合要求的Activity
return chooseBestActivity(intent, resolvedType, flags, query, userId);
}
resolveIntent
該方法主要做了2件事:
- 通過
queryIntentActivities
來查找符合HomeIntent
需求Activities
,我們來看下queryIntentActivities
的具體實現(xiàn)代碼:
@Override
public List<ResolveInfo> queryIntentActivities(Intent intent,
String resolvedType, int flags, int userId) {
if (!sUserManager.exists(userId)) return Collections.emptyList();
enforceCrossUserPermission(Binder.getCallingUid(), userId, false, false, "query intent activities");
ComponentName comp = intent.getComponent();
if (comp == null) {
if (intent.getSelector() != null) {
intent = intent.getSelector();
comp = intent.getComponent();
}
}
//如果ComponentName已指定,說明滿足條件的Activity只有一個
if (comp != null) {
final List<ResolveInfo> list = new ArrayList<ResolveInfo>(1);
final ActivityInfo ai = getActivityInfo(comp, flags, userId);
if (ai != null) {
final ResolveInfo ri = new ResolveInfo();
ri.activityInfo = ai;
list.add(ri);
}
return list;
}
// reader
synchronized (mPackages) {
final String pkgName = intent.getPackage();
//未指定包名
if (pkgName == null) {
List<CrossProfileIntentFilter> matchingFilters =
getMatchingCrossProfileIntentFilters(intent, resolvedType, userId);
// Check for results that need to skip the current profile.
ResolveInfo xpResolveInfo = querySkipCurrentProfileIntents(matchingFilters, intent,
resolvedType, flags, userId);
if (xpResolveInfo != null && isUserEnabled(xpResolveInfo.targetUserId)) {
List<ResolveInfo> result = new ArrayList<ResolveInfo>(1);
result.add(xpResolveInfo);
return filterIfNotPrimaryUser(result, userId);
}
// Check for results in the current profile.
//從所有應(yīng)用中查找
List<ResolveInfo> result = mActivities.queryIntent(
intent, resolvedType, flags, userId);
// Check for cross profile results.
xpResolveInfo = queryCrossProfileIntents(
matchingFilters, intent, resolvedType, flags, userId);
if (xpResolveInfo != null && isUserEnabled(xpResolveInfo.targetUserId)) {
result.add(xpResolveInfo);
Collections.sort(result, mResolvePrioritySorter);
}
result = filterIfNotPrimaryUser(result, userId);
if (hasWebURI(intent)) {
CrossProfileDomainInfo xpDomainInfo = null;
final UserInfo parent = getProfileParent(userId);
if (parent != null) {
xpDomainInfo = getCrossProfileDomainPreferredLpr(intent, resolvedType,
flags, userId, parent.id);
}
if (xpDomainInfo != null) {
if (xpResolveInfo != null) {
// If we didn't remove it, the cross-profile ResolveInfo would be twice
// in the result.
result.remove(xpResolveInfo);
}
if (result.size() == 0) {
result.add(xpDomainInfo.resolveInfo);
return result;
}
} else if (result.size() <= 1) {
return result;
}
result = filterCandidatesWithDomainPreferredActivitiesLPr(intent, flags, result,
xpDomainInfo, userId);
Collections.sort(result, mResolvePrioritySorter);
}
return result;
}
final PackageParser.Package pkg = mPackages.get(pkgName);
//如果Intent中已指定包名
if (pkg != null) {
return filterIfNotPrimaryUser(
//從滿足包名的應(yīng)用中進行查找
mActivities.queryIntentForPackage(
intent, resolvedType, flags, pkg.activities, userId),
userId);
}
return new ArrayList<ResolveInfo>();
}
}
queryIntentActivities
對傳入的Intent
的不同情形分別做了對應(yīng)的處理旺拉,主要包括:
- 如果
Intent
設(shè)置了ComponentName
,說明系統(tǒng)中滿足該條件的Activity
只存在一個假颇,通過getActivityInfo
獲取得到; - 如果
Intent
沒有指定應(yīng)用包名符相,那么通過mActivities.queryIntent
從系統(tǒng)所有應(yīng)用中查找滿足Intent
的Activity
拆融; - 如果
Intent
已指定應(yīng)用包名,那么通過mActivities.queryIntentForPackage
從滿足包名的應(yīng)用中查找符合Intent
的Activity
啊终;
最終镜豹,queryIntentActivities
方法會返回一個或多個符合Intent
需求的Activity
相關(guān)信息。
- 通過
chooseBestActivity
找到最符合Intent
需求的Activity
信息蓝牲,代碼具體實現(xiàn)如下:
private ResolveInfo chooseBestActivity(Intent intent, String resolvedType,
int flags, List<ResolveInfo> query, int userId) {
if (query != null) {
final int N = query.size();
if (N == 1) {
//只有一個滿足趟脂,直接返回
return query.get(0);
} 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.
//存在多個相同priority,只獲取前兩個Activity
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 desireable to pick it.
//前兩個Activity的priority或default不同例衍,則默認選擇第一個Activity
if (r0.priority != r1.priority
|| r0.preferredOrder != r1.preferredOrder
|| r0.isDefault != r1.isDefault) {
return query.get(0);
}
// If we have saved a preference for a preferred activity for
// this Intent, use that.
//如果已保存了默認選擇昔期,則使用
ResolveInfo ri = findPreferredActivity(intent, resolvedType,
flags, query, r0.priority, true, false, debug, userId);
if (ri != null) {
return ri;
}
ri = new ResolveInfo(mResolveInfo);
ri.activityInfo = new ActivityInfo(ri.activityInfo);
ri.activityInfo.applicationInfo = new ApplicationInfo(
ri.activityInfo.applicationInfo);
if (userId != 0) {
ri.activityInfo.applicationInfo.uid = UserHandle.getUid(userId,
UserHandle.getAppId(ri.activityInfo.applicationInfo.uid));
}
// Make sure that the resolver is displayable in car mode
if (ri.activityInfo.metaData == null) ri.activityInfo.metaData = new Bundle();
ri.activityInfo.metaData.putBoolean(Intent.METADATA_DOCK_HOME, true);
return ri;
}
}
return null;
}
chooseBestActivity
從多個維度進行抉擇獲取一個最符合Intent
條件的Activity
,返回對應(yīng)的ResolveInfo
:
1). 如果符合條件的只有一個應(yīng)用佛玄,則返回該應(yīng)用信息(源碼中確實只有 Launcher3 符合)硼一;
2). 如果存在多個Activity
符合條件,則只選出前兩個梦抢,比較優(yōu)先級priority
般贼、preferredOrder
、isDefault
三個參數(shù)奥吩,選出優(yōu)先級最高的Activity
哼蛆;
3). 如果priority
、preferredOrder
霞赫、isDefault
三個參數(shù)都一致腮介,則看下用戶是否配置了默認使用的Activity
,是則返回該Activity
對應(yīng)的ResolveInfo
端衰;
到此叠洗,ActivityManagerService.startHomeActivityLocked
從構(gòu)建一個HomeIntent
,到使用Binder
跨進程通訊通知PackageManagerService
查找符合HomeIntent
的一個最佳Activity
就已經(jīng)完成了旅东。
接下來惕味,我們繼續(xù)分析ActivityManagerService.startHomeActivityLocked
最后做的一件事。
-
ActivityManagerService.startHomeActivityLocked
最后通過mStackSupervisor.startHomeActivity
啟動了 Launcher 應(yīng)用玉锌,具體代碼如下:
frameworks\base\services\core\java\com\android\server\am\ActivityStackSupervisor.java
void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
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 */,
null /* outActivity */, null /* container */, null /* inTask */);
if (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.
scheduleResumeTopActivities();
}
}
可以看到名挥,startHomeActivity
方法最終調(diào)用的是startActivityLocked
,這其實就進入Activity
的啟動流程了主守,這里就不展開進行簡述了(關(guān)于Activity
的啟動流程可以參考:TODO::)
到此禀倔,Launcher 應(yīng)用就會被啟動起來了。
下面参淫,我們繼續(xù)深入分析 Launcher 的源碼救湖,來看下 Launcher 界面是如何顯示 Android 系統(tǒng)所有應(yīng)用圖標(biāo),以及如何啟動對應(yīng)應(yīng)用涎才。
總結(jié)
最后鞋既,簡單總結(jié)一下 Launcher 啟動流程:
首先力九,System Server 通過調(diào)用startOtherServices
啟動系統(tǒng)其他服務(wù),startOtherServices
內(nèi)部會調(diào)用ActivityManagerService.systemReady(Runnable)
邑闺,該方法內(nèi)部又會調(diào)用startHomeActivityLocked
開始準(zhǔn)備開啟 Launcher跌前;startHomeActivityLocked
內(nèi)部首先構(gòu)建了一個HomeIntent
(具體內(nèi)容就是一個ComponentName=null,action="android.intent.action.MAIN"陡舅,category ="android.intent.category.HOME"
的Intent
)抵乓,然后通過Binder
跨進程通訊通知PackageManagerService
從系統(tǒng)已安裝應(yīng)用中找到一個最符合該HomeIntent
的Activity
信息;找到滿足HomeIntent
的Activity
信息后靶衍,最后通過StackSupervisor.startHomeActivity
啟動 Launcher 應(yīng)用灾炭。
Launcher 啟動流程圖如下圖所示: