阅读更多
page4
我们看一下ViewRootImpl对象的创建过程, ViewRootImpl类的声明如下:
public final class ViewRootImpl implements ViewParent,
View.AttachInfo.Callbacks, HardwareRenderer.HardwareDrawCallbacks
ViewRootImpl类的构造函数定义如下:
1 public ViewRootImpl(Context context, Display display) {
2 super();
3
4 if (MEASURE_LATENCY) {
5 if (lt == null) {
6 lt = new LatencyTimer(100, 1000);
7 }
8 }
9
10 // Initialize the statics when this class is first instantiated. This is
11 // done here instead of in the static block because Zygote does not
12 // allow the spawning of threads.
13 mWindowSession = WindowManagerGlobal.getWindowSession(context.getMainLooper());
14 mDisplay = display;
15
16 CompatibilityInfoHolder cih = display.getCompatibilityInfo();
17 mCompatibilityInfo = cih != null ? cih : new CompatibilityInfoHolder();
18
19 mThread = Thread.currentThread();
20 mLocation = new WindowLeaked(null);
21 mLocation.fillInStackTrace();
22 mWidth = -1;
23 mHeight = -1;
24 mDirty = new Rect();
25 mTempRect = new Rect();
26 mVisRect = new Rect();
27 mWinFrame = new Rect();
28 mWindow = new W(this);
29 mTargetSdkVersion = context.getApplicationInfo().targetSdkVersion;
30 mInputMethodCallback = new InputMethodCallback(this);
31 mViewVisibility = View.GONE;
32 mTransparentRegion = new Region();
33 mPreviousTransparentRegion = new Region();
34 mFirst = true; // true for the first time the view is added
35 mAdded = false;
36 mAccessibilityManager = AccessibilityManager.getInstance(context);
37 mAccessibilityInteractionConnectionManager =
38 new AccessibilityInteractionConnectionManager();
39 mAccessibilityManager.addAccessibilityStateChangeListener(
40 mAccessibilityInteractionConnectionManager);
41 mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this);
42 mViewConfiguration = ViewConfiguration.get(context);
43 mDensity = context.getResources().getDisplayMetrics().densityDpi;
44 mNoncompatDensity = context.getResources().getDisplayMetrics().noncompatDensityDpi;
45 mFallbackEventHandler = PolicyManager.makeNewFallbackEventHandler(context);
46 mProfileRendering = Boolean.parseBoolean(
47 SystemProperties.get(PROPERTY_PROFILE_RENDERING, "false"));
48 mChoreographer = Choreographer.getInstance();
49
50 PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
51 mAttachInfo.mScreenOn = powerManager.isScreenOn();
52 loadSystemProperties();
53 }
第13行(ViewRootImpl->ViewRootImpl)会调用WindowManagerGlobal的getWindowSession函数, 关于getWindowSession函数的详细分析可以参考page5文件.
第28行(ViewRootImpl->ViewRootImpl)会创建一个W类型的对象, 并用该对象来初始化成员变量mWindow. 关于W类型的构造过程可以参考page7文件.
第41行(ViewRootImpl->ViewRootImpl)会创建一个AttachInfo对象, 表示ViewRootImpl所依附的Window的信息, 并用该AttachInfo对象初始化成员变量mAttachInfo.
page5
我们分析一下WindowManagerGlobal的getWindowSession函数的实现:
1 public static IWindowSession getWindowSession(Looper mainLooper) {
2 synchronized (WindowManagerGlobal.class) {
3 if (sWindowSession == null) {
4 try {
5 InputMethodManager imm = InputMethodManager.getInstance(mainLooper);
6 IWindowManager windowManager = getWindowManagerService();
7 sWindowSession = windowManager.openSession(
8 imm.getClient(), imm.getInputContext());
9 float animatorScale = windowManager.getAnimationScale(2);
10 ValueAnimator.setDurationScale(animatorScale);
11 } catch (RemoteException e) {
12 Log.e(TAG, "Failed to open window session", e);
13 }
14 }
15 return sWindowSession;
16 }
17 }
第5行(WindowManagerGlobal->getWindowSession)
第6行(WindowManagerGlobal->getWindowSession)会调用getWindowManagerService来得到WindowManager服务, 关于getWindowManagerService函数的详细分析可以参考page6文件.
第7-8行(WindowManagerGlobal->getWindowSession)会用刚刚获得的WindowManagerService调用openSession函数, 其实这会导致和WindowManagerService建立连接. 关于和WindowManagerService建立连接的部分可以参考相关系列的文章.
第15行(WindowManagerGlobal->getWindowSession)会返回和WindowManagerService的Session连接.
page6
WindowManagerGlobal的getWindowManagerService函数定义如下:
1 public static IWindowManager getWindowManagerService() {
2 synchronized (WindowManagerGlobal.class) {
3 if (sWindowManagerService == null) {
4 sWindowManagerService = IWindowManager.Stub.asInterface(
5 ServiceManager.getService("window"));
6 }
7 return sWindowManagerService;
8 }
9 }
WindowManagerGlobal的getWindowManagerService函数的主要逻辑就是通过Binder来获得"window"服务.