From 8c0a1932cf3d5cba0a51d7e0fa5459442aeace46 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 5 Oct 2021 16:16:32 +0200 Subject: [PATCH] add prevent computer sleep functionality --- .../sparrowwallet/sparrow/AppController.java | 10 +++++ .../sparrowwallet/sparrow/AppServices.java | 43 ++++++++++++++++++- .../com/sparrowwallet/sparrow/io/Config.java | 9 ++++ .../com/sparrowwallet/sparrow/app.fxml | 2 + 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 48261f8a..f4546269 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -146,6 +146,9 @@ public class AppController implements Initializable { @FXML private MenuItem sendToMany; + @FXML + private CheckMenuItem preventSleep; + @FXML private StackPane rootStack; @@ -289,6 +292,7 @@ public class AppController implements Initializable { lockWallet.setDisable(true); refreshWallet.disableProperty().bind(Bindings.or(exportWallet.disableProperty(), Bindings.or(serverToggle.disableProperty(), AppServices.onlineProperty().not()))); sendToMany.disableProperty().bind(exportWallet.disableProperty()); + preventSleep.setSelected(Config.get().isPreventSleep()); setServerType(Config.get().getServerType()); serverToggle.setSelected(isConnected()); @@ -742,6 +746,12 @@ public class AppController implements Initializable { EventManager.get().post(new BitcoinUnitChangedEvent(unit)); } + public void preventSleep(ActionEvent event) { + CheckMenuItem item = (CheckMenuItem)event.getSource(); + Config.get().setPreventSleep(item.isSelected()); + AppServices.get().setPreventSleep(item.isSelected()); + } + public void openFile(File file) { if(isWalletFile(file)) { openWalletFile(file, true); diff --git a/src/main/java/com/sparrowwallet/sparrow/AppServices.java b/src/main/java/com/sparrowwallet/sparrow/AppServices.java index 2c4f2f7c..a194f2ac 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppServices.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppServices.java @@ -2,7 +2,6 @@ package com.sparrowwallet.sparrow; import com.google.common.eventbus.Subscribe; import com.google.common.net.HostAndPort; -import com.samourai.whirlpool.client.wallet.WhirlpoolEventService; import com.sparrowwallet.drongo.Network; import com.sparrowwallet.drongo.address.Address; import com.sparrowwallet.drongo.protocol.BlockHeader; @@ -24,6 +23,7 @@ import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.concurrent.ScheduledService; +import javafx.concurrent.Task; import javafx.concurrent.Worker; import javafx.fxml.FXMLLoader; import javafx.scene.Node; @@ -48,6 +48,7 @@ import org.slf4j.LoggerFactory; import java.awt.*; import java.awt.desktop.OpenFilesHandler; import java.awt.desktop.OpenURIHandler; +import java.awt.event.KeyEvent; import java.io.File; import java.io.IOException; import java.net.*; @@ -94,6 +95,8 @@ public class AppServices { private TorService torService; + private ScheduledService preventSleepService; + private static Integer currentBlockHeight; private static BlockHeader latestBlockHeader; @@ -156,6 +159,7 @@ public class AppServices { ratesService = createRatesService(config.getExchangeSource(), config.getFiatCurrency()); versionCheckService = createVersionCheckService(); torService = createTorService(); + preventSleepService = createPreventSleepService(); onlineProperty.addListener(onlineServicesListener); @@ -183,6 +187,10 @@ public class AppServices { if(config.isCheckNewVersions() && Network.get() == Network.MAINNET) { restartService(versionCheckService); } + + if(config.isPreventSleep()) { + restartService(preventSleepService); + } } private void restartService(ScheduledService service) { @@ -382,6 +390,39 @@ public class AppServices { return torService; } + private ScheduledService createPreventSleepService() { + ScheduledService preventSleepService = new ScheduledService() { + @Override + protected Task createTask() { + return new Task<>() { + protected Void call() { + try { + Robot robot = new Robot(); + robot.keyRelease(KeyEvent.VK_F16); + } catch(Exception e) { + log.debug("Error preventing sleep", e); + } + + return null; + } + }; + } + }; + + preventSleepService.setPeriod(Duration.minutes(1)); + return preventSleepService; + } + + public void setPreventSleep(boolean preventSleep) { + if(preventSleepService != null) { + if(preventSleep) { + restartService(preventSleepService); + } else { + preventSleepService.cancel(); + } + } + } + public static boolean isTorRunning() { return Tor.getDefault() != null; } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Config.java b/src/main/java/com/sparrowwallet/sparrow/io/Config.java index 9a0d52b4..b78d2fc4 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Config.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Config.java @@ -42,6 +42,7 @@ public class Config { private boolean showTransactionHex = true; private boolean showLoadingLog = false; private boolean showUtxosChart = true; + private boolean preventSleep = false; private List recentWalletFiles; private Integer keyDerivationPeriod; private File hwi; @@ -279,6 +280,14 @@ public class Config { flush(); } + public boolean isPreventSleep() { + return preventSleep; + } + + public void setPreventSleep(boolean preventSleep) { + this.preventSleep = preventSleep; + } + public List getRecentWalletFiles() { return recentWalletFiles; } diff --git a/src/main/resources/com/sparrowwallet/sparrow/app.fxml b/src/main/resources/com/sparrowwallet/sparrow/app.fxml index d9053c70..7791efc3 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/app.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/app.fxml @@ -108,6 +108,8 @@ + +