diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Dialog.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Dialog.kt index 3f90d643b..acddfd767 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Dialog.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Dialog.kt @@ -34,7 +34,6 @@ import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.MutableState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -57,7 +56,6 @@ import com.topjohnwu.magisk.core.di.ServiceLocator import kotlinx.coroutines.CancellableContinuation import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.async import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.consumeAsFlow import kotlinx.coroutines.flow.onEach @@ -117,10 +115,6 @@ private class ConfirmDialogState( } } -interface LoadingDialogHandle { - suspend fun withLoading(block: suspend () -> R): R -} - interface ConfirmDialogHandle { fun showConfirm( title: String, @@ -139,22 +133,6 @@ interface ConfirmDialogHandle { ): ConfirmResult } -private class LoadingDialogHandleImpl( - private val visible: MutableState, - private val coroutineScope: CoroutineScope -) : LoadingDialogHandle { - override suspend fun withLoading(block: suspend () -> R): R { - return coroutineScope.async { - try { - visible.value = true - block() - } finally { - visible.value = false - } - }.await() - } -} - private class ConfirmDialogHandleImpl( private val state: ConfirmDialogState, private val coroutineScope: CoroutineScope, @@ -238,14 +216,6 @@ fun rememberConfirmCallback( } } -@Composable -fun rememberLoadingDialog(): LoadingDialogHandle { - val visible = remember { mutableStateOf(false) } - val scope = rememberCoroutineScope() - LoadingDialog(visible = visible.value) - return remember { LoadingDialogHandleImpl(visible, scope) } -} - @Composable fun rememberConfirmDialog( onConfirm: (() -> Unit)? = null, diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeScreen.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeScreen.kt index 59cbaa4d1..9ff349fdc 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeScreen.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeScreen.kt @@ -2,7 +2,6 @@ package com.topjohnwu.magisk.ui.home import android.os.Build import android.os.PowerManager -import android.widget.Toast import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -57,7 +56,6 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember -import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment @@ -81,23 +79,17 @@ import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.download.DownloadEngine import com.topjohnwu.magisk.core.download.Subject import com.topjohnwu.magisk.core.ktx.reboot -import com.topjohnwu.magisk.core.ktx.toast -import com.topjohnwu.magisk.core.tasks.MagiskInstaller import com.topjohnwu.magisk.ui.MainActivity import com.topjohnwu.magisk.ui.component.AdaptiveColumns +import com.topjohnwu.magisk.ui.component.LoadingDialog import com.topjohnwu.magisk.ui.component.MagiskDialog import com.topjohnwu.magisk.ui.component.MarkdownTextAsync import com.topjohnwu.magisk.ui.component.onlyHorizontal -import com.topjohnwu.magisk.ui.component.rememberLoadingDialog import com.topjohnwu.magisk.ui.component.verticalScrollbar import com.topjohnwu.magisk.ui.install.InstallDialog import com.topjohnwu.magisk.ui.install.InstallViewModel import com.topjohnwu.magisk.ui.navigation.LocalNavigator import com.topjohnwu.magisk.ui.navigation.Route -import kotlinx.coroutines.NonCancellable -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import java.io.File import com.topjohnwu.magisk.core.R as CoreR @@ -112,8 +104,6 @@ fun HomeScreen( val context = LocalContext.current val navigator = LocalNavigator.current val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior() - val scope = rememberCoroutineScope() - val loadingDialog = rememberLoadingDialog() var showUninstallDialog by rememberSaveable { mutableStateOf(false) } var showManagerDialog by rememberSaveable { mutableStateOf(false) } @@ -141,6 +131,8 @@ fun HomeScreen( } } + LoadingDialog(visible = uiState.showLoading) + if (showUninstallDialog) { UninstallComposableDialog( onDismiss = { showUninstallDialog = false }, @@ -151,15 +143,7 @@ fun HomeScreen( }, onRestoreImage = { showUninstallDialog = false - scope.launch { - val success = loadingDialog.withLoading { - MagiskInstaller.Restore().exec() - } - context.toast( - if (success) CoreR.string.restore_done else CoreR.string.restore_fail, - Toast.LENGTH_SHORT - ) - } + viewModel.restoreImages() } ) } @@ -187,22 +171,7 @@ fun HomeScreen( }, onFixEnv = { showEnvFixDialog = false - scope.launch { - val success = loadingDialog.withLoading { - MagiskInstaller.FixEnv().exec() - } - context.toast( - if (success) CoreR.string.reboot_delay_toast else CoreR.string.setup_fail, - Toast.LENGTH_LONG - ) - if (success) { - // The reboot toast has been shown; don't let leaving this screen cancel it - withContext(NonCancellable) { - delay(5000) - reboot() - } - } - } + viewModel.fixEnv() } ) } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt index 25ee26a46..4300cfc98 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt @@ -11,15 +11,20 @@ import com.topjohnwu.magisk.core.BuildConfig import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.ktx.await +import com.topjohnwu.magisk.core.ktx.reboot import com.topjohnwu.magisk.core.ktx.toast import com.topjohnwu.magisk.core.repository.NetworkService +import com.topjohnwu.magisk.core.tasks.MagiskInstaller import com.topjohnwu.magisk.utils.asFlow import com.topjohnwu.superuser.Shell +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import com.topjohnwu.magisk.core.R as CoreR class HomeViewModel( @@ -38,6 +43,7 @@ class HomeViewModel( val showUninstall: Boolean = false, val showManagerInstall: Boolean = false, val envFixCode: Int = 0, + val showLoading: Boolean = false, val magiskState: State = computeMagiskState(), val magiskInstalledVersion: String = computeMagiskInstalledVersion(), val managerInstalledVersion: String = computeManagerInstalledVersion(), @@ -137,6 +143,42 @@ class HomeViewModel( _uiState.update { it.copy(envFixCode = 0) } } + // Root operations run in the ViewModel, so a configuration change can't cancel them halfway + // and lose their result + + fun restoreImages() { + viewModelScope.launch { + val success = withLoading { MagiskInstaller.Restore().exec() } + showToast(if (success) CoreR.string.restore_done else CoreR.string.restore_fail) + } + } + + fun fixEnv() { + viewModelScope.launch { + val success = withLoading { MagiskInstaller.FixEnv().exec() } + AppContext.toast( + if (success) CoreR.string.reboot_delay_toast else CoreR.string.setup_fail, + Toast.LENGTH_LONG + ) + if (success) { + // The reboot toast has been shown; don't let leaving the app cancel it + withContext(NonCancellable) { + delay(5000) + reboot() + } + } + } + } + + private suspend fun withLoading(block: suspend () -> R): R { + _uiState.update { it.copy(showLoading = true) } + try { + return block() + } finally { + _uiState.update { it.copy(showLoading = false) } + } + } + fun hideNotice() { Config.safetyNotice = false _uiState.update { it.copy(isNoticeVisible = false) } diff --git a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt index 3087fe835..9c1284d80 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt @@ -506,8 +506,13 @@ abstract class MagiskInstallImpl protected constructor( if (haveActiveSession.getAndSet(true)) return false - val result = withContext(Dispatchers.IO) { operations() } - haveActiveSession.set(false) + // Always release the session: if the caller is cancelled, withContext throws once the + // operations finish, and a stuck session would make every later operation fail + val result = try { + withContext(Dispatchers.IO) { operations() } + } finally { + haveActiveSession.set(false) + } if (result) return true