Skip to content
Permalink
Browse files

Migrate TunnelRestoreWorker to Dagger

Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
  • Loading branch information
msfjarvis committed Dec 29, 2019
1 parent b08f384 commit e23f878140d4bda9e2c54d6c2684e07994066fd6
@@ -28,6 +28,7 @@ import com.wireguard.android.di.injector
import com.wireguard.android.fragment.AppListDialogFragment
import com.wireguard.android.model.TunnelManager
import com.wireguard.android.util.ApplicationPreferences
import com.wireguard.android.util.AsyncWorker
import com.wireguard.android.util.AuthenticationResult
import com.wireguard.android.util.Authenticator
import com.wireguard.android.util.BackendAsync
@@ -72,8 +73,9 @@ class SettingsActivity : AppCompatActivity() {

class SettingsFragment : PreferenceFragmentCompat(), AppListDialogFragment.AppExclusionListener {
@Inject lateinit var prefs: ApplicationPreferences
@Inject lateinit var tunnelManager: TunnelManager
@Inject lateinit var asyncWorker: AsyncWorker
@Inject lateinit var backendAsync: BackendAsync
@Inject lateinit var tunnelManager: TunnelManager

override fun onAttach(context: Context) {
injector.inject(this)
@@ -224,7 +226,7 @@ class SettingsActivity : AppCompatActivity() {
val ctx = requireContext()
val snackbarView = requireNotNull(requireActivity().findViewById<View>(android.R.id.content))
tunnelManager.getTunnels().thenAccept { tunnels ->
ZipExporter.exportZip(ctx.contentResolver, fileUri, tunnels) { throwable ->
ZipExporter.exportZip(asyncWorker, ctx.contentResolver, fileUri, tunnels) { throwable ->
if (throwable != null) {
val error = ExceptionLoggers.unwrapMessage(throwable)
val message = ctx.getString(R.string.zip_export_error, error)
@@ -39,6 +39,7 @@ import com.wireguard.android.util.AsyncWorker
import com.wireguard.android.util.BackendAsync
import com.wireguard.android.util.RootShell
import com.wireguard.android.util.ToolsInstaller
import com.wireguard.android.work.TunnelRestoreWorker
import dagger.BindsInstance
import dagger.Component
import dagger.Module
@@ -96,6 +97,9 @@ interface AppComponent {
// Services
fun inject(service: QuickTileService)
fun inject(service: GoBackend.VpnService)

// And some other thingies
fun inject(worker: TunnelRestoreWorker)
}

@Module
@@ -16,7 +16,7 @@ import com.wireguard.android.backend.WgQuickBackend
import com.wireguard.android.di.getInjector
import com.wireguard.android.model.TunnelManager
import com.wireguard.android.util.BackendAsync
import com.wireguard.android.work.TunnelRestoreWork
import com.wireguard.android.work.TunnelRestoreWorker
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import timber.log.Timber
@@ -36,7 +36,7 @@ class BootShutdownReceiver : BroadcastReceiver() {
val action = intent.action
if (Intent.ACTION_BOOT_COMPLETED == action) {
Timber.i("Broadcast receiver attempting to restore state (boot)")
val restoreWork = OneTimeWorkRequestBuilder<TunnelRestoreWork>()
val restoreWork = OneTimeWorkRequestBuilder<TunnelRestoreWorker>()
.setBackoffCriteria(
BackoffPolicy.LINEAR,
OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
@@ -7,18 +7,17 @@ package com.wireguard.android.util

import android.content.ContentResolver
import android.net.Uri
import com.wireguard.android.di.ext.getAsyncWorker
import com.wireguard.android.model.Tunnel
import com.wireguard.config.Config
import java.io.FileOutputStream
import java.nio.charset.StandardCharsets
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import java9.util.concurrent.CompletableFuture
import org.koin.core.KoinComponent

object ZipExporter : KoinComponent {
object ZipExporter {
fun exportZip(
asyncWorker: AsyncWorker,
contentResolver: ContentResolver,
fileUri: Uri,
tunnels: List<Tunnel>,
@@ -32,7 +31,7 @@ object ZipExporter : KoinComponent {
}
CompletableFuture.allOf(*futureConfigs.toTypedArray())
.whenComplete { _, exception ->
getAsyncWorker().runAsync {
asyncWorker.runAsync {
if (exception != null) {
throw exception
}
@@ -8,22 +8,23 @@ package com.wireguard.android.work
import android.content.Context
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.wireguard.android.di.ext.injectRootShell
import com.wireguard.android.di.ext.injectTunnelManager
import com.wireguard.android.di.getInjector
import com.wireguard.android.model.TunnelManager
import com.wireguard.android.util.ExceptionLoggers
import com.wireguard.android.util.RootShell
import javax.inject.Inject
import org.koin.core.KoinComponent
import timber.log.Timber

class TunnelRestoreWork(
class TunnelRestoreWorker(
appContext: Context,
workerParams: WorkerParameters
) : Worker(appContext, workerParams), KoinComponent {
private val tunnelManager: TunnelManager by injectTunnelManager()
private val rootShell: RootShell by injectRootShell()
@Inject lateinit var tunnelManager: TunnelManager
@Inject lateinit var rootShell: RootShell

override fun doWork(): Result {
getInjector(applicationContext).inject(this)
val result = ArrayList<String>()
rootShell.run(result, "iptables -L | grep Chain")
rootShell.run(result, "ip6tables -L | grep Chain")

0 comments on commit e23f878

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