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() { fun syncDeviceStatusAsync() {
runAsync { runAsync {
syncDeviceStatus() syncDeviceStatusFast()
} }
} }
@ -836,7 +836,7 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
while (true) { while (true) {
appLogic.deviceEntryIfEnabled.waitUntilValueMatches { it != null } appLogic.deviceEntryIfEnabled.waitUntilValueMatches { it != null }
syncDeviceStatus() syncDeviceStatusSlow()
appLogic.timeApi.sleep(CHECK_PERMISSION_INTERVAL) appLogic.timeApi.sleep(CHECK_PERMISSION_INTERVAL)
} }
@ -860,65 +860,92 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
} }
} }
private suspend fun syncDeviceStatus() { 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()
val notificationAccess = appLogic.platformIntegration.getNotificationAccessPermissionStatus()
val overlayPermission = appLogic.platformIntegration.getOverlayPermissionStatus()
val accessibilityService = appLogic.platformIntegration.isAccessibilityServiceEnabled()
val qOrLater = AndroidVersion.qOrLater
if (protectionLevel != deviceEntry.currentProtectionLevel) {
changes = changes.copy(
newProtectionLevel = protectionLevel
)
if (protectionLevel == ProtectionLevel.DeviceOwner) {
appLogic.platformIntegration.setEnableSystemLockdown(true)
}
}
if (usageStatsPermission != deviceEntry.currentUsageStatsPermission) {
changes = changes.copy(
newUsageStatsPermissionStatus = usageStatsPermission
)
}
if (notificationAccess != deviceEntry.currentNotificationAccessPermission) {
changes = changes.copy(
newNotificationAccessPermission = notificationAccess
)
}
if (overlayPermission != deviceEntry.currentOverlayPermission) {
changes = changes.copy(
newOverlayPermission = overlayPermission
)
}
if (accessibilityService != deviceEntry.accessibilityServiceEnabled) {
changes = changes.copy(
newAccessibilityServiceEnabled = accessibilityService
)
}
if (qOrLater && !deviceEntry.qOrLater) {
changes = changes.copy(isQOrLaterNow = true)
}
}
return changes
}
private suspend fun syncDeviceStatusFast() {
syncDeviceStatusLock.withLock { syncDeviceStatusLock.withLock {
val deviceEntry = appLogic.deviceEntry.waitForNullableValue() val changes = getUpdateDeviceStatusAction()
if (deviceEntry != null) { if (changes != UpdateDeviceStatusAction.empty) {
val protectionLevel = appLogic.platformIntegration.getCurrentProtectionLevel() ApplyActionUtil.applyAppLogicAction(
val usageStatsPermission = appLogic.platformIntegration.getForegroundAppPermissionStatus() action = changes,
val notificationAccess = appLogic.platformIntegration.getNotificationAccessPermissionStatus() appLogic = appLogic,
val overlayPermission = appLogic.platformIntegration.getOverlayPermissionStatus() ignoreIfDeviceIsNotConfigured = true
val accessibilityService = appLogic.platformIntegration.isAccessibilityServiceEnabled() )
val qOrLater = AndroidVersion.qOrLater }
}
}
var changes = UpdateDeviceStatusAction.empty private suspend fun syncDeviceStatusSlow() {
syncDeviceStatusLock.withLock {
val changesOne = getUpdateDeviceStatusAction()
if (protectionLevel != deviceEntry.currentProtectionLevel) { delay(2000)
changes = changes.copy(
newProtectionLevel = protectionLevel
)
if (protectionLevel == ProtectionLevel.DeviceOwner) { val changesTwo = getUpdateDeviceStatusAction()
appLogic.platformIntegration.setEnableSystemLockdown(true)
}
}
if (usageStatsPermission != deviceEntry.currentUsageStatsPermission) { if (
changes = changes.copy( changesOne != UpdateDeviceStatusAction.empty &&
newUsageStatsPermissionStatus = usageStatsPermission changesOne == changesTwo
) ) {
} ApplyActionUtil.applyAppLogicAction(
action = changesOne,
if (notificationAccess != deviceEntry.currentNotificationAccessPermission) { appLogic = appLogic,
changes = changes.copy( ignoreIfDeviceIsNotConfigured = true
newNotificationAccessPermission = notificationAccess )
)
}
if (overlayPermission != deviceEntry.currentOverlayPermission) {
changes = changes.copy(
newOverlayPermission = overlayPermission
)
}
if (accessibilityService != deviceEntry.accessibilityServiceEnabled) {
changes = changes.copy(
newAccessibilityServiceEnabled = accessibilityService
)
}
if (qOrLater && !deviceEntry.qOrLater) {
changes = changes.copy(isQOrLaterNow = true)
}
if (changes != UpdateDeviceStatusAction.empty) {
ApplyActionUtil.applyAppLogicAction(
action = changes,
appLogic = appLogic,
ignoreIfDeviceIsNotConfigured = true
)
}
} }
} }
} }