Android 窗口焦点介绍
背景¶
我们经常会遇到一种Application does not hava focused window的ANR异常,这种异常一般是没有焦点窗口FocusedWindow导致,且这类异常只会发生在key事件的派发,因为key事件是需要找到一个焦点窗口然后再派发,而触摸事件只需要找到当前显示的窗口即可
焦点窗口设定¶
WMS只管理窗口,无法确定是否有窗口盖住当前画面。 SurfaceFlinger管理显示,最贴近于用户看到的画面,可以知道是否有窗口盖住当前画面,根据真实的显示窗口设置对应的window信息给InputDispatcher。
关键日志¶
window¶
在dumpsys window中查看mCurrentFocus和mFocusedApp
mCurrentFocus=Window{f96644 u0 NotificationShade}
mFocusedApp=ActivityRecord{e9566ee u0 com.android.launcher3/.uioverrides.QuickstepLauncher} t12}
mCurrentFocus 指的是当前的焦点窗口
mFocusedApp 指的是当前的焦点Activity
查看有没有LAST ANR
这里没有LAST ANR,如果有,mCurrentFocus会显示null
SurfaceFlinger¶
在dumpsys SurfaceFlinger中查看 HWC layers
Display 4619827259835644672 (active) HWC layers:
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Layer name
Z | Window Type | Comp Type | Transform | Disp Frame (LTRB) | Source Crop (LTRB) | Frame Rate (Explicit) (Seamlessness) [Focused]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
com.example.mysystemdialog/com.example.mysystemdialog.MainActivity#118
rel 0 | 1 | CLIENT | 0 | 0 0 1440 2960 | 0.0 0.0 1440.0 2960.0 | [*]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
StatusBar#75
rel 0 | 2000 | CLIENT | 0 | 0 0 1440 84 | 0.0 0.0 1440.0 84.0 | [ ]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
NavigationBar0#74
rel 0 | 2019 | CLIENT | 0 | 0 2792 1440 2960 | 0.0 0.0 1440.0 168.0 | [ ]
---------------------------------------------------------------------------------------------------------------------------------------------------------------
[Focused] 这一列有带[*]号,则说明是焦点窗口
input¶
在dumpsys input中查看FocusedApplications和FocusedWindows
FocusedApplications:
displayId=0, name='ActivityRecord{e9566ee u0 com.android.launcher3/.uioverrides.QuickstepLauncher} t12}', dispatchingTimeout=5000ms
FocusedWindows:
displayId=0, name='f96644 NotificationShade'
如果发生ANR,焦点窗口以dumpsys input为主
Input Dispatcher State at time of last ANR:
ANR:
Time:......
Reason:......
Window:......
FocusedApplications:......
FocusedWindows: <none>
event log¶
05-18 19:22:55.806 580 607 I input_focus: [Focus request f96644 NotificationShade,reason=UpdateInputWindows]
05-18 19:22:55.837 580 675 I input_focus: [Focus leaving e225d94 com.android.launcher3/com.android.launcher3.uioverrides.QuickstepLauncher (server),reason=Waiting for window because NO_WINDOW]
05-18 19:22:55.855 580 675 I input_focus: [Focus entering f96644 NotificationShade (server),reason=Window became focusable. Previous reason: NOT_VISIBLE]
request 和 entering正常情况下是一一对应,打印了entering则表示真正的焦点已经进入到对应的窗口。 发生Application does not hava focused window时,一般request 有打印,我们可以通过是否有entering的打印来分析。 - entering部分有打印,代表焦点已经在input里面,但是仍然有ANR,就需要从input等方面分析。 - entering部分未打印,代表input没有被触发焦点窗口设置到input,需排查SurfaceFlinger或WMS。