mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
app: serialize superuser grant writes
Each tap on the grant screen started its own database write on the IO dispatcher, with nothing ordering them. A grant takes two queries (fetch, then replace) while a revoke takes one, so a quick grant and revoke could run as fetch, delete, replace and leave the app granted while it was shown as not granted. The new state was also computed from the item the UI captured, so two taps before a recomposition both granted. Write in tap order under a Mutex, like SuperuserViewModel does, and toggle the current state of the app. Assisted-by: Claude Opus 5.5
This commit is contained in:
+23
-11
@@ -28,6 +28,8 @@ import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.toCollection
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
import java.util.Locale
|
||||
@@ -63,6 +65,8 @@ class SuperuserGrantViewModel(
|
||||
private val policyDB: PolicyDao
|
||||
) : AsyncLoadViewModel() {
|
||||
|
||||
private val writeLock = Mutex()
|
||||
|
||||
private val _loading = MutableStateFlow(true)
|
||||
val loading: StateFlow<Boolean> = _loading.asStateFlow()
|
||||
|
||||
@@ -133,10 +137,14 @@ class SuperuserGrantViewModel(
|
||||
fun toggleSortReverse() { _sortReverse.value = !_sortReverse.value }
|
||||
|
||||
fun toggleGrant(app: SuGrantAppState) {
|
||||
val willGrant = !app.isGranted
|
||||
// Toggle the current state rather than the one the UI captured: two quick taps can
|
||||
// deliver the same item before it recomposes
|
||||
val uid = app.info.uid
|
||||
val target = _allApps.value.firstOrNull { it.info.uid == uid } ?: return
|
||||
val willGrant = !target.isGranted
|
||||
_allApps.update { apps ->
|
||||
apps.map { current ->
|
||||
if (current.info.uid == app.info.uid) {
|
||||
if (current.info.uid == uid) {
|
||||
current.copy(isGranted = willGrant)
|
||||
} else {
|
||||
current
|
||||
@@ -145,14 +153,18 @@ class SuperuserGrantViewModel(
|
||||
}
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
withContext(Dispatchers.IO) {
|
||||
if (willGrant) {
|
||||
val existing = policyDB.fetch(app.info.uid)
|
||||
val suPolicy = existing ?: SuPolicy(app.info.uid)
|
||||
suPolicy.policy = if (Config.suRestrict) SuPolicy.RESTRICT else SuPolicy.ALLOW
|
||||
policyDB.update(suPolicy)
|
||||
} else {
|
||||
policyDB.delete(app.info.uid)
|
||||
// Write in tap order: a grant takes two queries and a revoke one, so unordered
|
||||
// writes could leave an app granted while it is shown as not granted
|
||||
writeLock.withLock {
|
||||
withContext(Dispatchers.IO) {
|
||||
if (willGrant) {
|
||||
val existing = policyDB.fetch(uid)
|
||||
val suPolicy = existing ?: SuPolicy(uid)
|
||||
suPolicy.policy = if (Config.suRestrict) SuPolicy.RESTRICT else SuPolicy.ALLOW
|
||||
policyDB.update(suPolicy)
|
||||
} else {
|
||||
policyDB.delete(uid)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
@@ -160,7 +172,7 @@ class SuperuserGrantViewModel(
|
||||
Timber.e(e)
|
||||
_allApps.update { apps ->
|
||||
apps.map { current ->
|
||||
if (current.info.uid == app.info.uid && current.isGranted == willGrant) {
|
||||
if (current.info.uid == uid && current.isGranted == willGrant) {
|
||||
current.copy(isGranted = !willGrant)
|
||||
} else {
|
||||
current
|
||||
|
||||
Reference in New Issue
Block a user