Consolidate touch tracking timers

This commit is contained in:
Cameron Gutman
2022-09-15 02:05:40 -05:00
parent b07ffbde29
commit d1b24ea6af
3 changed files with 45 additions and 33 deletions
+9 -2
View File
@@ -84,6 +84,7 @@ import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory; import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.Locale; import java.util.Locale;
import java.util.Timer;
public class Game extends Activity implements SurfaceHolder.Callback, public class Game extends Activity implements SurfaceHolder.Callback,
@@ -94,6 +95,7 @@ public class Game extends Activity implements SurfaceHolder.Callback,
private int lastButtonState = 0; private int lastButtonState = 0;
// Only 2 touches are supported // Only 2 touches are supported
private Timer touchTimer;
private final TouchContext[] touchContextMap = new TouchContext[2]; private final TouchContext[] touchContextMap = new TouchContext[2];
private long threeFingerDownTime = 0; private long threeFingerDownTime = 0;
@@ -477,14 +479,15 @@ public class Game extends Activity implements SurfaceHolder.Callback,
inputManager.registerInputDeviceListener(keyboardTranslator, null); inputManager.registerInputDeviceListener(keyboardTranslator, null);
// Initialize touch contexts // Initialize touch contexts
touchTimer = new Timer("TouchTimer", true);
for (int i = 0; i < touchContextMap.length; i++) { for (int i = 0; i < touchContextMap.length; i++) {
if (!prefConfig.touchscreenTrackpad) { if (!prefConfig.touchscreenTrackpad) {
touchContextMap[i] = new AbsoluteTouchContext(conn, i, streamView); touchContextMap[i] = new AbsoluteTouchContext(conn, i, streamView, touchTimer);
} }
else { else {
touchContextMap[i] = new RelativeTouchContext(conn, i, touchContextMap[i] = new RelativeTouchContext(conn, i,
REFERENCE_HORIZ_RES, REFERENCE_VERT_RES, REFERENCE_HORIZ_RES, REFERENCE_VERT_RES,
streamView, prefConfig); streamView, prefConfig, touchTimer);
} }
} }
@@ -1028,6 +1031,10 @@ public class Game extends Activity implements SurfaceHolder.Callback,
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
if (touchTimer != null) {
touchTimer.cancel();
}
InputManager inputManager = (InputManager) getSystemService(Context.INPUT_SERVICE); InputManager inputManager = (InputManager) getSystemService(Context.INPUT_SERVICE);
if (controllerHandler != null) { if (controllerHandler != null) {
inputManager.unregisterInputDeviceListener(controllerHandler); inputManager.unregisterInputDeviceListener(controllerHandler);
@@ -22,12 +22,13 @@ public class AbsoluteTouchContext implements TouchContext {
private boolean cancelled; private boolean cancelled;
private boolean confirmedLongPress; private boolean confirmedLongPress;
private boolean confirmedTap; private boolean confirmedTap;
private Timer longPressTimer; private TimerTask longPressTimerTask;
private Timer tapDownTimer; private TimerTask tapDownTimerTask;
private final NvConnection conn; private final NvConnection conn;
private final int actionIndex; private final int actionIndex;
private final View targetView; private final View targetView;
private final Timer timer;
private final Handler handler; private final Handler handler;
private final Runnable leftButtonUpRunnable = new Runnable() { private final Runnable leftButtonUpRunnable = new Runnable() {
@@ -48,11 +49,12 @@ public class AbsoluteTouchContext implements TouchContext {
private static final int TOUCH_DOWN_DEAD_ZONE_TIME_THRESHOLD = 100; private static final int TOUCH_DOWN_DEAD_ZONE_TIME_THRESHOLD = 100;
private static final int TOUCH_DOWN_DEAD_ZONE_DISTANCE_THRESHOLD = 20; private static final int TOUCH_DOWN_DEAD_ZONE_DISTANCE_THRESHOLD = 20;
public AbsoluteTouchContext(NvConnection conn, int actionIndex, View view) public AbsoluteTouchContext(NvConnection conn, int actionIndex, View view, Timer timer)
{ {
this.conn = conn; this.conn = conn;
this.actionIndex = actionIndex; this.actionIndex = actionIndex;
this.targetView = view; this.targetView = view;
this.timer = timer;
this.handler = new Handler(Looper.getMainLooper()); this.handler = new Handler(Looper.getMainLooper());
} }
@@ -137,18 +139,18 @@ public class AbsoluteTouchContext implements TouchContext {
} }
private synchronized void startLongPressTimer() { private synchronized void startLongPressTimer() {
longPressTimer = new Timer(true); cancelLongPressTimer();
longPressTimer.schedule(new TimerTask() { longPressTimerTask = new TimerTask() {
@Override @Override
public void run() { public void run() {
synchronized (AbsoluteTouchContext.this) { synchronized (AbsoluteTouchContext.this) {
// Check if someone cancelled us // Check if someone cancelled us
if (longPressTimer == null) { if (longPressTimerTask == null) {
return; return;
} }
// Uncancellable now // Uncancellable now
longPressTimer = null; longPressTimerTask = null;
// This timer should have already expired, but cancel it just in case // This timer should have already expired, but cancel it just in case
cancelTapDownTimer(); cancelTapDownTimer();
@@ -161,41 +163,43 @@ public class AbsoluteTouchContext implements TouchContext {
conn.sendMouseButtonDown(MouseButtonPacket.BUTTON_RIGHT); conn.sendMouseButtonDown(MouseButtonPacket.BUTTON_RIGHT);
} }
} }
}, LONG_PRESS_TIME_THRESHOLD); };
timer.schedule(longPressTimerTask, LONG_PRESS_TIME_THRESHOLD);
} }
private synchronized void cancelLongPressTimer() { private synchronized void cancelLongPressTimer() {
if (longPressTimer != null) { if (longPressTimerTask != null) {
longPressTimer.cancel(); longPressTimerTask.cancel();
longPressTimer = null; longPressTimerTask = null;
} }
} }
private synchronized void startTapDownTimer() { private synchronized void startTapDownTimer() {
tapDownTimer = new Timer(true); cancelTapDownTimer();
tapDownTimer.schedule(new TimerTask() { tapDownTimerTask = new TimerTask() {
@Override @Override
public void run() { public void run() {
synchronized (AbsoluteTouchContext.this) { synchronized (AbsoluteTouchContext.this) {
// Check if someone cancelled us // Check if someone cancelled us
if (tapDownTimer == null) { if (tapDownTimerTask == null) {
return; return;
} }
// Uncancellable now // Uncancellable now
tapDownTimer = null; tapDownTimerTask = null;
// Start our tap // Start our tap
tapConfirmed(); tapConfirmed();
} }
} }
}, TOUCH_DOWN_DEAD_ZONE_TIME_THRESHOLD); };
timer.schedule(tapDownTimerTask, TOUCH_DOWN_DEAD_ZONE_TIME_THRESHOLD);
} }
private synchronized void cancelTapDownTimer() { private synchronized void cancelTapDownTimer() {
if (tapDownTimer != null) { if (tapDownTimerTask != null) {
tapDownTimer.cancel(); tapDownTimerTask.cancel();
tapDownTimer = null; tapDownTimerTask = null;
} }
} }
@@ -21,7 +21,7 @@ public class RelativeTouchContext implements TouchContext {
private boolean confirmedMove; private boolean confirmedMove;
private boolean confirmedDrag; private boolean confirmedDrag;
private boolean confirmedScroll; private boolean confirmedScroll;
private Timer dragTimer; private TimerTask dragTimerTask;
private double distanceMoved; private double distanceMoved;
private double xFactor, yFactor; private double xFactor, yFactor;
private int pointerCount; private int pointerCount;
@@ -33,6 +33,7 @@ public class RelativeTouchContext implements TouchContext {
private final int referenceHeight; private final int referenceHeight;
private final View targetView; private final View targetView;
private final PreferenceConfiguration prefConfig; private final PreferenceConfiguration prefConfig;
private final Timer timer;
private final Handler handler; private final Handler handler;
// Indexed by MouseButtonPacket.BUTTON_XXX - 1 // Indexed by MouseButtonPacket.BUTTON_XXX - 1
@@ -78,7 +79,8 @@ public class RelativeTouchContext implements TouchContext {
public RelativeTouchContext(NvConnection conn, int actionIndex, public RelativeTouchContext(NvConnection conn, int actionIndex,
int referenceWidth, int referenceHeight, int referenceWidth, int referenceHeight,
View view, PreferenceConfiguration prefConfig) View view, PreferenceConfiguration prefConfig,
Timer timer)
{ {
this.conn = conn; this.conn = conn;
this.actionIndex = actionIndex; this.actionIndex = actionIndex;
@@ -86,6 +88,7 @@ public class RelativeTouchContext implements TouchContext {
this.referenceHeight = referenceHeight; this.referenceHeight = referenceHeight;
this.targetView = view; this.targetView = view;
this.prefConfig = prefConfig; this.prefConfig = prefConfig;
this.timer = timer;
this.handler = new Handler(Looper.getMainLooper()); this.handler = new Handler(Looper.getMainLooper());
} }
@@ -185,11 +188,8 @@ public class RelativeTouchContext implements TouchContext {
} }
private synchronized void startDragTimer() { private synchronized void startDragTimer() {
// Cancel any existing drag timers
cancelDragTimer(); cancelDragTimer();
dragTimerTask = new TimerTask() {
dragTimer = new Timer(true);
dragTimer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
synchronized (RelativeTouchContext.this) { synchronized (RelativeTouchContext.this) {
@@ -204,25 +204,26 @@ public class RelativeTouchContext implements TouchContext {
} }
// Check if someone cancelled us // Check if someone cancelled us
if (dragTimer == null) { if (dragTimerTask == null) {
return; return;
} }
// Uncancellable now // Uncancellable now
dragTimer = null; dragTimerTask = null;
// We haven't been cancelled before the timer expired so begin dragging // We haven't been cancelled before the timer expired so begin dragging
confirmedDrag = true; confirmedDrag = true;
conn.sendMouseButtonDown(getMouseButtonIndex()); conn.sendMouseButtonDown(getMouseButtonIndex());
} }
} }
}, DRAG_TIME_THRESHOLD); };
timer.schedule(dragTimerTask, DRAG_TIME_THRESHOLD);
} }
private synchronized void cancelDragTimer() { private synchronized void cancelDragTimer() {
if (dragTimer != null) { if (dragTimerTask != null) {
dragTimer.cancel(); dragTimerTask.cancel();
dragTimer = null; dragTimerTask = null;
} }
} }