fix away detection, pause while user is present
This commit is contained in:
@@ -8,11 +8,15 @@ import java.awt.*;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
/**
|
||||
* Periodically starts a new movement of the mouse cursor to a random position,
|
||||
* or to the top-right corner of the screen for a double click.
|
||||
*
|
||||
* The movements only happen when the user is away: while the user is actively
|
||||
* using the machine, they are skipped, so the app never fights for the mouse.
|
||||
*
|
||||
* The movement itself is executed by a {@link SingleStepMovementTask}, which
|
||||
* re-schedules itself on the shared scheduler until it reaches its destination.
|
||||
*/
|
||||
@@ -24,16 +28,25 @@ public class CursorMover implements Runnable
|
||||
|
||||
private final Random random = new SecureRandom();
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final RobotMouseTracker robotTracker;
|
||||
private final BooleanSupplier isUserAway;
|
||||
|
||||
private int iteration = 0;
|
||||
|
||||
public CursorMover(ScheduledExecutorService scheduler) {
|
||||
public CursorMover(ScheduledExecutorService scheduler, RobotMouseTracker robotTracker, BooleanSupplier isUserAway) {
|
||||
this.scheduler = scheduler;
|
||||
this.robotTracker = robotTracker;
|
||||
this.isUserAway = isUserAway;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
if (!isUserAway.getAsBoolean()) {
|
||||
LOGGER.debug("User is present, skipping movement");
|
||||
return;
|
||||
}
|
||||
|
||||
Point location = MouseInfo.getPointerInfo().getLocation();
|
||||
LOGGER.info("Starting coordinates: {}, {}", location.x, location.y);
|
||||
|
||||
@@ -58,7 +71,7 @@ public class CursorMover implements Runnable
|
||||
LOGGER.info("Destination coordinates: {}, {}", destX, destY);
|
||||
|
||||
try {
|
||||
SingleStepMovementTask movement = new SingleStepMovementTask(scheduler, destX, destY, click);
|
||||
SingleStepMovementTask movement = new SingleStepMovementTask(scheduler, robotTracker, destX, destY, click);
|
||||
scheduler.execute(movement);
|
||||
} catch (AWTException ex) {
|
||||
LOGGER.error("Could not start movement task", ex);
|
||||
|
||||
Reference in New Issue
Block a user