Check permissions twice before updating the device status

This commit is contained in:
Jonas Lochmann 2021-05-10 02:00:00 +02:00
parent f315d2640a
commit 70f9a3df24
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36

View file

@ -828,7 +828,7 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
fun syncDeviceStatusAsync() {
runAsync {
syncDeviceStatus()
syncDeviceStatusFast()
}
}
@ -836,7 +836,7 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
while (true) {
appLogic.deviceEntryIfEnabled.waitUntilValueMatches { it != null }
syncDeviceStatus()
syncDeviceStatusSlow()
appLogic.timeApi.sleep(CHECK_PERMISSION_INTERVAL)
}
@ -860,10 +860,11 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
}
}
private suspend fun syncDeviceStatus() {
syncDeviceStatusLock.withLock {
private suspend fun getUpdateDeviceStatusAction(): UpdateDeviceStatusAction {
val deviceEntry = appLogic.deviceEntry.waitForNullableValue()
var changes = UpdateDeviceStatusAction.empty
if (deviceEntry != null) {
val protectionLevel = appLogic.platformIntegration.getCurrentProtectionLevel()
val usageStatsPermission = appLogic.platformIntegration.getForegroundAppPermissionStatus()
@ -872,8 +873,6 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
val accessibilityService = appLogic.platformIntegration.isAccessibilityServiceEnabled()
val qOrLater = AndroidVersion.qOrLater
var changes = UpdateDeviceStatusAction.empty
if (protectionLevel != deviceEntry.currentProtectionLevel) {
changes = changes.copy(
newProtectionLevel = protectionLevel
@ -911,6 +910,14 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
if (qOrLater && !deviceEntry.qOrLater) {
changes = changes.copy(isQOrLaterNow = true)
}
}
return changes
}
private suspend fun syncDeviceStatusFast() {
syncDeviceStatusLock.withLock {
val changes = getUpdateDeviceStatusAction()
if (changes != UpdateDeviceStatusAction.empty) {
ApplyActionUtil.applyAppLogicAction(
@ -921,6 +928,26 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
}
}
}
private suspend fun syncDeviceStatusSlow() {
syncDeviceStatusLock.withLock {
val changesOne = getUpdateDeviceStatusAction()
delay(2000)
val changesTwo = getUpdateDeviceStatusAction()
if (
changesOne != UpdateDeviceStatusAction.empty &&
changesOne == changesTwo
) {
ApplyActionUtil.applyAppLogicAction(
action = changesOne,
appLogic = appLogic,
ignoreIfDeviceIsNotConfigured = true
)
}
}
}
suspend fun resetTemporarilyAllowedApps() {