green-code-initiative / ecoCode-challenge

Emboard in the hackhatons serie for improving ecoCode
3 stars 4 forks source link

[Hackaton 2024][Coach Team][Java - Android] Idleness: Keep Screen On (Rule example for Hackaton spotter - documentation justification) #91

Open jhertout opened 1 month ago

jhertout commented 1 month ago

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 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").

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