summaryrefslogtreecommitdiffhomepage
path: root/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2020-09-14 14:27:55 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2020-09-14 14:40:10 +0200
commit2fc0bb1a03f624e297d2afdeb95231cf906afc21 (patch)
treeb1f9ed3d59683b5bbe4975821988c0e9ce7f2e84 /ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt
parentdd0ff8fe60daea6ae3666b5fe655c37303b77626 (diff)
coroutines: convert low-hanging fruits
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt')
-rw-r--r--ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt46
1 files changed, 25 insertions, 21 deletions
diff --git a/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt b/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt
index 055ed449..adf0dc27 100644
--- a/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt
+++ b/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt
@@ -15,40 +15,44 @@ import com.wireguard.android.Application
import com.wireguard.android.R
import com.wireguard.android.activity.SettingsActivity
import com.wireguard.android.util.ErrorMessages
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
import kotlin.system.exitProcess
class ModuleDownloaderPreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs) {
private var state = State.INITIAL
+ private val coroutineScope = CoroutineScope(Dispatchers.Main)
override fun getSummary() = context.getString(state.messageResourceId)
override fun getTitle() = context.getString(R.string.module_installer_title)
+ @SuppressLint("ApplySharedPref")
override fun onClick() {
setState(State.WORKING)
- Application.getAsyncWorker().supplyAsync(Application.getModuleLoader()::download).whenComplete(this::onDownloadResult)
- }
-
- @SuppressLint("ApplySharedPref")
- private fun onDownloadResult(result: Int, throwable: Throwable?) {
- when {
- throwable != null -> {
- setState(State.FAILURE)
- Toast.makeText(context, ErrorMessages[throwable], Toast.LENGTH_LONG).show()
- }
- result == OsConstants.ENOENT -> setState(State.NOTFOUND)
- result == OsConstants.EXIT_SUCCESS -> {
- setState(State.SUCCESS)
- Application.getSharedPreferences().edit().remove("disable_kernel_module").commit()
- Application.getAsyncWorker().runAsync {
- val restartIntent = Intent(context, SettingsActivity::class.java)
- restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
- restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
- Application.get().startActivity(restartIntent)
- exitProcess(0)
+ coroutineScope.launch {
+ try {
+ when (withContext(Dispatchers.IO) { Application.getModuleLoader().download() }) {
+ OsConstants.ENOENT -> setState(State.NOTFOUND)
+ OsConstants.EXIT_SUCCESS -> {
+ setState(State.SUCCESS)
+ Application.getSharedPreferences().edit().remove("disable_kernel_module").commit()
+ CoroutineScope(Dispatchers.Default).launch {
+ val restartIntent = Intent(context, SettingsActivity::class.java)
+ restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
+ restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ Application.get().startActivity(restartIntent)
+ exitProcess(0)
+ }
+ }
+ else -> setState(State.FAILURE)
}
+ } catch (e: Exception) {
+ setState(State.FAILURE)
+ Toast.makeText(context, ErrorMessages[e], Toast.LENGTH_LONG).show()
}
- else -> setState(State.FAILURE)
}
}