2.2.1 硬件中断
硬件中断这里就简单介绍一些,操作系统对硬件事件的接收是通过中断来进行的。
内核启动的时候会在中断描述符表中对中断类型以及对应的处理方法的地址进行注册。
当有中断的时候,就会调用对应的处理方法,把对应的事件写入到设备节点里。
2.2.2 InputManagerService 做的事情
InputManagerService 是用来处理 Input 事件的,Java 侧的 InputManagerService 就是 C++ 代码的一个封装,以及提供了一些 callback 用来传递事件到 Java 层。
我们看一下 native 侧的 InputManagerService 初始化代码。
NativeInputManager::NativeInputManager(jobject contextObj,
jobject serviceObj, const sp& looper) :
mLooper(looper), mInteractive(true) {
// …
sp eventHub = new EventHub();
mInputManager = new InputManager(eventHub, this, this);
}
主要做的两件事:
- 初始化 EventHub
EventHub::EventHub(void) {
// …
mINotifyFd = inotify_init();
int result = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);
result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &eventItem);
result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
}
EventHub 的作用是用来监控设备节点是否有更新。
- 初始化 InputManager
void InputManager::initialize() {
mReaderThread = new InputReaderThread(mReader);
mDispatcherThread = new InputDispatcherThread(mDispatcher);
}
InputManager 里初始化了 InputReaderThread 和 InputDispatcherThread 两个线程,一个用来读取事件,一个用来派发事件。
2.2.3 InputReaderThread 做的事情
bool InputReaderThread::threadLoop() {
mReader->loopOnce();
return true;
}
void InputReader::loopOnce() {
// 从 EventHub 获取事件
size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
// 处理事件
processEventsLocked(mEventBuffer, count);
// 事件发送给 InputDispatcher 去做分发
mQueuedListener->flush();
}
这里代码比较多,做一些省略。
InputReaderThread 里做了三件事情:
-
从 EventHub 获取事件
-
处理事件,这里事件有不同的类型,会做不同的处理和封装
-
把事件发送给 InputDispatcher
2.2.4 InputDispatcherThread 做的事情
bool InputDispatcherThread::threadLoop() {
mDispatcher->dispatchOnce(); // 内部调用 dispatchOnceInnerLocked
return true;
}
void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) {
// 从队列中取出一个事件
mPendingEvent = mInboundQueue.dequeueAtHead();
// 根据不同的事件类型,进行不同的操作
switch (mPendingEvent->type) {
case EventEntry::TYPE_CONFIGURATION_CHANGED: {
// …
case EventEntry::TYPE_DEVICE_RESET: {
// …
case EventEntry::TYPE_KEY: {
// …
case EventEntry::TYPE_MOTION: {
// 派发事件
done = dispatchMotionLocked(currentTime, typedEntry,
&dropReason, nextWakeupTime);
break;
}
}
上面通过 dispatchMotionLocked 方法派发事件,具体的函数调用过程省略如下:
dispatchMotionLocked -> dispatchEventLocked -> prepareDispatchCycleLocked -> enqueueDispatchEntriesLocked -> startDispatchCycleLocked -> publishMotionEvent -> InputChannel.sendMessage
其中会找到当前合适的 Window,然后调用 InputChannel 去发送事件。
这里的 InputChannel 对应的是 ViewRootImpl 里的 InputChannel。
至于中间的怎么做的关联,这里就先不做分析,整个代码比较长,而且对于流程的掌握影响不大。
2.2.5 WindowInputEventReceiver 接受事件并进行分发
在 ViewRootImpl 里有一个 WindowInputEventReceiver 用来接受事件并进行分发。
InputChannel 发送的事件最终都是通过 WindowInputEventReceiver 进行接受。
WindowInputEventReceiver 是在 ViewRootImpl.setView 里面初始化的,setView 的调用是在 ActivityThread.handleResumeActivity -> WindowManagerGlobal.addView。
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
// …
if (mInputChannel != null) {
if (mInputQueueCallback != null) {
mInputQueue = new InputQueue();
mInputQueueCallback.onInputQueueCreated(mInputQueue);
}
mInputEventReceiver = new WindowInputEventReceiver(mInputChannel,
Looper.myLooper());
}
}
public abstract class InputEventReceiver {
// native 侧代码调用这个方法,把事件派发过来
private void dispatchInputEvent(int seq, InputEvent event, int displayId) {
mSeqMap.put(event.getSequenceNumber(), seq);
onInputEvent(event, displayId);
}
}
final class WindowInputEventReceiver extends InputEventReceiver {
@Override
public void onInputEvent(InputEvent event, int displayId) {
// 事件接受
enqueueInputEvent(event, this, 0, true);
}
// …
}
void enqueueInputEvent(InputEvent event,
InputEventReceiver receiver, int flags, boolean processImmediately) {
// 是否要立即处理事件
if (processImmediately) {
doProcessInputEvents();
} else {
scheduleProcessInputEvents();
}
}
void doProcessInputEvents() {
// …
while (mPendingInputEventHead != null) {
deliverInputEvent(q);
}
// …
}
private void deliverInputEvent(QueuedInputEvent q) {
// …
InputStage stage;
if (q.shouldSendToSynthesizer()) {
stage = mSyntheticInputStage;
} else {
stage = q.shouldSkipIme() ? mFirstPostImeInputStage : mFirstInputStage;
}
// 分发事件
stage.deliver(q);
}
从上面的代码流程中,事件最终走到 InputStage.deliver 里。
abstract class InputStage {
public final void deliver(QueuedInputEvent q) {
if ((q.mFlags & QueuedInputEvent.FLAG_FINISHED) != 0) {
forward(q);
} else if (shouldDropInputEvent(q)) {
finish(q, false);
} else {
apply(q, onProcess(q));
}
}
}
在 deliver 里,最终调用 onProcess,实现是在 ViewPostImeInputStage。
final class ViewPostImeInputStage extends InputStage {
@Override
protected int onProcess(QueuedInputEvent q) {
if (q.mEvent instanceof KeyEvent) {
return processKeyEvent(q);
} else {
final int source = q.mEvent.getSource();
if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
return processPointerEvent(q);
} else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
return processTrackballEvent(q);
} else {
return processGenericMotionEvent(q);
}
}
}
private int processPointerEvent(QueuedInputEvent q) {
// 这里 mView 是 DecorView,调用到 DecorView.dispatchPointerEvent
boolean handled = mView.dispatchPointerEvent(event);
// …
return handled ? FINISH_HANDLED : FORWARD;
}
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
学习分享
在当下这个信息共享的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了
很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘
如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。
2021最新上万页的大厂面试真题
七大模块学习资料:如NDK模块开发、Android框架体系架构…
只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。
这份体系学习笔记,适应人群:
**第一,**学习知识比较碎片化,没有合理的学习路线与进阶方向。
**第二,**开发几年,不知道如何进阶更进一步,比较迷茫。
**第三,**到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。如果你有需要,我这里恰好有为什么,不来领取!说不定能改变你现在的状态呢!
由于文章内容比较多,篇幅不允许,部分未展示内容以截图方式展示 。如有需要获取完整的资料文档的朋友点击我的【GitHub】免费获取。
如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。
2021最新上万页的大厂面试真题
[外链图片转存中…(img-e9hZpHbQ-1711072416484)]
七大模块学习资料:如NDK模块开发、Android框架体系架构…
[外链图片转存中…(img-VRU2cOgI-1711072416484)]
只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。
这份体系学习笔记,适应人群:
**第一,**学习知识比较碎片化,没有合理的学习路线与进阶方向。
**第二,**开发几年,不知道如何进阶更进一步,比较迷茫。
**第三,**到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。如果你有需要,我这里恰好有为什么,不来领取!说不定能改变你现在的状态呢!