Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the questions about Window?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly talks about "what are the problems about Window". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the problems about Window"?

What is Window if you encounter these problems? The relationship with View? What is WindowManager? The relationship with WMS? How do I add a Window? How Window can be displayed to the lock screen interface Window all three types of cases, what the display level is. Does Window mean PhoneWindow? When was PhoneWindow created? What should I do to achieve a draggable View? The process of adding, deleting, and updating Window. The relationship between Activity, PhoneWindow, DecorView and ViewRootImpl? What is the token in Window and what is the use? Can Dialog pop up directly in Application? With regard to event distribution, is the event first to DecorView or to Window? What is Window?

Window. You can understand that the whole picture on the phone, all the views are rendered through Window, such as Activity, dialog are attached to the Window. The only implementation of the Window class is PhoneWindow, which is easier to remember, the mobile phone window.

So where is Window? Is the View we see Window? Yes and no.

If it is only the concept of Window, it can be said that View is the existence form of Window, and Window manages View.

If it's the Window class, it's really not View. The only implementation class, PhoneWindow, manages the View on the current interface, including the root layout-DecorView, the addition and deletion of other child view, and so on.

I do not know if you are dizzy, I conclude that Window is a conceptual thing, you can not see it, if you can sense its existence, then it is through View, so View is the existence form of Window, with View, you feel that there is a new emperor's clothes-window on the outside of View.

What is WindowManager? The relationship with WMS?

WindowManager is used to manage Window, the implementation class is WindowManagerImpl, and the actual work is delegated to the WindowManagerGlobal class.

For the specific Window operation, WM will tell WMS,WMS through Binder to do the final work of the real operation Window, assign Surface to the Window and draw it to the screen.

How do I add a Window? Var windowParams: WindowManager.LayoutParams = WindowManager.LayoutParams ()

WindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE

WindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG

Var btn = Button (this)

WindowManager.addView (btn, windowParams)

Simply paste the code and add a Button.

Some friends may wonder, this is clearly a Button, is a View ah, how to become a Window?

As I just said, View is the form of Window, in the actual implementation, adding window is actually adding a window that you can't see, and there is View in it to make you feel that this is a Window.

So adding View through windowManager is actually the process of adding Window.

There are also two more important attributes: flags and type, which in turn will be discussed below.

How can Window be displayed to the lock screen interface

Window's flag can control the display characteristics of Window, that is, how to display, touch event handling, relationship with the device, and so on. So the lock screen display asked here is also one of the Flag.

/ / Window does not need to gain focus and does not accept various input events.

Public static final int FLAG_NOT_FOCUSABLE = 0x00000008

/ / @ deprecated Use {@ link android.R.attr#showWhenLocked} or

/ / {@ link android.app.Activity#setShowWhenLocked (boolean)} instead to prevent an

/ / unintentional double life-cycle event.

/ / the window can be displayed on the Window of the lock screen

Public static final int FLAG_SHOW_WHEN_LOCKED = 0x000800000; shows what the hierarchy is when all three types of Window exist.

Type represents the type of Window, and there are three types:

Apply Window. Corresponds to an Activity,Window level of 1: 99, at the lowest level of the view. Son Window. Cannot exist alone and needs to be attached to a specific parent Window (for example, Dialog is a child Window), with a Window level of 1000 to 1999. System Window. Window that needs to declare permissions to create, such as Toast and system status bar, has a Window level of 2000-2999, at the top of the view.

As you can see, the difference is that there is a Window level (z-ordered), the higher level can cover the lower level, closer to the user.

Does Window mean PhoneWindow?

If someone asks me this question, I must be very confused.

Isn't that PhoneWindow? Are the only implementation of the class, just ask some strange questions.

But do you always have to answer this kind of question during the interview? At this time, it is time to pull out the concept of Window.

If you refer to the Window class, then PhoneWindow, as the only implementation class, generally refers to PhoneWindow.

If you are referring to the concept of Window, it certainly does not mean PhoneWindow, but the real View that exists on the interface. Of course, not all View is Window, but the view added to the screen through WindowManager is Window, so PopupWindow is Window, and the single View added in the above question is also Window.

When was PhoneWindow created?

Friends who are familiar with the Activity startup process should know that the startup process will be executed to the handleLaunchActivity method of ActivityThread. Here, WindowManagerGlobal is initialized, that is, the class where WindowManager actually operates Window, which you will see later:

Public Activity handleLaunchActivity (ActivityClientRecord r

PendingTransactionActions pendingActions, Intent customIntent) {

/ /...

WindowManagerGlobal.initialize ()

/ /...

Final Activity a = performLaunchActivity (r, customIntent)

/ /...

Return a

}

Then it executes to performLaunchActivity to create Activity, and calls the attach method to initialize some data (pseudo code):

Final void attach () {

/ / initialize PhoneWindow

MWindow = new PhoneWindow (this, window, activityConfigCallback)

MWindow.setWindowControllerCallback (mWindowControllerCallback)

MWindow.setCallback (this)

/ / Associated with WindowManager

MWindow.setWindowManager (

(WindowManager) context.getSystemService (Context.WINDOW_SERVICE)

MToken, mComponent.flattenToString ()

(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED)! = 0)

MWindowManager = mWindow.getWindowManager ()

}

You can see that in the attach method of Activity, PhoneWindow is created and callback,windowManager is set.

The callback here will talk about it later, it has something to do with event distribution, it can be said that the current Activity and PhoneWindow establish a connection.

What should I do to achieve a draggable View?

Let's go back to the btn example just now. If you want to change the location of the btn, use updateViewLayout, and then pass in the moving coordinates in the ontouch method.

Btn.setOnTouchListener {v, event->

Val index = event.findPointerIndex (0)

When (event.action) {

ACTION_MOVE-> {

WindowParams.x = event.getRawX (index). ToInt ()

WindowParams.y = event.getRawY (index). ToInt ()

WindowManager.updateViewLayout (btn, windowParams)

}

Else-> {

}

}

False

} the process of adding, deleting, and updating Window.

All the operations of Window are done through WindowManager, while WindowManager is an interface, and its implementation class is WindowManagerImpl, and all of it is handled by WindowManagerGlobal. Let's talk about addView,updateViewLayout, and removeView in detail.

1) addView//WindowManagerGlobal.java

Public void addView (View view, ViewGroup.LayoutParams params

Display display, Window parentWindow) {

If (parentWindow! = null) {

ParentWindow.adjustLayoutParamsForSubWindow (wparams)

}

ViewRootImpl root

View panelParentView = null

Root = new ViewRootImpl (view.getContext (), display)

View.setLayoutParams (wparams)

MViews.add (view)

MRoots.add (root)

MParams.add (wparams)

Try {

Root.setView (view, wparams, panelParentView)

}

}

As you can see here, an instance of ViewRootImpl is created, which shows that each Window corresponds to a ViewRootImpl. Then some parameters in WindowManagerGlobal are modified by add method. For example, mViews- stores layout parameters corresponding to all View,mRoots-- corresponding to all Window, all Window corresponding to ViewRootImpl,mParams-, and all Window corresponding to ViewRootImpl,mParams-. Finally, I call the setView method of ViewRootImpl, and take a look. Final IWindowSession mWindowSession

MWindowSession = WindowManagerGlobal.getWindowSession ()

Public void setView (View view, WindowManager.LayoutParams attrs, View panelParentView) {

/ /

RequestLayout ()

Res = mWindowSession.addToDisplay (mWindow,)

}

The setView method mainly completes two things: one is to complete the request of asynchronous refresh interface through the requestLayout method and carry out the complete view drawing process. Second, an IPC call is made through IWindowSession, which is handed over to WMS to add Window.

MWindowSession is a Binder object, which is equivalent to the proxy class on the client side, and the corresponding server implementation is Session, while Session is running in the SystemServer process, specifically in the WMS service, and will eventually call the addToDisplay method of this Session. From the method name, you can guess that this method is the logic of adding Window to the screen, not to analyze it. We will talk about it in detail next time when it comes to screen drawing.

2) updateViewLayout public void updateViewLayout (View view, ViewGroup.LayoutParams params) {

/ /...

Final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params

View.setLayoutParams (wparams)

Synchronized (mLock) {

Int index = findViewLocked (view, true)

ViewRootImpl root = mRoots.get (index)

MParams.remove (index)

MParams.add (index, wparams)

Root.setLayoutParams (wparams, false)

}

}

The WindowManager.LayoutParams and ViewRootImpl.LayoutParams are updated here, and then the View is also re-drawn within the ViewRootImpl, and finally through IPC communication, the relayoutWindow of the WMS is called to complete the update.

3) removeViewpublic void removeView (View view, boolean immediate) {

If (view = = null) {

Throw new IllegalArgumentException ("view must not be null")

}

Synchronized (mLock) {

Int index = findViewLocked (view, true)

View curView = mRoots.get (index). GetView ()

RemoveViewLocked (index, immediate)

If (curView = = view) {

Return

}

Throw new IllegalStateException ("Calling with view" + view

+ "but the ViewAncestor is attached to" + curView)

}

}

Private void removeViewLocked (int index, boolean immediate) {

ViewRootImpl root = mRoots.get (index)

View view = root.getView ()

If (view! = null) {

InputMethodManager imm = view.getContext () .getSystemService (InputMethodManager.class)

If (imm! = null) {

Imm.windowDismissed (mViews.get (index). GetWindowToken ()

}

}

Boolean deferred = root.die (immediate)

If (view! = null) {

View.assignParent (null)

If (deferred) {

MDyingViews.add (view)

}

}

}

In this method, we find the corresponding index in mRoots through view, and then go to ViewRootImpl to delete View. Through the die method, we finally go to the dispatchDetachedFromWindow () method, and mainly do the following things:

Callback onDetachedFromeWindow. Garbage collection related operations; delete Window; in WMS through Session's remove () remove the relationship between listeners Activity, PhoneWindow, DecorView, ViewRootImpl through Choreographer?

After reading the above process, let's sort out the relationship between these four partners:

PhoneWindow is actually the only subclass of Window, is the middle layer of Activity and View interaction system, is used to manage View, and creates a new ViewRootImpl instance when Window is created (added). DecorView is the top level of the entire View hierarchy, and ViewRootImpl is the parent of DecorView, but it is not a real View, it just inherits the ViewParent interface and is used to manage various events of View, including requestLayout, invalidate, dispatchInputEvent, and so on. What is the token in Window and what is the use?

Token? What is it? It didn't show up during the window operation just now.

In fact, token should be found in your work. For example, when you create a dialog in the context of application, you will report an error:

Unable to add window-token null

So this token has something to do with the window operation. Turning to the addview method just now, there is one detail we haven't talked about, that is, the adjustLayoutParamsForSubWindow method.

/ / Window.java

Void adjustLayoutParamsForSubWindow (WindowManager.LayoutParams wp) {

If (wp.type > = WindowManager.LayoutParams.FIRST_SUB_WINDOW & &

Wp.type = WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW & &

Wp.type DecorView-- > Activity-- > PhoneWindow-- > DecorView-- > ViewGroup

(the getCallback parameter is used, that is, the callback passed in addView, that is, Activity itself.)

But this process is really a little strange, why go around it, DecorView alone walked twice.

I agree with the reference link, the main reason is decoupling.

ViewRootImpl doesn't know there is such a thing as Activity, it just holds DecorView. So it was passed to DecorView first, and DecorView knew there was AC, so it was passed to AC. Activity doesn't know there is a DecorView either, it just holds the PhoneWindow, so such a chain of calls is formed. At this point, I believe you have a deeper understanding of "what are the problems about Window"? you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report