summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/javispedro/vndroid/PointerEventOutput.java
blob: f380671de715023876ab7213c9924a59b218988e (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
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 boolean btnPressed = false;

    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.e(TAG, "no controlservice");
        }
    }

    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;
        }
    }

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

        if ((buttonMask & 1) != 0) {
            // Main button pressed
            if (!btnPressed) {
                pointerGesture(false, x, y, curTime, true);
                btnPressed = true;
            } else {
                // Button moved while pressed
                pointerGesture(true, x, y, curTime, true);
            }
        } else if (btnPressed) {
            // Button was pressed but now released
            pointerGesture(true, x, y, curTime, false);

            btnPressed = false;
        }
    }

    @Override
    public void onCompleted(GestureDescription gestureDescription) {
    }

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