diff --git a/src/main/java/org/cryptomator/common/updates/AppUpdateChecker.java b/src/main/java/org/cryptomator/common/updates/AppUpdateChecker.java index 707e07615..aaa90e737 100644 --- a/src/main/java/org/cryptomator/common/updates/AppUpdateChecker.java +++ b/src/main/java/org/cryptomator/common/updates/AppUpdateChecker.java @@ -1,36 +1,71 @@ package org.cryptomator.common.updates; +import org.cryptomator.integrations.common.DistributionChannel; import org.cryptomator.integrations.update.UpdateService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.inject.Inject; +import java.util.List; import java.util.Optional; public class AppUpdateChecker { private static final Logger LOG = LoggerFactory.getLogger(AppUpdateChecker.class); - private final Optional updateService; + private final List updateServices; @Inject - public AppUpdateChecker(Optional updateService) { - this.updateService = updateService; + public AppUpdateChecker(List updateServices) { + this.updateServices = updateServices; } - public boolean isUpdateServiceAvailable() { - return updateService.isPresent(); + public boolean isUpdateServiceAvailable(Optional buildNumber) { + if (buildNumber.isEmpty()) { + return false; + } + switch (buildNumber.get()) { + case "flatpak-1" -> { + return !updateServices.isEmpty() && doServicesContainChannel(updateServices, DistributionChannel.Value.LINUX_FLATPAK); + } + + default -> { + LOG.error("Unexpected value 'buildNumber': {}", buildNumber.get()); + return false; + } + } } - public String checkForUpdates(UpdateService.DistributionChannel channel) { - if (!updateService.isPresent()) { + public String checkForUpdates(DistributionChannel.Value channel) { + if (updateServices.isEmpty()) { LOG.error("No UpdateService found"); return null; } switch (channel) { case LINUX_FLATPAK -> { - return updateService.map(service -> service.isUpdateAvailable(UpdateService.DistributionChannel.LINUX_FLATPAK)).orElse(null); + var flatpakService = getServiceForChannel(updateServices, DistributionChannel.Value.LINUX_FLATPAK); + if(null == flatpakService) { + LOG.error("Required service for channel LINUX_FLATPAK not available"); + return null; + } else { + return flatpakService.isUpdateAvailable(DistributionChannel.Value.LINUX_FLATPAK); + } } - default -> throw new IllegalStateException("Unexpected value: " + channel); + default -> throw new IllegalStateException("Unexpected value 'channel': " + channel); } } + + private boolean doServicesContainChannel(List services, DistributionChannel.Value requiredChannel) { + return services.stream().anyMatch(service -> { + DistributionChannel annotation = service.getClass().getAnnotation(DistributionChannel.class); + return annotation != null && annotation.value() == requiredChannel; + }); + } + + private UpdateService getServiceForChannel(List services, DistributionChannel.Value requiredChannel) { + return services.stream().filter(service -> { + DistributionChannel annotation = service.getClass().getAnnotation(DistributionChannel.class); + return annotation != null && annotation.value() == requiredChannel; + }).findFirst().orElse(null); + } + } diff --git a/src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java b/src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java index 0854d9d90..07b401234 100644 --- a/src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java +++ b/src/main/java/org/cryptomator/ui/fxapp/UpdateChecker.java @@ -4,6 +4,7 @@ import org.cryptomator.common.Environment; import org.cryptomator.common.SemVerComparator; import org.cryptomator.common.settings.Settings; import org.cryptomator.common.updates.AppUpdateChecker; +import org.cryptomator.integrations.common.DistributionChannel; import org.cryptomator.integrations.update.UpdateService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,9 +57,13 @@ public class UpdateChecker { public void automaticallyCheckForUpdatesIfEnabled() { if (!env.disableUpdateCheck() && settings.checkForUpdates.get()) { - if (updateChecker.isUpdateServiceAvailable()) { // prefer AppUpdateChecker - var x = updateChecker.checkForUpdates(UpdateService.DistributionChannel.LINUX_FLATPAK); - LOG.info("Retrieved version from Update Service {}", x); + if (updateChecker.isUpdateServiceAvailable(env.getBuildNumber())) { // prefer AppUpdateChecker + String version = ""; + switch (env.getBuildNumber().get()) { + case "flatpak-1" -> version = updateChecker.checkForUpdates(DistributionChannel.Value.LINUX_FLATPAK); + default -> LOG.error("Unexpected value 'buildNumber': {}", env.getBuildNumber().get()); + } + LOG.info("Retrieved version from Update Service {}", version); } else { // fallback is the "redirect user to website" approach LOG.info("Common \"redirect user to website\" approach"); startCheckingForUpdates(AUTO_CHECK_DELAY);