2018-05-02 17:29:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Samuel Holland <samuel@sholland.org>
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
package com.wireguard.android;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
|
|
|
|
import com.wireguard.android.backend.Backend;
|
2018-02-07 19:19:20 +01:00
|
|
|
import com.wireguard.android.backend.GoBackend;
|
2018-01-01 09:06:37 +01:00
|
|
|
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;
|
|
|
|
import com.wireguard.android.util.RootShell;
|
2018-01-08 07:34:55 +01:00
|
|
|
import com.wireguard.android.util.ToolsInstaller;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
2018-02-07 19:19:20 +01:00
|
|
|
import java.io.File;
|
2018-01-01 09:06:37 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
component = DaggerApplication_ApplicationComponent.builder()
|
|
|
|
.applicationModule(new ApplicationModule(this))
|
|
|
|
.build();
|
|
|
|
component.getTunnelManager().onCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApplicationScope
|
|
|
|
@Component(modules = ApplicationModule.class)
|
|
|
|
public interface ApplicationComponent {
|
|
|
|
AsyncWorker getAsyncWorker();
|
|
|
|
|
2018-04-30 18:39:12 +02:00
|
|
|
Class getBackendType();
|
|
|
|
|
2018-01-08 07:34:55 +01:00
|
|
|
ToolsInstaller getToolsInstaller();
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
TunnelManager getTunnelManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Qualifier
|
|
|
|
public @interface ApplicationContext {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Qualifier
|
|
|
|
public @interface ApplicationHandler {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Scope
|
|
|
|
public @interface ApplicationScope {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Module
|
|
|
|
public static final class ApplicationModule {
|
|
|
|
private final Context context;
|
|
|
|
|
|
|
|
private ApplicationModule(final Application application) {
|
|
|
|
context = application.getApplicationContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApplicationScope
|
|
|
|
@Provides
|
2018-01-07 07:24:56 +01:00
|
|
|
public static Backend getBackend(@ApplicationContext final Context context,
|
2018-01-09 06:26:39 +01:00
|
|
|
final RootShell rootShell,
|
|
|
|
final ToolsInstaller toolsInstaller) {
|
2018-02-07 19:19:20 +01:00
|
|
|
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();
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@ApplicationScope
|
|
|
|
@Provides
|
2018-01-07 07:24:56 +01:00
|
|
|
public static ConfigStore getConfigStore(@ApplicationContext final Context context) {
|
|
|
|
return new FileConfigStore(context);
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|