ATMS在我看来属于AMS的一个辅助类,剥离了AMS中的一些功能到ATMS,下面引用官方的简单说明
System service for managing activities and their containers (task, displays,... ).
//frameworks/base/services/java/com/android/server/SystemServer.java
startBootstrapServices
ActivityTaskManagerService atm = mSystemServiceManager.startService(ActivityTaskManagerService.Lifecycle.class).getService();
根据SystemServiceManager章节的分析可知 startService(ActivityTaskManagerService.Lifecycle.class) 对应代码如下
startService(ActivityTaskManagerService.Lifecycle.class)
Lifecycle(Context context)
//code 1
super(context);
//code 2
mService = new ActivityTaskManagerService(context);
...
mInternal = new LocalService();//final ActivityTaskManagerInternal mInternal;
ActivityTaskManagerService.Lifecycle.onStart()
//code 3
publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService);
//code 4
mService.start();// mService type is ActivityTaskManagerService
||
LocalServices.addService(ActivityTaskManagerInternal.class, mInternal);
父类 SystemService 在构造的时候会记录下传入的Context,获取的时候可以使用 getContext。
public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
public ActivityTaskManagerService(Context context) {
mContext = context;
mFactoryTest = FactoryTest.getMode();
mSystemThread = ActivityThread.currentActivityThread();
mUiContext = mSystemThread.getSystemUiContext();
mLifecycleManager = new ClientLifecycleManager();
mVisibleActivityProcessTracker = new VisibleActivityProcessTracker(this);
mInternal = new LocalService();
GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", GL_ES_VERSION_UNDEFINED);
mWindowOrganizerController = new WindowOrganizerController(this);
mTaskOrganizerController = mWindowOrganizerController.mTaskOrganizerController;
mTaskFragmentOrganizerController =
mWindowOrganizerController.mTaskFragmentOrganizerController;
mBackNavigationController = BackNavigationController.isEnabled()
? new BackNavigationController() : null;
}
}
这里就只展开一级代码,主要注意下 mInternal,因为最终调用ATMS的接口实现大都位于 mInternal 即 LocalService。至于其他变量的的作用放到具体的场景再涉及,此处先关注调用流程相关的部分。
//frameworks/base/services/core/java/com/android/server/SystemService.java
publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService)
publishBinderService(name, service, false);
publishBinderService(name, service, allowIsolated, DUMP_FLAG_PRIORITY_DEFAULT);
ServiceManager.addService(name, service, allowIsolated, dumpPriority);
在设置了一些标志位之后,最终的实现还是调用ServiceManager.addService 想系统中注册本ATM服务,如此其他有权限的用户就可以通过 ACTIVITY_TASK_SERVICE 服务名来跨进程调用ATM服务了。注意了注册进去的服务实例在code2处实例化,
mService = new ActivityTaskManagerService(context);
前面提到过ATM服务的接口实现,最终是调用到 mInternal 的即内部类 LocalService 。这一步和上面提到的ATM服务的注册作用是一样的,只是一个用于支持跨进程,另一个用于本进程内部使用。显然,进程内部调用是没必要走跨进程流程的,所以直接注册 LocalService 的实例而非 ATM服务的实例。下面看下 LocalService.add 的实现
//frameworks/base/core/java/com/android/server/LocalServices.java
public static <T> void addService(Class<T> type, T service) {
synchronized (sLocalServiceObjects) {
if (sLocalServiceObjects.containsKey(type)) {
throw new IllegalStateException("Overriding service registration");
}
sLocalServiceObjects.put(type, service);
}
}
sLocalServiceObjects 的定义如下,其位静态私有变量属于类
//frameworks/base/core/java/com/android/server/LocalServices.java
private static final ArrayMap<Class<?>, Object> sLocalServiceObjects =
new ArrayMap<Class<?>, Object>();
然后使用类作为key,对应类的实例化作为value进行存储。在本进程内可以调用 getService 来获取对应的实例。
AMS的启动也ATMS类似,都是始于SystemServer,位于system_server 进程中,下面是对应的代码
//frameworks/base/services/java/com/android/server/SystemServer.java
//private ActivityManagerService mActivityManagerService;
mActivityManagerService = ActivityManagerService.Lifecycle.startService(mSystemServiceManager, atm);
sAtm = atm;
return ssm.startService(ActivityManagerService.Lifecycle.class).getService();
ssm也就是SystemServer侧传递过来的 SystemServiceManager ,根据SystemServiceManager章节的分析可知 ssm.startService等同如下
//frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
ssm.startService(ActivityManagerService.Lifecycle.class)
Lifecycle(Context context)
super(context);
mService = new ActivityManagerService(context, sAtm);
...
mInternal = new LocalService();
onStart
mService.start();//mService type is ActivityManagerService
mBatteryStatsService.publish();
mAppOpsService.publish();
mProcessStats.publish();
LocalServices.addService(ActivityManagerInternal.class, mInternal);
LocalManagerRegistry.addManager(ActivityManagerLocal.class,
(ActivityManagerLocal) mInternal);
mActivityTaskManager.onActivityManagerInternalAdded();
mPendingIntentController.onActivityManagerInternalAdded();
mAppProfiler.onActivityManagerInternalAdded();
CriticalEventLog.init();
和ATMS的套路是一致的,这里也只展开一层的调用。不过需要注意下 mInternal,其为 LocalService 的实例,
public final class LocalService extends ActivityManagerInternal
implements ActivityManagerLocal {
}
因此,client调用到AMS的时候,大部分的实现都是 mInternal 实现的。此处主要注意下调用流程。