summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/backend
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2020-03-08 20:20:29 -0600
committerJason A. Donenfeld <Jason@zx2c4.com>2020-03-08 20:23:25 -0600
commit7a4af834c213672c65c89b14c75229bf5f2ea4ab (patch)
tree64fad45abe8f1586449b2f1090547d6d57f5b934 /app/src/main/java/com/wireguard/android/backend
parent314a0d124dabd11353a120e66bc872f2843bcdb1 (diff)
Backend: do not use singletons
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/backend')
-rw-r--r--app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java22
1 files changed, 13 insertions, 9 deletions
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 5769da66..75032f23 100644
--- a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java
+++ b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java
@@ -10,9 +10,10 @@ import androidx.annotation.Nullable;
import android.content.Context;
import android.util.Log;
-import com.wireguard.android.Application;
import com.wireguard.android.backend.BackendException.Reason;
import com.wireguard.android.backend.Tunnel.State;
+import com.wireguard.android.util.RootShell;
+import com.wireguard.android.util.ToolsInstaller;
import com.wireguard.config.Config;
import com.wireguard.crypto.Key;
@@ -40,12 +41,16 @@ import java9.util.stream.Stream;
public final class WgQuickBackend implements Backend {
private static final String TAG = "WireGuard/" + WgQuickBackend.class.getSimpleName();
+ private final RootShell rootShell;
+ 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) {
+ public WgQuickBackend(final Context context, final RootShell rootShell, final ToolsInstaller toolsInstaller) {
localTemporaryDir = new File(context.getCacheDir(), "tmp");
+ this.rootShell = rootShell;
+ this.toolsInstaller = toolsInstaller;
}
@Override
@@ -53,8 +58,8 @@ public final class WgQuickBackend implements Backend {
final List<String> output = new ArrayList<>();
// Don't throw an exception here or nothing will show up in the UI.
try {
- Application.getToolsInstaller().ensureToolsAvailable();
- if (Application.getRootShell().run(output, "wg show interfaces") != 0 || output.isEmpty())
+ toolsInstaller.ensureToolsAvailable();
+ if (rootShell.run(output, "wg show interfaces") != 0 || output.isEmpty())
return Collections.emptySet();
} catch (final Exception e) {
Log.w(TAG, "Unable to enumerate running tunnels", e);
@@ -74,7 +79,7 @@ public final class WgQuickBackend implements Backend {
final Statistics stats = new Statistics();
final Collection<String> output = new ArrayList<>();
try {
- if (Application.getRootShell().run(output, String.format("wg show '%s' transfer", tunnel.getName())) != 0)
+ if (rootShell.run(output, String.format("wg show '%s' transfer", tunnel.getName())) != 0)
return stats;
} catch (final Exception ignored) {
return stats;
@@ -94,8 +99,7 @@ public final class WgQuickBackend implements Backend {
@Override
public String getVersion() throws Exception {
final List<String> output = new ArrayList<>();
- if (Application.getRootShell()
- .run(output, "cat /sys/module/wireguard/version") != 0 || output.isEmpty())
+ if (rootShell.run(output, "cat /sys/module/wireguard/version") != 0 || output.isEmpty())
throw new BackendException(Reason.UNKNOWN_KERNEL_MODULE_NAME);
return output.get(0);
}
@@ -111,7 +115,7 @@ public final class WgQuickBackend implements Backend {
(state == State.DOWN && originalState == State.DOWN))
return originalState;
if (state == State.UP) {
- Application.getToolsInstaller().ensureToolsAvailable();
+ toolsInstaller.ensureToolsAvailable();
if (originalState == State.UP)
setStateInternal(tunnel, originalConfig == null ? config : originalConfig, State.DOWN);
try {
@@ -140,7 +144,7 @@ public final class WgQuickBackend implements Backend {
state.toString().toLowerCase(Locale.ENGLISH), tempFile.getAbsolutePath());
if (state == State.UP)
command = "cat /sys/module/wireguard/version && " + command;
- final int result = Application.getRootShell().run(null, command);
+ final int result = rootShell.run(null, command);
// noinspection ResultOfMethodCallIgnored
tempFile.delete();
if (result != 0)