AMSUML方法依赖图
public final class SystemServer implements Dumpable {
private Context mSystemContext;//2
private SystemServiceManager mSystemServiceManager;//2
private void run() {
...
System.loadLibrary("android_servers");//1
...
mSystemServiceManager = new SystemServiceManager(mSystemContext);//2
/**
* Adds a service instance of the specified interface to the global registry of local services.
* 将指定接口的服务实例mSystemServiceManager添加到本地服务的全局注册中心。
*/
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
...
try {
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartServices");
startBootstrapServices();//3
startCoreServices();//4
startOtherServices();//5
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
...
}
private void startBootstrapServices() {
Installer installer = mSystemServiceManager.startService(Installer.class);
// Activity manager runs the show.
//startService是ssm的方法,最后返回的是Lifecycle类的实例化对象,getService返回的是ams的对象,这一行就是ams对象的实例化启动
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();//1
//将systemservice的mSystemServiceManager和installer对象和ams中的对象保持一致。
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
...
}
}
public final class SystemServiceManager implements Dumpable {
// Whether to use multiple threads to run user lifecycle phases in parallel.
//是否使用多个线程并行运行用户生命周期阶段,默认为true。
private static boolean sUseLifecycleThreadPool = true;
// The default number of threads to use if lifecycle thread pool is enabled.
//启动线程池的默认数量为3
private static final int DEFAULT_MAX_USER_POOL_THREADS = 3;
//SystemServer类中实例化ssm对象后构造方法传过来context对象,接下来实例化List和Set集合。
private final Context mContext;
private List<SystemService> mServices;
private Set<String> mServiceClassnames;
...
SystemServiceManager(Context context) {//2
mContext = context;
mServices = new ArrayList<>();
mServiceClassnames = new ArraySet<>();
// Disable using the thread pool for low ram devices
//这里调用了AM的静态方法判断是否是低内存,然后取反,与线程池开关与运算,低内存的时时候就可以关闭多线程了。
sUseLifecycleThreadPool = sUseLifecycleThreadPool
&& !ActivityManager.isLowRamDeviceStatic();
//这里调用ART虚拟机判断当前可以使用的最大线程数,取最小。
mNumUserPoolThreads = Math.min(Runtime.getRuntime().availableProcessors(),
DEFAULT_MAX_USER_POOL_THREADS);
}
...
/**
* Creates and starts a system service. The class must be a subclass of
* {@link com.android.server.SystemService}.
*
* @param serviceClass A Java class that implements the SystemService interface.
* @return The service instance, never null.
* @throws RuntimeException if the service fails to start.
*/
public <T extends SystemService> T startService(Class<T> serviceClass) {
try {
...
final T service;
try {
//传进来的Lifecycle的构造器constructor
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
catch{...
}
startService(service);
return service;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
}
public void startService(@NonNull final SystemService service) {
// Check if already started
String className = service.getClass().getName();
//不能启动早已启动的service,mServiceClassnames是一个ArraySet<>
if (mServiceClassnames.contains(className)) {
Slog.i(TAG, "Not starting an already started service " + className);
return;
}
mServiceClassnames.add(className);
// Register it.
mServices.add(service);
// Start it.
long time = SystemClock.elapsedRealtime();
try {
service.onStart();
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + service.getClass().getName()
+ ": onStart threw an exception", ex);
}
warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
}
...
}
熬夜写的,有空补充完文本。