主要原因是和导航栏和屏幕最下方3个按键的属性配置有关,因为在PhoneWindowManager中调用方法performHapticFeedbackLw(null, HapticFeedbackConstants.VIRTUAL_KEY, false);会去判断policyFlags & WindowManagerPolicy.FLAG_VIRTUAL,policyFlags这一属性。

下面介绍一下事件的处理流程:

1.对象的创建

InputManagerService的初始化

InputDispatcher和InputReader对象,再调用initialize方法分别创建了与InputDispatcher和InputReader对应的线程InputDispatcherThread和InputReaderThread对象

2.事件的传递

RawEvent是待发出去的事件,InputReader经由QueueInputListener就可以关联到InputDispatch,最后由InputDispatch将事件处理或分发出去。

  1. InputManager::InputManager(
  2. const sp<EventHubInterface>& eventHub,
  3. const sp<InputReaderPolicyInterface>& readerPolicy,
  4. const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
  5. mDispatcher = new InputDispatcher(dispatcherPolicy);
  6. mReader = new InputReader(eventHub, readerPolicy, mDispatcher);
  7. initialize();
  8. }
  1. InputReader::InputReader(const sp<EventHubInterface>& eventHub,
  2. const sp<InputReaderPolicyInterface>& policy,
  3. const sp<InputListenerInterface>& listener) :
  4. mContext(this), mEventHub(eventHub), mPolicy(policy),
  5. mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
  6. mConfigurationChangesToRefresh(0) {
  7. mQueuedListener = new QueuedInputListener(listener);
  8. ...
  9. }

InputManager里创建了InputDispatch和InputReader,就是在此时将这两者关联了起来。以mDispatcher为参数创建了InputReader,mDispatcher就是InputReader的实际监听者,那么InputReader一收到事件就要主动通知监听者mDispatcher把这个事件处理掉。

2.1. InputReader

InputReader会创建InputReaderThread线程,threadloop()返回true表示该线程会一直执行loopOnce().

  1. bool InputReaderThread::threadLoop() {
  2. mReader->loopOnce();
  3. return true;
  4. }
  1. void InputReader::loopOnce() {
  2. size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
  3. { // acquire lock
  4. AutoMutex _l(mLock);
  5. if (count) {
  6. processEventsLocked(mEventBuffer, count);
  7. }
  8. } // release lock
  9. // Flush queued events out to the listener.
  10. // This must happen outside of the lock because the listener could potentially call
  11. // back into the InputReader's methods, such as getScanCodeState, or become blocked
  12. // on another thread similarly waiting to acquire the InputReader lock thereby
  13. // resulting in a deadlock.  This situation is actually quite plausible because the
  14. // listener is actually the input dispatcher, which calls into the window manager,
  15. // which occasionally calls into the input reader.
  16. mQueuedListener->flush();
  17. }

loopOnce()先用getEvents()从设备文件中读出事件数据,然后进行一些处理。这种处理包括根据事件的类型(EV_KEY, EV_ABS, EV_SW)使用不同的InputMapper来处理事件,最后将事件放到列队中。最后的flush()会调用监听者(实际上是InputDispatch)来处理掉列队中所有的事件。

3. 事件的策略标志policyFlags

Android里事件有许多标志,如flags, policyFlags, metaState等,其中policyFlags用于决定一个事件的策略行为。比如一些特殊按键POWER,VOLUME,HOME等的处理行为在上层策略上不同,就需要对policyFlags设置不同的标志位。

frameworks/base/include/ui/Input.h 文件中定义了policyFlags所有的标志位。

有两个地方会设置policyFlags:

1. EventHub 对每个设备都有一个struct Device结构,每个Device又有自己的KeyMap。EventHub::openDeviceLocked()打开一个设备时会使用loadKeyMapLocked()加载并解析keylayout文件。关于keylayout可以看官方的说明 http://source.android.com/tech/input/key-layout-files.html

比如 frameworks/base/data/keyboards/Generic.kl 这个文件中有:

  1. key 113   VOLUME_MUTE
  2. key 114   VOLUME_DOWN
  3. key 115   VOLUME_UP
  4. key 116   POWER             WAKE
 
frameworks/base/libs/ui/KeyLayoutMap.cpp

frameworks/base/include/ui/KeycodeLabels.h

  1. // NOTE: If you edit these flags, also edit policy flags in Input.h.
  2. static const KeycodeLabel FLAGS[] = {
  3. { "WAKE", 0x00000001 },
  4. { "WAKE_DROPPED", 0x00000002 },
  5. { "SHIFT", 0x00000004 },
  6. { "CAPS_LOCK", 0x00000008 },
  7. { "ALT", 0x00000010 },
  8. { "ALT_GR", 0x00000020 },
  9. { "MENU", 0x00000040 },
  10. { "LAUNCHER", 0x00000080 },
  11. { "VIRTUAL", 0x00000100 },
  12. { "FUNCTION", 0x00000200 },
  13. { NULL, 0 }
  14. };
需要注意的是,在 frameworks/base/include/ui/Input.h 中定义了所有策略相关的标志,且需要和FLAGS[]里的值保持一致,不然就乱套了。

EventHub::getEvents()读到一个事件之后,会从按照scancode从keylayout中得到相应的策略标志,此时记录于event->flags中。按下POWER键时,getEvents()收到的scanCode是116,再从keyMap中用扫一遍,POWER键有"WAKE"属性,则设置0x00000001标志位。

  1. if (iev.type == EV_KEY && device->keyMap.haveKeyLayout()) {
  2. status_t err = device->keyMap.keyLayoutMap->mapKey(iev.code,
  3. &event->keyCode, &event->flags);
  4. LOGV("iev.code=%d keyCode=%d flags=0x%08x err=%d\n",
  5. iev.code, event->keyCode, event->flags, err);
  6. }
 

2. InputDispatcher 中也会再做一些判断来设置policyFlags标志。

android O 打开设置->声音->“点按时震动问题”的更多相关文章

  1. Android判断网络是否打开,并打开设置网络界面

    由于Android的SDK版本不同所以里面的API和设置方式也是有少量变化的,尤其是在Android 3.0 及后面的版本,UI和显示方式也发生了变化,现在就以打开网络设置为例,同大家分享一下: 1. ...

  2. Android 情景模式设置

    情景模式的设置大家应当相当熟悉了,但是在Android中如何通过自己的程序进行情景模式的设置呢,情景模式分为多种多种,即可以使用系统自带的,也可 以使用自定义的,但是在开发某些程序时,可能需要在程序中 ...

  3. 【转】Android 当打开“开发者模式”中的“不保留活动”后,程序应当怎么保持正常运行

    当打开这个设置以后,程序的Activity会自动销毁,每次返回的时候就会不断重oncreate,此时伴随的问题多多. 参考文档:http://www.bubuko.com/infodetail-960 ...

  4. eclipse的android智能提示设置

    eclipse的android智能提示设置 分类: android 技术2011-12-07 23:13 3069人阅读 评论(0) 收藏 举报 eclipseandroidtriggersjavaf ...

  5. Xamarin.Android 入门之:Android API版本设置

    一.引言 Xamarin.Android有几个Android API级别设置,确定多个版本的Android应用程序的兼容性.本博客解释了这些设置意味着什么,如何配置它们,以及它们在运行时对您的应用程序 ...

  6. Android 当打开“开发人员模式”中的“不保留活动”后,程序应当怎么保持正常执行

    Android 当打开"开发人员模式"中的"不保留活动"后,程序应当怎么保持正常执行咧. .? 在这几天,我一直在纠结这个问题.从发现,程序出现这个问题,是由于 ...

  7. react-native 打开设置界面

    iOS iOS打开设置还是比较简单的,使用Linking组件即可: Linking.openURL('app-settings:') .catch(err => console.log('err ...

  8. 【转】Android中通知的提示音、震动和LED灯效果小例子

    通知(Notification)是 Android 系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现.发出一条通知后,手机最上方 ...

  9. android intent打开各种文件的方法

    android intent打开各种文件的方法   1./**  * 检测是否安装了某个软件  *   * @param pkgName "com.bill99.kuaishua" ...

随机推荐

  1. Oracle11g: datetime

    --上一月,上一年 select add_months(sysdate,-1) last_month,add_months(sysdate,-12) last_year from dual; --下一 ...

  2. AngularJS处理服务器端返回的JSON数据的格式问题

    用ng的$http服务发起ajax请求,php返回的JSON数据格式要正确! 一开始我的php页面是这样返回数据的: if($result){ $oid = mysqli_insert_id($con ...

  3. JS 事件绑定,监听,委托(代理)

    我们经常会遇到JavaScript的事件机制,例如,事件绑定.事件监听.事件委托(事件代理)等.这些名词是什么意思呢,有什么作用呢? 在JavaScript中,有三种常用的绑定事件的方法: 在DOM元 ...

  4. iOS-UIView指定圆角设置

    圆角设置可以指定左上.左下.右上.右下角:单个指定或多个指定. ///设置圆角[左上.右上角] - (void)setCircular{ UIBezierPath *maskPath = [UIBez ...

  5. 虚拟机安装macos 分辨率不正常修改不了,不能全屏如何解决

    1.苹果在OSX 10.11之后启动了一个System Integrity Protection (SIP),这个保护系统防止/library/preferences/systemconfigurat ...

  6. Centos 7 安装 ifconfig 管理命令

    1. 安装的需求背景 我们知道ifconfig 命令可以用于查看.配置.启用或禁用指定网络接口,如配置网卡的IP地址.掩码.广播地址.网关等,功能不可谓不丰富. 此命令的功能和windows系统的ip ...

  7. phpstorm设置编码格式

    phpstorm设置编码格式 默认: utf-8格式 设置方法: file -> settings -> Editor -> file encodng -> project e ...

  8. 模块简介:(logging)(re)(subprocess)

    ''' logging模块: logging的日志可以分为 debug():Detailed information, typically of interest only when diagnosi ...

  9. Django 简介

    一  MVC  与 MTV 模型 (1)MVC C: controller 控制器(url分发和视图函数) V: 存放html文件 M: model:数据库操作 Web服务器开发领域里著名的MVC模式 ...

  10. 微信小程序访问豆瓣api403问题解决方发法

    微信小程序访问豆瓣api403问题解决方法一览:通过豆瓣api可以获取很多电影.书籍等的数据信息.昨晚上用微信小程序请求豆瓣api,竟然被豆瓣拒绝了.(豆瓣设置了小程序的访问权限):下面就跟着小编一起 ...