From e67d0678f99412749c5a1569888f203b0c6db13e Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 23 Mar 2022 18:03:41 -0700 Subject: [PATCH] Use viewModelScope instead of GlobalScope --- .../magisk/core/su/SuRequestHandler.kt | 17 +++++++---------- .../magisk/ui/surequest/SuRequestViewModel.kt | 8 +++++--- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt b/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt index 4238f5323..6fbf15d8d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/su/SuRequestHandler.kt @@ -7,11 +7,8 @@ import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.magiskdb.PolicyDao import com.topjohnwu.magisk.core.model.su.SuPolicy import com.topjohnwu.magisk.core.model.su.toPolicy -import com.topjohnwu.magisk.ktx.now import com.topjohnwu.superuser.Shell import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import timber.log.Timber import java.io.Closeable @@ -54,10 +51,9 @@ class SuRequestHandler( return true } - @Throws(IOException::class) override fun close() { if (::output.isInitialized) - output.close() + runCatching { output.close() } } private class SuRequestError : IOException() @@ -73,7 +69,7 @@ class SuRequestHandler( when (e) { is IOException, is PackageManager.NameNotFoundException -> { Timber.e(e) - runCatching { close() } + close() false } else -> throw e // Unexpected error @@ -81,23 +77,24 @@ class SuRequestHandler( } } - fun respond(action: Int, time: Int) { + suspend fun respond(action: Int, time: Int) { val until = if (time > 0) - TimeUnit.MILLISECONDS.toSeconds(now) + TimeUnit.MINUTES.toSeconds(time.toLong()) + TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) + + TimeUnit.MINUTES.toSeconds(time.toLong()) else time.toLong() policy.policy = action policy.until = until - GlobalScope.launch(Dispatchers.IO) { + withContext(Dispatchers.IO) { try { output.writeInt(policy.policy) output.flush() } catch (e: IOException) { Timber.e(e) } finally { - runCatching { close() } + close() if (until >= 0) policyDB.update(policy) } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt index 7e9690819..7941526ec 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt @@ -125,10 +125,12 @@ class SuRequestViewModel( val pos = selectedItemPosition timeoutPrefs.edit().putInt(handler.policy.packageName, pos).apply() - handler.respond(action, Config.Value.TIMEOUT_LIST[pos]) - // Kill activity after response - DieEvent().publish() + viewModelScope.launch { + handler.respond(action, Config.Value.TIMEOUT_LIST[pos]) + // Kill activity after response + DieEvent().publish() + } } private fun cancelTimer() {