@@ -34,20 +34,41 @@ class BatteryPolicyManager(private val executor: CommandExecutor) {
3434
3535 fun restrictBackground (packageName : String ): Result <Unit > {
3636 validatePackageName(packageName).onFailure { return Result .failure(it) }
37+
38+ // Comprehensive background restriction
3739 val commands = listOf (
40+ // Core background restrictions
3841 " appops set $packageName RUN_IN_BACKGROUND ignore" ,
3942 " appops set $packageName RUN_ANY_IN_BACKGROUND ignore" ,
40- " appops set $packageName WAKE_LOCK ignore"
43+
44+ // Wake lock & alarms
45+ " appops set $packageName WAKE_LOCK ignore" ,
46+ " appops set $packageName SCHEDULE_EXACT_ALARM ignore" ,
47+
48+ // Boot & startup
49+ " appops set $packageName BOOT_COMPLETED ignore" ,
50+
51+ // Also use cmd appops for newer Android
52+ " cmd appops set $packageName RUN_IN_BACKGROUND ignore" ,
53+ " cmd appops set $packageName RUN_ANY_IN_BACKGROUND ignore" ,
54+
55+ // Force stop after restricting to kill current processes
56+ " am force-stop $packageName "
4157 )
4258 return executor.executeBatch(commands)
4359 }
4460
4561 fun allowBackground (packageName : String ): Result <Unit > {
4662 validatePackageName(packageName).onFailure { return Result .failure(it) }
63+
4764 val commands = listOf (
4865 " appops set $packageName RUN_IN_BACKGROUND allow" ,
4966 " appops set $packageName RUN_ANY_IN_BACKGROUND allow" ,
50- " appops set $packageName WAKE_LOCK allow"
67+ " appops set $packageName WAKE_LOCK allow" ,
68+ " appops set $packageName SCHEDULE_EXACT_ALARM allow" ,
69+ " appops set $packageName BOOT_COMPLETED allow" ,
70+ " cmd appops set $packageName RUN_IN_BACKGROUND allow" ,
71+ " cmd appops set $packageName RUN_ANY_IN_BACKGROUND allow"
5172 )
5273 return executor.executeBatch(commands)
5374 }
0 commit comments