Android应用程序键盘(Keyboard)消息处理机制分析(17)
Step 11.InputDispatcher.dispatchOnceInnerLocked 这个函数定义在frameworks/base/libs/ui/InputDispatcher.cpp文件中: voidInputDispatcher::dispatchOnceInnerLocked(nsecs_tkeyRepeatTimeout, nsecs_tkeyRepeatDelay,nsecs_t*nextWakeupTime){ ...... //Readytostartanewevent. //Ifwedon'talreadyhaveapendingevent,gograbone. if(!mPendingEvent){ if(mInboundQueue.isEmpty()){ ...... }else{ //Inboundqueuehasatleastoneentry. EventEntry*entry=mInboundQueue.headSentinel.next; ...... mInboundQueue.dequeue(entry); mPendingEvent=entry; } ...... } ...... switch(mPendingEvent->type){ ...... caseEventEntry::TYPE_KEY:{ KeyEntry*typedEntry=static_cast<KeyEntry*>(mPendingEvent); ...... done=dispatchKeyLocked(currentTime,typedEntry,keyRepeatTimeout, &dropReason,nextWakeupTime); break; } ...... } ...... } 我们忽略了这个函数的次要逻辑,主要关注键盘事件的主要处理流程。首先,如果前面发生的键盘事件都已经处理完毕,那么这里的mPendingEvent就为NULL,又因为前面我们把刚刚发生的键盘事件加入了mInboundQueue队列,因此,这里mInboundQueue不为NULL,于是,这里就把mInboundQueue队列中的键盘事件取出来,放在mPendingEvent变量中: mInboundQueue.dequeue(entry); mPendingEvent=entry; 由于这里发生的是键盘事件,即mPendingEvent->type的值为EventEntry::TYPE_KEY,于是,在接下来的switch语句中就会执行dispatchKeyLocked函数来分发键盘消息。 Step 12.InputDispatcher.dispatchKeyLocked 这个函数定义在frameworks/base/libs/ui/InputDispatcher.cpp文件中: boolInputDispatcher::dispatchKeyLocked( nsecs_tcurrentTime,KeyEntry*entry,nsecs_tkeyRepeatTimeout, DropReason*dropReason,nsecs_t*nextWakeupTime){ ...... //Identifytargets. if(!mCurrentInputTargetsValid){ int32_tinjectionResult=findFocusedWindowTargetsLocked(currentTime, entry,nextWakeupTime); ...... } //Dispatchthekey. dispatchEventToCurrentInputTargetsLocked(currentTime,entry,false); returntrue; } InputDispatcher类中的mCurrentInputTargetsValid成员变量表示InputDispatcher是否已经标志出谁是当前激活的Activity窗口,如果没有,就需要通过findFocusedWindowTargetsLocked函数来把它找出来。当把当前激活的Activity窗口找出来以后,接下来就调用dispatchEventToCurrentInputTargetsLocked函数把键盘事件分发给它了。 我们先来看一InputDispatcher是如何找到当前激活的Activity窗口的,然后再分析它把键盘事件分发给当前激活Activity窗口的过程。 Step 13.InputDispatcher.findFocusedWindowTargetsLocked 这个函数定义在frameworks/base/libs/ui/InputDispatcher.cpp文件中: int32_tInputDispatcher::findFocusedWindowTargetsLocked(nsecs_tcurrentTime, constEventEntry*entry,nsecs_t*nextWakeupTime){ mCurrentInputTargets.clear(); int32_tinjectionResult; //Ifthereisnocurrentlyfocusedwindowandnofocusedapplication //thendroptheevent. if(!mFocusedWindow){ if(mFocusedApplication){ ...... injectionResult=handleTargetsNotReadyLocked(currentTime,entry, mFocusedApplication,NULL,nextWakeupTime); gotoUnresponsive; } ...... injectionResult=INPUT_EVENT_INJECTION_FAILED; gotoFailed; } //Checkpermissions. if(!checkInjectionPermission(mFocusedWindow,entry->injectionState)){ injectionResult=INPUT_EVENT_INJECTION_PERMISSION_DENIED; gotoFailed; } //Ifthecurrentlyfocusedwindowispausedthenkeepwaiting. if(mFocusedWindow->paused){ ...... injectionResult=handleTargetsNotReadyLocked(currentTime,entry, mFocusedApplication,mFocusedWindow,nextWakeupTime); gotoUnresponsive; } //Ifthecurrentlyfocusedwindowisstillworkingonpreviouseventsthenkeepwaiting. if(!isWindowFinishedWithPreviousInputLocked(mFocusedWindow)){ ...... injectionResult=handleTargetsNotReadyLocked(currentTime,entry, mFocusedApplication,mFocusedWindow,nextWakeupTime); gotoUnresponsive; } //Success!Outputtargets. injectionResult=INPUT_EVENT_INJECTION_SUCCEEDED; addWindowTargetLocked(mFocusedWindow,InputTarget::FLAG_FOREGROUND,BitSet32(0)); ...... returninjectionResult; } 回忆前面我们分析应用程序注册键盘消息接收通道的过程时,在Step 9中,当前处于激活状态的应用程序会通过调用InputDispatcher类setInputWindows函数把把当前获得焦点的Activity窗口设置到mFocusedWindow中去,因此,这里的mFocusedWindow不为NULL,于是,就通过了第一个if语句的检查。 第二个if语句检查权限问题,原来,这个键盘事件除了是由硬件触发的外,也可以由其它进程注入进来的,如果这个键盘事件是由其它进程注入进来的,那么entry->injectState就不为NULL,它里面包含了事件注册者的进程ID和用户ID,于是,这里就会调用checkInjectionPermission来检查这个事件注入者的进程ID和用户ID,看看它是否具有这个权限。这里我们不考虑这种情况,因此,这里的entry->injectState为NULL,于是,这个if语句的检查也通过了。 第三个if语句检查当前激活的Activity窗口是否是处于paused状态,如果是的话,也不用进一步处理了。一般情况下,当前激活的Activity窗口都是处于resumed状态的,于是,这个if语句的检查也通过了。 第四个if语句检查当前激活的Activity窗口是否还正在处理前一个键盘事件,如果是的话,那就要等待它处理完前一个键盘事件后再来处理新的键盘事件了。这里我们也假设当前激活的Activity窗口不是正在处理前面的键盘事件,因此,这个if语句的检查也通过了。 最后,就调用addWindowTargetLocked函数把当前激活的Activity窗口添加到InputDispatcher类的mCurrentInputTargets成员变量中去。 Step 14.InputDispatcher.addWindowTargetLocked 这个函数定义在frameworks/base/libs/ui/InputDispatcher.cpp文件中: voidInputDispatcher::addWindowTargetLocked(constInputWindow*window,int32_ttargetFlags, BitSet32pointerIds){ mCurrentInputTargets.push(); InputTarget&target=mCurrentInputTargets.editTop(); target.inputChannel=window->inputChannel; target.flags=targetFlags; target.xOffset=-window->frameLeft; target.yOffset=-window->frameTop; target.pointerIds=pointerIds; } 这个函数简单,就是把传进来的参数window添加到mCurrentInputTargets中去就完事了,后面InputDispatcher就会从mCurrentInputTargets中取出恰当的Activity窗口,然后把键盘事件分发给它。 回到Step 12中的dispatchKeyLocked函数,它接下来就调用dispatchEventToCurrentInputTargetsLocked来进一步处理了。 Step 15.InputDispatcher.dispatchEventToCurrentInputTargetsLocked 这个函数定义在frameworks/base/libs/ui/InputDispatcher.cpp文件中: voidInputDispatcher::dispatchEventToCurrentInputTargetsLocked(nsecs_tcurrentTime, EventEntry*eventEntry,boolresumeWithAppendedMotionSample){ ...... for(size_ti=0;i<mCurrentInputTargets.size();i++){ constInputTarget&inputTarget=mCurrentInputTargets.itemAt(i); ssize_tconnectionIndex=getConnectionIndexLocked(inputTarget.inputChannel); if(connectionIndex>=0){ sp<Connection>connection=mConnectionsByReceiveFd.valueAt(connectionIndex); prepareDispatchCycleLocked(currentTime,connection,eventEntry,&inputTarget, resumeWithAppendedMotionSample); }else{ ...... } } 这个函数的实现也比较简单,前面我们已经把当前需要接受键盘事件的Activity窗口添加到mCurrentInputTargets中去了,因此,这里就分别把它们取出来,然后调用prepareDispatchCycleLocked函数把键盘事件分发给它们处理。 前面我们在分析应用程序注册键盘消息接收通道的过程时,在Step 18中(InputDispatcher.registerInputChannel),把Server端的InputChannel封装成了一个Connection,然后以这个InputChannel中的Receive Pipe Fd作为键值把这个Connection对象保存在mConnectionsByReceiveFd中。这里,既然我们已经通过mCurrentInputTargets得到了表示当前需要接收键盘事件的Activity窗口的InputTarget对象,而且这个InputTarget对象的inputChannel就表示当初在InputDispatcher中注册的Server端InputChannel,因此,这里就可以把这个Connection对象取出来,最后调用prepareDispatchCycleLocked函数来进一步处理。 本文转自 Luoshengyang 51CTO博客,原文链接:http://blog.51cto.com/shyluo/966635,如需转载请自行联系原作者