Closed baltpeter closed 1 year ago
One way to do that is using dumpsys deviceidle whitelist
(via):
adb shell dumpsys deviceidle whitelist "+<app ID>"
adb shell dumpsys deviceidle whitelist "-<app ID>"
true
means it is on the allowlist, i.e. battery optimizations are disabled; false
means it is not on the allowlist, i.e. battery optimizations are enabled): adb shell dumpsys deviceidle whitelist "=<app ID>"
There are also the RUN_IN_BACKGROUND
and RUN_ANY_IN_BACKGROUND
permissions that can be set via appops
(via).
The state of that can be read using adb shell cmd appops get <app ID> <permission ID>
. And according to the source:
adb shell cmd appops set <app ID> RUN_IN_BACKGROUND ignore
adb shell cmd appops set <app ID> RUN_IN_BACKGROUND allow
I'm not quite sure what these do yet, and especially what the difference between RUN_IN_BACKGROUND
and RUN_ANY_IN_BACKGROUND
is.
Ah okay, "battery optimizations" is actually a tristate rather than a boolean.
On Android 9, this was split into two options. There was a "background restriction" option that could either be set to "restricted" or "app can use battery in background" (default), and a separate "background optimization" option that could be "not optimized" or "optimizing battery use" (default):
And if "background optimization" was set to "not optimizing battery use", the "background restriction" option was disabled.
On Android 13 (probably earlier than that, but that's what I tested with), these have been collapsed into a single "app battery usage" option that can be "unrestricted", "optimized", or "restricted":
So, let's see what these options correspond to.
On Android 13:
dumpsys deviceidle whitelist
: true
appops get RUN_IN_BACKGROUND
: No operations. Default mode: allow
appops get RUN_ANY_IN_BACKGROUND
: allow
dumpsys deviceidle whitelist
: false
appops get RUN_IN_BACKGROUND
: No operations. Default mode: allow
appops get RUN_ANY_IN_BACKGROUND
: allow
dumpsys deviceidle whitelist
: false
appops get RUN_IN_BACKGROUND
: No operations. Default mode: allow
appops get RUN_ANY_IN_BACKGROUND
: ignore
On Android 9:
dumpsys deviceidle whitelist
: true
appops get RUN_IN_BACKGROUND
: No operations. Default mode: allow
appops get RUN_ANY_IN_BACKGROUND
: allow
dumpsys deviceidle whitelist
: false
appops get RUN_IN_BACKGROUND
: No operations. Default mode: allow
appops get RUN_ANY_IN_BACKGROUND
: No operations. Default mode: allow
dumpsys deviceidle whitelist
: false
appops get RUN_IN_BACKGROUND
: No operations. Default mode: allow
appops get RUN_ANY_IN_BACKGROUND
: ignore
Where does that leave us? Luckily, despite the UI changes, it seems like the underlying mechanism hasn't changed and we won't need to do things differently for different Android versions.
RUN_IN_BACKGROUND
doesn't seem to be used (anymore?). According to https://appops.rikka.app/guide/technical/run_in_background/, RUN_IN_BACKGROUND
was introduced in Android 7, and RUN_ANY_IN_BACKGROUND
was introduced in Android 9 (presumably replacing the former?).
https://github.com/tweaselORG/appstraction/issues/34#issuecomment-1455938039 Won't work with MIUI/hyperOS
Needed for automatically controlling WireGuard (cf. https://github.com/tweaselORG/meta/issues/19).