mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-05-19 11:11:28 +00:00
Have multiple services
This commit is contained in:
@@ -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> updateService;
|
||||
private final List<UpdateService> updateServices;
|
||||
|
||||
@Inject
|
||||
public AppUpdateChecker(Optional<UpdateService> updateService) {
|
||||
this.updateService = updateService;
|
||||
public AppUpdateChecker(List<UpdateService> updateServices) {
|
||||
this.updateServices = updateServices;
|
||||
}
|
||||
|
||||
public boolean isUpdateServiceAvailable() {
|
||||
return updateService.isPresent();
|
||||
public boolean isUpdateServiceAvailable(Optional<String> 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<UpdateService> 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<UpdateService> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user