【Android】【input子系统】【Android 焦点窗口问题分析思路】
概述
我们开发中可能回遇到Application does not hava focused window的ANR异常或InputDispatching TimeOut的异常,这里我们梳理一下分析的思路。
问题分析
关键日志
1、window
在dumpsys window中查看mCurrentFocus和mFocusedApp,也可以通过如下shell命令来查看当前的FocusWindow:
console:/ # dumpsys window d | egrep "mCurrentFocus|mFocusedApp"mCurrentFocus=Window{fee82dd u0 com.android.launcher3/com.android.launcher3.LauncherActivity}mFocusedApp=ActivityRecord{2a7b70a u0 com.android.launcher3/.LauncherActivity t48}
mCurrentFocus指的是当前的焦点窗口,mFocusedApp指的是当前的焦点Activity。
InputWindow是指能接收input事件的窗口,当WMS中状态发生变化后,会将所有符合条件的窗口设置给底层InputFlinger中,在派发事件时,将对从这些窗口中选择目标窗口进行派发,这些窗口就是InputWindow。焦点窗口只有一个,但InputWindow可以有多个。
2、SurfaceFlinger
console:/ # dumpsys SurfaceFlinger | grep -i "HWC layer" -A 20
Display 4616378844849242880 (active) HWC layers:
---------------------------------------------------------------------------------------------------------------------------------------------------------------Layer nameZ | Window Type | Comp Type | Transform | Disp Frame (LTRB) | Source Crop (LTRB) | Frame Rate (Explicit) (Seamlessness) [Focused]
---------------------------------------------------------------------------------------------------------------------------------------------------------------Wallpaper BBQ wrapper#1032rel 0 | 0 | CLIENT | 0 | 0 0 1920 1080 | 131.0 164.0 2749.0 1636.0 | [ ]
---------------------------------------------------------------------------------------------------------------------------------------------------------------com.android.launcher3/com.android.launcher3.LauncherActivity#1191rel 0 | 1 | CLIENT | 0 | 0 0 1920 1080 | 0.0 0.0 1920.0 1080.0 | [*]
---------------------------------------------------------------------------------------------------------------------------------------------------------------StatusBar#1041rel 0 | 2000 | CLIENT | 0 | 0 0 1920 32 | 0.0 0.0 1920.0 32.0 | [ ]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
[Focused]这一列有带[*]号,则说明是焦点窗口。
3、input
在dumpsys input中查看FocusedApplications和FocusedWindows
console:/ # dumpsys input | grep "FocusedApplications" -A 10FocusedApplications:displayId=0, name='ActivityRecord{2a7b70a u0 com.android.launcher3/.LauncherActivity t48}', dispatchingTimeout=5000msFocusedWindows:displayId=0, name='fee82dd com.android.launcher3/com.android.launcher3.LauncherActivity'FocusRequests:displayId=0, name='fee82dd com.android.launcher3/com.android.launcher3.LauncherActivity' result='OK'Pointer Capture Requested: falseCurrent Window with Pointer Capture: NoneTouchStates: <no displays touched>Display: 0logicalSize=1920x1080
4、eventlog
logcat -b events | grep -i anr
logcat -s InputDispatcher
logcat -b events | grep -i input
logcat -b events | grep -i input_focus
打开protoLog的动态Focus Log,打开input子系统的Focus Log。
关键Log:
11-27 16:15:58.902 3932 4137 I input_focus: [Focus request 5e78d93 com.android.mms/com.android.mms.ui.MmsTabActivity,reason=UpdateInputWindows]
11-27 16:15:58.922 3932 6384 I input_focus: [Focus receive :5e78d93 com.android.mms/com.android.mms.ui.MmsTabActivity,reason=setFocusedWindow]
11-27 16:15:59.027 3932 4436 I input_focus: [Focus entering 5e78d93 com.android.mms/com.android.mms.ui.MmsTabActivity (server),reason=Window became focusable. Previous reason: NOT_VISIBLE]
request 和 entering正常情况下是一一对应,打印了entering则表示真正的焦点已经进入到对应的窗口
发生Application does not hava focused window时,一般request 有打印,我们可以通过是否有entering的打印来分析
1.entering部分有打印,代表焦点已经在input里面,但是仍然有ANR,就需要从input等方面分析 2.entering部分未打印,代表input没有被触发焦点窗口设置到input,需排查SurfaceFlinger或WMS。
说明
1、wms焦点窗口正常,不代表焦点窗口已经同步到inputdispatch
2、同步到inputdispatch也不代表可以正常使用,可能该window对应的layer,已经移除了。