Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,7 +48,8 @@ public static List<Class<?>> getXmlChecks() {
ChargeAwarenessXmlRule.class,
ServiceBootTimeXmlRule.class,
SaveModeAwarenessXmlRule.class,
HardwareAccelerationXmlRule.class
HardwareAccelerationXmlRule.class,
ExtraneousAnimationXmlRule.class
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* ecoCode Android plugin - Provides rules to reduce the environmental footprint of your Android applications
* Copyright © 2020 Green Code Initiative ([email protected])
*
* 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 <http://www.gnu.org/licenses/>.
*/
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"EC545",
"EC546",
"EC548",
"EC549"
"EC549",
"EC550"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<img src="http://www.neomades.com/extern/partage/ecoCode/2sur5_1x.png">
<p>
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 <code>res/animator/</code> resource directory.
</p>
17 changes: 17 additions & 0 deletions android-plugin/src/main/resources/io/ecocode/rules/xml/EC550.json
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?><!-- Noncompliant {{Avoiding extraneous animations in the UI is a good practice for saving battery power.}} -->
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="alpha"
android:valueFrom="0"
android:valueTo="1"
android:valueType="floatType"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="alpha"
android:valueFrom="1"
android:valueTo="0"
android:valueType="floatType"/>
Loading