Implementing new settings and locking after a certain amount of minutes

This commit is contained in:
Martin Beyer
2021-03-29 10:44:39 +02:00
parent 8592e3667b
commit 1292548042
6 changed files with 64 additions and 14 deletions

View File

@@ -40,6 +40,8 @@ public class VaultSettings {
public static final boolean DEFAULT_LOCK_ON_SLEEP = false;
public static final boolean DEFAULT_LOCK_AFTER_IDLETIME = false;
public static final String DEFAULT_LOCK_IDLETIME_IN_MINUTES = "30";
public static final boolean DEFAULT_LOCK_AFTER_TIME = false;
public static final String DEFAULT_LOCK_TIME_IN_MINUTES = "30";
private static final Random RNG = new Random();
@@ -58,6 +60,8 @@ public class VaultSettings {
private final BooleanProperty lockOnSleep = new SimpleBooleanProperty(DEFAULT_LOCK_ON_SLEEP);
private final BooleanProperty lockAfterIdleTime = new SimpleBooleanProperty(DEFAULT_LOCK_AFTER_IDLETIME);
private final StringProperty lockIdleTimeInMinutes = new SimpleStringProperty(DEFAULT_LOCK_IDLETIME_IN_MINUTES);
private final BooleanProperty lockAfterTime = new SimpleBooleanProperty(DEFAULT_LOCK_AFTER_TIME);
private final StringProperty lockTimeInMinutes = new SimpleStringProperty(DEFAULT_LOCK_TIME_IN_MINUTES);
private final StringBinding mountName;
public VaultSettings(String id) {
@@ -66,7 +70,7 @@ public class VaultSettings {
}
Observable[] observables() {
return new Observable[]{path, displayName, winDriveLetter, unlockAfterStartup, revealAfterMount, useCustomMountPath, customMountPath, usesReadOnlyMode, mountFlags, filenameLengthLimit, actionAfterUnlock, lockOnSleep, lockAfterIdleTime, lockIdleTimeInMinutes};
return new Observable[]{path, displayName, winDriveLetter, unlockAfterStartup, revealAfterMount, useCustomMountPath, customMountPath, usesReadOnlyMode, mountFlags, filenameLengthLimit, actionAfterUnlock, lockOnSleep, lockAfterIdleTime, lockIdleTimeInMinutes, lockAfterTime, lockTimeInMinutes};
}
public static VaultSettings withRandomId() {
@@ -179,6 +183,14 @@ public class VaultSettings {
return lockIdleTimeInMinutes;
}
public BooleanProperty lockAfterTime() {
return lockAfterTime;
}
public StringProperty lockTimeInMinutes() {
return lockTimeInMinutes;
}
/* Hashcode/Equals */
@Override

View File

@@ -34,6 +34,8 @@ class VaultSettingsJsonAdapter {
out.name("lockOnSleep").value(value.lockOnSleep().get());
out.name("lockAfterIdleTime").value(value.lockAfterIdleTime().get());
out.name("lockIdleTimeInMinutes").value(value.lockIdleTimeInMinutes().get());
out.name("lockAfterTime").value(value.lockAfterTime().get());
out.name("lockTimeInMinutes").value(value.lockTimeInMinutes().get());
out.endObject();
}
@@ -54,6 +56,8 @@ class VaultSettingsJsonAdapter {
boolean lockOnSleep = VaultSettings.DEFAULT_LOCK_ON_SLEEP;
boolean lockAfterIdleTime = VaultSettings.DEFAULT_LOCK_AFTER_IDLETIME;
String lockIdleTimeInMinutes = VaultSettings.DEFAULT_LOCK_IDLETIME_IN_MINUTES;
boolean lockAfterTime = VaultSettings.DEFAULT_LOCK_AFTER_TIME;
String lockTimeInMinutes = VaultSettings.DEFAULT_LOCK_TIME_IN_MINUTES;
in.beginObject();
while (in.hasNext()) {
@@ -75,6 +79,8 @@ class VaultSettingsJsonAdapter {
case "lockOnSleep" -> lockOnSleep = in.nextBoolean();
case "lockAfterIdleTime" -> lockAfterIdleTime = in.nextBoolean();
case "lockIdleTimeInMinutes" -> lockIdleTimeInMinutes = in.nextString();
case "lockAfterTime" -> lockAfterTime = in.nextBoolean();
case "lockTimeInMinutes" -> lockTimeInMinutes = in.nextString();
default -> {
LOG.warn("Unsupported vault setting found in JSON: " + name);
in.skipValue();
@@ -102,6 +108,8 @@ class VaultSettingsJsonAdapter {
vaultSettings.lockOnSleep().set(lockOnSleep);
vaultSettings.lockAfterIdleTime().set(lockAfterIdleTime);
vaultSettings.lockIdleTimeInMinutes().set(lockIdleTimeInMinutes);
vaultSettings.lockAfterTime().set(lockAfterTime);
vaultSettings.lockTimeInMinutes().set(lockTimeInMinutes);
return vaultSettings;
}

View File

@@ -15,9 +15,9 @@ import org.cryptomator.common.settings.UiTheme;
import org.cryptomator.common.vaults.Vault;
import org.cryptomator.common.vaults.VaultListManager;
import org.cryptomator.common.vaults.Volume;
import org.cryptomator.integrations.autolock.AutoLockException;
import org.cryptomator.integrations.autolock.AutoLockProvider;
import org.cryptomator.integrations.autolock.SystemState;
//import org.cryptomator.integrations.autolock.AutoLockException;
//import org.cryptomator.integrations.autolock.AutoLockProvider;
//import org.cryptomator.integrations.autolock.SystemState;
import org.cryptomator.integrations.tray.TrayIntegrationProvider;
import org.cryptomator.integrations.uiappearance.Theme;
import org.cryptomator.integrations.uiappearance.UiAppearanceException;
@@ -36,8 +36,7 @@ import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.inject.Provider;
import java.awt.desktop.QuitResponse;
import java.awt.desktop.SystemSleepEvent;
import java.awt.desktop.SystemSleepListener;
import java.util.Date;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
@@ -61,10 +60,12 @@ public class FxApplication extends Application {
private final BooleanBinding hasVisibleWindows;
private final UiAppearanceListener systemInterfaceThemeListener = this::systemInterfaceThemeChanged;
private final VaultListManager vaultListManager;
private final Optional<AutoLockProvider> autoLockProvider;
//private final Optional<AutoLockProvider> autoLockProvider;
@Inject
FxApplication(Settings settings, Lazy<MainWindowComponent> mainWindow, Lazy<PreferencesComponent> preferencesWindow, Provider<UnlockComponent.Builder> unlockWindowBuilderProvider, Provider<LockComponent.Builder> lockWindowBuilderProvider, Lazy<QuitComponent> quitWindow, Optional<TrayIntegrationProvider> trayIntegration, Optional<UiAppearanceProvider> appearanceProvider, VaultService vaultService, LicenseHolder licenseHolder, VaultListManager vaultListManager, Optional<AutoLockProvider> autoLockProvider) {
//FxApplication(Settings settings, Lazy<MainWindowComponent> mainWindow, Lazy<PreferencesComponent> preferencesWindow, Provider<UnlockComponent.Builder> unlockWindowBuilderProvider, Provider<LockComponent.Builder> lockWindowBuilderProvider, Lazy<QuitComponent> quitWindow, Optional<TrayIntegrationProvider> trayIntegration, Optional<UiAppearanceProvider> appearanceProvider, VaultService vaultService, LicenseHolder licenseHolder, VaultListManager vaultListManager, Optional<AutoLockProvider> autoLockProvider) {
FxApplication(Settings settings, Lazy<MainWindowComponent> mainWindow, Lazy<PreferencesComponent> preferencesWindow, Provider<UnlockComponent.Builder> unlockWindowBuilderProvider, Provider<LockComponent.Builder> lockWindowBuilderProvider, Lazy<QuitComponent> quitWindow, Optional<TrayIntegrationProvider> trayIntegration, Optional<UiAppearanceProvider> appearanceProvider, VaultService vaultService, LicenseHolder licenseHolder, VaultListManager vaultListManager) {
this.settings = settings;
this.mainWindow = mainWindow;
this.preferencesWindow = preferencesWindow;
@@ -78,7 +79,7 @@ public class FxApplication extends Application {
this.visibleWindows = Stage.getWindows().filtered(Window::isShowing);
this.hasVisibleWindows = Bindings.isNotEmpty(visibleWindows);
this.vaultListManager = vaultListManager;
this.autoLockProvider = autoLockProvider;
//this.autoLockProvider = autoLockProvider;
}
public void start() {
@@ -89,7 +90,7 @@ public class FxApplication extends Application {
settings.theme().addListener(this::appThemeChanged);
loadSelectedStyleSheet(settings.theme().get());
applySystemListener();
//applySystemListener();
}
@Override
@@ -128,6 +129,7 @@ public class FxApplication extends Application {
unlockWindowBuilderProvider.get().vault(vault).owner(owner).build().startUnlockWorkflow();
LOG.debug("Showing UnlockWindow for {}", vault.getDisplayName());
});
checkAutolock(vault, owner);
}
public void startLockWorkflow(Vault vault, Optional<Stage> owner) {
@@ -204,7 +206,7 @@ public class FxApplication extends Application {
});
}
private void applySystemListener() {
/*private void applySystemListener() {
autoLockProvider.ifPresent(autoLockProvider -> {
try {
autoLockProvider.addListener(this::systemInterfaceStateChanged);
@@ -229,5 +231,21 @@ public class FxApplication extends Application {
}
});
}
*/
public void checkAutolock(Vault vault, Optional<Stage> owner){
if (vault.getVaultSettings().lockAfterTime().get()){ //TODO: this is lock after a fixed amount of minutes
LOG.info("Locking after {} minutes", vault.getVaultSettings().lockTimeInMinutes().get());
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
startLockWorkflow(vault, owner);
}
},
new Date(System.currentTimeMillis() + Integer.parseInt(vault.getVaultSettings().lockTimeInMinutes().get()) * 60 * 1000)
);
}
}
}

View File

@@ -19,13 +19,14 @@ public class AutoLockVaultOptionsController implements FxController {
private final Stage window;
public CheckBox lockOnSleepCheckbox;
public CheckBox lockAfterIdleTimeCheckbox;
public CheckBox lockAfterTimeCheckbox;
public TextField lockIdleTimeInMinutesTextField;
public TextField lockTimeInMinutesTextField;
@Inject
AutoLockVaultOptionsController(@VaultOptionsWindow Stage window, @VaultOptionsWindow Vault vault, ResourceBundle resourceBundle) {
this.window = window;
this.vault = vault;
//this.resourceBundle = resourceBundle;
}
@FXML
@@ -33,5 +34,7 @@ public class AutoLockVaultOptionsController implements FxController {
lockOnSleepCheckbox.selectedProperty().bindBidirectional(vault.getVaultSettings().lockOnSleep());
lockAfterIdleTimeCheckbox.selectedProperty().bindBidirectional(vault.getVaultSettings().lockAfterIdleTime());
lockIdleTimeInMinutesTextField.textProperty().bindBidirectional(vault.getVaultSettings().lockIdleTimeInMinutes());
lockAfterTimeCheckbox.selectedProperty().bindBidirectional(vault.getVaultSettings().lockAfterTime());
lockTimeInMinutesTextField.textProperty().bindBidirectional(vault.getVaultSettings().lockTimeInMinutes());
}
}

View File

@@ -15,11 +15,19 @@
<Insets topRightBottomLeft="12"/>
</padding>
<children>
<TextFlow styleClass="text-flow" prefWidth="-Infinity">
<CheckBox text="%vaultOptions.autoLock.lockAfterTimePart1" fx:id="lockAfterTimeCheckbox"/>
<Text text=" "/>
<TextField fx:id="lockTimeInMinutesTextField" prefWidth="50"/>
<Text text=" "/>
<FormattedLabel format="%vaultOptions.autoLock.lockAfterTimePart2"/>
</TextFlow>
<CheckBox text="%vaultOptions.autoLock.lockOnSleep" fx:id="lockOnSleepCheckbox"/>
<TextFlow styleClass="text-flow" prefWidth="-Infinity">
<CheckBox text="%vaultOptions.autoLock.lockAfterIdleTimePart1" fx:id="lockAfterIdleTimeCheckbox"/>
<Text text=" "/>
<TextField fx:id="lockIdleTimeInMinutesTextField" prefWidth="50"/> <!--prefWidth="${controller.calculatedWidthForTextfield}"/>-->
<TextField fx:id="lockIdleTimeInMinutesTextField" prefWidth="50"/>
<Text text=" "/>
<FormattedLabel format="%vaultOptions.autoLock.lockAfterIdleTimePart2"/>
</TextFlow>

View File

@@ -297,7 +297,8 @@ vaultOptions.autoLock=Auto-Lock
vaultOptions.autoLock.lockOnSleep=Lock on sleep
vaultOptions.autoLock.lockAfterIdleTimePart1=Lock after computer is idle for
vaultOptions.autoLock.lockAfterIdleTimePart2=minutes.
vaultOptions.autoLock.lockAfterAmountOfIdleTime=Following minutes are set:
vaultOptions.autoLock.lockAfterTimePart1=Lock after
vaultOptions.autoLock.lockAfterTimePart2=minutes.
# Recovery Key
recoveryKey.title=Recovery Key