// ......
// mPendingEvent默认为空,会从mInboundQueue中获取到值
if (!mPendingEvent) {
// mInboundQueue是通过enqueueInboundEventLocked()赋值,所以有事件的时候mInboundQueue不为空        // notifyConfigurationChanged() -> enqueueInboundEventLocked()
// notifyKey() -> enqueueInboundEventLocked()
// notifyMotion() -> enqueueInboundEventLocked()
// notifyDeviceReset() -> enqueueInboundEventLocked()
// injectInputEvent() -> enqueueInboundEventLocked()
// setInputWindowsLocked() -> enqueueFocusEventLocked()
if (pty()) {
// ......
// 如果mInboundQueue也没有pending的事件,直接返回
if (!mPendingEvent) {
return;
}
} else {
// ⼀般⾛这个流程,取mInboundQueue第⼀个元素
mPendingEvent = mInboundQueue.front();
mInboundQueue.pop_front();
traceInboundQueueLengthLocked();
}
// ......
}
// ⾛到这个地⽅,mPendingEvent⼀般不会为空
ALOG_ASSERT(mPendingEvent != nullptr);
bool done = false;
DropReason dropReason = DropReason::NOT_DROPPED;
// ......
notify for mi band// 根据pendingEvent的类型创建不同的实例去分发事件
// 不同的⼊⼝的类型不⼀样,如调⽤了notifyKey()就会创建⼀个KeyEntry
switch (mPendingEvent->type) {
// 来源:notifyConfigurationChanged() -> new ConfigurationChangedEntry()
case EventEntry::Type::CONFIGURATION_CHANGED: {
ConfigurationChangedEntry* typedEntry =
static_cast<ConfigurationChangedEntry*>(mPendingEvent);
done = dispatchConfigurationChangedLocked(currentTime, typedEntry);
dropReason = DropReason::NOT_DROPPED; // configuration changes are never dropped
break;
}
// 来源:notifyDeviceReset() -> new DeviceResetEntry()
case EventEntry::Type::DEVICE_RESET: {
DeviceResetEntry* typedEntry = static_cast<DeviceResetEntry*>(mPendingEvent);
done = dispatchDeviceResetLocked(currentTime, typedEntry);
dropReason = DropReason::NOT_DROPPED; // device resets are never dropped
break;
}
// 来源:setInputWindowsLocked() -> enqueueFocusEventLocked() -> new FocusEntry()
case EventEntry::Type::FOCUS: {
FocusEntry* typedEntry = static_cast<FocusEntry*>(mPendingEvent);
dispatchFocusLocked(currentTime, typedEntry);
done = true;
dropReason = DropReason::NOT_DROPPED; // focus events are never dropped
break;
}
// 来源:notifyKey() -> new KeyEntry()
case EventEntry::Type::KEY: {
KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent);
// ......
// 跳转到findFocusedWindowTargetsLocked() -> dispatchEventLocked()
done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime);
break;
}
// 来源:notifyMotion() -> new MotionEntry()
case EventEntry::Type::MOTION: {
MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent);
// ......
/
/ 跳转到findFocusedWindowTargetsLocked() -> dispatchEventLocked()
done = dispatchMotionLocked(currentTime, typedEntry, &dropReason, nextWakeupTime);
break;
}
}
前⾯看到,是否触发ANR跟focusedWindowHandle和focusedApplicationHandle有关,那么这两个值的赋值流程是在哪⾥呢?
跟踪代码流程,可以看到,这两个流程都是WindowManagerService在窗⼝切换的过程中,设置到InputDispatcher中的,最后调
⽤setInputWindowsLocked()设置focusedWindowHandle,调⽤setFocusedApplication()设置focusedApplicationHandle,相关流程如下,关键步骤有注释。
setInputWindow.PNG
// frameworks/native/services/inputflinger/dispatcher/InputDispatcher.cpp
// 当VSYNC信号来了之后,会调⽤到SurfaceFlinger的onMessageInvalidate()⽅法
// SurfaceFlinger::onMessageInvalidate()
//  ==> SurfaceFlinger: updateInputFlinger()
//    ==> SurfaceFlinger: updateInputWindowInfo()
//      ==> InputManager::setInputWindows()
//        ==> InputDispatcher::setInputWindows()
//          ==> InputDispatcher::setInputWindowsLocked()
void InputDispatcher::setInputWindowsLocked(
const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) {
/
/ ......
const std::vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId);
// 更新mWindowHandlesByDisplay这个map,然后通过getWindowHandlesLocked()newFocusedWindowHandle
updateWindowHandlesForDisplayLocked(inputWindowHandles, displayId);
sp<InputWindowHandle> newFocusedWindowHandle = nullptr;
bool foundHoveredWindow = false;
// 在mWindowHandlesByDisplay这个map⾥⾯newFocusedWindowHandle
for (const sp<InputWindowHandle>& windowHandle : getWindowHandlesLocked(displayId)) {
// newFocusedWindowHandle要不为空,windowHandle具备focusable和visible属性
if (!newFocusedWindowHandle && windowHandle->getInfo()->hasFocus &&
windowHandle->getInfo()->visible) {
// 给newFocusedWindowHandle赋值,最后这个值存到mFocusedWindowHandlesByDisplay这个map
newFocusedWindowHandle = windowHandle;
}
if (windowHandle == mLastHoverWindowHandle) {
foundHoveredWindow = true;
}
}
if (!foundHoveredWindow) {
mLastHoverWindowHandle = nullptr;
}
/
/ 在mFocusedWindowHandlesByDisplay这个map⾥当前的焦点窗⼝
sp<InputWindowHandle> oldFocusedWindowHandle =
getValueByKey(mFocusedWindowHandlesByDisplay, displayId);
// 判断oldFocusedWindowHandle是否等于newFocusedWindowHandle,如果相等则不⾛focus change流程
if (!haveSameToken(oldFocusedWindowHandle, newFocusedWindowHandle)) {
// 如果当前的焦点窗⼝不为空,需要从mFocusedWindowHandlesByDisplay移除掉
if (oldFocusedWindowHandle != nullptr) {
为空

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。