2018-05-02 17:29:58 +02:00
|
|
|
/*
|
2018-05-02 21:40:01 +02:00
|
|
|
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2018-05-02 17:29:58 +02:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
package com.wireguard.android;
|
|
|
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.preference.PreferenceManager;
|
2018-06-01 07:34:00 +02:00
|
|
|
import android.support.v7.app.AppCompatDelegate;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
|
|
|
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-06-07 04:12:42 +02:00
|
|
|
import java.lang.ref.WeakReference;
|
2018-01-01 09:06:37 +01:00
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
|
|
|
|
public class Application extends android.app.Application {
|
2018-06-07 04:12:42 +02:00
|
|
|
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);
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static Application get() {
|
|
|
|
return weakSelf.get();
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static AsyncWorker getAsyncWorker() {
|
|
|
|
return get().asyncWorker;
|
|
|
|
}
|
2018-01-08 07:34:55 +01:00
|
|
|
|
2018-06-07 04:35:07 +02:00
|
|
|
public static Backend getBackend() {
|
|
|
|
return get().backend;
|
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static Class getBackendType() {
|
|
|
|
return get().backend.getClass();
|
|
|
|
}
|
2018-06-07 03:27:06 +02:00
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static RootShell getRootShell() {
|
|
|
|
return get().rootShell;
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static SharedPreferences getSharedPreferences() {
|
|
|
|
return get().sharedPreferences;
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static ToolsInstaller getToolsInstaller() {
|
|
|
|
return get().toolsInstaller;
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static TunnelManager getTunnelManager() {
|
|
|
|
return get().tunnelManager;
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
@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();
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
}
|