R项目上面的出现了no focused window的ANR
09-09 17:49:58.482 1164 1347 W InputDispatcher: Waiting because no window has focus but ActivityRecord{94be3e0 u0 com.android.settings/.SubSettings t50} may eventually add a window when it finishes starting up. Will wait for 5000ms
...
09-09 17:49:58.482 1164 1347 W InputDispatcher: Still no focused window. Will drop the event in 4999ms
...
09-09 17:49:58.683 1747 1747 I wm_on_resume_called: [155968480,com.android.settings.SubSettings,RESUME_ACTIVITY]
09-09 17:49:58.710 1747 1747 I wm_on_top_resumed_gained_called: [155968480,com.android.settings.SubSettings,topWhenResuming]
09-09 17:49:59.324 1747 1747 I wm_on_stop_called: [246711063,com.transsion.settings.LocalPickerSubSetting,LIFECYCLER_STOP_ACTIVITY]
09-09 17:49:59.331 1747 1747 I wm_on_destroy_called: [246711063,com.transsion.settings.LocalPickerSubSetting,performDestroy]
....
09-09 17:50:03.261 1164 1347 W InputDispatcher: Still no focused window. Will drop the event in 221ms
09-09 17:50:03.488 1164 1347 I WindowManager: Input event dispatching timed out sending to application ActivityRecord{94be3e0 u0 com.android.settings/.SubSettings. Reason: ActivityRecord{94be3e0 u0 com.android.settings/.SubSettings t50} does not have a focused window
...
09-09 17:50:03.734 1164 9946 I am_anr : [0,1747,com.android.settings,952745541,Input dispatching timed out (ActivityRecord{94be3e0 u0 com.android.settings/.SubSettings t50} does not have a focused window)]
wm_add_to_stopping: [0,229779648,com.transsion.phonemaster/com.transsion.antivirus.view.activity.SecurityScanActivity,makeInvisible] update focus 置为null
分析流程:
frameworks/native/services/inputflinger/dispatcher/InputDispatcher.cpp
onAnrLocked{ does not have a focused window}
InputDispatcher::onAnrLocked --->
738 /**
739 * Check if any of the connections' wait queues have events that are too old.
740 * If we waited for events to be ack'ed for more than the window timeout, raise an ANR.
741 * Return the time at which we should wake up next.
742 */
InputDispatcher::processAnrsLocked--->
///
这句输出是由于
09-09 17:49:58.482 1164 1347 W InputDispatcher: Waiting because no window has focus but ActivityRecord{94be3e0 u0 com.android.settings/.SubSettings t50} may eventually add a window when it finishes starting up. Will wait for 5000ms
这句输出是由于focusedWindowHandle为空,而 focusedApplicationHandle不为空
1796 // Compatibility behavior: raise ANR if there is a focused application, but no focused window.
1797 // Only start counting when we have a focused event to dispatch. The ANR is canceled if we
1798 // start interacting with another application via touch (app switch). This code can be removed
1799 // if the "no focused window ANR" is moved to the policy. Input doesn't know whether
1800 // an app is expected to have a focused window.
1782 sp<InputWindowHandle> focusedWindowHandle =
1783 getValueByKey(mFocusedWindowHandlesByDisplay, displayId);
1784 sp<InputApplicationHandle> focusedApplicationHandle =
1785 getValueByKey(mFocusedApplicationHandlesByDisplay, displayId);
而这两个分别是从mFocusedWindowHandlesByDisplay和mFocusedApplicationHandlesByDisplay中根据displayId这个key值来获取的
InputDispatcher::findFocusedWindowTargetsLocked--->
mFocusedWindowHandlesByDisplay从setInputWindowsLocked的传入的 sp<InputWindowHandle>序列中获取。
在setInputWindowsLocked里根据传入的新的newFocusedWindowHandle在其序列中获取有焦点,可见的window。
与前一个oldFocusedWindowHandle比较,如果不相同则进一步判断,做赋值的切换替换旧窗口。
4056 /**
4057 * Called from InputManagerService, update window handle list by displayId that can receive input.
4058 * A window handle contains information about InputChannel, Touch Region, Types, Focused,...
4059 * If set an empty list, remove all handles from the specific display.
4060 * For focused handle, check if need to change and send a cancel event to previous one.
4061 * For removed handle, check if need to send a cancel event if already in touch.
4062 */
InputDispatcher::setInputWindowsLocked--->
InputDispatcher::setInputWindows -->
/frameworks/native/libs/input/IInputFlinger.cpp
在的BpInputFlinger将InputWindowInfo中的IInputFlinger::getInterfaceDescriptor()和inputInfo.size()封装进parcel再通过Binder发送到Bn端处理
44 remote()->transact(BnInputFlinger::SET_INPUT_WINDOWS_TRANSACTION, data, &reply,
45 IBinder::FLAG_ONEWAY);
/frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
当mVisibleRegionsDirty 和 mInputInfoChanged其中一个变量变为true时,则会刷新。
SurfaceFlinger::updateInputWindowInfo--->
SurfaceFlinger::updateInputFlinger-->
MessageQueue::INVALIDATE刷新时,调用updateInputFlinger,从layer中获取信息,通过setInputWindows传入InputFlinger中。
SurfaceFlinger::onMessageReceived {case: MessageQueue::INVALIDATE}-->
frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp
MessageQueue::Handler::handleMessage--->
发生ANR时,需要连续两次事件发送到同一个window中,而且是同样的条件,即focusedWindowHandle为null,而focusedApplicationHandle不为null,如果application退出,那么将不会继续发送事件到前一个application中,不会出现anr。如上分析,ANR发生时,SubSettings的application并没有stop,但是对应界面的SubSettings不再进行改变,而已一直都为null。
Input刷新流程.
SurfaceFlinger.run -->
surfaceflinger/Scheduler/MessageQueue.waitMessage -->
surfaceflinger/Scheduler/MessageQueue.cb_eventReceiver -->
surfaceflinger/Scheduler/MessageQueue.eventReceiver--->
//Input刷新
surfaceflinger/Scheduler/MessageQueue.dispatchInvalidate---> mQueue.mFlinger->onMessageReceived(message.what, mExpectedVSyncTime);
--->
SurfaceFlinger.onMessageReceived--->MessageQueue::INVALIDATE
SurfaceFlinger.onMessageInvalidate-->
SurfaceFlinger.updateInputFlinger-->
SurfaceFlinger.updateInputWindowInfo -->
IInputFlinger.setInputWindows{
remote()->transact(BnInputFlinger::SET_INPUT_WINDOWS_TRANSACTION, data,
&reply,IBinder::FLAG_ONEWAY);
}--->
InputDispatcher.setInputWindows-->
///相关堆栈
09-23 20:21:46.960 1150 2473 D InputDispatcher: #00 pc 0002e033 /system/lib/libinputflinger.so (android::inputdispatcher::InputDispatcher::setInputWindows(std::__1::unordered_map<int, std::__1::vector<android::sp<android::InputWindowHandle>, std::__1::allocator<android::sp<android::InputWindowHandle> > >, std::__1::hash<int>, std::__1::equal_to<int>, std::__1::allocator<std::__1::pair<int const, std::__1::vector<android::sp<android::InputWindowHandle>, std::__1::allocator<android::sp<android::InputWindowHandle> > > > > > const&)+70)
09-23 20:21:46.960 1150 2473 D InputDispatcher: #01 pc 00020d21 /system/lib/libinputflinger.so (android::InputManager::setInputWindows(std::__1::vector<android::InputWindowInfo, std::__1::allocator<android::InputWindowInfo> > const&, android::sp<android::ISetInputWindowsListener> const&)+288)
09-23 20:21:46.961 1150 2473 D InputDispatcher: #02 pc 0001d361 /system/lib/libinput.so (android::BnInputFlinger::onTransact(unsigned int, android::Parcel const&, android::Parcel*, unsigned int)+636)
09-23 20:21:46.961 1150 2473 D InputDispatcher: #03 pc 000343b9 /system/lib/libbinder.so (android::BBinder::transact(unsigned int, android::Parcel const&, android::Parcel*, unsigned int)+144)
09-23 20:21:46.961 1150 2473 D InputDispatcher: #04 pc 0003a459 /system/lib/libbinder.so (android::IPCThreadState::executeCommand(int)+624)
09-23 20:21:46.961 1150 2473 D InputDispatcher: #05 pc 0003a133 /system/lib/libbinder.so (android::IPCThreadState::getAndExecuteCommand()+98)
09-23 20:21:46.961 1150 2473 D InputDispatcher: #06 pc 0003a7b3 /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+38)
09-23 20:21:46.961 1150 2473 D InputDispatcher: #07 pc 00053de9 /system/lib/libbinder.so (android::PoolThread::threadLoop()+12)
09-23 20:21:46.961 1150 2473 D InputDispatcher: #08 pc 0000ee65 /system/lib/libutils.so (android::Thread::_threadLoop(void*)+168)
09-23 20:21:46.961 1150 2473 D InputDispatcher: #09 pc 0006cd4f /system/lib/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell(void*)+86)
09-23 20:21:46.961 1150 2473 D InputDispatcher: #10 pc 0000e9ad /system/lib/libutils.so (thread_data_t::trampoline(thread_data_t const*)+256)
09-23 20:21:46.961 1150 2473 D InputDispatcher: #11 pc 00080823 /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+40)
09-23 20:21:46.961 1150 2473 D InputDispatcher: #12 pc 00039d03 /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+30)
09-23 20:21:46.961 1150 2473 I InputDispatcher: setInputWindows displayId=0 Window{5e8ef54 u0 NavigationBar0} Window{f5cbd95 u0 StatusBar} Window{43e24d0 u0 com.android.settings/com.android.settings.Settings} Window{30a7619 u0 com.android.systemui.ImageWallpaper}
09-23 20:21:46.843 512 512 D IInputFlinger: #00 pc 0001d647 /system/lib/libinput.so (android::BpInputFlinger::setInputWindows(std::__1::vector<android::InputWindowInfo, std::__1::allocator<android::InputWindowInfo> > const&, android::sp<android::ISetInputWindowsListener> const&)+278)
09-23 20:21:46.843 512 512 D IInputFlinger: #01 pc 000bb7bb /system/lib/libsurfaceflinger.so (android::SurfaceFlinger::onMessageInvalidate(long long)+6982)
09-23 20:21:46.843 512 512 D IInputFlinger: #02 pc 000b9c23 /system/lib/libsurfaceflinger.so (android::SurfaceFlinger::onMessageReceived(int, long long)+110)
09-23 20:21:46.843 512 512 D IInputFlinger: #03 pc 00011d0d /system/lib/libutils.so (android::Looper::pollInner(int)+292)
09-23 20:21:46.843 512 512 D IInputFlinger: #04 pc 00011b93 /system/lib/libutils.so (android::Looper::pollOnce(int, int*, int*, void**)+62)
09-23 20:21:46.843 512 512 D IInputFlinger: #05 pc 000a6dc1 /system/lib/libsurfaceflinger.so (android::impl::MessageQueue::waitMessage()+56)
09-23 20:21:46.843 512 512 D IInputFlinger: #06 pc 000b4b07 /system/lib/libsurfaceflinger.so (android::SurfaceFlinger::run()+8)
09-23 20:21:46.843 512 512 D IInputFlinger: #07 pc 0000248b /system/bin/surfaceflinger (main+590)
09-23 20:21:46.843 512 512 D IInputFlinger: #08 pc 000330c1 /apex/com.android.runtime/lib/bionic/libc.so (__libc_init+68)
09-23 22:24:18.529 505 552 I SurfaceFlinger: [SF client] NEW(0xae4345d0) for (1152:system_server)
09-23 22:24:18.549 505 505 D MessageQueue: #00 pc 000a6e7d /system/lib/libsurfaceflinger.so (android::impl::MessageQueue::cb_eventReceiver(int, int, void*)+224)
09-23 22:24:18.549 505 505 D MessageQueue: #01 pc 00011ea1 /system/lib/libutils.so (android::Looper::pollInner(int)+696)
09-23 22:24:18.549 505 505 D MessageQueue: #02 pc 00011b93 /system/lib/libutils.so (android::Looper::pollOnce(int, int*, int*, void**)+62)
09-23 22:24:18.549 505 505 D MessageQueue: #03 pc 000a6f55 /system/lib/libsurfaceflinger.so (android::impl::MessageQueue::waitMessage()+56)
09-23 22:24:18.549 505 505 D MessageQueue: #04 pc 000b4c97 /system/lib/libsurfaceflinger.so (android::SurfaceFlinger::run()+8)
09-23 22:24:18.549 505 505 D MessageQueue: #05 pc 0000248b /system/bin/surfaceflinger (main+590)
09-23 22:24:18.549 505 505 D MessageQueue: #06 pc 000330c1 /apex/com.android.runtime/lib/bionic/libc.so (__libc_init+68)