10.7 android输入系统_Dispatcher线程情景分析_Reader线程传递事件和dispatch前处理

android输入系统C++最上层文件是com_android_serve_input_InputManagerService.cpp

global key:
按下按键,启动某个APP
可以自己指定,修改frameworks\base\core\res\res\xml\Global_keys.xml
假设它是AKEYCODE_TV

system key:
比如音量键 AKEYCODE_VOLUME_DOWN

user key:
其他按键,比如ABCD
AKEYCODE_A

reader线程把驱动上报的scancode根据.kl文件转换成keycode


android_system_code\frameworks\base\core\res\res\xml\Global_keys.xml
  A global key will NOT go to the foreground application and instead only ever be sent via targeted
  broadcast to the specified component. The action of the intent will be
  android.intent.action.GLOBAL_BUTTON and the KeyEvent will be included in the intent as
  android.intent.extra.KEY_EVENT.

 

三种按键的处理过程:

(1)Globalkey:AKEYCODE_TV

 NotifyKeyArgs args()//在InputReader.cpp中,使用收到的数据构造args

  getListener()->notifyKey(&args)//通知listener,这里的listener是在new InputReader的时候传入的mDispatcher对象,其是一个InputDispatcher类

    mPolicy->interceptKeyBeforeQueueing(&event,policyFlags)//稍加处理,其是调用phoneWindowManager.java中的同名函数,对于Globalkey,返回PASS_TO_USER,根据返回值设置policeFlags(表示是否把事件传给用户)

    needWake = enqueueInboundEventLocked(newEntry)//放入mInboundQueue队列,newEntry根据policyFlags被构造

    if(needWake){

      mLooper->wake()//唤醒Dispatcher线程

    }

(2)system key:AKEYCODE_VOLUME_DOWN

 NotifyKeyArgs args()//在InputReader.cpp中,使用收到的数据构造args

  getListener()->notifyKey(&args)//通知listener,这里的listener是在new InputReader的时候传入的mDispatcher对象,其是一个InputDispatcher类

    mPolicy->interceptKeyBeforeQueueing(&event,policyFlags)//稍加处理,其是调用phoneWindowManager.java中的同名函数,对于systemkey,分类处理,如果可以直接处理的话,处理它,设置返回!PASS_TO_USER;如果可以直接处理设置为PASS_TO_USER,,根据返回值设置policeFlags(表示是否把事件传给用户)

    needWake = enqueueInboundEventLocked(newEntry)//放入mInboundQueue队列,newEntry根据policyFlags被构造

    if(needWake){

      mLooper->wake()//唤醒Dispatcher线程

    }

(3)user key:AKEYCODE_A

 NotifyKeyArgs args()//在InputReader.cpp中,使用收到的数据构造args

  getListener()->notifyKey(&args)//通知listener,这里的listener是在new InputReader的时候传入的mDispatcher对象,其是一个InputDispatcher类

    mPolicy->interceptKeyBeforeQueueing(&event,policyFlags)//稍加处理,其是调用phoneWindowManager.java中的同名函数,对于普通按键,返回PASS_TO_USER,根据返回值设置policeFlags(表示是否把事件传给用户)

    needWake = enqueueInboundEventLocked(newEntry)//放入mInboundQueue队列,newEntry根据policyFlags被构造

    if(needWake){

      mLooper->wake()//唤醒Dispatcher线程

    }

 

10. Dispatcher线程情景分析_dispatch前处理

(1)、命令队列为空时//if(!havaCommandsLocked){dispatchOnceInndrLocked(&nextWakeupTime)}

a、从mInboundQueue取出事件

b、用它来生成命令放入命令队列或者丢弃(对于!PASS_TO_USER的事件)

d、执行完命令后,对于经过处理的事件:Globalkey丢弃、Systemkey丢弃、userkey找到target后Dispatcher分发

(2)命令队列友数据时//if(runCommandsLockedInterruptible()){nextWakeupTime = LONG_LONG_MIN}

c、执行命令:Globalkey发广播、Systemkey直接处理、userkey不处理

 

情景分析:

(1)policyFlags = !PASS_TO_USER(不传给APP)

dispatchOnceInndrLocked()//从mInboundQueue取出事件

  DropReason dropReason = DROP_REASON_NOT_DROPPED//初始化为不丢弃

  if(!(mPendingEvent->policyFlags & POLLCY_FLAG_PASS_TO_USER))//数据的flag为!PASS_TO_USER

    dropReason = DROP_REASON_POLLCY //设置丢弃标准

  dispatchKeyLocked()//如果是按键类事件,执行该函数

    if(*dropReason  !=DROP_REASON_NOT_DROPPED){

      setInjectionResultLocked();

      return true;//dropReason表示放弃该数据的时候直接发回true

    }

(2)policyFlags == PASS_TO_USER

如果flags是PASS_TO_USER,构造命令把其放入命令队列

nextWakeupTime =LONG_LONG_MIN 表示Dispatch线程不会休眠,会再次执行dispatchOnce函数

 

 

processKey//Reader线程

  getListener()->notifyKey(&args)

    polictFlags |= POLICY_FLAG_TRUSTED

    event.initialize()

    mPolicy->interceptKeyBeforeQueueing(&event,policyFlags)//放入队列前先稍加处理:分类(Global输入/System输入/User输入)、处理紧急事件(比如来电、按音量、静音)

      policyFlags |= POLICY_FLAG_INTERACTIVE

      jobject keyEventObjg = android_view_keyEvent_fromNative(env,keyEvent)

      wmActions = env->CalllntMethod(...gServiceClassInfo.interceptKeyBeforeQueueing...)

        mWindowManagerCallBacks.interceptKeyBeforeQueueing(event,policyFlags)

          interceptKeyBeforeQueueing

      handleInterceptActions(...)

        policyFlags |= POLLCY_FLAG_PASS_TO_USER

    KeyEntry* newEntry = new KeyEntry()

    enqueueInboundEventLocked(newEntry)

      mInboundQueue.enqueueAtTail(entry)//InputReader线程把数据放入mInboundQueue

    mLooper->wake()

threadLoop

  dispatchOnce

    dispatchOnceInnerLocked(&nextWakeupTime)//从mInboundQueue取出数据生成命令

      mPendingEvent = mInboundQueue.dequeueAtHead()//从mInboundQueue从取出数据

      KeyEntry* typedEntry = static_case<KeyEntry*>(mPendingEvent)

      dispatchKeyLocked

        postCommandLocked(&InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible)//对于Global/System按键处理:放入mcommandQueue命令队列,依次处理

        findFocusedWindowTargetsLocked

        dispatchEventLocked

          prepareDispatchCycleLocked

            enqueueDispatchEntriesLocked

              enqueueDispatchEntryLocked

                connection->outboundQueue.enqueueAtTail//

对于User按键放入队列,查找目标APP,得到connection,放人APP里面的outboundQueue

              startDispatchCycleLocked//从outboundQueue取出事件,通过connection发给APP

    runCommandsLockedInterruptible()//执行命令,命令就是doInterceptKeyBeforeDispatchingLockedInterruptible函数

    mLooper->pollOnce()

    

posted on 2018-06-10 22:06  拉风摊主  阅读(383)  评论(0编辑  收藏  举报

导航