diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ba6c326..3b5bfb45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,11 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added - -- Rule EC533 google ads consent +- Rule EC550/ESOB015: Extraneous Animation should not be used +- Rule EC549/ESOB016: Hardware Acceleration should not be activated by default +- Rule EC533/SGDP001: Google ads consent should be asked +- EC534/SPRI004: Add rule to avoid use of Tracking Id using TelephonyManager#getDeviceId() ### Changed -- SPRI004: Add rule to avoid use of Tracking Id using TelephonyManager#getDeviceId() - The embedded Groovy language analyzer was reconfigured to scan only `.gradle` files since it is the files we are interested in for the Android project configuration rules. The associated language is named `Groovy (Gradle)` instead of just `Groovy`. diff --git a/android-plugin/src/main/java/io/ecocode/xml/XmlCheckList.java b/android-plugin/src/main/java/io/ecocode/xml/XmlCheckList.java index 9eeeb274..b4df701c 100644 --- a/android-plugin/src/main/java/io/ecocode/xml/XmlCheckList.java +++ b/android-plugin/src/main/java/io/ecocode/xml/XmlCheckList.java @@ -17,6 +17,7 @@ */ package io.ecocode.xml; +import io.ecocode.xml.checks.power.ExtraneousAnimationXmlRule; import io.ecocode.xml.checks.batch.ServiceBootTimeXmlRule; import io.ecocode.xml.checks.power.ChargeAwarenessXmlRule; import io.ecocode.xml.checks.power.SaveModeAwarenessXmlRule; @@ -47,7 +48,8 @@ public static List> getXmlChecks() { ChargeAwarenessXmlRule.class, ServiceBootTimeXmlRule.class, SaveModeAwarenessXmlRule.class, - HardwareAccelerationXmlRule.class + HardwareAccelerationXmlRule.class, + ExtraneousAnimationXmlRule.class ); } diff --git a/android-plugin/src/main/java/io/ecocode/xml/checks/power/ExtraneousAnimationXmlRule.java b/android-plugin/src/main/java/io/ecocode/xml/checks/power/ExtraneousAnimationXmlRule.java new file mode 100644 index 00000000..169c2162 --- /dev/null +++ b/android-plugin/src/main/java/io/ecocode/xml/checks/power/ExtraneousAnimationXmlRule.java @@ -0,0 +1,53 @@ +/* + * ecoCode Android plugin - Provides rules to reduce the environmental footprint of your Android applications + * Copyright © 2020 Green Code Initiative (contact@ecocode.io) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.ecocode.xml.checks.power; + +import org.sonar.check.Priority; +import org.sonar.check.Rule; +import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; +import org.sonarsource.analyzer.commons.xml.XmlFile; +import org.sonarsource.analyzer.commons.xml.XmlTextRange; +import org.sonarsource.analyzer.commons.xml.checks.SonarXmlCheck; + +import java.net.URI; +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.nio.file.PathMatcher; +import java.nio.file.Paths; +import java.util.Collections; + +/** + * Checks the use extraneous animations. + */ +@Rule(key = "EC550", name = "Avoid extraneous animation", priority = Priority.MINOR) +@DeprecatedRuleKey(repositoryKey = "ecoCode-java", ruleKey = "ESOB015") +public class ExtraneousAnimationXmlRule extends SonarXmlCheck { + private static final String ERROR_MESSAGE = "Avoiding extraneous animations in the UI is a good practice for saving battery power."; + + + @Override + public void scanFile(XmlFile xmlFile) { + PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:**/res/animator/*.xml"); + URI uri = xmlFile.getInputFile().uri(); + Path xmlFilePath = Paths.get(uri); + if (pathMatcher.matches(xmlFilePath)) { + XmlTextRange textRange = new XmlTextRange(1, 1, 1, 2); + this.reportIssue(textRange, ERROR_MESSAGE, Collections.emptyList()); + } + } +} \ No newline at end of file diff --git a/android-plugin/src/main/resources/io/ecocode/android/xml/ecocode_xml_profile.json b/android-plugin/src/main/resources/io/ecocode/android/xml/ecocode_xml_profile.json index d37fa446..51f017dd 100644 --- a/android-plugin/src/main/resources/io/ecocode/android/xml/ecocode_xml_profile.json +++ b/android-plugin/src/main/resources/io/ecocode/android/xml/ecocode_xml_profile.json @@ -9,6 +9,7 @@ "EC545", "EC546", "EC548", - "EC549" + "EC549", + "EC550" ] } diff --git a/android-plugin/src/main/resources/io/ecocode/rules/xml/EC550.html b/android-plugin/src/main/resources/io/ecocode/rules/xml/EC550.html new file mode 100644 index 00000000..0e98580e --- /dev/null +++ b/android-plugin/src/main/resources/io/ecocode/rules/xml/EC550.html @@ -0,0 +1,6 @@ + +

+ Avoiding extraneous animations in the UI is a good practice for saving battery power. + This can be checked either in the Java code when an object is instance of Animator (sub)class, + or simply through the presence of xml files in the res/animator/ resource directory. +

\ No newline at end of file diff --git a/android-plugin/src/main/resources/io/ecocode/rules/xml/EC550.json b/android-plugin/src/main/resources/io/ecocode/rules/xml/EC550.json new file mode 100644 index 00000000..1b1c1d11 --- /dev/null +++ b/android-plugin/src/main/resources/io/ecocode/rules/xml/EC550.json @@ -0,0 +1,17 @@ +{ + "title": "Power: Extraneous Animation", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "privacy", + "social", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Minor" +} \ No newline at end of file diff --git a/android-plugin/src/test/java/io/ecocode/xml/XmlRulesDefinitionTest.java b/android-plugin/src/test/java/io/ecocode/xml/XmlRulesDefinitionTest.java index a5d31363..5235cf04 100644 --- a/android-plugin/src/test/java/io/ecocode/xml/XmlRulesDefinitionTest.java +++ b/android-plugin/src/test/java/io/ecocode/xml/XmlRulesDefinitionTest.java @@ -59,22 +59,25 @@ public void test() { assertThat(chargeAwarenessXmlRule).isNotNull(); assertThat(chargeAwarenessXmlRule.name()).isEqualTo("Power: Charge Awareness"); - RulesDefinition.Rule DarkUIBrightColorsXmlRule = repository.rule("EC547"); - assertThat(DarkUIBrightColorsXmlRule).isNotNull(); - assertThat(DarkUIBrightColorsXmlRule.name()).isEqualTo("Sobriety: Dark UI (Bright Colors)"); - - RulesDefinition.Rule DarkUIThemeXmlRule = repository.rule("EC548"); - assertThat(DarkUIThemeXmlRule).isNotNull(); - assertThat(DarkUIThemeXmlRule.name()).isEqualTo("Sobriety: Dark UI (Theme)"); - RulesDefinition.Rule saveModeAwarenessXml = repository.rule("EC546"); assertThat(saveModeAwarenessXml).isNotNull(); assertThat(saveModeAwarenessXml.name()).isEqualTo("Power: Save Mode Awareness"); + RulesDefinition.Rule darkUIBrightColorsXmlRule = repository.rule("EC547"); + assertThat(darkUIBrightColorsXmlRule).isNotNull(); + assertThat(darkUIBrightColorsXmlRule.name()).isEqualTo("Sobriety: Dark UI (Bright Colors)"); + + RulesDefinition.Rule darkUIThemeXmlRule = repository.rule("EC548"); + assertThat(darkUIThemeXmlRule).isNotNull(); + assertThat(darkUIThemeXmlRule.name()).isEqualTo("Sobriety: Dark UI (Theme)"); + RulesDefinition.Rule hardwareAccelerationXml = repository.rule("EC549"); assertThat(hardwareAccelerationXml).isNotNull(); assertThat(hardwareAccelerationXml.name()).isEqualTo("Sobriety: Hardware acceleration"); + RulesDefinition.Rule extraneousAnimationXmlRule = repository.rule("EC550"); + assertThat(extraneousAnimationXmlRule).isNotNull(); + assertThat(extraneousAnimationXmlRule.name()).isEqualTo("Power: Extraneous Animation"); for (RulesDefinition.Rule rule : repository.rules()) { for (RulesDefinition.Param param : rule.params()) { diff --git a/android-plugin/src/test/java/io/ecocode/xml/checks/power/ExtraneousAnimationXmlRuleTest.java b/android-plugin/src/test/java/io/ecocode/xml/checks/power/ExtraneousAnimationXmlRuleTest.java new file mode 100644 index 00000000..b99686a1 --- /dev/null +++ b/android-plugin/src/test/java/io/ecocode/xml/checks/power/ExtraneousAnimationXmlRuleTest.java @@ -0,0 +1,16 @@ +package io.ecocode.xml.checks.power; + +import org.junit.Test; +import org.sonarsource.analyzer.commons.xml.checks.SonarXmlCheckVerifier; + +public class ExtraneousAnimationXmlRuleTest { + @Test + public void shouldTriggerRule() { + SonarXmlCheckVerifier.verifyIssues("res/animator/fade_in.xml", new ExtraneousAnimationXmlRule()); + } + + @Test + public void shouldNotTriggerRule() { + SonarXmlCheckVerifier.verifyNoIssue("res/fade_out.xml", new ExtraneousAnimationXmlRule()); + } +} \ No newline at end of file diff --git a/android-plugin/src/test/resources/checks/ExtraneousAnimationXmlRule/res/animator/fade_in.xml b/android-plugin/src/test/resources/checks/ExtraneousAnimationXmlRule/res/animator/fade_in.xml new file mode 100644 index 00000000..0dc8ef7f --- /dev/null +++ b/android-plugin/src/test/resources/checks/ExtraneousAnimationXmlRule/res/animator/fade_in.xml @@ -0,0 +1,7 @@ + + \ No newline at end of file diff --git a/android-plugin/src/test/resources/checks/ExtraneousAnimationXmlRule/res/fade_out.xml b/android-plugin/src/test/resources/checks/ExtraneousAnimationXmlRule/res/fade_out.xml new file mode 100644 index 00000000..2ed3a3bc --- /dev/null +++ b/android-plugin/src/test/resources/checks/ExtraneousAnimationXmlRule/res/fade_out.xml @@ -0,0 +1,7 @@ + + \ No newline at end of file