-
Notifications
You must be signed in to change notification settings - Fork 27
Added rule Sobriety > Hardware acceleration #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jduigoudev
merged 1 commit into
green-code-initiative:main
from
LofoWalker:feat/ESOB016_hardware_acceleration
May 21, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
android-plugin/src/main/java/io/ecocode/xml/checks/sobriety/HardwareAccelerationXmlRule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * 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 <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package io.ecocode.xml.checks.sobriety; | ||
|
|
||
| import io.ecocode.xml.checks.XPathSimpleCheck; | ||
| import org.sonar.check.Priority; | ||
| import org.sonar.check.Rule; | ||
| import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; | ||
| import org.w3c.dom.Node; | ||
|
|
||
| @Rule(key = "EC549", name = "Hardware acceleration", priority = Priority.MAJOR) | ||
| @DeprecatedRuleKey(repositoryKey = "ecoCode-java", ruleKey = "ESOB016") | ||
| public class HardwareAccelerationXmlRule extends XPathSimpleCheck { | ||
|
|
||
| private static final String ERROR_MESSAGE_ENABLED = | ||
| "Hardware acceleration is enabled. Consider disabling it to reduce RAM usage."; | ||
|
|
||
| private static final String ERROR_MESSAGE_IMPLICIT = | ||
| "Hardware acceleration is implicitly enabled. Consider disabling it explicitly with android:hardwareAccelerated=\"false\"."; | ||
|
|
||
| private static final String ANDROID_HARDWARE_ACCELERATED = "android:hardwareAccelerated"; | ||
| private static final String XPATH_TARGET_COMPONENTS = "//application | //activity | //window | //view"; | ||
|
|
||
| @Override | ||
| protected String getMessage() { | ||
| return ERROR_MESSAGE_IMPLICIT; | ||
| } | ||
|
|
||
| @Override | ||
| protected String getXPathExpressionString() { | ||
| return XPATH_TARGET_COMPONENTS; | ||
| } | ||
|
|
||
| @Override | ||
| protected void visitNode(Node node, String message) { | ||
| Node hardwareAttributeNode = getHardwareAccelerationAttribute(node); | ||
|
|
||
| if ("true".equals(getNodeValue(hardwareAttributeNode))) { | ||
| reportIssue(hardwareAttributeNode, ERROR_MESSAGE_ENABLED); | ||
| } | ||
| else if (!isHardwareAccelerationDisabledInParent(node)) { | ||
| reportIssue(node, ERROR_MESSAGE_IMPLICIT); | ||
| } | ||
| } | ||
|
|
||
| private boolean isHardwareAccelerationDisabledInParent(Node node) { | ||
| Node parent = node; | ||
|
|
||
| while (parent != null && !"application".equals(parent.getNodeName())) { | ||
| parent = parent.getParentNode(); | ||
| } | ||
|
|
||
| if (parent == null) { | ||
| return false; | ||
| } | ||
|
|
||
| Node parentHardwareAttr = getHardwareAccelerationAttribute(parent); | ||
| return "false".equals(getNodeValue(parentHardwareAttr)); | ||
| } | ||
|
|
||
| private static Node getHardwareAccelerationAttribute(Node node) { | ||
| if (node == null || node.getAttributes() == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return node.getAttributes().getNamedItem(ANDROID_HARDWARE_ACCELERATED); | ||
| } | ||
|
|
||
| private String getNodeValue(Node node) { | ||
| return (node != null) ? node.getNodeValue() : null; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| "EC544", | ||
| "EC545", | ||
| "EC546", | ||
| "EC548" | ||
| "EC548", | ||
| "EC549" | ||
| ] | ||
| } | ||
41 changes: 41 additions & 0 deletions
41
android-plugin/src/main/resources/io/ecocode/rules/xml/EC549.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <img src="http://www.neomades.com/extern/partage/ecoCode/2sur5_1x.png"> | ||
| <p> | ||
| Hardware acceleration is a feature that improves rendering performance by offloading certain 2D drawing operations to the GPU. | ||
| While it can enhance UI responsiveness, it also increases memory and power consumption — especially on lower-end or older devices. | ||
| <br> | ||
| Since API level 14, hardware acceleration is enabled by default in Android applications. However, if not required, | ||
| it is recommended to disable it explicitly in the manifest to reduce resource usage and promote energy-efficient design. | ||
| <br> | ||
| This rule detects components (activities, services, etc.) that either explicitly enable hardware acceleration or inherit it implicitly without being disabled at the application level. | ||
| </p> | ||
|
|
||
| <h2>Noncompliant Code Example</h2> | ||
| <pre> | ||
| <application | ||
| android:label="@string/app_name"> | ||
|
|
||
| <activity | ||
| android:name=".FastActivity" | ||
| android:hardwareAccelerated="true" /> | ||
|
|
||
| <activity | ||
| android:name=".AnotherFastActivity" /> | ||
|
|
||
| </application> | ||
| </pre> | ||
|
|
||
| <h2>Compliant Solution</h2> | ||
| <pre> | ||
| <application | ||
| android:label="@string/app_name" | ||
| android:hardwareAccelerated="false"> | ||
|
|
||
| <activity | ||
| android:name=".FastActivity" | ||
| android:hardwareAccelerated="true" /> <!-- Only if really needed --> | ||
|
|
||
| <activity | ||
| android:name=".AnotherFastActivity" /> <!-- Inherits 'false' --> | ||
|
|
||
| </application> | ||
| </pre> |
17 changes: 17 additions & 0 deletions
17
android-plugin/src/main/resources/io/ecocode/rules/xml/EC549.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "title": "Sobriety: Hardware acceleration", | ||
| "type": "CODE_SMELL", | ||
| "status": "ready", | ||
| "remediation": { | ||
| "func": "Constant\/Issue", | ||
| "constantCost": "1min" | ||
| }, | ||
| "tags": [ | ||
| "sobriety", | ||
| "environment", | ||
| "ecocode", | ||
| "android", | ||
| "eco-design" | ||
| ], | ||
| "defaultSeverity": "Major" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...-plugin/src/test/java/io/ecocode/xml/checks/sobriety/HardwareAccelerationXmlRuleTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.ecocode.xml.checks.sobriety; | ||
|
|
||
| import org.junit.Test; | ||
| import org.sonarsource.analyzer.commons.xml.checks.SonarXmlCheckVerifier; | ||
|
|
||
| public class HardwareAccelerationXmlRuleTest { | ||
|
|
||
| @Test | ||
| public void detectExplicitOrImplicitHardwareAcceleration() { | ||
| SonarXmlCheckVerifier.verifyIssues("HardwareAccelerationCheck.xml", new HardwareAccelerationXmlRule()); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
...lugin/src/test/resources/checks/HardwareAccelerationXmlRule/HardwareAccelerationCheck.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| package="com.example.hardwaretest"> | ||
|
|
||
| <application android:label="@string/app_name" android:hardwareAccelerated="false"> | ||
|
|
||
| <activity android:name=".SafeActivity" android:hardwareAccelerated="false"/> | ||
| <activity android:name=".FastActivity" android:hardwareAccelerated="true"/> <!-- Noncompliant {{Hardware acceleration is enabled. Consider disabling it to reduce RAM usage.}} --> | ||
| <activity android:name=".MainActivity"/> | ||
| <service android:name=".MyService"/> | ||
| <activity android:name=".AnotherFastActivity" android:hardwareAccelerated="true"/> <!-- Noncompliant {{Hardware acceleration is enabled. Consider disabling it to reduce RAM usage.}} --> | ||
| <receiver android:name=".MyReceiver"/> | ||
| <window android:name=".FancyWindow" android:hardwareAccelerated="true"/> <!-- Noncompliant {{Hardware acceleration is enabled. Consider disabling it to reduce RAM usage.}} --> | ||
| <view android:name=".DefaultNotAcceleratedView"/> | ||
|
|
||
| </application> | ||
|
|
||
| <activity android:name=".OutOfAppActivity"/> <!-- Noncompliant {{Hardware acceleration is implicitly enabled. Consider disabling it explicitly with android:hardwareAccelerated="false".}} --> | ||
| <view android:name=".DefaultAcceleratedView"/> <!-- Noncompliant {{Hardware acceleration is implicitly enabled. Consider disabling it explicitly with android:hardwareAccelerated="false".}} --> | ||
|
|
||
| <activity android:name=".OutOfAppActivityExplicit" android:hardwareAccelerated="true"/><!-- Noncompliant {{Hardware acceleration is enabled. Consider disabling it to reduce RAM usage.}} --> | ||
| <view android:name=".DefaultAcceleratedViewExplicit" android:hardwareAccelerated="true"/><!-- Noncompliant {{Hardware acceleration is enabled. Consider disabling it to reduce RAM usage.}} --> | ||
|
|
||
| <application android:label="@string/alt_app" android:hardwareAccelerated="false"> | ||
| <activity android:name=".AltActivity"/> | ||
| </application> | ||
| </manifest> | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.