目录
简介
Insets管理架构
Insets相关类图
app侧的类
WMS侧的类
inset show的流程
接口
流程
WMS侧确定InsetsSourceControl的流程
两个问题
窗口显示时不改变现有的inset状态
全屏窗口上的dialog 不显示statusbar问题
View 和 DecorView 设置insets信息
输入法显示流程
1. 在某个app侧点击编辑框
2.输入法app接收到显示输入法消息
3.InputMethodManagerService接收到输入法app的显示状态信息
4.输入法窗口接收到showInsets@IWindow
监听inset 变化
设置Insetscontroller变化监听
应用WindowInsets变化
android 11上新增一套inset管理方法。
Insets 是指系统边衬区的窗口, 包括statusbar, navigation bar, 输入法等, 都在insets管理中。下面说的这些insets即为这些窗口。
通过insets相关的接口, app可以控制insets窗口的显示, 隐藏, 沉浸式等。
inset 最基本的控制是show 和hide。 在inset 不同的状态下, 应用区的位置也会发生变化, 这部分的计算也是inset控制的重要内容之一。
insets完整的实现逻辑, 包含app端和服务端,服务端主要是WMS(window manager service)。 本文梳理insets的管理架构和主要逻辑, 如show insets 等。
下面为dump 出来的insets信息, 使用命令adb shell dumpsys window。 InsetsState 是系统当前所有inset状态的集合,InsetsSource 对应每一个inset, 包括type, frame, visible项:
InsetsState
InsetsSource type=ITYPE_STATUS_BAR frame=[0,0][2776,130] visible=false
InsetsSource type=ITYPE_NAVIGATION_BAR frame=[0,0][744,1022] visible=false
InsetsSource type=ITYPE_TOP_GESTURES frame=[0,0][2776,130] visible=true
InsetsSource type=ITYPE_BOTTOM_GESTURES frame=[0,1017][744,1022] visible=true
InsetsSource type=ITYPE_LEFT_GESTURES frame=[0,0][0,1022] visible=true
InsetsSource type=ITYPE_RIGHT_GESTURES frame=[2776,0][2776,1022] visible=true
InsetsSource type=ITYPE_TOP_TAPPABLE_ELEMENT frame=[0,0][2776,130] visible=true
InsetsSource type=ITYPE_BOTTOM_TAPPABLE_ELEMENT frame=[0,1017][744,1022] visible=true
InsetsSource type=ITYPE_IME frame=[0,0][0,0] visible=false
分为app侧和系统服务侧(wms)侧。
app侧持有的InsetsSourceControl来自于添加窗口时 addWindow()和 relayoutWindow()时传回的 mTempControls。 实测 addWindow时mTempControls传回值为null. 在relayoutWindow 时传回mTempControls为实际值。WMS在在relayout 过程中, 会寻找inset 的target 窗口, 通过addToControlMaps@ InsetsStateController.java为该类型的窗口添加target, 然后在 relayoutWindow()中通过调用win.getDisplayContent().getInsetsStateController().getControlsForDispatch(win); 获取该win的InsetsSourceControl 传回给app端的mTempControls。
ViewRootImp.setView()-->
addWindow(...mTempControls) //获得mTempControls
mInsetsController.onControlsChanged(mTempControls)-->
consumer.setControl(control, showTypes, hideTypes);
创建InsetsController stack, 从下向上
at android.view.InsetsController.(InsetsController.java:525)
at android.view.ViewRootImpl.(ViewRootImpl.java:768)
at android.view.ViewRootImpl.(ViewRootImpl.java:720)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:401)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109)
mProviders为insets的provider的集合。
mTypeControlTargetMap为可以控制每种Insets(如显示或隐藏)的窗口的集合。
mState为InsetsState, 当前系统所有inset 的状态。
(注: 每个WindowState通过InsetsStateController.getInsetsForDispatch获取该窗口的state, 通过relayoutWindow或者addWindow 中的outInsetsState.set(win.getInsetsState(), win.isClientLocal())将当前窗口insetState 转给app。app端通过mInsetsController.onStateChanged @ViewRootImpl将状态设置给InsetsController。 app 根据这个insetstate计算内容区域。 app 计算inset区域:mInsetsController.calculateInsets。)
DisplayContent和InsetsPolicy均持有InsetsStateController,为同一实例。
inset 基本的控制是show 和hide, inset show和hide流程基本一样,以show流程说明流程。
app 控制insets显示或者隐藏调用的接口如下, 关于InsetsController参见InsetsController类说明。
getWindow().getInsetsController().show(insetTypeList) //实际对应InsetsController.show()
getWindow().getInsetsController().hide(insetTypeList) //实际对应InsetsController.hide()
调用InsetsController.show/hide的调用stack如下, 从下到上,这个stack只到InsetsController.notifyVisibilityChanged(),该stack只是记录和参考,不作详细说明。 具体需要注意的是InsetsController.notifyVisibilityChanged()之后流程。
//此函数先调用ViewRootInsetsControllerHost.notifyInsetsChanged 后调用updateRequestedState
at android.view.InsetsController.notifyVisibilityChanged(InsetsController.java:1208)
at android.view.InsetsSourceConsumer.setRequestedVisible(InsetsSourceConsumer.java:344)
at android.view.InsetsSourceConsumer.show(InsetsSourceConsumer.java:196)
at android.view.InsetsController.showDirectly(InsetsController.java:1325)
at android.view.InsetsController.controlAnimationUnchecked(InsetsController.java:1013)
at android.view.InsetsController.applyAnimation(InsetsController.java:1305)
at android.view.InsetsController.show(InsetsController.java:870)
at android.view.InsetsController.show(InsetsController.java:826)
InsetsController.notifyVisibilityChanged()函数如下, 先调用ViewRootInsetsControllerHost.notifyInsetsChanged 后调用updateRequestedState。
public void notifyVisibilityChanged() {
mHost.notifyInsetsChanged(); //实际在viewRootImpl中requestLayout, 也就是在下一个vsync中与wms交互去Relayout()。
updateRequestedState(); //调用了ViewRootInsetsControllerHost.onInsetsModified。 交互流程见下面的流程图。
}
at android.view.ViewRootImpl.notifyInsetsChanged(ViewRootImpl.java:1603) //下一个vsync relayout
at android.view.ViewRootInsetsControllerHost.notifyInsetsChanged(ViewRootInsetsControllerHost.java:54) //
at android.view.InsetsController.notifyVisibilityChanged(InsetsController.java:1208)
updateRequestedState用于通知wms端app请求inset变化,wms处理相应的inset显示和隐藏, 并通知其他app系统inset变化。 调用了ViewRootInsetsControllerHost.onInsetsModified,交互流程见下面的流程图。app端调用IWindowSession.insetsModified(IWindow window, in InsetsState state)通知wms inset变化, 其中InsetsState为修改后的inset状态。 wms通过InsetSourceProvider.setClientVisible设置inset窗口显示状态。 然后发送消息给所有的活动窗口, 通知insetsChanged。 每个活动窗口调用mInsetsController.onStateChanged设置自己的inset窗口状态,onStateChanged也会调用notifyInsetsChanged重新relayout 窗口。而发起show流程的窗口,因为state已经修改为当前的状态, 所以onStateChanged不再执行该操作。
at android.view.ViewRootInsetsControllerHost.onInsetsModified(ViewRootInsetsControllerHost.java:147) //调用WindowSession.insetsModified()通知wms 修改inset visibility
at android.view.InsetsController.updateRequestedState(InsetsController.java:1287)
at android.view.InsetsController.notifyVisibilityChanged(InsetsController.java:1209)
注: app 调用notifyInsetsChanged的几个地方,仅做参考:
1. 在onStateChanged@InsetsController中调用, 比如在ViewRootImpl.setView中调用onStateChanged, 从下到上:
ViewRootInsetsControllerHost.notifyInsetsChanged()
android.view.ViewRootInsetsControllerHost.notifyInsetsChanged(ViewRootInsetsControllerHost.java:54)
at android.view.InsetsController.onStateChanged(InsetsController.java:630) //在ViewRootImpl.setView中调用mWindowSession.addToDisplayAsUser后,wms 返回了当前窗口对应的InsetsSourceControl和InsetsState, 调用本函数 at android.view.ViewRootImpl.setView(ViewRootImpl.java:1059)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:411)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109)
2. 在setFrame@ViewRootImpl中调用, 如在ViewRootImpl.setView中调用setFrame(), 从小到上
ViewRootInsetsControllerHost.notifyInsetsChanged()
android.view.ViewRootInsetsControllerHost.notifyInsetsChanged(ViewRootInsetsControllerHost.java:54)
at android.view.InsetsController.onFrameChanged(InsetsController.java:594)
at android.view.ViewRootImpl.setFrame(ViewRootImpl.java:7493)
at android.view.ViewRootImpl.setView(ViewRootImpl.java:1036)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:411)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109)
WMS侧如何确定某个app 可以持有哪些InsetsSourceControl。 在该窗口relayout中, 根据当前window 的focus情况, 只有focus的窗口可以获取InsetsSourceControl,也就是控制inset的显示和隐藏, 没有focus的窗口不能获取InsetsSourceControl,也就是不能控制inset的显示和隐藏。当一个窗口设置了FLAG_NOT_FOCUSABLE, 就不在能控制inset的显示和隐藏。流程从下到上:
// addToControlMaps 设置了[email protected], 设置了mControlTargetTypeMap后, 通过在relayout 中调用getInsetsSourceControls传回给app进程的mTempControls。
at com.android.server.wm.InsetsStateController.addToControlMaps(InsetsStateController.java:523)
at com.android.server.wm.InsetsStateController.onControlChanged(InsetsStateController.java:469)
at com.android.server.wm.InsetsStateController.onBarControlTargetChanged(InsetsStateController.java:424)
at com.android.server.wm.InsetsPolicy.updateBarControlTarget(InsetsPolicy.java:150)
at com.android.server.wm.DisplayPolicy.updateSystemUiVisibilityLw(DisplayPolicy.java:3940)
at com.android.server.wm.DisplayPolicy.focusChangedLw(DisplayPolicy.java:3736)
at com.android.server.wm.DisplayContent.updateFocusedWindowLocked(DisplayContent.java:3286)
at com.android.server.wm.RootWindowContainer.updateFocusedWindowLocked(RootWindowContainer.java:461)
at com.android.server.wm.WindowManagerService.updateFocusedWindowLocked(WindowManagerService.java:5552)
at com.android.server.wm.WindowManagerService.relayoutWindow(WindowManagerService.java:2348)
两种方法:
一种就是控制inset 和上一个窗口相同,获取当前insets状态方法: getWindowManager().getCurrentWindowMetrics().getWindowInsets。示例
在onCreate 或 onResume中调用以下代码,也就是mWm.addView(mDecorView, l);之前调用。
WindowInsets windowInsets =
getWindowManager().getCurrentWindowMetrics().getWindowInsets();
hideTypeList = getHideTypeList(windowInsets)//获取hide 的type list。
showTypeList = getShowTypeList(windowInsets)//获取show 的type list。
WindowInsetsController controller = decorView.getWindowInsetsController();
controller.hide(hideTypeList);
controller.show(hideTypeList);
另一种,就是设置窗口为FLAG_NOT_FOCUSABLE。
FLAG_NOT_FOCUSABLE, 导致没有focus window 变化, 从而不能设置获取InsetsSourceControl,也就无法控制所有insets窗口的show和hide。 代码:
l.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
// | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE // 本句需要注释掉,才能获取InsetsSourceControl。
| WindowManager.LayoutParams.FLAG_FULLSCREEN;
wms中判断应用是否focus的代码: mFindFocusedWindow
at com.android.server.wm.WindowState.canReceiveKeys(WindowState.java:2877) // 如果设置FLAG_NOT_FOCUSABLE, canReceiveKeys 返回false, 认为改窗口非焦点窗口, 不改变inset设置。
受全屏窗口设置影响。 系统单独对status bar做了设置:
流程如下, DecorView的onApplyWindowInsets 会调用mInsetsController.calculateInsets,计算应用区的大小。
at com.android.internal.policy.DecorView.onApplyWindowInsets(DecorView.java:1046)//demorview 将inset区域减去,其他的区域作为内容区
at android.view.View.dispatchApplyWindowInsets(View.java:11311)
at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7320)
at android.view.ViewRootImpl.dispatchApplyInsets(ViewRootImpl.java:2311) //会通过calculateInsets 计算当前inset, 将当前inset应用到view。
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2439)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1948)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8179)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
at android.view.Choreographer.doCallbacks(Choreographer.java:796)
at android.view.Choreographer.doFrame(Choreographer.java:731)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
输入法也是一种inset, 其显示和隐藏的流程也与inset 显示隐藏一致。 下面介绍的是在点击编辑框时,输入法显示流程。
最后调用showSoftInput@InputMethodManager :
showSoftInput:1587, InputMethodManager (android.view.inputmethod)
onTouchEvent:11082, TextView (android.widget)
dispatchTouchEvent:14309, View (android.view)
dispatchTransformedTouchEvent:3118, ViewGroup (android.view)
dispatchTouchEvent:2799, ViewGroup (android.view)
dispatchTransformedTouchEvent:3118, ViewGroup (android.view)
dispatchTouchEvent:2799, ViewGroup (android.view)
dispatchTransformedTouchEvent:3118, ViewGroup (android.view)
dispatchTouchEvent:2799, ViewGroup (android.view)
dispatchTransformedTouchEvent:3118, ViewGroup (android.view)
dispatchTouchEvent:2799, ViewGroup (android.view)
superDispatchTouchEvent:515, DecorView (com.android.internal.policy)
superDispatchTouchEvent:1879, PhoneWindow (com.android.internal.policy)
dispatchTouchEvent:4135, Activity (android.app)
dispatchTouchEvent:473, DecorView (com.android.internal.policy)
dispatchPointerEvent:14568, View (android.view)
processPointerEvent:6024, ViewRootImpl$ViewPostImeInputStage (android.view)
onProcess:5827, ViewRootImpl$ViewPostImeInputStage (android.view)
deliver:5318, ViewRootImpl$InputStage (android.view)
onDeliverToNext:5375, ViewRootImpl$InputStage (android.view)
forward:5341, ViewRootImpl$InputStage (android.view)
forward:5493, ViewRootImpl$AsyncInputStage (android.view)
apply:5349, ViewRootImpl$InputStage (android.view)
apply:5550, ViewRootImpl$AsyncInputStage (android.view)
deliver:5322, ViewRootImpl$InputStage (android.view)
onDeliverToNext:5375, ViewRootImpl$InputStage (android.view)
forward:5341, ViewRootImpl$InputStage (android.view)
apply:5349, ViewRootImpl$InputStage (android.view)
deliver:5322, ViewRootImpl$InputStage (android.view)
deliverInputEvent:8088, ViewRootImpl (android.view)
doProcessInputEvents:8039, ViewRootImpl (android.view)
enqueueInputEvent:8000, ViewRootImpl (android.view)
onInputEvent:8211, ViewRootImpl$WindowInputEventReceiver (android.view)
dispatchInputEvent:220, InputEventReceiver (android.view)
nativePollOnce:-1, MessageQueue (android.os)
next:335, MessageQueue (android.os)
loop:183, Looper (android.os)
main:7664, ActivityThread (android.app)
invoke:-1, Method (java.lang.reflect)
run:592, RuntimeInit$MethodAndArgsCaller (com.android.internal.os)
main:947, ZygoteInit (com.android.internal.os)
输入法app, 即实现inputmethodservice的app。 其与InputMethodManagerService的接口为IInputMethodWrapper。 InputMethodManagerService 调用 IInputMethodWrapper.showSoftInput, 其发送消息DO_SHOW_SOFT_INPUT。 处理DO_SHOW_SOFT_INPUT, 调用到InputMethodService.showSoftInput (), 最终调用到IInputMethodPrivilegedOperations.applyImeVisibility().IInputMethodPrivilegedOperations 为inputMethodService与InputMethodManagerService的通信接口。
applyVisibilityInInsetsConsumerIfNecessary:2219, InputMethodService (android.inputmethodservice)
access$400:263, InputMethodService (android.inputmethodservice)
showSoftInput:748, InputMethodService$InputMethodImpl (android.inputmethodservice)
showSoftInputWithToken:718, InputMethodService$InputMethodImpl (android.inputmethodservice)
executeMessage:226, IInputMethodWrapper (android.inputmethodservice)
handleMessage:44, HandlerCaller$MyHandler (com.android.internal.os)
dispatchMessage:106, Handler (android.os)
loop:223, Looper (android.os)
main:7664, ActivityThread (android.app)
invoke:-1, Method (java.lang.reflect)
run:592, RuntimeInit$MethodAndArgsCaller (com.android.internal.os)
main:947, ZygoteInit (com.android.internal.os)
输入法app调用InputMethodManagerService.applyImeVisibility() 通知InputMethodManagerService(IMMS)显示状态变化,IMMS然后调用到scheduleShowImePostLayout@ImeInsetsSourceProvider,该函数在下一个vsync调用输入法窗口的windowState.showInsets通知输入法窗口: ims: show.
scheduleShowImePostLayout:52, ImeInsetsSourceProvider (com.android.server.wm)
showImePostLayout:7615, WindowManagerService$LocalService (com.android.server.wm)
applyImeVisibility:4088, InputMethodManagerService (com.android.server.inputmethod)
access$4700:188, InputMethodManagerService (com.android.server.inputmethod)
applyImeVisibility:5935, InputMethodManagerService$InputMethodPrivilegedOperationsImpl (com.android.server.inputmethod)
onTransact:336, IInputMethodPrivilegedOperations$Stub (com.android.internal.inputmethod)
execTransactInternal:1154, Binder (android.os)
execTransact:1123, Binder (android.os)
输入法在接收到ims显示状态变化后, 如下面流程,调用InsetsController.show, 后面的流程和前面介绍的inset show的流程一致: 通知wms ims inset状态变化, 并通知给所有活动中的窗口。 在下一个vsync, 调用relayoutWindow(),重新布局。下面为输入法窗口的showInsets流程。
"main@15899" prio=5 tid=0x2 nid=NA runnable
java.lang.Thread.State: RUNNABLE
at android.view.InsetsController.updateRequestedState(InsetsController.java:1262)
at android.view.InsetsController.notifyVisibilityChanged(InsetsController.java:1209)
at android.view.InsetsSourceConsumer.setRequestedVisible(InsetsSourceConsumer.java:344)
at android.view.InsetsSourceConsumer.show(InsetsSourceConsumer.java:196)
at android.view.InsetsController.showDirectly(InsetsController.java:1325)
at android.view.InsetsController.controlAnimationUnchecked(InsetsController.java:1013)
at android.view.InsetsController.applyAnimation(InsetsController.java:1305)
at android.view.InsetsController.show(InsetsController.java:870)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:5031) //处理 MSG_SHOW_INSETS, MSG_SHOW_INSETS 为wms 调用iWindow.showInsets触发。
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7664)
at java.lang.reflect.Method.invoke(Method.java:-1)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
窗口中的view可以在insets变化时, 改变默认的inset 占位行为。 监听inset变化, 然后自行设置insets如何占位。 如:
InsetsController.addOnControllableInsetsChangedListener(OnControllableInsetsChangedListener ...);
getWindow().getDecorView().setOnApplyWindowInsetsListener(OnApplyWindowInsetsListener ...);
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
mImeVisible = insets.isVisible(ime());
return v.onApplyWindowInsets(insets);
}