diff options
author | Samuel Holland <samuel@sholland.org> | 2018-01-07 21:52:14 -0600 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2018-01-07 21:52:14 -0600 |
commit | 1f30e133d6995689a89add2d0f87102cadf6938b (patch) | |
tree | 219cb8afecf9be30454e5aa1ea3008c097298c08 /app/src | |
parent | 4a3d68bb7df598aab64f7fcd8a4d43eaa31dc6bf (diff) |
TunnelManager: Simplify save/resume methods
Signed-off-by: Samuel Holland <samuel@sholland.org>
Diffstat (limited to 'app/src')
-rw-r--r-- | app/src/main/java/com/wireguard/android/BootShutdownReceiver.java | 2 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/android/model/TunnelManager.java | 17 |
2 files changed, 9 insertions, 10 deletions
diff --git a/app/src/main/java/com/wireguard/android/BootShutdownReceiver.java b/app/src/main/java/com/wireguard/android/BootShutdownReceiver.java index 37de83c0..6c5418ba 100644 --- a/app/src/main/java/com/wireguard/android/BootShutdownReceiver.java +++ b/app/src/main/java/com/wireguard/android/BootShutdownReceiver.java @@ -22,7 +22,7 @@ public class BootShutdownReceiver extends BroadcastReceiver { tunnelManager.restoreState().whenComplete(ExceptionLoggers.D); } else if (Intent.ACTION_SHUTDOWN.equals(action)) { Log.d(TAG, "Broadcast receiver saving state (shutdown)"); - tunnelManager.saveState().whenComplete(ExceptionLoggers.D); + tunnelManager.saveState(); } } } diff --git a/app/src/main/java/com/wireguard/android/model/TunnelManager.java b/app/src/main/java/com/wireguard/android/model/TunnelManager.java index 09c33b2d..feb60124 100644 --- a/app/src/main/java/com/wireguard/android/model/TunnelManager.java +++ b/app/src/main/java/com/wireguard/android/model/TunnelManager.java @@ -197,22 +197,21 @@ public final class TunnelManager extends BaseObservable { public CompletionStage<Void> restoreState() { if (!preferences.getBoolean(KEY_RESTORE_ON_BOOT, false)) return CompletableFuture.completedFuture(null); - final Set<String> tunnelsToEnable = - preferences.getStringSet(KEY_RUNNING_TUNNELS, Collections.emptySet()); - final CompletableFuture[] futures = StreamSupport.stream(tunnelsToEnable) - .map(tunnels::get) - .map(tunnel -> tunnel.setState(State.UP)) - .toArray(CompletableFuture[]::new); - return CompletableFuture.allOf(futures); + final Set<String> previouslyRunning = preferences.getStringSet(KEY_RUNNING_TUNNELS, null); + if (previouslyRunning == null) + return CompletableFuture.completedFuture(null); + return CompletableFuture.allOf(StreamSupport.stream(tunnels) + .filter(tunnel -> previouslyRunning.contains(tunnel.getName())) + .map(tunnel -> setTunnelState(tunnel, State.UP)) + .toArray(CompletableFuture[]::new)); } - public CompletionStage<Void> saveState() { + public void saveState() { final Set<String> runningTunnels = StreamSupport.stream(tunnels) .filter(tunnel -> tunnel.getState() == State.UP) .map(Tunnel::getName) .collect(Collectors.toUnmodifiableSet()); preferences.edit().putStringSet(KEY_RUNNING_TUNNELS, runningTunnels).apply(); - return CompletableFuture.completedFuture(null); } private void setLastUsedTunnel(final Tunnel tunnel) { |