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文章。