track user presence by time instead of poll count
This commit is contained in:
@@ -4,6 +4,8 @@ import com.github.kwhat.jnativehook.GlobalScreen;
|
||||
import com.github.kwhat.jnativehook.NativeHookException;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import wtf.beatrice.autosqueal.config.AutoSquealConfig;
|
||||
import wtf.beatrice.autosqueal.config.ConfigStore;
|
||||
import wtf.beatrice.autosqueal.listener.KeyPressListener;
|
||||
import wtf.beatrice.autosqueal.ui.MainWindow;
|
||||
|
||||
@@ -13,7 +15,7 @@ import javax.swing.UnsupportedLookAndFeelException;
|
||||
public class Main {
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger(Main.class);
|
||||
private static final MainWindow mainWindow = new MainWindow();
|
||||
private static MainWindow mainWindow;
|
||||
|
||||
/** Whether the hook and its listener are registered, so retries don't add duplicates. */
|
||||
private static boolean hookRegistered = false;
|
||||
@@ -21,6 +23,9 @@ public class Main {
|
||||
public static void main(String[] args) {
|
||||
LOGGER.info("Hello world!");
|
||||
|
||||
AutoSquealConfig config = new ConfigStore().load();
|
||||
mainWindow = new MainWindow(config);
|
||||
|
||||
// ⌘Q on macOS, the IDE stop button and signals all bypass
|
||||
// windowClosing: make cleanup run on every exit path instead
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(mainWindow::cleanup, "autosqueal-shutdown"));
|
||||
|
||||
@@ -2,41 +2,50 @@ package wtf.beatrice.autosqueal.listener;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import wtf.beatrice.autosqueal.config.AutoSquealConfig;
|
||||
import wtf.beatrice.autosqueal.controls.RobotMouseTracker;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.function.LongSupplier;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Watches the cursor position and tracks whether the user is away: if the cursor
|
||||
* stays still for long enough — ignoring the movements the app performs on its
|
||||
* own — the user is considered away. Keyboard activity counts as presence,
|
||||
* too: a user who is typing is using the machine, even if the mouse never moves.
|
||||
* Watches the cursor position and tracks whether the user is away: if the
|
||||
* user has not touched mouse or keyboard for long enough — ignoring the
|
||||
* movements the app performs on its own — the user is considered away.
|
||||
* Keyboard activity counts as presence, too: a user who is typing is using
|
||||
* the machine, even if the mouse never moves.
|
||||
*
|
||||
* The away state is time-based: how long the user has been idle is compared
|
||||
* against the threshold at every poll, so a threshold changed on the fly
|
||||
* applies from the very next poll.
|
||||
*/
|
||||
public class CursorMoveListener implements Runnable {
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger(CursorMoveListener.class);
|
||||
|
||||
/** Number of consecutive still polls after which the user is considered away. */
|
||||
private static final int LOOPS_BEFORE_AWAY = 30;
|
||||
|
||||
private final RobotMouseTracker robotTracker;
|
||||
private final Supplier<Point> currentPosition;
|
||||
private final AutoSquealConfig config;
|
||||
private final LongSupplier clock;
|
||||
|
||||
private int lastSeenX;
|
||||
private int lastSeenY;
|
||||
private boolean firstPoll = true;
|
||||
private int loops = 0;
|
||||
private long lastUserActivityTime;
|
||||
private volatile boolean userAway = false;
|
||||
private volatile boolean keyboardActivity = false;
|
||||
|
||||
public CursorMoveListener(RobotMouseTracker robotTracker) {
|
||||
this(robotTracker, () -> MouseInfo.getPointerInfo().getLocation());
|
||||
public CursorMoveListener(RobotMouseTracker robotTracker, AutoSquealConfig config) {
|
||||
this(robotTracker, config, () -> MouseInfo.getPointerInfo().getLocation(), System::currentTimeMillis);
|
||||
}
|
||||
|
||||
CursorMoveListener(RobotMouseTracker robotTracker, Supplier<Point> currentPosition) {
|
||||
CursorMoveListener(RobotMouseTracker robotTracker, AutoSquealConfig config,
|
||||
Supplier<Point> currentPosition, LongSupplier clock) {
|
||||
this.robotTracker = robotTracker;
|
||||
this.config = config;
|
||||
this.currentPosition = currentPosition;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,13 +65,15 @@ public class CursorMoveListener implements Runnable {
|
||||
this.keyboardActivity = false;
|
||||
|
||||
Point location = currentPosition.get();
|
||||
long now = clock.getAsLong();
|
||||
|
||||
if (firstPoll) {
|
||||
// seed the comparison baseline, so that the first poll is not
|
||||
// mistaken for a movement
|
||||
// seed the comparison baseline and the activity clock, so that
|
||||
// the first poll is neither a movement nor a full idle period
|
||||
firstPoll = false;
|
||||
lastSeenX = location.x;
|
||||
lastSeenY = location.y;
|
||||
lastUserActivityTime = now;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -70,18 +81,16 @@ public class CursorMoveListener implements Runnable {
|
||||
boolean userMoved = keyboardActivity || (positionChanged && !isAppMovement(location));
|
||||
|
||||
if (userMoved) {
|
||||
if (userAway) {
|
||||
LOGGER.info("User is no longer away!");
|
||||
}
|
||||
loops = 0;
|
||||
} else if (loops < LOOPS_BEFORE_AWAY) {
|
||||
loops++;
|
||||
if (loops == LOOPS_BEFORE_AWAY) {
|
||||
LOGGER.info("User is away!");
|
||||
}
|
||||
lastUserActivityTime = now;
|
||||
}
|
||||
|
||||
userAway = loops >= LOOPS_BEFORE_AWAY;
|
||||
boolean away = now - lastUserActivityTime >= config.getAwayThresholdSeconds() * 1000L;
|
||||
if (away && !userAway) {
|
||||
LOGGER.info("User is away!");
|
||||
} else if (!away && userAway) {
|
||||
LOGGER.info("User is no longer away!");
|
||||
}
|
||||
userAway = away;
|
||||
|
||||
lastSeenX = location.x;
|
||||
lastSeenY = location.y;
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import wtf.beatrice.autosqueal.Main;
|
||||
import wtf.beatrice.autosqueal.config.AutoSquealConfig;
|
||||
import wtf.beatrice.autosqueal.controls.CursorMover;
|
||||
import wtf.beatrice.autosqueal.controls.RobotMouseTracker;
|
||||
import wtf.beatrice.autosqueal.listener.CursorMoveListener;
|
||||
@@ -35,10 +36,22 @@ public class MainWindow
|
||||
private final JFrame frame = new JFrame();
|
||||
private JButton toggleButton;
|
||||
|
||||
private final AutoSquealConfig config;
|
||||
|
||||
// the away detection tracks the user, not the automation: it lives from
|
||||
// the window's creation and is polled only while the automation runs
|
||||
private final RobotMouseTracker robotTracker = new RobotMouseTracker();
|
||||
private final CursorMoveListener awayDetector = new CursorMoveListener(robotTracker);
|
||||
private final RobotMouseTracker robotTracker;
|
||||
private final CursorMoveListener awayDetector;
|
||||
|
||||
/**
|
||||
* Creates the window around the given settings: the components read
|
||||
* them live, so a change applies without a restart.
|
||||
*/
|
||||
public MainWindow(AutoSquealConfig config) {
|
||||
this.config = config;
|
||||
this.robotTracker = new RobotMouseTracker();
|
||||
this.awayDetector = new CursorMoveListener(robotTracker, config);
|
||||
}
|
||||
|
||||
private ScheduledExecutorService periodicScheduler;
|
||||
private ExecutorService movementExecutor;
|
||||
|
||||
Reference in New Issue
Block a user