Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 06a35c34 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add ACTION_CANCEL to input command"

parents 4e329ef8 9a470b66
Loading
Loading
Loading
Loading
+39 −21
Original line number Diff line number Diff line
@@ -199,7 +199,7 @@ public class InputShellCommand extends ShellCommand {
                    + " (Default: touchscreen)");
            out.println("      press (Default: trackball)");
            out.println("      roll <dx> <dy> (Default: trackball)");
            out.println("      motionevent <DOWN|UP|MOVE> <x> <y> (Default: touchscreen)");
            out.println("      motionevent <DOWN|UP|MOVE|CANCEL> <x> <y> (Default: touchscreen)");
        }
    }

@@ -357,32 +357,50 @@ public class InputShellCommand extends ShellCommand {
                displayId);
    }

    private int getAction() {
        String actionString = getNextArgRequired();
        switch (actionString.toUpperCase()) {
            case "DOWN":
                return MotionEvent.ACTION_DOWN;
            case "UP":
                return MotionEvent.ACTION_UP;
            case "MOVE":
                return MotionEvent.ACTION_MOVE;
            case "CANCEL":
                return MotionEvent.ACTION_CANCEL;
            default:
                throw new IllegalArgumentException("Unknown action: " + actionString);
        }
    }

    private void runMotionEvent(int inputSource, int displayId) {
        inputSource = getSource(inputSource, InputDevice.SOURCE_TOUCHSCREEN);
        sendMotionEvent(inputSource, getNextArgRequired(), Float.parseFloat(getNextArgRequired()),
                Float.parseFloat(getNextArgRequired()), displayId);
        int action = getAction();
        float x = 0, y = 0;
        if (action == MotionEvent.ACTION_DOWN
                || action == MotionEvent.ACTION_MOVE
                || action == MotionEvent.ACTION_UP) {
            x = Float.parseFloat(getNextArgRequired());
            y = Float.parseFloat(getNextArgRequired());
        } else {
            // For ACTION_CANCEL, the positions are optional
            String xString = getNextArg();
            String yString = getNextArg();
            if (xString != null && yString != null) {
                x = Float.parseFloat(xString);
                y = Float.parseFloat(yString);
            }
        }

        sendMotionEvent(inputSource, action, x, y, displayId);
    }

    private void sendMotionEvent(int inputSource, String motionEventType, float x, float y,
    private void sendMotionEvent(int inputSource, int action, float x, float y,
            int displayId) {
        final int action;
        final float pressure;
        float pressure = NO_PRESSURE;

        switch (motionEventType.toUpperCase()) {
            case "DOWN":
                action = MotionEvent.ACTION_DOWN;
        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
            pressure = DEFAULT_PRESSURE;
                break;
            case "UP":
                action = MotionEvent.ACTION_UP;
                pressure = NO_PRESSURE;
                break;
            case "MOVE":
                action = MotionEvent.ACTION_MOVE;
                pressure = DEFAULT_PRESSURE;
                break;
            default:
                throw new IllegalArgumentException("Unknown motionevent " + motionEventType);
        }

        final long now = SystemClock.uptimeMillis();