Launcher 概述
我们知道,Android 系统启动的最后一步,就是去启动一个桌面应用程序,这个应用程序就是 Launcher。
Launcher 其实就是一个普通的 App 应用程序,只是它的功能是可以显示 Android 系统中所有已经安装的程序,然后提供用户点击相应的应用快捷图标就可以启动相关应用的功能······。
那么,Launcher 进程的具体启动流程是怎样的呢?我们下面就来结合源码进行分析。
Launcher 启动流程解析(源码:android-6.0.1_r81)
在前面的文章 Android 系统启动流程简析 有讲过,在 System Server 进程启动各种系统服务后,Android 系统就已经启动完成了。Launcher 进程的创建,其实就存在于 System Server 启动系统服务过程中,具体代码如下:frameworks\base\services\java\com\android\server\SystemServer.java
private void run() {
...
// Start services.
startBootstrapServices();
startCoreServices();
startOtherServices();
...
}
System Server 在其run
方法内部开启了3类系统服务:BootstrapServices,CoreServices 和 OtherServices。
我们进入startOtherServices
方法内部看下:
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() {
/*
*执行各种SystemService的启动方法,各种SystemService的systemReady方法...
*/
}
});
}
在startOtherServices
方法内调用了ActivityManagerService.systemReady(Runnable)
方法,其参数Runnable
是一个回调,当系统达到可以运行第三方代码的状态后,会回调该Runnable
,执行各种SystemService
的启动方法,各种SystemService
的systemReady
方法。不过这个过程我们并不关心,我们主要要看下ActivityManagerService.systemReady(Runnable)
这个方法的内部实现: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
里面调用了startHomeActivityLocked
方法,见名知意,该方法应该就是用来启动 Launcher 桌面程序。那么我们就接着看下这个方法的内部实现:
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);
// 系统运行模式不是低级工厂模式,则添加CATEGORY_HOME
if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
intent.addCategory(Intent.CATEGORY_HOME);
}
return intent;
}
startHomeActivityLocked
主要做了3件事:
- 首先是调用
getHomeIntent
构建一个Intent
,
该Intent
的Action
为mTopAction
,ComponentName
为mTopComponent
,mTopAction
默认为Intent.ACTION_MAIN
,mTopComponent
默认为空,只有当系统处于低级工厂模式时,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) {
//设置为工厂模式Action
mTopAction = Intent.ACTION_FACTORY_TEST;
mTopData = null;
//设置工厂模式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);
}
}
...
}
因此,系统正常启动时,getHomeIntent
构建的Intent
的ComponentName
为null
,Action
为"android.intent.action.MAIN"
,Category
为"android.intent.category.HOME"
,(所以,Launcher 与普通 App 的区别就是多了一个
)。
- 获取到
HomeIntent
后,就又通过resolveActivityInfo
方法结合HomeIntent
获取到要启动的应用相关信息,具体代码如下:
private ActivityInfo resolveActivityInfo(Intent intent, int flags, int userId) {
ActivityInfo ai = null;
//系统正常启动时,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
方法时,已经知道resolveActivityInfo
传入的Intent
的ComponentName
为null
,因此,resolveActivityInfo
最终调用的是AppGlobals.getPackageManager().resolveIntent
方法:
那么我们首先来看下AppGlobals.getPackageManager()
的具体实现:
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
实例,从其内部实现ActivityThread.getPackageManager()
可以很明显地知道该IPackageManager
实例是一个远程Binder
代理,对应的服务端其实就是PackageManagerService
:
public class PackageManagerService extends IPackageManager.Stub{···}
因此,AppGlobals.getPackageManager().resolveIntent
最终调用的就是PackageManagerService.resolveIntent
方法,我们来查看下该方法具体实现:
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;
//权限检查
enforceCrossUserPermission(Binder.getCallingUid(), userId, false, false, "resolve intent");
//查找符合Intent的Activity相关信息
List query = queryIntentActivities(intent, resolvedType, flags, userId);
//选择一个最符合要求的Activity
return chooseBestActivity(intent, resolvedType, flags, query, userId);
}
resolveIntent
该方法主要做了2件事:
- 通过
queryIntentActivities
来查找符合HomeIntent
需求Activities
,我们来看下queryIntentActivities
的具体实现代码:
@Override
public List 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 list = new ArrayList(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 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 result = new ArrayList(1);
result.add(xpResolveInfo);
return filterIfNotPrimaryUser(result, userId);
}
// Check for results in the current profile.
//从所有应用中查找
List 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(
//从满足包名的应用中进行查找
mActivities.queryIntentForPackage(
intent, resolvedType, flags, pkg.activities, userId),
userId);
}
return new ArrayList();
}
}
queryIntentActivities
对传入的Intent
的不同情形分别做了对应的处理,主要包括:
- 如果
Intent
设置了ComponentName
,说明系统中满足该条件的Activity
只存在一个,通过getActivityInfo
获取得到; - 如果
Intent
没有指定应用包名,那么通过mActivities.queryIntent
从系统所有应用中查找满足Intent
的Activity
; - 如果
Intent
已指定应用包名,那么通过mActivities.queryIntentForPackage
从满足包名的应用中查找符合Intent
的Activity
;
最终,queryIntentActivities
方法会返回一个或多个符合Intent
需求的Activity
相关信息。
- 通过
chooseBestActivity
找到最符合Intent
需求的Activity
信息,代码具体实现如下:
private ResolveInfo chooseBestActivity(Intent intent, String resolvedType,
int flags, List 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
,返回对应的ResolveInfo
:
1). 如果符合条件的只有一个应用,则返回该应用信息(源码中确实只有 Launcher3 符合);
2). 如果存在多个Activity
符合条件,则只选出前两个,比较优先级priority
、preferredOrder
、isDefault
三个参数,选出优先级最高的Activity
;
3). 如果priority
、preferredOrder
、isDefault
三个参数都一致,则看下用户是否配置了默认使用的Activity
,是则返回该Activity
对应的ResolveInfo
;
到此,ActivityManagerService.startHomeActivityLocked
从构建一个HomeIntent
,到使用Binder
跨进程通讯通知PackageManagerService
查找符合HomeIntent
的一个最佳Activity
就已经完成了。
接下来,我们继续分析ActivityManagerService.startHomeActivityLocked
最后做的一件事。
-
ActivityManagerService.startHomeActivityLocked
最后通过mStackSupervisor.startHomeActivity
启动了 Launcher 应用,具体代码如下:
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
方法最终调用的是startActivityLocked
,这其实就进入Activity
的启动流程了,这里就不展开进行简述了(关于Activity
的启动流程可以参考:TODO::)
到此,Launcher 应用就会被启动起来了。
下面,我们继续深入分析 Launcher 的源码,来看下 Launcher 界面是如何显示 Android 系统所有应用图标,以及如何启动对应应用。
总结
最后,简单总结一下 Launcher 启动流程:
首先,System Server 通过调用startOtherServices
启动系统其他服务,startOtherServices
内部会调用ActivityManagerService.systemReady(Runnable)
,该方法内部又会调用startHomeActivityLocked
开始准备开启 Launcher;startHomeActivityLocked
内部首先构建了一个HomeIntent
(具体内容就是一个ComponentName=null,action="android.intent.action.MAIN",category ="android.intent.category.HOME"
的Intent
),然后通过Binder
跨进程通讯通知PackageManagerService
从系统已安装应用中找到一个最符合该HomeIntent
的Activity
信息;找到满足HomeIntent
的Activity
信息后,最后通过StackSupervisor.startHomeActivity
启动 Launcher 应用。
Launcher 启动流程图如下图所示:
参考
Android源码解析之(十)-->Launcher启动流程
android6.0默认Home(Launcher3)的启动分析
Android系统默认Home应用程序(Launcher)的启动过程源代码分析