Framework 全局监听屏幕点击事件 INPUT_EVENT_INJECTION

简介: Framework 全局监听屏幕点击事件 INPUT_EVENT_INJECTION

需求:用户点击屏幕后取消原有定时任务,无操作后顺延原来定时任务


简单分析


要想全局监听,那必须是在 framework 中了,应该从哪里切入呢?先看看 log,每点击一次屏幕后发现打印


InputDispatcher: Asynchronous input event injection succeeded.


全局搜索找到 frameworks\native\services\inputflinger\InputDispatcher.cpp 中


这就有点难顶了,cpp 中想要通知 java 层还是有点困难的,一开始尝试了在此处发送广播

#include <unistd.h>
switch (injectionResult) {
case INPUT_EVENT_INJECTION_SUCCEEDED:
ALOGV(“Asynchronous input event injection succeeded.”);
execlp(“am”, “am”, “broadcast”,
“-n”, “com.android.systemui/com.pdd.sutie.CustomerReceiver”,
“-a”, “com.pdd.action.inputevent.inject”,
NULL);
exit(errno);


通过函数 execlp() 调用 am 指令发送广播,尝试后发现会把 system 进程搞挂,看来 execlp() 不能在此使用,那就只能搂底浆了,

从 cpp 往上找,最终找到


frameworks\base\services\core\java\com\android\server\input\InputManagerService.java 中


调用顺序如下

InputManagerService.java
injectInputEventInternal(event, mode)
com_android_server_input_InputManagerService.cpp
im->getInputManager()->getDispatcher()->injectInputEvent(
InputDispatcher.cpp
int32_t InputDispatcher::injectInputEvent(const InputEvent* event,
int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
uint32_t policyFlags)


解决办法


frameworks\base\services\core\java\com\android\server\input\InputManagerService.java

@Override // Binder call
    public boolean injectInputEvent(InputEvent event, int mode) {
        return injectInputEventInternal(event, mode);
    }
    private boolean injectInputEventInternal(InputEvent event, int mode) {
        if (event == null) {
            throw new IllegalArgumentException("event must not be null");
        }
        if (mode != InputManager.INJECT_INPUT_EVENT_MODE_ASYNC
                && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
                && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT) {
            throw new IllegalArgumentException("mode is invalid");
        }
        final int pid = Binder.getCallingPid();
        final int uid = Binder.getCallingUid();
        final long ident = Binder.clearCallingIdentity();
        final int result;
        try {
            result = nativeInjectInputEvent(mPtr, event, pid, uid, mode,
                    INJECTION_TIMEOUT_MILLIS, WindowManagerPolicy.FLAG_DISABLE_KEY_REPEAT);
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
        switch (result) {
            case INPUT_EVENT_INJECTION_PERMISSION_DENIED:
                Slog.w(TAG, "Input event injection from pid " + pid + " permission denied.");
                throw new SecurityException(
                        "Injecting to another application requires INJECT_EVENTS permission");
            case INPUT_EVENT_INJECTION_SUCCEEDED:
                //cczheng add for listen lcd input start
                android.util.Log.e("InputDispatcher", "Input event injection from pid " + pid + " succeeded.");
                Intent pIntent = new Intent("com.pdd.action.inputevent.inject");
                pIntent.setComponent(new android.content.ComponentName("com.android.systemui",
                            "com.pdd.sutie.CustomerReceiver"));
                mContext.sendBroadcast(pIntent);
                //cczheng add for listen lcd input end
                return true;
            case INPUT_EVENT_INJECTION_TIMED_OUT:
                Slog.w(TAG, "Input event injection from pid " + pid + " timed out.");
                return false;
            case INPUT_EVENT_INJECTION_FAILED:
            default:
                Slog.w(TAG, "Input event injection from pid " + pid + " failed.");
                return false;
        }
    }

Android Input(四) -InputDispatcher分发事件

Android10 InputManagerService事件输入输出

目录
相关文章
|
SQL 缓存 JSON
vue利用级联选择器实现全国省市区乡村五级菜单联动
vue利用级联选择器实现全国省市区乡村五级菜单联动
|
8月前
|
人工智能 弹性计算 算法
【云故事探索】NO.5:PETKIT 小佩,科技与爱,共绘宠物智能生活新篇章
【云故事探索】NO.5:PETKIT 小佩,科技与爱,共绘宠物智能生活新篇章
194 0
|
监控 Unix Linux
cpu相关指标(top、uptime、vmstat、mpstat、sar、pidstat、ps、dstat、perf、tcpdump、lscpu)等常见使用方法(二)
cpu相关指标(top、uptime、vmstat、mpstat、sar、pidstat、ps、dstat、perf、tcpdump、lscpu)等常见使用方法
462 0
MimicBrush:奇迹画刷,重新定义局部重绘
图像编辑是一项实用而又具有挑战性的任务,因为用户的需求多种多样,其中最困难的部分之一是准确描述编辑后的图像应该是什么样子。在MimicBrush这项工作中,提出了一种新的编辑形式,称为模仿编辑,以帮助用户更方便地发挥创造力。
|
Android开发
Android获取横竖屏状态及监听
Android获取横竖屏状态及监听
293 0
|
计算机视觉
ONNX转换NCNN
ONNX转换NCNN
1056 0
|
传感器 Java API
Android Input系统(1) Input事件的产生与传递
Android Input系统(1) Input事件的产生与传递
1094 0
|
存储 编解码 API
名声大噪的YOLO迎来YOLOv8,迅速包揽目标检测、实例分割新SOTA(1)
名声大噪的YOLO迎来YOLOv8,迅速包揽目标检测、实例分割新SOTA
654 0
名声大噪的YOLO迎来YOLOv8,迅速包揽目标检测、实例分割新SOTA(1)
|
存储 Kubernetes Cloud Native
全面掌握 Kubernetes:部署、管理和扩展云原生应用
Kubernetes 是一个强大的云原生应用部署、管理和扩展平台,提供了丰富的功能和工具。通过本文的介绍,您应该能够了解 Kubernetes 的基本概念、核心组件,以及如何使用 Kubernetes 部署、管理和扩展云原生应用。同时,了解到 Kubernetes Dashboard 作为一个图形化工具,可以更方便地管理集群中的资源和应用程序。在实际应用中,深入学习和实践 Kubernetes 将有助于更好地掌握云原生应用的部署和管理。
665 1
全面掌握 Kubernetes:部署、管理和扩展云原生应用