From e2f985900c3b07f13c768c803106bdd4d9b94772 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 24 Feb 2018 02:37:23 +0100 Subject: Unify unit future typing --- .../java/org/pacien/tincapp/activities/ConfigureActivity.kt | 2 +- .../main/java/org/pacien/tincapp/activities/StatusActivity.kt | 3 ++- app/src/main/java/org/pacien/tincapp/commands/Executor.kt | 6 +++--- app/src/main/java/org/pacien/tincapp/commands/TincApp.kt | 7 +++---- .../main/java/org/pacien/tincapp/service/TincVpnService.kt | 11 ++++++----- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt index a67887c..e571cf5 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt @@ -113,7 +113,7 @@ class ConfigureActivity : BaseActivity() { R.string.message_encrypting_decrypting_private_keys, TincApp.setPassphrase(netName, currentPassphrase, newPassphrase)) - private fun execAction(@StringRes label: Int, action: CompletableFuture) { + private fun execAction(@StringRes label: Int, action: CompletableFuture) { showProgressDialog(label).let { progressDialog -> action .whenComplete { _, _ -> progressDialog.dismiss() } diff --git a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt index 0726ee9..1872081 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt @@ -17,6 +17,7 @@ import kotlinx.android.synthetic.main.dialog_node_details.view.* import kotlinx.android.synthetic.main.fragment_list_view.* import kotlinx.android.synthetic.main.fragment_network_status_header.* import org.pacien.tincapp.R +import org.pacien.tincapp.commands.Executor import org.pacien.tincapp.commands.Tinc import org.pacien.tincapp.data.VpnInterfaceConfiguration import org.pacien.tincapp.extensions.Android.setElements @@ -163,7 +164,7 @@ class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRef TincVpnService.isConnected() -> Tinc.dumpNodes(TincVpnService.getCurrentNetName()!!).thenApply> { it.map { it.substringBefore(' ') } } else -> - CompletableFuture.supplyAsync> { emptyList() } + Executor.supplyAsyncTask> { emptyList() } } } } diff --git a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt index fedd0d2..39c413e 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt @@ -33,12 +33,12 @@ internal object Executor { private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() - fun forkExec(cmd: Command): CompletableFuture { + fun forkExec(cmd: Command): CompletableFuture { val pid = forkExec(cmd.asArray()).also { if (it == FAILED) throw CommandExecutionException("Could not fork child process.") } - return CompletableFuture.runAsync { + return runAsyncTask { when (wait(pid)) { SUCCESS -> Unit FAILED -> throw CommandExecutionException("Process terminated abnormally.") @@ -60,6 +60,6 @@ internal object Executor { } } - fun runAsyncTask(r: () -> Unit) = CompletableFuture.runAsync(Runnable(r), AsyncTask.THREAD_POOL_EXECUTOR)!! + fun runAsyncTask(r: () -> Unit) = CompletableFuture.supplyAsync(Supplier(r), AsyncTask.THREAD_POOL_EXECUTOR)!! fun supplyAsyncTask(s: () -> U) = CompletableFuture.supplyAsync(Supplier(s), AsyncTask.THREAD_POOL_EXECUTOR)!! } diff --git a/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt b/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt index e5172c8..3c2e27a 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/TincApp.kt @@ -1,6 +1,5 @@ package org.pacien.tincapp.commands -import java8.util.concurrent.CompletableFuture import org.pacien.tincapp.R import org.pacien.tincapp.commands.Executor.runAsyncTask import org.pacien.tincapp.context.App @@ -30,17 +29,17 @@ object TincApp { throw FileNotFoundException(App.getResources().getString(R.string.message_network_config_not_found_format, e.message!!)) } - fun removeScripts(netName: String): CompletableFuture = runAsyncTask { + fun removeScripts(netName: String) = runAsyncTask { listScripts(netName).forEach { it.delete() } } - fun generateIfaceCfg(netName: String): CompletableFuture = runAsyncTask { + fun generateIfaceCfg(netName: String) = runAsyncTask { VpnInterfaceConfiguration .fromInvitation(AppPaths.invitationFile(netName)) .write(AppPaths.netConfFile(netName)) } - fun setPassphrase(netName: String, currentPassphrase: String? = null, newPassphrase: String?): CompletableFuture = runAsyncTask { + fun setPassphrase(netName: String, currentPassphrase: String? = null, newPassphrase: String?) = runAsyncTask { listPrivateKeys(netName) .filter { it.exists() } .map { Pair(PemUtils.read(it), it) } diff --git a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt index 223763d..b59be80 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt @@ -11,6 +11,7 @@ import org.apache.commons.configuration2.ex.ConversionException import org.bouncycastle.openssl.PEMException import org.pacien.tincapp.BuildConfig import org.pacien.tincapp.R +import org.pacien.tincapp.commands.Executor import org.pacien.tincapp.commands.Tinc import org.pacien.tincapp.commands.Tincd import org.pacien.tincapp.context.App @@ -136,9 +137,9 @@ class TincVpnService : VpnService() { } private fun waitForDaemonStartup() = - CompletableFuture - .runAsync { Thread.sleep(SETUP_DELAY) } - .thenCompose { if (daemon!!.isDone) daemon!! else CompletableFuture.runAsync { } } + Executor + .runAsyncTask { Thread.sleep(SETUP_DELAY) } + .thenCompose { if (daemon!!.isDone) daemon!! else Executor.runAsyncTask { Unit } } companion object { private const val SETUP_DELAY = 500L // ms @@ -146,10 +147,10 @@ class TincVpnService : VpnService() { private var netName: String? = null private var interfaceCfg: VpnInterfaceConfiguration? = null private var fd: ParcelFileDescriptor? = null - private var daemon: CompletableFuture? = null + private var daemon: CompletableFuture? = null private fun setState(netName: String?, interfaceCfg: VpnInterfaceConfiguration?, - fd: ParcelFileDescriptor?, daemon: CompletableFuture?) { + fd: ParcelFileDescriptor?, daemon: CompletableFuture?) { TincVpnService.netName = netName TincVpnService.interfaceCfg = interfaceCfg -- cgit v1.2.3