Use Handlers instead of Timers for one-shot events

This commit is contained in:
Cameron Gutman
2022-09-16 00:08:48 -05:00
parent 9cf27d8fb1
commit 03f9ea8435
5 changed files with 74 additions and 140 deletions

View File

@@ -7,9 +7,6 @@ import android.view.View;
import com.limelight.nvstream.NvConnection;
import com.limelight.nvstream.input.MouseButtonPacket;
import java.util.Timer;
import java.util.TimerTask;
public class AbsoluteTouchContext implements TouchContext {
private int lastTouchDownX = 0;
private int lastTouchDownY = 0;
@@ -22,13 +19,33 @@ public class AbsoluteTouchContext implements TouchContext {
private boolean cancelled;
private boolean confirmedLongPress;
private boolean confirmedTap;
private TimerTask longPressTimerTask;
private TimerTask tapDownTimerTask;
private final Runnable longPressRunnable = new Runnable() {
@Override
public void run() {
// This timer should have already expired, but cancel it just in case
cancelTapDownTimer();
// Switch from a left click to a right click after a long press
confirmedLongPress = true;
if (confirmedTap) {
conn.sendMouseButtonUp(MouseButtonPacket.BUTTON_LEFT);
}
conn.sendMouseButtonDown(MouseButtonPacket.BUTTON_RIGHT);
}
};
private final Runnable tapDownRunnable = new Runnable() {
@Override
public void run() {
// Start our tap
tapConfirmed();
}
};
private final NvConnection conn;
private final int actionIndex;
private final View targetView;
private final Timer timer;
private final Handler handler;
private final Runnable leftButtonUpRunnable = new Runnable() {
@@ -49,12 +66,11 @@ public class AbsoluteTouchContext implements TouchContext {
private static final int TOUCH_DOWN_DEAD_ZONE_TIME_THRESHOLD = 100;
private static final int TOUCH_DOWN_DEAD_ZONE_DISTANCE_THRESHOLD = 20;
public AbsoluteTouchContext(NvConnection conn, int actionIndex, View view, Timer timer)
public AbsoluteTouchContext(NvConnection conn, int actionIndex, View view)
{
this.conn = conn;
this.actionIndex = actionIndex;
this.targetView = view;
this.timer = timer;
this.handler = new Handler(Looper.getMainLooper());
}
@@ -138,69 +154,22 @@ public class AbsoluteTouchContext implements TouchContext {
lastTouchUpTime = eventTime;
}
private synchronized void startLongPressTimer() {
private void startLongPressTimer() {
cancelLongPressTimer();
longPressTimerTask = new TimerTask() {
@Override
public void run() {
synchronized (AbsoluteTouchContext.this) {
// Check if someone cancelled us
if (longPressTimerTask == null) {
return;
}
// Uncancellable now
longPressTimerTask = null;
// This timer should have already expired, but cancel it just in case
cancelTapDownTimer();
// Switch from a left click to a right click after a long press
confirmedLongPress = true;
if (confirmedTap) {
conn.sendMouseButtonUp(MouseButtonPacket.BUTTON_LEFT);
}
conn.sendMouseButtonDown(MouseButtonPacket.BUTTON_RIGHT);
}
}
};
timer.schedule(longPressTimerTask, LONG_PRESS_TIME_THRESHOLD);
handler.postDelayed(longPressRunnable, LONG_PRESS_TIME_THRESHOLD);
}
private synchronized void cancelLongPressTimer() {
if (longPressTimerTask != null) {
longPressTimerTask.cancel();
longPressTimerTask = null;
}
private void cancelLongPressTimer() {
handler.removeCallbacks(longPressRunnable);
}
private synchronized void startTapDownTimer() {
private void startTapDownTimer() {
cancelTapDownTimer();
tapDownTimerTask = new TimerTask() {
@Override
public void run() {
synchronized (AbsoluteTouchContext.this) {
// Check if someone cancelled us
if (tapDownTimerTask == null) {
return;
}
// Uncancellable now
tapDownTimerTask = null;
// Start our tap
tapConfirmed();
}
}
};
timer.schedule(tapDownTimerTask, TOUCH_DOWN_DEAD_ZONE_TIME_THRESHOLD);
handler.postDelayed(tapDownRunnable, TOUCH_DOWN_DEAD_ZONE_TIME_THRESHOLD);
}
private synchronized void cancelTapDownTimer() {
if (tapDownTimerTask != null) {
tapDownTimerTask.cancel();
tapDownTimerTask = null;
}
private void cancelTapDownTimer() {
handler.removeCallbacks(tapDownRunnable);
}
private void tapConfirmed() {

View File

@@ -8,9 +8,6 @@ import com.limelight.nvstream.NvConnection;
import com.limelight.nvstream.input.MouseButtonPacket;
import com.limelight.preferences.PreferenceConfiguration;
import java.util.Timer;
import java.util.TimerTask;
public class RelativeTouchContext implements TouchContext {
private int lastTouchX = 0;
private int lastTouchY = 0;
@@ -21,7 +18,6 @@ public class RelativeTouchContext implements TouchContext {
private boolean confirmedMove;
private boolean confirmedDrag;
private boolean confirmedScroll;
private TimerTask dragTimerTask;
private double distanceMoved;
private double xFactor, yFactor;
private int pointerCount;
@@ -33,9 +29,27 @@ public class RelativeTouchContext implements TouchContext {
private final int referenceHeight;
private final View targetView;
private final PreferenceConfiguration prefConfig;
private final Timer timer;
private final Handler handler;
private final Runnable dragTimerRunnable = new Runnable() {
@Override
public void run() {
// Check if someone already set move
if (confirmedMove) {
return;
}
// The drag should only be processed for the primary finger
if (actionIndex != maxPointerCountInGesture - 1) {
return;
}
// We haven't been cancelled before the timer expired so begin dragging
confirmedDrag = true;
conn.sendMouseButtonDown(getMouseButtonIndex());
}
};
// Indexed by MouseButtonPacket.BUTTON_XXX - 1
private final Runnable[] buttonUpRunnables = new Runnable[] {
new Runnable() {
@@ -79,8 +93,7 @@ public class RelativeTouchContext implements TouchContext {
public RelativeTouchContext(NvConnection conn, int actionIndex,
int referenceWidth, int referenceHeight,
View view, PreferenceConfiguration prefConfig,
Timer timer)
View view, PreferenceConfiguration prefConfig)
{
this.conn = conn;
this.actionIndex = actionIndex;
@@ -88,7 +101,6 @@ public class RelativeTouchContext implements TouchContext {
this.referenceHeight = referenceHeight;
this.targetView = view;
this.prefConfig = prefConfig;
this.timer = timer;
this.handler = new Handler(Looper.getMainLooper());
}
@@ -187,47 +199,16 @@ public class RelativeTouchContext implements TouchContext {
}
}
private synchronized void startDragTimer() {
private void startDragTimer() {
cancelDragTimer();
dragTimerTask = new TimerTask() {
@Override
public void run() {
synchronized (RelativeTouchContext.this) {
// Check if someone already set move
if (confirmedMove) {
return;
}
// The drag should only be processed for the primary finger
if (actionIndex != maxPointerCountInGesture - 1) {
return;
}
// Check if someone cancelled us
if (dragTimerTask == null) {
return;
}
// Uncancellable now
dragTimerTask = null;
// We haven't been cancelled before the timer expired so begin dragging
confirmedDrag = true;
conn.sendMouseButtonDown(getMouseButtonIndex());
}
}
};
timer.schedule(dragTimerTask, DRAG_TIME_THRESHOLD);
handler.postDelayed(dragTimerRunnable, DRAG_TIME_THRESHOLD);
}
private synchronized void cancelDragTimer() {
if (dragTimerTask != null) {
dragTimerTask.cancel();
dragTimerTask = null;
}
private void cancelDragTimer() {
handler.removeCallbacks(dragTimerRunnable);
}
private synchronized void checkForConfirmedMove(int eventX, int eventY) {
private void checkForConfirmedMove(int eventX, int eventY) {
// If we've already confirmed something, get out now
if (confirmedMove || confirmedDrag) {
return;