Fix coroutine stuck

This commit is contained in:
Nullptr 2022-10-21 19:57:24 +08:00
parent 5db7ecf7bd
commit 667ac6282f
No known key found for this signature in database
5 changed files with 26 additions and 24 deletions

View File

@ -5,7 +5,8 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.util.Log import android.util.Log
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.launch
import org.lsposed.lspatch.lspApp
import org.lsposed.lspatch.util.LSPPackageManager import org.lsposed.lspatch.util.LSPPackageManager
class AppBroadcastReceiver : BroadcastReceiver() { class AppBroadcastReceiver : BroadcastReceiver() {
@ -30,7 +31,7 @@ class AppBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
if (intent.action in actions) { if (intent.action in actions) {
runBlocking { lspApp.globalScope.launch {
Log.i(TAG, "Received intent: $intent") Log.i(TAG, "Received intent: $intent")
LSPPackageManager.fetchAppList() LSPPackageManager.fetchAppList()
} }

View File

@ -391,7 +391,7 @@ private fun InstallDialog(patchApp: AppInfo, onFinish: (Int, String?) -> Unit) {
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
var uninstallFirst by remember { mutableStateOf(ShizukuApi.isPackageInstalledWithoutPatch(patchApp.app.packageName)) } var uninstallFirst by remember { mutableStateOf(ShizukuApi.isPackageInstalledWithoutPatch(patchApp.app.packageName)) }
var installing by remember { mutableStateOf(0) } var installing by remember { mutableStateOf(0) }
val doInstall = suspend { suspend fun doInstall() {
Log.i(TAG, "Installing app ${patchApp.app.packageName}") Log.i(TAG, "Installing app ${patchApp.app.packageName}")
installing = 1 installing = 1
val (status, message) = LSPPackageManager.install() val (status, message) = LSPPackageManager.install()

View File

@ -229,8 +229,8 @@ fun AppManageBody(
} }
) )
val uninstallSuccessfully = stringResource(R.string.manage_uninstall_successfully) val uninstallSuccessfully = stringResource(R.string.manage_uninstall_successfully)
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (it.resultCode == Activity.RESULT_OK) { if (result.resultCode == Activity.RESULT_OK) {
scope.launch { scope.launch {
snackbarHost.showSnackbar(uninstallSuccessfully) snackbarHost.showSnackbar(uninstallSuccessfully)
} }

View File

@ -66,6 +66,7 @@ class NewPatchViewModel : ViewModel() {
} }
fun dispatch(action: ViewAction) { fun dispatch(action: ViewAction) {
viewModelScope.launch {
when (action) { when (action) {
is ViewAction.DoneInit -> doneInit() is ViewAction.DoneInit -> doneInit()
is ViewAction.ConfigurePatch -> configurePatch(action.app) is ViewAction.ConfigurePatch -> configurePatch(action.app)
@ -73,6 +74,7 @@ class NewPatchViewModel : ViewModel() {
is ViewAction.LaunchPatch -> launchPatch() is ViewAction.LaunchPatch -> launchPatch()
} }
} }
}
private fun doneInit() { private fun doneInit() {
patchState = PatchState.SELECTING patchState = PatchState.SELECTING
@ -95,9 +97,8 @@ class NewPatchViewModel : ViewModel() {
patchState = PatchState.PATCHING patchState = PatchState.PATCHING
} }
private fun launchPatch() { private suspend fun launchPatch() {
logger.i("Launch patch") logger.i("Launch patch")
viewModelScope.launch {
patchState = try { patchState = try {
Patcher.patch(logger, patchOptions) Patcher.patch(logger, patchOptions)
PatchState.FINISHED PatchState.FINISHED
@ -109,5 +110,4 @@ class NewPatchViewModel : ViewModel() {
LSPPackageManager.cleanTmpApkDir() LSPPackageManager.cleanTmpApkDir()
} }
} }
}
} }

View File

@ -11,6 +11,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.google.gson.Gson import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.lsposed.lspatch.Patcher import org.lsposed.lspatch.Patcher
import org.lsposed.lspatch.lspApp import org.lsposed.lspatch.lspApp
@ -69,8 +70,8 @@ class AppManageViewModel : ViewModel() {
} }
} }
suspend fun dispatch(action: ViewAction) { fun dispatch(action: ViewAction) {
withContext(viewModelScope.coroutineContext) { viewModelScope.launch {
when (action) { when (action) {
is ViewAction.UpdateLoader -> updateLoader(action.appInfo, action.config) is ViewAction.UpdateLoader -> updateLoader(action.appInfo, action.config)
is ViewAction.ClearUpdateLoaderResult -> updateLoaderState = ProcessingState.Idle is ViewAction.ClearUpdateLoaderResult -> updateLoaderState = ProcessingState.Idle