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

相关推荐
朱涛的自习室9 小时前
我给 Gemini 4 和 DeepSeek 搞了套「末位淘汰」,AI 干活会更猛吗?(Day 3)
android·前端·后端
应用市场15 小时前
离线小游戏做“每日挑战”:日期当随机种子,整局存成 JSON,再加一个“我不是机器人”
android·kotlin·游戏开发
一铭1119915 小时前
海豹办公 1.9.15 跨端办公软件实践 文档兼容 Access 数据库与移动 WebView 排错
android·数据库·electron·开源软件·access
geats人山人海16 小时前
java 7异常
android·java·开发语言
2601_9620710017 小时前
Kimi K3 vs DeepSeek V4-Pro实测对比:2026年同一入口十道题,耗时与token消耗差在哪
android·java·数据库
右手吉他18 小时前
高通平台IA APN问题分析之二
android·物联网
夜雪一千19 小时前
MySQL 自增 ID 的底层原理与坑点
android·mysql·adb
不浪费一个字节19 小时前
一个纯 Python 实现的 APK 解析与设备操控工具箱
android
itmigrate19 小时前
PHP 导出大 CSV 时内存溢出的一次排查
android·开发语言·php
hai_android19 小时前
LiveEventBusCore 跨 Activity 通讯源码解析
android·kotlin·android jetpack