diff --git a/main/ui/src/main/java/org/cryptomator/ui/common/VaultService.java b/main/ui/src/main/java/org/cryptomator/ui/common/VaultService.java index 68e1fecb0..b215c0c36 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/common/VaultService.java +++ b/main/ui/src/main/java/org/cryptomator/ui/common/VaultService.java @@ -62,22 +62,12 @@ public class VaultService { if (!keychain.isPresent()) { LOG.debug("No system keychain found. Unable to auto unlock without saved passwords."); } else { - for (Vault vault : vaults) { - attemptAutoUnlock(vault, keychain.get()); - } + List> unlockTasks = vaults.stream().map(v -> createAutoUnlockTask(v, keychain.get())).collect(Collectors.toList()); + Task> runSequentiallyTask = new RunSequentiallyTask(unlockTasks); + executorService.execute(runSequentiallyTask); } } - /** - * Unlocks a vault in a background thread using a stored passphrase - * - * @param vault The vault to unlock - * @param keychainAccess The system keychain holding the passphrase for the vault - */ - public void attemptAutoUnlock(Vault vault, KeychainAccess keychainAccess) { - executorService.execute(createAutoUnlockTask(vault, keychainAccess)); - } - /** * Creates but doesn't start an auto-unlock task. * @@ -199,14 +189,14 @@ public class VaultService { } @Override - protected Collection call() throws Exception { + protected Collection call() throws ExecutionException, InterruptedException { Iterator> remainingTasks = startedTasks.iterator(); Collection completed = new ArrayList<>(); try { // wait for all tasks: while (remainingTasks.hasNext()) { - Vault lockedVault = remainingTasks.next().get(); - completed.add(lockedVault); + Vault done = remainingTasks.next().get(); + completed.add(done); } } catch (ExecutionException e) { // cancel all remaining: @@ -215,7 +205,30 @@ public class VaultService { } throw e; } - return List.copyOf(completed); + return completed; + } + } + + /** + * A task that runs a list of tasks in their given order + */ + private static class RunSequentiallyTask extends Task> { + + private final List> tasks; + + public RunSequentiallyTask(List> tasks) { + this.tasks = List.copyOf(tasks); + } + + @Override + protected List call() throws ExecutionException, InterruptedException { + List completed = new ArrayList<>(); + for (Task task : tasks) { + task.run(); + Vault done = task.get(); + completed.add(done); + } + return completed; } }