Marshmallow DOZE feature : What is this !!!
Every major Android version brings lots of good stuff ( Let me focus only on positive energies in this article -;) ) and this time in Marshmallow DOZE feature. Google finally serious about battery performance.
DOZE = Saving your device battery performance when device is not active or in other words when it meets certain criteria.
Is this the first we have feature like this in Android phones ? Nope not at all, almost every OEM spent lots of time and resources improving battery performance.
Sony Mobile XPERIA : "Stamina Mode" has been around for couple of years and i think they need to retire that feature as DOZE and Stamina Mode conflicts and achieves similar goals. This frustrates OEMs given they spend time and resource and google delivers it later stage of the game.
In a nutshell, Android
M includes new functions, Doze and App Standby, to improve battery life.
What does it means to Applications: You need to verify your application while battery
saving mode is active and when device resumes from this mode.
For 3rd party Apps : For 99% of the time you just need to abide Android laws. If you have a strong reason to deviate request to register your app to whitelist to exempt from Doze/App Standby
Let me cover some basics of DOZE mode and App standby before dwelling into "How to verify your app"
Information presented in this blog is based on my understanding of Android developer page, Source code and Other sources.
Overview of DOZE:
Starting from Android 6.0 (API level 23), Android introduces two power-saving features that extend battery life for users by managing how apps behave when a device is not connected to a power source
If a user leaves a device unplugged and stationary for a period of time, with the screen off, the device enters Doze mode. In Doze mode, the system attempts to conserve battery by restricting apps' access to network and CPU-intensive services. It also prevents apps from accessing the network and defers their jobs, syncs, and standard alarms.
Enter Doze mode when the following conditions are met
- Screen is Off
- Not charging
- Idle (65 mins at least)
What is restricted in Doze idle mode?
- Alarm deferred. Excludes alarm clock alarms and alarms set using setAndAllowWhileIdle()
- WiFi scans not performed
- Apps not allowed network access (Mobile data, WiFi)
- App wakelocks ignored
- SyncAdapter syncs and Jobshceduler jobs deferred
- Location with GPS not performed
Little more detailed way :
Fig 2: Life cycle of DOZE
Active or Charging:
If( Screen == ON || Charging == YES || ( Idle == N/A || BatteryLevel == N/A || Wakelock == N/A || true ))
printf(" Device is in Active OR Charging mode );
Transition mode:
if ( Screen == OFF && Charging == NO && Idle == YES && ( BatteryLevel == N/A || Wakelock == N/A || true )
printf("Device is in transitioning to DOZE mode");
Entering DOZE mode ( Further details on Transition to DOZE mode ):
It should be crystal clear from below diagram
Fig 3 : Entering IDLE mode.
Google did lot of tweaks to existing DeviceIdleController and IDLE transaction logic be found form below ( It is not simple straight forward so you need to go different files if you want to understand entire logic ).
https://android.googlesource.com/platform/frameworks/base/+/a750a63/services/core/java/com/android/server/DeviceIdleController.java
What is exempted and what is NOT:
#1 Apps NOT allowed N/W access bit Cellular Data and WiFi ) :
What happens to those services like VoIP / SIP / Skype ...etc ????
- Use Google Cloud Messaging(GCM) high priority message to see a notification right away - GCM is optimized to work with DOZE mode so Google recommends everyone to use GCM if possible.
- Not all so excited about GCM. If you don't want to change your app just register the app to whitelist . It is process, you need to fill in document and send it to Google explaining why you need to whitelisted :D.
#2 Alarm deferred:
Use setAndAllowWhileIdle() and setExactAndAllowWhileIdle()
Note: can’t fire alarm more than once per 15 mins per app.
#3 App wakelocks ignored
If you want wakelocks not to be ignored then register the app to whitelist. It is process, you need to fill in document and send it to Google explaining why you need to whitelisted :D.
#4 WiFi
scans not performed
No exemptions to be provided.
#5 SyncAdapter
syncs and Jobshceduler
jobs deferred
No exemptions to be provided.
#6 : Location
with GPS not performe
No exemptions to be provided.
How to test your App for Doze mode:
It is as easy as 1 - 2 - 3
1. Put your into DOZE mode forcibly by following steps:
1. Connect device to your PC
2. Run your app and leave it active
3. Turn screen off
4. Make the device unplugged state by the
following command
$ adb shell dumpsys battery unplug
•Move the device to Doze idle mode step by
step(IDLE_PENDING->SENSING->LOCATING->IDLE) by repeating the following
command
$ adb shell dumpsys deviceidle step
2. Observe
the behavior of your app during IDLE mode
•Make sure the effect of the restriction
is acceptable for user
3. Observe
the behavior of your app after you reactivate the device
Make sure the app recovers gracefully
when the device exits Doze.
Comments