首页 > 其他分享 >【framework】ATMS启动流程

【framework】ATMS启动流程

时间:2023-03-19 17:23:33浏览次数:57  
标签:ActivityTaskManagerService ... java service 流程 ATMS framework new

1 前言

​ ATMS 即 ActivityTaskManagerService,用于管理 Activity 及其容器(任务、堆栈、显示等)。ATMS 在 Android 10 中才出现,由原来的 AMS(ActivityManagerService)分离而来,承担了 AMS 的部分职责。因此,在 AMS初始化过程中(AMS启动流程),也伴随着了 ATMS 的初始化。本文主要介绍 ATMS 的启动流程和初始化过程。

(1)ATMS 创建流程

img

  • SystemServer:依次调用 main()、run()、startBootstrapServices(),再调用 SystemServiceManager 的 startService() 方法,并将 Lifecyle.class 传入;
  • SystemServiceManager :startService() 方法通过反射调用 Lifecyle 的构造方法,生成 Lifecyle 对象;
  • Lifecyle:构造方法中调用 ATMS 的构造方法创建 ATMS 对象,并通过 getService() 方法返回 ATMS 对象。

(2)ATMS 初始化

​ 如图,ATMS 在初始化时创建了图中蓝色类的对象。

img

2 ATMS 启动流程

(1)main

​ /frameworks/base/services/java/com/android/server/SystemServer.java

public static void main(String[] args) {
    new SystemServer().run();
}

(2)run

​ /frameworks/base/services/java/com/android/server/SystemServer.java

private void run() {
	try {
		...
		// 创建Looper
		Looper.prepareMainLooper();
		// 加载libandroid_servers.so
		System.loadLibrary("android_servers");
		// 创建系统的 Context:ContextImpl.createSystemContext(new ActivityThread())
		createSystemContext();
		// 创建 SystemServiceManager
		mSystemServiceManager = new SystemServiceManager(mSystemContext);
		LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
		...
	}
	...
	try {
		//启动引导服务,ActivityManagerService、ActivityTaskManagerService、PackageManagerService、PowerManagerService、DisplayManagerService 等
		startBootstrapServices(); 
		//启动核心服务,BatteryService、UsageStatusService 等
		startCoreServices(); 
		//启动其他服务,InputManagerService、WindowManagerService、CameraService、AlarmManagerService 等
		startOtherServices(); 
		...
	}
	...
	// 开启消息循环
	Looper.loop();
}

(3)startBootstrapServices

​ /frameworks/base/services/java/com/android/server/SystemServer.java

private void startBootstrapServices() {
	...
    //启动 ATMS
	ActivityTaskManagerService atm = mSystemServiceManager.startService(ActivityTaskManagerService.Lifecycle.class).getService();
    //启动 AMS,并将 ATMS 注入
	mActivityManagerService = ActivityManagerService.Lifecycle.startService(mSystemServiceManager, atm);
	...
}

(4)startService

​ /frameworks/base/services/core/java/com/android/server/SystemServiceManager.java

public <T extends SystemService> T startService(Class<T> serviceClass) {
	try {
		final String name = serviceClass.getName();
		...
		final T service;
		try { //通过反射调用 serviceClass 的构造方法 创建 Lifecycle 对象
			Constructor<T> constructor = serviceClass.getConstructor(Context.class);
			service = constructor.newInstance(mContext);
		}
		...
		startService(service);
		return service;
	}
	...
}

public void startService(SystemService service) {
	mServices.add(service); //mServices: ArrayList<SystemService>
	...
	try {
		service.onStart(); //调用 Lifecycle 的 onStart 方法
	}
	...
}

(5)ATMS.Lifecycle

/frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.Lifecycle.java

public static final class Lifecycle extends SystemService {
	private final ActivityTaskManagerService mService;

	public Lifecycle(Context context) {//被 SystemServiceManager 的 startService() 方法调用
		super(context);
		mService = new ActivityTaskManagerService(context);
	}

	public void onStart() {
        //添加 ATMS 服务,方便跨进程调用:ServiceManager.addService(Context.ACTIVITY_TASK_SERVICE, mService, false, DUMP_FLAG_PRIORITY_DEFAULT)
		publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService);
		mService.start(); //调用 ATMS 的 start() 方法
	}
	
	...

	public ActivityTaskManagerService getService() {
		return mService;
	}
}

​ 注意:onStart() 方法中调用 ATMS 的 start() 方法初始化(下文还会介绍)。 已通过 ServiceManager.addService() 将 Context.ACTIVITY_TASK_SERVICE 与 ATMS 绑定,因此在其他进程中可以通过如下方式获取 ATMS。

IBinder b = ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE); 
IActivityTaskManager atm = IActivityTaskManager.Stub.asInterface(b);

3 ATMS 初始化

(1)ATMS 的构造方法

​ /frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java

public ActivityTaskManagerService(Context context) {
	mContext = context;
	...
	mSystemThread = ActivityThread.currentActivityThread();
	mUiContext = mSystemThread.getSystemUiContext(); //ContextImpl.createSystemUiContext(getSystemContext())
	mLifecycleManager = new ClientLifecycleManager();
	mInternal = new LocalService(); //ActivityTaskManagerInternal 的子类
	...
}

(2)start

​ start() 方法被 Lifecycle 的 onStart() 方法调用,onStart() 方法又被 SystemServiceManager 的 startService() 方法调用。

​ /frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java

private void start() {
	LocalServices.addService(ActivityTaskManagerInternal.class, mInternal);
}

​ mInternal 属于 LocalService 类(ActivityTaskManagerInternal 的子类),在 ATMS 的构造方法中创建。

(3)initialize

​ 在 AMS 的构造方法中,调用了 ATMS 的 initialize() 方法进一步初始化。

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

public ActivityManagerService(Context systemContext, ActivityTaskManagerService atm) {
	...
	mHandlerThread = new ServiceThread(TAG, THREAD_PRIORITY_FOREGROUND, false);
    ...
    mUserController = new UserController(this);
	mPendingIntentController = new PendingIntentController(mHandlerThread.getLooper(), mUserController);
	...
	mIntentFirewall = new IntentFirewall(new IntentFirewallInterface(), mHandler);
	mActivityTaskManager = atm;
	//进一步初始化 ATMS
	mActivityTaskManager.initialize(mIntentFirewall, mPendingIntentController, DisplayThread.get().getLooper());
	...
}

​ /frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java

public void initialize(IntentFirewall intentFirewall, PendingIntentController intentController, Looper looper) {
	mH = new H(looper);
	mUiHandler = new UiHandler();
	mIntentFirewall = intentFirewall;
	...
	mPendingIntentController = intentController;
	mTempConfig.setToDefaults(); //定义时即被创建:mTempConfig = new Configuration()
	...
	//new ActivityStackSupervisor(this, mH.getLooper())
	mStackSupervisor = createStackSupervisor(); 
	mRootActivityContainer = new RootActivityContainer(this);
	mRootActivityContainer.onConfigurationChanged(mTempConfig);
	...
	mLockTaskController = new LockTaskController(mContext, mStackSupervisor, mH);
	mActivityStartController = new ActivityStartController(this);
	mRecentTasks = createRecentTasks(); //new RecentTasks(this, mStackSupervisor)
	mStackSupervisor.setRecentTasks(mRecentTasks);
	...
}

(4)onActivityManagerInternalAdded

​ 在 AMS 的 start() 方法中,调用了 ATMS 的 onActivityManagerInternalAdded() 方法进一步初始化。

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

private void start() {
	...
	LocalServices.addService(ActivityManagerInternal.class, new LocalService());
    //调用 ATMS 的 onActivityManagerInternalAdded 方法进一步初始化
	mActivityTaskManager.onActivityManagerInternalAdded();
	mUgmInternal.onActivityManagerInternalAdded();
	mPendingIntentController.onActivityManagerInternalAdded();
	...
}

​ /frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java

public void onActivityManagerInternalAdded() {
	synchronized (mGlobalLock) {
		mAmInternal = LocalServices.getService(ActivityManagerInternal.class);
		mUgmInternal = LocalServices.getService(UriGrantsManagerInternal.class);
	}
}

(5)ActivityStartController

​ ActivityStartController 作为 ATMS 的一个重要成员,控制了 Activity 的启动,因此我们继续跟进 ActivityStartController 的构造方法。

​ /frameworks/base/services/core/java/com/android/server/am/ActivityStartController.java

ActivityStartController(ActivityTaskManagerService service) {
	this(service, service.mStackSupervisor,
			new DefaultFactory(service, service.mStackSupervisor,
				new ActivityStartInterceptor(service, service.mStackSupervisor)));
}

ActivityStartController(ActivityTaskManagerService service, ActivityStackSupervisor supervisor,
		Factory factory) {
	mService = service;
	mSupervisor = supervisor;
	mHandler = new StartHandler(mService.mH.getLooper());
	mFactory = factory;
	mFactory.setController(this);
	...
}

(6)DefaultFactory

​ DefaultFactory 是 ActivityStarter 的静态内部类,负责 ActivityStarter 的创建和回收。因此我们继续跟进 DefaultFactory 类。

​ /frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.DefaultFactory.java

static class DefaultFactory implements Factory {
	...
	private ActivityStartController mController;
	private ActivityTaskManagerService mService;
	private ActivityStackSupervisor mSupervisor;
	private ActivityStartInterceptor mInterceptor;
	
	//MAX_STARTER_COUNT = 3
	private SynchronizedPool<ActivityStarter> mStarterPool = new SynchronizedPool<>(MAX_STARTER_COUNT);

	DefaultFactory(ActivityTaskManagerService service, ActivityStackSupervisor supervisor, ActivityStartInterceptor interceptor) {
		mService = service;
		mSupervisor = supervisor;
		mInterceptor = interceptor;
	}

	public ActivityStarter obtain() {
		ActivityStarter starter = mStarterPool.acquire();
		if (starter == null) {
			starter = new ActivityStarter(mController, mService, mSupervisor, mInterceptor);
		}
		return starter;
	}

	@Override
	public void recycle(ActivityStarter starter) {
		mStarterPool.release(starter);
	}
}

标签:ActivityTaskManagerService,...,java,service,流程,ATMS,framework,new
From: https://www.cnblogs.com/zhyan8/p/17233661.html

相关文章

  • 【framework】IMS启动流程
    1前言​IMS是InputManagerService的简称,主要负责输入事件管理。1.1基本概念输入设备:屏幕、电源/音量、键鼠、充电口、蓝牙、wifi等设备节点:当输入设备可用......
  • 【framework】应用进程启动流程
    1前言Activity启动流程中介绍了从点击桌面上应用快捷方式到Activity的onCreate()方法调用流程,本将介绍应用进程的启动流程。由于应用进程启动流程复杂,本文按进......
  • 【framework】InputChannel创建流程
    1前言IMS启动流程中介绍了IMS在Java层和Native层的初始化流程,以及创建NativeInputManager、InputManager、InputReader、InputDispatcher、EventHub等对象......
  • 【framework】surfaceflinger启动流程
    1前言​surfaceflinger的作用是合成来自WMS的Surface数据,并发送到显示设备。​SurfaceFlinger服务不同于AMS、WMS、IMP、PMS、DMS等服务,主要区别如下:......
  • java的流程控制-break continue
    breakcontinuebreak:在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break也在switch语句中使用) continue:在......
  • Vue.js props配置(微信收款确认流程)
    视频直接修改外来数据props会产生警告props配置项1.功能:让组件接收外部传过来的数据2.传递数据:```<Demoname="xxx"/>```3.接收数据:1.第一种方式(只接收......
  • java流程控制
    java流程控制Scanner对象通过新建Scanner类来获取用户的输入基本语法:Scannerscanner=newScanner(system.in)Stringstr=scanner.nextLine(),inti=scanner.ne......
  • python安装robotframework的一些常见的错误
    python安装robotframework的一些常见的错误首先的电脑环境是x86的,然后下载的python版本起初是3.10.1的在cmd中出入pipinstallrobotframwork是没有问题的,但是在输入下......
  • 【Python从入门到进阶】10、流程控制语句-循环语句(for-while)
    接上篇《9、流程控制语句-条件语句(if-else)》上一篇我们学习了Python的控制流语句的概念,以及其中的条件语句(if/else),本篇我们来学习控制流语句中的循环语句(for/while)。......
  • 逻辑备份与online ddl流程
    逻辑备份:6步   onlineddl:5步 ......