Launcher3源码分析之启动分析

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

参考文章:https://www.jianshu.com/p/35e66fe56a58

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一 Launcher默认界面配置(default workspace) 2 1 界面默认配置文件 2 2 LauncherProvider java的loadFavorites分析 3 二 Icon修改 界面布局调整 壁纸设置 5 1 图标大小和标题大小 5 2 Launcher 图标加入默认背景 6 3 更换Launcher默认壁纸 7 4 壁纸设置过程: 8 三 Launcher启动和初始化 9 1 Launcher进程启动过程 9 2 Launcher初始化――LauncherApplication 10 3 Launcher java初始化 12 1 Callbacks接口 15 2 数据加载流程 16 4 LoaderTask的run 方法 17 5 workspace加载数据 18 6 workspace绑定数据 20 7 ALL APP数据加载绑定 22 五 HotSeat分析 24 1 Hotseat配置文件 24 2 Hotseat构造函数 26 3 Hotseat加载数据 27 4 Hotseat绑定数据 27 5 Hotseat类 28 6 总结 30 六 页面滑动 PagedView 30 七 AllApp全部应用列表 AppsCustomizeTabHost 38 1 AllApp列表配置文件 38 2 AppsCustomizeTabHost分析 40 3 Tab变化时执行onTabChanged 41 八 AllApp界面拖拽元素 42 1 触摸拦截 43 2 handleTouchEvent方法 43 4 拖曳按钮 44 九 Launcher启动APP流程 45 1 WorkSpace触摸 45 2 CellLayout的onInterceptTouchEvent 方法 46 3 WorkSpace的onTouch 事件 47 4 BubbleTextView 48 5 onClick 方法 49 6 总结 50 1 常用类介绍 50 2 Launcher的数据库 51">一 Launcher默认界面配置(default workspace) 2 1 界面默认配置文件 2 2 LauncherProvider java的loadFavorites分析 3 二 Icon修改 界面布局调整 壁纸设置 5 1 图标大小和标题大小 5 2 Launcher 图标加入默认背景 6 3 更换Launcher默认壁纸 7 [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值