WindowManagerService介绍

Andorid系统中的窗口是屏幕上一块用于绘制各种UI并且可以响应用户输入的一个矩形区域。WMS主要负责管理所有窗口;根据窗口的类型来调整窗口的显示次序,窗口大小位置,向SF申请画布, 以及窗口动画的管理

WindowManagerService主要完成了以下几部分功能:
1 窗口的添加和删除
2 窗口的显示次序
3 窗口布局
4 窗口动画 



WMS类之间的联系:

在这里插入图片描述

Token和WindowState之间的关系

在这里插入图片描述

WMS类对象介绍
// 一个apk进程在WMS中对应一个session对象,客服端和WMS连接的桥梁
Session

// 窗口令牌,一个窗口令牌可以对应多个WindowState
WindowToken

// activity令牌, 一个窗口对应一个Activity对应一个ActivityRecord
AppWindowToken

// WMS中记录窗口的状态信息
WindowState

// 一个可以绘制屏幕对应一个DisplayContent
DisplayContent

// WindowManagerPolicy策略类的具体实现
PhoneWindowManager

// 动画相关
WindowAnimator

// 管理窗口相关的类
RootWindowContainer

// 画布相关
SurfaceControl
WMS成员变量介绍
// 一个apk进程在WMS中对应一个session对象,客服端和WMS连接的桥梁
final ArraySet<Session> mSessions = new ArraySet<>();

// 存储所有的WindowState 以IBinder为key, WindowState为value
final WindowHashMap mWindowMap = new WindowHashMap();

// 管理窗口相关
RootWindowContainer mRoot;

// 窗口策略对象,具体实现为PhoneWindowManager
final WindowManagerPolicy mPolicy;

final WindowSurfacePlacer mWindowPlacerLocked;

// 输入法管理器
IInputMethodManager mInputMethodManager;

// 当前聚焦的窗口
WindowState mCurrentFocus = null;

// 输入法窗口对应的目标窗口
WindowState mInputMethodTarget = null;

// 输入法窗口
WindowState mInputMethodWindow = null;

// 当前聚焦的Activity
AppWindowToken mFocusedApp = null;

WMS重要函数介绍
// 和WMS之间建立联系
IWindowSession openSession(in IWindowSessionCallback callback);

// 添加对应的窗口令牌
void addWindowToken(IBinder token, int type, int displayId);

// 删除对应的窗口令牌
void removeWindowToken(IBinder token, int displayId);

// 添加窗口 addToDisplay = addWindow
int addToDisplay(IWindow window, int seq, in WindowManager.LayoutParams attrs,
            in int viewVisibility, in int layerStackId, out Rect outFrame,
            out Rect outContentInsets, out Rect outStableInsets,
            out DisplayCutout.ParcelableWrapper displayCutout, out InputChannel outInputChannel,
            out InsetsState insetsState, out InsetsSourceControl[] activeControls);

// 移除窗口
void remove(IWindow window);

// 布局, 分配画布
int relayout(IWindow window, int seq, in WindowManager.LayoutParams attrs,
            int requestedWidth, int requestedHeight, int viewVisibility,
            int flags, long frameNumber, out Rect outFrame,
            out Rect outContentInsets, out Rect outVisibleInsets, out Rect outStableInsets,
            out Rect outBackdropFrame,
            out DisplayCutout.ParcelableWrapper displayCutout,
            out MergedConfiguration outMergedConfiguration, out SurfaceControl outSurfaceControl,
            out InsetsState insetsState, out InsetsSourceControl[] activeControls,
            out Point outSurfaceSize, out SurfaceControl outBlastSurfaceControl);

窗口类型
// 应用程序窗口: Activity, Dailog
public static final int FIRST_APPLICATION_WINDOW = 1;
public static final int LAST_APPLICATION_WINDOW = 99;

// 子窗口
public static final int FIRST_SUB_WINDOW = 1000;
public static final int LAST_SUB_WINDOW = 1999;

// 系统窗口:输入法,壁纸, Toast, 状态栏等窗口
public static final int FIRST_SYSTEM_WINDOW = 2000;
public static final int TYPE_STATUS_BAR = FIRST_SYSTEM_WINDOW;
public static final int TYPE_SEARCH_BAR = FIRST_SYSTEM_WINDOW+1;
以添加壁纸窗口为列
// step 1, 添加壁纸token
mIWindowManager.addWindowToken(newConn.mToken, TYPE_WALLPAPER, DEFAULT_DISPLAY);

// step 2 , 获取对应的Session对象
mSession = WindowManagerGlobal.getWindowSession();

// step 3 , 创建对应的window
BaseIWindow mWindow = new BaseIWindow();

// step 4 , 将window添加到WMS中
mSession.addToDisplay(mWindow, mWindow.mSeq, mLayout, View.VISIBLE,
            Display.DEFAULT_DISPLAY, mContentInsets, mStableInsets, mOutsets, mInputChannel)


Android低版本添加系统窗口
WindowManager mWm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
Button view = new Button(this);
view.setText("window manager test!");
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
// WindowToken由WMS创建
mParams.type = TYPE_SYSTEM_ALERT;
mWm.addView(view, mParams);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值