green-code-initiative / ecoCode-android

Reduce the environmental footprint of your Java-based Android apps with SonarQube
https://ecocode.io
GNU General Public License v3.0
54 stars 17 forks source link

Add Theme.MaterialComponents.Light detection in DarkUIThemeXmlRule #32

Closed qonto-android-team closed 1 year ago

qonto-android-team commented 1 year ago
    Hi! We'd like to suggest an improvement of `DarkUIThemeXmlRule`. Currently, it handles `Theme.AppCompat.Light` but it lacks `Theme.MaterialComponents.Light` detection. Adding this detection can be valuable as `MaterialComponents` themes are more and more used on Android.

It can be achieved by applying these changes:

@Rule(key = "ESOB004", name = "ecoCodeDarkUITheme")
public class DarkUIThemeXmlRule extends XPathSimpleCheck {

    private static final String STYLE_HOLO_LIGHT = "/resources/style[starts-with(@parent, \"@android:style/Theme.Holo.Light\")]/@parent";
    private static final String STYLE_APPCOMPAT_LIGHT = "/resources/style[starts-with(@parent, \"Theme.AppCompat.Light\")]/@parent";
    private static final String MANIFEST_HOLO_LIGHT = "/manifest/application[starts-with(@theme, \"@android:style/Theme.Holo.Light\")]/@theme";
    private static final String MANIFEST_APPCOMPAT_LIGHT = "/manifest/application[starts-with(@theme, \"@style/Theme.AppCompat.Light\")]/@theme";
    // New Theme.MaterialComponents.Light detection
    private static final String MANIFEST_MATERIAL_LIGHT = "/manifest/application[starts-with(@theme, \"@style/Theme.MaterialComponents.Light\")]/@theme";

    @Override
    protected String getMessage() {
        return "Using a light theme may have a significant impact on energy consumption on (AM)OLED screens.";
    }

    @Override
    protected String getXPathExpressionString() {
        return STYLE_HOLO_LIGHT + "|" + STYLE_APPCOMPAT_LIGHT + "|" + MANIFEST_HOLO_LIGHT + "|" + MANIFEST_APPCOMPAT_LIGHT + "|" + MANIFEST_MATERIAL_LIGHT;
    }
}

Originally posted by @qonto-android-team in https://github.com/green-code-initiative/ecoCode-mobile/discussions/30

Jberque commented 1 year ago

Hi, We will add this check soon, there is an Hackathon next month, the rule will be updated at this time.

Thank you for your interest.

jhertout commented 1 year ago

The issue was solved during the hackaton and merge #57. Thanks for your contribution.