summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/javispedro/vndroid/PointerEventOutput.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/com/javispedro/vndroid/PointerEventOutput.java')
-rw-r--r--app/src/main/java/com/javispedro/vndroid/PointerEventOutput.java101
1 files changed, 101 insertions, 0 deletions
diff --git a/app/src/main/java/com/javispedro/vndroid/PointerEventOutput.java b/app/src/main/java/com/javispedro/vndroid/PointerEventOutput.java
new file mode 100644
index 0000000..f380671
--- /dev/null
+++ b/app/src/main/java/com/javispedro/vndroid/PointerEventOutput.java
@@ -0,0 +1,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");
+ }
+}