Android Input事件注入

  1. Android可以通过adb shell input xxx命令注入输入事件,调用IMS.injectInputEvent,最终走到通过JNI调用InputDispatcher注入事件,没有经过InputReader
java 复制代码
public class Input extends BaseCommand {
    // ... ... ... ...
    Input() {
        COMMANDS.put("text", new InputText());
        COMMANDS.put("keyevent", new InputKeyEvent());
        COMMANDS.put("tap", new InputTap());
        COMMANDS.put("swipe", new InputSwipe());
        COMMANDS.put("draganddrop", new InputDragAndDrop());
        COMMANDS.put("press", new InputPress());
        COMMANDS.put("roll", new InputRoll());
        COMMANDS.put("motionevent", new InputMotionEvent());
    }
    @Override
    public void onRun() throws Exception {
        String arg = nextArgRequired();
        int inputSource = InputDevice.SOURCE_UNKNOWN;

        // Get source (optional).
        if (SOURCES.containsKey(arg)) {
            inputSource = SOURCES.get(arg);
            arg = nextArgRequired();
        }

        // Get displayId (optional).
        int displayId = INVALID_DISPLAY;
        if ("-d".equals(arg)) {
            displayId = getDisplayId();
            arg = nextArgRequired();
        }

        // Get command and run.
        InputCmd cmd = COMMANDS.get(arg);
        if (cmd != null) {
            try {
                cmd.run(inputSource, displayId);
                return;
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException(INVALID_ARGUMENTS + arg);
            }
        }

        throw new IllegalArgumentException("Error: Unknown command: " + arg);
    }
    // ... ... ... ...
    class InputKeyEvent implements InputCmd {
        @Override
        public void run(int inputSource, int displayId) {
            String arg = nextArgRequired();
            final boolean longpress = "--longpress".equals(arg);
            if (longpress) {
                arg = nextArgRequired();
            }

            do {
                final int keycode = KeyEvent.keyCodeFromString(arg);
                sendKeyEvent(inputSource, keycode, longpress, displayId);
            } while ((arg = nextArg()) != null);
        }

        private void sendKeyEvent(int inputSource, int keyCode, boolean longpress, int displayId) {
            final long now = SystemClock.uptimeMillis();
            int repeatCount = 0;

            KeyEvent event = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, repeatCount,
                    0 /*metaState*/, KeyCharacterMap.VIRTUAL_KEYBOARD, 0 /*scancode*/, 0 /*flags*/,
                    inputSource);
            event.setDisplayId(displayId);

            injectKeyEvent(event);
            if (longpress) {
                repeatCount++;
                injectKeyEvent(KeyEvent.changeTimeRepeat(event, now, repeatCount,
                        KeyEvent.FLAG_LONG_PRESS));
            }
            injectKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP));
        }
    }
  1. 通过InputManager提供的接口注入
java 复制代码
        invokeSetDisplayId(mMotionSetDisplayId, ev, displayId); 
            Class clm = MotionEvent.class;
            m.invoke(ev, displayId);   // mMotionSetDisplayId = clm.getMethod("setDisplayId", int.class);            
        InjectionUtil.getInstance().injectInputEvent(ev, INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
            invokeInjectInputEvent(event, mode);
                Class cl = InputManager.class;
                Method method = cl.getMethod("getInstance");            
                mInjectInputEvent = cl.getMethod("injectInputEvent", InputEvent.class, int.class);
                mInjectInputEvent.invoke(inputManager, event, mode);  //InputManager::injectInputEvent
@frameworks/base/core/java/android/hardware/input/InputManager.java
                    return mIm.injectInputEvent(event, mode);
                        ......
@frameworks/native/services/inputflinger/dispatcher/InputDispatcher.cpp                 
                            InputDispatcher::injectInputEvent
@frameworks/native/include/input/Input.h          
                                event->getDisplayId()
                                    needWake |= enqueueInboundEventLocked(std::move(injectedEntries.front()));
                                        mInboundQueue.push_back(std::move(newEntry));  //向InputDispatcher中的mInboundQueue队列注入事件
相关推荐
叽哥27 分钟前
flutter学习第 2 节:第一个 Flutter 应用
android·flutter
2501_9160074735 分钟前
iOS 文件管理实战指南 查看 App 数据与系统日志的完整方法
android·ios·小程序·https·uni-app·iphone·webview
余辉zmh1 小时前
【MySQL基础篇】:MySQL常用内置函数以及实用示例
android·mysql·adb
惺惺作态1 小时前
Android 项目构建编译概述
android
_祝你今天愉快2 小时前
Java Lock
android·java·后端
2501_915106322 小时前
iOS 内测上架流程详解:跨平台团队如何快速部署 TestFlight
android·ios·小程序·https·uni-app·iphone·webview
Edylan3 小时前
关于Lifecycle,来讲个明白
android·架构
M0066883 小时前
低代码系统的技术深度:超越“可视化操作”的架构与实现挑战
android·rxjava
whysqwhw3 小时前
Activity 的启动模式|Flags|IntentFilter
android
whysqwhw4 小时前
安卓Hook系统服务实现插件化的核心原理
android