In the last article “AndroidP under the SystemUI startup and customization”, understand the SystemUI startup process, but also know that the last process is mainly StatusBar through WindowManager addView() to load the view in the system interface, Before this to use WindowManager. LayoutParams to set the display of the window, so the WindowManager appear in the source code is what? What is the distinguishing feature of WindowManager. The parameters of the LayoutParams again? The following sections will examine WindowManager from these two perspectives.

What is WindowManager?

1.1 Relationship between WindowManager and Windows

What is WindowManager? The only official explanation for it is this

The interface that apps use to talk to the window manager.

This is an interface for app to communicate with Windows. Windows Manager is a semantically used interface to manage Windows. What is Windows? In fact, Dialog, Popup and StatusBar are essentially Windows. Window is an abstract class, equivalent to an alliance. Views like Dialog and Popup can function only when they are attached to the alliance. WindowManager, on the other hand, acts as the federation’s president, communicating with members such as child Views and being able to add, update, and delete them (WindowManager is an interface that operates in the Windows Manager ImpL implementation class).

WindowManager inherits from ViewManager, which is the interface for adding and removing subviews to an Activity. There are three abstract methods in ViewManager that display on official documents:

  • abstract void addView(View view, ViewGroup.LayoutParams params)
  • abstract void removeView(View view)
  • abstract void updateViewLayout(View view, ViewGroup.LayoutParams params)

The first addView() : means passing arguments to the view through LayoutParams, and then adding the view to the window. We usually add custom popups, dialogs, menus, and so on to the system interface.

Second removeView() : removes the view from the window.

Third, updateViewLayout() : Updates the view.

1.2 WindowManager object acquisition

When we want to add a child view to the interface, we need to call WindowManager addView(). How do we get an instance of WindowManager? The documentation gives a method like this:

  • Context.getSystemService(String name)

You can see that the difference in the parameter name in the method will fetch different objects. When the name is WINDOW_SERVICE, that is, context.getSystemService (context.window_service) returns a WindowManager object. In addition, the objects corresponding to common names are listed below.

name object
POWER_SERVICE PowerManager
ALARM_SERVICE AlarmManager
NOTIFICATION_SERVICE NotificationManager
ACTIVITY_SERVICE ActivityManager
LAYOUT_INFLATER_SERVICE LayoutInflater
LOCATION_SERVICE LocationManager
WIFI_SERVICE WifiManager
. .

Second, the WindowManager. LayoutParams argument parsing

2.1 Constructors

AddView (View View, viewGroup.layoutParams params) is a LayoutParams method that passes parameters to the View and then adds the View to the window. LayoutParams contains layout width, height, location, type and other information. By setting these information, different views are generated.

AddView () in the second parameter ViewGroup. LayoutParams is WindowManager LayoutParams parent class, let’s turn to the WindowManager. Specific LayoutParams.

WindowManager. LayoutParams has the following 7 kinds of constructor:

Public constructors
1.WindowManager.LayoutParams()
2.WindowManager.LayoutParams(int _type)
3.WindowManager.LayoutParams(int _type, int _flags)
4.WindowManager.LayoutParams(int _type, int _flags, int _format)
5.WindowManager.LayoutParams(int w, int h, int _type, int _flags, int _format)
6.WindowManager.LayoutParams(int w, int h, int xpos, int ypos, int _type, int _flags, int _format)
7.WindowManager.LayoutParams(Parcel in)

Details of specific parameters are described in the following sections.

2.2 Type

The second constructor contains a type of int. Type stands for different types of window. Window has three types:

  • Application Windows

    The hierarchy ranges from 1 to 99, and is a normal top-level application window, such as the Activity we saw.

  • Sub-windows (sub-windows)

    The hierarchy ranges from 1000 to 1999, such as a partial Dialog.

  • System Windows

    The level ranges from 2000 to 2999, which is the highest level. For example, StatusBar and NavigationBar cover all Windows.

These three types can be divided into many different states. Various types are introduced in official documents. Here are some common and important types.

  • TYPE_APPLICATION_OVERLAY

    Covering all the activity window, but lower than the critical systems Windows (such as the status bar, the IME, etc.), system can always change the window position, size, or the visibility, and need to apply for the Manifest. Permission. SYSTEM_ALERT_WINDOW permissions.

  • TYPE_STATUS_BAR

    StatusBar, the system has only one StatusBar window, it is located at the top of the screen, all other Windows move down, when we want to replace with our own custom StatusBar, we can set type to it.

  • TYPE_SEARCH_BAR

    Search bar, the system has only one search bar window located at the top of the screen.

  • TYPE_KEYGUARD_DIALOG

    Keyboard lock display dialog box.

Note: TYPE_TOAST, TYPE_SYSTEM_OVERLAY, TYPE_SYSTEM_ERROR, TYPE_SYSTEM_ALERT, TYPE_PRIORITY_PHONE, and TYPE_PHONE are deprecated in API 26. Replaced by TYPE_APPLICATION_OVERLAY.

2.3 Flags

The third constructor takes an int Flag, which represents the window property. Here are some common Flags.

  • With no Flag set by default, Windows in the new Window hierarchy will not receive any touch events, even outside the scope of the new window.

  • FLAG_NOT_FOCUSABLE indicates that events within the scope of this window are handled by itself, and events outside the scope are handled by the original window. For example, if you click on the view outside the window, you will still get a response. In addition, whenever this Flag is set, FLAG_NOT_TOUCH_MODAL will be enabled. Finally, if this Flag is set, the window will not interact with the input method. For example, the window has an EditView, and clicking the EditView will not bring up the soft keyboard.

  • FLAG_NOT_TOUCH_MODAL indicates that views outside the new window range can respond to touch events even if the window is in the above default state.

  • FLAG_NOT_TOUCHABLE indicates that the window will not receive any touch events, such as clicking on the window.

  • FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS

    Represents responsible for drawing the system bar background. If set, the system bar will be drawn with a transparent background, and the corresponding area in this window will be filled with the colors specified in Windows # getStatusBarColor () and Windows # getNavigationBarColor ().

  • FLAG_FULLSCREEN

    Hide all screen decorations (including the status bar) while displaying the window

  • FLAG_FORCE_NOT_FULLSCREEN

    FLAG_FULLSCREEN is one level lower than FLAG_FULLSCREEN and displays the status bar

  • FLAG_SHOW_WALLPAPER

    The window surface must be translucent in order to see the wallpaper behind it

  • FLAG_SHOW_WHEN_LOCKED

    The window is displayed on the lock screen. This symbol has been abandoned in the API27, use of state Richard armitage TTR event. ShowWhenLocked or Activity. SetShowWhenLocked (Boolean) instead.

2.4 the Format

In the fourth constructor, there is an int format parameter. This parameter represents the bitmap format required for window, which is OPAQUE by default and may be one of PixelFormat. SetColorMode (int) may override the format selection.

2.5 w, h

The fifth constructor has two more int w, h, w is the width of the window, h is the height, can be a specific value, layoutparams.match_parent, layoutparams.wrap_content.

2.6 xpos, ypos

In the sixth constructor, there are two more int xpos and ypos, where xpos represents the x-coordinate position of the window and ypos represents the y-coordinate position.

Also known as layoutparams.x and layoutparams.y.

2.7 the Gravity

Gravity.BOTTOM indicates the BOTTOM of the container, Gravity.CENTER_HORIZONTAL indicates the horizontal CENTER of the container, and Gravity.CENTER indicates the CENTER of the container. Of course, there are other types of Gravity, and you can set the position of Gravity according to your own needs.

2.8 token

A token is a Binder proxy object that represents a token for Window. WMS checks whether the incoming token can be added to the system. Generally, the system automatically adds the token for us.

2.9 softInputMode

This property is used to set the display mode of the Input area of the Window soft keyboard. For example, we sometimes find that the window soft keyboard takes up the entire screen when opened, blocking the view behind. In this case, we can set this property to adjust the style of the window soft keyboard.

For example:

  • SOFT_INPUT_ADJUST_NOTHING

    Will not resize and overwrite the window directly.

  • SOFT_INPUT_ADJUST_PAN

    The window with input method can be translated, for example, there are two EditView input boxes, one for Ev1, one for Ev2, when you click Ev1 to input data, the current Ev1 input box will move to the top of the soft keyboard, software disk is following Ev1 below, ensure that Ev1 is visible,Ev2 is not necessarily visible.

    In addition, this mode cannot be used in conjunction with SOFT_INPUT_ADJUST_RESIZE.

  • SOFT_INPUT_ADJUST_RESIZE

    If you click on an EditView, for example, the entire Layout will be visible and placed on top of the software tray.

    Similarly, this mode cannot be used in conjunction with SOFT_INPUT_ADJUST_PAN. In addition, if the window’s layout parameter flag contains FLAG_FULLSCREEN, this value is ignored and the window does not resize, but remains full-screen.

  • SOFT_INPUT_ADJUST_UNSPECIFIED

    Do not specify, the system automatically sets this mode and other modes according to the content.

  • SOFT_INPUT_MASK_ADJUST

    The window is resized to fit the soft keyboard window.

  • SOFT_INPUT_STATE_ALWAYS_HIDDEN

    When this window gets focus, always hide any soft input areas.

  • SOFT_INPUT_STATE_ALWAYS_VISIBLE

    When this window gets focus, any soft input fields are always displayed.

Third, summary

The above is the introduction of WindowManager and LayoutParams, the official documentation for their introduction is a little difficult to understand, the above part of the translation into everyday terms. In actual project development, we sometimes need to customize a window, so we need to set the display mode and type of the window, which needs to understand the meaning of each attribute of LayoutParams, to apply to the changeable needs. Most of the above covers the Settings used in daily development, and the rest can be found in the official documentation.

References:

The official documentation

Personal Blog:

fuusy.github.io/