summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/Application.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/com/wireguard/android/Application.java')
-rw-r--r--app/src/main/java/com/wireguard/android/Application.java153
1 files changed, 50 insertions, 103 deletions
diff --git a/app/src/main/java/com/wireguard/android/Application.java b/app/src/main/java/com/wireguard/android/Application.java
index eaf2d75c..d0dc22d9 100644
--- a/app/src/main/java/com/wireguard/android/Application.java
+++ b/app/src/main/java/com/wireguard/android/Application.java
@@ -1,12 +1,10 @@
/*
- * Copyright © 2018 Samuel Holland <samuel@sholland.org>
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
* SPDX-License-Identifier: GPL-2.0-or-later
*/
package com.wireguard.android;
-import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Handler;
@@ -25,123 +23,72 @@ import com.wireguard.android.util.RootShell;
import com.wireguard.android.util.ToolsInstaller;
import java.io.File;
+import java.lang.ref.WeakReference;
import java.util.concurrent.Executor;
-import javax.inject.Qualifier;
-import javax.inject.Scope;
-
-import dagger.Component;
-import dagger.Module;
-import dagger.Provides;
-
-/**
- * Base context for the WireGuard Android application. This class (instantiated once during the
- * application lifecycle) maintains and mediates access to the global state of the application.
- */
-
public class Application extends android.app.Application {
- private static ApplicationComponent component;
-
- public static ApplicationComponent getComponent() {
- if (component == null)
- throw new IllegalStateException("Application instance not yet created");
- return component;
+ private static WeakReference<Application> weakSelf;
+ private AsyncWorker asyncWorker;
+ private Backend backend;
+ private RootShell rootShell;
+ private SharedPreferences sharedPreferences;
+ private ToolsInstaller toolsInstaller;
+ private TunnelManager tunnelManager;
+ public Application() {
+ weakSelf = new WeakReference<>(this);
}
- @Override
- public void onCreate() {
- super.onCreate();
- component = DaggerApplication_ApplicationComponent.builder()
- .applicationModule(new ApplicationModule(this))
- .build();
- component.getTunnelManager().onCreate();
+ public static Application get() {
+ return weakSelf.get();
}
- @ApplicationScope
- @Component(modules = ApplicationModule.class)
- public interface ApplicationComponent {
- AsyncWorker getAsyncWorker();
-
- Class getBackendType();
-
- ToolsInstaller getToolsInstaller();
+ public static AsyncWorker getAsyncWorker() {
+ return get().asyncWorker;
+ }
- TunnelManager getTunnelManager();
+ public static Class getBackendType() {
+ return get().backend.getClass();
+ }
- RootShell getRootShell();
+ public static RootShell getRootShell() {
+ return get().rootShell;
}
- @Qualifier
- public @interface ApplicationContext {
+ public static SharedPreferences getSharedPreferences() {
+ return get().sharedPreferences;
}
- @Qualifier
- public @interface ApplicationHandler {
+ public static ToolsInstaller getToolsInstaller() {
+ return get().toolsInstaller;
}
- @Scope
- public @interface ApplicationScope {
+ public static TunnelManager getTunnelManager() {
+ return get().tunnelManager;
}
- @Module
- public static final class ApplicationModule {
- private final Context context;
-
- private ApplicationModule(final Application application) {
- context = application.getApplicationContext();
-
- AppCompatDelegate.setDefaultNightMode(
- getPreferences(context).getBoolean("dark_theme", false) ?
- AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
- }
-
- @ApplicationScope
- @Provides
- public static Backend getBackend(@ApplicationContext final Context context,
- final RootShell rootShell,
- final ToolsInstaller toolsInstaller) {
- if (new File("/sys/module/wireguard").exists())
- return new WgQuickBackend(context, rootShell, toolsInstaller);
- else
- return new GoBackend(context);
- }
-
- @ApplicationScope
- @Provides
- public static Class getBackendType(final Backend backend) {
- return backend.getClass();
- }
-
- @ApplicationScope
- @Provides
- public static ConfigStore getConfigStore(@ApplicationContext final Context context) {
- return new FileConfigStore(context);
- }
-
- @ApplicationScope
- @Provides
- public static Executor getExecutor() {
- return AsyncTask.SERIAL_EXECUTOR;
- }
-
- @ApplicationHandler
- @ApplicationScope
- @Provides
- public static Handler getHandler() {
- return new Handler(Looper.getMainLooper());
- }
-
- @ApplicationScope
- @Provides
- public static SharedPreferences getPreferences(@ApplicationContext final Context context) {
- return PreferenceManager.getDefaultSharedPreferences(context);
- }
-
- @ApplicationContext
- @ApplicationScope
- @Provides
- public Context getContext() {
- return context;
- }
+ @Override
+ public void onCreate() {
+ super.onCreate();
+
+ final Executor executor = AsyncTask.SERIAL_EXECUTOR;
+ final Handler handler = new Handler(Looper.getMainLooper());
+ final ConfigStore configStore = new FileConfigStore(getApplicationContext());
+
+ asyncWorker = new AsyncWorker(executor, handler);
+ rootShell = new RootShell(getApplicationContext());
+ toolsInstaller = new ToolsInstaller(getApplicationContext());
+
+ sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+ AppCompatDelegate.setDefaultNightMode(
+ sharedPreferences.getBoolean("dark_theme", false) ?
+ AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
+
+ if (new File("/sys/module/wireguard").exists())
+ backend = new WgQuickBackend(getApplicationContext());
+ else
+ backend = new GoBackend(getApplicationContext());
+
+ tunnelManager = new TunnelManager(backend, configStore);
+ tunnelManager.onCreate();
}
}