Android应用程序键盘(Keyboard)消息处理机制分析(11)
16lz
2021-01-23
Step 8.NativeInputManager.setInputWindows 这个函数定义在frameworks/base/services/jni/com_android_server_InputManager.cpp文件中:
- voidNativeInputManager::setInputWindows(JNIEnv*env,jobjectArraywindowObjArray){
- Vector<InputWindow>windows;
- jsizelength=env->GetArrayLength(windowObjArray);
- for(jsizei=0;i<length;i++){
- jobjectinputTargetObj=env->GetObjectArrayElement(windowObjArray,i);
- if(!inputTargetObj){
- break;//foundnullelementindicatingendofusedportionofthearray
- }
- windows.push();
- InputWindow&window=windows.editTop();
- boolvalid=populateWindow(env,inputTargetObj,window);
- if(!valid){
- windows.pop();
- }
- env->DeleteLocalRef(inputTargetObj);
- }
- mInputManager->getDispatcher()->setInputWindows(windows);
- }
- voidInputDispatcher::setInputWindows(constVector<InputWindow>&inputWindows){
- ......
- {//acquirelock
- AutoMutex_l(mLock);
- //Clearoldwindowpointers.
- sp<InputChannel>oldFocusedWindowChannel;
- if(mFocusedWindow){
- oldFocusedWindowChannel=mFocusedWindow->inputChannel;
- mFocusedWindow=NULL;
- }
- mWindows.clear();
- //Loopovernewwindowsandrebuildthenecessarywindowpointersfor
- //trackingfocusandtouch.
- mWindows.appendVector(inputWindows);
- size_tnumWindows=mWindows.size();
- for(size_ti=0;i<numWindows;i++){
- constInputWindow*window=&mWindows.itemAt(i);
- if(window->hasFocus){
- mFocusedWindow=window;
- break;
- }
- }
- ......
- }//releaselock
- ......
- }
- mInputChannel=newInputChannel();
- try{
- res=sWindowSession.add(mWindow,mWindowAttributes,
- getHostVisibility(),mAttachInfo.mContentInsets,
- mInputChannel);
- }catch(RemoteExceptione){
- ......
- }finally{
- ......
- }
- publicclassWindowManagerServiceextendsIWindowManager.Stub
- implementsWatchdog.Monitor{
- ......
- privatefinalclassSessionextendsIWindowSession.Stub
- implementsIBinder.DeathRecipient{
- ......
- publicintadd(IWindowwindow,WindowManager.LayoutParamsattrs,
- intviewVisibility,RectoutContentInsets,InputChanneloutInputChannel){
- returnaddWindow(this,window,attrs,viewVisibility,outContentInsets,
- outInputChannel);
- }
- ......
- }
- ......
- }
- publicclassWindowManagerServiceextendsIWindowManager.Stub
- implementsWatchdog.Monitor{
- ......
- publicintaddWindow(Sessionsession,IWindowclient,
- WindowManager.LayoutParamsattrs,intviewVisibility,
- RectoutContentInsets,InputChanneloutInputChannel){
- ......
- WindowStatewin=null;
- synchronized(mWindowMap){
- ......
- win=newWindowState(session,client,token,
- attachedWindow,attrs,viewVisibility);
- ......
- if(outInputChannel!=null){
- Stringname=win.makeInputChannelName();
- InputChannel[]inputChannels=InputChannel.openInputChannelPair(name);
- win.mInputChannel=inputChannels[0];
- inputChannels[1].transferToBinderOutParameter(outInputChannel);
- mInputManager.registerInputChannel(win.mInputChannel);
- }
- ......
- }
- ......
- }
- ......
- }
这里的outInputChannel即为前面在Step 1中创建的InputChannel,它不为NULL,因此,这里会通过InputChannel.openInputChannelPair函数来创建一对输入通道,其中一个位于WindowManagerService中,另外一个通过outInputChannel参数返回到应用程序中:
- inputChannels[1].transferToBinderOutParameter(outInputChannel);
创建输入通道之前,WindowManagerService会为当前Activity窗口创建一个WindowState对象win,用来记录这个Activity窗口的状态信息。当创建这对输入管道成功以后,也会把其中的一个管道保存在这个WindowState对象win的成员变量mInputChannel中,后面要注销这个管道的时候,就是从这个WindownState对象中取回这个管道的:
- win.mInputChannel=inputChannels[0];
接下来我们就看一下InputChannel.openInputChannelPair函数的实现。
更多相关文章
- 输入法与窗口交互
- Android图形显示之本地窗口
- Android 抽象回调函数以及接口回调更新UI
- Android Render(一)Activity窗口构成和绘制解析
- Android 依赖注入函数库Roboguice(一)
- WindowManager实现悬浮窗口&可自由移动的悬浮窗口
- Android 的窗口管理系统 (View, Canvas, WindowManager)