Android Framework实战视频--SystemServer启动FallbackHome篇

93 篇文章 71 订阅
42 篇文章 22 订阅

课程答疑和新课信息:QQ交流群:422901085进行课程讨论
FrameWork入门课视频链接:https://edu.csdn.net/course/detail/30298
FrameWork实战课1视频链接:https://edu.csdn.net/course/detail/30275
专题博客系列:
Android 8.1 zygote 启动过程源码
Android Framework实战视频–Zygote的fork进程篇
Android Framework实战视频–SystemServer启动篇
Android Framework实战视频–SystemServer启动FallbackHome篇
Android Framework实战视频–FallbackHome进程启动及Activity启动篇
Android Framework实战视频–FallbackHome结束启动Launcher篇

Android Framework实战视频–SystemServer启动FallbackHome篇

Android 8.1代码分析FallbackHome的启动:
前面已经讲解到了在startOtherService里面不仅仅会start系统服务,等系统服务都启动完成后,还会调用各个Service的systemReady方法,这里需要分析首个Ativity的启动,即常见的“Launcher”,当然是从ActivityManagerService开始分析:

public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
   //省略。。
    synchronized(this) {
       //省略。。
    synchronized (this) {
          //省略。。
        startHomeActivityLocked(currentUserId, "systemReady");
  		 //省略。。
        mStackSupervisor.resumeFocusedStackTopActivityLocked();
          //省略。。
    }
}

这里对大部分不是分析的核心代码进行了省略,这里看到了startHomeActivityLocked(currentUserId, “systemReady”);被调用,从名字既可以看得出他是要启动Home类型的Activtiy,常见的Launcher就是一种Home类型的Activity,但这里其实并不是Launcher,而是设置中的FallbackHome类型的,它也是一个Home类型的Activity,这里FallbackHome是google新加入的,主要就是因为涉及整个android系统的加密等原因,系统在还没有完全解锁前,不可以启动Launcher,因为Launcher中明显和各个第三方应用耦合较多(比如桌面可能显示着一堆的各个应用的Widget),如果直接Launcher作为FallbackHome启动,相对就会要求Launcher依赖的应用也是支持直接解密类型,那就肯定不现实。所以就先启动了FallbackHome一个什么也不显示的界面来作为启动真正Launcher的一个过度。

那来分析一下,到底是怎么启动FallbackHome,又是凭什么确定就是FallbackHome?接下来继续分析startHomeActivityLocked代码:

 boolean startHomeActivityLocked(int userId, String reason) {
	//省略
  		Intent intent = getHomeIntent();//获取Home类型的Intent
        ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);//通过PackageManagerService查询出目前系统中可以匹配这个intent的ActivtiyInfo
        if (aInfo != null) {
            intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
            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) {
                //省略。。
                mActivityStarter.startHomeActivityLocked(intent, aInfo, myReason);
            }
        } 
        //省略。。
        return true;
    }
 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) {
        //这里是匹配HomeIntent的核心
            intent.addCategory(Intent.CATEGORY_HOME);
        }
        return intent;
    }

这里的getHomeIntent主要就是在Intent中增加 intent.addCategory(Intent.CATEGORY_HOME);即有category为Intent.CATEGORY_HOME的Intent
这里面比较简单,就是获取了HomeIntent后,然后利用intent进行ActivityInfo获取,然后判断是否还没有启动过该进程,没有则继续调用mActivityStarter.startHomeActivityLocked(intent, aInfo, myReason);调用ActivityStarter里面的startHomeActivityLocked

 void startHomeActivityLocked(Intent intent, ActivityInfo aInfo, String reason) {
        mSupervisor.moveHomeStackTaskToTop(reason);
        mLastHomeActivityStartResult = startActivityLocked(null /*caller*/, intent,
                null /*ephemeralIntent*/, null /*resolvedType*/, aInfo, null /*rInfo*/,
                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*/, mLastHomeActivityStartRecord /*outActivity*/,
                null /*inTask*/, "startHomeActivity: " + reason);
        if (mSupervisor.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.
            mSupervisor.scheduleResumeTopActivities();
        }
    }

这里又调用到了startActivityLocked方法:

int startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
            String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
            String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
            ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
            ActivityRecord[] outActivity, TaskRecord inTask, String reason) {
//省略
        mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,
                aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,
                callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
                options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,
                inTask);
//省略
        return mLastStartActivityResult != START_ABORTED ? mLastStartActivityResult : START_SUCCESS;
    }

又调用到了startActivity方法:

 private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
            String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
            String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
            ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
            ActivityRecord[] outActivity, TaskRecord inTask) {
     //省略
        ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,
                callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),
                resultRecord, resultWho, requestCode, componentSpecified, voiceSession != null,
                mSupervisor, options, sourceRecord);
       //省略
        return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true,
                options, inTask, outActivity);
    }

又调用 startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true,options, inTask, outActivity);

 private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
            ActivityRecord[] outActivity) {
//省略
            mService.mWindowManager.deferSurfaceLayout();
            result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,
                    startFlags, doResume, options, inTask, outActivity);
   //省略

        return result;
    }

这里又调用到startActivityUnchecked方法:

private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
        IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
        int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
     
	//省略
    if (mDoResume) {
        //省略
        } else {
           //省略
            mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,
                    mOptions);
        }
    } 
    //省略
    return START_SUCCESS;
}

这里用会调用到ActivityStackSupervisor的resumeFocusedStackTopActivityLocked方法:
mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,mOptions);

 boolean resumeFocusedStackTopActivityLocked(
            ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
//省略
        if (targetStack != null && isFocusedStack(targetStack)) {
            return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
        }
//省略
        return false;
    }

这里又会调用到targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
frameworks/base/services/core/java/com/android/server/am/ActivityStack.java

 boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
     //省略
        try {
            // Protect against recursion.
            mStackSupervisor.inResumeTopActivity = true;
            result = resumeTopActivityInnerLocked(prev, options);
        } finally {
            mStackSupervisor.inResumeTopActivity = false;
        }
		//省略
        return result;
    }

又会调用到resumeTopActivityInnerLocked:

private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {

//省略
        mStackSupervisor.startSpecificActivityLocked(next, true, true);
        //省略
    return true;
}

最后又会调回ActivityStackSupervisor的startSpecificActivityLocked(next, true, true):

void startSpecificActivityLocked(ActivityRecord r,
        boolean andResume, boolean checkConfig) {
//省略
    mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
            "activity", r.intent.getComponent(), false, false, true);
}

这里又会调用ActivityManagerService的startProcessLocked方法,startProcessLocked也有几个重载最后调到如下方法:

 final ProcessRecord startProcessLocked(String processName, ApplicationInfo info,
            boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName,
            boolean allowWhileBooting, boolean isolated, int isolatedUid, boolean keepIfLarge,
            String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) {
        long startTime = SystemClock.elapsedRealtime();
    //省略

        if (app == null) {
            app = newProcessRecordLocked(info, processName, isolated, isolatedUid);
        } 
         //省略
        startProcessLocked(
                app, hostingType, hostingNameStr, abiOverride, entryPoint, entryPointArgs); 
                //省略
        return (app.pid != 0) ? app : null;
    }

刚开始进程ProcessRecord app肯定位null所以newProcessRecordLocked会构造一个ProcessRecord,再次调用 startProcessLocked(
app, hostingType, hostingNameStr, abiOverride, entryPoint, entryPointArgs); 方法

private final void startProcessLocked(ProcessRecord app, String hostingType,
            String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs) {
        //省略。。
            boolean isActivityProcess = (entryPoint == null);
            //这里entryPoint传递的是null,所以赋值为entryPoint = "android.app.ActivityThread"
            if (entryPoint == null) entryPoint = "android.app.ActivityThread";
          //省略
            ProcessStartResult startResult;
            if (hostingType.equals("webview_service")) {
                startResult = startWebView(entryPoint,
                        app.processName, uid, uid, gids, debugFlags, mountExternal,
                        app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
                        app.info.dataDir, null, entryPointArgs);
            } else {
              //调用process的start方法来启动进程
                startResult = Process.start(entryPoint,
                        app.processName, uid, uid, gids, debugFlags, mountExternal,
                        app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
                        app.info.dataDir, invokeWith, entryPointArgs);
            }
          //省略
    }
Android中,fallbackhome是指当用户按下设备的home键时默认返回的应用程序。如果我们想要去掉fallbackhome,我们可以通过以下步骤实现: 1. 首先,我们需要找到Android设备上的"Settings"(设置)应用程序图标。通常情况下,该图标在应用程序手机桌面或应用程序抽屉中。 2. 点击打开"Settings"应用程序。 3. 在设置菜单中,我们需要找到"Applications"(应用程序)选项。它的图标通常为一个小方块,上面有几个小圆点。 4. 点击打开"Applications"选项。 5. 在应用程序菜单中,我们需要找到"Default Applications"(默认应用程序)选项。点击打开该选项。 6. 在默认应用程序列表中,我们会看到"Home"(主页)选项。点击打开该选项。 7. 在主页设置菜单中,我们可以看到一个名为"Choose default app"(选择默认应用程序)的下拉菜单。点击打开该菜单。 8. 在下拉菜单中,我们可以看到已安装在设备上的所有可用主页应用程序的列表。 9. 找到我们想要设置为默认主页的应用程序,并点击该应用程序的名称。 10. 设备会自动将选择的应用程序设置为新的默认主页。 11. 现在,当我们按下设备的home键时,系统将不再返回fallbackhome,而是跳转到我们所选择的应用程序。 需要注意的是,不同的Android设备可能在界面和菜单的名称上略有不同,但基本的步骤是相似的。在操作时,请按照自己设备上的具体界面展现进行操作。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千里马学框架

帮助你了,就请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值