Step 16.InputDispatcher.prepareDispatchCycleLocked

这个函数定义在frameworks/base/libs/ui/InputDispatcher.cpp文件中:

  1. voidInputDispatcher::prepareDispatchCycleLocked(nsecs_tcurrentTime,
  2. constsp<Connection>&connection,EventEntry*eventEntry,constInputTarget*inputTarget,
  3. boolresumeWithAppendedMotionSample){
  4. ......
  5. //Resumethedispatchcyclewithafreshlyappendedmotionsample.
  6. //Firstwecheckthatthelastdispatchentryintheoutboundqueueisforthesame
  7. //motioneventtowhichweappendedthemotionsample.Ifwefindsuchadispatch
  8. //entry,andifitiscurrentlyinprogressthenwetrytostreamthenewsample.
  9. boolwasEmpty=connection->outboundQueue.isEmpty();
  10. if(!wasEmpty&&resumeWithAppendedMotionSample){
  11. ......
  12. return;
  13. }
  14. //Thisisanewevent.
  15. //Enqueueanewdispatchentryontotheoutboundqueueforthisconnection.
  16. DispatchEntry*dispatchEntry=mAllocator.obtainDispatchEntry(eventEntry,//incrementsref
  17. inputTarget->flags,inputTarget->xOffset,inputTarget->yOffset);
  18. ......
  19. //Enqueuethedispatchentry.
  20. connection->outboundQueue.enqueueAtTail(dispatchEntry);
  21. //Iftheoutboundqueuewaspreviouslyempty,startthedispatchcyclegoing.
  22. if(wasEmpty){
  23. ......
  24. startDispatchCycleLocked(currentTime,connection);
  25. }
  26. }

在开始处理键盘事件之前,这个函数会检查一下传进来的参数connection中的outboundQueue事件队列是否为空,如果不为空,就要看看当前要处理的事件和outboundQueue队列中的最后一个事件是不是同一个motion事件,如果是的话,并且从上面传进来的resumeWithAppendedMotionSample参数为true,这时候就要以流水线的方式来处理这些motion事件了。在我们这个情景中,要处理的是键盘事件,因此在上面Step 12中传进来的resumeWithAppendedMotionSample参数为false,因此,我们略过这种情况。

接下来,就会把当前的键盘事件封装成一个DispatchEntry对象,然后添加到connection对象的outboundQueue队列中去,表示当前键盘事件是一个待处理的键盘事件。

当connection中的outboundQueue事件队列不为空,即wasEmpty为false时,说明当前这个Activity窗口正在处键盘事件了,因此,就不需要调用startDispatchCycleLocked来启动Activity窗口来处理这个事件了,因为一旦这个Activity窗口正在处键盘事件,它就会一直处理下去,直到它里的connection对象的outboundQueue为空为止。当connection中的outboundQueue事件队列为空时,就需要调用startDispatchCycleLocked来通知这个Activity窗口来执行键盘事件处理的流程了。

Step 17.InputDispatcher.startDispatchCycleLocked

这个函数定义在frameworks/base/libs/ui/InputDispatcher.cpp文件中:

  1. voidInputDispatcher::startDispatchCycleLocked(nsecs_tcurrentTime,
  2. constsp<Connection>&connection){
  3. ......
  4. DispatchEntry*dispatchEntry=connection->outboundQueue.headSentinel.next;
  5. //Markthedispatchentryasinprogress.
  6. dispatchEntry->inProgress=true;
  7. //Updatetheconnection'sinputstate.
  8. EventEntry*eventEntry=dispatchEntry->eventEntry;
  9. ......
  10. //Publishtheevent.
  11. status_tstatus;
  12. switch(eventEntry->type){
  13. caseEventEntry::TYPE_KEY:{
  14. KeyEntry*keyEntry=static_cast<KeyEntry*>(eventEntry);
  15. //Applytargetflags.
  16. int32_taction=keyEntry->action;
  17. int32_tflags=keyEntry->flags;
  18. //Publishthekeyevent.
  19. status=connection->inputPublisher.publishKeyEvent(keyEntry->deviceId,keyEntry->source,
  20. action,flags,keyEntry->keyCode,keyEntry->scanCode,
  21. keyEntry->metaState,keyEntry->repeatCount,keyEntry->downTime,
  22. keyEntry->eventTime);
  23. ......
  24. break;
  25. }
  26. ......
  27. }
  28. //Sendthedispatchsignal.
  29. status=connection->inputPublisher.sendDispatchSignal();
  30. ......
  31. }

这个函数主要围绕传进来的Connection对象做两件事情,一是从它的outboundQueue队列中取出当前需要处理的键盘事件,然后把这个事件记录在它的内部对象inputPublisher中,二是通过它的内部对象inputPublisher通知它所关联的Activity窗口,现在有键盘事件需要处理了。第一件事情是通过调用它的InputPublisher对象的publishKeyEvent函数来完成的,而第二件事情是通过调用它的InputPublisher对象的sendDispatchSignal来完成的。我们先来看InputPublisher的成员函数publishKeyEvent的实现,然后再回来分析它的另外一个成员函数sendDispatchSignal的实现。

Step 18. InputPublisher.publishKeyEvent

这个函数定义在frameworks/base/libs/ui/InputTransport.cpp文件中:

  1. status_tInputPublisher::publishKeyEvent(
  2. int32_tdeviceId,
  3. int32_tsource,
  4. int32_taction,
  5. int32_tflags,
  6. int32_tkeyCode,
  7. int32_tscanCode,
  8. int32_tmetaState,
  9. int32_trepeatCount,
  10. nsecs_tdownTime,
  11. nsecs_teventTime){
  12. ......
  13. status_tresult=publishInputEvent(AINPUT_EVENT_TYPE_KEY,deviceId,source);
  14. if(result<0){
  15. returnresult;
  16. }
  17. mSharedMessage->key.action=action;
  18. mSharedMessage->key.flags=flags;
  19. mSharedMessage->key.keyCode=keyCode;
  20. mSharedMessage->key.scanCode=scanCode;
  21. mSharedMessage->key.metaState=metaState;
  22. mSharedMessage->key.repeatCount=repeatCount;
  23. mSharedMessage->key.downTime=downTime;
  24. mSharedMessage->key.eventTime=eventTime;
  25. returnOK;
  26. }

这个函数主要就是把键盘事件记录在InputPublisher类的成员变量mSharedMessage中了,这个mSharedMessage成员变量指向的是一个匿名共享内存。

这个匿名共享内存是什么时候创建的呢?前面我们在分析应用程序注册键盘消息接收通道的过程时,在Step 18中(InputDispatcher.registerInputChannel),在把Server端的InputChannel封装成一个 Connection对象时,会调用它的initialize成员函数来执行一些初始化工作,就是在这个时候创建这个匿名共享内存的了:

  1. sp<Connection>connection=newConnection(inputChannel);
  2. status_tstatus=connection->initialize();

我们来看一下这个initialize函数的实现,它定义在frameworks/base/libs/ui/InputTransport.cpp文件中:

  1. status_tInputPublisher::initialize(){
  2. ......
  3. intashmemFd=mChannel->getAshmemFd();
  4. intresult=ashmem_get_size_region(ashmemFd);
  5. ......
  6. mAshmemSize=(size_t)result;
  7. mSharedMessage=static_cast<InputMessage*>(mmap(NULL,mAshmemSize,
  8. PROT_READ|PROT_WRITE,MAP_SHARED,ashmemFd,0));
  9. ......
  10. mPinned=true;
  11. mSharedMessage->consumed=false;
  12. returnreset();
  13. }

InputPublisher的成员变量mChannel就是指注册在InputDispatcher中的Server端InputChannel了。我们知道,这个InputChannel除了拥有一个反向管道的读端文件描述符和一个前向管道的写端文件描述符之后,还有一个匿名共享文件描述符,这个匿名共享文件描述符就是用来创建匿名共享内存mSharedMessage的了。

这个匿名共享内存mSharedMessage的作用是什么呢?原来,在InputChannel中,前向管道和反向管道的作用只是用来在Server端和Client端之间相互通知有事件发生了,但是具体是什么样的事件,还需要去读取这个匿名共享内存的内容才知道。前面我们在分析应用程序注册键盘消息接收通道的过程时,在Step 14中(InputChannel.openInputChannelPair)创建Server端和Client端的InputChannel对时,创建一个匿名共享内存,这个匿名共享内存有两个文件描述符同时指向它,其中一个放在Server端的InputChannel中,另外一个放在Client端的InputChannel中。这样,当InputDispatcher通过Server端的InputChannel的前向管道来通知Client端有键盘事件发生时,Client端只要通过它的InputChannel中的匿名共享内存文件描述符去读取匿名共享内存中的内容,就可以知道发生了什么事情了。

回到Step 17中,接下来就是调用InputPublisher的成员函数sendDispatchSignal来通知Activity窗口处理键盘事件了。

Step 19.InputPublishe.sendDispatchSignal
这个函数定义在frameworks/base/libs/ui/InputTransport.cpp文件中:

  1. status_tInputPublisher::sendDispatchSignal(){
  2. ......
  3. mWasDispatched=true;
  4. returnmChannel->sendSignal(INPUT_SIGNAL_DISPATCH);
  5. }

这个函数很简单,它通过调用内部成员变量mChannel的sendSignal函数来通知相应的Activity窗口来处理键盘事件。

Step 20. InputChannel.sendSignal

这个函数定义在frameworks/base/libs/ui/InputTransport.cpp文件中:

  1. status_tInputChannel::sendSignal(charsignal){
  2. ssize_tnWrite;
  3. do{
  4. nWrite=::write(mSendPipeFd,&signal,1);
  5. }while(nWrite==-1&&errno==EINTR);
  6. if(nWrite==1){
  7. ......
  8. returnOK;
  9. }
  10. return-errno;
  11. }

这里所谓的发送信号通知,其实是通过向其内部一个管道的写端写入一个字符来实现的。前面我们分析应用程序注册键盘消息接收通道的过程时,在Step 21中(NativeInputQueue.registerInputChannel),它把一个InputChannel注册到应用程序主线程中的Looper对象中,然后应用程序的主线程就通过这个Looper对象睡眠等待在这个InputChannel中的前向管道中有新的内容可读了,这里的mSendPipeFd就是对应这个前向管道的写端。现在既然向这个前向管道的写端写入新的内容了,于是,应用程序的主线程就被唤醒了。

在前面分析应用程序注册键盘消息接收通道过程的Step 21中,我们也说过,当应用程序的主线程因为这个InputChannel中的前向管道的写端唤醒时,NativeInputQueue的成员函数handleReceiveCallback就会被回调,因此,接下来,应用程序的主线程就会被唤醒,然后执行NativeInputQueue的成员函数handleReceiveCallback。

更多相关文章

  1. Android事件传递流程
  2. android隐藏全面屏虚拟键盘实现
  3. android 返回键 事件
  4. Android应用程序键盘(Keyboard)消息处理机制分析(11)
  5. Android下的Touch事件分发
  6. 在android4.2中添加自己的intent事件并使用
  7. android 设置键盘按钮为发送按钮并监听 及 键盘显示与隐藏 监听
  8. android Spinner点击事件处理
  9. android 把虚拟键盘的回车改为搜索功能(亲试)

随机推荐

  1. Android获取通话记录
  2. Android中的Selector的使用
  3. android KEYCODE_BACK监听处理
  4. How to install Android SDK in Ubuntu S
  5. android textView描边
  6. How to Cross Compile the Bash shell fo
  7. 在android中创建包含对象数组对象List 的
  8. Android system大小上限设置
  9. Android的SensorEventListener例子(摇晃手
  10. Android(Java):md5加密