From c6d7c9ab84616b4642c9dc6bcabc8f35ce5a099a Mon Sep 17 00:00:00 2001
From: Javier <dev.git@javispedro.com>
Date: Tue, 4 Feb 2020 23:10:12 +0100
Subject: right button clicks -> back gestures

---
 .../com/javispedro/vndroid/PointerEventOutput.java | 36 +++++++++++++++++-----
 1 file changed, 28 insertions(+), 8 deletions(-)

(limited to 'app')

diff --git a/app/src/main/java/com/javispedro/vndroid/PointerEventOutput.java b/app/src/main/java/com/javispedro/vndroid/PointerEventOutput.java
index f380671..ef4d98f 100644
--- a/app/src/main/java/com/javispedro/vndroid/PointerEventOutput.java
+++ b/app/src/main/java/com/javispedro/vndroid/PointerEventOutput.java
@@ -9,8 +9,6 @@ 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) {
@@ -18,7 +16,16 @@ public class PointerEventOutput extends AccessibilityService.GestureResultCallba
                 Log.w(TAG, "gesture not dispatched");
             }
         } else {
-            Log.e(TAG, "no controlservice");
+            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");
+            }
         }
     }
 
@@ -70,24 +77,37 @@ public class PointerEventOutput extends AccessibilityService.GestureResultCallba
         }
     }
 
+    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 ((buttonMask & 1) != 0) {
+        if (buttonPressed(buttonMask, BTN_LEFT)) {
             // Main button pressed
-            if (!btnPressed) {
+            if (!buttonPressed(prevButtonMask, BTN_LEFT)) {
                 pointerGesture(false, x, y, curTime, true);
-                btnPressed = true;
             } else {
                 // Button moved while pressed
                 pointerGesture(true, x, y, curTime, true);
             }
-        } else if (btnPressed) {
+        } else if (buttonPressed(prevButtonMask, BTN_LEFT)) {
             // Button was pressed but now released
             pointerGesture(true, x, y, curTime, false);
+        }
 
-            btnPressed = false;
+        if (buttonPressed(buttonMask, BTN_RIGHT) &&
+                !buttonPressed(prevButtonMask, BTN_RIGHT)) {
+            postGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
         }
+
+        prevButtonMask = buttonMask;
     }
 
     @Override
-- 
cgit v1.2.3