TunnelManager: Simplify save/resume methods

Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
Samuel Holland 2018-01-07 21:52:14 -06:00
parent 4a3d68bb7d
commit 1f30e133d6
2 changed files with 9 additions and 10 deletions

View File

@ -22,7 +22,7 @@ public class BootShutdownReceiver extends BroadcastReceiver {
tunnelManager.restoreState().whenComplete(ExceptionLoggers.D); tunnelManager.restoreState().whenComplete(ExceptionLoggers.D);
} else if (Intent.ACTION_SHUTDOWN.equals(action)) { } else if (Intent.ACTION_SHUTDOWN.equals(action)) {
Log.d(TAG, "Broadcast receiver saving state (shutdown)"); Log.d(TAG, "Broadcast receiver saving state (shutdown)");
tunnelManager.saveState().whenComplete(ExceptionLoggers.D); tunnelManager.saveState();
} }
} }
} }

View File

@ -197,22 +197,21 @@ public final class TunnelManager extends BaseObservable {
public CompletionStage<Void> restoreState() { public CompletionStage<Void> restoreState() {
if (!preferences.getBoolean(KEY_RESTORE_ON_BOOT, false)) if (!preferences.getBoolean(KEY_RESTORE_ON_BOOT, false))
return CompletableFuture.completedFuture(null); return CompletableFuture.completedFuture(null);
final Set<String> tunnelsToEnable = final Set<String> previouslyRunning = preferences.getStringSet(KEY_RUNNING_TUNNELS, null);
preferences.getStringSet(KEY_RUNNING_TUNNELS, Collections.emptySet()); if (previouslyRunning == null)
final CompletableFuture[] futures = StreamSupport.stream(tunnelsToEnable) return CompletableFuture.completedFuture(null);
.map(tunnels::get) return CompletableFuture.allOf(StreamSupport.stream(tunnels)
.map(tunnel -> tunnel.setState(State.UP)) .filter(tunnel -> previouslyRunning.contains(tunnel.getName()))
.toArray(CompletableFuture[]::new); .map(tunnel -> setTunnelState(tunnel, State.UP))
return CompletableFuture.allOf(futures); .toArray(CompletableFuture[]::new));
} }
public CompletionStage<Void> saveState() { public void saveState() {
final Set<String> runningTunnels = StreamSupport.stream(tunnels) final Set<String> runningTunnels = StreamSupport.stream(tunnels)
.filter(tunnel -> tunnel.getState() == State.UP) .filter(tunnel -> tunnel.getState() == State.UP)
.map(Tunnel::getName) .map(Tunnel::getName)
.collect(Collectors.toUnmodifiableSet()); .collect(Collectors.toUnmodifiableSet());
preferences.edit().putStringSet(KEY_RUNNING_TUNNELS, runningTunnels).apply(); preferences.edit().putStringSet(KEY_RUNNING_TUNNELS, runningTunnels).apply();
return CompletableFuture.completedFuture(null);
} }
private void setLastUsedTunnel(final Tunnel tunnel) { private void setLastUsedTunnel(final Tunnel tunnel) {