在Android系统中,键盘按键事件是由WindowManagerService服务来管理的,然后再以消息的形式来分发给应用程序处理,不过和普通消息不一样,它是由硬件中断触发的;在上一篇文章《Android应用程序消息处理机制(Looper、Handler)分析》中,我们分析了Android应用程序的消息处理机制,本文将结合这种消息处理机制来详细分析Android应用程序是如何获得键盘按键消息的。

在系统启动的时候,SystemServer会启动窗口管理服务WindowManagerService,WindowManagerService在启动的时候就会通过系统输入管理器InputManager来总负责监控键盘消息。这些键盘消息一般都是分发给当前激活的Activity窗口来处理的,因此,当前激活的Activity窗口在创建的时候,会到WindowManagerService中去注册一个接收键盘消息的通道,表明它要处理键盘消息,而当InputManager监控到有键盘消息时,就会分给给它处理。当当前激活的Activity窗口不再处于激活状态时,它也会到WindowManagerService中去反注册之前的键盘消息接收通道,这样,InputManager就不会再把键盘消息分发给它来处理。

由于本文的内容比较多,在接下面的章节中,我们将分为五个部分来详细描述Android应用程序获得键盘按键消息的过程,每一个部分都是具体描述键盘消息处理过程中的一个过程。结合上面的键盘消息处理框架,这四个过程分别是InputManager的启动过程、应用程序注册键盘消息接收通道的过程、InputManager分发键盘消息给应用程序的过程以及应用程序注销键盘消息接收通道的过程。为了更好地理解Android应用程序获得键盘按键消息的整个过程,建议读者首先阅读Android应用程序消息处理机制(Looper、Handler)分析一文,理解了Android应用程序的消息处理机制后,就能很好的把握本文的内容。

1. InputManager的启动过程分析

前面说过,Android系统的键盘事件是由InputManager来监控的,而InputManager是由窗口管理服务WindowManagerService来启动的。

从前面一篇文章Android系统进程Zygote启动过程的源代码分析中,我们知道在Android系统中,Zygote进程负责启动系统服务进程SystemServer,而系统服务进程SystemServer负责启动系统中的各种关键服务,例如我们在前面两篇文章Android应用程序安装过程源代码分析和Android系统默认Home应用程序(Launcher)的启动过程源代码分析中提到的Package管理服务PackageManagerService和Activity管理服务ActivityManagerService。这里我们所讨论的窗口管理服务WindowManagerService也是由SystemServer来启动的,具体的启动过程这里就不再详述了,具体可以参考PackageManagerService和ActivityManagerService的启动过程。

了解了WindowManagerService的启动过程之后,我们就可以继续分析InputManager的启动过程了。我们先来看一下InputManager启动过程的序列图,然后根据这个序列图来一步步分析它的启动过程:

Step 1. WindowManagerService.main

这个函数定义在frameworks/base/services/java/com/android/server/WindowManagerService.java文件中:

[java] view plain copy
  1. publicclassWindowManagerServiceextendsIWindowManager.Stub
  2. implementsWatchdog.Monitor{
  3. ......
  4. publicstaticWindowManagerServicemain(Contextcontext,
  5. PowerManagerServicepm,booleanhaveInputMethods){
  6. WMThreadthr=newWMThread(context,pm,haveInputMethods);
  7. thr.start();
  8. synchronized(thr){
  9. while(thr.mService==null){
  10. try{
  11. thr.wait();
  12. }catch(InterruptedExceptione){
  13. }
  14. }
  15. returnthr.mService;
  16. }
  17. }
  18. ......
  19. }
它通过一个线程WMThread实例来执行全局唯一的WindowManagerService实例的启动操作。这里调用WMThread实例thr的start成员函数时,会进入到WMThread实例thr的run函数中去。

Step 2. WMThread.run

这个函数定义在frameworks/base/services/java/com/android/server/WindowManagerService.java文件中:

[java] view plain copy
  1. publicclassWindowManagerServiceextendsIWindowManager.Stub
  2. implementsWatchdog.Monitor{
  3. ......
  4. staticclassWMThreadextendsThread{
  5. ......
  6. publicvoidrun(){
  7. ......
  8. WindowManagerServices=newWindowManagerService(mContext,mPM,
  9. mHaveInputMethods);
  10. ......
  11. }
  12. }
  13. ......
  14. }
这里执行的主要操作就是创建一个WindowManagerService实例,这样会调用到WindowManagerService构造函数中去。

Step 3. WindowManagerService<init>

WindowManagerService类的构造函数定义在frameworks/base/services/java/com/android/server/WindowManagerService.java文件中:

[java] view plain copy
  1. publicclassWindowManagerServiceextendsIWindowManager.Stub
  2. implementsWatchdog.Monitor{
  3. ......
  4. finalInputManagermInputManager;
  5. ......
  6. privateWindowManagerService(Contextcontext,PowerManagerServicepm,
  7. booleanhaveInputMethods){
  8. ......
  9. mInputManager=newInputManager(context,this);
  10. ......
  11. mInputManager.start();
  12. ......
  13. }
  14. ......
  15. }
这里我们只关心InputManager的创建过程,而忽略其它无关部分。首先是创建一个InputManager实例,然后再调用它的start成员函数来监控键盘事件。在创建InputManager实例的过程中,会执行一些初始化工作,因此,我们先进入到InputManager类的构造函数去看看,然后再回过头来分析它的start成员函数。

Step 4. InputManager<init>@java

Java层的InputManager类的构造函数定义在frameworks/base/services/java/com/android/server/InputManager.java文件中:

[java] view plain copy
  1. publicclassInputManager{
  2. ......
  3. publicInputManager(Contextcontext,WindowManagerServicewindowManagerService){
  4. this.mContext=context;
  5. this.mWindowManagerService=windowManagerService;
  6. this.mCallbacks=newCallbacks();
  7. init();
  8. }
  9. ......
  10. }
这里只是简单地初始化InputManager类的一些成员变量,然后调用init函数进一步执行初始化操作。

Step 5. InputManager.init

这个函数定义在frameworks/base/services/java/com/android/server/InputManager.java文件中:

[java] view plain copy
  1. publicclassInputManager{
  2. ......
  3. privatevoidinit(){
  4. Slog.i(TAG,"Initializinginputmanager");
  5. nativeInit(mCallbacks);
  6. }
  7. ......
  8. }
函数init通过调用本地方法nativeInit来执行C++层的相关初始化操作。

Step 6. InputManager.nativeInit

这个函数定义在frameworks/base/services/jni$ vi com_android_server_InputManager.cpp文件中:

[cpp] view plain copy
  1. staticvoidandroid_server_InputManager_nativeInit(JNIEnv*env,jclassclazz,
  2. jobjectcallbacks){
  3. if(gNativeInputManager==NULL){
  4. gNativeInputManager=newNativeInputManager(callbacks);
  5. }else{
  6. LOGE("Inputmanageralreadyinitialized.");
  7. jniThrowRuntimeException(env,"Inputmanageralreadyinitialized.");
  8. }
  9. }
这个函数的作用是创建一个NativeInputManager实例,并保存在gNativeInputManager变量中。由于是第一次调用到这里,因此,gNativeInputManager为NULL,于是就会new一个NativeInputManager对象出来,这样就会执行NativeInputManager类的构造函数来执其它的初始化操作。

Step 7. NativeInputManager<init>

NativeInputManager类的构造函数定义在frameworks/base/services/jni$ vi com_android_server_InputManager.cpp文件中:

[cpp] view plain copy
  1. NativeInputManager::NativeInputManager(jobjectcallbacksObj):
  2. mFilterTouchEvents(-1),mFilterJumpyTouchEvents(-1),mVirtualKeyQuietTime(-1),
  3. mMaxEventsPerSecond(-1),
  4. mDisplayWidth(-1),mDisplayHeight(-1),mDisplayOrientation(ROTATION_0){
  5. JNIEnv*env=jniEnv();
  6. mCallbacksObj=env->NewGlobalRef(callbacksObj);
  7. sp<EventHub>eventHub=newEventHub();
  8. mInputManager=newInputManager(eventHub,this,this);
  9. }
这里只要是创建了一个EventHub实例,并且把这个EventHub作为参数来创建InputManager对象。注意,这里的InputManager类是定义在C++层的,和前面在Java层的InputManager不一样,不过它们是对应关系。EventHub类是真正执行监控键盘事件操作的地方,后面我们会进一步分析到,现在我们主要关心InputManager实例的创建过程,它会InputManager类的构造函数里面执行一些初始化操作。

Step 8. InputManager<init>@C++

C++层的InputManager类的构造函数定义在frameworks/base/libs/ui/InputManager.cpp文件中:

[cpp] view plain copy
  1. InputManager::InputManager(
  2. constsp<EventHubInterface>&eventHub,
  3. constsp<InputReaderPolicyInterface>&readerPolicy,
  4. constsp<InputDispatcherPolicyInterface>&dispatcherPolicy){
  5. mDispatcher=newInputDispatcher(dispatcherPolicy);
  6. mReader=newInputReader(eventHub,readerPolicy,mDispatcher);
  7. initialize();
  8. }
这里主要是创建了一个InputDispatcher对象和一个InputReader对象,并且分别保存在成员变量mDispatcher和mReader中。InputDispatcher类是负责把键盘消息分发给当前激活的Activity窗口的,而InputReader类则是通过EventHub类来实现读取键盘事件的,后面我们会进一步分析。创建了这两个对象后,还要调用initialize函数来执行其它的初始化操作。

Step 9. InputManager.initialize

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

[cpp] view plain copy
  1. voidInputManager::initialize(){
  2. mReaderThread=newInputReaderThread(mReader);
  3. mDispatcherThread=newInputDispatcherThread(mDispatcher);
  4. }
这个函数创建了一个InputReaderThread线程实例和一个InputDispatcherThread线程实例,并且分别保存在成员变量mReaderThread和mDispatcherThread中。这里的InputReader实列mReader就是通过这里的InputReaderThread线程实列mReaderThread来读取键盘事件的,而InputDispatcher实例mDispatcher则是通过这里的InputDispatcherThread线程实例mDisptacherThread来分发键盘消息的。

至此,InputManager的初始化工作就完成了,在回到Step 3中继续分析InputManager的进一步启动过程之前,我们先来作一个小结,看看这个初始化过程都做什么事情:

A. 在Java层中的WindowManagerService中创建了一个InputManager对象,由它来负责管理Android应用程序框架层的键盘消息处理;

B. 在C++层也相应地创建一个InputManager本地对象来负责监控键盘事件;

C. 在C++层中的InputManager对象中,分别创建了一个InputReader对象和一个InputDispatcher对象,前者负责读取系统中的键盘消息,后者负责把键盘消息分发出去;

D.InputReader对象和一个InputDispatcher对象分别是通过InputReaderThread线程实例和InputDispatcherThread线程实例来实键盘消息的读取和分发的。

有了这些对象之后,万事就俱备了,回到Step 3中,调用InputManager类的start函数来执行真正的启动操作。

Step 10. InputManager.start

这个函数定义在frameworks/base/services/java/com/android/server/InputManager.java文件中:

[cpp] view plain copy
  1. publicclassInputManager{
  2. ......
  3. publicvoidstart(){
  4. Slog.i(TAG,"Startinginputmanager");
  5. nativeStart();
  6. }
  7. ......
  8. }
这个函数通过调用本地方法nativeStart来执行进一步的启动操作。

Step 11. InputManager.nativeStart

这个函数定义在frameworks/base/services/jni$ vi com_android_server_InputManager.cpp文件中:

[cpp] view plain copy
  1. staticvoidandroid_server_InputManager_nativeStart(JNIEnv*env,jclassclazz){
  2. if(checkInputManagerUnitialized(env)){
  3. return;
  4. }
  5. status_tresult=gNativeInputManager->getInputManager()->start();
  6. if(result){
  7. jniThrowRuntimeException(env,"Inputmanagercouldnotbestarted.");
  8. }
  9. }
这里的gNativeInputManager对象是在前面的Step 6中创建的,通过它的getInputManager函数可以返回C++层的InputManager对象,接着调用这个InputManager对象的start函数。

Step 12. InputManager.start

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

[cpp] view plain copy
  1. status_tInputManager::start(){
  2. status_tresult=mDispatcherThread->run("InputDispatcher",PRIORITY_URGENT_DISPLAY);
  3. if(result){
  4. LOGE("CouldnotstartInputDispatcherthreadduetoerror%d.",result);
  5. returnresult;
  6. }
  7. result=mReaderThread->run("InputReader",PRIORITY_URGENT_DISPLAY);
  8. if(result){
  9. LOGE("CouldnotstartInputReaderthreadduetoerror%d.",result);
  10. mDispatcherThread->requestExit();
  11. returnresult;
  12. }
  13. returnOK;
  14. }
这个函数主要就是分别启动一个InputDispatcherThread线程和一个InputReaderThread线程来读取和分发键盘消息的了。这里的InputDispatcherThread线程对象mDispatcherThread和InputReaderThread线程对象是在前面的Step 9中创建的,调用了它们的run函数后,就会进入到它们的threadLoop函数中去,只要threadLoop函数返回true,函数threadLoop就会一直被循环调用,于是这两个线程就起到了不断地读取和分发键盘消息的作用。

我们先来分析InputDispatcherThread线程分发消息的过程,然后再回过头来分析InputReaderThread线程读取消息的过程。

Step 13.InputDispatcherThread.threadLoop

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

[cpp] view plain copy
  1. boolInputDispatcherThread::threadLoop(){
  2. mDispatcher->dispatchOnce();
  3. returntrue;
  4. }
这里的成员变量mDispatcher即为在前面Step 8中创建的InputDispatcher对象,调用它的dispatchOnce成员函数执行一次键盘消息分发的操作。

Step 14. InputDispatcher.dispatchOnce

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

[cpp] view plain copy
  1. voidInputDispatcher::dispatchOnce(){
  2. nsecs_tkeyRepeatTimeout=mPolicy->getKeyRepeatTimeout();
  3. nsecs_tkeyRepeatDelay=mPolicy->getKeyRepeatDelay();
  4. nsecs_tnextWakeupTime=LONG_LONG_MAX;
  5. {//acquirelock
  6. AutoMutex_l(mLock);
  7. dispatchOnceInnerLocked(keyRepeatTimeout,keyRepeatDelay,&nextWakeupTime);
  8. if(runCommandsLockedInterruptible()){
  9. nextWakeupTime=LONG_LONG_MIN;//forcenextpolltowakeupimmediately
  10. }
  11. }//releaselock
  12. //Waitforcallbackortimeoutorwake.(makesureweroundup,notdown)
  13. nsecs_tcurrentTime=now();
  14. int32_ttimeoutMillis;
  15. if(nextWakeupTime>currentTime){
  16. uint64_ttimeout=uint64_t(nextWakeupTime-currentTime);
  17. timeout=(timeout+999999LL)/1000000LL;
  18. timeoutMillis=timeout>INT_MAX?-1:int32_t(timeout);
  19. }else{
  20. timeoutMillis=0;
  21. }
  22. mLooper->pollOnce(timeoutMillis);
  23. }
这个函数很简单,把键盘消息交给dispatchOnceInnerLocked函数来处理,这个过程我们在后面再详细分析,然后调用mLooper->pollOnce函数等待下一次键盘事件的发生。这里的成员变量mLooper的类型为Looper,它定义在C++层中,具体可以参考前面 Android应用程序消息处理机制(Looper、Handler)分析 一文。

Step 15. Looper.pollOnce

这个函数定义在frameworks/base/libs/utils/Looper.cpp文件中,具体可以参考前面Android应用程序消息处理机制(Looper、Handler)分析一文,这里就不再详述了。总的来说,就是在Looper类中,会创建一个管道,当调用Looper类的pollOnce函数时,如果管道中没有内容可读,那么当前线程就会进入到空闲等待状态;当有键盘事件发生时,InputReader就会往这个管道中写入新的内容,这样就会唤醒前面正在等待键盘事件发生的线程。

InputDispatcher类分发消息的过程就暂时分析到这里,后面会有更进一步的分析,现在,我们回到Step 12中,接着分析InputReader类读取键盘事件的过程。在调用了InputReaderThread线程类的run就函数后,同样会进入到InputReaderThread线程类的threadLoop函数中去。

Step 16. InputReaderThread.threadLoop

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

[cpp] view plain copy
  1. boolInputReaderThread::threadLoop(){
  2. mReader->loopOnce();
  3. returntrue;
  4. }
这里的成员变量mReader即为在前面Step 8中创建的InputReader对象,调用它的loopOnce成员函数执行一次键盘事件的读取操作。

Step 17. InputReader.loopOnce

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

[cpp] view plain copy
  1. voidInputReader::loopOnce(){
  2. RawEventrawEvent;
  3. mEventHub->getEvent(&rawEvent);
  4. #ifDEBUG_RAW_EVENTS
  5. LOGD("Inputevent:device=0x%xtype=0x%xscancode=%dkeycode=%dvalue=%d",
  6. rawEvent.deviceId,rawEvent.type,rawEvent.scanCode,rawEvent.keyCode,
  7. rawEvent.value);
  8. #endif
  9. process(&rawEvent);
  10. }
这里通过成员函数mEventHub来负责键盘消息的读取工作,如果当前有键盘事件发生或者有键盘事件等待处理,通过mEventHub的getEvent函数就可以得到这个事件,然后交给process函数进行处理,这个函数主要就是唤醒前面的InputDispatcherThread线程,通知它有新的键盘事件发生了,它需要进行一次键盘消息的分发操作了,这个函数我们后面再进一步详细分析;如果没有键盘事件发生或者没有键盘事件等待处理,那么调用mEventHub的getEvent函数时就会进入等待状态。

Step 18. EventHub.getEvent

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

[cpp] view plain copy
  1. boolEventHub::getEvent(RawEvent*outEvent)
  2. {
  3. outEvent->deviceId=0;
  4. outEvent->type=0;
  5. outEvent->scanCode=0;
  6. outEvent->keyCode=0;
  7. outEvent->flags=0;
  8. outEvent->value=0;
  9. outEvent->when=0;
  10. //NotethatweonlyallowonecallertogetEvent(),sodon'tneed
  11. //todolockinghere...onlywhenadding/removingdevices.
  12. if(!mOpened){
  13. mError=openPlatformInput()?NO_ERROR:UNKNOWN_ERROR;
  14. mOpened=true;
  15. mNeedToSendFinishedDeviceScan=true;
  16. }
  17. for(;;){
  18. //Reportanydevicesthathadlastbeenadded/removed.
  19. if(mClosingDevices!=NULL){
  20. device_t*device=mClosingDevices;
  21. LOGV("Reportingdeviceclosed:id=0x%x,name=%s\n",
  22. device->id,device->path.string());
  23. mClosingDevices=device->next;
  24. if(device->id==mFirstKeyboardId){
  25. outEvent->deviceId=0;
  26. }else{
  27. outEvent->deviceId=device->id;
  28. }
  29. outEvent->type=DEVICE_REMOVED;
  30. outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC);
  31. deletedevice;
  32. mNeedToSendFinishedDeviceScan=true;
  33. returntrue;
  34. }
  35. if(mOpeningDevices!=NULL){
  36. device_t*device=mOpeningDevices;
  37. LOGV("Reportingdeviceopened:id=0x%x,name=%s\n",
  38. device->id,device->path.string());
  39. mOpeningDevices=device->next;
  40. if(device->id==mFirstKeyboardId){
  41. outEvent->deviceId=0;
  42. }else{
  43. outEvent->deviceId=device->id;
  44. }
  45. outEvent->type=DEVICE_ADDED;
  46. outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC);
  47. mNeedToSendFinishedDeviceScan=true;
  48. returntrue;
  49. }
  50. if(mNeedToSendFinishedDeviceScan){
  51. mNeedToSendFinishedDeviceScan=false;
  52. outEvent->type=FINISHED_DEVICE_SCAN;
  53. outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC);
  54. returntrue;
  55. }
  56. //Grabthenextinputevent.
  57. for(;;){
  58. //Consumebufferedinputevents,ifany.
  59. if(mInputBufferIndex<mInputBufferCount){
  60. conststructinput_event&iev=mInputBufferData[mInputBufferIndex++];
  61. constdevice_t*device=mDevices[mInputDeviceIndex];
  62. LOGV("%sgot:t0=%d,t1=%d,type=%d,code=%d,v=%d",device->path.string(),
  63. (int)iev.time.tv_sec,(int)iev.time.tv_usec,iev.type,iev.code,iev.value);
  64. if(device->id==mFirstKeyboardId){
  65. outEvent->deviceId=0;
  66. }else{
  67. outEvent->deviceId=device->id;
  68. }
  69. outEvent->type=iev.type;
  70. outEvent->scanCode=iev.code;
  71. if(iev.type==EV_KEY){
  72. status_terr=device->layoutMap->map(iev.code,
  73. &outEvent->keyCode,&outEvent->flags);
  74. LOGV("iev.code=%dkeyCode=%dflags=0x%08xerr=%d\n",
  75. iev.code,outEvent->keyCode,outEvent->flags,err);
  76. if(err!=0){
  77. outEvent->keyCode=AKEYCODE_UNKNOWN;
  78. outEvent->flags=0;
  79. }
  80. }else{
  81. outEvent->keyCode=iev.code;
  82. }
  83. outEvent->value=iev.value;
  84. //Useaneventtimestampinthesametimebaseas
  85. //java.lang.System.nanoTime()andandroid.os.SystemClock.uptimeMillis()
  86. //asexpectedbytherestofthesystem.
  87. outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC);
  88. returntrue;
  89. }
  90. //Finishreadingalleventsfromdevicesidentifiedinpreviouspoll().
  91. //ThiscodeassumesthatmInputDeviceIndexisinitially0andthatthe
  92. //reventsmemberofpollfdisinitializedto0whenthedeviceisfirstadded.
  93. //SincemFDs[0]isusedforinotify,weprocessregulareventsstartingatindex1.
  94. mInputDeviceIndex+=1;
  95. if(mInputDeviceIndex>=mFDCount){
  96. break;
  97. }
  98. conststructpollfd&pfd=mFDs[mInputDeviceIndex];
  99. if(pfd.revents&POLLIN){
  100. int32_treadSize=read(pfd.fd,mInputBufferData,
  101. sizeof(structinput_event)*INPUT_BUFFER_SIZE);
  102. if(readSize<0){
  103. if(errno!=EAGAIN&&errno!=EINTR){
  104. LOGW("couldnotgetevent(errno=%d)",errno);
  105. }
  106. }elseif((readSize%sizeof(structinput_event))!=0){
  107. LOGE("couldnotgetevent(wrongsize:%d)",readSize);
  108. }else{
  109. mInputBufferCount=readSize/sizeof(structinput_event);
  110. mInputBufferIndex=0;
  111. }
  112. }
  113. }
  114. ......
  115. mInputDeviceIndex=0;
  116. //Pollforevents.Mindthewakelockdance!
  117. //Weholdawakelockatalltimesexceptduringpoll().Thisworksduetosome
  118. //subtlechoreography.Whenadevicedriverhaspending(unread)events,itacquires
  119. //akernelwakelock.However,oncethelastpendingeventhasbeenread,thedevice
  120. //driverwillreleasethekernelwakelock.Topreventthesystemfromgoingtosleep
  121. //whenthishappens,theEventHubholdsontoitsownuserwakelockwhiletheclient
  122. //isprocessingevents.Thusthesystemcanonlysleepiftherearenoevents
  123. //pendingorcurrentlybeingprocessed.
  124. release_wake_lock(WAKE_LOCK_ID);
  125. intpollResult=poll(mFDs,mFDCount,-1);
  126. acquire_wake_lock(PARTIAL_WAKE_LOCK,WAKE_LOCK_ID);
  127. if(pollResult<=0){
  128. if(errno!=EINTR){
  129. LOGW("pollfailed(errno=%d)\n",errno);
  130. usleep(100000);
  131. }
  132. }
  133. }
  134. }
这个函数比较长,我们一步一步来分析。

首先,如果是第一次进入到这个函数中时,成员变量mOpened的值为false,于是就会调用openPlatformInput函数来打开系统输入设备,在本文中,我们主要讨论的输入设备就是键盘了。打开了这些输入设备文件后,就可以对这些输入设备进行是监控了。如果不是第一次进入到这个函数,那么就会分析当前有没有输入事件发生,如果有,就返回这个事件,否则就会进入等待状态,等待下一次输入事件的发生。在我们这个场景中,就是等待下一次键盘事件的发生了。

我们先分析openPlatformInput函数的实现,然后回过头来分析这个getEvent函数的具体的实现。

Step 19. EventHub.openPlatformInput

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

[cpp] view plain copy
  1. boolEventHub::openPlatformInput(void)
  2. {
  3. ......
  4. res=scanDir(device_path);
  5. if(res<0){
  6. LOGE("scandirfailedfor%s\n",device_path);
  7. }
  8. returntrue;
  9. }
这个函数主要是扫描device_path目录下的设备文件,然后打开它们,这里的变量device_path定义在frameworks/base/libs/ui/EventHub.cpp文件开始的地方:

[cpp] view plain copy
  1. staticconstchar*device_path="/dev/input";
在设备目录/dev/input中,一般有三个设备文件存在,分别是event0、mice和mouse0设备文件,其中,键盘事件就包含在event0设备文件中了。

Step 20.EventHub.scanDir

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

[cpp] view plain copy
  1. intEventHub::scanDir(constchar*dirname)
  2. {
  3. chardevname[PATH_MAX];
  4. char*filename;
  5. DIR*dir;
  6. structdirent*de;
  7. dir=opendir(dirname);
  8. if(dir==NULL)
  9. return-1;
  10. strcpy(devname,dirname);
  11. filename=devname+strlen(devname);
  12. *filename++='/';
  13. while((de=readdir(dir))){
  14. if(de->d_name[0]=='.'&&
  15. (de->d_name[1]=='\0'||
  16. (de->d_name[1]=='.'&&de->d_name[2]=='\0')))
  17. continue;
  18. strcpy(filename,de->d_name);
  19. openDevice(devname);
  20. }
  21. closedir(dir);
  22. return0;
  23. }
根据上面一步的分析,这个函数主要就是调用openDevice函数来分别打开/dev/input/event0、/dev/input/mice和/dev/input/mouse0三个设备文件了。

Step 21.EventHub.openDevice
这个函数定义在frameworks/base/libs/ui/EventHub.cpp文件中:

[cpp] view plain copy
  1. intEventHub::openDevice(constchar*deviceName){
  2. intversion;
  3. intfd;
  4. structpollfd*new_mFDs;
  5. device_t**new_devices;
  6. char**new_device_names;
  7. charname[80];
  8. charlocation[80];
  9. charidstr[80];
  10. structinput_idid;
  11. LOGV("Openingdevice:%s",deviceName);
  12. AutoMutex_l(mLock);
  13. fd=open(deviceName,O_RDWR);
  14. if(fd<0){
  15. LOGE("couldnotopen%s,%s\n",deviceName,strerror(errno));
  16. return-1;
  17. }
  18. ......
  19. intdevid=0;
  20. while(devid<mNumDevicesById){
  21. if(mDevicesById[devid].device==NULL){
  22. break;
  23. }
  24. devid++;
  25. }
  26. ......
  27. mDevicesById[devid].seq=(mDevicesById[devid].seq+(1<<SEQ_SHIFT))&SEQ_MASK;
  28. if(mDevicesById[devid].seq==0){
  29. mDevicesById[devid].seq=1<<SEQ_SHIFT;
  30. }
  31. new_mFDs=(pollfd*)realloc(mFDs,sizeof(mFDs[0])*(mFDCount+1));
  32. new_devices=(device_t**)realloc(mDevices,sizeof(mDevices[0])*(mFDCount+1));
  33. if(new_mFDs==NULL||new_devices==NULL){
  34. LOGE("outofmemory");
  35. return-1;
  36. }
  37. mFDs=new_mFDs;
  38. mDevices=new_devices;
  39. ......
  40. device_t*device=newdevice_t(devid|mDevicesById[devid].seq,deviceName,name);
  41. if(device==NULL){
  42. LOGE("outofmemory");
  43. return-1;
  44. }
  45. device->fd=fd;
  46. mFDs[mFDCount].fd=fd;
  47. mFDs[mFDCount].events=POLLIN;
  48. mFDs[mFDCount].revents=0;
  49. //Figureoutthekindsofeventsthedevicereports.
  50. uint8_tkey_bitmask[sizeof_bit_array(KEY_MAX+1)];
  51. memset(key_bitmask,0,sizeof(key_bitmask));
  52. LOGV("Gettingkeys...");
  53. if(ioctl(fd,EVIOCGBIT(EV_KEY,sizeof(key_bitmask)),key_bitmask)>=0){
  54. //Seeifthisisakeyboard.Ignoreeverythinginthebuttonrangeexceptfor
  55. //gamepadswhicharealsoconsideredkeyboards.
  56. if(containsNonZeroByte(key_bitmask,0,sizeof_bit_array(BTN_MISC))
  57. ||containsNonZeroByte(key_bitmask,sizeof_bit_array(BTN_GAMEPAD),
  58. sizeof_bit_array(BTN_DIGI))
  59. ||containsNonZeroByte(key_bitmask,sizeof_bit_array(KEY_OK),
  60. sizeof_bit_array(KEY_MAX+1))){
  61. device->classes|=INPUT_DEVICE_CLASS_KEYBOARD;
  62. device->keyBitmask=newuint8_t[sizeof(key_bitmask)];
  63. if(device->keyBitmask!=NULL){
  64. memcpy(device->keyBitmask,key_bitmask,sizeof(key_bitmask));
  65. }else{
  66. deletedevice;
  67. LOGE("outofmemoryallocatingkeybitmask");
  68. return-1;
  69. }
  70. }
  71. }
  72. ......
  73. if((device->classes&INPUT_DEVICE_CLASS_KEYBOARD)!=0){
  74. chartmpfn[sizeof(name)];
  75. charkeylayoutFilename[300];
  76. //amoredescriptivename
  77. device->name=name;
  78. //replaceallthespaceswithunderscores
  79. strcpy(tmpfn,name);
  80. for(char*p=strchr(tmpfn,'');p&&*p;p=strchr(tmpfn,''))
  81. *p='_';
  82. //findthe.klfileweneedforthisdevice
  83. constchar*root=getenv("ANDROID_ROOT");
  84. snprintf(keylayoutFilename,sizeof(keylayoutFilename),
  85. "%s/usr/keylayout/%s.kl",root,tmpfn);
  86. booldefaultKeymap=false;
  87. if(access(keylayoutFilename,R_OK)){
  88. snprintf(keylayoutFilename,sizeof(keylayoutFilename),
  89. "%s/usr/keylayout/%s",root,"qwerty.kl");
  90. defaultKeymap=true;
  91. }
  92. status_tstatus=device->layoutMap->load(keylayoutFilename);
  93. if(status){
  94. LOGE("Error%dloadingkeylayout.",status);
  95. }
  96. //telltheworldaboutthedevname(thedescriptivename)
  97. if(!mHaveFirstKeyboard&&!defaultKeymap&&strstr(name,"-keypad")){
  98. //thebuilt-inkeyboardhasawell-knowndeviceIDof0,
  99. //thisdevicebetternotgoaway.
  100. mHaveFirstKeyboard=true;
  101. mFirstKeyboardId=device->id;
  102. property_set("hw.keyboards.0.devname",name);
  103. }else{
  104. //ensuremFirstKeyboardIdissetto-something-.
  105. if(mFirstKeyboardId==0){
  106. mFirstKeyboardId=device->id;
  107. }
  108. }
  109. charpropName[100];
  110. sprintf(propName,"hw.keyboards.%u.devname",device->id);
  111. property_set(propName,name);
  112. //'Q'keysupport=cheaptestofwhetherthisisanalpha-capablekbd
  113. if(hasKeycodeLocked(device,AKEYCODE_Q)){
  114. device->classes|=INPUT_DEVICE_CLASS_ALPHAKEY;
  115. }
  116. //SeeifthisdevicehasaDPAD.
  117. if(hasKeycodeLocked(device,AKEYCODE_DPAD_UP)&&
  118. hasKeycodeLocked(device,AKEYCODE_DPAD_DOWN)&&
  119. hasKeycodeLocked(device,AKEYCODE_DPAD_LEFT)&&
  120. hasKeycodeLocked(device,AKEYCODE_DPAD_RIGHT)&&
  121. hasKeycodeLocked(device,AKEYCODE_DPAD_CENTER)){
  122. device->classes|=INPUT_DEVICE_CLASS_DPAD;
  123. }
  124. //Seeifthisdevicehasagamepad.
  125. for(size_ti=0;i<sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]);i++){
  126. if(hasKeycodeLocked(device,GAMEPAD_KEYCODES[i])){
  127. device->classes|=INPUT_DEVICE_CLASS_GAMEPAD;
  128. break;
  129. }
  130. }
  131. LOGI("Newkeyboard:device->id=0x%xdevname='%s'propName='%s'keylayout='%s'\n",
  132. device->id,name,propName,keylayoutFilename);
  133. }
  134. ......
  135. mDevicesById[devid].device=device;
  136. device->next=mOpeningDevices;
  137. mOpeningDevices=device;
  138. mDevices[mFDCount]=device;
  139. mFDCount++;
  140. return0;
  141. }
函数首先根据文件名来打开这个设备文件:

[cpp] view plain copy
  1. fd=open(deviceName,O_RDWR);
系统中所有输入设备文件信息都保存在成员变量mDevicesById中,因此,先在mDevicesById找到一个空位置来保存当前打开的设备文件信息:

[cpp] view plain copy
  1. mDevicesById[devid].seq=(mDevicesById[devid].seq+(1<<SEQ_SHIFT))&SEQ_MASK;
  2. if(mDevicesById[devid].seq==0){
  3. mDevicesById[devid].seq=1<<SEQ_SHIFT;
  4. }
找到了空闲位置后,就为这个输入设备文件创建相应的device_t信息:
[cpp] view plain copy
  1. mDevicesById[devid].seq=(mDevicesById[devid].seq+(1<<SEQ_SHIFT))&SEQ_MASK;
  2. if(mDevicesById[devid].seq==0){
  3. mDevicesById[devid].seq=1<<SEQ_SHIFT;
  4. }
  5. new_mFDs=(pollfd*)realloc(mFDs,sizeof(mFDs[0])*(mFDCount+1));
  6. new_devices=(device_t**)realloc(mDevices,sizeof(mDevices[0])*(mFDCount+1));
  7. if(new_mFDs==NULL||new_devices==NULL){
  8. LOGE("outofmemory");
  9. return-1;
  10. }
  11. mFDs=new_mFDs;
  12. mDevices=new_devices;
  13. ......
  14. device_t*device=newdevice_t(devid|mDevicesById[devid].seq,deviceName,name);
  15. if(device==NULL){
  16. LOGE("outofmemory");
  17. return-1;
  18. }
  19. device->fd=fd;

同时,这个设备文件还会保存在数组mFDs中:

[cpp] view plain copy
  1. mFDs[mFDCount].fd=fd;
  2. mFDs[mFDCount].events=POLLIN;
  3. mFDs[mFDCount].revents=0;
接下来查看这个设备是不是键盘:

[cpp] view plain copy
  1. //Figureoutthekindsofeventsthedevicereports.
  2. uint8_tkey_bitmask[sizeof_bit_array(KEY_MAX+1)];
  3. memset(key_bitmask,0,sizeof(key_bitmask));
  4. LOGV("Gettingkeys...");
  5. if(ioctl(fd,EVIOCGBIT(EV_KEY,sizeof(key_bitmask)),key_bitmask)>=0){
  6. //Seeifthisisakeyboard.Ignoreeverythinginthebuttonrangeexceptfor
  7. //gamepadswhicharealsoconsideredkeyboards.
  8. if(containsNonZeroByte(key_bitmask,0,sizeof_bit_array(BTN_MISC))
  9. ||containsNonZeroByte(key_bitmask,sizeof_bit_array(BTN_GAMEPAD),
  10. sizeof_bit_array(BTN_DIGI))
  11. ||containsNonZeroByte(key_bitmask,sizeof_bit_array(KEY_OK),
  12. sizeof_bit_array(KEY_MAX+1))){
  13. device->classes|=INPUT_DEVICE_CLASS_KEYBOARD;
  14. device->keyBitmask=newuint8_t[sizeof(key_bitmask)];
  15. if(device->keyBitmask!=NULL){
  16. memcpy(device->keyBitmask,key_bitmask,sizeof(key_bitmask));
  17. }else{
  18. deletedevice;
  19. LOGE("outofmemoryallocatingkeybitmask");
  20. return-1;
  21. }
  22. }
  23. }
如果是的话,还要继续进一步初始化前面为这个设备文件所创建的device_t结构体,主要就是把结构体device的classes成员变量的INPUT_DEVICE_CLASS_KEYBOARD位置为1了,以表明这是一个键盘。
如果是键盘设备,初始化工作还未完成,还要继续设置键盘的布局等信息:

[cpp] view plain copy
  1. if((device->classes&INPUT_DEVICE_CLASS_KEYBOARD)!=0){
  2. chartmpfn[sizeof(name)];
  3. charkeylayoutFilename[300];
  4. //amoredescriptivename
  5. device->name=name;
  6. //replaceallthespaceswithunderscores
  7. strcpy(tmpfn,name);
  8. for(char*p=strchr(tmpfn,'');p&&*p;p=strchr(tmpfn,''))
  9. *p='_';
  10. //findthe.klfileweneedforthisdevice
  11. constchar*root=getenv("ANDROID_ROOT");
  12. snprintf(keylayoutFilename,sizeof(keylayoutFilename),
  13. "%s/usr/keylayout/%s.kl",root,tmpfn);
  14. booldefaultKeymap=false;
  15. if(access(keylayoutFilename,R_OK)){
  16. snprintf(keylayoutFilename,sizeof(keylayoutFilename),
  17. "%s/usr/keylayout/%s",root,"qwerty.kl");
  18. defaultKeymap=true;
  19. }
  20. status_tstatus=device->layoutMap->load(keylayoutFilename);
  21. if(status){
  22. LOGE("Error%dloadingkeylayout.",status);
  23. }
  24. //telltheworldaboutthedevname(thedescriptivename)
  25. if(!mHaveFirstKeyboard&&!defaultKeymap&&strstr(name,"-keypad")){
  26. //thebuilt-inkeyboardhasawell-knowndeviceIDof0,
  27. //thisdevicebetternotgoaway.
  28. mHaveFirstKeyboard=true;
  29. mFirstKeyboardId=device->id;
  30. property_set("hw.keyboards.0.devname",name);
  31. }else{
  32. //ensuremFirstKeyboardIdissetto-something-.
  33. if(mFirstKeyboardId==0){
  34. mFirstKeyboardId=device->id;
  35. }
  36. }
  37. charpropName[100];
  38. sprintf(propName,"hw.keyboards.%u.devname",device->id);
  39. property_set(propName,name);
  40. //'Q'keysupport=cheaptestofwhetherthisisanalpha-capablekbd
  41. if(hasKeycodeLocked(device,AKEYCODE_Q)){
  42. device->classes|=INPUT_DEVICE_CLASS_ALPHAKEY;
  43. }
  44. //SeeifthisdevicehasaDPAD.
  45. if(hasKeycodeLocked(device,AKEYCODE_DPAD_UP)&&
  46. hasKeycodeLocked(device,AKEYCODE_DPAD_DOWN)&&
  47. hasKeycodeLocked(device,AKEYCODE_DPAD_LEFT)&&
  48. hasKeycodeLocked(device,AKEYCODE_DPAD_RIGHT)&&
  49. hasKeycodeLocked(device,AKEYCODE_DPAD_CENTER)){
  50. device->classes|=INPUT_DEVICE_CLASS_DPAD;
  51. }
  52. //Seeifthisdevicehasagamepad.
  53. for(size_ti=0;i<sizeof(GAMEPAD_KEYCODES)/sizeof(GAMEPAD_KEYCODES[0]);i++){
  54. if(hasKeycodeLocked(device,GAMEPAD_KEYCODES[i])){
  55. device->classes|=INPUT_DEVICE_CLASS_GAMEPAD;
  56. break;
  57. }
  58. }
  59. LOGI("Newkeyboard:device->id=0x%xdevname='%s'propName='%s'keylayout='%s'\n",
  60. device->id,name,propName,keylayoutFilename);
  61. }
到这里,系统中的输入设备文件就打开了。

回到Step 18中,我们继续分析EventHub.getEvent函数的实现。

在中间的for循环里面,首先会检查当前是否有输入设备被关闭,如果有,就返回一个设备移除的事件给调用方:

[cpp] view plain copy
  1. //Reportanydevicesthathadlastbeenadded/removed.
  2. if(mClosingDevices!=NULL){
  3. device_t*device=mClosingDevices;
  4. LOGV("Reportingdeviceclosed:id=0x%x,name=%s\n",
  5. device->id,device->path.string());
  6. mClosingDevices=device->next;
  7. if(device->id==mFirstKeyboardId){
  8. outEvent->deviceId=0;
  9. }else{
  10. outEvent->deviceId=device->id;
  11. }
  12. outEvent->type=DEVICE_REMOVED;
  13. outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC);
  14. deletedevice;
  15. mNeedToSendFinishedDeviceScan=true;
  16. returntrue;
  17. }
接着,检查当前是否有新的输入设备加入进来:

[cpp] view plain copy
  1. if(mOpeningDevices!=NULL){
  2. device_t*device=mOpeningDevices;
  3. LOGV("Reportingdeviceopened:id=0x%x,name=%s\n",
  4. device->id,device->path.string());
  5. mOpeningDevices=device->next;
  6. if(device->id==mFirstKeyboardId){
  7. outEvent->deviceId=0;
  8. }else{
  9. outEvent->deviceId=device->id;
  10. }
  11. outEvent->type=DEVICE_ADDED;
  12. outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC);
  13. mNeedToSendFinishedDeviceScan=true;
  14. returntrue;
  15. }
接着,再检查是否需要结束监控输入事件:

[cpp] view plain copy
  1. if(mNeedToSendFinishedDeviceScan){
  2. mNeedToSendFinishedDeviceScan=false;
  3. outEvent->type=FINISHED_DEVICE_SCAN;
  4. outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC);
  5. returntrue;
  6. }
最后,就是要检查当前是否有还未处理的输入设备事件发生了:

[cpp] view plain copy
  1. //Grabthenextinputevent.
  2. for(;;){
  3. //Consumebufferedinputevents,ifany.
  4. if(mInputBufferIndex<mInputBufferCount){
  5. conststructinput_event&iev=mInputBufferData[mInputBufferIndex++];
  6. constdevice_t*device=mDevices[mInputDeviceIndex];
  7. LOGV("%sgot:t0=%d,t1=%d,type=%d,code=%d,v=%d",device->path.string(),
  8. (int)iev.time.tv_sec,(int)iev.time.tv_usec,iev.type,iev.code,iev.value);
  9. if(device->id==mFirstKeyboardId){
  10. outEvent->deviceId=0;
  11. }else{
  12. outEvent->deviceId=device->id;
  13. }
  14. outEvent->type=iev.type;
  15. outEvent->scanCode=iev.code;
  16. if(iev.type==EV_KEY){
  17. status_terr=device->layoutMap->map(iev.code,
  18. &outEvent->keyCode,&outEvent->flags);
  19. LOGV("iev.code=%dkeyCode=%dflags=0x%08xerr=%d\n",
  20. iev.code,outEvent->keyCode,outEvent->flags,err);
  21. if(err!=0){
  22. outEvent->keyCode=AKEYCODE_UNKNOWN;
  23. outEvent->flags=0;
  24. }
  25. }else{
  26. outEvent->keyCode=iev.code;
  27. }
  28. outEvent->value=iev.value;
  29. //Useaneventtimestampinthesametimebaseas
  30. //java.lang.System.nanoTime()andandroid.os.SystemClock.uptimeMillis()
  31. //asexpectedbytherestofthesystem.
  32. outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC);
  33. returntrue;
  34. }
  35. //Finishreadingalleventsfromdevicesidentifiedinpreviouspoll().
  36. //ThiscodeassumesthatmInputDeviceIndexisinitially0andthatthe
  37. //reventsmemberofpollfdisinitializedto0whenthedeviceisfirstadded.
  38. //SincemFDs[0]isusedforinotify,weprocessregulareventsstartingatindex1.
  39. mInputDeviceIndex+=1;
  40. if(mInputDeviceIndex>=mFDCount){
  41. break;
  42. }
  43. conststructpollfd&pfd=mFDs[mInputDeviceIndex];
  44. if(pfd.revents&POLLIN){
  45. int32_treadSize=read(pfd.fd,mInputBufferData,
  46. sizeof(structinput_event)*INPUT_BUFFER_SIZE);
  47. if(readSize<0){
  48. if(errno!=EAGAIN&&errno!=EINTR){
  49. LOGW("couldnotgetevent(errno=%d)",errno);
  50. }
  51. }elseif((readSize%sizeof(structinput_event))!=0){
  52. LOGE("couldnotgetevent(wrongsize:%d)",readSize);
  53. }else{
  54. mInputBufferCount=readSize/sizeof(structinput_event);
  55. mInputBufferIndex=0;
  56. }
  57. }
  58. }
未处理的输入事件保存在成员变量mInputBufferData中,如果有的话,就可以直接返回了,否则的话,就要通过系统调用poll来等待输入设备上发生新的事件了,在我们这个场景中,就是等待键盘有键被按下或者松开了。:

[cpp] view plain copy
  1. intpollResult=poll(mFDs,mFDCount,-1);

这里的mFDs包含了我们所要监控的输入设备的打开文件描述符,这是在前面的openPlatformInput函数中初始化的。

Step 22. poll

这是一个Linux系统的文件操作系统调用,它用来查询指定的文件列表是否有有可读写的,如果有,就马上返回,否则的话,就阻塞线程,并等待驱动程序唤醒,重新调用poll函数,或超时返回。在我们的这个场景中,就是要查询是否有键盘事件发生,如果有的话,就返回,否则的话,当前线程就睡眠等待键盘事件的发生了。

这样,InputManager的启动过程就分析完了,下面我们再分析应用程序注册键盘消息接收通道的过程。

2.应用程序注册键盘消息接收通道的过程分析

InputManager启动以后,就开始负责监控键盘输入事件了。当InputManager监控到键盘输入事件时,它应该把这个键盘事件分发给谁呢?当然是要把这个键盘消息分发给当前激活的Activity窗口了,不过,当前激活的Activity窗口还需要主动注册一个键盘消息接收通道到InputManager中去,InputManager才能把这个键盘消息分发给它处理。那么,当前被激活的Activity窗口又是什么时候去注册这个键盘消息接收通道的呢?在前面一篇文章Android应用程序启动过程源代码分析中,我们分析Android应用程序的启动过程时,在Step 33中分析到ActivityThread类的handleLaunchActivity函数中,我们曾经说过,当函数handleLaunchActivity调用performLaunchActivity函数来加载这个完毕应用程序的默认Activity后,再次回到handleLaunchActivity函数时,会调用handleResumeActivity函数来使这个Activity进入Resumed状态。在调用handleResumeActivity函数的过程中,ActivityThread会通过android.view.WindowManagerImpl类为该Activity创建一个ViewRoot实例,并且会通过调用ViewRoot类的setView成员函数把与该Activity关联的View设置到这个ViewRoot中去,而Activity正是通过ViewRoot类的setView成员函数来注册键盘消息接收通道的。

有了这些背影知识后,接下来,我们就可以从ViewRoot.setView函数开始分析应用程序注册键盘消息接收通道的过程了。首先看一下这个注册过程的序列图,然后再详细分析每一个步骤:

Step 1. ViewRoot.setView

这个函数定义在frameworks/base/core/java/android/view/ViewRoot.java文件中:

[java] view plain copy
  1. publicfinalclassViewRootextendsHandlerimplementsViewParent,
  2. View.AttachInfo.Callbacks{
  3. ......
  4. publicvoidsetView(Viewview,WindowManager.LayoutParamsattrs,
  5. ViewpanelParentView){
  6. ......
  7. synchronized(this){
  8. if(mView==null){
  9. ......
  10. //Schedulethefirstlayout-before-addingtothewindow
  11. //manager,tomakesurewedotherelayoutbeforereceiving
  12. //anyothereventsfromthesystem.
  13. requestLayout();
  14. mInputChannel=newInputChannel();
  15. try{
  16. res=sWindowSession.add(mWindow,mWindowAttributes,
  17. getHostVisibility(),mAttachInfo.mContentInsets,
  18. mInputChannel);
  19. }catch(RemoteExceptione){
  20. ......
  21. }finally{
  22. ......
  23. }
  24. ......
  25. if(viewinstanceofRootViewSurfaceTaker){
  26. mInputQueueCallback=
  27. ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue();
  28. }
  29. if(mInputQueueCallback!=null){
  30. mInputQueue=newInputQueue(mInputChannel);
  31. mInputQueueCallback.onInputQueueCreated(mInputQueue);
  32. }else{
  33. InputQueue.registerInputChannel(mInputChannel,mInputHandler,
  34. Looper.myQueue());
  35. }
  36. ......
  37. }
  38. }
  39. }
  40. }

这个函数中与注册键盘消息接收通道(InputChannel)相关的逻辑主要有三处,一是调用requestLayout函数来通知InputManager,这个Activity窗口是当前被激活的窗口,二是调用sWindowSession(WindowManagerService内部类Session的远程接口)的add成员函数来把键盘消息接收通道的一端注册在InputManager中,三是调用InputQueue的registerInputChannel成员函数来把键盘消息接收通道的另一端注册在本应用程序的消息循环(Looper)中。这样,当InputManager监控到有键盘消息时,就会先找到当前被激活的窗口,然后找到其在InputManager中对应的键盘消息接收通道,通过这个通道在InputManager中的一端来通知在应用程序消息循环中的另一端,就把键盘消息分发给当前激活的Activity窗口了。

在接下来的内容中,我们首先描述requestLayout函数是如何告诉InputManager当前的Activity窗口便是激活窗口的,接着再回过头来分析应用程序是如何把键盘消息接收通道的一端注册到InputManager中去的,最后分析应用程序是如何键盘消息接收通道的另一端注册到本应用程序的消息循环中去了。

Step 2.ViewRoot.requestLayout

这个函数定义在frameworks/base/core/java/android/view/ViewRoot.java文件中:

[java] view plain copy
  1. publicfinalclassViewRootextendsHandlerimplementsViewParent,
  2. View.AttachInfo.Callbacks{
  3. ......
  4. publicvoidrequestLayout(){
  5. ......
  6. mLayoutRequested=true;
  7. scheduleTraversals();
  8. }
  9. ......
  10. }
这个函数调用了scheduleTraversals函数来进一步执行操作,由于篇幅关系,我们就不详细描述scheduleTraversals函数了,简单来说,在scheduleTraversals函数中,会通过sendEmptyMessage(DO_TRAVERSAL)发送一个消息到应用程序的消息队列中,这个消息最终由ViewRoot的handleMessage函数处理,而ViewRoot的handleMessage函数把这个消息交给ViewRoot类的performTraversals来处理,在performTraversals函数中,又会调用ViewRoot类的relayoutWindow函数来进一步执行操作,最后在relayoutWindow函数中,就会通过WindowManagerService内部类Session的远程接口sWindowSession的relayout函数来进入到WindowManagerService中。

Step 3.WindowManagerService.Session.relayout

这个函数定义在frameworks/base/services/java/com/android/server/WindowManagerService.java文件中:

[java] view plain copy
  1. publicclassWindowManagerServiceextendsIWindowManager.Stub
  2. implementsWatchdog.Monitor{
  3. ......
  4. privatefinalclassSessionextendsIWindowSession.Stub
  5. implementsIBinder.DeathRecipient{
  6. ......
  7. publicintrelayout(IWindowwindow,WindowManager.LayoutParamsattrs,
  8. intrequestedWidth,intrequestedHeight,intviewFlags,
  9. booleaninsetsPending,RectoutFrame,RectoutContentInsets,
  10. RectoutVisibleInsets,ConfigurationoutConfig,SurfaceoutSurface){
  11. //Log.d(TAG,">>>>>>ENTEREDrelayoutfrom"+Binder.getCallingPid());
  12. intres=relayoutWindow(this,window,attrs,
  13. requestedWidth,requestedHeight,viewFlags,insetsPending,
  14. outFrame,outContentInsets,outVisibleInsets,outConfig,outSurface);
  15. //Log.d(TAG,"<<<<<<EXITINGrelayoutto"+Binder.getCallingPid());
  16. returnres;
  17. }
  18. ......
  19. }
  20. ......
  21. }

这个函数只是简单地调用WindowManagerService的成员函数relayoutWIndow来进一步处理。

Step 4.WindowManagerService.relayoutWIndow

这个函数定义在frameworks/base/services/java/com/android/server/WindowManagerService.java文件中:

[java] view plain copy
  1. publicclassWindowManagerServiceextendsIWindowManager.Stub
  2. implementsWatchdog.Monitor{
  3. ......
  4. publicintrelayoutWindow(Sessionsession,IWindowclient,
  5. WindowManager.LayoutParamsattrs,intrequestedWidth,
  6. intrequestedHeight,intviewVisibility,booleaninsetsPending,
  7. RectoutFrame,RectoutContentInsets,RectoutVisibleInsets,
  8. ConfigurationoutConfig,SurfaceoutSurface){
  9. ......
  10. synchronized(mWindowMap){
  11. ......
  12. mInputMonitor.updateInputWindowsLw();
  13. }
  14. ......
  15. }
  16. ......
  17. }
这个函数又会继续调用mInputMonitor的updateInputWindowsLw成员函数来更新当前的输入窗口,mInputMonitor是WindowManagerService的成员变量,它的类型为InputMonitor。

Step 5.InputMonitor.updateInputWindowsLw

这个函数定义在frameworks/base/services/java/com/android/server/WindowManagerService.java文件中:

[java] view plain copy
  1. publicclassWindowManagerServiceextendsIWindowManager.Stub
  2. implementsWatchdog.Monitor{
  3. ......
  4. finalclassInputMonitor{
  5. ......
  6. /*Updatesthecachedwindowinformationprovidedtotheinputdispatcher.*/
  7. publicvoidupdateInputWindowsLw(){
  8. //Populatetheinputwindowlistwithinformationaboutallofthewindowsthat
  9. //couldpotentiallyreceiveinput.
  10. //Asanoptimization,wecouldtrytoprunethelistofwindowsbutthisturns
  11. //outtobedifficultbecauseonlythenativecodeknowsforsurewhichwindow
  12. //currentlyhastouchfocus.
  13. finalArrayList<WindowState>windows=mWindows;
  14. finalintN=windows.size();
  15. for(inti=N-1;i>=0;i--){
  16. finalWindowStatechild=windows.get(i);
  17. if(child.mInputChannel==null||child.mRemoved){
  18. //Skipthiswindowbecauseitcannotpossiblyreceiveinput.
  19. continue;
  20. }
  21. ......
  22. //Addawindowtoourlistofinputwindows.
  23. finalInputWindowinputWindow=mTempInputWindows.add();
  24. ......
  25. }
  26. //Sendwindowstonativecode.
  27. mInputManager.setInputWindows(mTempInputWindows.toNullTerminatedArray());
  28. ......
  29. }
  30. ......
  31. }
  32. ......
  33. }
这个函数将当前系统中带有InputChannel的Activity窗口都设置为InputManager的输入窗口,但是后面我们会看到,只有当前激活的窗口才会响应键盘消息。

Step 6.InputManager.setInputWindows

这个函数定义在frameworks/base/services/java/com/android/server/InputManager.java文件中:

[java] view plain copy
  1. publicclassInputManager{
  2. ......
  3. publicvoidsetInputWindows(InputWindow[]windows){
  4. nativeSetInputWindows(windows);
  5. }
  6. ......
  7. }
这个函数调用了本地方法nativeSetInputWindows来进一步执行操作。
Step 7.InputManager.nativeSetInputWindows

这个函数定义在frameworks/base/services/jni/com_android_server_InputManager.cpp文件中:

[cpp] view plain copy
  1. staticvoidandroid_server_InputManager_nativeSetInputWindows(JNIEnv*env,jclassclazz,
  2. jobjectArraywindowObjArray){
  3. if(checkInputManagerUnitialized(env)){
  4. return;
  5. }
  6. gNativeInputManager->setInputWindows(env,windowObjArray);
  7. }
这里的gNativeInputManager我们前面分析InputManager的启动过程时已经见过了,这是一个本地InputManager对象,通过它进一步设置当前系统的输入窗口。

Step 8.NativeInputManager.setInputWindows

这个函数定义在frameworks/base/services/jni/com_android_server_InputManager.cpp文件中:

[cpp] view plain copy
  1. voidNativeInputManager::setInputWindows(JNIEnv*env,jobjectArraywindowObjArray){
  2. Vector<InputWindow>windows;
  3. jsizelength=env->GetArrayLength(windowObjArray);
  4. for(jsizei=0;i<length;i++){
  5. jobjectinputTargetObj=env->GetObjectArrayElement(windowObjArray,i);
  6. if(!inputTargetObj){
  7. break;//foundnullelementindicatingendofusedportionofthearray
  8. }
  9. windows.push();
  10. InputWindow&window=windows.editTop();
  11. boolvalid=populateWindow(env,inputTargetObj,window);
  12. if(!valid){
  13. windows.pop();
  14. }
  15. env->DeleteLocalRef(inputTargetObj);
  16. }
  17. mInputManager->getDispatcher()->setInputWindows(windows);
  18. }
这个函数首先将Java层的Window转换成C++层的InputWindow,然后放在windows向量中,最后将这些输入窗口设置到InputDispatcher中去。

Step 9. InputDispatcher.setInputWindows

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

[cpp] view plain copy
  1. voidInputDispatcher::setInputWindows(constVector<InputWindow>&inputWindows){
  2. ......
  3. {//acquirelock
  4. AutoMutex_l(mLock);
  5. //Clearoldwindowpointers.
  6. sp<InputChannel>oldFocusedWindowChannel;
  7. if(mFocusedWindow){
  8. oldFocusedWindowChannel=mFocusedWindow->inputChannel;
  9. mFocusedWindow=NULL;
  10. }
  11. mWindows.clear();
  12. //Loopovernewwindowsandrebuildthenecessarywindowpointersfor
  13. //trackingfocusandtouch.
  14. mWindows.appendVector(inputWindows);
  15. size_tnumWindows=mWindows.size();
  16. for(size_ti=0;i<numWindows;i++){
  17. constInputWindow*window=&mWindows.itemAt(i);
  18. if(window->hasFocus){
  19. mFocusedWindow=window;
  20. break;
  21. }
  22. }
  23. ......
  24. }//releaselock
  25. ......
  26. }
这里InputDispatcher的成员变量mFocusedWindow就代表当前激活的窗口的。这个函数首先清空mFocusedWindow,然后再通过一个for循环检查当前的输入窗口中的哪一个窗口是获得焦点的,获得焦点的输入窗口即为当前激活的窗口。

这样,InputManager就把当前激活的Activity窗口保存在InputDispatcher中了,后面就可以把键盘消息分发给它来处理。

回到Step 1中的ViewRoot.setView函数中,接下来就调用下面语句来注册键盘消息接收通道的一端到InputManager中去:

[java] view plain copy
  1. mInputChannel=newInputChannel();
  2. try{
  3. res=sWindowSession.add(mWindow,mWindowAttributes,
  4. getHostVisibility(),mAttachInfo.mContentInsets,
  5. mInputChannel);
  6. }catch(RemoteExceptione){
  7. ......
  8. }finally{
  9. ......
  10. }
前面说过,这里的sWindowSession是WindowManagerService内部类Session的一个远程接口,通过它可以进入到WindowManagerService中去。

Step 10.WindowManagerService.Session.add

这个函数定义在frameworks/base/services/java/com/android/server/WindowManagerService.java文件中:

[java] view plain copy
  1. publicclassWindowManagerServiceextendsIWindowManager.Stub
  2. implementsWatchdog.Monitor{
  3. ......
  4. privatefinalclassSessionextendsIWindowSession.Stub
  5. implementsIBinder.DeathRecipient{
  6. ......
  7. publicintadd(IWindowwindow,WindowManager.LayoutParamsattrs,
  8. intviewVisibility,RectoutContentInsets,InputChanneloutInputChannel){
  9. returnaddWindow(this,window,attrs,viewVisibility,outContentInsets,
  10. outInputChannel);
  11. }
  12. ......
  13. }
  14. ......
  15. }
这里调用WindowManagerService类的addWindow函数来进一步执行操作。

Step 11.WindowManagerService.addWindow

这个函数定义在frameworks/base/services/java/com/android/server/WindowManagerService.java文件中:

[java] view plain copy
  1. publicclassWindowManagerServiceextendsIWindowManager.Stub
  2. implementsWatchdog.Monitor{
  3. ......
  4. publicintaddWindow(Sessionsession,IWindowclient,
  5. WindowManager.LayoutParamsattrs,intviewVisibility,
  6. RectoutContentInsets,InputChanneloutInputChannel){
  7. ......
  8. WindowStatewin=null;
  9. synchronized(mWindowMap){
  10. ......
  11. win=newWindowState(session,client,token,
  12. attachedWindow,attrs,viewVisibility);
  13. ......
  14. if(outInputChannel!=null){
  15. Stringname=win.makeInputChannelName();
  16. InputChannel[]inputChannels=InputChannel.openInputChannelPair(name);
  17. win.mInputChannel=inputChannels[0];
  18. inputChannels[1].transferToBinderOutParameter(outInputChannel);
  19. mInputManager.registerInputChannel(win.mInputChannel);
  20. }
  21. ......
  22. }
  23. ......
  24. }
  25. ......
  26. }

这里的outInputChannel即为前面在Step 1中创建的InputChannel,它不为NULL,因此,这里会通过InputChannel.openInputChannelPair函数来创建一对输入通道,其中一个位于WindowManagerService中,另外一个通过outInputChannel参数返回到应用程序中:

[java] view plain copy
  1. inputChannels[1].transferToBinderOutParameter(outInputChannel);

创建输入通道之前,WindowManagerService会为当前Activity窗口创建一个WindowState对象win,用来记录这个Activity窗口的状态信息。当创建这对输入管道成功以后,也会把其中的一个管道保存在这个WindowState对象win的成员变量mInputChannel中,后面要注销这个管道的时候,就是从这个WindownState对象中取回这个管道的:

[java] view plain copy
  1. win.mInputChannel=inputChannels[0];

接下来我们就看一下InputChannel.openInputChannelPair函数的实现。

Step 12.InputChannel.openInputChannelPair

这个函数定义在frameworks/base/core/java/android/view/InputChannel.java文件中:

[java] view plain copy
  1. publicfinalclassInputChannelimplementsParcelable{
  2. ......
  3. /**
  4. *Createsanewinputchannelpair.Onechannelshouldbeprovidedtotheinput
  5. *dispatcherandtheothertotheapplication'sinputqueue.
  6. *@paramnameThedescriptive(non-unique)nameofthechannelpair.
  7. *@returnApairofinputchannels.Theyaresymmetricandindistinguishable.
  8. */
  9. publicstaticInputChannel[]openInputChannelPair(Stringname){
  10. ......
  11. returnnativeOpenInputChannelPair(name);
  12. }
  13. ......
  14. }
这个函数调用本地方法nativeOpenInputChannelPair来进一步执行操作。

Step 13.InputChannel.nativeOpenInputChannelPair
这个函数定义在frameworks/base/core/jni/android_view_InputChannel.cpp文件中:

[cpp] view plain copy
  1. staticjobjectArrayandroid_view_InputChannel_nativeOpenInputChannelPair(JNIEnv*env,
  2. jclassclazz,jstringnameObj){
  3. constchar*nameChars=env->GetStringUTFChars(nameObj,NULL);
  4. String8name(nameChars);
  5. env->ReleaseStringUTFChars(nameObj,nameChars);
  6. sp<InputChannel>serverChannel;
  7. sp<InputChannel>clientChannel;
  8. status_tresult=InputChannel::openInputChannelPair(name,serverChannel,clientChannel);
  9. if(result){
  10. LOGE("Couldnotopeninputchannelpair.status=%d",result);
  11. jniThrowRuntimeException(env,"Couldnotopeninputchannelpair.");
  12. returnNULL;
  13. }
  14. //TODOmorerobusterrorchecking
  15. jobjectserverChannelObj=android_view_InputChannel_createInputChannel(env,
  16. newNativeInputChannel(serverChannel));
  17. jobjectclientChannelObj=android_view_InputChannel_createInputChannel(env,
  18. newNativeInputChannel(clientChannel));
  19. jobjectArraychannelPair=env->NewObjectArray(2,gInputChannelClassInfo.clazz,NULL);
  20. env->SetObjectArrayElement(channelPair,0,serverChannelObj);
  21. env->SetObjectArrayElement(channelPair,1,clientChannelObj);
  22. returnchannelPair;
  23. }
这个函数根据传进来的参数name在C++层分别创建两个InputChannel,一个作为Server端使用,一个作为Client端使用,这里的Server端即是指InputManager,而Client端即是指应用程序。这两个本地的InputChannel是通过InputChannel::openInputChannelPair函数创建的,创建完成后,再相应地在Java层创建相应的两个InputChannel,然后返回。

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

[cpp] view plain copy
  1. status_tInputChannel::openInputChannelPair(constString8&name,
  2. sp<InputChannel>&outServerChannel,sp<InputChannel>&outClientChannel){
  3. status_tresult;
  4. intserverAshmemFd=ashmem_create_region(name.string(),DEFAULT_MESSAGE_BUFFER_SIZE);
  5. if(serverAshmemFd<0){
  6. ......
  7. }else{
  8. result=ashmem_set_prot_region(serverAshmemFd,PROT_READ|PROT_WRITE);
  9. if(result<0){
  10. ......
  11. }else{
  12. //Dupthefiledescriptorbecausetheserverandclientinputchannelobjectsthat
  13. //arereturnedmayhavedifferentlifetimesbuttheysharethesamesharedmemoryregion.
  14. intclientAshmemFd;
  15. clientAshmemFd=dup(serverAshmemFd);
  16. if(clientAshmemFd<0){
  17. ......
  18. }else{
  19. intforward[2];
  20. if(pipe(forward)){
  21. ......
  22. }else{
  23. intreverse[2];
  24. if(pipe(reverse)){
  25. ......
  26. }else{
  27. String8serverChannelName=name;
  28. serverChannelName.append("(server)");
  29. outServerChannel=newInputChannel(serverChannelName,
  30. serverAshmemFd,reverse[0],forward[1]);
  31. String8clientChannelName=name;
  32. clientChannelName.append("(client)");
  33. outClientChannel=newInputChannel(clientChannelName,
  34. clientAshmemFd,forward[0],reverse[1]);
  35. returnOK;
  36. }
  37. ......
  38. }
  39. ......
  40. }
  41. }
  42. }
  43. ......
  44. }
在阅读这个函数之前,我们首先了解一下C++层的InputChannel的构造函数:

[cpp] view plain copy
  1. InputChannel::InputChannel(constString8&name,int32_tashmemFd,int32_treceivePipeFd,
  2. int32_tsendPipeFd):
  3. mName(name),mAshmemFd(ashmemFd),mReceivePipeFd(receivePipeFd),mSendPipeFd(sendPipeFd){
  4. ......
  5. }
为了创建一个InputChannel,我们需要准备四个参数,一个是输入通道的名称name,一个是 匿名共享内存 文件描述符,一个是管道的读端文件描述符,一个是管道的写端文件描述符。在上面的openInputChannelPair函数,输入通道的名称已经作为参数传递进来,因此,还需要创建匿名共享内存文件,还有管道。这里需要创建两个管道,一个称为前向管道(forward pipe),一个称为反向管道(reverse pipe),它们交叉使用在Server端和Client端的InputChannel中,这样就使入Server和Client可以互相通信了。

具体来说,Server端和Client端的InputChannel分别是这样构成的:

Server Input Channel: ashmem - reverse(read) - forward(write)

Client Input Channel: ashmem - forward(read) - reverse(write)
前面我们在Android应用程序消息处理机制(Looper、Handler)分析一文中学习Android应用程序的消息处理机制时知道,管道可以用作进程间通信,其中一个进程在管道的读端等待新的内空可读,另一个进程在管道的写端写入新的内容以唤醒在管道读端等待的进程,这样就实现了进程间通信。在我们这个情景中,Client端可以在前向管道(forward pipe)的读端睡眠等待新的内容可读,而Server端可以通过向前向管道(forward pipe)的写端写入新的内容来唤醒Client端,同样,把前向管道(forward pipe)换成反向管道(reverse pipe),也能实现Client端唤醒Server端。在后面我们分析InputDispatcher分发键盘消息时,会看到它们的用法。

有了这些背景知识后,相信上面的openInputChannelPair的代码就容易理解了,这里就不再详述了。

创建好了这两个输入通道后,回到Step 11中的WindowManagerService.addWindow函数中,一方面它把刚才创建的Client端的输入通道通过outInputChannel参数返回到应用程序中:

[java] view plain copy
  1. inputChannels[1].transferToBinderOutParameter(outInputChannel);

另一方面,它还要把刚才创建的Server端的输入通道注册到InputManager中:

[java] view plain copy
  1. mInputManager.registerInputChannel(win.mInputChannel);
Step 15. InputManager.registerInputChannel

这个函数定义在frameworks/base/services/java/com/android/server/InputManager.java文件中:

[java] view plain copy
  1. publicclassInputManager{
  2. ......
  3. /**
  4. *Registersaninputchannelsothatitcanbeusedasaninputeventtarget.
  5. *@paraminputChannelTheinputchanneltoregister.
  6. */
  7. publicvoidregisterInputChannel(InputChannelinputChannel){
  8. if(inputChannel==null){
  9. thrownewIllegalArgumentException("inputChannelmustnotbenull.");
  10. }
  11. nativeRegisterInputChannel(inputChannel,false);
  12. }
  13. ......
  14. }
它通过调用本地方法nativeRegisterInputChannel来执行进一步的操作。

Step 16.InputManager.nativeRegisterInputChannel

这个函数定义在frameworks/base/services/jni/com_android_server_InputManager.cpp 文件中:

[cpp] view plain copy
  1. staticvoidandroid_server_InputManager_nativeRegisterInputChannel(JNIEnv*env,jclassclazz,
  2. jobjectinputChannelObj,jbooleanmonitor){
  3. ......
  4. sp<InputChannel>inputChannel=android_view_InputChannel_getInputChannel(env,
  5. inputChannelObj);
  6. ......
  7. status_tstatus=gNativeInputManager->registerInputChannel(
  8. env,inputChannel,inputChannelObj,monitor);
  9. ......
  10. }
这里首先通过Java层的InputChannel对象获得C++层的InputChannel对象,它们之间的对应关系是在前面的Step 13中设置好的,接着调用NativeInputManager的registerInputChannel执行进一步的操作。

Step 17. NativeInputManager.registerInputChannel

这个函数定义在frameworks/base/services/jni/com_android_server_InputManager.cpp 文件中:

[cpp] view plain copy
  1. status_tNativeInputManager::registerInputChannel(JNIEnv*env,
  2. constsp<InputChannel>&inputChannel,jobjectinputChannelObj,boolmonitor){
  3. ......
  4. status=mInputManager->getDispatcher()->registerInputChannel(inputChannel,monitor);
  5. ......
  6. }
这个函数主要是调用了InputDispatcher的registerInputChannel来真正执行注册输入通道的操作。

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

[cpp] view plain copy
  1. status_tInputDispatcher::registerInputChannel(constsp<InputChannel>&inputChannel,boolmonitor){
  2. ......
  3. {//acquirelock
  4. AutoMutex_l(mLock);
  5. if(getConnectionIndexLocked(inputChannel)>=0){
  6. LOGW("Attemptedtoregisteralreadyregisteredinputchannel'%s'",
  7. inputChannel->getName().string());
  8. returnBAD_VALUE;
  9. }
  10. sp<Connection>connection=newConnection(inputChannel);
  11. status_tstatus=connection->initialize();
  12. if(status){
  13. LOGE("Failedtoinitializeinputpublisherforinputchannel'%s',status=%d",
  14. inputChannel->getName().string(),status);
  15. returnstatus;
  16. }
  17. int32_treceiveFd=inputChannel->getReceivePipeFd();
  18. mConnectionsByReceiveFd.add(receiveFd,connection);
  19. if(monitor){
  20. mMonitoringChannels.push(inputChannel);
  21. }
  22. mLooper->addFd(receiveFd,0,ALOOPER_EVENT_INPUT,handleReceiveCallback,this);
  23. runCommandsLockedInterruptible();
  24. }//releaselock
  25. returnOK;
  26. }
这个函数首先会通过getConnectionIndexLocked检查从参数传进来的InputChannel是否已经注册过了,如果已经注册过了,就返回一个BAD_VALUE值了,否则的话,就会创建一个Connection对象来封装即将要注册的inputChannel,我们可以不关心这个Connection对象的实现,接着还通过调用inputChannel->getReceivePipeFd获得一个管
道的读端描述符。回忆一下Step 14中的InputChannel.openInputChannelPair函数,我们创建了一个Server端的InputChannel,就是对应这里的inputChannel了,这个inputChannel的Receive Pipe Fd就是我们前面说的反向管道的读端描述符了。有了这个Receive Pipe Fd后,就以它作为Key值来把前面创建的Connection对象保存在InputDispatcher中,这样就基本完成键盘消息接收通道的注册了。但是,注册的工作还未完成,最后,还要把这个Receive Pipe Fd添加到InputDispatcher的成员变量mLooper中去,这里的成员变量mLooper的类型为Looper,我们在前面介绍InputManager的启动过程的Step 15中已经见过了,这里就不再详述了,不过这里仍然值得介绍一下它的addFd函数。

在前面一篇文章Android应用程序消息处理机制(Looper、Handler)分析中,我们在介绍到Android应用程序的消息循环一节时,曾经说过,在Looper类内部,会创建一个管道,然后Looper会睡眠在这个管道的读端,等待另外一个线程来往这个管道的写端写入新的内容,从而唤醒等待在这个管道读端的线程,除此之外,Looper还可以同时睡眠等待在其它的文件描述符上,因为它是通过Linux系统的epoll机制来批量等待指定的文件有新的内容可读的。这些其它的文件描述符就是通过Looper类的addFd成函数添加进去的了,在添加的时候,还可以指定回调函数,即当这个文件描述符所指向的文件有新的内容可读时,Looper就会调用这个hanldeReceiveCallback函数,有兴趣的读者可以自己研究一下Looper类的addFd函数的实现,它位于frameworks/base/libs/utils/Looper.cpp文件中。

分析到这里,Server端的InputChannel就注册完成了。回忆一下前面介绍InputManager启动过程的Step 14,这时InputDispatcherThread同时睡眠在InputDispatcher的成员变量mLooper内部的管道的读端以及这里的Server端InputChannel里面的反向管道的读端上,mLooper内部的管道的读端等待键盘事件的发生而被唤醒,而Server端InputChannel里面的反向管道的读端等待Client端InputChannel里面的反向管道的写端被写入新的内容而被唤醒。

Server端的InputChannel注册完成后,回到Step 11中的WindowManagerService.addWindow函数,接下来就是把Client端的InputChannel转换成addWindow的参数outInputChannel中,然后返回到Step 1中的ViewRoot.setView函数中,继续执行Client端的InputChannel的注册过程,即为应用程序这一侧注册键盘消息接收通道:

[java] view plain copy
  1. if(viewinstanceofRootViewSurfaceTaker){
  2. mInputQueueCallback=
  3. ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue();
  4. }
  5. if(mInputQueueCallback!=null){
  6. mInputQueue=newInputQueue(mInputChannel);
  7. mInputQueueCallback.onInputQueueCreated(mInputQueue);
  8. }else{
  9. InputQueue.registerInputChannel(mInputChannel,mInputHandler,
  10. Looper.myQueue());
  11. }

这里的变量view一般不为RootViewSurfaceTaker的实例,因此,最后会执行下面语句:

[java] view plain copy
  1. InputQueue.registerInputChannel(mInputChannel,mInputHandler,
  2. Looper.myQueue());
它调用InputQueue的registerInputChannel函数为应用程序注册键盘消息接收通道,这里的mInputChannel即为我们在前面Step 14中创建的Client端的InputChannel;Looper.myQueue函数返回的便是应用程序主线程的消息队列,具体可以参考前面一篇文章 Android应用程序消息处理机制(Looper、Handler)分析 ;参数mInputHandler是一个回调对象,当有键盘事件发生时,这个mInputHandler的handleKey函数就会被调用,在后面的分析中,我们将会看到。

Step 19. InputQueue.registerInputChannel

这个函数定义在frameworks/base/core/java/android/view/InputQueue.java文件中:

[java] view plain copy
  1. publicfinalclassInputQueue{
  2. ......
  3. publicstaticvoidregisterInputChannel(InputChannelinputChannel,InputHandlerinputHandler,
  4. MessageQueuemessageQueue){
  5. ......
  6. synchronized(sLock){
  7. ......
  8. nativeRegisterInputChannel(inputChannel,inputHandler,messageQueue);
  9. }
  10. }
  11. ......
  12. }
这个函数调用本地方法nativeRegisterInputChannel函数来执行进一步的操作。

Step 20.InputQueue.nativeRegisterInputChannel

这个函数定义在frameworks/base/core/jni/android_view_InputQueue.cpp文件中:

[cpp] view plain copy
  1. staticvoidandroid_view_InputQueue_nativeRegisterInputChannel(JNIEnv*env,jclassclazz,
  2. jobjectinputChannelObj,jobjectinputHandlerObj,jobjectmessageQueueObj){
  3. status_tstatus=gNativeInputQueue.registerInputChannel(
  4. env,inputChannelObj,inputHandlerObj,messageQueueObj);
  5. ......
  6. }
这里继续调用NativeInputQueue的registerInputChannel函数来执行真正的键盘消息接收通道的工作。

Step 21. NativeInputQueue.registerInputChannel

这个函数定义在frameworks/base/core/jni/android_view_InputQueue.cpp文件中:

[cpp] view plain copy
  1. status_tNativeInputQueue::registerInputChannel(JNIEnv*env,jobjectinputChannelObj,
  2. jobjectinputHandlerObj,jobjectmessageQueueObj){
  3. sp<InputChannel>inputChannel=android_view_InputChannel_getInputChannel(env,
  4. inputChannelObj);
  5. ......
  6. sp<Looper>looper=android_os_MessageQueue_getLooper(env,messageQueueObj);
  7. {//acquirelock
  8. AutoMutex_l(mLock);
  9. if(getConnectionIndex(inputChannel)>=0){
  10. LOGW("Attemptedtoregisteralreadyregisteredinputchannel'%s'",
  11. inputChannel->getName().string());
  12. returnBAD_VALUE;
  13. }
  14. uint16_tconnectionId=mNextConnectionId++;
  15. sp<Connection>connection=newConnection(connectionId,inputChannel,looper);
  16. status_tresult=connection->inputConsumer.initialize();
  17. if(result){
  18. LOGW("Failedtoinitializeinputconsumerforinputchannel'%s',status=%d",
  19. inputChannel->getName().string(),result);
  20. returnresult;
  21. }
  22. connection->inputHandlerObjGlobal=env->NewGlobalRef(inputHandlerObj);
  23. int32_treceiveFd=inputChannel->getReceivePipeFd();
  24. mConnectionsByReceiveFd.add(receiveFd,connection);
  25. looper->addFd(receiveFd,0,ALOOPER_EVENT_INPUT,handleReceiveCallback,this);
  26. }//releaselock
  27. ......
  28. returnOK;
  29. }
这里注册应用程序的InputChannel的逻辑和前面介绍的Step 18中在InputDispatcher中注册Server端的InputChannel是一样的,所不同的是,这里用的looper是应用程序主线程中的消息循环对象Looper,而添加到这个looper对象中的Receive Pipe Fd是前面在Step 14中创建的前向管道的读端文件描述符,而使用的回调函数是NativeInputQueue的成员函数handleReceiveCallback。

介绍到这里,应用程序注册键盘消息接收通道的过程就分析完成了。这个过程比较复杂,这里小结一下:

A. 即将会被激活的Activity窗口,会通知InputManager,它是当前激活的窗口,因此,一旦发生键盘事件的时候,InputManager就把这个键盘事件抛给这个Activity处理;

B. 应用程序会为这个Activity窗口和InputManager之间创建一个键盘消息接收通道,这个通道的一端由一个Server端的InputChannel构成,另一端由Client端的InputChannel构成,Server端的InputChannel注册在由InputManager所管理的InputDispatcher中,而Client端的InputChannel注册在由应用程序主线程的消息循环对象Looper中;

C. 注册在InputDispatcher中的InputChannel由一个反向管道的读端和一个前向管道的写端组成,而注册在应用程序主线程的消息循环对象Looper中的InputChannel由这个前向管道的读端和反向管道的写端组成,这种交叉结构使得当有键盘事件发生时,InputDispatcher可以把这个事件通知给应用程序。

应用程序注册好键盘消息接收通道后,接下来就开始分析InputManager分发键盘消息给应用程序的过程了。

3.InputManager分发键盘消息给应用程序的过程分析

在分析InputManager分发键盘消息给应用程序的过程之前,我们先假设现在没有键盘事件发生,因此,InputManager中的InputReader正在睡眠等待键盘事件的发生,而InputManager中的InputDispatcher正在等待InputReader从睡眠中醒过来并且唤醒它,而应用程序也正在消息循环中等待InputDispatcher从睡眠中醒过来并且唤醒它。这时候,用户按下键盘中的一个键,于是,一系列唤醒的事件就依次发生了,一直到应用程序中正在显示的Activity得到通知,有键盘事件发生了。我们先来看这个过程的序列图,然后再详细分析每一个步骤:

Step 1. InputReader.pollOnce

Step 2. EventHub.getEvent

这两个函数分别定义在frameworks/base/libs/ui/InputReader.cpp和frameworks/base/libs/ui/EventHub.cpp文件中,前面我们在分析InputManager的启动过程的Step 17和Step 18时,已经看到过这两个函数了。InputReaderThread线程会不民地循环调用InputReader.pollOnce函数来读入键盘事件,而实际的键盘事件读入操作是由EventHub.getEvent函数来进行的。如果当前没有键盘事件发生,InputReaderThread线程就会睡眠在EventHub.getEvent函数上,而当键盘事件发生后,就会把这个事件封装成一个RawEvent对象,然后返回到pollOnce函数中,执行process函数进一步处理:

[cpp] view plain copy
  1. voidInputReader::loopOnce(){
  2. RawEventrawEvent;
  3. mEventHub->getEvent(&rawEvent);
  4. ......
  5. process(&rawEvent);
  6. }
Step 3. InputReader.process

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

[cpp] view plain copy
  1. voidInputReader::process(constRawEvent*rawEvent){
  2. switch(rawEvent->type){
  3. caseEventHubInterface::DEVICE_ADDED:
  4. addDevice(rawEvent->deviceId);
  5. break;

更多相关文章

  1. Android线程间通信机制——深入理解 Looper、Handler、Message
  2. Android(安卓)Handler 异步消息处理机制的妙用 创建强大的图片加
  3. 很容易理解的Android消息机制分析
  4. Android(安卓)Handler 异步消息处理机制的妙用 创建强大的图片加
  5. Android消息处理-概念普及篇
  6. Handler 机制(一)
  7. Android的init过程(二):初始化语言(init.rc)解析
  8. Android(安卓)推送通知指南
  9. 箭头函数的基础使用

随机推荐

  1. android 动态修改菜单menu
  2. Android(安卓)Frame动画
  3. Linux下的Android(安卓)+ Eclipse环境搭
  4. Android(安卓)使用Instrumentation进行界
  5. 关于Eclipse无法在线安装升级Android(安
  6. BadTokenException Error In Android(安
  7. Android-显示传感器的值
  8. android界面设计(一)侧边栏的两种实现方式
  9. Android对话框使用详解(二)
  10. IDEA 快捷键 Android(安卓)Studio快捷键