The creation process of Activity's ViewRoot (2)


    On page 4 , let's take a look at the creation process of the ViewRootImpl object. The declaration of the ViewRootImpl class is as follows: public
    final class ViewRootImpl implements ViewParent,
            View.AttachInfo.Callbacks, HardwareRenderer.HardwareDrawCallbacks

    The constructor of the ViewRootImpl class is defined as follows:
    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 }

    Line 13 (ViewRootImpl->ViewRootImpl) will call the getWindowSession function of WindowManagerGlobal. For a detailed analysis of the getWindowSession function, please refer to the page5 file .

    Line 28 (ViewRootImpl->ViewRootImpl) will create an object of type W, and use this object to initialize the member variable mWindow. For the construction process of type W, please refer to the page7 file.
    Line 41 (ViewRootImpl->ViewRootImpl) will create an AttachInfo object, which represents the information of the Window to which ViewRootImpl is attached, and uses the AttachInfo object to initialize the member variable mAttachInfo.
On page 5,
    we analyze the implementation of the getWindowSession function of WindowManagerGlobal:
    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)
    Line 6 (WindowManagerGlobal->getWindowSession) will call getWindowManagerService to get the WindowManager service. For a detailed analysis of the getWindowManagerService function, please refer to the page6 file.
    Lines 7-8 (WindowManagerGlobal->getWindowSession) will call the openSession function with the WindowManagerService just obtained. This will lead to establishing a connection with WindowManagerService. For the part about establishing a connection with WindowManagerService, please refer to related articles.

    Line 15 (WindowManagerGlobal->getWindowSession) will return the Session connection with WindowManagerService.
page6
The getWindowManagerService function of WindowManagerGlobal is defined as follows:
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 }

The main logic of the getWindowManagerService function of WindowManagerGlobal is to obtain the "window" service through Binder.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326069864&siteId=291194637