mirror of
https://codeberg.org/timelimit/timelimit-android.git
synced 2025-10-03 09:49:25 +02:00
Check permissions twice before updating the device status
This commit is contained in:
parent
f315d2640a
commit
70f9a3df24
1 changed files with 82 additions and 55 deletions
|
@ -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
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue