From 803982a271c311231a05c08c96f498fe6e4246c0 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 24 Feb 2021 01:10:49 -0800 Subject: [PATCH] Prevent multiple installation sessions running in parallel --- .../magisk/core/tasks/MagiskInstaller.kt | 15 ++++++++++++++- .../topjohnwu/magisk/ui/flash/FlashFragment.kt | 1 + .../topjohnwu/magisk/ui/flash/FlashViewModel.kt | 8 ++------ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt b/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt index 1c1aca137..77151dd88 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt @@ -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 = NOPList.getInstance(), private val logs: MutableList = 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( diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashFragment.kt b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashFragment.kt index 8b4e22d72..331b0045d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashFragment.kt @@ -50,6 +50,7 @@ class FlashFragment : BaseUIFragment() defaultOrientation = activity.requestedOrientation activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR + viewModel.startFlashing() } @SuppressLint("WrongConstant") diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt index 796a8f506..da86e6dc3 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt @@ -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 -> {