Skip to content

Commit

Permalink
Merge pull request #58 from StanislasJC/rule/ESOB004_darkUI_18-days
Browse files Browse the repository at this point in the history
#ESOB004_DarkUI
  • Loading branch information
jhertout authored Apr 7, 2023
2 parents 077ed18 + 2abcd6b commit 9aa9817
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,45 @@
/**
* Checks in theme xmls and manifest if the application override a "light" theme:
* <ul>
* <li>Check styles that inherit Theme.Holo.Light or Theme.AppCompat.Light themes (or other themes that begin by
* Theme.Holo.Light or Theme.AppCompat.Light)</li>
* <li>Check themes set in manifest that begin by "@android:style/Theme.Holo.Light" or "@style/Theme.AppCompat.Light"
* </li>
* <li>Check styles that inherit Theme.Holo.Light,
* Theme.AppCompat.Light,
* Theme.Material.Light,
* Theme.Material3.Light or
* Theme.MaterialComponents.Light
* (or other themes that begin by Theme.Holo.Light,
* Theme.AppCompat.Light,
* Theme.Material.Light,
* Theme.Material3.Light or
* Theme.MaterialComponents.Light )</li>
*
* <li>Check themes set in manifest that begin by "@android:style/Theme.Holo.Light",
* "@style/Theme.AppCompat.Light",
* "@android:style/Theme.Material.Light",
* "@style/Theme.Material3.Light" or
* "@style/Theme.MaterialComponents.Light"</li>
* </ul>
*/
@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 STYLE_MATERIAL_LIGHT = "/resources/style[starts-with(@parent, \"@android:style/Theme.Material.Light\")]/@parent";
private static final String STYLE_MATERIAL3_LIGHT = "/resources/style[starts-with(@parent, \"Theme.Material3.Light\")]/@parent";
private static final String STYLE_MATERIALCOMPONENTS_LIGHT = "/resources/style[starts-with(@parent, \"Theme.MaterialComponents.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";

private static final String MANIFEST_MATERIAL_LIGHT = "/manifest/application[starts-with(@theme, \"@android:style/Theme.Material.Light\")]/@theme";
private static final String MANIFEST_MATERIAL3_LIGHT = "/manifest/application[starts-with(@theme, \"@style/Theme.Material3.Light\")]/@theme";
private static final String MANIFEST_MATERIALCOMPONENTS_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;
return STYLE_HOLO_LIGHT + "|" + STYLE_APPCOMPAT_LIGHT + "|" + STYLE_MATERIAL_LIGHT + "|" + STYLE_MATERIAL3_LIGHT + "|" + STYLE_MATERIALCOMPONENTS_LIGHT + "|" + MANIFEST_HOLO_LIGHT + "|" + MANIFEST_APPCOMPAT_LIGHT + "|" + MANIFEST_MATERIAL_LIGHT + "|" + MANIFEST_MATERIAL3_LIGHT + "|" + MANIFEST_MATERIALCOMPONENTS_LIGHT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@

<application android:theme="@style/Theme.AppCompat.Light.DarkActionBar"> <!-- Noncompliant {{Using a light theme may have a significant impact on energy consumption on (AM)OLED screens.}} -->
</application>

<application android:theme="@android:style/Theme.Material.Light"> <!-- Noncompliant {{Using a light theme may have a significant impact on energy consumption on (AM)OLED screens.}} -->
</application>

<application android:theme="@style/Theme.Material3.Light"> <!-- Noncompliant {{Using a light theme may have a significant impact on energy consumption on (AM)OLED screens.}} -->
</application>

<application android:theme="@style/Theme.MaterialComponents.Light"> <!-- Noncompliant {{Using a light theme may have a significant impact on energy consumption on (AM)OLED screens.}} -->
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,21 @@
<!-- Customize your theme here. -->
</style>

<style name="Theme.EcoCodeDark" parent="@android:style/Theme.Holo">
<style name="Theme.EcoCode" parent="@android:style/Theme.Holo">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>

<style name="Theme.EcoCodeDark" parent="@android:style/Theme.Material.Light"> <!-- Noncompliant {{Using a light theme may have a significant impact on energy consumption on (AM)OLED screens.}} -->
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
Expand Down Expand Up @@ -56,4 +70,15 @@
<!-- Primary brand color. -->
<item name="colorPrimary">@color/black</item>
</style>

<style name="Theme.MaterialComponents.Light" parent="Theme.MaterialComponents.Light"> <!-- Noncompliant {{Using a light theme may have a significant impact on energy consumption on (AM)OLED screens.}} -->
<!-- Primary brand color. -->
<item name="colorPrimary">@color/white</item>
</style>

<style name="Theme.Material3.Light" parent="Theme.Material3.Light"> <!-- Noncompliant {{Using a light theme may have a significant impact on energy consumption on (AM)OLED screens.}} -->
<!-- Primary brand color. -->
<item name="colorPrimary">@color/white</item>
</style>

</resources>

0 comments on commit 9aa9817

Please sign in to comment.