To avoid draining the battery, an Android device that is left idle quickly falls asleep.
Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically use the FLAG_KEEP_SCREEN_ON in their activity. Another way to implement this is in their application's layout XML file, by using the android:keepScreenOn attribute.
Noncompliant Code Example:
getWindow().addFlags(FLAG_KEEP_SCREEN_ON);
Rule short description
Keeping the screen on should be avoided to avoid draining the battery.
Rule justification
This rule was identified by the research scientist Olivier Le Goaër and can be found in the cnmur best practices documentation: https://github.com/cnumr/best-practices-mobile (see "Idleness" section - "Keep Screen On").
Severity: Critical - the impact on the battery energy consumption is important compared to an application that does not have the flag activated.
Remediation cost: Easy - if the flag to keep the screen on was not added on purpose it is easy to remove it, else the flag was required by the application and in this case the developer will have to manually ignore the rule detection in its SonarQube analysis.
Implementation principle
On the method android.view.Window#addFlags(int), checks that it is not called with parameter value FLAG_KEEP_SCREEN_ON (0x00000080)
Same thing on the method android.view.Window#setFlags(int)
Rule title
Idleness: Keep Screen On
Language and platform
Java - Android.
Rule description
To avoid draining the battery, an Android device that is left idle quickly falls asleep.
Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically use the
FLAG_KEEP_SCREEN_ON
in their activity. Another way to implement this is in their application's layout XML file, by using theandroid:keepScreenOn
attribute.Noncompliant Code Example:
Rule short description
Keeping the screen on should be avoided to avoid draining the battery.
Rule justification
This rule was identified by the research scientist Olivier Le Goaër and can be found in the cnmur best practices documentation: https://github.com/cnumr/best-practices-mobile (see "Idleness" section - "Keep Screen On").
The official Android documentation: https://developer.android.com/develop/background-work/background-tasks/scheduling/wakelock indicates that "To avoid draining the battery, an Android device that is left idle quickly falls asleep.". The documentation then explain that using the KEEP_SCREEN_ON flag will bypass this behavior.
Severity / Remediation Cost
Implementation principle
android.view.Window#addFlags(int)
, checks that it is not called with parameter valueFLAG_KEEP_SCREEN_ON (0x00000080)
android.view.Window#setFlags(int)