by Prasanth S
Version 1 (July 19, 2016)
Download (100 downloads)
Mimics network restrictions aspect of Marshmallow's Doze on Lollipop (and lower), by toggling Wi-Fi. Probably makes sense to use on Marshmallow too.
No root required.
Use only if cell data is managed manually, and predominantly kept off. If not, use the 'Data and Wi-Fi' version (root required for Android 5+).
Flow description:
1. If screen stays off for a certain timeout (default 5 min), enter doze phase.
2. Doze phase consists of cycles of
2.1. Network-sleeping (Wi-Fi off) for a certain doze period (default 9 min), and
2.2. Network-awake (Wi-Fi on) for a certain 'maintenance window' (default 1 min).
3. Doze phase is broken if the screen is turned back on.
Implementation notes:
1. If Wi-Fi is off to begin with, doze phase is redundant and isn't entered.
2. This flow is robust under changes to Wi-Fi state made prior to screen turning off (including, importantly, manual changes by the user).
3. Race conditions (when the user toggles screen on/off states too fast to be registered by 'when screen on') are handled properly (with 'if screen on'). But in case some race condition persists and causes an error, an error notification will be shown, and the flow will restart after terminating loose fibers (no leak). The error notification can be safely ignored.
4. I didn't implement inactivity based dozing because I don't want my phone to not doze while I'm commuting with my phone in my pocket. But the idea will be the same: kill the doze phase if activity detected.