Android window focus introduction

background

We often encounter an Application does not hava focused windowexception ANR. This exception is usually caused by the FocusedWindow not having the focus window, and this type of exception will only occur when the key event is dispatched , because the key event needs to find a focused window and then dispatch it, while the touch event Just find the currently displayed window

Focus window settings

Insert image description hereWMS only manages windows, and cannot determine whether there is a window covering the current screen.
SurfaceFlinger manages the display, which is closest to the screen seen by the user. It can know whether there is a window covering the current screen, and sets the corresponding window information based on the real display window. InputDispatcher

Key log

window

View mCurrentFocus and mFocusedApp in dumpsys window

  mCurrentFocus=Window{
    
    f96644 u0 NotificationShade}
  mFocusedApp=ActivityRecord{
    
    e9566ee u0 com.android.launcher3/.uioverrides.QuickstepLauncher} t12}

mCurrentFocusRefers to the current focus window
mFocusedApprefers to the current focus Activity

Check if there is LAST ANR

WINDOW MANAGER LAST ANR (dumpsys window lastanr)
  <no ANR has occurred since boot>

There is no LAST ANR here, if there is, mCurrentFocus will show null

SurfaceFlinger

View HWC layers in dumpsys SurfaceFlinger

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]If this column is marked with [*], it means it is the focus window.

input

View FocusedApplications and FocusedWindows in dumpsys input

  FocusedApplications:
    displayId=0, name='ActivityRecord{e9566ee u0 com.android.launcher3/.uioverrides.QuickstepLauncher} t12}', dispatchingTimeout=5000ms
  FocusedWindows:
    displayId=0, name='f96644 NotificationShade'

If ANR occurs, the focus window will be 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]

requestThere is a one-to-one correspondence with enteringthe normal situation. If it is printed, enteringit means that the real focus has entered the corresponding window. When this
occurs Application does not hava focused window, there is usually requestprinting. We can analyze it by whether enteringthere is printing.
1. enteringIf there is some printing, it means that the focus is already in the input. , but there is still ANR, you need to analyze it from the input and other aspects .
2. enteringPart of it is not printed, which means that the input is not triggered and the focus window is set to the input. You need to check SurfaceFlinger or WMS.

Guess you like

Origin blog.csdn.net/yimelancholy/article/details/130751737