Android window mechanism

DecorView by PhoneWindow communication with WindowManagerService Schematic

Here Insert Picture Description

Each Activity has a Window object that is PhoneWindow type.
Each Window object which maintains a WindowManager object.

Activity which added a View is achieved by a method of addView WindowManager () of

Related key classes

WindowManagerService
high-level, window management service system

Window
a an abstract base class appearance and behavior strategies of top-level window.

Window is not real, it represents an abstract feature set, Android View is the view presented in the form, not the Window View is drawn to the screen, but the View can not exist alone, it must rely on the abstract in Window above concept, Android relies window in view there is provided Activity, Dialog, Toast, PopupWindow, StatusBarWindow ( system status bar), input windows, etc., thus Activity, Dialog and so maintains a window object. When we call the Activity of setContentView (), in fact, eventually calls setContentView Window of (), when we call the Activity of findViewById () when, in fact, the final call is the Window of findViewById ().
Window operation is accomplished by WindowManager.

WindowManager
WindowManager is an interface, which is commonly used methods are: add View, update, and delete View View, WindowManager inherited from ViewManager, these three methods defined in ViewManager in:

public interface ViewManager
{
    public void addView(View view, ViewGroup.LayoutParams params);
    public void updateViewLayout(View view, ViewGroup.LayoutParams params);
    public void removeView(View view);
}

WindowManagerImpl
low level, responsible for communicating with the operating system window management services, to associate with Context.
Is WindowManager implementation class, most of the work accomplished by WindowManagerGlobal.
WindowManagerGlobal
low level, responsible for communicating with the operating system window management services, not to associate with Context.

ViewRootImpl
ViewRootImpl is a top view of the hierarchy, which implements the protocol required between View and WindowManager as most of the internal WindowManagerGlobal implemented, it said WindowManagerGlobal method eventually calls the most ViewRootImpl.
Similar ApplicationThread in charge of communication, like AMS, an important role ViewRootImpl is that it (in IWindowSession instance static variables sWindowSession communication with WMS WindowManagerGlobal creation) to communicate with the WMS.

Source code analysis

ActivityThread.handleResumeActivity()方法 :

  @Override
    public void handleResumeActivity(IBinder token, boolean finalStateRequest, boolean isForward, String reason) {
    ...//其他代码
          if (a.mVisibleFromClient) {
                if (!a.mWindowAdded) {
                    a.mWindowAdded = true;
                    wm.addView(decor, l);
                } else {
                    // The activity will get a callback for this {@link LayoutParams} change
                    // earlier. However, at that time the decor will not be set (this is set
                    // in this method), so no action will be taken. This call ensures the
                    // callback occurs with the decor set.
                    a.onWindowAttributesChanged(l);
                }
            }
    ...//其他代码
 }

Call WindowManagerImpl.addView ():

    @Override
    public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
        applyDefaultToken(params);
        mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
    }

Call WindowManagerGlobal.addView ():

    public void addView(View view, ViewGroup.LayoutParams params,
            Display display, Window parentWindow) {
        ...//其他代码
            root = new ViewRootImpl(view.getContext(), display);

            view.setLayoutParams(wparams);

            mViews.add(view);
            mRoots.add(root);
            mParams.add(wparams);

            // do this last because it fires off messages to start doing things
            try {
                root.setView(view, wparams, panelParentView);
            } catch (RuntimeException e) {
                // BadTokenException or InvalidDisplayException, clean up.
                if (index >= 0) {
                    removeViewLocked(index, true);
                }
                throw e;
            }
        }
    }

Call ViewRootImpl.java method of setView

    /**
     * We have one child
     */
    public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
        synchronized (this) {
        
         		...//其他代码
         		
         		
                // Schedule the first layout -before- adding to the window
                // manager, to make sure we do the relayout before receiving
                // any other events from the system.
                requestLayout(); //请求布局
                if ((mWindowAttributes.inputFeatures
                        & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
                    mInputChannel = new InputChannel();
                }
                mForceDecorViewVisibility = (mWindowAttributes.privateFlags
                        & PRIVATE_FLAG_FORCE_DECOR_VIEW_VISIBILITY) != 0;
                try {
                    mOrigWindowType = mWindowAttributes.type;
                    mAttachInfo.mRecomputeGlobalAttributes = true;
                    collectViewAttributes();
                    res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
                            getHostVisibility(), mDisplay.getDisplayId(), mWinFrame,
                            mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
                            mAttachInfo.mOutsets, mAttachInfo.mDisplayCutout, mInputChannel);
                } 

			    ...//其他代码

}

res = mWindowSession.addToDisplay () is called Session of addToDisplay () method:

    @Override
    public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
            int viewVisibility, int displayId, Rect outFrame, Rect outContentInsets,
            Rect outStableInsets, Rect outOutsets,
            DisplayCutout.ParcelableWrapper outDisplayCutout, InputChannel outInputChannel) {
        return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId, outFrame,
                outContentInsets, outStableInsets, outOutsets, outDisplayCutout, outInputChannel);
    }

Call mService.addWindow () method to add Window, in fact, this mService WindowManagerService objects, that is to say the final layout is WindowManagerService to add.
Analysis can be seen here, ViewRootImpl by mWindowSession (from WindowManagerGlobal sWindowSession object) and WindowManagerService communication.

Reference:
ViewManager, ViewRootImp, WindowManagerImpl, and other detailed analysis WindowSession
Android window mechanism (a) - Window, PhoneWindow, DecorView appreciated
Window window mechanism (b) - Window, WindowManager appreciated
Window window mechanism (c) - WindowManager, ViewRootImpl, View understand
Android window mechanism
Android window mechanism (four) ViewRootImpl with View and WindowManager
Android WindowManagerService
relationship between window, WindowManager and WindowManagerService

android window (three) ViewRootImpl
ViewRootImpl monologue, I'm not a View (layout article)

ViewRootImpl source code analysis event distribution

Advanced window mechanism of the interview

https://www.androidos.net.cn/android/9.0.0_r8/xref/frameworks/base/core/java/android/view/Window.java
https://www.androidos.net.cn/android/9.0.0_r8/xref/frameworks/base/core/java/android/view/WindowManagerImpl.java
https://www.androidos.net.cn/android/9.0.0_r8/xref/frameworks/base/core/java/android/view/WindowManagerGlobal.java
https://www.androidos.net.cn/android/9.0.0_r8/xref/frameworks/base/core/java/android/view/ViewRootImpl.java
http://androidos.net.cn/android/9.0.0_r8/xref/frameworks/base/services/core/java/com/android/server/wm/Session.java
https://www.androidos.net.cn/android/9.0.0_r8/xref/frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java

Published 516 original articles · won praise 89 · views 730 000 +

Guess you like

Origin blog.csdn.net/yzpbright/article/details/104583793