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.
return tunnel.getConfigAsync().thenCompose(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.Intent;
import android.os.SystemClock;
import android.util.AttributeSet;
import com.wireguard.android.Application;
import com.wireguard.android.R;
import com.wireguard.android.backend.Tunnel;
import com.wireguard.android.backend.WgQuickBackend;
import com.wireguard.util.NonNullForAll;
import java.util.ArrayList;
import java.util.Collection;
import androidx.preference.Preference;
import java9.util.concurrent.CompletableFuture;
import java9.util.stream.Collectors;
import java9.util.stream.StreamSupport;
@NonNullForAll
public class KernelModuleDisablerPreference extends Preference {
@ -44,16 +52,23 @@ public class KernelModuleDisablerPreference extends Preference {
setState(State.DISABLING);
Application.getSharedPreferences().edit().putBoolean("disable_kernel_module", true).apply();
}
Application.getAsyncWorker().runAsync(() -> {
Thread.sleep(1000 * 5);
Intent i = getContext().getPackageManager().getLaunchIntentForPackage(getContext().getPackageName());
if (i == null)
return;
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Application.get().startActivity(i);
System.exit(0);
});
final long start = SystemClock.elapsedRealtime();
Application.getAsyncWorker().runAsync(() -> Application.getTunnelManager().getTunnels().thenApply(observableTunnels -> {
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)
return;
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Application.get().startActivity(i);
System.exit(0);
});
}).join());
}
private void setState(final State state) {