【Android】Android Framework系列--Launcher3各启动场景源码分析

Android Framework系列–Launcher3各启动场景源码分析

Launcher3启动场景

Launcher3是Android系统提供的默认桌面应用(Launcher),它的源码路径在“packages/apps/Launcher3/”。
Launcher3的启动场景主要包括:

  • 开机后启动:开机时,android ams服务拉起Launcher
  • 按键启动:比如短压home键,android wms中的PhoneWindowManager拉起Launcher
  • 异常崩溃后启动:Launcher异常崩溃后,android ams再次拉起Launcher

针对这三种情况,分析一下Aosp源码如何实现。
源码基于Android10版本,时序图基于Android12(基本上与Android10的流程差不多,可以参考)。
下述说明中Launcher3和Launcher意思相同。

开机启动Launcher

在这里插入图片描述

Launcher的开机启动由Android的AMS服务完成。
AMS在SystemReady阶段会调用startHomeOnAllDisplays函数。Android支持多Display(虚拟Display或者由硬件上报的实际Display),多Display情况下一般Launcher会针对不同Display做不同的效果。

// frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {// 启动Home,也就是LaunchermAtmInternal.startHomeOnAllDisplays(currentUserId, "systemReady");
}

调用ActivityTaskManagerInternal类型的接口startHomeOnAllDisplays,这个接口在ActivityTaskManagerService.java文件中实现。

// frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
final class LocalService extends ActivityTaskManagerInternal {@Overridepublic boolean startHomeOnAllDisplays(int userId, String reason) {synchronized (mGlobalLock) {// 这个对象是RootActivityContainer类型return mRootActivityContainer.startHomeOnAllDisplays(userId, reason);}}
}

接下来调用RootActivityContainer的接口startHomeOnAllDisplays,第二个参数reson的值为“systemReady”。

// frameworks/base/services/core/java/com/android/server/wm/RootActivityContainer.java
boolean startHomeOnAllDisplays(int userId, String reason) {boolean homeStarted = false;for (int i = mActivityDisplays.size() - 1; i >= 0; i--) {final int displayId = mActivityDisplays.get(i).mDisplayId;homeStarted |= startHomeOnDisplay(userId, reason, displayId);}return homeStarted;
}

遍历mActivityDisplays对于所有处于Active状态的Display调用startHomeOnDisplay,在每个Display上都启动Home(Launcher)。

// frameworks/base/services/core/java/com/android/server/wm/RootActivityContainer.java
boolean startHomeOnDisplay(int userId, String reason, int displayId) {// allowInstrumenting :false// fromHomeKey : falsereturn startHomeOnDisplay(userId, reason, displayId, false /* allowInstrumenting */,false /* fromHomeKey */);
}boolean startHomeOnDisplay(int userId, String reason, int displayId, boolean allowInstrumenting,boolean fromHomeKey) {// 如果DisplayID是非法的,使用当前处于顶层焦点的 Display// Fallback to top focused display if the displayId is invalid.if (displayId == INVALID_DISPLAY) {displayId = getTopDisplayFocusedStack().mDisplayId;}// 构建一个IntentIntent homeIntent = null;ActivityInfo aInfo = null;if (displayId == DEFAULT_DISPLAY) {// 如果DisplayID是默认的Display(一般是主屏)// 调用ActivityTaskManagerService的getHomeIntent,拿到用来启动Home的IntenthomeIntent = mService.getHomeIntent();// 查找当前系统里包含 android.intent.category.HOME的Activity。// 择优选择使用哪个ActivityaInfo = resolveHomeActivity(userId, homeIntent);} else if (shouldPlaceSecondaryHomeOnDisplay(displayId)) {// 如果不是默认Display屏幕Pair<ActivityInfo, Intent> info = resolveSecondaryHomeActivity(userId, displayId);aInfo = info.first;homeIntent = info.second;}if (aInfo == null || homeIntent == null) {return false;}// 判断是否运行启动Homeif (!canStartHomeOnDisplay(aInfo, displayId, allowInstrumenting)) {return false;}// 更新Home Intent// Updates the home component of the intent.homeIntent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));homeIntent.setFlags(homeIntent.getFlags() | FLAG_ACTIVITY_NEW_TASK);// 如果是从HomeKey启动的,添加额外参数// Updates the extra information of the intent.if (fromHomeKey) {homeIntent.putExtra(WindowManagerPolicy.EXTRA_FROM_HOME_KEY, true);}// Update the reason for ANR debugging to verify if the user activity is the one that// actually launched.final String myReason = reason + ":" + userId + ":" + UserHandle.getUserId(aInfo.applicationInfo.uid) + ":" + displayId;// 启动HomemService.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason,displayId);return true;
}

startHomeOnDisplay函数中查询当前系统中包含 android.intent.category.HOME信息的Activity(resolveHomeActivity),如果找到了多个Activity则选择高优先级的。根据查找的Activity信息构建Intent,使用ActivityTaskManagerService的ActivityStartController启动Home对应的Activity。
getHomeIntent函数在ActivityTaskManagerService中实现。

// frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
Intent getHomeIntent() {// String mTopAction = Intent.ACTION_MAIN;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) {// 非FactoryTest模式下走这里。// android.intent.category.HOMEintent.addCategory(Intent.CATEGORY_HOME);}return intent;
}

resolveHomeActivity函数用于查找包含android.intent.category.HOME信息的Activity。实现上是通过PMS完成的。

// frameworks/base/services/core/java/com/android/server/wm/RootActivityContainer.java
/*** This resolves the home activity info.* @return the home activity info if any.*/
@VisibleForTesting
ActivityInfo resolveHomeActivity(int userId, Intent homeIntent) {final ComponentName comp = homeIntent.getComponent();try {if (comp != null) {// Factory test.} else {final String resolvedType =homeIntent.resolveTypeIfNeeded(mService.mContext.getContentResolver());// 调用PMS查找信息final ResolveInfo info = AppGlobals.getPackageManager().resolveIntent(homeIntent, resolvedType, flags, userId);if (info != null) {aInfo = info.activityInfo;}}} catch (RemoteException e) {// ignore}aInfo = new ActivityInfo(aInfo);aInfo.applicationInfo = mService.getAppInfoForUser(aInfo.applicationInfo, userId);return aInfo;
}

当找到所需信息后,调用ActivityStartController的startHomeActivity启动Home。该接口与AMS的startActivity和startActivityAsUser实现上基本原理一样,都是通过Intent启动Activity(fork一个进程出来)

// frameworks/base/services/core/java/com/android/server/wm/ActivityStartController.java
void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason, int displayId) {final ActivityOptions options = ActivityOptions.makeBasic();options.setLaunchWindowingMode(WINDOWING_MODE_FULLSCREEN);if (!ActivityRecord.isResolverActivity(aInfo.name)) {// The resolver activity shouldn't be put in home stack because when the foreground is// standard type activity, the resolver activity should be put on the top of current// foreground instead of bring home stack to front.options.setLaunchActivityType(ACTIVITY_TYPE_HOME);}options.setLaunchDisplayId(displayId);// 启动ActivitymLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason).setOutActivity(tmpOutRecord).setCallingUid(0).setActivityInfo(aInfo).setActivityOptions(options.toBundle()).execute();mLastHomeActivityStartRecord = tmpOutRecord[0];final ActivityDisplay display =mService.mRootActivityContainer.getActivityDisplay(displayId);final ActivityStack homeStack = display != null ? display.getHomeStack() : null;if (homeStack != null && homeStack.mInResumeTopActivity) {// 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();}
}

短压Home键启动Launcher

在这里插入图片描述

短压与长按区分,点一下Home键就属于短压。短压Home键后,Android会启动Home。

// frameworks/base/services/core/java/com/android/server/input/InputManagerService.java// Native callback.
private long interceptKeyBeforeDispatching(IBinder focus, KeyEvent event, int policyFlags) {// native层的 inputservice,通过这个接口上报回调return mWindowManagerCallbacks.interceptKeyBeforeDispatching(focus, event, policyFlags);
}
// frameworks/base/services/core/java/com/android/server/wm/InputManagerCallback.java
/**
* Provides an opportunity for the window manager policy to process a key before
* ordinary dispatch.
*/
@Override
public long interceptKeyBeforeDispatching(IBinder focus, KeyEvent event, int policyFlags) {WindowState windowState = mService.windowForClientLocked(null, focus, false);return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
}

mService是WindowManagerService类型,WindowManagerService中的mPolicy是PhoneWindowManager。PhoneWindowManager作为WMS的Policy配置文件,专门用来处理与UI行为有关的事件。PhoneWindowManager会拦截HomeKey事件进行相应处理后选择不再派发Home(PhoneWindowManager处理完就不需要其他人处理了),或者继续派发HomeKey给当前焦点View。

// frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
private long interceptKeyBeforeDispatchingInner(WindowState win, KeyEvent event,int policyFlags) {final boolean keyguardOn = keyguardOn();final int keyCode = event.getKeyCode();final int repeatCount = event.getRepeatCount();final int metaState = event.getMetaState();final int flags = event.getFlags();final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;final boolean canceled = event.isCanceled();final int displayId = event.getDisplayId();// First we always handle the home key here, so applications// can never break it, although if keyguard is on, we do let// it handle it, because that gives us the correct 5 second// timeout.if (keyCode == KeyEvent.KEYCODE_HOME) {// 处理HomeKeyDisplayHomeButtonHandler handler = mDisplayHomeButtonHandlers.get(displayId);if (handler == null) {handler = new DisplayHomeButtonHandler(displayId);mDisplayHomeButtonHandlers.put(displayId, handler);}return handler.handleHomeButton(win, event);} else if (keyCode == KeyEvent.KEYCODE_MENU) {// 省略}
}/** A handler to handle home keys per display */
private class DisplayHomeButtonHandler {int handleHomeButton(WindowState win, KeyEvent event) {final boolean keyguardOn = keyguardOn();final int repeatCount = event.getRepeatCount();final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;final boolean canceled = event.isCanceled();// If we have released the home key, and didn't do anything else// while it was pressed, then it is time to go home!if (!down) {// 省略// Post to main thread to avoid blocking input pipeline.// 处理短压HomemHandler.post(() -> handleShortPressOnHome(mDisplayId));return -1;}// 省略return -1;}
}private void handleShortPressOnHome(int displayId) {// Turn on the connected TV and switch HDMI input if we're a HDMI playback device.final HdmiControl hdmiControl = getHdmiControl();if (hdmiControl != null) {hdmiControl.turnOnTv();}// If there's a dream running then use home to escape the dream// but don't actually go home.if (mDreamManagerInternal != null && mDreamManagerInternal.isDreaming()) {mDreamManagerInternal.stopDream(false /*immediate*/);return;}// 启动Home// Go home!launchHomeFromHotKey(displayId);
}

PhoneWindowManager针对每个Display创建一个DisplayHomeButtonHandler ,通过它处理HomeKey。在启动Home期间如果开始了dream模式(类似于屏保),会先退出dream。最后调用launchHomeFromHotKey来启动Home,后续流程基本上与Home开机启动一致了。

Launcher异常崩溃后的自启动

在这里插入图片描述
Launcher意外退出(比如崩溃了)时,会触发AMS的forceStopPackage。AMS会再次将Home拉起。

//frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
@Override
public void forceStopPackage(final String packageName, int userId) {try {IPackageManager pm = AppGlobals.getPackageManager();synchronized(this) {int[] users = userId == UserHandle.USER_ALL? mUserController.getUsers() : new int[] { userId };for (int user : users) {if (mUserController.isUserRunning(user, 0)) {// 对每个运行的User,停掉 packageName对应的应用forceStopPackageLocked(packageName, pkgUid, "from pid " + callingPid);finishForceStopPackageLocked(packageName, pkgUid);}}}} finally {Binder.restoreCallingIdentity(callingId);}
}

forceStopPackageLocked函数中,会先Kill掉应用对应的进程。然后 resume focused app,在resume的过程中会拉起Home。

//frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
@GuardedBy("this")
final boolean forceStopPackageLocked(String packageName, int appId,boolean callerWillRestart, boolean purgeCache, boolean doit,boolean evenPersistent, boolean uninstalling, int userId, String reason) {// kill进程boolean didSomething = mProcessList.killPackageProcessesLocked(packageName, appId, userId,ProcessList.INVALID_ADJ, callerWillRestart, true /* allowRestart */, doit,evenPersistent, true /* setRemoved */,packageName == null ? ("stop user " + userId) : ("stop " + packageName));if (doit) {if (purgeCache && packageName != null) {AttributeCache ac = AttributeCache.instance();if (ac != null) {ac.removePackage(packageName);}}if (mBooted) {// resume focused app// 通过这个函数重新拉起HomemAtmInternal.resumeTopActivities(true /* scheduleIdle */);}}return didSomething;
}

调用ActivityTaskManagerServiced的resumeTopActivities函数。在 Home崩溃的情况下,调用这个函数,可以保证Home重新被拉起(这个函数最终会调用到RootActivityContainer 的resumeHomeActivity函数。感兴趣的可以继续顺着代码往下看)

//frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
@Override
public void resumeTopActivities(boolean scheduleIdle) {synchronized (mGlobalLock) {mRootActivityContainer.resumeFocusedStacksTopActivities();if (scheduleIdle) {mStackSupervisor.scheduleIdleLocked();}}
}// frameworks/base/services/core/java/com/android/server/wm/RootActivityContainer.java
boolean resumeFocusedStacksTopActivities(ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {// 省略if (!resumedOnDisplay) {// In cases when there are no valid activities (e.g. device just booted or launcher// crashed) it's possible that nothing was resumed on a display. Requesting resume// of top activity in focused stack explicitly will make sure that at least home// activity is started and resumed, and no recursion occurs.final ActivityStack focusedStack = display.getFocusedStack();if (focusedStack != null) {focusedStack.resumeTopActivityUncheckedLocked(target, targetOptions);}}}return result;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/171281.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring Boot 改版如何解决?使用阿里云创建项目、使用IDEA进行创建

接上次博客&#xff1a;JavaEE进阶&#xff08;2&#xff09;SpringBoot 快速上手&#xff08;环境准备、Maven&#xff1a;核心功能&#xff0c;Maven仓库、第⼀个SpringBoot程序&#xff1a;Spring介绍&#xff0c;Spring Boot介绍、创建项目&#xff09;-CSDN博客 目录 使…

网络篇---第一篇

系列文章目录 文章目录 系列文章目录前言一、HTTP 响应码有哪些?分别代表什么含义?二、Forward 和 Redirect 的区别?三、Get 和 Post 请求有哪些区别?前言 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站,这篇文章男…

ctfshow sql

180 过滤%23 %23被过滤&#xff0c;没办法注释了&#xff0c;还可以用’1’1来闭合后边。 或者使用--%0c-- 1%0corder%0cby%0c3--%0c--1%0cunion%0cselect%0c1,2,database()--%0c--1%0cunion%0cselect%0c1,2,table_name%0cfrom%0cinformation_schema.tables%0cwhere%0ctable_…

YOLO目标检测——背包检测数据集下载分享【含对应voc、coco和yolo三种格式标签】

实际项目应用&#xff1a;各种背包检测数据集说明&#xff1a;背包检测数据集&#xff0c;真实场景的高质量图片数据&#xff0c;数据场景丰富标签说明&#xff1a;使用lableimg标注软件标注&#xff0c;标注框质量高&#xff0c;含voc(xml)、coco(json)和yolo(txt)三种格式标签…

图论——最小割问题

Capacity&#xff08;S&#xff0c;T) Min-Cut(通俗的说就是用最小的力气隔断&#xff09; 最小割并不唯一 最大流最小割定理 对于一个网络流问题&#xff0c;最大流的流量最小割的容量 寻找最小割 可以使用Edmonds-karp or Dinic algorithm 首先寻找任意一个最大流&#xff…

LangChain 10思维链Chain of Thought一步一步的思考 think step by step

LangChain系列文章 LangChain 实现给动物取名字&#xff0c;LangChain 2模块化prompt template并用streamlit生成网站 实现给动物取名字LangChain 3使用Agent访问Wikipedia和llm-math计算狗的平均年龄LangChain 4用向量数据库Faiss存储&#xff0c;读取YouTube的视频文本搜索I…

Linux内核--内存管理(一)任务空间管理

目录 一、引言 二、基本概念梳理 三、用户态进程内存管理 ------>3.1、用户态 ------>3.2、内核态 ------>3.3、内存管理结构 ------>3.4、mm_struct ------>4.5、vm_area_struct 四、内核态结构 ------>4.1、32位内核态结构 ------>4.2、64位…

2018年4月26日 Go生态洞察:Go新品牌形象及标识发布

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

从代码执行,看单片机内存的分配

1、单片机执行指令过程详解 单片机执行程序的过程&#xff0c;实际上就是执行我们所编制程序的过程。即逐条指令的过程。计算机每执行一条指令都可分为三个阶段进行&#xff0c;即取指令--分析指令--执行指令。 取指令的任务是&#xff1a;根据程序计数器PC中的值从程序存储器读…

C语言基础介绍

1. C语言基础知识 C语言是一种计算机编程语言&#xff0c;是一门用于编写系统软件和应用软件的高级语言。C语言的基础知识包括&#xff1a; 数据类型&#xff1a;C语言中的数据类型包括整型、浮点型、字符型等。 变量&#xff1a;C语言中使用变量来存储数据&#xff0c;变量必…

Spring RabbitMQ那些事(2-两种方式实现延时消息订阅)

目录 一、序言二、死信交换机和消息TTL实现延迟消息1、死信队列介绍2、代码示例(1) 死信交换机配置(2) 消息生产者(3) 消息消费者 3、测试用例 三、延迟消息交换机实现延迟消息1、安装延时消息插件2、代码示例(1) 延时消息交换机配置(2) 消息生产者(3) 消息消费者 3、测试用例 …

[Linux]进程创建➕进程终止

文章目录 1.再谈fork()函数1.1fork()创建子进程 OS都做了哪些工作?1.2对上述问题的理解1.3写时拷贝进行父子进程分离的优势1.4了解eip寄存器和pc1.5了解进程的上下文数据1.6对计算机组成的理解1.7fork常规用法1.8fork调用失败的原因 2.进程终止2.1进程终止时操作系统要做的工作…

人工智能-注意力机制之Transformer

Transformer 比较了卷积神经网络&#xff08;CNN&#xff09;、循环神经网络&#xff08;RNN&#xff09;和自注意力&#xff08;self-attention&#xff09;。值得注意的是&#xff0c;自注意力同时具有并行计算和最短的最大路径长度这两个优势。因此&#xff0c;使用自注意力…

13年老鸟总结,性能测试方法汇总+性能响应很慢排查方法(详全)

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 1、性能测试包含哪…

windows的bat文件(学习笔记)

简介 通过windows的cmd执行的批处理&#xff0c;扩展名可以是.bat或.cmd&#xff08;类似linux的shell脚本&#xff09; 所有语句符号不区分大小写 帮助提示信息&#xff1a;命令 /? 1 基本语法 (1) 注释&#xff1a;rem 注释文本不执行 (2) 关闭盘符输出&#xff1a;e…

Java实现-数据结构 2.时间和空间复杂度

.如何衡量一个算法的好坏&#xff1a;时间复杂度和空间复杂度 算法效率分为时间效率和空间效率&#xff0c;时间效率称为时间复杂度&#xff0c;空间效率称为空间复杂度 时间复杂度 算法的时间复杂度是一个数学函数&#xff0c;它描述了算法的运行时间&#xff0c;一个算法执…

vim+xxd编辑十六进制的一个大坑:自动添加0x0a

问题描述 今天在做一个ctf题&#xff0c;它给了一个elf文件&#xff0c;我要做的事情是修复这个elf文件&#xff0c;最后执行它&#xff0c;这个可执行文件会计算它自身的md5作为这道题的flag。我把所有需要修复的地方都修复了&#xff0c;程序也能成功运行&#xff0c;但是fl…

【电路笔记】-快速了电阻

电阻类型 文章目录 电阻类型1、概述2、电阻器的组成类型2.1 碳电阻器2.2 薄膜电阻器2.3 绕线电阻器 3、总结 电阻器是所有电子元件中最基本、最常用的元件&#xff0c;人们几乎认为电阻器是理所当然的&#xff0c;但它们在电路中起着至关重要的作用。 1、概述 有许多不同类型的…

SpringCloud之Gateway(统一网关)

文章目录 前言一、搭建网关服务1、导入依赖2、在application.yml中写配置 二、路由断言工厂Route Predicate Factory三、路由过滤器 GatewayFilter案例1给所有进入userservice的请求添加一个请求头总结 四、全局过滤器 GlobalFilter定义全局过滤器&#xff0c;拦截并判断用户身…

深入解析:如何开发抖音票务小程序

当下&#xff0c;开发抖音票务小程序成为了吸引年轻用户群体的一种创新方式。本文将深入解析如何开发抖音票务小程序&#xff0c;探讨关键步骤和技术要点。 1.确定需求和功能 考虑到抖音的用户特点&#xff0c;可以加入与短视频相关的票务功能&#xff0c;如在线购票、观影记录…