Monday, 29 October 2018

How can I allow Android to Doze while my app is open and the screen is on?

I am making an app that will run when the device is locked, via Activity#setShowWhenLocked(true). I do not want to prevent the device from entering a low-power state. I know that the system does this with Always-On Display, and the display has an associated power mode for that:

/frameworks/base/core/java/android/view/Display.java

286    /**
287     * Display state: The display is dozing in a low power state; it is still
288     * on but is optimized for showing system-provided content while the
289     * device is non-interactive.
290     *
291     * @see #getState
292     * @see android.os.PowerManager#isInteractive
293     */
294    public static final int STATE_DOZE = ViewProtoEnums.DISPLAY_STATE_DOZE; // 3
295
296    /**
297     * Display state: The display is dozing in a suspended low power state; it is still
298     * on but the CPU is not updating it. This may be used in one of two ways: to show
299     * static system-provided content while the device is non-interactive, or to allow
300     * a "Sidekick" compute resource to update the display. For this reason, the
301     * CPU must not control the display in this mode.
302     *
303     * @see #getState
304     * @see android.os.PowerManager#isInteractive
305     */
306    public static final int STATE_DOZE_SUSPEND = ViewProtoEnums.DISPLAY_STATE_DOZE_SUSPEND; // 4

There is also this section in /frameworks/base/core/java/android/os/PowerManagerInternal.java:

51     * Wakefulness: The device is dozing.  It is almost asleep but is allowing a special
52     * low-power "doze" dream to run which keeps the display on but lets the application
53     * processor be suspended.  It can be awoken by a call to wakeUp() which ends the dream.
54     * The device fully goes to sleep if the dream cannot be started or ends on its own.
55     */
56    public static final int WAKEFULNESS_DOZING = 3;

If possible, I would like to avoid using root to do this, but if all else fails I could manually control doze with root using these commands, but that sounds messy and I don't want to mess up any other app's dozing interactions.

Also, I'm only particularly concerned with Android Pie (9.0)

Update:

I have also tried acquiring a DOZE_WAKE_LOCK, but it requires the system permission DEVICE_POWER. I tried to adb shell pm grant the permission to my app, but it is not a changeable permission type.



from How can I allow Android to Doze while my app is open and the screen is on?

No comments:

Post a Comment