From ce131e4653d5666f633b8507f3d5b770a4a1f7f1 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Fri, 6 Nov 2020 14:46:32 +0100 Subject: [PATCH] Implemented AutoStart integration for macOS --- .../ui/launcher/UiLauncherModule.java | 8 +++++++ .../ui/preferences/AutoStartMacStrategy.java | 22 +++++++++++-------- .../ui/preferences/AutoStartModule.java | 8 ++++--- .../ui/preferences/AutoStartStrategy.java | 1 + .../ui/preferences/AutoStartWinStrategy.java | 2 ++ 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/main/ui/src/main/java/org/cryptomator/ui/launcher/UiLauncherModule.java b/main/ui/src/main/java/org/cryptomator/ui/launcher/UiLauncherModule.java index fdf31b491..63f119291 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/launcher/UiLauncherModule.java +++ b/main/ui/src/main/java/org/cryptomator/ui/launcher/UiLauncherModule.java @@ -3,6 +3,7 @@ package org.cryptomator.ui.launcher; import dagger.Module; import dagger.Provides; import org.cryptomator.common.JniModule; +import org.cryptomator.integrations.autostart.AutoStartProvider; import org.cryptomator.integrations.tray.TrayIntegrationProvider; import org.cryptomator.integrations.uiappearance.UiAppearanceProvider; import org.cryptomator.ui.fxapp.FxApplicationComponent; @@ -25,6 +26,13 @@ public abstract class UiLauncherModule { return ServiceLoader.load(UiAppearanceProvider.class).findFirst(); } + @Provides + @Singleton + static Optional provideAutostartProvider() { + return ServiceLoader.load(AutoStartProvider.class).findFirst(); + } + + @Provides @Singleton static Optional provideTrayIntegrationProvider() { diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartMacStrategy.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartMacStrategy.java index 0bc469a77..7eeb2a72d 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartMacStrategy.java +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartMacStrategy.java @@ -1,5 +1,7 @@ package org.cryptomator.ui.preferences; +import org.cryptomator.integrations.autostart.AutoStartProvider; +import org.cryptomator.integrations.autostart.ToggleAutoStartFailedException; import org.cryptomator.jni.MacFunctions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -7,36 +9,38 @@ import org.slf4j.LoggerFactory; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; +@Deprecated class AutoStartMacStrategy implements AutoStartStrategy { private static final Logger LOG = LoggerFactory.getLogger(AutoStartMacStrategy.class); - private final MacFunctions macFunctions; + private final AutoStartProvider autoStartProvider; - public AutoStartMacStrategy(MacFunctions macFunctions) { - this.macFunctions = macFunctions; + public AutoStartMacStrategy(AutoStartProvider autoStartProvider) { + this.autoStartProvider = autoStartProvider; } @Override public CompletionStage isAutoStartEnabled() { - boolean enabled = macFunctions.launchServices().isLoginItemEnabled(); - return CompletableFuture.completedFuture(enabled); + return CompletableFuture.completedFuture(autoStartProvider.isEnabled()); } @Override public void enableAutoStart() throws TogglingAutoStartFailedException { - if (macFunctions.launchServices().enableLoginItem()) { + try { + autoStartProvider.enable(); LOG.debug("Added login item."); - } else { + } catch (ToggleAutoStartFailedException e) { throw new TogglingAutoStartFailedException("Failed to add login item."); } } @Override public void disableAutoStart() throws TogglingAutoStartFailedException { - if (macFunctions.launchServices().disableLoginItem()) { + try { + autoStartProvider.disable(); LOG.debug("Removed login item."); - } else { + } catch (ToggleAutoStartFailedException e) { throw new TogglingAutoStartFailedException("Failed to remove login item."); } } diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartModule.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartModule.java index fa01756ab..c84ad5ac8 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartModule.java +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartModule.java @@ -4,18 +4,20 @@ import dagger.Module; import dagger.Provides; import org.apache.commons.lang3.SystemUtils; import org.cryptomator.common.Environment; +import org.cryptomator.integrations.autostart.AutoStartProvider; import org.cryptomator.jni.MacFunctions; import java.util.Optional; +@Deprecated @Module abstract class AutoStartModule { @Provides @PreferencesScoped - public static Optional provideAutoStartStrategy(Optional macFunctions, Environment env) { - if (SystemUtils.IS_OS_MAC_OSX && macFunctions.isPresent()) { - return Optional.of(new AutoStartMacStrategy(macFunctions.get())); + public static Optional provideAutoStartStrategy(Optional autoStartProvider) { + if (SystemUtils.IS_OS_MAC_OSX && autoStartProvider.isPresent()) { + return Optional.of(new AutoStartMacStrategy(autoStartProvider.get())); } else if (SystemUtils.IS_OS_WINDOWS) { Optional exeName = ProcessHandle.current().info().command(); return exeName.map(AutoStartWinStrategy::new); diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartStrategy.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartStrategy.java index 64ad27abc..9848e58f8 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartStrategy.java +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartStrategy.java @@ -2,6 +2,7 @@ package org.cryptomator.ui.preferences; import java.util.concurrent.CompletionStage; +@Deprecated public interface AutoStartStrategy { CompletionStage isAutoStartEnabled(); diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartWinStrategy.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartWinStrategy.java index 438c468bd..b3c0a4674 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartWinStrategy.java +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/AutoStartWinStrategy.java @@ -25,7 +25,9 @@ import java.util.concurrent.TimeUnit; * To disable it, first it is determined by an internal state, which strategies must be used and in the second step those are executed. * * @apiNote This class is not thread safe, hence it should be avoided to call its methods simultaniously by different threads. + * @deprecated To be moved to integration-win project */ +@Deprecated class AutoStartWinStrategy implements AutoStartStrategy { private static final Logger LOG = LoggerFactory.getLogger(AutoStartWinStrategy.class);