Step 12.InputChannel.openInputChannelPair

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

  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文件中:

  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文件中:

  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的构造函数:

  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参数返回到应用程序中:

  1. inputChannels[1].transferToBinderOutParameter(outInputChannel);

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

  1. mInputManager.registerInputChannel(win.mInputChannel);

Step 15. InputManager.registerInputChannel

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

  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 文件中:

  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 文件中:

  1. status_tNativeInputManager::registerInputChannel(JNIEnv*env,
  2. constsp<InputChannel>&inputChannel,jobjectinputChannelObj,boolmonitor){
  3. ......
  4. status=mInputManager->getDispatcher()->registerInputChannel(inputChannel,monitor);
  5. ......
  6. }

这个函数主要是调用了InputDispatcher的registerInputChannel来真正执行注册输入通道的操作。

更多相关文章

  1. C语言函数以及函数的使用
  2. android客户端利用sokcet通信和向Java服务端发请求,Java服务端把
  3. Android Layout布局文件里的android:layout_height等属性为什么
  4. Android 自动编译、打包生成apk文件 3 - 使用SDK Ant方式

随机推荐

  1. Android aidl通信详解
  2. Android(安卓)给View添加一个点击的水波
  3. 安装android studio遇到的问题及解决方案
  4. 解决使用android studio中的git update后
  5. 【乱】乱,乱,乱,android真乱!
  6. Android 辅助功能(无障碍)自定义开发类似微
  7. Android(安卓)Intent 常用的Flag
  8. 如何降低android应用程序的耗电量
  9. Android中Dialog设置外部点击事件
  10. android打开存储卡(TF卡\SD卡)中的sqlit