源码分析之Launcher启动流程

之前分析了安卓开机启动流程,详细可以查看Zygote和SystemServer相关的博客。系统开机经由init进程拉起后,调用Zygote孵化器开启SystemServer服务,其中系统核心重要的服务都在SystemServer中加载完成,之后便进入各个服务的SystemReady状态。由此,我们从这里开始分析。

源码分析之Launcher启动流程

具体流程分析

AMS进入SystemReady状态

ActivityManagerService启动完成后,便进入SystemReady状态,代码如下:

--> /frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

public void systemReady(final Runnable goingCallback) {

        ...

       // Start up initial activity.
        mBooting = true;
        // 以当前userid,启动主页
        startHomeActivityLocked(mCurrentUserId, "systemReady");
        ...

}

AMS的systemReady中有很多复杂的逻辑,例如广播发送,工厂模式的判定等,这里我们只关心主页的启动。通过startHomeActivityLocked以当前userid启动主页。

startHomeActivityLocked
    boolean startHomeActivityLocked(int userId, String reason) {

        //工厂模式启动失败
        if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
                && mTopAction == null) {
            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.instrumentationClass == null) {
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
                // 根据获取的intent,启动主页应用
                mStackSupervisor.startHomeActivity(intent, aInfo, reason);
            }
        }

        return true;
    }

通过给定intent过滤信息,通过调用resolveActivityInfo检索出合适的主页应用,回传回来ActivityInfo,然后初始化新应用的相关信息,调用mStackSupervisor.startHomeActivity启动主页。

--> /frameworks/base/services/core/java/com/android/server/am/ActivityStackSupervisor.java

    Intent getHomeIntent() {
        Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
        intent.setComponent(mTopComponent);
        if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
            intent.addCategory(Intent.CATEGORY_HOME);
        }
        return intent;
    }
resolveActivityInfo筛选合适的应用信息

resolveActivityInfo中判断系统是否处于工厂模式,如果是,直接根据ComponentName调用PMS的getActivityInfo来获取应用信息;如果不是,则调用PMS的resolveIntent全局筛选合适的主页应用。

private ActivityInfo resolveActivityInfo(Intent intent, int flags, int userId) {
        ActivityInfo ai = null;
        ComponentName comp = intent.getComponent();
        try {
            if (comp != null) {
                // 工厂模式
                ai = AppGlobals.getPackageManager().getActivityInfo(comp, flags, userId);
            } else {
                    // 调用PMS全局筛选合适的主页应用
                ResolveInfo info = AppGlobals.getPackageManager().resolveIntent(
                        intent,
                        intent.resolveTypeIfNeeded(mContext.getContentResolver()),
                        flags, userId);

                if (info != null) {
                    ai = info.activityInfo;
                }
            }
        } catch (RemoteException e) {
        }

        return ai;
    }
PMS筛选主页应用信息
-->/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

AMS通过Binder通信,获取PMS,调用resolveIntent查找出合适Intent的应用信息

    public ResolveInfo resolveIntent(Intent intent, String resolvedType,
                int flags, int userId) {
        // userID检测
        if (!sUserManager.exists(userId)) return null;
        // 权限检测
        enforceCrossUserPermission(Binder.getCallingUid(), userId, false, false, "resolve intent");
        // 全局包名查询意图匹配的应用信息
        List<ResolveInfo> query = queryIntentActivities(intent, resolvedType, flags, userId);
        return chooseBestActivity(intent, resolvedType, flags, query, userId);
    }

resolveIntent中主要实现的逻辑如下:

  1. 检测UserManager是否存在当前的用户id
  2. 检测调用方法者是否拥有相应的权限
  3. 通过queryIntentActivities全局查找出合适的应用信息
  4. 通过chooseBestActivity进一步筛选出最合适的应用
queryIntentActivities全局查询合适intent

queryIntentActivities会根据传入的intent来选择查找方式,一种是没有包名,再系统所有的包中查找,否则查找安装包中指定的包。

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();
        }
    }

    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.
            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);
        if (pkg != null) {
            return filterIfNotPrimaryUser(
                    mActivities.queryIntentForPackage(
                            intent, resolvedType, flags, pkg.activities, userId),
                    userId);
        }
        return new ArrayList<ResolveInfo>();
    }
}
chooseBestActivity筛选最合适的应用

对于前面传来的List集合,需要从中筛选出最合适的应用。

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.
            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.
            // 取前两条,选择优先级高的
            if (r0.priority != r1.priority
                    || r0.preferredOrder != r1.preferredOrder
                    || r0.isDefault != r1.isDefault) {
                return query.get(0);
            }

            // 已经设置过合适的主页应用,返回它
            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;
}

筛选逻辑总结如下:

  1. 只存在一条信息,直接返回
  2. 存在两条以上,获取前两条,比较优先级高的返回
  3. 如果一样,查看之前用户是否设置过合适的主页,返回它

到此,超找到合适的主页应用信息,直接调用之前的mStackSupervisor.startHomeActivity便可以启动主页应用

小结

了解了主页应用的启动流程,可以方便我们入手定制系统信息的Launcher,一般实现用户自定的主页可以有以下几个实现方式:

  1. 修改应用层在Manifest.xml文件,设置优先级属性等,例如priority默认值为0,设置的值越大优先级越高
  2. framework可以仿照源码中工厂模式,在工厂模式检测前返回自己的主页应用
  3. 在chooseBestActivity代码的筛选中添加合适代码直接放回自己的主页应用

相关说明

在主页的启动流程中涉及到了安卓新手引导页的相关知识,具体可以参考一下博文

[请戳这里](https://github.com/Huoxubeiyin/SourceCode/blob/master/%E6%BA%90%E7%A0%81%E4%BF%AE%E6%94%B9%E4%B9%8B%E6%96%B0%E6%89%8B%E5%BC%95%E5%AF%BC.md)
坚持原创技术分享,您的支持将鼓励我继续创作!