KernelModuleDisablerPreference: turn off tunnels before switching backend

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-03-09 16:18:55 -06:00
parent 492fcce053
commit d55fb25a40
2 changed files with 26 additions and 11 deletions

View File

@ -247,7 +247,7 @@ public final class TunnelManager extends BaseObservable {
}); });
} }
CompletionStage<State> setTunnelState(final ObservableTunnel tunnel, final State state) { public CompletionStage<State> setTunnelState(final ObservableTunnel tunnel, final State state) {
// Ensure the configuration is loaded before trying to use it. // Ensure the configuration is loaded before trying to use it.
return tunnel.getConfigAsync().thenCompose(config -> return tunnel.getConfigAsync().thenCompose(config ->
Application.getAsyncWorker().supplyAsync(() -> Application.getBackend().setState(tunnel, state, config)) Application.getAsyncWorker().supplyAsync(() -> Application.getBackend().setState(tunnel, state, config))

View File

@ -7,14 +7,22 @@ package com.wireguard.android.preference;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.SystemClock;
import android.util.AttributeSet; import android.util.AttributeSet;
import com.wireguard.android.Application; import com.wireguard.android.Application;
import com.wireguard.android.R; import com.wireguard.android.R;
import com.wireguard.android.backend.Tunnel;
import com.wireguard.android.backend.WgQuickBackend; import com.wireguard.android.backend.WgQuickBackend;
import com.wireguard.util.NonNullForAll; import com.wireguard.util.NonNullForAll;
import java.util.ArrayList;
import java.util.Collection;
import androidx.preference.Preference; import androidx.preference.Preference;
import java9.util.concurrent.CompletableFuture;
import java9.util.stream.Collectors;
import java9.util.stream.StreamSupport;
@NonNullForAll @NonNullForAll
public class KernelModuleDisablerPreference extends Preference { public class KernelModuleDisablerPreference extends Preference {
@ -44,9 +52,15 @@ public class KernelModuleDisablerPreference extends Preference {
setState(State.DISABLING); setState(State.DISABLING);
Application.getSharedPreferences().edit().putBoolean("disable_kernel_module", true).apply(); Application.getSharedPreferences().edit().putBoolean("disable_kernel_module", true).apply();
} }
Application.getAsyncWorker().runAsync(() -> { final long start = SystemClock.elapsedRealtime();
Thread.sleep(1000 * 5); Application.getAsyncWorker().runAsync(() -> Application.getTunnelManager().getTunnels().thenApply(observableTunnels -> {
Intent i = getContext().getPackageManager().getLaunchIntentForPackage(getContext().getPackageName()); final Collection<CompletableFuture<Tunnel.State>> c = StreamSupport.stream(observableTunnels.values()).map(t -> t.setState(Tunnel.State.DOWN).toCompletableFuture()).collect(Collectors.toCollection(ArrayList::new));
return CompletableFuture.allOf(c.toArray(new CompletableFuture[0])).thenRun(() -> {
try {
Thread.sleep(Math.max(0, 1000 * 5 - (SystemClock.elapsedRealtime() - start)));
} catch (final Exception ignored) {
}
final Intent i = getContext().getPackageManager().getLaunchIntentForPackage(getContext().getPackageName());
if (i == null) if (i == null)
return; return;
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
@ -54,6 +68,7 @@ public class KernelModuleDisablerPreference extends Preference {
Application.get().startActivity(i); Application.get().startActivity(i);
System.exit(0); System.exit(0);
}); });
}).join());
} }
private void setState(final State state) { private void setState(final State state) {