Commit e67d0678 authored by topjohnwu's avatar topjohnwu

Use viewModelScope instead of GlobalScope

parent b1faa5ee
...@@ -7,11 +7,8 @@ import com.topjohnwu.magisk.core.Config ...@@ -7,11 +7,8 @@ import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.magiskdb.PolicyDao import com.topjohnwu.magisk.core.magiskdb.PolicyDao
import com.topjohnwu.magisk.core.model.su.SuPolicy import com.topjohnwu.magisk.core.model.su.SuPolicy
import com.topjohnwu.magisk.core.model.su.toPolicy import com.topjohnwu.magisk.core.model.su.toPolicy
import com.topjohnwu.magisk.ktx.now
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.io.Closeable import java.io.Closeable
...@@ -54,10 +51,9 @@ class SuRequestHandler( ...@@ -54,10 +51,9 @@ class SuRequestHandler(
return true return true
} }
@Throws(IOException::class)
override fun close() { override fun close() {
if (::output.isInitialized) if (::output.isInitialized)
output.close() runCatching { output.close() }
} }
private class SuRequestError : IOException() private class SuRequestError : IOException()
...@@ -73,7 +69,7 @@ class SuRequestHandler( ...@@ -73,7 +69,7 @@ class SuRequestHandler(
when (e) { when (e) {
is IOException, is PackageManager.NameNotFoundException -> { is IOException, is PackageManager.NameNotFoundException -> {
Timber.e(e) Timber.e(e)
runCatching { close() } close()
false false
} }
else -> throw e // Unexpected error else -> throw e // Unexpected error
...@@ -81,23 +77,24 @@ class SuRequestHandler( ...@@ -81,23 +77,24 @@ class SuRequestHandler(
} }
} }
fun respond(action: Int, time: Int) { suspend fun respond(action: Int, time: Int) {
val until = if (time > 0) 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 else
time.toLong() time.toLong()
policy.policy = action policy.policy = action
policy.until = until policy.until = until
GlobalScope.launch(Dispatchers.IO) { withContext(Dispatchers.IO) {
try { try {
output.writeInt(policy.policy) output.writeInt(policy.policy)
output.flush() output.flush()
} catch (e: IOException) { } catch (e: IOException) {
Timber.e(e) Timber.e(e)
} finally { } finally {
runCatching { close() } close()
if (until >= 0) if (until >= 0)
policyDB.update(policy) policyDB.update(policy)
} }
......
...@@ -125,11 +125,13 @@ class SuRequestViewModel( ...@@ -125,11 +125,13 @@ class SuRequestViewModel(
val pos = selectedItemPosition val pos = selectedItemPosition
timeoutPrefs.edit().putInt(handler.policy.packageName, pos).apply() timeoutPrefs.edit().putInt(handler.policy.packageName, pos).apply()
handler.respond(action, Config.Value.TIMEOUT_LIST[pos])
viewModelScope.launch {
handler.respond(action, Config.Value.TIMEOUT_LIST[pos])
// Kill activity after response // Kill activity after response
DieEvent().publish() DieEvent().publish()
} }
}
private fun cancelTimer() { private fun cancelTimer() {
timer?.cancel() timer?.cancel()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment