Android Framework- AMS/ATMS/AS/ASC - 简书 (jianshu.com)
这篇中简要讲到这几个的作用及关系.
这里看看 ASC(ActivityStartController ) / AS(ActivityStarter) 的源码,加深理解.
下面将以 ASC 和 AS 简称. 源码基于 API34(UOS)
1. ASC源码:
ASC (ActivityStartController )
用于 委派 启动 Activity 的控制器。
此类的主要目标是 获取外部活动 启动请求,
并将它们准备为一系列可由 {@link ActivityStarter} 处理的 离散Activity启动
/**
* Controller for delegating activity launches.
*
* This class' main objective is to take external activity start requests and prepare them into
* a series of discrete activity launches that can be handled by an {@link ActivityStarter}. It is
* also responsible for handling logic that happens around an activity launch, but doesn't
* necessarily influence the activity start. Examples include power hint management, processing
* through the pending activity list, and recording home activity launches.
*/
public class ActivityStartController {
}
它的构造函数:
ActivityStartController(ActivityTaskManagerService service) {
this(service, service.mTaskSupervisor,
new DefaultFactory(service, service.mTaskSupervisor,
new ActivityStartInterceptor(service, service.mTaskSupervisor)));
}
@VisibleForTesting
ActivityStartController(ActivityTaskManagerService service, ActivityTaskSupervisor supervisor,
Factory factory) {
mService = service;
mSupervisor = supervisor;
mFactory = factory;
mFactory.setController(this);
mPendingRemoteAnimationRegistry = new PendingRemoteAnimationRegistry(service.mGlobalLock,
service.mH);
它持有 ATMS 对象, ATS 对象, 以及 Factory对象。
那么这个 Factory 类是什么?
看它导入的包是
import com.android.server.wm.ActivityStarter.Factory;
也即是在 AS 里定义的. 下面先看 AS 源码,然后再看这个 Factory 是什么。
2. AS 源码
AS(ActivityStarter)
用于解析如何启动 Activity 的 控制器
此类 收集 用于确定如何 将Intent和Flag转换为Activity, 以及关联的任务和根任务的所有逻辑。
package com.android.server.wm;
/**
* Controller for interpreting how and then launching an activity.
*
* This class collects all the logic for determining how an intent and flags should be turned into
* an activity and associated task and root task.
*/
class ActivityStarter {
}
2.1 AS.Factory 源码
看它的 Factory, 实际上是一个接口, :
/**
* An interface that to provide {@link ActivityStarter} instances to the controller. This is
* used by tests to inject their own starter implementations for verification purposes.
*/
@VisibleForTesting
interface Factory {
/**
* Sets the {@link ActivityStartController} to be passed to {@link ActivityStarter}.
*/
void setController(ActivityStartController controller);
/**
* Generates an {@link ActivityStarter} that is ready to handle a new start request.
* @return an {@link ActivityStarter}
*/
ActivityStarter obtain();
/**
* Recycles a starter for reuse.
*/
void recycle(ActivityStarter starter);
}
通过 setController 注入 ASC, 并且 通过 obtain 获取本身 AS 对象, recycle 进行重新利用.
2.2 AS.Factory 的默认实现类 DefaultFactory
提供了默认的实现.
/**
* Default implementation of {@link StarterFactory}.
*/
static class DefaultFactory implements Factory {
/**
* The maximum count of starters that should be active at one time:
* 1. last ran starter (for logging and post activity processing)
* 2. current running starter
* 3. starter from re-entry in (2)
*/
private final int MAX_STARTER_COUNT = 3;
private ActivityStartController mController;
private ActivityTaskManagerService mService;
private ActivityTaskSupervisor mSupervisor;
private ActivityStartInterceptor mInterceptor;
private SynchronizedPool<ActivityStarter> mStarterPool =
new SynchronizedPool<>(MAX_STARTER_COUNT);
DefaultFactory(ActivityTaskManagerService service,
ActivityTaskSupervisor supervisor, ActivityStartInterceptor interceptor) {
mService = service;
mSupervisor = supervisor;
mInterceptor = interceptor;
}
@Override
public void setController(ActivityStartController controller) {
mController = controller;
}
@Override
public ActivityStarter obtain() {
ActivityStarter starter = mStarterPool.acquire();
if (starter == null) {
if (mService.mRootWindowContainer == null) {
throw new IllegalStateException("Too early to start activity.");
}
starter = new ActivityStarter(mController, mService, mSupervisor, mInterceptor);
}
return starter;
}
@Override
public void recycle(ActivityStarter starter) {
starter.reset(true /* clearRequest*/);
mStarterPool.release(starter);
}
}
仔细看 obtain() 函数,它返回了 AS 的实例对象.
它先是从线程池 mStarterPool (大小为3)中获取,如果不存在则 new AS 实例。
3. ASC 创建中 AS.DefaultFactory
在 ASC的构造函数中,就创建这个对象.
ActivityStartController(ActivityTaskManagerService service) {
this(service, service.mTaskSupervisor,
new DefaultFactory(service, service.mTaskSupervisor,
new ActivityStartInterceptor(service, service.mTaskSupervisor)));
}
4. ASC 获取 AS 对象
在ATMS 中,可以通过 ASC 获取 AS 对象,然后执行启动 Activity 请求
private int startActivityAsUser(IApplicationThread caller, String callingPackage, ) {
// TODO: Switch to user app stacks here.
return getActivityStartController().obtainStarter(intent, "startActivityAsUser")
.setCaller(caller)
.setCallingPackage(callingPackage)
.setCallingFeatureId(callingFeatureId)
.setResolvedType(resolvedType)
.setResultTo(resultTo)
.setResultWho(resultWho)
.setRequestCode(requestCode)
.setStartFlags(startFlags)
.setProfilerInfo(profilerInfo)
.setActivityOptions(opts)
.setUserId(userId)
.execute();
}
ASC.obtainStarter 源码为:
/**
* @return A starter to configure and execute starting an activity. It is valid until after
* {@link ActivityStarter#execute} is invoked. At that point, the starter should be
* considered invalid and no longer modified or used.
*/
ActivityStarter obtainStarter(Intent intent, String reason) {
return mFactory.obtain().setIntent(intent).setReason(reason);
}
AS 执行请求的将在后续单独介绍.
AS.execute() / executeRequest()
--