Android Window Management

Source: Internet
Author: User
<span id="Label3"></p><p><p><br class="Apple-interchange-newline">I. <strong>Overview</strong></p></p><p><p>In the Android system, from the design point of view, the window management system is based on the c/s Mode. The whole window system is divided into two parts: server and client, the client is responsible for requesting the creation window and using the window, the service side completes the Window maintenance, window display and so On.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>On the client side, it is not directly interacting with windowmanagerservice, but interacting directly with the local object windowmanager, and then by WindowManager and Windowmanagerservice. This interaction is transparent to Android apps, and applications cannot perceive the existence of windowmanagerservice</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>second, <strong>the definition of the window</strong></p></p><p><p>In the Android application framework, The window is divided into two main types:</p></p><p><p>The first is the application Window: an activity has a main window, a popup dialog has a window, and a menu is a window. In the same activity, the activity is associated with the main window, dialog box, and menu Window. The application-related window representation classes are Phonewindow and Window,phonewindow inherit from window and do some optimization work on the phone Screen. Phonewindow is just a window wrapper class, inside the core is Mdecorview this variable, Mdecorview is a top-level view, the window is added by calling Getdecorview () Gets to Mdecorview and calls Windowmanager.addview () to add the view to Windowmanager.</p></p><p><p>The second is the public interface window: such as the recent run dialog, shutdown dialog, status bar drop-down bar, lock screen interface and so On. These windows are system-level windows that do not belong to any application and do not have any relationship to the Activity. This window does not have any window class to encapsulate, call Windowmanager.addview () directly to add a view to the Windowmanager.</p></p><p><p>When the application is initialized, an activity object is generated first, and the activity does not have a window of his own. Immediately after calling the attach () function, the activity in the Attach () function calls Policymanager.makenewwindow () to create a new phonewindow, Then in the Activity's oncreate () life cycle, The general application invokes Setcontentview () to set the Activity's display Interface. In Setcontentview (), the framework automatically generates a layout that contains elements such as the title bar, actionbar, and most importantly, the Contentview that contains the app. This layout corresponds to the Mdecorview inside the PHONEWINDOW. finally, before the activity is going to be shown, get to Decorview by GetWindow (). Getdecorview () and pass Windowmanager.addview () Add the Decorview to the Windowmanager.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Activity Add Client Window timing diagram</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><strong>third, Window Management</strong></p></p><p><p>The Windows management of Android is based on the C/S mode and is implemented using an independent process. The Windows Managed server Windowmanagerservice runs in a standalone process system_server, and when the application needs to create a window, requests the Windowmanagerservice creation window through process Communication. An interactive message that is passed by Windowmanagerservice to the application and related to the WINDOW. All the windows of the program are managed on the server, and the display and control of the windows are handled in Windowmanagerservice.</p></p><p><p>Windowmanagerservice mainly accomplishes the following functions:</p></p><p><p>1. Adding and removing windows</p></p><p><p>2. Window display and hide control</p></p><p><p>3. Z-order Sequential Management</p></p><p><p>4. Management of focus windows and focus applications</p></p><p><p>5. IME window management and Wallpaper window management</p></p><p><p>6. Transition Animations</p></p><p><p>7. System Message Collection and distribution</p></p><p><p></p></p><p><p>The implementation code for the server is in/framework/base/services/java/com/android/server/wm/, and several of the core classes Are:</p></p><p><p>Windowmanagerservice.java</p></p><p><p>Windowstate.java</p></p><p><p>Windowtoken.java</p></p><p><p>Appwindowtoken.java</p></p><p><p>Session.java</p></p><p><p>Inputmanager.java</p></p><p><p>Inputmonitor.java</p></p><p><p></p></p><p><p>Class Explanation:</p></p><p><p>Windowmanagerservice is responsible for the completion of the window management work;</p></p><p><p>WindowState corresponds to client window one by one, when the application calls Windowmanager.addview (), it will eventually add a windowstate to the Windowmanagerservice.</p></p><p><p>Windowtoken is a handle that holds all windowstate with the same Token. When the application request Windowmanagerservice adds a window, it provides a token that identifies the attribution of the added window and Windowmanagerservice generates a Windowtoken object for the Token. All tokens of the same windowstate are associated to the same Windowtoken. When an input method is added to a window, a mcurrtoken is passed, and a newconn.mtoken is passed when the wallpaper service adds a window.</p></p><p><p>Appwindowtoken is inherited from Windowtoken and is specifically designed to identify an Activity. The token in Appwindowtoken is actually pointing to an Activity. Activitymanagerservice notifies the application to launch, a token is generated on the server to identify the activity, and the token is passed to the application client, and the Client's activity is applied to the add WINDOW. The token is passed to the Windowmanagerservice as an Identity. The main window, dialog window, and Menu window in the same activity are associated to the same Appwindowtoken.</p></p><p><p>The session represents an interactive session between the client and the Server. In general, different applications interact with Windowmanagerservice through different sessions, but different applications in the same process interact through the same session.</p></p><p><p>InputManager and Inputmonitor are responsible for the message distribution capabilities of the upper Layer.</p></p><p><p></p></p><p><p>Several important member variables within the windowmanagerservice:</p></p><p><p>Arraylist<windowstate> mwindows</p></p><p><p>hashmap<ibinder, windowstate> Mwindowmap</p></p><p><p>Arraylist<windowtoken> mtokenlist</p></p><p><p>Arraylist<appwindowtoken> Mapptokens</p></p><p><p></p></p><p><p>Mwindows preserves all the WindowState in the system;</p></p><p><p>Mwindowmap Save the mapping of each windowstate and client window, when the Client application request window operation, through Mwindowmap query to the corresponding windowstate;</p></p><p><p>Mtokenlist saved all the Windowtoken.</p></p><p><p>Mapptokens saved all the Appwindowtoken.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Window Management Server main class diagram</p></p><p><p></p></p><p><p></p></p><p><p>The entire flow of activity from the start to the Add window is as Follows:</p></p><p><p>Activitymanagerservice when a request to initiate an activity is received, a token is first generated as a unique identifier for that Activity. It then calls Windowmanagerservice to add a appwindowtoken, which Appwindowtoken encapsulates the Activity's Token. The AMS then launches the application client process and passes tokens to the process, initializing the activity in the client Process. In the Activity's attach () function, The activity completes the creation of the Phonewindow and passes the token to PHONEWINDOW. When the activity calls Windowmanager.addview (), the token is associated with the view inside the windowmanager, and when the Windowmanagerservice is actually requested to create the window, Then pass the token to Windowmanagerservice. Windowmanagerservice when a request to create a window is received, the Mtokenmap queries the Appwindowtoken to the token, and throws an exception if it is Empty. Otherwise create a windowstate and complete the work of initialization and other data structure Adjustments. In this process, token runs through the Server's ams, wms, and client activity, Window.</p></p><p><p></p></p><p><p></p></p><p><p>Timeline diagram for creating Windows during activity Startup</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><strong>Iv. interface and data structure of server and client in WMS</strong></p></p><p><p>When the application request creates a window, it is the WindowManager object that interacts directly with the app. WindowManager is just an interface that calls AddView () when creating a window that is really interacting with the Windowmanagerimpl Object. Windowmanagerimpl manages all local windows for a single app. When the application calls AddView () to create a window, Windowmanagerimpl generates a Viewroot object corresponding to it and saves the corresponding parameter Layoutparams.</p></p><p><p>The execution process for AddView () is as Follows:</p></p><p><p>(1) Check whether the added window has been added, not allowed to repeat the addition;</p></p><p><p>(2) if the added window is a child window type, find its parent window and save it in the internal variable;</p></p><p><p>(3) Create a new viewroot and save the corresponding view (decorview) and layoutparams;</p></p><p><p>(4) call Viewroot's Setview () method, complete the real meaning of the add Work.</p></p><p><p></p></p><p><p>Viewroot is essentially a handler and implements the Viewparent Interface. The main functions of Viewroot are:</p></p><p><p>1. Responsible for distributing message events, such as key, motion events, etc.;</p></p><p><p>2. Responsible for interacting with WMS and distributing Wms's interactive commands;</p></p><p><p>3. As the parent of decorview, draw, measure, layout and other operations for decorview;</p></p><p><p></p></p><p><p>After the completion of the 3rd and 4 steps of AddView (), Viewroot took full control of the interaction with WMS and Decorview did not need to do any interactive Action. The two-way dialogue between viewroot and WMS is carried out mainly through the following two data structures:</p></p><p><p>Iwindowsession</p></p><p><p>Iwindow</p></p><p><p>Both of these data structures are standard AIDL interfaces for synchronous communication between Processes. Iwindowsession is responsible for viewroot a one-way request to the wms, Iwindow is used for WMS callback Viewroot. Inside the Viewroot object, there is a static member of the Iwindowsession and a iwindow non-static member, so there is only one Iwindowsession object in a process, but there can be more than one Iwindow Object.</p></p><p><p>Window, windowmanager, decorview, viewroot, iwindowsession, iwindowsession, WindowState, The relationship between Windowmanagerservice can be expressed as:</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>In the Viewroot constructor, call getwindowsession () to initialize static member Swindowsession and Non-static member Mwindow. When you call the Setview () method in step 4th, Viewroot calls the Swindowsession.add () method, adds Iwindow to the wms, and the WMS generates a windowstate corresponding to it. and save the Iwindow object inside the WindowState as the callback Interface. All WMS commands are then distributed to the viewroot,viewroot in the form of messages by direct access to the Iwindow interface, or to the decorview, or to the WMS after completion via Swindowsession.</p></p><p><p></p></p><p><p></p></p><p><p>A window from the add to display can be represented by the following timing diagram:</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Window Add Process timing diagram</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>To this end, the overall window Management system architecture can be expressed as follows:</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Windows Management system Overall architecture diagram</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><strong>V. WindowState and surface</strong></p></p><p><p><strong></strong></p></p><p><p>Calling Windowmanager's AddView () method from the client side to the WMS to complete the initialization of the windowstate, in this whole process, just completed the creation of a window data structure, that is to say, until now, Client-side windows and Server-side windows have established a relatively fixed connection, and the client side and server side can communicate properly, the WMS can transparently operate on the client side of the window, and the WMS can also receive the Client side window commands , and adjust the WindowState accordingly.</p></p><p><p>A windowstate want to display on the screen, must request a display cache, this display cache management and maintenance is implemented in the underlying graphics module, the Java layer has an operational package object Surface. WindowState after applying to the surface object, the relevant data for this surface object is copied to the client side of viewroot, and a surface object is maintained in viewroot, actually pointing to the same block display Cache. Viewroot with this reference to display the cache, you can get the drawing canvas through lockcanvas, and after the drawing is finished, the drawing content is flushed to the display cache by Unlockandpostcanvas. That is, the client side window and the Server side window share a surface,client responsible for drawing surface content, and the server is responsible for controlling the size and position of surface on the Screen.</p></p><p><p>Viewroot sends request commands to the WMS via the iwindowsession relayout () interface, including window display and shadowing, window layout information such as position size, and receiving WMS processing Results. The WMS determines the final layout information for the window based on the screen size and the layout parameters requested by the client, and returns a valid or invalid surface object based on the display hidden command of the client Request. typically, A window is displayed as a process:</p></p><p><p>1. Client requests the display window and passes layout parameters;</p></p><p><p>2. WMS applies a Surface object and returns it to the client based on layout parameters;</p></p><p><p>3. Client paints operations on surface and tells WMS when finished;</p></p><p><p>4. WMS will display surface on the screen, and the level of the corresponding adjustment;</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>window display process sequence diagram</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>A holistic concept across activity, View, viewroot, iwindowsession, iwindow, WindowState, windowmanagerservice, and surface is as follows:</p></p><p><p></p></p><p><p></p></p><p><p>Windows Management System Complete architecture diagram</p></p><p><p>Android Window Management</p></p></span>

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.