aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.kt
diff options
context:
space:
mode:
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)
}
}