Understand Windows and WindowManager

The Window is an abstract class, it is the concrete implementation of PhoneWindow, Window to add, update, and delete by WindowManager corresponding addView, updateViewLayout, removeVeiw three ways. There is no external need to instantiate the window directly.WindowManager will eventually interact with WindowManagerService(WMS) (IPC process) to add, update, and delete Windows. Common Windows include Activity,Dialog,Toast and so on.

WindowManager

WindowManager inherits from VeiwManager, whose three important methods are also defined in VeiwManager.

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

WindowManager.LayoutParams

As you can see, WindowManager methods that operate on Windows are actually Veiw of the operation, and View is the carrier of the Window. But even though the View is the one we touch most often, a View is nothing without a Window, it can’t be displayed on the screen, so Window is a very important abstraction. Digress. As you can see, LayoutParams are used in both the add View and update View methods.

WindowLayoutParams in addition to some common properties, such as X, Y,gravity and so on, there are two more important properties, flag,type.

Flags

The Flag parameter represents the properties of the Window. It has a number of options that combine to control the display properties of the Window

  • FLAG_NOT_FOCUSABLE indicates that the Window does not need to get focus

  • FLAG_NOT_TOUCH_MODAL In this mode, the system passes click events outside the current window area to the underlying window. Click events within the current region are handled by themselves.

  • FLAG_SHOW_WHEN_LOCKED Enables Windows to be displayed on the lock screen

Type

Type indicates the Type of window. There are three types of window: application window, child window, and system window. Applying a window corresponds to an Activity. A child window cannot exist on its own; it needs to be attached to a particular parent window, such as a Dialog. System Windows requires permissions to create. Like Toast and the system status bar.

Windows are hierarchical, with higher-level Windows overlaying lower-level Windows.

Windows internal mechanism

Window is an abstract concept. Each Window corresponds to a View and a ViewRootImpl. The link between Window and Veiw is established through the VeiwRootImpl. So the Window doesn’t actually exist, it exists as a View. This can also be seen from the WindowManager definition. The three interface methods he provides are named after Views. Windows cannot be accessed directly during actual use. Access to Windows must be through WindowManager.

The Window to add

WindowManager – > WindowManagerImpl – > WindowManagerGlobal. Through the source code we can see that add delete, modify the three methods are finally handed over to Windows ManagerGlobal, which is a typical bridge mode. Delegate all operations to Windows ManagerGlobal.

In the Windows ManagerGlobal addView method, there are roughly three steps.

  • Check whether the parameters are valid. If it is a child Window, adjust some layout parameters

  • Create the VeiwRootImpl and add the Veiw to the list.

  • Update the interface and add it to Windows Servicemanager using the setView method of VeiwRootImpl.

It is important to maintain several lists within Windows ManagerGlobal.

private final ArrayList<View> mViews = new ArrayList<View>();
private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
private final ArrayList<WindowManager.LayoutParams> mParams =
            new ArrayList<WindowManager.LayoutParams>();
private final ArraySet<View> mDyingViews = new ArraySet<View>();
Copy the code

MViews stores views corresponding to all Windows,mRoots stores ViewRootImpl corresponding to all Windows,mParams stores layout parameters corresponding to all Windows, and mDyingViews stores ViewD objects that are being deleted.

The deletion of the Window

The die method will eventually be called to the removeWindow method of The WindowManager via the Remove method of the WindowSession.

Windows update

The setLayoutParams method of ViewRootImpl is also called. In View Windows PL, the relayoutWindow of WindowManagerService is accessed via WindowSession to update the window.

Summary of Window additions, deletions, and updates.

Start with Windows Manager, go to View Windows PL, go to Windows Session, go to WindowManagerService. The step to WMS is the IPC process, think about it, in our application to call a method, to show changes on the screen, etc., must be cross-process.