版本:android 10
SystemServer.java
private void startOtherServices() {
WindowManagerService wm = null;
//创建PhoneWindowManager对象,传递给WindowManagerService,同时创建WindowManagerService对象。
wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,
new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
//将WindowManagerService添加到bindler 服务管理中
ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false,
DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
//...
//初始化PhoneWindowManager
wm.onInitReady();
//display相关
wm.displayReady();
//..
wm.systemReady();
}
解析WindowManagerService.java
public static WindowManagerService main(final Context context, final InputManagerService im,final boolean showBootMsgs, final boolean onlyCore, WindowManagerPolicy policy,
ActivityTaskManagerService atm, TransactionFactory transactionFactory) {
DisplayThread.getHandler().runWithScissors(() ->
//创建WindowManagerService对象
sInstance = new WindowManagerService(context, im, showBootMsgs, onlyCore, policy,
atm, transactionFactory), 0);
return sInstance;
}
查看WindowManagerService构造方法
private WindowManagerService(Context context, InputManagerService inputManager,
boolean showBootMsgs, boolean onlyCore, WindowManagerPolicy policy,
ActivityTaskManagerService atm, TransactionFactory transactionFactory) {
//...
//保存PhoneWindowManager对象
mPolicy = policy;
mAnimator = new WindowAnimator(this);
//RootWindowContainer窗口容器的根容器,DisplayContent 用于管理屏幕,一块屏幕对应一个 DisplayContent 对象,
//虽然手机只有一个显示屏,但是可以创建多个 DisplayContent 对象,如投屏时,可以创建一个虚拟的 DisplayContent
mRoot = new RootWindowContainer(this);
//...
//将PhoneWindowManager放入LocalServices
LocalServices.addService(WindowManagerPolicy.class, mPolicy);
//获取Display的代理对象
mDisplayManager =(DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
//创建WindowManagerService内部类LocalService,并且添加到LocalServices
LocalServices.addService(WindowManagerInternal.class, new LocalService());
}
SystemServer中有调用wm.onInitReady(),我们跟踪下这个方法
public void onInitReady() {
//初始化PhoneWindowManagerService
initPolicy();
//...
}
private void initPolicy() {
UiThread.getHandler().runWithScissors(new Runnable() {
@Override
public void run() {
//调用PhoneWindowManagerService的init初始化方法
mPolicy.init(mContext, WindowManagerService.this, WindowManagerService.this);
}
}, 0);
}
跟踪PhoneWindowManagerService的init方法
@Override
public void init(Context context, IWindowManager windowManager,
WindowManagerFuncs windowManagerFuncs) {
mContext = context;
//WindowManagerService
mWindowManager = windowManager;
mWindowManagerFuncs = windowManagerFuncs;
//获取WindowManagerService的内部类LocalService
mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
//创建Handler
mHandler = new PolicyHandler();
//用于广播监听SettingProvider中的数据变化,进行相关逻辑操作
mSettingsObserver = new SettingsObserver(mHandler);
//观察所有的用户改变
mSettingsObserver.observe();
//...
//注册截屏广播
// register for screenshot broadcasts
filter=new IntentFilter();
filter.addAction("android.intent.action.SCREENSHOT");
context.registerReceiver(mScreenshotReceiver, filter);
//一些对象的初始化
//...
}
SystemServer中调用wm.systemReady(),我们跟踪下该方法
public void systemReady() {
mSystemReady = true;
//调用PhoneWindowManager的systemReady方法
mPolicy.systemReady();
//调用RootWindowContainer的forAllDisplayPolicies
mRoot.forAllDisplayPolicies(DisplayPolicy::systemReady);
}