[Android FrameWork 6.0 source code learning] The addView method of WindowManager in View repainting process,

Source: Internet
Author: User

[Android FrameWork 6.0 source code learning] The addView method of WindowManager in View repainting process,

Blog home: http://www.cnblogs.com/kezhuang/p/


I have analyzed the construction process of the contentView OF THE Activity IN MY BLOG. If you do not know it, please take a look.

[Android FrameWork 6.0 source code learning] Window class analysis

The blog in this chapter is followed by the previous blog analysis. The purpose is to introduce the ViewRootImpl class. Now we have analyzed the call process of Window and ActivityThread.

From ActivityThread to WindowManager to ViewRootImpl, this part is still blank.

At the end of the previous blog, the addView of WindowManager is used to request the display interface.

WindowManager is an interface that inherits the ViewManager interface, so it has the ability to manage views.

The WindowManager instance is obtained through the Activity. getWindowManager method. We first find its instance and gradually go to The addView function.

    public WindowManager getWindowManager() {        return mWindowManager;    }

 

This function is defined in Activity in this way. The mWindowManager object is returned. Next, find the location where the mWindowManager object is assigned.

    final void attach(Context context, ActivityThread aThread,            Instrumentation instr, IBinder token, int ident,            Application application, Intent intent, ActivityInfo info,            CharSequence title, Activity parent, String id,            NonConfigurationInstances lastNonConfigurationInstances,            Configuration config, String referrer, IVoiceInteractor voiceInteractor) {            mWindow = new PhoneWindow(this);        mWindow.setCallback(this);        mWindow.setOnWindowDismissedCallback(this);        mWindow.getLayoutInflater().setPrivateFactory(this);        mWindow.setWindowManager(                (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),                mToken, mComponent.flattenToString(),                (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);        if (mParent != null) {            mWindow.setContainer(mParent.getWindow());        }        mWindowManager = mWindow.getWindowManager();    }

 

We can see that the mWindowManager method is assigned to the attach method called during Activity initialization, but this value is obtained from the mWindow object. Let's take a look.

    public WindowManager getWindowManager() {        return mWindowManager;    }

 

This is also defined in Window. mWindowManager is returned.

    public void setWindowManager(WindowManager wm, IBinder appToken, String appName,            boolean hardwareAccelerated) {        mAppToken = appToken;        mAppName = appName;        mHardwareAccelerated = hardwareAccelerated                || SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);        if (wm == null) {            wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);        }        mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);    }

 

The mWindowManager in Window is created in setWindowManager, which is called in the attach method of the Activity.
We can see that the system service WindowManager is used to convert it into WindowManagerImpl, and then the object obtained by calling the createLocalWindowManager method is called.

    public WindowManagerImpl createLocalWindowManager(Window parentWindow) {        return new WindowManagerImpl(mDisplay, parentWindow);    }

 

This method is also very simple, that is, new WindowManagerImpl

Okay. Now we can know that the Implementation class of WindowManager is WindowManagerImpl, so we will continue to analyze the addView method implemented by it.

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

 

This method is defined in WindowManagerImpl. mGlobal is a WindowManagerGlobal object and is a system singleton object.

Public void addView (View view, ViewGroup. layoutParams params, Display display, Window parentWindow) {if (view = null) {throw new IllegalArgumentException ("view must not be null");} if (display = null) {throw new IllegalArgumentException ("display must not be null");} if (! (Params instanceof WindowManager. layoutParams) {throw new IllegalArgumentException ("Params must be WindowManager. layoutParams ");} final WindowManager. layoutParams wparams = (WindowManager. layoutParams) params; if (parentWindow! = Null) {parentWindow. adjustLayoutParamsForSubWindow (wparams);} else {// If there's no parent, then hardware acceleration for this view is // set from the application's hardware acceleration setting. final Context context = view. getContext (); if (context! = Null & (context. getApplicationInfo (). flags & ApplicationInfo. FLAG_HARDWARE_ACCELERATED )! = 0) {wparams. flags | = WindowManager. layoutParams. FLAG_HARDWARE_ACCELERATED;} ViewRootImpl root; View panelParentView = null; synchronized (mLock) {// Start watching for system property changes. if (mSystemPropertyUpdater = null) {mSystemPropertyUpdater = new Runnable () {@ Override public void run () {synchronized (mLock) {for (int I = mRoots. size ()-1; I> = 0; -- I) {mRoots. get (I ). loadSystemProperties () ;}}}; SystemProperties. addChangeCallback (mSystemPropertyUpdater);} int index = findViewLocked (view, false); if (index> = 0) {if (mDyingViews. contains (view) {// Don't wait for MSG_DIE to make it's way through root's queue. mRoots. get (index ). doDie ();} else {throw new IllegalStateException ("View" + view + "has already been added to the window manager. ");} // The previous removeView () had not completed executing. now it has .} // If this is a panel window, then find the window it is being // attached to for future reference. if (wparams. type> = WindowManager. layoutParams. FIRST_SUB_WINDOW & wparams. type <= WindowManager. layoutParams. LAST_SUB_WINDOW) {final int count = mViews. size (); for (int I = 0; I <count; I ++) {if (mRoots. get (I ). mWindow. asBinder () = wparams. token) {panelParentView = mViews. get (I) ;}}// a new ViewRootImpl object is created. root = new ViewRootImpl (view. getContext (), display); view. setLayoutParams (wparams); // save ViewRootImpl and view and add the Layout parameter mViews for the current interface. add (view); mRoots. add (root); mParams. add (wparams);} // do this last because it fires off messages to start doing things try {// call the setView method in ViewRootImpl to prepare the redrawing process of the View, and draw it to root on the interface. setView (view, wparams, panelParentView);} catch (RuntimeException e) {// BadTokenException or InvalidDisplayException, clean up. synchronized (mLock) {final int index = findViewLocked (view, false); if (index >=0) {removeViewLocked (index, true) ;}} throw e ;}}

 

This blog is over now. Next I will summarize the above process.
1. A WindowManager object is created during Activity initialization (which is different from the system's WindowManager service)
2. Use this Activity's exclusive WindowManager object to request to add a View to the current interface.
3. WindowManager finds its own implementation class WindowManagerImpl, and then delegates it to the system Singleton class WindowManagerGlobal for processing.
4. WindowManagerGlobal creates a ViewRootImpl object.
5. The ViewRootImpl object calls the setView method, re-draws the interface, and finally submits it to the Surface for interface display.


OK. The process from WindowManager to ViewRootImpl is clearly analyzed. Next we can analyze the javasmtraversals methods of ViewRootImpl. setView and ViewRootImpl.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.