Android Input framework(一)

 

1 InputFramework概述

Android输入系统在整个图形系统框架中扮演了很重要的角色,主要负责用户消息的管理,具体职责包括以下几个方面:

 

1、 从底层驱动中获取各种原始的用户消息,包括按键、触摸屏、鼠标、滚迹球等用户事件消息。

2、 最原始消息进行预处理,包括两个方面:一方面,将消息转化成系统可以处理的消息事件;另一方面,处理一些特殊的事件,比如HOMEMENUPOWER键等处理。

3、 将处理后的消息事件分发到各个应用进程,这个需要使用IPC机制,Android系统使用管道来进行消息的传递。

 

Android系统使用InputManager类来管理消息,而具体的功能则是通过InputReaderThreadInputDispatcherThread两个线程来实现。其中InputReaderThread线程负责消息的读取,而

InputDispatcherThread则负责消息的预处理和分发到各个应用进程中。输入系统的整体框架如下图所示:

Android <wbr>Input <wbr>Framework(一)

从框图中可以看出,Android输入系统通过EventHub收集输入设备的原始数据,InputReader调用接口读取EventHub中获取的数据,然后通知InputDispatcher数据已经准备好,InputDispatcher获得数据回调InputManager的接口间接回调WMS 中的InputMonitor对输入消息进行处理,如果WMS没有消耗掉该消息,则InputDispatcher会将该消息通过管道的方式,直接发送到应用进程中,当前焦点应用的ViewrootImpl会收到该消息,并对消息进行分发处理,最终将其发送到对应的View对象中进行界面响应。

 

1 NativeInputManager初始化

WindowManagerService构造函数中,经过JNI调用完成了NativeInputManager的初始化,初始化工作有如下几点。

1.1 调用时序图

Android <wbr>Input <wbr>Framework(一)

1.2 类图对象关系

Android <wbr>Input <wbr>Framework(一)

1.3Native层注册java层的CallBacks回调接口

InputManagerService类中定义了一个Callback接口:

  publicinterface Callbacks {

       public void notifyConfigurationChanged();

       public void notifyLidSwitchChanged(long whenNanos, booleanlidOpen);

       public void notifyInputChannelBroken(InputWindowHandleinputWindowHandle);

       public long notifyANR(InputApplicationHandleinputApplicationHandle,

               InputWindowHandle inputWindowHandle);

       public int interceptKeyBeforeQueueing(KeyEvent event, intpolicyFlags, boolean isScreenOn);

       public int interceptMotionBeforeQueueingWhenScreenOff(intpolicyFlags);

       public long interceptKeyBeforeDispatching(InputWindowHandlefocus,

               KeyEvent event, int policyFlags);

       public KeyEvent dispatchUnhandledKey(InputWindowHandlefocus,

               KeyEventevent, int policyFlags);

       public int getPointerLayer();

   }

InputMonitor实现了InputManagerService.Callbacks接口,在WindowManagerService的构造函数中创建了InputMonitor对象,并以mInputMonitor作为参数创建InputManagerService的对象,在InputManagerService构造函数中,将mInputMonitor作为参数调用了JNI函数nativeInit(),将回调接口传到JNI层,在需要的时候,JNI在回调mInputMonitor中的函数,实现数据才传递。

class InputMonitorimplements InputManagerService.Callbacks{

   ……

}

privateWindowManagerService(Context context, PowerManagerServicepm,

           boolean haveInputMethods, boolean showBootMsgs, boolean onlyCore){

final InputMonitormInputMonitor = new InputMonitor(this);

mInputManager = newInputManagerService(context, mInputMonitor);

}

 

1.4 创建InputDispatcherInputReader线程

Android <wbr>Input <wbr>Framework(一)

InputManagerService的构造函数中,调用了

<

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值