Android WindowManger的层级分析详解
程序员文章站
2022-03-20 13:59:33
目录一. window 分类二. window层级(三)如何真正查看 window 的优先级(四) 层级高低具体分析(对比toast以及软键盘)一. window 分类 应用 window(ap...
一. window 分类
- 应用 window(applicationwindow: 对应一个 acitivity)
- 子 window (subwindow:不能单独存在,需要依附在特定的父 window 中,比如常见的一些 dialog 就是一个子 window)
- 系统 window (systemwindow:需要声明权限才能创建的 window,比如 toast 和系统状态栏都是系统 window)
二. window层级
window 是分层的,每个 window 都有对应的 z-ordered,层级大的会覆盖在层级小的 window 上面,这和 html 中的 z-index 概念是完全一致的。
在三种 window 中,每一种window的层级范围也是不同的,如下:
应用window 1~99
子window 1000~1999
系统window 2000~2999
这些层级范围对应着 windowmanager.layoutparams 的 type 参数,如果想要 window 位于所有 window 的最顶层,那么采用较大的层级即可,很显然系统 window 的层级是最大的,当我们采用系统层级时,需要声明权限。
(1)应用程序窗口:
package android.view; public interface windowmanager /** * start of window types that represent normal application windows. */ public static final int first_application_window = 1; /** * window type: an application window that serves as the "base" window * of the overall application; all other application windows will * appear on top of it. * in multiuser systems shows only on the owning user's window. */ public static final int type_base_application = 1; /** * window type: a normal application window. the {@link #token} must be * an activity token identifying who the window belongs to. * in multiuser systems shows only on the owning user's window. */ public static final int type_application = 2; /** * window type: special application window that is displayed while the * application is starting. not for use by applications themselves; * this is used by the system to display something until the * application can show its own windows. * in multiuser systems shows on all users' windows. */ public static final int type_application_starting = 3; /** * window type: a variation on type_application that ensures the window * manager will wait for this window to be drawn before the app is shown. * in multiuser systems shows only on the owning user's window. */ public static final int type_drawn_application = 4; /** * end of types of application windows. */ public static final int last_application_window = 99;
(2)子窗口:
package android.view; public interface windowmanager /** * start of types of sub-windows. the {@link #token} of these windows * must be set to the window they are attached to. these types of * windows are kept next to their attached window in z-order, and their * coordinate space is relative to their attached window. */ public static final int first_sub_window = 1000; /** * window type: a panel on top of an application window. these windows * appear on top of their attached window. */ public static final int type_application_panel = first_sub_window; /** * window type: window for showing media (such as video). these windows * are displayed behind their attached window. */ public static final int type_application_media = first_sub_window + 1; /** * window type: a sub-panel on top of an application window. these * windows are displayed on top their attached window and any * {@link #type_application_panel} panels. */ public static final int type_application_sub_panel = first_sub_window + 2; /** window type: like {@link #type_application_panel}, but layout * of the window happens as that of a top-level window, <em>not</em> * as a child of its container. */ public static final int type_application_attached_dialog = first_sub_window + 3; /** * window type: window for showing overlays on top of media windows. * these windows are displayed between type_application_media and the * application window. they should be translucent to be useful. this * is a big ugly hack so: * @hide */ @unsupportedappusage public static final int type_application_media_overlay = first_sub_window + 4; /** * window type: a above sub-panel on top of an application window and it's * sub-panel windows. these windows are displayed on top of their attached window * and any {@link #type_application_sub_panel} panels. * @hide */ public static final int type_application_above_sub_panel = first_sub_window + 5; /** * end of types of sub-windows. */ public static final int last_sub_window = 1999;
(3)系统窗口:
package android.view; public interface windowmanager /** * start of system-specific window types. these are not normally * created by applications. */ public static final int first_system_window = 2000; /** * window type: the status bar. there can be only one status bar * window; it is placed at the top of the screen, and all other * windows are shifted down so they are below it. * in multiuser systems shows on all users' windows. */ public static final int type_status_bar = first_system_window; /** * window type: the search bar. there can be only one search bar * window; it is placed at the top of the screen. * in multiuser systems shows on all users' windows. */ public static final int type_search_bar = first_system_window+1; /** * window type: phone. these are non-application windows providing * user interaction with the phone (in particular incoming calls). * these windows are normally placed above all applications, but behind * the status bar. * in multiuser systems shows on all users' windows. * @deprecated for non-system apps. use {@link #type_application_overlay} instead. */ @deprecated public static final int type_phone = first_system_window+2; /** * window type: system window, such as low power alert. these windows * are always on top of application windows. * in multiuser systems shows only on the owning user's window. * @deprecated for non-system apps. use {@link #type_application_overlay} instead. */ @deprecated public static final int type_system_alert = first_system_window+3; /** * window type: keyguard window. * in multiuser systems shows on all users' windows. * @removed */ public static final int type_keyguard = first_system_window+4; /** * window type: transient notifications. * in multiuser systems shows only on the owning user's window. * @deprecated for non-system apps. use {@link #type_application_overlay} instead. */ @deprecated public static final int type_toast = first_system_window+5; /** * window type: system overlay windows, which need to be displayed * on top of everything else. these windows must not take input * focus, or they will interfere with the keyguard. * in multiuser systems shows only on the owning user's window. * @deprecated for non-system apps. use {@link #type_application_overlay} instead. */ @deprecated public static final int type_system_overlay = first_system_window+6; /** * window type: priority phone ui, which needs to be displayed even if * the keyguard is active. these windows must not take input * focus, or they will interfere with the keyguard. * in multiuser systems shows on all users' windows. * @deprecated for non-system apps. use {@link #type_application_overlay} instead. */ @deprecated public static final int type_priority_phone = first_system_window+7; /** * window type: panel that slides out from the status bar * in multiuser systems shows on all users' windows. */ public static final int type_system_dialog = first_system_window+8; /** * window type: dialogs that the keyguard shows * in multiuser systems shows on all users' windows. */ public static final int type_keyguard_dialog = first_system_window+9; /** * window type: internal system error windows, appear on top of * everything they can. * in multiuser systems shows only on the owning user's window. * @deprecated for non-system apps. use {@link #type_application_overlay} instead. */ @deprecated public static final int type_system_error = first_system_window+10; /** * window type: internal input methods windows, which appear above * the normal ui. application windows may be resized or panned to keep * the input focus visible while this window is displayed. * in multiuser systems shows only on the owning user's window. */ public static final int type_input_method = first_system_window+11; /** * window type: internal input methods dialog windows, which appear above * the current input method window. * in multiuser systems shows only on the owning user's window. */ public static final int type_input_method_dialog= first_system_window+12; /** * window type: wallpaper window, placed behind any window that wants * to sit on top of the wallpaper. * in multiuser systems shows only on the owning user's window. */ public static final int type_wallpaper = first_system_window+13; /** * window type: panel that slides out from over the status bar * in multiuser systems shows on all users' windows. */ public static final int type_status_bar_panel = first_system_window+14; /** * window type: secure system overlay windows, which need to be displayed * on top of everything else. these windows must not take input * focus, or they will interfere with the keyguard. * * this is exactly like {@link #type_system_overlay} except that only the * system itself is allowed to create these overlays. applications cannot * obtain permission to create secure system overlays. * * in multiuser systems shows only on the owning user's window. * @hide */ @unsupportedappusage public static final int type_secure_system_overlay = first_system_window+15; /** * window type: the drag-and-drop pseudowindow. there is only one * drag layer (at most), and it is placed on top of all other windows. * in multiuser systems shows only on the owning user's window. * @hide */ public static final int type_drag = first_system_window+16; /** * window type: panel that slides out from over the status bar * in multiuser systems shows on all users' windows. these windows * are displayed on top of the stauts bar and any {@link #type_status_bar_panel} * windows. * @hide */ public static final int type_status_bar_sub_panel = first_system_window+17; /** * window type: (mouse) pointer * in multiuser systems shows on all users' windows. * @hide */ public static final int type_pointer = first_system_window+18; /** * window type: navigation bar (when distinct from status bar) * in multiuser systems shows on all users' windows. * @hide */ public static final int type_navigation_bar = first_system_window+19; /** * window type: the volume level overlay/dialog shown when the user * changes the system volume. * in multiuser systems shows on all users' windows. * @hide */ public static final int type_volume_overlay = first_system_window+20; /** * window type: the boot progress dialog, goes on top of everything * in the world. * in multiuser systems shows on all users' windows. * @hide */ public static final int type_boot_progress = first_system_window+21; /** * window type to consume input events when the systemui bars are hidden. * in multiuser systems shows on all users' windows. * @hide */ public static final int type_input_consumer = first_system_window+22; /** * window type: dreams (screen saver) window, just above keyguard. * in multiuser systems shows only on the owning user's window. * @hide */ public static final int type_dream = first_system_window+23; /** * window type: navigation bar panel (when navigation bar is distinct from status bar) * in multiuser systems shows on all users' windows. * @hide */ public static final int type_navigation_bar_panel = first_system_window+24; /** * window type: display overlay window. used to simulate secondary display devices. * in multiuser systems shows on all users' windows. * @hide */ @unsupportedappusage public static final int type_display_overlay = first_system_window+26; /** * window type: magnification overlay window. used to highlight the magnified * portion of a display when accessibility magnification is enabled. * in multiuser systems shows on all users' windows. * @hide */ public static final int type_magnification_overlay = first_system_window+27; /** * window type: window for presentation on top of private * virtual display. */ public static final int type_private_presentation = first_system_window+30; /** * window type: windows in the voice interaction layer. * @hide */ public static final int type_voice_interaction = first_system_window+31; /** * window type: windows that are overlaid <em>only</em> by a connected {@link * android.accessibilityservice.accessibilityservice} for interception of * user interactions without changing the windows an accessibility service * can introspect. in particular, an accessibility service can introspect * only windows that a sighted user can interact with which is they can touch * these windows or can type into these windows. for example, if there * is a full screen accessibility overlay that is touchable, the windows * below it will be introspectable by an accessibility service even though * they are covered by a touchable window. */ public static final int type_accessibility_overlay = first_system_window+32; /** * window type: starting window for voice interaction layer. * @hide */ public static final int type_voice_interaction_starting = first_system_window+33; /** * window for displaying a handle used for resizing docked stacks. this window is owned * by the system process. * @hide */ public static final int type_dock_divider = first_system_window+34; /** * window type: like {@link #type_application_attached_dialog}, but used * by quick settings tiles. * @hide */ public static final int type_qs_dialog = first_system_window+35; /** * window type: shares similar characteristics with {@link #type_dream}. the layer is * reserved for screenshot region selection. these windows must not take input focus. * @hide */ public static final int type_screenshot = first_system_window + 36; /** * window type: window for presentation on an external display. * @see android.app.presentation * @hide */ public static final int type_presentation = first_system_window + 37; /** * window type: application overlay windows are displayed above all activity windows * (types between {@link #first_application_window} and {@link #last_application_window}) * but below critical system windows like the status bar or ime. * <p> * the system may change the position, size, or visibility of these windows at anytime * to reduce visual clutter to the user and also manage resources. * <p> * requires {@link android.manifest.permission#system_alert_window} permission. * <p> * the system will adjust the importance of processes with this window type to reduce the * chance of the low-memory-killer killing them. * <p> * in multi-user systems shows only on the owning user's screen. */ public static final int type_application_overlay = first_system_window + 38; /** * end of types of system windows. */ public static final int last_system_window = 2999;
窗口显示顺序:
type 值越大层级越高,type 值大的覆盖 type 值小的,这只是一般规律。
(三)如何真正查看 window 的优先级
/frameworks/base/services/core/java/com/android/server/policy/windowmanagerpolicy.java default int getwindowlayerfromtypelw(int type, boolean canaddinternalsystemwindow) { if (type >= first_application_window && type <= last_application_window) { return application_layer; } switch (type) { case type_wallpaper: // wallpaper is at the bottom, though the window manager may move it. return 1; case type_presentation: case type_private_presentation: return application_layer; case type_dock_divider: return application_layer; case type_qs_dialog: return application_layer; case type_phone: return 3; case type_search_bar: case type_voice_interaction_starting: return 4; case type_voice_interaction: // voice interaction layer is almost immediately above apps. return 5; case type_input_consumer: return 6; case type_system_dialog: return 7; case type_toast: // toasts and the plugged-in battery thing return 8; case type_priority_phone: // sim errors and unlock. not sure if this really should be in a high layer. return 9; case type_system_alert: // like the anr / app crashed dialogs return canaddinternalsystemwindow ? 11 : 10; case type_application_overlay: return 12; case type_dream: // used for dreams (screensavers with type_dream windows) return 13; case type_input_method: // on-screen keyboards and other such input method user interfaces go here. return 14; case type_input_method_dialog: // on-screen keyboards and other such input method user interfaces go here. return 15; case type_status_bar: return 17; case type_status_bar_panel: return 18; case type_status_bar_sub_panel: return 19; case type_keyguard_dialog: return 20; case type_volume_overlay: // the on-screen volume indicator and controller shown when the user // changes the device volume return 21; case type_system_overlay: // the on-screen volume indicator and controller shown when the user // changes the device volume return canaddinternalsystemwindow ? 22 : 11; case type_navigation_bar: // the navigation bar, if available, shows atop most things return 23; case type_navigation_bar_panel: // some panels (e.g. search) need to show on top of the navigation bar return 24; case type_screenshot: // screenshot selection layer shouldn't go above system error, but it should cover // navigation bars at the very least. return 25; case type_system_error: // system-level error dialogs return canaddinternalsystemwindow ? 26 : 10; case type_magnification_overlay: // used to highlight the magnified portion of a display return 27; case type_display_overlay: // used to simulate secondary display devices return 28; case type_drag: // the drag layer: input for drag-and-drop is associated with this window, // which sits above all other focusable windows return 29; case type_accessibility_overlay: // overlay put by accessibility services to intercept user interaction return 30; case type_secure_system_overlay: return 31; case type_boot_progress: return 32; case type_pointer: // the (mouse) pointer layer return 33; default: slog.e("windowmanager", "unknown window type: " + type); return application_layer; } }
以上返回的是除 application 外的 window 的层级,在开发系统应用时可以选择一种开发自己的 window.
(四) 层级高低具体分析(对比toast以及软键盘)
低于toast--->盖不住toast 低于软键盘--->盖不住软键盘 依附activity使用--->is your activity running? public static int getwindowtype(int num) { switch (num) { case 1: return windowmanager.layoutparams.type_phone;//低于toast 低于软键盘 case 2: return windowmanager.layoutparams.type_search_bar;//低于toast 低于软键盘 case 3: return windowmanager.layoutparams.type_system_dialog;//低于toast 低于软键盘 case 4: return windowmanager.layoutparams.type_priority_phone;//高于toast 低于软键盘 case 5: return windowmanager.layoutparams.type_application_overlay;//高于toast 低于软键盘 case 6: return windowmanager.layoutparams.type_input_method_dialog;//高于toast 盖住键盘 case 7: return windowmanager.layoutparams.type_system_error;//高于toast 低于软键盘 case 8: return windowmanager.layoutparams.type_keyguard_dialog;//高于toast 低于软键盘 case 9: return windowmanager.layoutparams.type_system_alert;//高于toast 高于软键盘 case 10: return windowmanager.layoutparams.type_status_bar;//高于toast 高于软键盘(键盘会把透明view顶起) case 11: return windowmanager.layoutparams.type_status_bar_panel;//高于toast 高于软键盘(键盘会把透明view顶起) case 12: return windowmanager.layoutparams.type_system_overlay;//低于下级页面 case 13: return windowmanager.layoutparams.type_accessibility_overlay;//is your activity running? case 14: return windowmanager.layoutparams.type_base_application;// is your activity running? case 15: return windowmanager.layoutparams.type_application;// is your activity running? case 16: return windowmanager.layoutparams.type_application_starting;// is your activity running? case 17: return windowmanager.layoutparams.type_drawn_application;// is your activity running? case 18: return windowmanager.layoutparams.type_application_panel;// is your activity running? case 19: return windowmanager.layoutparams.type_application_media;// is your activity running? case 20: return windowmanager.layoutparams.type_application_sub_panel;// is your activity running? case 21: return windowmanager.layoutparams.type_application_attached_dialog;// is your activity running? case 22: return windowmanager.layoutparams.type_wallpaper;// is your activity running? case 23: return windowmanager.layoutparams.type_toast;// is your activity running? case 24: return windowmanager.layoutparams.type_input_method;//is your activity running? case 25: return windowmanager.layoutparams.type_private_presentation;//permission denied for window type 2030 default: return 0; } }
(五)如何定制系统层级
改变层级关系需要改写 getwindowlayerfromtypelw 的 switch 顺序结构和返回值
如果需要更改一些 window 的行为,需要修改
/frameworks/base/services/core/java/com/android/server/policy/phonewindowmanager.java
到此这篇关于android windowmanger的层级分析详解的文章就介绍到这了,更多相关android windowmanger的层级分析内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!