aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-03-31 03:50:33 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-04-10 00:09:20 +0530
commit0e3bc9fd287b2244ac53de545c97ea904a857e41 (patch)
tree12a8782fe779b3e12fb48dba4c084e96ca78aad3
parentui: animate fab position in tunnel deletion flow (diff)
downloadwireguard-android-0e3bc9fd287b2244ac53de545c97ea904a857e41.tar.xz
wireguard-android-0e3bc9fd287b2244ac53de545c97ea904a857e41.zip
ui: make TunnelManager init asynchronous
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
-rw-r--r--ui/src/main/java/com/wireguard/android/Application.kt13
-rw-r--r--ui/src/main/java/com/wireguard/android/configStore/ConfigStore.kt2
-rw-r--r--ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt2
-rw-r--r--ui/src/main/java/com/wireguard/android/model/TunnelManager.kt12
4 files changed, 20 insertions, 9 deletions
diff --git a/ui/src/main/java/com/wireguard/android/Application.kt b/ui/src/main/java/com/wireguard/android/Application.kt
index d533028f..a7bf3e75 100644
--- a/ui/src/main/java/com/wireguard/android/Application.kt
+++ b/ui/src/main/java/com/wireguard/android/Application.kt
@@ -28,10 +28,15 @@ import com.wireguard.android.util.ModuleLoader
import com.wireguard.android.util.RootShell
import com.wireguard.android.util.ToolsInstaller
import java9.util.concurrent.CompletableFuture
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.launch
import java.lang.ref.WeakReference
import java.util.Locale
+import kotlin.coroutines.CoroutineContext
-class Application : android.app.Application(), OnSharedPreferenceChangeListener {
+class Application : android.app.Application(), OnSharedPreferenceChangeListener, CoroutineScope {
private val futureBackend = CompletableFuture<Backend>()
private lateinit var asyncWorker: AsyncWorker
private var backend: Backend? = null
@@ -40,6 +45,9 @@ class Application : android.app.Application(), OnSharedPreferenceChangeListener
private lateinit var sharedPreferences: SharedPreferences
private lateinit var toolsInstaller: ToolsInstaller
private lateinit var tunnelManager: TunnelManager
+ private val job = Job()
+ override val coroutineContext: CoroutineContext
+ get() = job + Dispatchers.Default
override fun attachBaseContext(context: Context) {
super.attachBaseContext(context)
@@ -71,7 +79,7 @@ class Application : android.app.Application(), OnSharedPreferenceChangeListener
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
tunnelManager = TunnelManager(FileConfigStore(applicationContext))
- tunnelManager.onCreate()
+ launch { tunnelManager.onCreate() }
asyncWorker.supplyAsync(Companion::getBackend).thenAccept { futureBackend.complete(it) }
sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
@@ -82,6 +90,7 @@ class Application : android.app.Application(), OnSharedPreferenceChangeListener
}
override fun onTerminate() {
+ job.cancel()
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
super.onTerminate()
}
diff --git a/ui/src/main/java/com/wireguard/android/configStore/ConfigStore.kt b/ui/src/main/java/com/wireguard/android/configStore/ConfigStore.kt
index 6c381b69..69d2bba7 100644
--- a/ui/src/main/java/com/wireguard/android/configStore/ConfigStore.kt
+++ b/ui/src/main/java/com/wireguard/android/configStore/ConfigStore.kt
@@ -34,7 +34,7 @@ interface ConfigStore {
*
* @return The set of present tunnel names.
*/
- fun enumerate(): Set<String>
+ suspend fun enumerate(): Set<String>
/**
* Load the configuration for the tunnel given by `name`.
diff --git a/ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt b/ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt
index 6fe2bece..99dca71e 100644
--- a/ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt
+++ b/ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt
@@ -38,7 +38,7 @@ class FileConfigStore(private val context: Context) : ConfigStore {
throw IOException(context.getString(R.string.config_delete_error, file.name))
}
- override fun enumerate(): Set<String> {
+ override suspend fun enumerate(): Set<String> {
return context.fileList()
.filter { it.endsWith(".conf") }
.map { it.substring(0, it.length - ".conf".length) }
diff --git a/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt b/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt
index 5091ed3b..5d2afa76 100644
--- a/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt
+++ b/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt
@@ -26,6 +26,9 @@ import com.wireguard.android.util.ExceptionLoggers
import com.wireguard.config.Config
import java9.util.concurrent.CompletableFuture
import java9.util.concurrent.CompletionStage
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.async
+import kotlinx.coroutines.withContext
import java.util.ArrayList
/**
@@ -95,11 +98,10 @@ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() {
fun getTunnelConfig(tunnel: ObservableTunnel): CompletionStage<Config> = getAsyncWorker()
.supplyAsync { configStore.load(tunnel.name) }.thenApply(tunnel::onConfigChanged)
-
- fun onCreate() {
- getAsyncWorker().supplyAsync { configStore.enumerate() }
- .thenAcceptBoth(getAsyncWorker().supplyAsync { getBackend().runningTunnelNames }, this::onTunnelsLoaded)
- .whenComplete(ExceptionLoggers.E)
+ suspend fun onCreate() = withContext(Dispatchers.IO) {
+ val storedConfigs = async { configStore.enumerate() }
+ val runningTunnels = async { getBackend().runningTunnelNames }
+ onTunnelsLoaded(storedConfigs.await(), runningTunnels.await())
}
private fun onTunnelsLoaded(present: Iterable<String>, running: Collection<String>) {