Skip to content
Permalink
Browse files

Give Samuel heart attack by removing Dagger

  • Loading branch information
zx2c4 committed Jun 7, 2018
1 parent 0a9aa03 commit 712b6c6f600ef6eb683d356a6e9a05e9415b7e12
Showing with 115 additions and 233 deletions.
  1. +0 −3 app/build.gradle
  2. +50 −103 app/src/main/java/com/wireguard/android/Application.java
  3. +2 −2 app/src/main/java/com/wireguard/android/BootShutdownReceiver.java
  4. +3 −5 app/src/main/java/com/wireguard/android/QuickTileService.java
  5. +2 −2 app/src/main/java/com/wireguard/android/activity/BaseActivity.java
  6. +1 −1 app/src/main/java/com/wireguard/android/activity/SettingsActivity.java
  7. +4 −3 app/src/main/java/com/wireguard/android/activity/ThemeChangeAwareActivity.java
  8. +2 −2 app/src/main/java/com/wireguard/android/backend/GoBackend.java
  9. +6 −10 app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java
  10. +1 −2 app/src/main/java/com/wireguard/android/configStore/FileConfigStore.java
  11. +1 −1 app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java
  12. +4 −10 app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java
  13. +19 −31 app/src/main/java/com/wireguard/android/model/TunnelManager.java
  14. +1 −1 app/src/main/java/com/wireguard/android/preference/LogExporterPreference.java
  15. +5 −14 app/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.java
  16. +4 −9 app/src/main/java/com/wireguard/android/preference/VersionPreference.java
  17. +2 −2 app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java
  18. +1 −8 app/src/main/java/com/wireguard/android/util/AsyncWorker.java
  19. +1 −7 app/src/main/java/com/wireguard/android/util/RootShell.java
  20. +6 −17 app/src/main/java/com/wireguard/android/util/ToolsInstaller.java
@@ -52,19 +52,16 @@ android {
ext {
databindingVersion = '3.1.2'
supportLibsVersion = '27.1.1'
daggerVersion = '2.14.1'
streamsupportVersion = '1.6.0'
}

dependencies {
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
implementation "com.android.databinding:library:$databindingVersion"
implementation "com.android.support:appcompat-v7:$supportLibsVersion"
implementation "com.android.support:cardview-v7:$supportLibsVersion"
implementation "com.android.support:design:$supportLibsVersion"
implementation "com.android.support:preference-v14:$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-retrostreams:$streamsupportVersion"
}
@@ -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.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();
}
}
@@ -20,13 +20,13 @@

@Override
public void onReceive(final Context context, final Intent intent) {
if (Application.getComponent().getBackendType() != WgQuickBackend.class) {
if (Application.getBackendType() != WgQuickBackend.class) {
return;
}
final String action = intent.getAction();
if (action == null)
return;
final TunnelManager tunnelManager = Application.getComponent().getTunnelManager();
final TunnelManager tunnelManager = Application.getTunnelManager();
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Log.i(TAG, "Broadcast receiver restoring state (boot)");
tunnelManager.restoreState(false).whenComplete(ExceptionLoggers.D);
@@ -38,7 +38,6 @@
private final OnStateChangedCallback onStateChangedCallback = new OnStateChangedCallback();
private final OnTunnelChangedCallback onTunnelChangedCallback = new OnTunnelChangedCallback();
private Tunnel tunnel;
private TunnelManager tunnelManager;

@Override
public void onClick() {
@@ -54,12 +53,11 @@ public void onClick() {
@Override
public void onCreate() {
super.onCreate();
tunnelManager = Application.getComponent().getTunnelManager();
}

@Override
public void onStartListening() {
tunnelManager.addOnPropertyChangedCallback(onTunnelChangedCallback);
Application.getTunnelManager().addOnPropertyChangedCallback(onTunnelChangedCallback);
if (tunnel != null)
tunnel.addOnPropertyChangedCallback(onStateChangedCallback);
updateTile();
@@ -69,7 +67,7 @@ public void onStartListening() {
public void onStopListening() {
if (tunnel != null)
tunnel.removeOnPropertyChangedCallback(onStateChangedCallback);
tunnelManager.removeOnPropertyChangedCallback(onTunnelChangedCallback);
Application.getTunnelManager().removeOnPropertyChangedCallback(onTunnelChangedCallback);
}

private void onToggleFinished(@SuppressWarnings("unused") final State state,
@@ -84,7 +82,7 @@ private void onToggleFinished(@SuppressWarnings("unused") final State state,

private void updateTile() {
// Update the tunnel.
final Tunnel newTunnel = tunnelManager.getLastUsedTunnel();
final Tunnel newTunnel = Application.getTunnelManager().getLastUsedTunnel();
if (newTunnel != tunnel) {
if (tunnel != null)
tunnel.removeOnPropertyChangedCallback(onStateChangedCallback);
@@ -47,14 +47,14 @@ protected void onCreate(final Bundle savedInstanceState) {
else if (getIntent() != null)
savedTunnelName = getIntent().getStringExtra(KEY_SELECTED_TUNNEL);
if (savedTunnelName != null) {
final TunnelManager tunnelManager = Application.getComponent().getTunnelManager();
final TunnelManager tunnelManager = Application.getTunnelManager();
selectedTunnel = tunnelManager.getTunnels().get(savedTunnelName);
}

// The selected tunnel must be set before the superclass method recreates fragments.
super.onCreate(savedInstanceState);

if (Application.getComponent().getBackendType() == GoBackend.class) {
if (Application.getBackendType() == GoBackend.class) {
final Intent intent = GoBackend.VpnService.prepare(this);
if (intent != null) {
startActivityForResult(intent, 0);
@@ -95,7 +95,7 @@ public void onRequestPermissionsResult(final int requestCode,
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String key) {
addPreferencesFromResource(R.xml.preferences);
if (Application.getComponent().getBackendType() != WgQuickBackend.class) {
if (Application.getBackendType() != WgQuickBackend.class) {
Preference pref = getPreferenceManager().findPreference("tools_installer");
getPreferenceScreen().removePreference(pref);
pref = getPreferenceManager().findPreference("restore_on_boot");
@@ -8,11 +8,12 @@
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.util.Log;

import com.wireguard.android.Application;

import java.lang.reflect.Field;

public abstract class ThemeChangeAwareActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
@@ -52,12 +53,12 @@ private static synchronized void invalidateDrawableCache(final Resources resourc
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
Application.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onDestroy() {
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
Application.getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}

@@ -229,7 +229,7 @@ public void onCreate() {

@Override
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)
tunnel.setState(State.DOWN);
}
@@ -242,7 +242,7 @@ public int onStartCommand(final Intent intent, final int flags, final int startI
vpnService.complete(this);
if (intent == null || intent.getComponent() == null || !intent.getComponent().getPackageName().equals(getPackageName())) {
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);
}
@@ -9,6 +9,7 @@
import android.content.Context;
import android.util.Log;

import com.wireguard.android.Application;
import com.wireguard.android.model.Tunnel;
import com.wireguard.android.model.Tunnel.State;
import com.wireguard.android.model.Tunnel.Statistics;
@@ -35,14 +36,9 @@
private static final String TAG = "WireGuard/" + WgQuickBackend.class.getSimpleName();

private final File localTemporaryDir;
private final RootShell rootShell;
private final ToolsInstaller toolsInstaller;

public WgQuickBackend(final Context context, final RootShell rootShell,
final ToolsInstaller toolsInstaller) {
public WgQuickBackend(final Context context) {
localTemporaryDir = new File(context.getCacheDir(), "tmp");
this.rootShell = rootShell;
this.toolsInstaller = toolsInstaller;
}

@Override
@@ -66,8 +62,8 @@ public Config applyConfig(final Tunnel tunnel, final Config config) throws Excep
final List<String> output = new ArrayList<>();
// Don't throw an exception here or nothing will show up in the UI.
try {
toolsInstaller.ensureToolsAvailable();
if (rootShell.run(output, "wg show interfaces") != 0 || output.isEmpty())
Application.getToolsInstaller().ensureToolsAvailable();
if (Application.getRootShell().run(output, "wg show interfaces") != 0 || output.isEmpty())
return Collections.emptySet();
} catch (final Exception e) {
Log.w(TAG, "Unable to enumerate running tunnels", e);
@@ -95,7 +91,7 @@ public State setState(final Tunnel tunnel, State state) throws Exception {
if (state == originalState)
return originalState;
Log.d(TAG, "Changing tunnel " + tunnel.getName() + " to state " + state);
toolsInstaller.ensureToolsAvailable();
Application.getToolsInstaller().ensureToolsAvailable();
setStateInternal(tunnel, tunnel.getConfig(), state);
return getState(tunnel);
}
@@ -110,7 +106,7 @@ private void setStateInternal(final Tunnel tunnel, final Config config, final St
state.toString().toLowerCase(), tempFile.getAbsolutePath());
if (state == State.UP)
command = "cat /sys/module/wireguard/version && " + command;
final int result = rootShell.run(null, command);
final int result = Application.getRootShell().run(null, command);
// noinspection ResultOfMethodCallIgnored
tempFile.delete();
if (result != 0)

0 comments on commit 712b6c6

Please sign in to comment.
You can’t perform that action at this time.