Prevent multiple installation sessions running in parallel

This commit is contained in:
topjohnwu 2021-02-24 01:10:49 -08:00 committed by John Wu
parent 9164bf22c2
commit 803982a271
3 changed files with 17 additions and 7 deletions

View File

@ -43,6 +43,8 @@ import java.security.SecureRandom
import java.util.*
import java.util.zip.ZipFile
private var haveActiveSession: Boolean = false
abstract class MagiskInstallImpl protected constructor(
protected val console: MutableList<String> = NOPList.getInstance(),
private val logs: MutableList<String> = NOPList.getInstance()
@ -415,7 +417,18 @@ abstract class MagiskInstallImpl protected constructor(
@WorkerThread
protected abstract suspend fun operations(): Boolean
open suspend fun exec() = withContext(Dispatchers.IO) { operations() }
open suspend fun exec(): Boolean {
synchronized(haveActiveSession) {
if (haveActiveSession)
return false
haveActiveSession = true
}
val result = withContext(Dispatchers.IO) { operations() }
synchronized(haveActiveSession) {
haveActiveSession = false
}
return result
}
}
abstract class MagiskInstaller(

View File

@ -50,6 +50,7 @@ class FlashFragment : BaseUIFragment<FlashViewModel, FragmentFlashMd2Binding>()
defaultOrientation = activity.requestedOrientation
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
viewModel.startFlashing()
}
@SuppressLint("WrongConstant")

View File

@ -1,6 +1,5 @@
package com.topjohnwu.magisk.ui.flash
import android.net.Uri
import android.view.MenuItem
import androidx.databinding.Bindable
import androidx.lifecycle.LiveData
@ -27,7 +26,7 @@ import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class FlashViewModel(args: FlashFragmentArgs) : BaseViewModel() {
class FlashViewModel(private val args: FlashFragmentArgs) : BaseViewModel() {
@get:Bindable
var showReboot = Shell.rootAccess()
@ -49,14 +48,11 @@ class FlashViewModel(args: FlashFragmentArgs) : BaseViewModel() {
}
}
init {
fun startFlashing() {
val (action, uri, id) = args
if (id != -1)
Notifications.mgr.cancel(id)
startFlashing(action, uri)
}
private fun startFlashing(action: String, uri: Uri?) {
viewModelScope.launch {
val result = when (action) {
Const.Value.FLASH_ZIP -> {