summaryrefslogtreecommitdiffhomepage
path: root/app
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2020-03-09 04:32:51 -0600
committerJason A. Donenfeld <Jason@zx2c4.com>2020-03-09 04:46:17 -0600
commitd9e9dd04af9267e2e45c301020dc057ac3d9d711 (patch)
tree9021070fe941a7f152b56b837cc2184cfc329193 /app
parent3c2fa15dc207fc6f5fcbc78c3077457424fe6322 (diff)
Tunnel: move state change into interface
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app')
-rw-r--r--app/src/main/java/com/wireguard/android/backend/Backend.java18
-rw-r--r--app/src/main/java/com/wireguard/android/backend/GoBackend.java20
-rw-r--r--app/src/main/java/com/wireguard/android/backend/Tunnel.java13
-rw-r--r--app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java15
-rw-r--r--app/src/main/java/com/wireguard/android/model/ObservableTunnel.java7
-rw-r--r--app/src/main/java/com/wireguard/android/model/TunnelManager.java15
6 files changed, 23 insertions, 65 deletions
diff --git a/app/src/main/java/com/wireguard/android/backend/Backend.java b/app/src/main/java/com/wireguard/android/backend/Backend.java
index 84e32eb2..ed3a5ebd 100644
--- a/app/src/main/java/com/wireguard/android/backend/Backend.java
+++ b/app/src/main/java/com/wireguard/android/backend/Backend.java
@@ -60,22 +60,4 @@ public interface Backend {
* @return The updated state of the tunnel.
*/
Tunnel.State setState(Tunnel tunnel, Tunnel.State state, @Nullable Config config) throws Exception;
-
- interface TunnelStateChangeNotificationReceiver {
- void tunnelStateChange(Tunnel tunnel, Tunnel.State state);
- }
-
- /**
- * Register a state change notification callback.
- *
- * @param receiver The receiver object to receive the notification.
- */
- void registerStateChangeNotification(TunnelStateChangeNotificationReceiver receiver);
-
- /**
- * Unregister a state change notification callback.
- *
- * @param receiver The receiver object to no longer receive the notification.
- */
- void unregisterStateChangeNotification(TunnelStateChangeNotificationReceiver receiver);
}
diff --git a/app/src/main/java/com/wireguard/android/backend/GoBackend.java b/app/src/main/java/com/wireguard/android/backend/GoBackend.java
index 77e40e53..6ad5afa4 100644
--- a/app/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/app/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -5,7 +5,6 @@
package com.wireguard.android.backend;
-import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
@@ -25,7 +24,6 @@ import com.wireguard.crypto.KeyFormatException;
import java.net.InetAddress;
import java.util.Collections;
-import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -48,8 +46,6 @@ public final class GoBackend implements Backend {
@Nullable private Config currentConfig;
private int currentTunnelHandle = -1;
- private final Set<TunnelStateChangeNotificationReceiver> notifiers = new HashSet<>();
-
public GoBackend(final Context context) {
SharedLibraryLoader.loadSharedLibrary(context, "wg-go");
this.context = context;
@@ -240,8 +236,7 @@ public final class GoBackend implements Backend {
currentConfig = null;
}
- for (final TunnelStateChangeNotificationReceiver notifier : notifiers)
- notifier.tunnelStateChange(tunnel, state);
+ tunnel.onStateChange(state);
}
private void startVpnService() {
@@ -249,16 +244,6 @@ public final class GoBackend implements Backend {
context.startService(new Intent(context, VpnService.class));
}
- @Override
- public void registerStateChangeNotification(final TunnelStateChangeNotificationReceiver receiver) {
- notifiers.add(receiver);
- }
-
- @Override
- public void unregisterStateChangeNotification(final TunnelStateChangeNotificationReceiver receiver) {
- notifiers.remove(receiver);
- }
-
public static class VpnService extends android.net.VpnService {
@Nullable private GoBackend owner;
@@ -286,8 +271,7 @@ public final class GoBackend implements Backend {
owner.currentTunnel = null;
owner.currentTunnelHandle = -1;
owner.currentConfig = null;
- for (final TunnelStateChangeNotificationReceiver notifier : owner.notifiers)
- notifier.tunnelStateChange(tunnel, State.DOWN);
+ tunnel.onStateChange(State.DOWN);
}
}
vpnService = vpnService.newIncompleteFuture();
diff --git a/app/src/main/java/com/wireguard/android/backend/Tunnel.java b/app/src/main/java/com/wireguard/android/backend/Tunnel.java
index 86661355..af2f59f7 100644
--- a/app/src/main/java/com/wireguard/android/backend/Tunnel.java
+++ b/app/src/main/java/com/wireguard/android/backend/Tunnel.java
@@ -29,5 +29,18 @@ public interface Tunnel {
return !NAME_PATTERN.matcher(name).matches();
}
+ /**
+ * Get the name of the tunnel, which should always pass the !isNameInvalid test.
+ *
+ * @return The name of the tunnel.
+ */
String getName();
+
+ /**
+ * React to a change in state of the tunnel. Should only be directly called by Backend.
+ *
+ * @param newState The new state of the tunnel.
+ * @return The new state of the tunnel.
+ */
+ void onStateChange(State newState);
}
diff --git a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java
index 75032f23..9695aab7 100644
--- a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java
+++ b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java
@@ -23,7 +23,6 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
-import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -45,7 +44,6 @@ public final class WgQuickBackend implements Backend {
private final ToolsInstaller toolsInstaller;
private final File localTemporaryDir;
private final Map<Tunnel, Config> runningConfigs = new HashMap<>();
- private final Set<TunnelStateChangeNotificationReceiver> notifiers = new HashSet<>();
public WgQuickBackend(final Context context, final RootShell rootShell, final ToolsInstaller toolsInstaller) {
localTemporaryDir = new File(context.getCacheDir(), "tmp");
@@ -155,17 +153,6 @@ public final class WgQuickBackend implements Backend {
else
runningConfigs.remove(tunnel);
- for (final TunnelStateChangeNotificationReceiver notifier : notifiers)
- notifier.tunnelStateChange(tunnel, state);
- }
-
- @Override
- public void registerStateChangeNotification(final TunnelStateChangeNotificationReceiver receiver) {
- notifiers.add(receiver);
- }
-
- @Override
- public void unregisterStateChangeNotification(final TunnelStateChangeNotificationReceiver receiver) {
- notifiers.remove(receiver);
+ tunnel.onStateChange(state);
}
}
diff --git a/app/src/main/java/com/wireguard/android/model/ObservableTunnel.java b/app/src/main/java/com/wireguard/android/model/ObservableTunnel.java
index 826a6a2a..ce3197f2 100644
--- a/app/src/main/java/com/wireguard/android/model/ObservableTunnel.java
+++ b/app/src/main/java/com/wireguard/android/model/ObservableTunnel.java
@@ -96,7 +96,7 @@ public class ObservableTunnel extends BaseObservable implements Keyed<String>, T
return config;
}
- public String onNameChanged(final String name) {
+ String onNameChanged(final String name) {
this.name = name;
notifyPropertyChanged(BR.name);
return name;
@@ -110,6 +110,11 @@ public class ObservableTunnel extends BaseObservable implements Keyed<String>, T
return state;
}
+ @Override
+ public void onStateChange(final State newState) {
+ onStateChanged(state);
+ }
+
@Nullable
Statistics onStatisticsChanged(@Nullable final Statistics statistics) {
this.statistics = statistics;
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 e7bb9cd5..35d56c81 100644
--- a/app/src/main/java/com/wireguard/android/model/TunnelManager.java
+++ b/app/src/main/java/com/wireguard/android/model/TunnelManager.java
@@ -15,7 +15,6 @@ import androidx.annotation.Nullable;
import com.wireguard.android.Application;
import com.wireguard.android.BR;
import com.wireguard.android.R;
-import com.wireguard.android.backend.Backend.TunnelStateChangeNotificationReceiver;
import com.wireguard.android.configStore.ConfigStore;
import com.wireguard.android.backend.Tunnel;
import com.wireguard.android.backend.Tunnel.State;
@@ -40,7 +39,7 @@ import java9.util.stream.StreamSupport;
* Maintains and mediates changes to the set of available WireGuard tunnels,
*/
-public final class TunnelManager extends BaseObservable implements TunnelStateChangeNotificationReceiver {
+public final class TunnelManager extends BaseObservable {
private static final Comparator<String> COMPARATOR = Comparators.<String>thenComparing(
String.CASE_INSENSITIVE_ORDER, Comparators.naturalOrder());
private static final String KEY_LAST_USED_TUNNEL = "last_used_tunnel";
@@ -57,13 +56,6 @@ public final class TunnelManager extends BaseObservable implements TunnelStateCh
public TunnelManager(final ConfigStore configStore) {
this.configStore = configStore;
- Application.getBackendAsync().thenAccept(backend -> backend.registerStateChangeNotification(this));
- }
-
- @Override
- protected void finalize() throws Throwable {
- Application.getBackendAsync().thenAccept(backend -> backend.unregisterStateChangeNotification(this));
- super.finalize();
}
static CompletionStage<State> getTunnelState(final ObservableTunnel tunnel) {
@@ -266,11 +258,6 @@ public final class TunnelManager extends BaseObservable implements TunnelStateCh
});
}
- @Override
- public void tunnelStateChange(final Tunnel tunnel, final State state) {
- ((ObservableTunnel)tunnel).onStateChanged(state);
- }
-
public static final class IntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, @Nullable final Intent intent) {