summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/Application.java
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-07-26 03:17:22 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-07-26 04:54:14 +0200
commit62d8beff9693080225fe550d82f105ba3cde5df2 (patch)
tree3c2200d148fa0bcd00071202e3feace131390a0a /app/src/main/java/com/wireguard/android/Application.java
parent7d438e9dbc5b5fbaeb57e6ae4ba99388cebdf0f2 (diff)
Application: use proper completablefuture for backend
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/Application.java')
-rw-r--r--app/src/main/java/com/wireguard/android/Application.java68
1 files changed, 20 insertions, 48 deletions
diff --git a/app/src/main/java/com/wireguard/android/Application.java b/app/src/main/java/com/wireguard/android/Application.java
index ffecd4df..8dff8a54 100644
--- a/app/src/main/java/com/wireguard/android/Application.java
+++ b/app/src/main/java/com/wireguard/android/Application.java
@@ -19,7 +19,6 @@ import android.support.v7.app.AppCompatDelegate;
import com.wireguard.android.backend.Backend;
import com.wireguard.android.backend.GoBackend;
import com.wireguard.android.backend.WgQuickBackend;
-import com.wireguard.android.configStore.ConfigStore;
import com.wireguard.android.configStore.FileConfigStore;
import com.wireguard.android.model.TunnelManager;
import com.wireguard.android.util.AsyncWorker;
@@ -37,16 +36,14 @@ import java.io.File;
import java.lang.ref.WeakReference;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Objects;
-import java.util.concurrent.Executor;
+
+import java9.util.concurrent.CompletableFuture;
@AcraCore(reportFormat = StringFormat.JSON,
buildConfigClass = BuildConfig.class,
- logcatArguments = { "-b", "all", "-d", "-v", "threadtime", "*:V" },
- excludeMatchingSharedPreferencesKeys={"last_used_tunnel", "enabled_configs"})
+ logcatArguments = {"-b", "all", "-d", "-v", "threadtime", "*:V"},
+ excludeMatchingSharedPreferencesKeys = {"last_used_tunnel", "enabled_configs"})
@AcraHttpSender(uri = "https://crashreport.zx2c4.com/android/report",
basicAuthLogin = "6RCovLxEVCTXGiW5",
basicAuthPassword = "O7I3sVa5ULVdiC51",
@@ -59,10 +56,8 @@ public class Application extends android.app.Application {
@SuppressWarnings("NullableProblems") private SharedPreferences sharedPreferences;
@SuppressWarnings("NullableProblems") private ToolsInstaller toolsInstaller;
@SuppressWarnings("NullableProblems") private TunnelManager tunnelManager;
- @SuppressWarnings("NullableProblems") private Handler handler;
@Nullable private Backend backend;
- @Nullable private Collection<BackendCallback> haveBackendCallbacks = new ArrayList<>();
- private final Object haveBackendCallbacksLock = new Object();
+ private final CompletableFuture<Backend> futureBackend = new CompletableFuture<>();
public Application() {
weakSelf = new WeakReference<>(this);
@@ -114,43 +109,26 @@ public class Application extends android.app.Application {
public static Backend getBackend() {
final Application app = get();
- synchronized (app) {
+ synchronized (app.futureBackend) {
if (app.backend == null) {
+ Backend backend = null;
if (new File("/sys/module/wireguard").exists()) {
try {
app.rootShell.start();
- app.backend = new WgQuickBackend(app.getApplicationContext());
- } catch (final Exception ignored) { }
- }
- if (app.backend == null)
- app.backend = new GoBackend(app.getApplicationContext());
- synchronized (app.haveBackendCallbacksLock) {
- if (app.haveBackendCallbacks != null) {
- for (final BackendCallback callback : app.haveBackendCallbacks)
- app.handler.post(() -> callback.callback(app.backend));
- app.haveBackendCallbacks = null;
+ backend = new WgQuickBackend(app.getApplicationContext());
+ } catch (final Exception ignored) {
}
}
+ if (backend == null)
+ backend = new GoBackend(app.getApplicationContext());
+ app.backend = backend;
}
return app.backend;
}
}
- @FunctionalInterface
- public interface BackendCallback {
- void callback(final Backend backend);
- }
-
- public static void onHaveBackend(final BackendCallback callback) {
- final Application app = get();
- synchronized (app.haveBackendCallbacksLock) {
- if (app.haveBackendCallbacks == null) {
- Objects.requireNonNull(app.backend, "Backend still null in onHaveBackend call");
- callback.callback(app.backend);
- } else {
- app.haveBackendCallbacks.add(callback);
- }
- }
+ public static CompletableFuture<Backend> getBackendAsync() {
+ return get().futureBackend;
}
public static RootShell getRootShell() {
@@ -173,11 +151,7 @@ public class Application extends android.app.Application {
public void onCreate() {
super.onCreate();
- handler = new Handler(Looper.getMainLooper());
- final Executor executor = AsyncTask.SERIAL_EXECUTOR;
- final ConfigStore configStore = new FileConfigStore(getApplicationContext());
-
- asyncWorker = new AsyncWorker(executor, handler);
+ asyncWorker = new AsyncWorker(AsyncTask.SERIAL_EXECUTOR, new Handler(Looper.getMainLooper()));
rootShell = new RootShell(getApplicationContext());
toolsInstaller = new ToolsInstaller(getApplicationContext());
@@ -186,16 +160,14 @@ public class Application extends android.app.Application {
sharedPreferences.getBoolean("dark_theme", false) ?
AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
- tunnelManager = new TunnelManager(configStore);
- asyncWorker.runAsync(Application::getBackend);
+ tunnelManager = new TunnelManager(new FileConfigStore(getApplicationContext()));
tunnelManager.onCreate();
- onHaveBackend(backend -> {
+ asyncWorker.supplyAsync(Application::getBackend).thenAccept(backend -> {
+ futureBackend.complete(backend);
ACRA.getErrorReporter().putCustomData("backend", backend.getClass().getSimpleName());
- getAsyncWorker().supplyAsync(backend::getVersion).whenComplete((version, exception) -> {
- if (exception == null)
- ACRA.getErrorReporter().putCustomData("backendVersion", version);
- });
+ asyncWorker.supplyAsync(backend::getVersion).thenAccept(version ->
+ ACRA.getErrorReporter().putCustomData("backendVersion", version));
});
}
}