tweaselORG / appstraction

An abstraction layer for common instrumentation functions (e.g. installing and starting apps, setting preferences, etc.) on Android and iOS.
MIT License
5 stars 1 forks source link

Add function to configure battery optimizations on Android #34

Closed baltpeter closed 1 year ago

baltpeter commented 1 year ago

Needed for automatically controlling WireGuard (cf. https://github.com/tweaselORG/meta/issues/19).

baltpeter commented 1 year ago

One way to do that is using dumpsys deviceidle whitelist (via):

baltpeter commented 1 year ago

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:

I'm not quite sure what these do yet, and especially what the difference between RUN_IN_BACKGROUND and RUN_ANY_IN_BACKGROUND is.

baltpeter commented 1 year ago

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):

Screenshot_20230306-114931_Settings

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":

Screenshot_20230306-114826_Settings

baltpeter commented 1 year ago

So, let's see what these options correspond to.

On Android 13:

On Android 9:

baltpeter commented 1 year ago

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?).

scruel commented 4 months ago

https://github.com/tweaselORG/appstraction/issues/34#issuecomment-1455938039 Won't work with MIUI/hyperOS