aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-03-29 12:25:34 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-03-29 12:25:34 +0530
commit46c5d53f1a1b9f83c1b9832662b0d25715ccf588 (patch)
tree57e618ef80d184553d38836f4f152d0d5ae7f793
parentAddTunnelsSheet: Make behaviour nullable (diff)
downloadwireguard-android-refactor/dagger-migration.tar.xz
wireguard-android-refactor/dagger-migration.zip
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
-rw-r--r--build.gradle1
-rw-r--r--ui/build.gradle2
-rw-r--r--ui/src/main/java/com/wireguard/android/Application.kt7
-rw-r--r--ui/src/main/java/com/wireguard/android/di/AppComponent.kt134
-rw-r--r--ui/src/main/java/com/wireguard/android/di/InjectorProvider.kt25
-rw-r--r--ui/src/main/java/com/wireguard/android/di/factory/BackendFactory.kt55
6 files changed, 223 insertions, 1 deletions
diff --git a/build.gradle b/build.gradle
index 7710893f..c2b6a413 100644
--- a/build.gradle
+++ b/build.gradle
@@ -15,6 +15,7 @@ buildscript {
coroutinesVersion = '1.3.5'
constraintLayoutVersion = '1.1.3'
coordinatorLayoutVersion = '1.1.0'
+ daggerVersion = '2.27'
agpVersion = '3.6.1'
fragmentVersion = '1.2.3'
materialComponentsVersion = '1.1.0'
diff --git a/ui/build.gradle b/ui/build.gradle
index 2ebfc1af..c67216b3 100644
--- a/ui/build.gradle
+++ b/ui/build.gradle
@@ -83,12 +83,14 @@ dependencies {
implementation "androidx.fragment:fragment:$fragmentVersion"
implementation "androidx.preference:preference:$preferenceVersion"
implementation "com.google.android.material:material:$materialComponentsVersion"
+ implementation "com.google.dagger:dagger:$daggerVersion"
implementation "com.journeyapps:zxing-android-embedded:$zxingEmbeddedVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
implementation "net.sourceforge.streamsupport:android-retrofuture:$streamsupportVersion"
implementation "net.sourceforge.streamsupport:android-retrostreams:$streamsupportVersion"
+ kapt "com.google.dagger:dagger-compiler:$daggerVersion"
}
tasks.withType(JavaCompile) {
diff --git a/ui/src/main/java/com/wireguard/android/Application.kt b/ui/src/main/java/com/wireguard/android/Application.kt
index d533028f..8dddf7df 100644
--- a/ui/src/main/java/com/wireguard/android/Application.kt
+++ b/ui/src/main/java/com/wireguard/android/Application.kt
@@ -21,6 +21,9 @@ import com.wireguard.android.backend.Backend
import com.wireguard.android.backend.GoBackend
import com.wireguard.android.backend.WgQuickBackend
import com.wireguard.android.configStore.FileConfigStore
+import com.wireguard.android.di.AppComponent
+import com.wireguard.android.di.DaggerAppComponent
+import com.wireguard.android.di.InjectorProvider
import com.wireguard.android.model.TunnelManager
import com.wireguard.android.util.AsyncWorker
import com.wireguard.android.util.ExceptionLoggers
@@ -31,7 +34,7 @@ import java9.util.concurrent.CompletableFuture
import java.lang.ref.WeakReference
import java.util.Locale
-class Application : android.app.Application(), OnSharedPreferenceChangeListener {
+class Application : android.app.Application(), OnSharedPreferenceChangeListener, InjectorProvider {
private val futureBackend = CompletableFuture<Backend>()
private lateinit var asyncWorker: AsyncWorker
private var backend: Backend? = null
@@ -41,6 +44,8 @@ class Application : android.app.Application(), OnSharedPreferenceChangeListener
private lateinit var toolsInstaller: ToolsInstaller
private lateinit var tunnelManager: TunnelManager
+ override val component by lazy { DaggerAppComponent.factory().create(applicationContext) }
+
override fun attachBaseContext(context: Context) {
super.attachBaseContext(context)
if (BuildConfig.MIN_SDK_VERSION > Build.VERSION.SDK_INT) {
diff --git a/ui/src/main/java/com/wireguard/android/di/AppComponent.kt b/ui/src/main/java/com/wireguard/android/di/AppComponent.kt
new file mode 100644
index 00000000..f9b41583
--- /dev/null
+++ b/ui/src/main/java/com/wireguard/android/di/AppComponent.kt
@@ -0,0 +1,134 @@
+/*
+ * Copyright © 2020 WireGuard LLC. All Rights Reserved.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package com.wireguard.android.di
+
+import android.content.Context
+import android.content.SharedPreferences
+import android.os.AsyncTask
+import android.os.Handler
+import android.os.Looper
+import androidx.preference.PreferenceManager
+import com.wireguard.android.Application
+import com.wireguard.android.activity.BaseActivity
+import com.wireguard.android.activity.MainActivity
+import com.wireguard.android.activity.SettingsActivity.SettingsFragment
+import com.wireguard.android.activity.TunnelToggleActivity
+import com.wireguard.android.backend.Backend
+import com.wireguard.android.backend.GoBackend
+import com.wireguard.android.configStore.ConfigStore
+import com.wireguard.android.configStore.FileConfigStore
+import com.wireguard.android.di.factory.BackendFactory
+import com.wireguard.android.fragment.AppListDialogFragment
+import com.wireguard.android.fragment.BaseFragment
+import com.wireguard.android.fragment.ConfigNamingDialogFragment
+import com.wireguard.android.fragment.TunnelDetailFragment
+import com.wireguard.android.fragment.TunnelEditorFragment
+import com.wireguard.android.fragment.TunnelListFragment
+import com.wireguard.android.model.TunnelManager
+import com.wireguard.android.preference.ToolsInstallerPreference
+import com.wireguard.android.preference.VersionPreference
+import com.wireguard.android.util.ModuleLoader
+import com.wireguard.android.util.RootShell
+import com.wireguard.android.util.ToolsInstaller
+import dagger.BindsInstance
+import dagger.Component
+import dagger.Module
+import dagger.Provides
+import java9.util.concurrent.CompletableFuture
+import java.util.concurrent.Executor
+import javax.inject.Singleton
+
+@Singleton
+@Component(modules = [ApplicationModule::class])
+interface AppComponent {
+
+ @Component.Factory
+ interface Factory {
+ fun create(@BindsInstance applicationContext: Context): AppComponent
+ }
+
+ // Application
+ fun inject(application: Application)
+
+ // Activities
+ fun inject(activity: BaseActivity)
+ fun inject(activity: MainActivity)
+ fun inject(activity: TunnelToggleActivity)
+
+ // Fragments
+ fun inject(fragment: BaseFragment)
+ fun inject(fragment: AppListDialogFragment)
+ fun inject(fragment: ConfigNamingDialogFragment)
+ fun inject(fragment: SettingsFragment)
+ fun inject(fragment: TunnelDetailFragment)
+ fun inject(fragment: TunnelEditorFragment)
+ fun inject(fragment: TunnelListFragment)
+
+ // Preferences
+ fun inject(preference: ToolsInstallerPreference)
+ fun inject(preference: VersionPreference)
+ fun inject(receiver: TunnelManager.IntentReceiver)
+
+ // Services
+ fun inject(service: GoBackend.VpnService)
+}
+
+@Module
+object ApplicationModule {
+ @get:Singleton
+ @get:Provides
+ val executor: Executor = AsyncTask.SERIAL_EXECUTOR
+
+ @get:Singleton
+ @get:Provides
+ val handler: Handler = Handler(Looper.getMainLooper())
+
+ @Singleton
+ @Provides
+ fun provideSharedPrefs(context: Context): SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
+
+ @Singleton
+ @Provides
+ fun provideConfigStore(context: Context): ConfigStore = FileConfigStore(context)
+
+ @Singleton
+ @Provides
+ fun provideBackend(
+ context: Context,
+ moduleLoader: ModuleLoader,
+ rootShell: RootShell,
+ sharedPreferences: SharedPreferences,
+ toolsInstaller: ToolsInstaller,
+ tunnelManager: TunnelManager
+ ): Backend {
+ return BackendFactory.getBackend(
+ context,
+ moduleLoader,
+ rootShell,
+ sharedPreferences,
+ toolsInstaller,
+ tunnelManager
+ )
+ }
+
+ @Provides
+ @Singleton
+ fun provideModuleLoader(context: Context, rootShell: RootShell): ModuleLoader {
+ return ModuleLoader(context, rootShell, Application.USER_AGENT)
+ }
+
+ @Singleton
+ @Provides
+ fun provideBackendType(backend: Backend): Class<Backend> = backend.javaClass
+
+ @Singleton
+ @Provides
+ fun provideBackendAsync(backend: Backend): CompletableFuture<Backend> {
+ val backendAsync = CompletableFuture<Backend>()
+ backendAsync.complete(backend)
+ return backendAsync
+ }
+}
diff --git a/ui/src/main/java/com/wireguard/android/di/InjectorProvider.kt b/ui/src/main/java/com/wireguard/android/di/InjectorProvider.kt
new file mode 100644
index 00000000..ab9141ca
--- /dev/null
+++ b/ui/src/main/java/com/wireguard/android/di/InjectorProvider.kt
@@ -0,0 +1,25 @@
+/*
+ * Copyright © 2020 WireGuard LLC. All Rights Reserved.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package com.wireguard.android.di
+
+
+import android.app.Service
+import android.content.ContentProvider
+import android.content.Context
+import androidx.fragment.app.Fragment
+import androidx.fragment.app.FragmentActivity
+import androidx.preference.Preference
+
+interface InjectorProvider {
+ val component: AppComponent
+}
+
+val ContentProvider.injector get() = (context?.applicationContext as InjectorProvider).component
+val FragmentActivity.injector get() = (application as InjectorProvider).component
+val Fragment.injector get() = (requireContext().applicationContext as InjectorProvider).component
+val Preference.injector get() = (context.applicationContext as InjectorProvider).component
+val Service.injector get() = (applicationContext as InjectorProvider).component
+fun getInjector(context: Context) = (context.applicationContext as InjectorProvider).component
diff --git a/ui/src/main/java/com/wireguard/android/di/factory/BackendFactory.kt b/ui/src/main/java/com/wireguard/android/di/factory/BackendFactory.kt
new file mode 100644
index 00000000..cc07363a
--- /dev/null
+++ b/ui/src/main/java/com/wireguard/android/di/factory/BackendFactory.kt
@@ -0,0 +1,55 @@
+/*
+ * Copyright © 2020 WireGuard LLC. All Rights Reserved.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package com.wireguard.android.di.factory
+
+import android.content.Context
+import android.content.SharedPreferences
+import com.wireguard.android.backend.Backend
+import com.wireguard.android.backend.GoBackend
+import com.wireguard.android.backend.WgQuickBackend
+import com.wireguard.android.model.TunnelManager
+import com.wireguard.android.util.ExceptionLoggers
+import com.wireguard.android.util.ModuleLoader
+import com.wireguard.android.util.RootShell
+import com.wireguard.android.util.ToolsInstaller
+
+object BackendFactory {
+ fun getBackend(
+ context: Context,
+ moduleLoader: ModuleLoader,
+ rootShell: RootShell,
+ sharedPreferences: SharedPreferences,
+ toolsInstaller: ToolsInstaller,
+ tunnelManager: TunnelManager
+ ): Backend {
+ var ret: Backend? = null
+ var didStartRootShell = false
+ if (!ModuleLoader.isModuleLoaded() && moduleLoader.moduleMightExist()) {
+ try {
+ rootShell.start()
+ didStartRootShell = true
+ moduleLoader.loadModule()
+ } catch (_: Exception) {
+ }
+ }
+ if (!sharedPreferences.getBoolean("disable_kernel_module", false) && ModuleLoader.isModuleLoaded()) {
+ try {
+ if (!didStartRootShell) rootShell.start()
+ ret = WgQuickBackend(context, rootShell, toolsInstaller).apply {
+ setMultipleTunnels(sharedPreferences.getBoolean("multiple_tunnels", false))
+ }
+ } catch (_: Exception) {
+ }
+ }
+ if (ret == null) {
+ ret = GoBackend(context)
+ GoBackend.setAlwaysOnCallback {
+ tunnelManager.restoreState(true).whenComplete(ExceptionLoggers.D)
+ }
+ }
+ return ret
+ }
+}