summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/javispedro/vndroid/PointerEventOutput.java
blob: ef4d98f5c36bd97db00b89b35cbae8ef8778588b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.javispedro.vndroid;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.graphics.Path;
import android.os.SystemClock;
import android.util.Log;

public class PointerEventOutput extends AccessibilityService.GestureResultCallback {
    private final String TAG = PointerEventOutput.class.getSimpleName();

    private void dispatchGesture(GestureDescription gesture) {
        ControlService service = ControlService.getInstance();
        if (service != null) {
            if (!service.dispatchGesture(gesture, this, null)) {
                Log.w(TAG, "gesture not dispatched");
            }
        } else {
            Log.w(TAG, "accessibility service not enabled, cannot control gestures");
        }
    }

    private void postGlobalAction(int action) {
        ControlService service = ControlService.getInstance();
        if (service != null) {
            if (!service.performGlobalAction(action)) {
                Log.w(TAG, "global action not posted");
            }
        }
    }

    private long lastTime = 0;
    private GestureDescription lastGesture = null;
    private int lastX = -1, lastY = -1;

    private void pointerGesture(boolean cont, int x, int y, long curTime, boolean willContinue) {
        Path path = new Path();
        long duration = 1;

        if (cont && lastGesture == null) {
            Log.w(TAG, "continuing gesture but no previous data");
        }

        if (cont && lastGesture != null) {
            assert lastX != -1 && lastY != -1;
            path.moveTo(lastX, lastY);
            duration = curTime - lastTime;
            assert duration > 0;
        } else {
            path.moveTo(x, y);
        }

        path.lineTo(x, y);

        GestureDescription.Builder builder = new GestureDescription.Builder();
        GestureDescription.StrokeDescription stroke;
        if (cont && lastGesture != null) {
            stroke = lastGesture.getStroke(0);
            stroke = stroke.continueStroke(path, 0, duration, willContinue);
        } else {
            stroke = new GestureDescription.StrokeDescription(path, 0, duration, willContinue);
        }
        builder.addStroke(stroke);
        GestureDescription gesture = builder.build();
        dispatchGesture(gesture);

        if (willContinue) {
            lastGesture = gesture;
            lastTime = curTime;
            lastX = x;
            lastY = y;
        } else {
            lastGesture = null;
            lastTime = 0;
            lastX = -1;
            lastY = -1;
        }
    }

    private final int BTN_LEFT = 0;
    private final int BTN_MIDDLE = 1;
    private final int BTN_RIGHT = 2;
    private byte prevButtonMask = 0;

    private boolean buttonPressed(byte mask, int btn) {
        return (mask & (1 << btn)) != 0;
    }

    public void postPointerEvent(byte buttonMask, int x, int y) {
        final long curTime = SystemClock.uptimeMillis();

        if (buttonPressed(buttonMask, BTN_LEFT)) {
            // Main button pressed
            if (!buttonPressed(prevButtonMask, BTN_LEFT)) {
                pointerGesture(false, x, y, curTime, true);
            } else {
                // Button moved while pressed
                pointerGesture(true, x, y, curTime, true);
            }
        } else if (buttonPressed(prevButtonMask, BTN_LEFT)) {
            // Button was pressed but now released
            pointerGesture(true, x, y, curTime, false);
        }

        if (buttonPressed(buttonMask, BTN_RIGHT) &&
                !buttonPressed(prevButtonMask, BTN_RIGHT)) {
            postGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
        }

        prevButtonMask = buttonMask;
    }

    @Override
    public void onCompleted(GestureDescription gestureDescription) {
    }

    @Override
    public void onCancelled(GestureDescription gestureDescription) {
        Log.d(TAG, "gesture cancelled");
    }
}