Android14 WMS-IWindowSession介绍

IWindowSession客户端是应用,服务端是WMS。

官网介绍其作用是是"System private per-application interface to the window manager.",

客户端通过IWindowSession向WMS发送请求,IWindowSession接口代码如下

java 复制代码
/**
 * System private per-application interface to the window manager.
 *
 * {@hide}
 */
interface IWindowSession {
    int addToDisplay(IWindow window, in WindowManager.LayoutParams attrs,
            in int viewVisibility, in int layerStackId, int requestedVisibleTypes,
            out InputChannel outInputChannel, out InsetsState insetsState,
            out InsetsSourceControl.Array activeControls, out Rect attachedFrame,
            out float[] sizeCompatScale);
    int addToDisplayAsUser(IWindow window, in WindowManager.LayoutParams attrs,
            in int viewVisibility, in int layerStackId, in int userId, int requestedVisibleTypes,
            out InputChannel outInputChannel, out InsetsState insetsState,
            out InsetsSourceControl.Array activeControls, out Rect attachedFrame,
            out float[] sizeCompatScale);
    int addToDisplayWithoutInputChannel(IWindow window, in WindowManager.LayoutParams attrs,
            in int viewVisibility, in int layerStackId, out InsetsState insetsState,
            out Rect attachedFrame, out float[] sizeCompatScale);
    @UnsupportedAppUsage
    void remove(IWindow window);
...

frameworks/base/core/java/android/view/IWindowSession.aidl

IWindowSession的方法可以参考链接如下:

IWindowSession.aidl - OpenGrok cross reference for /frameworks/base/core/java/android/view/IWindowSession.aidl

这里我们来看一下客户端如何获取WindowSession

WindowManagerGlobal.java - OpenGrok cross reference for /frameworks/base/core/java/android/view/WindowManagerGlobal.java

java 复制代码
    @UnsupportedAppUsage
    public static IWindowSession getWindowSession() {
        synchronized (WindowManagerGlobal.class) {
            if (sWindowSession == null) {
                try {
// Emulate the legacy behavior.  The global instance of InputMethodManager
// was instantiated here.
// TODO(b/116157766): Remove this hack after cleaning up @UnsupportedAppUsage
                    InputMethodManager.ensureDefaultInstanceForDefaultDisplayIfNecessary();
//第一步
                    IWindowManager windowManager = getWindowManagerService();
//第二步
                    sWindowSession = windowManager.openSession(
                            new IWindowSessionCallback.Stub() {
                                @Override
                                public void onAnimatorScaleChanged(float scale) {
                                    ValueAnimator.setDurationScale(scale);
                                }
                            });
                } catch (RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
            }
            return sWindowSession;
        }
    }

上述代码中,在第一步中,通过getWindowManagerService方法获取了server端binder对象,即获取了WMS服务, 看代码是单例模式,可以看出来这个sWindowSession是一个单例,也就是一个应用的所有ViewRootImpl的WindowSession都是同一个,也就是一个应用只有一个WindowSession,用于与WMS进行通信。

java 复制代码
    @UnsupportedAppUsage
    public static IWindowManager getWindowManagerService() {
        synchronized (WindowManagerGlobal.class) {
            if (sWindowManagerService == null) {
//获取server端binder对象,即获取WMS服务
                sWindowManagerService = IWindowManager.Stub.asInterface(
                        ServiceManager.getService("window"));
...
            return sWindowManagerService;
        }
    }

在第二步中,**则调用到了Server端服务WMS中的方法,这里也是单例模式的写法,**sWindowManagerService 在Client端也只有一个对象。

WindowManagerService.java - OpenGrok cross reference for /frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java

复制代码
    @Override
    public IWindowSession openSession(IWindowSessionCallback callback) {
        return new Session(this, callback);
    }

然后在WMS中new了一个Session,Session实例化相关如下

Session.java - OpenGrok cross reference for /frameworks/base/services/core/java/com/android/server/wm/Session.java

复制代码
class Session extends IWindowSession.Stub implements IBinder.DeathRecipient {
    final WindowManagerService mService;
    final IWindowSessionCallback mCallback;
    final int mUid;
    final int mPid;
  ...
    public Session(WindowManagerService service, IWindowSessionCallback callback) {
        mService = service;
        mCallback = callback;
        mUid = Binder.getCallingUid();
        mPid = Binder.getCallingPid();
...

来看看WMS,其实也是继承了 IWindowSession.Stub

复制代码
/** {@hide} */
public class WindowManagerService extends IWindowManager.Stub
        implements Watchdog.Monitor, WindowManagerPolicy.WindowManagerFuncs {

从上我们总结出:

一个应用只有一个WindowSession, 用于与WMS进行通信。

此外,可以通过dumpsys来看下当前系统所存在的Session

WINDOW MANAGER SESSIONS (dumpsys window sessions )

Session Session{d3f0caf 2509:1000}:

mNumWindow=1 mCanAddInternalSystemWindow=true mAppOverlaySurfaces={} mAlertWindowSurfaces={} mClientDead=false mSurfaceSession=android.view.SurfaceSession@fcb7984

mPackageName=com.android.settings

相关推荐
CYRUS STUDIO3 分钟前
ARM64汇编寻址、汇编指令、指令编码方式
android·汇编·arm开发·arm·arm64
weixin_449310841 小时前
高效集成:聚水潭采购数据同步到MySQL
android·数据库·mysql
Zender Han1 小时前
Flutter自定义矩形进度条实现详解
android·flutter·ios
白乐天_n3 小时前
adb:Android调试桥
android·adb
姑苏风7 小时前
《Kotlin实战》-附录
android·开发语言·kotlin
数据猎手小k10 小时前
AndroidLab:一个系统化的Android代理框架,包含操作环境和可复现的基准测试,支持大型语言模型和多模态模型。
android·人工智能·机器学习·语言模型
你的小1011 小时前
JavaWeb项目-----博客系统
android
风和先行11 小时前
adb 命令查看设备存储占用情况
android·adb
AaVictory.12 小时前
Android 开发 Java中 list实现 按照时间格式 yyyy-MM-dd HH:mm 顺序
android·java·list
似霰13 小时前
安卓智能指针sp、wp、RefBase浅析
android·c++·binder