by Prasanth S
Mimics network restrictions aspect of Marshmallow's Doze on Lollipop (and lower), by toggling data and Wi-Fi. Probably makes sense to use on Marshmallow too.
Root required for Android 5+.
A non-root 'Wi-Fi only' version is also available, which is what I personally use.
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 (data and Wi-Fi off) for a certain doze period (default 9 min), and
2.2. Network-awake (data and Wi-Fi reverted to their pre-doze states) for a certain 'maintenance window' (default 1 min).
3. Doze phase is broken if the screen is turned back on.
1. If data and Wi-Fi are both off to begin with, doze phase is redundant and isn't entered.
2. This flow is robust under changes to data and Wi-Fi states 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.