Give Samuel heart attack by removing Dagger
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
7b59353910
commit
24605c9c01
@ -52,19 +52,16 @@ android {
|
|||||||
ext {
|
ext {
|
||||||
databindingVersion = '3.1.2'
|
databindingVersion = '3.1.2'
|
||||||
supportLibsVersion = '27.1.1'
|
supportLibsVersion = '27.1.1'
|
||||||
daggerVersion = '2.14.1'
|
|
||||||
streamsupportVersion = '1.6.0'
|
streamsupportVersion = '1.6.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
|
|
||||||
implementation "com.android.databinding:library:$databindingVersion"
|
implementation "com.android.databinding:library:$databindingVersion"
|
||||||
implementation "com.android.support:appcompat-v7:$supportLibsVersion"
|
implementation "com.android.support:appcompat-v7:$supportLibsVersion"
|
||||||
implementation "com.android.support:cardview-v7:$supportLibsVersion"
|
implementation "com.android.support:cardview-v7:$supportLibsVersion"
|
||||||
implementation "com.android.support:design:$supportLibsVersion"
|
implementation "com.android.support:design:$supportLibsVersion"
|
||||||
implementation "com.android.support:preference-v14:$supportLibsVersion"
|
implementation "com.android.support:preference-v14:$supportLibsVersion"
|
||||||
implementation "com.android.support:support-annotations:$supportLibsVersion"
|
implementation "com.android.support:support-annotations:$supportLibsVersion"
|
||||||
implementation "com.google.dagger:dagger:$daggerVersion"
|
|
||||||
implementation "net.sourceforge.streamsupport:android-retrofuture:$streamsupportVersion"
|
implementation "net.sourceforge.streamsupport:android-retrofuture:$streamsupportVersion"
|
||||||
implementation "net.sourceforge.streamsupport:android-retrostreams:$streamsupportVersion"
|
implementation "net.sourceforge.streamsupport:android-retrostreams:$streamsupportVersion"
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2018 Samuel Holland <samuel@sholland.org>
|
|
||||||
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
||||||
* SPDX-License-Identifier: GPL-2.0-or-later
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.wireguard.android;
|
package com.wireguard.android;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
@ -25,123 +23,72 @@ import com.wireguard.android.util.RootShell;
|
|||||||
import com.wireguard.android.util.ToolsInstaller;
|
import com.wireguard.android.util.ToolsInstaller;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.concurrent.Executor;
|
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 {
|
public class Application extends android.app.Application {
|
||||||
private static ApplicationComponent 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);
|
||||||
|
}
|
||||||
|
|
||||||
public static ApplicationComponent getComponent() {
|
public static Application get() {
|
||||||
if (component == null)
|
return weakSelf.get();
|
||||||
throw new IllegalStateException("Application instance not yet created");
|
}
|
||||||
return component;
|
|
||||||
|
public static AsyncWorker getAsyncWorker() {
|
||||||
|
return get().asyncWorker;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Class getBackendType() {
|
||||||
|
return get().backend.getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RootShell getRootShell() {
|
||||||
|
return get().rootShell;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SharedPreferences getSharedPreferences() {
|
||||||
|
return get().sharedPreferences;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ToolsInstaller getToolsInstaller() {
|
||||||
|
return get().toolsInstaller;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TunnelManager getTunnelManager() {
|
||||||
|
return get().tunnelManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
component = DaggerApplication_ApplicationComponent.builder()
|
|
||||||
.applicationModule(new ApplicationModule(this))
|
|
||||||
.build();
|
|
||||||
component.getTunnelManager().onCreate();
|
|
||||||
}
|
|
||||||
|
|
||||||
@ApplicationScope
|
final Executor executor = AsyncTask.SERIAL_EXECUTOR;
|
||||||
@Component(modules = ApplicationModule.class)
|
final Handler handler = new Handler(Looper.getMainLooper());
|
||||||
public interface ApplicationComponent {
|
final ConfigStore configStore = new FileConfigStore(getApplicationContext());
|
||||||
AsyncWorker getAsyncWorker();
|
|
||||||
|
|
||||||
Class getBackendType();
|
asyncWorker = new AsyncWorker(executor, handler);
|
||||||
|
rootShell = new RootShell(getApplicationContext());
|
||||||
ToolsInstaller getToolsInstaller();
|
toolsInstaller = new ToolsInstaller(getApplicationContext());
|
||||||
|
|
||||||
TunnelManager getTunnelManager();
|
|
||||||
|
|
||||||
RootShell getRootShell();
|
|
||||||
}
|
|
||||||
|
|
||||||
@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();
|
|
||||||
|
|
||||||
|
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
||||||
AppCompatDelegate.setDefaultNightMode(
|
AppCompatDelegate.setDefaultNightMode(
|
||||||
getPreferences(context).getBoolean("dark_theme", false) ?
|
sharedPreferences.getBoolean("dark_theme", false) ?
|
||||||
AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
|
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())
|
if (new File("/sys/module/wireguard").exists())
|
||||||
return new WgQuickBackend(context, rootShell, toolsInstaller);
|
backend = new WgQuickBackend(getApplicationContext());
|
||||||
else
|
else
|
||||||
return new GoBackend(context);
|
backend = new GoBackend(getApplicationContext());
|
||||||
}
|
|
||||||
|
|
||||||
@ApplicationScope
|
tunnelManager = new TunnelManager(backend, configStore);
|
||||||
@Provides
|
tunnelManager.onCreate();
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,13 +20,13 @@ public class BootShutdownReceiver extends BroadcastReceiver {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(final Context context, final Intent intent) {
|
public void onReceive(final Context context, final Intent intent) {
|
||||||
if (Application.getComponent().getBackendType() != WgQuickBackend.class) {
|
if (Application.getBackendType() != WgQuickBackend.class) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final String action = intent.getAction();
|
final String action = intent.getAction();
|
||||||
if (action == null)
|
if (action == null)
|
||||||
return;
|
return;
|
||||||
final TunnelManager tunnelManager = Application.getComponent().getTunnelManager();
|
final TunnelManager tunnelManager = Application.getTunnelManager();
|
||||||
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
|
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
|
||||||
Log.i(TAG, "Broadcast receiver restoring state (boot)");
|
Log.i(TAG, "Broadcast receiver restoring state (boot)");
|
||||||
tunnelManager.restoreState(false).whenComplete(ExceptionLoggers.D);
|
tunnelManager.restoreState(false).whenComplete(ExceptionLoggers.D);
|
||||||
|
@ -38,7 +38,6 @@ public class QuickTileService extends TileService {
|
|||||||
private final OnStateChangedCallback onStateChangedCallback = new OnStateChangedCallback();
|
private final OnStateChangedCallback onStateChangedCallback = new OnStateChangedCallback();
|
||||||
private final OnTunnelChangedCallback onTunnelChangedCallback = new OnTunnelChangedCallback();
|
private final OnTunnelChangedCallback onTunnelChangedCallback = new OnTunnelChangedCallback();
|
||||||
private Tunnel tunnel;
|
private Tunnel tunnel;
|
||||||
private TunnelManager tunnelManager;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick() {
|
public void onClick() {
|
||||||
@ -54,12 +53,11 @@ public class QuickTileService extends TileService {
|
|||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
tunnelManager = Application.getComponent().getTunnelManager();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStartListening() {
|
public void onStartListening() {
|
||||||
tunnelManager.addOnPropertyChangedCallback(onTunnelChangedCallback);
|
Application.getTunnelManager().addOnPropertyChangedCallback(onTunnelChangedCallback);
|
||||||
if (tunnel != null)
|
if (tunnel != null)
|
||||||
tunnel.addOnPropertyChangedCallback(onStateChangedCallback);
|
tunnel.addOnPropertyChangedCallback(onStateChangedCallback);
|
||||||
updateTile();
|
updateTile();
|
||||||
@ -69,7 +67,7 @@ public class QuickTileService extends TileService {
|
|||||||
public void onStopListening() {
|
public void onStopListening() {
|
||||||
if (tunnel != null)
|
if (tunnel != null)
|
||||||
tunnel.removeOnPropertyChangedCallback(onStateChangedCallback);
|
tunnel.removeOnPropertyChangedCallback(onStateChangedCallback);
|
||||||
tunnelManager.removeOnPropertyChangedCallback(onTunnelChangedCallback);
|
Application.getTunnelManager().removeOnPropertyChangedCallback(onTunnelChangedCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onToggleFinished(@SuppressWarnings("unused") final State state,
|
private void onToggleFinished(@SuppressWarnings("unused") final State state,
|
||||||
@ -84,7 +82,7 @@ public class QuickTileService extends TileService {
|
|||||||
|
|
||||||
private void updateTile() {
|
private void updateTile() {
|
||||||
// Update the tunnel.
|
// Update the tunnel.
|
||||||
final Tunnel newTunnel = tunnelManager.getLastUsedTunnel();
|
final Tunnel newTunnel = Application.getTunnelManager().getLastUsedTunnel();
|
||||||
if (newTunnel != tunnel) {
|
if (newTunnel != tunnel) {
|
||||||
if (tunnel != null)
|
if (tunnel != null)
|
||||||
tunnel.removeOnPropertyChangedCallback(onStateChangedCallback);
|
tunnel.removeOnPropertyChangedCallback(onStateChangedCallback);
|
||||||
|
@ -47,14 +47,14 @@ public abstract class BaseActivity extends ThemeChangeAwareActivity {
|
|||||||
else if (getIntent() != null)
|
else if (getIntent() != null)
|
||||||
savedTunnelName = getIntent().getStringExtra(KEY_SELECTED_TUNNEL);
|
savedTunnelName = getIntent().getStringExtra(KEY_SELECTED_TUNNEL);
|
||||||
if (savedTunnelName != null) {
|
if (savedTunnelName != null) {
|
||||||
final TunnelManager tunnelManager = Application.getComponent().getTunnelManager();
|
final TunnelManager tunnelManager = Application.getTunnelManager();
|
||||||
selectedTunnel = tunnelManager.getTunnels().get(savedTunnelName);
|
selectedTunnel = tunnelManager.getTunnels().get(savedTunnelName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// The selected tunnel must be set before the superclass method recreates fragments.
|
// The selected tunnel must be set before the superclass method recreates fragments.
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
if (Application.getComponent().getBackendType() == GoBackend.class) {
|
if (Application.getBackendType() == GoBackend.class) {
|
||||||
final Intent intent = GoBackend.VpnService.prepare(this);
|
final Intent intent = GoBackend.VpnService.prepare(this);
|
||||||
if (intent != null) {
|
if (intent != null) {
|
||||||
startActivityForResult(intent, 0);
|
startActivityForResult(intent, 0);
|
||||||
|
@ -95,7 +95,7 @@ public class SettingsActivity extends ThemeChangeAwareActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onCreatePreferences(final Bundle savedInstanceState, final String key) {
|
public void onCreatePreferences(final Bundle savedInstanceState, final String key) {
|
||||||
addPreferencesFromResource(R.xml.preferences);
|
addPreferencesFromResource(R.xml.preferences);
|
||||||
if (Application.getComponent().getBackendType() != WgQuickBackend.class) {
|
if (Application.getBackendType() != WgQuickBackend.class) {
|
||||||
Preference pref = getPreferenceManager().findPreference("tools_installer");
|
Preference pref = getPreferenceManager().findPreference("tools_installer");
|
||||||
getPreferenceScreen().removePreference(pref);
|
getPreferenceScreen().removePreference(pref);
|
||||||
pref = getPreferenceManager().findPreference("restore_on_boot");
|
pref = getPreferenceManager().findPreference("restore_on_boot");
|
||||||
|
@ -8,11 +8,12 @@ package com.wireguard.android.activity;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.PreferenceManager;
|
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.support.v7.app.AppCompatDelegate;
|
import android.support.v7.app.AppCompatDelegate;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.wireguard.android.Application;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
public abstract class ThemeChangeAwareActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
public abstract class ThemeChangeAwareActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
@ -52,12 +53,12 @@ public abstract class ThemeChangeAwareActivity extends AppCompatActivity impleme
|
|||||||
@Override
|
@Override
|
||||||
protected void onCreate(final Bundle savedInstanceState) {
|
protected void onCreate(final Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
|
Application.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
|
Application.getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -229,7 +229,7 @@ public final class GoBackend implements Backend {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
for (final Tunnel tunnel : Application.getComponent().getTunnelManager().getTunnels()) {
|
for (final Tunnel tunnel : Application.getTunnelManager().getTunnels()) {
|
||||||
if (tunnel != null && tunnel.getState() != State.DOWN)
|
if (tunnel != null && tunnel.getState() != State.DOWN)
|
||||||
tunnel.setState(State.DOWN);
|
tunnel.setState(State.DOWN);
|
||||||
}
|
}
|
||||||
@ -242,7 +242,7 @@ public final class GoBackend implements Backend {
|
|||||||
vpnService.complete(this);
|
vpnService.complete(this);
|
||||||
if (intent == null || intent.getComponent() == null || !intent.getComponent().getPackageName().equals(getPackageName())) {
|
if (intent == null || intent.getComponent() == null || !intent.getComponent().getPackageName().equals(getPackageName())) {
|
||||||
Log.d(TAG, "Service started by Always-on VPN feature");
|
Log.d(TAG, "Service started by Always-on VPN feature");
|
||||||
Application.getComponent().getTunnelManager().restoreState(true).whenComplete(ExceptionLoggers.D);
|
Application.getTunnelManager().restoreState(true).whenComplete(ExceptionLoggers.D);
|
||||||
}
|
}
|
||||||
return super.onStartCommand(intent, flags, startId);
|
return super.onStartCommand(intent, flags, startId);
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ package com.wireguard.android.backend;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.wireguard.android.Application;
|
||||||
import com.wireguard.android.model.Tunnel;
|
import com.wireguard.android.model.Tunnel;
|
||||||
import com.wireguard.android.model.Tunnel.State;
|
import com.wireguard.android.model.Tunnel.State;
|
||||||
import com.wireguard.android.model.Tunnel.Statistics;
|
import com.wireguard.android.model.Tunnel.Statistics;
|
||||||
@ -35,14 +36,9 @@ public final class WgQuickBackend implements Backend {
|
|||||||
private static final String TAG = "WireGuard/" + WgQuickBackend.class.getSimpleName();
|
private static final String TAG = "WireGuard/" + WgQuickBackend.class.getSimpleName();
|
||||||
|
|
||||||
private final File localTemporaryDir;
|
private final File localTemporaryDir;
|
||||||
private final RootShell rootShell;
|
|
||||||
private final ToolsInstaller toolsInstaller;
|
|
||||||
|
|
||||||
public WgQuickBackend(final Context context, final RootShell rootShell,
|
public WgQuickBackend(final Context context) {
|
||||||
final ToolsInstaller toolsInstaller) {
|
|
||||||
localTemporaryDir = new File(context.getCacheDir(), "tmp");
|
localTemporaryDir = new File(context.getCacheDir(), "tmp");
|
||||||
this.rootShell = rootShell;
|
|
||||||
this.toolsInstaller = toolsInstaller;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -66,8 +62,8 @@ public final class WgQuickBackend implements Backend {
|
|||||||
final List<String> output = new ArrayList<>();
|
final List<String> output = new ArrayList<>();
|
||||||
// Don't throw an exception here or nothing will show up in the UI.
|
// Don't throw an exception here or nothing will show up in the UI.
|
||||||
try {
|
try {
|
||||||
toolsInstaller.ensureToolsAvailable();
|
Application.getToolsInstaller().ensureToolsAvailable();
|
||||||
if (rootShell.run(output, "wg show interfaces") != 0 || output.isEmpty())
|
if (Application.getRootShell().run(output, "wg show interfaces") != 0 || output.isEmpty())
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
Log.w(TAG, "Unable to enumerate running tunnels", e);
|
Log.w(TAG, "Unable to enumerate running tunnels", e);
|
||||||
@ -95,7 +91,7 @@ public final class WgQuickBackend implements Backend {
|
|||||||
if (state == originalState)
|
if (state == originalState)
|
||||||
return originalState;
|
return originalState;
|
||||||
Log.d(TAG, "Changing tunnel " + tunnel.getName() + " to state " + state);
|
Log.d(TAG, "Changing tunnel " + tunnel.getName() + " to state " + state);
|
||||||
toolsInstaller.ensureToolsAvailable();
|
Application.getToolsInstaller().ensureToolsAvailable();
|
||||||
setStateInternal(tunnel, tunnel.getConfig(), state);
|
setStateInternal(tunnel, tunnel.getConfig(), state);
|
||||||
return getState(tunnel);
|
return getState(tunnel);
|
||||||
}
|
}
|
||||||
@ -110,7 +106,7 @@ public final class WgQuickBackend implements Backend {
|
|||||||
state.toString().toLowerCase(), tempFile.getAbsolutePath());
|
state.toString().toLowerCase(), tempFile.getAbsolutePath());
|
||||||
if (state == State.UP)
|
if (state == State.UP)
|
||||||
command = "cat /sys/module/wireguard/version && " + command;
|
command = "cat /sys/module/wireguard/version && " + command;
|
||||||
final int result = rootShell.run(null, command);
|
final int result = Application.getRootShell().run(null, command);
|
||||||
// noinspection ResultOfMethodCallIgnored
|
// noinspection ResultOfMethodCallIgnored
|
||||||
tempFile.delete();
|
tempFile.delete();
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
|
@ -9,7 +9,6 @@ package com.wireguard.android.configStore;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.wireguard.android.Application.ApplicationContext;
|
|
||||||
import com.wireguard.config.Config;
|
import com.wireguard.config.Config;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -32,7 +31,7 @@ public final class FileConfigStore implements ConfigStore {
|
|||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
|
|
||||||
public FileConfigStore(@ApplicationContext final Context context) {
|
public FileConfigStore(final Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,7 +128,7 @@ public class TunnelEditorFragment extends BaseFragment {
|
|||||||
}
|
}
|
||||||
if (tunnel == null) {
|
if (tunnel == null) {
|
||||||
Log.d(TAG, "Attempting to create new tunnel " + binding.getConfig().getName());
|
Log.d(TAG, "Attempting to create new tunnel " + binding.getConfig().getName());
|
||||||
final TunnelManager manager = Application.getComponent().getTunnelManager();
|
final TunnelManager manager = Application.getTunnelManager();
|
||||||
manager.create(binding.getConfig().getName(), newConfig)
|
manager.create(binding.getConfig().getName(), newConfig)
|
||||||
.whenComplete(this::onTunnelCreated);
|
.whenComplete(this::onTunnelCreated);
|
||||||
} else if (!tunnel.getName().equals(binding.getConfig().getName())) {
|
} else if (!tunnel.getName().equals(binding.getConfig().getName())) {
|
||||||
|
@ -34,7 +34,6 @@ import android.widget.AdapterView.OnItemClickListener;
|
|||||||
import android.widget.AdapterView.OnItemLongClickListener;
|
import android.widget.AdapterView.OnItemLongClickListener;
|
||||||
|
|
||||||
import com.wireguard.android.Application;
|
import com.wireguard.android.Application;
|
||||||
import com.wireguard.android.Application.ApplicationComponent;
|
|
||||||
import com.wireguard.android.R;
|
import com.wireguard.android.R;
|
||||||
import com.wireguard.android.activity.TunnelCreatorActivity;
|
import com.wireguard.android.activity.TunnelCreatorActivity;
|
||||||
import com.wireguard.android.databinding.TunnelListFragmentBinding;
|
import com.wireguard.android.databinding.TunnelListFragmentBinding;
|
||||||
@ -70,9 +69,7 @@ public class TunnelListFragment extends BaseFragment {
|
|||||||
private final MultiChoiceModeListener actionModeListener = new ActionModeListener();
|
private final MultiChoiceModeListener actionModeListener = new ActionModeListener();
|
||||||
private final ListViewCallbacks listViewCallbacks = new ListViewCallbacks();
|
private final ListViewCallbacks listViewCallbacks = new ListViewCallbacks();
|
||||||
private ActionMode actionMode;
|
private ActionMode actionMode;
|
||||||
private AsyncWorker asyncWorker;
|
|
||||||
private TunnelListFragmentBinding binding;
|
private TunnelListFragmentBinding binding;
|
||||||
private TunnelManager tunnelManager;
|
|
||||||
|
|
||||||
public boolean collapseActionMenu() {
|
public boolean collapseActionMenu() {
|
||||||
if (binding.createMenu.isExpanded()) {
|
if (binding.createMenu.isExpanded()) {
|
||||||
@ -90,7 +87,7 @@ public class TunnelListFragment extends BaseFragment {
|
|||||||
|
|
||||||
final Collection<CompletableFuture<Tunnel>> futureTunnels = new ArrayList<>();
|
final Collection<CompletableFuture<Tunnel>> futureTunnels = new ArrayList<>();
|
||||||
final List<Throwable> throwables = new ArrayList<>();
|
final List<Throwable> throwables = new ArrayList<>();
|
||||||
asyncWorker.supplyAsync(() -> {
|
Application.getAsyncWorker().supplyAsync(() -> {
|
||||||
final String[] columns = {OpenableColumns.DISPLAY_NAME};
|
final String[] columns = {OpenableColumns.DISPLAY_NAME};
|
||||||
String name = null;
|
String name = null;
|
||||||
try (Cursor cursor = contentResolver.query(uri, columns,
|
try (Cursor cursor = contentResolver.query(uri, columns,
|
||||||
@ -137,11 +134,11 @@ public class TunnelListFragment extends BaseFragment {
|
|||||||
throwables.add(e);
|
throwables.add(e);
|
||||||
}
|
}
|
||||||
if (config != null)
|
if (config != null)
|
||||||
futureTunnels.add(tunnelManager.create(name, config).toCompletableFuture());
|
futureTunnels.add(Application.getTunnelManager().create(name, config).toCompletableFuture());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
futureTunnels.add(tunnelManager.create(name,
|
futureTunnels.add(Application.getTunnelManager().create(name,
|
||||||
Config.from(contentResolver.openInputStream(uri))).toCompletableFuture());
|
Config.from(contentResolver.openInputStream(uri))).toCompletableFuture());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,9 +187,6 @@ public class TunnelListFragment extends BaseFragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onCreate(final Bundle savedInstanceState) {
|
public void onCreate(final Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
final ApplicationComponent applicationComponent = Application.getComponent();
|
|
||||||
asyncWorker = applicationComponent.getAsyncWorker();
|
|
||||||
tunnelManager = applicationComponent.getTunnelManager();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -284,7 +278,7 @@ public class TunnelListFragment extends BaseFragment {
|
|||||||
public void onViewStateRestored(final Bundle savedInstanceState) {
|
public void onViewStateRestored(final Bundle savedInstanceState) {
|
||||||
super.onViewStateRestored(savedInstanceState);
|
super.onViewStateRestored(savedInstanceState);
|
||||||
binding.setFragment(this);
|
binding.setFragment(this);
|
||||||
binding.setTunnels(tunnelManager.getTunnels());
|
binding.setTunnels(Application.getTunnelManager().getTunnels());
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class ActionModeListener implements MultiChoiceModeListener {
|
private final class ActionModeListener implements MultiChoiceModeListener {
|
||||||
|
@ -9,19 +9,16 @@ package com.wireguard.android.model;
|
|||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.databinding.BaseObservable;
|
import android.databinding.BaseObservable;
|
||||||
import android.databinding.Bindable;
|
import android.databinding.Bindable;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
import com.wireguard.android.Application;
|
import com.wireguard.android.Application;
|
||||||
import com.wireguard.android.Application.ApplicationScope;
|
|
||||||
import com.wireguard.android.BR;
|
import com.wireguard.android.BR;
|
||||||
import com.wireguard.android.backend.Backend;
|
import com.wireguard.android.backend.Backend;
|
||||||
import com.wireguard.android.configStore.ConfigStore;
|
import com.wireguard.android.configStore.ConfigStore;
|
||||||
import com.wireguard.android.model.Tunnel.State;
|
import com.wireguard.android.model.Tunnel.State;
|
||||||
import com.wireguard.android.model.Tunnel.Statistics;
|
import com.wireguard.android.model.Tunnel.Statistics;
|
||||||
import com.wireguard.android.util.AsyncWorker;
|
|
||||||
import com.wireguard.android.util.ExceptionLoggers;
|
import com.wireguard.android.util.ExceptionLoggers;
|
||||||
import com.wireguard.android.util.ObservableKeyedList;
|
import com.wireguard.android.util.ObservableKeyedList;
|
||||||
import com.wireguard.android.util.ObservableSortedKeyedArrayList;
|
import com.wireguard.android.util.ObservableSortedKeyedArrayList;
|
||||||
@ -33,8 +30,6 @@ import java.util.Collection;
|
|||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import java9.util.Comparators;
|
import java9.util.Comparators;
|
||||||
import java9.util.concurrent.CompletableFuture;
|
import java9.util.concurrent.CompletableFuture;
|
||||||
import java9.util.concurrent.CompletionStage;
|
import java9.util.concurrent.CompletionStage;
|
||||||
@ -45,7 +40,6 @@ import java9.util.stream.StreamSupport;
|
|||||||
* Maintains and mediates changes to the set of available WireGuard tunnels,
|
* Maintains and mediates changes to the set of available WireGuard tunnels,
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ApplicationScope
|
|
||||||
public final class TunnelManager extends BaseObservable {
|
public final class TunnelManager extends BaseObservable {
|
||||||
private static final Comparator<String> COMPARATOR = Comparators.<String>thenComparing(
|
private static final Comparator<String> COMPARATOR = Comparators.<String>thenComparing(
|
||||||
String.CASE_INSENSITIVE_ORDER, Comparators.naturalOrder());
|
String.CASE_INSENSITIVE_ORDER, Comparators.naturalOrder());
|
||||||
@ -53,23 +47,17 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
private static final String KEY_RESTORE_ON_BOOT = "restore_on_boot";
|
private static final String KEY_RESTORE_ON_BOOT = "restore_on_boot";
|
||||||
private static final String KEY_RUNNING_TUNNELS = "enabled_configs";
|
private static final String KEY_RUNNING_TUNNELS = "enabled_configs";
|
||||||
|
|
||||||
private final AsyncWorker asyncWorker;
|
|
||||||
private final Backend backend;
|
private final Backend backend;
|
||||||
private final ConfigStore configStore;
|
private final ConfigStore configStore;
|
||||||
private final SharedPreferences preferences;
|
|
||||||
private final ObservableSortedKeyedList<String, Tunnel> tunnels =
|
private final ObservableSortedKeyedList<String, Tunnel> tunnels =
|
||||||
new ObservableSortedKeyedArrayList<>(COMPARATOR);
|
new ObservableSortedKeyedArrayList<>(COMPARATOR);
|
||||||
private Tunnel lastUsedTunnel;
|
private Tunnel lastUsedTunnel;
|
||||||
private boolean haveLoaded;
|
private boolean haveLoaded;
|
||||||
private final ArrayList<CompletableFuture<Void>> delayedLoadRestoreTunnels = new ArrayList<>();
|
private final ArrayList<CompletableFuture<Void>> delayedLoadRestoreTunnels = new ArrayList<>();
|
||||||
|
|
||||||
@Inject
|
public TunnelManager(final Backend backend, final ConfigStore configStore) {
|
||||||
public TunnelManager(final AsyncWorker asyncWorker, final Backend backend,
|
|
||||||
final ConfigStore configStore, final SharedPreferences preferences) {
|
|
||||||
this.asyncWorker = asyncWorker;
|
|
||||||
this.backend = backend;
|
this.backend = backend;
|
||||||
this.configStore = configStore;
|
this.configStore = configStore;
|
||||||
this.preferences = preferences;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Tunnel addToList(final String name, final Config config, final State state) {
|
private Tunnel addToList(final String name, final Config config, final State state) {
|
||||||
@ -85,7 +73,7 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
final String message = "Tunnel " + name + " already exists";
|
final String message = "Tunnel " + name + " already exists";
|
||||||
return CompletableFuture.failedFuture(new IllegalArgumentException(message));
|
return CompletableFuture.failedFuture(new IllegalArgumentException(message));
|
||||||
}
|
}
|
||||||
return asyncWorker.supplyAsync(() -> configStore.create(name, config))
|
return Application.getAsyncWorker().supplyAsync(() -> configStore.create(name, config))
|
||||||
.thenApply(savedConfig -> addToList(name, savedConfig, State.DOWN));
|
.thenApply(savedConfig -> addToList(name, savedConfig, State.DOWN));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +84,7 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
if (wasLastUsed)
|
if (wasLastUsed)
|
||||||
setLastUsedTunnel(null);
|
setLastUsedTunnel(null);
|
||||||
tunnels.remove(tunnel);
|
tunnels.remove(tunnel);
|
||||||
return asyncWorker.runAsync(() -> {
|
return Application.getAsyncWorker().runAsync(() -> {
|
||||||
if (originalState == State.UP)
|
if (originalState == State.UP)
|
||||||
backend.setState(tunnel, State.DOWN);
|
backend.setState(tunnel, State.DOWN);
|
||||||
try {
|
try {
|
||||||
@ -123,17 +111,17 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
CompletionStage<Config> getTunnelConfig(final Tunnel tunnel) {
|
CompletionStage<Config> getTunnelConfig(final Tunnel tunnel) {
|
||||||
return asyncWorker.supplyAsync(() -> configStore.load(tunnel.getName()))
|
return Application.getAsyncWorker().supplyAsync(() -> configStore.load(tunnel.getName()))
|
||||||
.thenApply(tunnel::onConfigChanged);
|
.thenApply(tunnel::onConfigChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
CompletionStage<State> getTunnelState(final Tunnel tunnel) {
|
CompletionStage<State> getTunnelState(final Tunnel tunnel) {
|
||||||
return asyncWorker.supplyAsync(() -> backend.getState(tunnel))
|
return Application.getAsyncWorker().supplyAsync(() -> backend.getState(tunnel))
|
||||||
.thenApply(tunnel::onStateChanged);
|
.thenApply(tunnel::onStateChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
CompletionStage<Statistics> getTunnelStatistics(final Tunnel tunnel) {
|
CompletionStage<Statistics> getTunnelStatistics(final Tunnel tunnel) {
|
||||||
return asyncWorker.supplyAsync(() -> backend.getStatistics(tunnel))
|
return Application.getAsyncWorker().supplyAsync(() -> backend.getStatistics(tunnel))
|
||||||
.thenApply(tunnel::onStatisticsChanged);
|
.thenApply(tunnel::onStatisticsChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,8 +130,8 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
asyncWorker.supplyAsync(configStore::enumerate)
|
Application.getAsyncWorker().supplyAsync(configStore::enumerate)
|
||||||
.thenAcceptBoth(asyncWorker.supplyAsync(backend::enumerate), this::onTunnelsLoaded)
|
.thenAcceptBoth(Application.getAsyncWorker().supplyAsync(backend::enumerate), this::onTunnelsLoaded)
|
||||||
.whenComplete(ExceptionLoggers.E);
|
.whenComplete(ExceptionLoggers.E);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,7 +139,7 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
private void onTunnelsLoaded(final Iterable<String> present, final Collection<String> running) {
|
private void onTunnelsLoaded(final Iterable<String> present, final Collection<String> running) {
|
||||||
for (final String name : present)
|
for (final String name : present)
|
||||||
addToList(name, null, running.contains(name) ? State.UP : State.DOWN);
|
addToList(name, null, running.contains(name) ? State.UP : State.DOWN);
|
||||||
final String lastUsedName = preferences.getString(KEY_LAST_USED_TUNNEL, null);
|
final String lastUsedName = Application.getSharedPreferences().getString(KEY_LAST_USED_TUNNEL, null);
|
||||||
if (lastUsedName != null)
|
if (lastUsedName != null)
|
||||||
setLastUsedTunnel(tunnels.get(lastUsedName));
|
setLastUsedTunnel(tunnels.get(lastUsedName));
|
||||||
final CompletableFuture<Void>[] toComplete;
|
final CompletableFuture<Void>[] toComplete;
|
||||||
@ -171,7 +159,7 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void refreshTunnelStates() {
|
public void refreshTunnelStates() {
|
||||||
asyncWorker.supplyAsync(backend::enumerate)
|
Application.getAsyncWorker().supplyAsync(backend::enumerate)
|
||||||
.thenAccept(running -> {
|
.thenAccept(running -> {
|
||||||
for (final Tunnel tunnel : tunnels)
|
for (final Tunnel tunnel : tunnels)
|
||||||
tunnel.onStateChanged(running.contains(tunnel.getName()) ? State.UP : State.DOWN);
|
tunnel.onStateChanged(running.contains(tunnel.getName()) ? State.UP : State.DOWN);
|
||||||
@ -180,7 +168,7 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public CompletionStage<Void> restoreState(final boolean force) {
|
public CompletionStage<Void> restoreState(final boolean force) {
|
||||||
if (!force && !preferences.getBoolean(KEY_RESTORE_ON_BOOT, false))
|
if (!force && !Application.getSharedPreferences().getBoolean(KEY_RESTORE_ON_BOOT, false))
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
synchronized (delayedLoadRestoreTunnels) {
|
synchronized (delayedLoadRestoreTunnels) {
|
||||||
if (!haveLoaded) {
|
if (!haveLoaded) {
|
||||||
@ -189,7 +177,7 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final Set<String> previouslyRunning = preferences.getStringSet(KEY_RUNNING_TUNNELS, null);
|
final Set<String> previouslyRunning = Application.getSharedPreferences().getStringSet(KEY_RUNNING_TUNNELS, null);
|
||||||
if (previouslyRunning == null)
|
if (previouslyRunning == null)
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
return CompletableFuture.allOf(StreamSupport.stream(tunnels)
|
return CompletableFuture.allOf(StreamSupport.stream(tunnels)
|
||||||
@ -203,7 +191,7 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
.filter(tunnel -> tunnel.getState() == State.UP)
|
.filter(tunnel -> tunnel.getState() == State.UP)
|
||||||
.map(Tunnel::getName)
|
.map(Tunnel::getName)
|
||||||
.collect(Collectors.toUnmodifiableSet());
|
.collect(Collectors.toUnmodifiableSet());
|
||||||
preferences.edit().putStringSet(KEY_RUNNING_TUNNELS, runningTunnels).apply();
|
Application.getSharedPreferences().edit().putStringSet(KEY_RUNNING_TUNNELS, runningTunnels).apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setLastUsedTunnel(final Tunnel tunnel) {
|
private void setLastUsedTunnel(final Tunnel tunnel) {
|
||||||
@ -212,13 +200,13 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
lastUsedTunnel = tunnel;
|
lastUsedTunnel = tunnel;
|
||||||
notifyPropertyChanged(BR.lastUsedTunnel);
|
notifyPropertyChanged(BR.lastUsedTunnel);
|
||||||
if (tunnel != null)
|
if (tunnel != null)
|
||||||
preferences.edit().putString(KEY_LAST_USED_TUNNEL, tunnel.getName()).apply();
|
Application.getSharedPreferences().edit().putString(KEY_LAST_USED_TUNNEL, tunnel.getName()).apply();
|
||||||
else
|
else
|
||||||
preferences.edit().remove(KEY_LAST_USED_TUNNEL).apply();
|
Application.getSharedPreferences().edit().remove(KEY_LAST_USED_TUNNEL).apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
CompletionStage<Config> setTunnelConfig(final Tunnel tunnel, final Config config) {
|
CompletionStage<Config> setTunnelConfig(final Tunnel tunnel, final Config config) {
|
||||||
return asyncWorker.supplyAsync(() -> {
|
return Application.getAsyncWorker().supplyAsync(() -> {
|
||||||
final Config appliedConfig = backend.applyConfig(tunnel, config);
|
final Config appliedConfig = backend.applyConfig(tunnel, config);
|
||||||
return configStore.save(tunnel.getName(), appliedConfig);
|
return configStore.save(tunnel.getName(), appliedConfig);
|
||||||
}).thenApply(tunnel::onConfigChanged);
|
}).thenApply(tunnel::onConfigChanged);
|
||||||
@ -237,7 +225,7 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
if (wasLastUsed)
|
if (wasLastUsed)
|
||||||
setLastUsedTunnel(null);
|
setLastUsedTunnel(null);
|
||||||
tunnels.remove(tunnel);
|
tunnels.remove(tunnel);
|
||||||
return asyncWorker.supplyAsync(() -> {
|
return Application.getAsyncWorker().supplyAsync(() -> {
|
||||||
if (originalState == State.UP)
|
if (originalState == State.UP)
|
||||||
backend.setState(tunnel, State.DOWN);
|
backend.setState(tunnel, State.DOWN);
|
||||||
configStore.rename(tunnel.getName(), name);
|
configStore.rename(tunnel.getName(), name);
|
||||||
@ -259,7 +247,7 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
CompletionStage<State> setTunnelState(final Tunnel tunnel, final State state) {
|
CompletionStage<State> setTunnelState(final Tunnel tunnel, final State state) {
|
||||||
// Ensure the configuration is loaded before trying to use it.
|
// Ensure the configuration is loaded before trying to use it.
|
||||||
return tunnel.getConfigAsync().thenCompose(x ->
|
return tunnel.getConfigAsync().thenCompose(x ->
|
||||||
asyncWorker.supplyAsync(() -> backend.setState(tunnel, state))
|
Application.getAsyncWorker().supplyAsync(() -> backend.setState(tunnel, state))
|
||||||
).whenComplete((newState, e) -> {
|
).whenComplete((newState, e) -> {
|
||||||
// Ensure onStateChanged is always called (failure or not), and with the correct state.
|
// Ensure onStateChanged is always called (failure or not), and with the correct state.
|
||||||
tunnel.onStateChanged(e == null ? newState : tunnel.getState());
|
tunnel.onStateChanged(e == null ? newState : tunnel.getState());
|
||||||
@ -272,7 +260,7 @@ public final class TunnelManager extends BaseObservable {
|
|||||||
public static final class IntentReceiver extends BroadcastReceiver {
|
public static final class IntentReceiver extends BroadcastReceiver {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(final Context context, final Intent intent) {
|
public void onReceive(final Context context, final Intent intent) {
|
||||||
final TunnelManager manager = Application.getComponent().getTunnelManager();
|
final TunnelManager manager = Application.getTunnelManager();
|
||||||
if (intent == null)
|
if (intent == null)
|
||||||
return;
|
return;
|
||||||
final String action = intent.getAction();
|
final String action = intent.getAction();
|
||||||
|
@ -51,7 +51,7 @@ public class LogExporterPreference extends Preference {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void exportLog() {
|
private void exportLog() {
|
||||||
Application.getComponent().getAsyncWorker().supplyAsync(() -> {
|
Application.getAsyncWorker().supplyAsync(() -> {
|
||||||
final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
||||||
final File file = new File(path, "wireguard-log.txt");
|
final File file = new File(path, "wireguard-log.txt");
|
||||||
if (!path.isDirectory() && !path.mkdirs())
|
if (!path.isDirectory() && !path.mkdirs())
|
||||||
|
@ -13,10 +13,7 @@ import android.system.OsConstants;
|
|||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
|
||||||
import com.wireguard.android.Application;
|
import com.wireguard.android.Application;
|
||||||
import com.wireguard.android.Application.ApplicationComponent;
|
|
||||||
import com.wireguard.android.R;
|
import com.wireguard.android.R;
|
||||||
import com.wireguard.android.util.AsyncWorker;
|
|
||||||
import com.wireguard.android.util.ToolsInstaller;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Preference implementing a button that asynchronously runs {@code ToolsInstaller} and displays the
|
* Preference implementing a button that asynchronously runs {@code ToolsInstaller} and displays the
|
||||||
@ -24,16 +21,10 @@ import com.wireguard.android.util.ToolsInstaller;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public class ToolsInstallerPreference extends Preference {
|
public class ToolsInstallerPreference extends Preference {
|
||||||
private final AsyncWorker asyncWorker;
|
|
||||||
private final ToolsInstaller toolsInstaller;
|
|
||||||
private State state = State.INITIAL;
|
private State state = State.INITIAL;
|
||||||
|
|
||||||
@SuppressWarnings({"SameParameterValue", "WeakerAccess"})
|
|
||||||
public ToolsInstallerPreference(final Context context, final AttributeSet attrs) {
|
public ToolsInstallerPreference(final Context context, final AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
final ApplicationComponent applicationComponent = Application.getComponent();
|
|
||||||
asyncWorker = applicationComponent.getAsyncWorker();
|
|
||||||
toolsInstaller = applicationComponent.getToolsInstaller();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -49,7 +40,7 @@ public class ToolsInstallerPreference extends Preference {
|
|||||||
@Override
|
@Override
|
||||||
public void onAttached() {
|
public void onAttached() {
|
||||||
super.onAttached();
|
super.onAttached();
|
||||||
asyncWorker.supplyAsync(toolsInstaller::areInstalled).whenComplete(this::onCheckResult);
|
Application.getAsyncWorker().supplyAsync(Application.getToolsInstaller()::areInstalled).whenComplete(this::onCheckResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onCheckResult(final Integer result, final Throwable throwable) {
|
private void onCheckResult(final Integer result, final Throwable throwable) {
|
||||||
@ -60,7 +51,7 @@ public class ToolsInstallerPreference extends Preference {
|
|||||||
@Override
|
@Override
|
||||||
protected void onClick() {
|
protected void onClick() {
|
||||||
setState(workingState());
|
setState(workingState());
|
||||||
asyncWorker.supplyAsync(toolsInstaller::install).whenComplete(this::onInstallResult);
|
Application.getAsyncWorker().supplyAsync(Application.getToolsInstaller()::install).whenComplete(this::onInstallResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onInstallResult(final Integer result, final Throwable throwable) {
|
private void onInstallResult(final Integer result, final Throwable throwable) {
|
||||||
@ -86,13 +77,13 @@ public class ToolsInstallerPreference extends Preference {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private State initialState() {
|
private State initialState() {
|
||||||
return toolsInstaller.willInstallAsMagiskModule(false) ? State.INITIAL_MAGISK : State.INITIAL_SYSTEM;
|
return Application.getToolsInstaller().willInstallAsMagiskModule(false) ? State.INITIAL_MAGISK : State.INITIAL_SYSTEM;
|
||||||
}
|
}
|
||||||
private State workingState() {
|
private State workingState() {
|
||||||
return toolsInstaller.willInstallAsMagiskModule(false) ? State.WORKING_MAGISK : State.WORKING_SYSTEM;
|
return Application.getToolsInstaller().willInstallAsMagiskModule(false) ? State.WORKING_MAGISK : State.WORKING_SYSTEM;
|
||||||
}
|
}
|
||||||
private State successState() {
|
private State successState() {
|
||||||
return toolsInstaller.willInstallAsMagiskModule(false) ? State.SUCCESS_MAGISK : State.SUCCESS_SYSTEM;
|
return Application.getToolsInstaller().willInstallAsMagiskModule(false) ? State.SUCCESS_MAGISK : State.SUCCESS_SYSTEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum State {
|
private enum State {
|
||||||
|
@ -16,28 +16,23 @@ import com.wireguard.android.BuildConfig;
|
|||||||
import com.wireguard.android.R;
|
import com.wireguard.android.R;
|
||||||
import com.wireguard.android.backend.GoBackend;
|
import com.wireguard.android.backend.GoBackend;
|
||||||
import com.wireguard.android.backend.WgQuickBackend;
|
import com.wireguard.android.backend.WgQuickBackend;
|
||||||
import com.wireguard.android.util.RootShell;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import java9.util.concurrent.CompletionStage;
|
|
||||||
|
|
||||||
public class VersionPreference extends Preference {
|
public class VersionPreference extends Preference {
|
||||||
private String versionSummary;
|
private String versionSummary;
|
||||||
|
|
||||||
public VersionPreference(final Context context, final AttributeSet attrs) {
|
public VersionPreference(final Context context, final AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
|
|
||||||
if (Application.getComponent().getBackendType() == GoBackend.class) {
|
if (Application.getBackendType() == GoBackend.class) {
|
||||||
versionSummary = getContext().getString(R.string.version_userspace_summary, GoBackend.getVersion());
|
versionSummary = getContext().getString(R.string.version_userspace_summary, GoBackend.getVersion());
|
||||||
} else if (Application.getComponent().getBackendType() == WgQuickBackend.class) {
|
} else if (Application.getBackendType() == WgQuickBackend.class) {
|
||||||
versionSummary = getContext().getString(R.string.version_kernel_summary_checking);
|
versionSummary = getContext().getString(R.string.version_kernel_summary_checking);
|
||||||
Application.getComponent().getAsyncWorker().supplyAsync(() -> {
|
Application.getAsyncWorker().supplyAsync(() -> {
|
||||||
final List<String> output = new ArrayList<>();
|
final List<String> output = new ArrayList<>();
|
||||||
if (Application.getComponent().getRootShell()
|
if (Application.getRootShell()
|
||||||
.run(output, "cat /sys/module/wireguard/version") != 0 || output.isEmpty())
|
.run(output, "cat /sys/module/wireguard/version") != 0 || output.isEmpty())
|
||||||
throw new RuntimeException("Unable to determine kernel module version");
|
throw new RuntimeException("Unable to determine kernel module version");
|
||||||
return output.get(0);
|
return output.get(0);
|
||||||
|
@ -58,7 +58,7 @@ public class ZipExporterPreference extends Preference {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void exportZip() {
|
private void exportZip() {
|
||||||
final List<Tunnel> tunnels = new ArrayList<>(Application.getComponent().getTunnelManager().getTunnels());
|
final List<Tunnel> tunnels = new ArrayList<>(Application.getTunnelManager().getTunnels());
|
||||||
final List<CompletableFuture<Config>> futureConfigs = new ArrayList<>(tunnels.size());
|
final List<CompletableFuture<Config>> futureConfigs = new ArrayList<>(tunnels.size());
|
||||||
for (final Tunnel tunnel : tunnels)
|
for (final Tunnel tunnel : tunnels)
|
||||||
futureConfigs.add(tunnel.getConfigAsync().toCompletableFuture());
|
futureConfigs.add(tunnel.getConfigAsync().toCompletableFuture());
|
||||||
@ -67,7 +67,7 @@ public class ZipExporterPreference extends Preference {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
CompletableFuture.allOf(futureConfigs.toArray(new CompletableFuture[futureConfigs.size()]))
|
CompletableFuture.allOf(futureConfigs.toArray(new CompletableFuture[futureConfigs.size()]))
|
||||||
.whenComplete((ignored1, exception) -> Application.getComponent().getAsyncWorker().supplyAsync(() -> {
|
.whenComplete((ignored1, exception) -> Application.getAsyncWorker().supplyAsync(() -> {
|
||||||
if (exception != null)
|
if (exception != null)
|
||||||
throw exception;
|
throw exception;
|
||||||
final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
||||||
|
@ -8,13 +8,8 @@ package com.wireguard.android.util;
|
|||||||
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
|
||||||
import com.wireguard.android.Application.ApplicationHandler;
|
|
||||||
import com.wireguard.android.Application.ApplicationScope;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import java9.util.concurrent.CompletableFuture;
|
import java9.util.concurrent.CompletableFuture;
|
||||||
import java9.util.concurrent.CompletionStage;
|
import java9.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
@ -22,13 +17,11 @@ import java9.util.concurrent.CompletionStage;
|
|||||||
* Helper class for running asynchronous tasks and ensuring they are completed on the main thread.
|
* Helper class for running asynchronous tasks and ensuring they are completed on the main thread.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ApplicationScope
|
|
||||||
public class AsyncWorker {
|
public class AsyncWorker {
|
||||||
private final Executor executor;
|
private final Executor executor;
|
||||||
private final Handler handler;
|
private final Handler handler;
|
||||||
|
|
||||||
@Inject
|
public AsyncWorker(final Executor executor, final Handler handler) {
|
||||||
AsyncWorker(final Executor executor, @ApplicationHandler final Handler handler) {
|
|
||||||
this.executor = executor;
|
this.executor = executor;
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,6 @@ package com.wireguard.android.util;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.wireguard.android.Application.ApplicationContext;
|
|
||||||
import com.wireguard.android.Application.ApplicationScope;
|
|
||||||
import com.wireguard.android.R;
|
import com.wireguard.android.R;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
@ -23,13 +21,10 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class for running commands as root.
|
* Helper class for running commands as root.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ApplicationScope
|
|
||||||
public class RootShell {
|
public class RootShell {
|
||||||
private static final String SU = "su";
|
private static final String SU = "su";
|
||||||
private static final String TAG = "WireGuard/" + RootShell.class.getSimpleName();
|
private static final String TAG = "WireGuard/" + RootShell.class.getSimpleName();
|
||||||
@ -44,8 +39,7 @@ public class RootShell {
|
|||||||
private OutputStreamWriter stdin;
|
private OutputStreamWriter stdin;
|
||||||
private BufferedReader stdout;
|
private BufferedReader stdout;
|
||||||
|
|
||||||
@Inject
|
public RootShell(final Context context) {
|
||||||
public RootShell(@ApplicationContext final Context context) {
|
|
||||||
deviceNotRootedMessage = context.getString(R.string.error_root);
|
deviceNotRootedMessage = context.getString(R.string.error_root);
|
||||||
final File cacheDir = context.getCacheDir();
|
final File cacheDir = context.getCacheDir();
|
||||||
localBinaryDir = new File(cacheDir, "bin");
|
localBinaryDir = new File(cacheDir, "bin");
|
||||||
|
@ -11,27 +11,19 @@ import android.system.OsConstants;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.wireguard.android.Application;
|
import com.wireguard.android.Application;
|
||||||
import com.wireguard.android.Application.ApplicationContext;
|
|
||||||
import com.wireguard.android.Application.ApplicationScope;
|
|
||||||
import com.wireguard.android.BuildConfig;
|
import com.wireguard.android.BuildConfig;
|
||||||
import com.wireguard.android.util.RootShell.NoRootException;
|
import com.wireguard.android.util.RootShell.NoRootException;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import java9.util.concurrent.CompletionStage;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper to install WireGuard tools to the system partition.
|
* Helper to install WireGuard tools to the system partition.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ApplicationScope
|
|
||||||
public final class ToolsInstaller {
|
public final class ToolsInstaller {
|
||||||
private static final String[][] EXECUTABLES = {
|
private static final String[][] EXECUTABLES = {
|
||||||
{"libwg.so", "wg"},
|
{"libwg.so", "wg"},
|
||||||
@ -47,15 +39,12 @@ public final class ToolsInstaller {
|
|||||||
private final File localBinaryDir;
|
private final File localBinaryDir;
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
private final File nativeLibraryDir;
|
private final File nativeLibraryDir;
|
||||||
private final RootShell rootShell;
|
|
||||||
private Boolean areToolsAvailable;
|
private Boolean areToolsAvailable;
|
||||||
private Boolean installAsMagiskModule;
|
private Boolean installAsMagiskModule;
|
||||||
|
|
||||||
@Inject
|
public ToolsInstaller(final Context context) {
|
||||||
public ToolsInstaller(@ApplicationContext final Context context, final RootShell rootShell) {
|
|
||||||
localBinaryDir = new File(context.getCacheDir(), "bin");
|
localBinaryDir = new File(context.getCacheDir(), "bin");
|
||||||
nativeLibraryDir = new File(context.getApplicationInfo().nativeLibraryDir);
|
nativeLibraryDir = new File(context.getApplicationInfo().nativeLibraryDir);
|
||||||
this.rootShell = rootShell;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static File getInstallDir() {
|
private static File getInstallDir() {
|
||||||
@ -82,7 +71,7 @@ public final class ToolsInstaller {
|
|||||||
}
|
}
|
||||||
script.append("exit ").append(OsConstants.EALREADY).append(';');
|
script.append("exit ").append(OsConstants.EALREADY).append(';');
|
||||||
try {
|
try {
|
||||||
return rootShell.run(null, script.toString());
|
return Application.getRootShell().run(null, script.toString());
|
||||||
} catch (final IOException ignored) {
|
} catch (final IOException ignored) {
|
||||||
return OsConstants.EXIT_FAILURE;
|
return OsConstants.EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
@ -114,7 +103,7 @@ public final class ToolsInstaller {
|
|||||||
if (!checkForIt)
|
if (!checkForIt)
|
||||||
throw new RuntimeException("Expected to already know whether this is a Magisk system");
|
throw new RuntimeException("Expected to already know whether this is a Magisk system");
|
||||||
try {
|
try {
|
||||||
installAsMagiskModule = rootShell.run(null, "[ -d /sbin/.core/mirror -a -d /sbin/.core/img -a ! -f /cache/.disable_magisk ]") == OsConstants.EXIT_SUCCESS;
|
installAsMagiskModule = Application.getRootShell().run(null, "[ -d /sbin/.core/mirror -a -d /sbin/.core/img -a ! -f /cache/.disable_magisk ]") == OsConstants.EXIT_SUCCESS;
|
||||||
} catch (final Exception ignored) {
|
} catch (final Exception ignored) {
|
||||||
installAsMagiskModule = false;
|
installAsMagiskModule = false;
|
||||||
}
|
}
|
||||||
@ -134,7 +123,7 @@ public final class ToolsInstaller {
|
|||||||
new File(nativeLibraryDir, names[0]), destination, destination, destination));
|
new File(nativeLibraryDir, names[0]), destination, destination, destination));
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return rootShell.run(null, script.toString());
|
return Application.getRootShell().run(null, script.toString());
|
||||||
} catch (final IOException ignored) {
|
} catch (final IOException ignored) {
|
||||||
return OsConstants.EXIT_FAILURE;
|
return OsConstants.EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
@ -155,7 +144,7 @@ public final class ToolsInstaller {
|
|||||||
script.append("trap - INT TERM EXIT;");
|
script.append("trap - INT TERM EXIT;");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return rootShell.run(null, script.toString());
|
return Application.getRootShell().run(null, script.toString());
|
||||||
} catch (final IOException ignored) {
|
} catch (final IOException ignored) {
|
||||||
return OsConstants.EXIT_FAILURE;
|
return OsConstants.EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
@ -182,7 +171,7 @@ public final class ToolsInstaller {
|
|||||||
script.append("exit ").append(OsConstants.EXIT_SUCCESS).append(';');
|
script.append("exit ").append(OsConstants.EXIT_SUCCESS).append(';');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return rootShell.run(null, script.toString());
|
return Application.getRootShell().run(null, script.toString());
|
||||||
} catch (final IOException ignored) {
|
} catch (final IOException ignored) {
|
||||||
return OsConstants.EXIT_FAILURE;
|
return OsConstants.EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user