Skip to content

Commit b70a500

Browse files
committed
Merge branch 'fix-timer-cancel-on-stop' into 'master'
Fix: Exception on stop because timer already cancelled & input prompt See merge request group-bear/mouse-automove!1
2 parents 595dea4 + ec386f6 commit b70a500

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# mouse-automove
22

33
## Background
4-
My employee is financial institution, therefore a lot of securities were imposed on laptop.
4+
My employer is financial institution, therefore a lot of securities were imposed on laptop.
55
Although the laptop could have restricted access to internet, but any download will be blocked.
66
Setting was made so that the laptop will be lock after few minutes, good when in office, but a hassle when working from home.
77
Since download was blocked, so anti-idle application could not be downloaded and used.
88

9-
Therefore, the app was born, so that I could make a coffee with ease of mind.
9+
Therefore, the app was born, so that I could make a coffee with ease of mind.
1010

1111
## Introduction
1212
**mouse-automove** is simple Java application that uses Java AWT **Robot API** to move mouse randomly.

script/compile.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@ECHO OFF
2+
TITLE mouse-automation-compile
23
CD ..
34
IF EXIST bin (RMDIR /S /Q bin)
45
MKDIR bin

script/run.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@ECHO OFF
2+
TITLE mouse-automation-run
23
cd ..\bin
34
java -jar mouse-app.jar
45
cd ..\script

src/com/aizuddindeyn/mouse/MouseApplication.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,37 @@
44
*/
55
package com.aizuddindeyn.mouse;
66

7+
import java.text.MessageFormat;
8+
import java.util.Scanner;
9+
710
/**
811
* @author aizuddindeyn
912
* @date 11/7/2020
1013
*/
1114
public class MouseApplication {
1215

16+
private static final Scanner SCANNER = new Scanner(System.in);
17+
1318
public static void main(String[] args) {
1419
MouseUtils.log("Starting application");
1520

1621
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
1722
MouseInstance.getInstance().stop();
23+
SCANNER.close();
1824
MouseUtils.log("Stopping application");
1925
}));
2026

2127
MouseInstance.getInstance().start();
2228

2329
while (true) {
2430
try {
25-
int key = System.in.read();
31+
String status = (MouseInstance.getInstance().isStarted()) ? "started" : "paused";
32+
String action = (MouseInstance.getInstance().isStarted()) ? "pause" : "resume";
33+
System.out.println();
34+
System.out.print(MessageFormat.format("App {0}. Press 'p' and Enter to {1}: ", status, action));
35+
String input = SCANNER.next();
2636
// Type 'p' to pause or resume
27-
if (key == 112) {
37+
if ((input != null && !input.isEmpty()) && 'p' == input.charAt(0)) {
2838
if (MouseInstance.getInstance().isStarted()) {
2939
MouseInstance.getInstance().stop();
3040
} else {

src/com/aizuddindeyn/mouse/MouseInstance.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ public static MouseInstance getInstance() {
3535
return INSTANCE;
3636
}
3737

38-
public void start() {
38+
public synchronized void start() {
3939
timer = new Timer();
4040
MouseUtils.log("Timer started");
4141

4242
new MouseTask(timer, INSTANCE).run();
4343
started = true;
4444
}
4545

46-
public void stop() {
46+
public synchronized void stop() {
4747
timer.cancel();
4848
started = false;
4949
MouseUtils.log("Timer stopped");
@@ -63,7 +63,7 @@ public void execute() {
6363
}
6464
}
6565

66-
public boolean isStarted() {
66+
public synchronized boolean isStarted() {
6767
return started;
6868
}
6969
}

src/com/aizuddindeyn/mouse/MouseTask.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public void run() {
3434
private void setup() {
3535
int seconds = MouseUtils.generateIntervalSeconds(DELAY_TIMES_MIN, (DELAY_TIMES_MAX - DELAY_TIMES_MIN));
3636
MouseUtils.log("Next interval: " + seconds + "s");
37-
timer.schedule(new MouseTask(timer, instance), seconds * 1000L);
37+
if (instance.isStarted()) {
38+
timer.schedule(new MouseTask(timer, instance), seconds * 1000L);
39+
}
3840
}
3941
}

0 commit comments

Comments
 (0)