aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-04-16 09:33:50 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-04-16 09:33:50 +0530
commitd4942550e77f72a265f28e57fc49ea8e8ee4be9c (patch)
tree794a468ff1275088a07f7c534afbe6290dc6ba8a
parentconfigStore: make all IO operations suspend (diff)
downloadwireguard-android-d4942550e77f72a265f28e57fc49ea8e8ee4be9c.tar.xz
wireguard-android-d4942550e77f72a265f28e57fc49ea8e8ee4be9c.zip
TunnelListFragment: Make coroutine APIs suspend
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
-rw-r--r--ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt4
-rw-r--r--ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt2
-rw-r--r--ui/src/main/java/com/wireguard/android/model/TunnelManager.kt52
3 files changed, 24 insertions, 34 deletions
diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt
index ccf0d584..7d5092b6 100644
--- a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt
+++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt
@@ -132,14 +132,14 @@ class TunnelListFragment : BaseFragment() {
null
}?.let {
deferredTunnels.add(async {
- Application.getTunnelManager().createAsync(name, it)
+ Application.getTunnelManager().create(name, it)
})
}
}
}
} else {
deferredTunnels.add(async {
- Application.getTunnelManager().createAsync(name, Config.parse(contentResolver.openInputStream(uri)!!))
+ Application.getTunnelManager().create(name, Config.parse(contentResolver.openInputStream(uri)!!))
})
}
}
diff --git a/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt b/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt
index 32e0fa9e..02227dab 100644
--- a/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt
+++ b/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt
@@ -82,7 +82,7 @@ class ObservableTunnel internal constructor(
suspend fun getConfigAsync(): Deferred<Config> {
return if (config == null)
- manager.getTunnelConfigAsync(this)
+ manager.getTunnelConfig(this)
else
CompletableDeferred<Config>().apply { complete(config!!) }
}
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 fa844e19..5d609f8c 100644
--- a/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt
+++ b/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt
@@ -28,7 +28,6 @@ import java9.util.concurrent.CompletableFuture
import java9.util.concurrent.CompletionStage
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
@@ -53,15 +52,7 @@ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() {
return tunnel
}
- fun create(name: String, config: Config?): CompletionStage<ObservableTunnel> {
- if (Tunnel.isNameInvalid(name))
- return CompletableFuture.failedFuture(IllegalArgumentException(context.getString(R.string.tunnel_error_invalid_name)))
- if (tunnelMap.containsKey(name))
- return CompletableFuture.failedFuture(IllegalArgumentException(context.getString(R.string.tunnel_error_already_exists, name)))
- return getAsyncWorker().supplyAsync { configStore.create(name, config!!) }.thenApply { addToList(name, it, Tunnel.State.DOWN) }
- }
-
- suspend fun createAsync(name: String, config: Config?): ObservableTunnel {
+ suspend fun create(name: String, config: Config?): ObservableTunnel {
if (Tunnel.isNameInvalid(name))
throw IllegalArgumentException(context.getString(R.string.tunnel_error_invalid_name))
if (tunnelMap.containsKey(name))
@@ -108,16 +99,12 @@ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() {
suspend fun getTunnelsAsync() = tunnelsAsync.await()
- fun getTunnelConfig(tunnel: ObservableTunnel): CompletionStage<Config> = getAsyncWorker()
- .supplyAsync { configStore.load(tunnel.name) }.thenApply(tunnel::onConfigChanged)
-
- suspend fun getTunnelConfigAsync(tunnel: ObservableTunnel): Deferred<Config> = withContext(Dispatchers.IO) {
- val deferred = CompletableDeferred<Config>()
- configStore.load(tunnel.name).also {
- tunnel.onConfigChanged(it)
- deferred.complete(it)
+ suspend fun getTunnelConfig(tunnel: ObservableTunnel): Config {
+ val config = withContext(Dispatchers.IO) {
+ configStore.load(tunnel.name)
}
- deferred
+ tunnel.onConfigChanged(config)
+ return config
}
suspend fun onCreate() = withContext(Dispatchers.IO) {
@@ -176,16 +163,18 @@ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() {
getSharedPreferences().edit().putStringSet(KEY_RUNNING_TUNNELS, tunnelMap.filter { it.state == Tunnel.State.UP }.map { it.name }.toSet()).commit()
}
- fun setTunnelConfig(tunnel: ObservableTunnel, config: Config): CompletionStage<Config> = getAsyncWorker().supplyAsync {
+ suspend fun setTunnelConfig(tunnel: ObservableTunnel, config: Config): Config {
getBackend().setState(tunnel, tunnel.state, config)
- configStore.save(tunnel.name, config)
- }.thenApply { tunnel.onConfigChanged(it) }
+ val conf = configStore.save(tunnel.name, config)
+ tunnel.onConfigChanged(conf)
+ return conf
+ }
- fun setTunnelName(tunnel: ObservableTunnel, name: String): CompletionStage<String> {
+ suspend fun setTunnelName(tunnel: ObservableTunnel, name: String): String {
if (Tunnel.isNameInvalid(name))
- return CompletableFuture.failedFuture(IllegalArgumentException(context.getString(R.string.tunnel_error_invalid_name)))
+ throw IllegalArgumentException(context.getString(R.string.tunnel_error_invalid_name))
if (tunnelMap.containsKey(name)) {
- return CompletableFuture.failedFuture(IllegalArgumentException(context.getString(R.string.tunnel_error_already_exists, name)))
+ throw IllegalArgumentException(context.getString(R.string.tunnel_error_already_exists, name))
}
val originalState = tunnel.state
val wasLastUsed = tunnel == lastUsedTunnel
@@ -193,23 +182,24 @@ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() {
if (wasLastUsed)
lastUsedTunnel = null
tunnelMap.remove(tunnel)
- return getAsyncWorker().supplyAsync {
+ val newName = try {
if (originalState == Tunnel.State.UP)
getBackend().setState(tunnel, Tunnel.State.DOWN, null)
configStore.rename(tunnel.name, name)
val newName = tunnel.onNameChanged(name)
if (originalState == Tunnel.State.UP)
getBackend().setState(tunnel, Tunnel.State.UP, tunnel.config)
- newName
- }.whenComplete { _, e ->
- // On failure, we don't know what state the tunnel might be in. Fix that.
- if (e != null)
- getTunnelState(tunnel)
// Add the tunnel back to the manager, under whatever name it thinks it has.
tunnelMap.add(tunnel)
if (wasLastUsed)
lastUsedTunnel = tunnel
+ newName
+ } catch (e: Exception) {
+ // On failure, we don't know what state the tunnel might be in. Fix that.
+ getTunnelState(tunnel)
+ null
}
+ return newName!!
}
fun setTunnelState(tunnel: ObservableTunnel, state: Tunnel.State): CompletionStage<Tunnel.State> = tunnel.configAsync