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的方法可以参考链接如下:
这里我们来看一下客户端如何获取WindowSession
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端也只有一个对象。
@Override public IWindowSession openSession(IWindowSessionCallback callback) { return new Session(this, callback); }
然后在WMS中new了一个Session,Session实例化相关如下
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