From 0346f17d21d3a1218233d16e541efb009f3d259d Mon Sep 17 00:00:00 2001 From: Julien Hertout Date: Wed, 18 Oct 2023 17:03:10 +0200 Subject: [PATCH 01/32] Add the first "refused" rule in "rules.md" --- RULES.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/RULES.md b/RULES.md index dd5832c84..9a6b94198 100644 --- a/RULES.md +++ b/RULES.md @@ -1,4 +1,6 @@ -# Rules support matrix by techno +# Rules + +## Rules support matrix by techno This matrix lists all the rules identified as implementable within the standard ecoCode plugin. @@ -52,3 +54,10 @@ Some are applicable for different technologies. | | Avoid high accuracy geolocation | Avoid using high accuracy geolocation in web applications (linter key : `@ecocode/avoid-high-accuracy-geolocation`) | | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | | | No import all from library | Should not import all from library (linter key : `@ecocode/no-import-all-from-library`) | | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | | | Prefer collections with pagination | Prefer API collections with pagination (linter key : `@ecocode/prefer-collections-with-pagination`) | | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | + +## Refused rules + +This array lists rules proposed by the community but refused in ecoCode plugins with the justification. + +| Name | Description | Language | Invalidation | +| Use numpy array instead of standard list | The use of numpy library to perform array manipulation is more energy efficient than the use of the standard list functions. | Python | [Github discussion with measures](https://github.com/green-code-initiative/ecoCode/issues/132) | From 0a2e9c8962be3c3cca9d75e13952d0ef94ca49a0 Mon Sep 17 00:00:00 2001 From: Julien Hertout Date: Thu, 19 Oct 2023 07:07:41 +0200 Subject: [PATCH 02/32] Remove rule EC66 for Python with justification --- RULES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RULES.md b/RULES.md index 9a6b94198..9419138e5 100644 --- a/RULES.md +++ b/RULES.md @@ -32,7 +32,7 @@ Some are applicable for different technologies. | EC53 | Using arrays in foreach loops | foreach deduplicates items in a list before starting the enumeration. It is therefore generally more economical to use a simple for loop when you have a good command of the collection. | [cnumr best practices (3rd edition) BP_053 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | 🚀 | 🚀 | 🚫 | 🚀 | | EC7 | Rewrite native getter/setters | Overloading them lengthens the compilation and execution times of these methods, which are usually much better optimized by the language than by the developer. | [cnumr best practices (3rd edition) BP_062 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | | EC63 | Unnecessarily assigning values to variables | Avoid declaring and using variables when it is not indis-thinkable. Indeed, each allocation corresponds to the RAM occupied. | [cnumr best practices (3rd edition) BP_063 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | 🚀 | 🚀 | 🚀 | 🚀 | -| EC66 | Use single quote (') instead of quotation mark (") | The shape using the quotation marks allows the developer to insert variables that will be substituted at run time. But if the string does not have a variable, use quotes instead. Thus, language will not look for variables to subtituture, which will reduce the consumption of CPU cycles. | [cnumr best practices (3rd edition) BP_066 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | ✅ | 🚀 | +| EC66 | Use single quote (') instead of quotation mark (") | The shape using the quotation marks allows the developer to insert variables that will be substituted at run time. But if the string does not have a variable, use quotes instead. Thus, language will not look for variables to subtituture, which will reduce the consumption of CPU cycles. | [cnumr best practices (3rd edition) BP_066 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | 🚫
[see](https://github.com/green-code-initiative/ecoCode-python/issues/4) | 🚀 | | EC67 | Use the $i++ variable during an iteration | The $i++ form has the disadvantage of generating a tem-porary variable during incrementation, which is not the case with the ++$i form. | [cnumr best practices (3rd edition) BP_067 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | 🚫 | 🚫 | | EC69 | Calling a function in the declaration of a for loop | Avoid calling the function each time the loop is iterated. | [cnumr best practices (3rd edition) BP_069 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | | EC72 | Perform an SQL query inside a loop | Servers are optimized to process multiple selections, insertions, or changes in a single query or transaction. consume CPU cycles, RAM, and bandwidth unnecessarily. | [cnumr best practices (3rd edition) BP_072](https://github.com/cnumr/best-practices/blob/main/chapters/BP_072_fr.md) | ✅ | ✅ | 🚀 | ✅ | 🚀 | From d4383a04e8db94b33805cee21a11c218f08088ff Mon Sep 17 00:00:00 2001 From: Julien Hertout <57949794+jhertout@users.noreply.github.com> Date: Thu, 19 Oct 2023 07:18:44 +0200 Subject: [PATCH 03/32] RULES.md: fix markdown syntax --- RULES.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/RULES.md b/RULES.md index 9419138e5..d7f6a6901 100644 --- a/RULES.md +++ b/RULES.md @@ -32,7 +32,7 @@ Some are applicable for different technologies. | EC53 | Using arrays in foreach loops | foreach deduplicates items in a list before starting the enumeration. It is therefore generally more economical to use a simple for loop when you have a good command of the collection. | [cnumr best practices (3rd edition) BP_053 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | 🚀 | 🚀 | 🚫 | 🚀 | | EC7 | Rewrite native getter/setters | Overloading them lengthens the compilation and execution times of these methods, which are usually much better optimized by the language than by the developer. | [cnumr best practices (3rd edition) BP_062 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | | EC63 | Unnecessarily assigning values to variables | Avoid declaring and using variables when it is not indis-thinkable. Indeed, each allocation corresponds to the RAM occupied. | [cnumr best practices (3rd edition) BP_063 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | 🚀 | 🚀 | 🚀 | 🚀 | -| EC66 | Use single quote (') instead of quotation mark (") | The shape using the quotation marks allows the developer to insert variables that will be substituted at run time. But if the string does not have a variable, use quotes instead. Thus, language will not look for variables to subtituture, which will reduce the consumption of CPU cycles. | [cnumr best practices (3rd edition) BP_066 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | 🚫
[see](https://github.com/green-code-initiative/ecoCode-python/issues/4) | 🚀 | +| EC66 | Use single quote (') instead of quotation mark (") | The shape using the quotation marks allows the developer to insert variables that will be substituted at run time. But if the string does not have a variable, use quotes instead. Thus, language will not look for variables to subtituture, which will reduce the consumption of CPU cycles. | [cnumr best practices (3rd edition) BP_066 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | 🚫
[see](https://github.com/green-code-initiative/ecoCode-python/issues/4) | 🚀 | | EC67 | Use the $i++ variable during an iteration | The $i++ form has the disadvantage of generating a tem-porary variable during incrementation, which is not the case with the ++$i form. | [cnumr best practices (3rd edition) BP_067 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | 🚫 | 🚫 | | EC69 | Calling a function in the declaration of a for loop | Avoid calling the function each time the loop is iterated. | [cnumr best practices (3rd edition) BP_069 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | | EC72 | Perform an SQL query inside a loop | Servers are optimized to process multiple selections, insertions, or changes in a single query or transaction. consume CPU cycles, RAM, and bandwidth unnecessarily. | [cnumr best practices (3rd edition) BP_072](https://github.com/cnumr/best-practices/blob/main/chapters/BP_072_fr.md) | ✅ | ✅ | 🚀 | ✅ | 🚀 | @@ -59,5 +59,6 @@ Some are applicable for different technologies. This array lists rules proposed by the community but refused in ecoCode plugins with the justification. -| Name | Description | Language | Invalidation | +| Name | Description | Language | Invalidation | +|------------|------------|------------|------------| | Use numpy array instead of standard list | The use of numpy library to perform array manipulation is more energy efficient than the use of the standard list functions. | Python | [Github discussion with measures](https://github.com/green-code-initiative/ecoCode/issues/132) | From fe050558d181782006e3e086e23cf92266fb37e5 Mon Sep 17 00:00:00 2001 From: Julien Hertout Date: Fri, 20 Oct 2023 14:16:43 +0200 Subject: [PATCH 04/32] Add a refused rule for HTML and fix an english mistake --- RULES.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/RULES.md b/RULES.md index 9419138e5..57a4c5a54 100644 --- a/RULES.md +++ b/RULES.md @@ -57,7 +57,9 @@ Some are applicable for different technologies. ## Refused rules -This array lists rules proposed by the community but refused in ecoCode plugins with the justification. +This table lists rules proposed by the community but refused in ecoCode plugins with the justification. | Name | Description | Language | Invalidation | | Use numpy array instead of standard list | The use of numpy library to perform array manipulation is more energy efficient than the use of the standard list functions. | Python | [Github discussion with measures](https://github.com/green-code-initiative/ecoCode/issues/132) | +| HTML page must contain a doctype tag | The difference in performance is negligible, this rule is more related to + the user experience. | HTML | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/103) | From ad752647329351431f67112b72767906ae273302 Mon Sep 17 00:00:00 2001 From: Julien Hertout <57949794+jhertout@users.noreply.github.com> Date: Fri, 20 Oct 2023 14:21:48 +0200 Subject: [PATCH 05/32] Update RULES.md Fix Markdown error in Rules.md --- RULES.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/RULES.md b/RULES.md index b1ffcac40..8fb779a7a 100644 --- a/RULES.md +++ b/RULES.md @@ -62,5 +62,4 @@ This table lists rules proposed by the community but refused in ecoCode plugins | Name | Description | Language | Invalidation | |------------|------------|------------|------------| | Use numpy array instead of standard list | The use of numpy library to perform array manipulation is more energy efficient than the use of the standard list functions. | Python | [Github discussion with measures](https://github.com/green-code-initiative/ecoCode/issues/132) | -| HTML page must contain a doctype tag | The difference in performance is negligible, this rule is more related to - the user experience. | HTML | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/103) | +| HTML page must contain a doctype tag | The difference in performance is negligible, this rule is more related to the user experience. | HTML | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/103) | From 745f6ad3aacd61f496af082f2a355bc69c0b843e Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sat, 21 Oct 2023 23:34:43 +0200 Subject: [PATCH 06/32] [PR 128-ecocode] replace EC34 by EC35 : update CHANGELOG, update rule specifications --- CHANGELOG.md | 1 + RULES.md | 86 +++++++++---------- ecocode-rules-specifications/pom.xml | 2 +- .../rules/{EC34/EC34.json => EC35/EC35.json} | 2 +- .../rules/{EC34 => EC35}/php/1GB.etsdiff.csv | 0 .../EC34.asciidoc => EC35/php/EC35.asciidoc} | 3 +- .../python/EC35.asciidoc} | 3 +- 7 files changed, 50 insertions(+), 47 deletions(-) rename ecocode-rules-specifications/src/main/rules/{EC34/EC34.json => EC35/EC35.json} (73%) rename ecocode-rules-specifications/src/main/rules/{EC34 => EC35}/php/1GB.etsdiff.csv (100%) rename ecocode-rules-specifications/src/main/rules/{EC34/php/EC34.asciidoc => EC35/php/EC35.asciidoc} (88%) rename ecocode-rules-specifications/src/main/rules/{EC34/python/EC34.asciidoc => EC35/python/EC35.asciidoc} (79%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96d824280..e2d15379d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#136](https://github.com/green-code-initiative/ecoCode/issues/136) Upgrade rule EC53 for Python : no implementation possible for python ### Deleted +- [#128](https://github.com/green-code-initiative/ecoCode/pull/128) Delete rule EC34 for Python and PHP : rule replaced by EC35 ("file not found" sepcific) ## [1.4.0] - 2023-08-08 diff --git a/RULES.md b/RULES.md index dd5832c84..dec0b3718 100644 --- a/RULES.md +++ b/RULES.md @@ -9,46 +9,46 @@ Some are applicable for different technologies. - 🚀 Rule to implement - 🚫 Non applicable rule -| Rule key | Name | Description | Reference/Validation | Java | Php | JS | Python | Rust | -|----------|------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------|-----|----|--------|------| -| | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | -| | Non-grouped similar CSS declarations | When multiple Document Object Model (DOM) elements have common CSS properties, declare them together in the same style sheet. This method reduces the weight of CSS. | [cnumr best practices (3rd edition) BP_025](https://github.com/cnumr/best-practices/blob/main/chapters/BP_025_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | -| | CSS shorthand notations not used | Reduces the weight of the style sheet. | [cnumr best practices (3rd edition) BP_026](https://github.com/cnumr/best-practices/blob/main/chapters/BP_026_fr.md) | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| | CSS print not included | This style sheet reduces the number of pages printed. | [cnumr best practices (3rd edition) BP_027](https://github.com/cnumr/best-practices/blob/main/chapters/BP_027_fr.md) | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| | Non-standard fonts used | Prefer standard fonts, as they are already present on the user's computer, so they do not need to download them. This saves bandwidth, while speeding up the display of the site. | [cnumr best practices (3rd edition) BP_029](https://github.com/cnumr/best-practices/blob/main/chapters/BP_029_fr.md) | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| | Non-outsourced CSS and Javascript | If you include CSS or JavaScript code in the body of the HTML file, while the HTML file is used by several pages (or even the entire site), this code must be transferred for each page requested by the user, which increases the volume of data transmitted. | [cnumr best practices (3rd edition) BP_032](https://github.com/cnumr/best-practices/blob/main/chapters/BP_032_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | -| | Resize images browser-side | Do not resize images using the HEIGHT and WIDTH attributes of the HTML code. This approach requires transferring these images to their original size, wasting bandwidth and CPU cycles. | [cnumr best practices (3rd edition) BP_034](https://github.com/cnumr/best-practices/blob/main/chapters/BP_034_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | -| EC10 | Use unoptimized vector images | Less heavy SVG images using less bandwidth | [cnumr best practices (3rd edition) BP_036](https://github.com/cnumr/best-practices/blob/main/chapters/BP_036_fr.md) | 🚧 | 🚀 | 🚀 | ✅ | 🚀 | -| | Using too many CSS/javascript animations | JavaScript/CSS animations can be very expensive in terms of CPU cycles and memory consumption. | [cnumr best practices (3rd edition) BP_039](https://github.com/cnumr/best-practices/blob/main/chapters/BP_039_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | -| | Modify the DOM when traversing it | Modifying the DOM (Document Object Model) as you traverse it can lead to situations where the loop becomes very resource-intensive, especially CPU cycles. | [cnumr best practices (3rd edition) BP_041](https://github.com/cnumr/best-practices/blob/main/chapters/BP_041_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | -| | Edit DOM elements to make it invisible | When an element of the Document Object Model (DOM) needs to be modified by several properties, each change in style or content will generate a repaint or reflow. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | -| | Modify several CSS properties all at once | To limit the number of repaints/reflows, it is recommended not to modify properties one by one. (linter key : `@ecocode/no-multiple-style-changes`) | [cnumr best practices (3rd edition) BP_045](https://github.com/cnumr/best-practices/blob/main/chapters/BP_045_fr.md) | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | -| EC34 | Using try...catch...finally calls | When an exception is thrown, a variable (the exception itself) is created in the catch block and destroyed at the end of the block. Creating this variable and destroying it consumes CPU cycles and RAM unnecessarily. That is why it is important not to use this construction and to prefer, as much as possible, a logical test. | [cnumr best practices (3rd edition) BP_047 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | ✅ | 🚀 | -| EC22 | The use of methods for basic operations | Using methods for basic operations consumes additional system resources. The interpreter must in effect and solve the objects and then the methods, just to carry out these simple operations of the language. | [cnumr best practices (3rd edition) BP_048 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | 🚀 | 🚀 | -| ??? | Call a DOM element multiple times without caching (linter key : `@ecocode/no-multiple-access-dom-element`) | Access to the Document Object Model (DOM) is costly in terms of CPU resources (CPU cycles). Also, when you use the same DOM element from JavaScript multiple times, store its reference in a variable so that you do not go through the DOM again for the same element. | [cnumr best practices (3rd edition) BP_049](https://github.com/cnumr/best-practices/blob/main/chapters/BP_049_fr.md) | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | -| EC4 | Use global variables | When using a global variable, the interpretation engine must check: 1) that it exists in the current scope, in the one above, etc. ; 2) the variable has a value; 3) ... To avoid all these checks, it is often possible to pass the useful variables as arguments of routines, making them local. This process saves computational time (CPU cycles). | [cnumr best practices (3rd edition) BP_050 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | -| EC53 | Using arrays in foreach loops | foreach deduplicates items in a list before starting the enumeration. It is therefore generally more economical to use a simple for loop when you have a good command of the collection. | [cnumr best practices (3rd edition) BP_053 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | 🚀 | 🚀 | 🚫 | 🚀 | -| EC7 | Rewrite native getter/setters | Overloading them lengthens the compilation and execution times of these methods, which are usually much better optimized by the language than by the developer. | [cnumr best practices (3rd edition) BP_062 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | -| EC63 | Unnecessarily assigning values to variables | Avoid declaring and using variables when it is not indis-thinkable. Indeed, each allocation corresponds to the RAM occupied. | [cnumr best practices (3rd edition) BP_063 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | 🚀 | 🚀 | 🚀 | 🚀 | -| EC66 | Use single quote (') instead of quotation mark (") | The shape using the quotation marks allows the developer to insert variables that will be substituted at run time. But if the string does not have a variable, use quotes instead. Thus, language will not look for variables to subtituture, which will reduce the consumption of CPU cycles. | [cnumr best practices (3rd edition) BP_066 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | ✅ | 🚀 | -| EC67 | Use the $i++ variable during an iteration | The $i++ form has the disadvantage of generating a tem-porary variable during incrementation, which is not the case with the ++$i form. | [cnumr best practices (3rd edition) BP_067 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | 🚫 | 🚫 | -| EC69 | Calling a function in the declaration of a for loop | Avoid calling the function each time the loop is iterated. | [cnumr best practices (3rd edition) BP_069 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | -| EC72 | Perform an SQL query inside a loop | Servers are optimized to process multiple selections, insertions, or changes in a single query or transaction. consume CPU cycles, RAM, and bandwidth unnecessarily. | [cnumr best practices (3rd edition) BP_072](https://github.com/cnumr/best-practices/blob/main/chapters/BP_072_fr.md) | ✅ | ✅ | 🚀 | ✅ | 🚀 | -| EC74 | Write SELECT * FROM | The database server must resolve the fields based on the schema. If you are familiar with the diagram, it is strongly recommended to name the fields. | [cnumr best practices (3rd edition) BP_074 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | -| EC1 | Calling a Spring repository inside a loop | The use of Spring repository in a loop induces unnecessary calculations by the CPU and therefore superfluous energy consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | -| EC3 | Getting the size of the collection in the loop | When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power. | | ✅ | ✅ | 🚀 | 🚫 | 🚀 | -| EC2 | Multiple if-else statement | Using too many conditional if-else statements will impact performance since JVM will have to compare the conditions. Prefer using a switch statement instead of multiple if-else if possible, or refactor your code to reduce conditonnal statements on the same variable. Switch statement has a performance advantage over if – else. | | ✅ | ✅ | 🚀 | 🚧 | 🚀 | -| EC76 | Usage of static collections | Avoid usage of static collections. If you want to use static collections make them final and create for example a singleton if needed containing the collections. The static fields are more complicated for the Garbage Collector to manage and can lead to memory leaks. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | -| EC77 | Usage Pattern.compile() in a non-static context | Avoid using Pattern.compile() in a non-static context. This operation requires a non negligible amount of computational power, Using a single match saves CPU cycles and RAM consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | -| EC75 | Concatenate Strings in loop | Don't concatenate Strings in loop. User StringBuilder instead. Strings are immutable so each time you concatenate a String, a new String is created. This is a waste of memory and CPU. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | -| EC78 | Const parameter in batch update | Don't set const parameter in batch update => Put its in query. Creating this parameter and destroying it consumes CPU cycles and RAM unnecessarily. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | -| EC79 | Free resources | try-with-resources Statement needs to be implemented for any object that implements the AutoCloseable interface, it save computer resources. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | -| EC32 | Initialize builder/buffer with the appropriate size | If you know in advance how many characters would be appended, initialize builder/buffer with the appropriate size. They will thus never have to be resized. This saves CPU cycles and therefore consumes less energy. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | -| EC28 | Optimize read file exceptions | | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | -| EC5 | Usage of preparedStatement instead of Statement | SQL will only commit the query once, whereas if you used only one statement, it would commit the query every time and thus induce unnecessary calculations by the CPU and therefore superfluous energy consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | -| EC27 | Usage of system.arraycopy to copy arrays | Programs spend most of the time in loops. These can be resource consuming, especially when they integrate heavy processing (IO access). Moreover, the size of the data and processing inside the loops will not allow full use of hardware mechanisms such as the cache or compiler optimization mechanisms. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | -| EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | ✅ | 🚫 | -| EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | -| | Avoid high accuracy geolocation | Avoid using high accuracy geolocation in web applications (linter key : `@ecocode/avoid-high-accuracy-geolocation`) | | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | -| | No import all from library | Should not import all from library (linter key : `@ecocode/no-import-all-from-library`) | | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | -| | Prefer collections with pagination | Prefer API collections with pagination (linter key : `@ecocode/prefer-collections-with-pagination`) | | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | +| Rule key | Name | Description | Reference/Validation | Java | Php | JS | Python | Rust | +|----------|------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------|-----|----|--------|------| +| | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | +| | Non-grouped similar CSS declarations | When multiple Document Object Model (DOM) elements have common CSS properties, declare them together in the same style sheet. This method reduces the weight of CSS. | [cnumr best practices (3rd edition) BP_025](https://github.com/cnumr/best-practices/blob/main/chapters/BP_025_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | +| | CSS shorthand notations not used | Reduces the weight of the style sheet. | [cnumr best practices (3rd edition) BP_026](https://github.com/cnumr/best-practices/blob/main/chapters/BP_026_fr.md) | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| | CSS print not included | This style sheet reduces the number of pages printed. | [cnumr best practices (3rd edition) BP_027](https://github.com/cnumr/best-practices/blob/main/chapters/BP_027_fr.md) | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| | Non-standard fonts used | Prefer standard fonts, as they are already present on the user's computer, so they do not need to download them. This saves bandwidth, while speeding up the display of the site. | [cnumr best practices (3rd edition) BP_029](https://github.com/cnumr/best-practices/blob/main/chapters/BP_029_fr.md) | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| | Non-outsourced CSS and Javascript | If you include CSS or JavaScript code in the body of the HTML file, while the HTML file is used by several pages (or even the entire site), this code must be transferred for each page requested by the user, which increases the volume of data transmitted. | [cnumr best practices (3rd edition) BP_032](https://github.com/cnumr/best-practices/blob/main/chapters/BP_032_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | +| | Resize images browser-side | Do not resize images using the HEIGHT and WIDTH attributes of the HTML code. This approach requires transferring these images to their original size, wasting bandwidth and CPU cycles. | [cnumr best practices (3rd edition) BP_034](https://github.com/cnumr/best-practices/blob/main/chapters/BP_034_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | +| EC10 | Use unoptimized vector images | Less heavy SVG images using less bandwidth | [cnumr best practices (3rd edition) BP_036](https://github.com/cnumr/best-practices/blob/main/chapters/BP_036_fr.md) | 🚧 | 🚀 | 🚀 | ✅ | 🚀 | +| | Using too many CSS/javascript animations | JavaScript/CSS animations can be very expensive in terms of CPU cycles and memory consumption. | [cnumr best practices (3rd edition) BP_039](https://github.com/cnumr/best-practices/blob/main/chapters/BP_039_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | +| | Modify the DOM when traversing it | Modifying the DOM (Document Object Model) as you traverse it can lead to situations where the loop becomes very resource-intensive, especially CPU cycles. | [cnumr best practices (3rd edition) BP_041](https://github.com/cnumr/best-practices/blob/main/chapters/BP_041_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | +| | Edit DOM elements to make it invisible | When an element of the Document Object Model (DOM) needs to be modified by several properties, each change in style or content will generate a repaint or reflow. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | +| | Modify several CSS properties all at once | To limit the number of repaints/reflows, it is recommended not to modify properties one by one. (linter key : `@ecocode/no-multiple-style-changes`) | [cnumr best practices (3rd edition) BP_045](https://github.com/cnumr/best-practices/blob/main/chapters/BP_045_fr.md) | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | +| EC35 | Using try...catch calls (on File Not Found Exception) | When an exception is thrown, a variable (the exception itself) is created in the catch block and destroyed at the end of the block. Creating this variable and destroying it consumes CPU cycles and RAM unnecessarily. That is why it is important not to use this construction and to prefer, as much as possible, a logical test. This new rule replace old EC34 only for a particular use case (FileNotFoundException) | [cnumr best practices (3rd edition) BP_047 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | ✅ | 🚀 | +| EC22 | The use of methods for basic operations | Using methods for basic operations consumes additional system resources. The interpreter must in effect and solve the objects and then the methods, just to carry out these simple operations of the language. | [cnumr best practices (3rd edition) BP_048 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | 🚀 | 🚀 | +| ??? | Call a DOM element multiple times without caching (linter key : `@ecocode/no-multiple-access-dom-element`) | Access to the Document Object Model (DOM) is costly in terms of CPU resources (CPU cycles). Also, when you use the same DOM element from JavaScript multiple times, store its reference in a variable so that you do not go through the DOM again for the same element. | [cnumr best practices (3rd edition) BP_049](https://github.com/cnumr/best-practices/blob/main/chapters/BP_049_fr.md) | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | +| EC4 | Use global variables | When using a global variable, the interpretation engine must check: 1) that it exists in the current scope, in the one above, etc. ; 2) the variable has a value; 3) ... To avoid all these checks, it is often possible to pass the useful variables as arguments of routines, making them local. This process saves computational time (CPU cycles). | [cnumr best practices (3rd edition) BP_050 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | +| EC53 | Using arrays in foreach loops | foreach deduplicates items in a list before starting the enumeration. It is therefore generally more economical to use a simple for loop when you have a good command of the collection. | [cnumr best practices (3rd edition) BP_053 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | 🚀 | 🚀 | 🚫 | 🚀 | +| EC7 | Rewrite native getter/setters | Overloading them lengthens the compilation and execution times of these methods, which are usually much better optimized by the language than by the developer. | [cnumr best practices (3rd edition) BP_062 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | +| EC63 | Unnecessarily assigning values to variables | Avoid declaring and using variables when it is not indis-thinkable. Indeed, each allocation corresponds to the RAM occupied. | [cnumr best practices (3rd edition) BP_063 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | 🚀 | 🚀 | 🚀 | 🚀 | +| EC66 | Use single quote (') instead of quotation mark (") | The shape using the quotation marks allows the developer to insert variables that will be substituted at run time. But if the string does not have a variable, use quotes instead. Thus, language will not look for variables to subtituture, which will reduce the consumption of CPU cycles. | [cnumr best practices (3rd edition) BP_066 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | ✅ | 🚀 | +| EC67 | Use the $i++ variable during an iteration | The $i++ form has the disadvantage of generating a tem-porary variable during incrementation, which is not the case with the ++$i form. | [cnumr best practices (3rd edition) BP_067 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | 🚫 | 🚫 | +| EC69 | Calling a function in the declaration of a for loop | Avoid calling the function each time the loop is iterated. | [cnumr best practices (3rd edition) BP_069 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | +| EC72 | Perform an SQL query inside a loop | Servers are optimized to process multiple selections, insertions, or changes in a single query or transaction. consume CPU cycles, RAM, and bandwidth unnecessarily. | [cnumr best practices (3rd edition) BP_072](https://github.com/cnumr/best-practices/blob/main/chapters/BP_072_fr.md) | ✅ | ✅ | 🚀 | ✅ | 🚀 | +| EC74 | Write SELECT * FROM | The database server must resolve the fields based on the schema. If you are familiar with the diagram, it is strongly recommended to name the fields. | [cnumr best practices (3rd edition) BP_074 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | +| EC1 | Calling a Spring repository inside a loop | The use of Spring repository in a loop induces unnecessary calculations by the CPU and therefore superfluous energy consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | +| EC3 | Getting the size of the collection in the loop | When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power. | | ✅ | ✅ | 🚀 | 🚫 | 🚀 | +| EC2 | Multiple if-else statement | Using too many conditional if-else statements will impact performance since JVM will have to compare the conditions. Prefer using a switch statement instead of multiple if-else if possible, or refactor your code to reduce conditonnal statements on the same variable. Switch statement has a performance advantage over if – else. | | ✅ | ✅ | 🚀 | 🚧 | 🚀 | +| EC76 | Usage of static collections | Avoid usage of static collections. If you want to use static collections make them final and create for example a singleton if needed containing the collections. The static fields are more complicated for the Garbage Collector to manage and can lead to memory leaks. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | +| EC77 | Usage Pattern.compile() in a non-static context | Avoid using Pattern.compile() in a non-static context. This operation requires a non negligible amount of computational power, Using a single match saves CPU cycles and RAM consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | +| EC75 | Concatenate Strings in loop | Don't concatenate Strings in loop. User StringBuilder instead. Strings are immutable so each time you concatenate a String, a new String is created. This is a waste of memory and CPU. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | +| EC78 | Const parameter in batch update | Don't set const parameter in batch update => Put its in query. Creating this parameter and destroying it consumes CPU cycles and RAM unnecessarily. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | +| EC79 | Free resources | try-with-resources Statement needs to be implemented for any object that implements the AutoCloseable interface, it save computer resources. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | +| EC32 | Initialize builder/buffer with the appropriate size | If you know in advance how many characters would be appended, initialize builder/buffer with the appropriate size. They will thus never have to be resized. This saves CPU cycles and therefore consumes less energy. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | +| EC28 | Optimize read file exceptions | | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | +| EC5 | Usage of preparedStatement instead of Statement | SQL will only commit the query once, whereas if you used only one statement, it would commit the query every time and thus induce unnecessary calculations by the CPU and therefore superfluous energy consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | +| EC27 | Usage of system.arraycopy to copy arrays | Programs spend most of the time in loops. These can be resource consuming, especially when they integrate heavy processing (IO access). Moreover, the size of the data and processing inside the loops will not allow full use of hardware mechanisms such as the cache or compiler optimization mechanisms. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | +| EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | ✅ | 🚫 | +| EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | +| | Avoid high accuracy geolocation | Avoid using high accuracy geolocation in web applications (linter key : `@ecocode/avoid-high-accuracy-geolocation`) | | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | +| | No import all from library | Should not import all from library (linter key : `@ecocode/no-import-all-from-library`) | | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | +| | Prefer collections with pagination | Prefer API collections with pagination (linter key : `@ecocode/prefer-collections-with-pagination`) | | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index 81977d3d7..8c5b0459d 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -64,7 +64,7 @@ ASCIIDOC format is used with custom features such as : - syntax highlighting (see code blocks on ASCIIDOC rules) - inclusions (see: php/EC74.asciidoc) - - table data generation from CSV (see: php/EC34.asciidoc) + - table data generation from CSV (see: php/EC35.asciidoc) --> org.asciidoctor asciidoctor-maven-plugin diff --git a/ecocode-rules-specifications/src/main/rules/EC34/EC34.json b/ecocode-rules-specifications/src/main/rules/EC35/EC35.json similarity index 73% rename from ecocode-rules-specifications/src/main/rules/EC34/EC34.json rename to ecocode-rules-specifications/src/main/rules/EC35/EC35.json index 00bc65320..719ac25d2 100644 --- a/ecocode-rules-specifications/src/main/rules/EC34/EC34.json +++ b/ecocode-rules-specifications/src/main/rules/EC35/EC35.json @@ -1,5 +1,5 @@ { - "title": "Avoid using try-catch statement", + "title": "Avoid using try-catch statement with file open operation in try block", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/ecocode-rules-specifications/src/main/rules/EC34/php/1GB.etsdiff.csv b/ecocode-rules-specifications/src/main/rules/EC35/php/1GB.etsdiff.csv similarity index 100% rename from ecocode-rules-specifications/src/main/rules/EC34/php/1GB.etsdiff.csv rename to ecocode-rules-specifications/src/main/rules/EC35/php/1GB.etsdiff.csv diff --git a/ecocode-rules-specifications/src/main/rules/EC34/php/EC34.asciidoc b/ecocode-rules-specifications/src/main/rules/EC35/php/EC35.asciidoc similarity index 88% rename from ecocode-rules-specifications/src/main/rules/EC34/php/EC34.asciidoc rename to ecocode-rules-specifications/src/main/rules/EC35/php/EC35.asciidoc index f7b2ab71d..60f1e8c0b 100644 --- a/ecocode-rules-specifications/src/main/rules/EC34/php/EC34.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC35/php/EC35.asciidoc @@ -1,4 +1,5 @@ -Inside complex code parts (for exemple multiple loops, complex data constructions...), avoid using try...catch...finally. +Inside complex code parts (for exemple multiple loops, complex data constructions...), avoid using try...catch. +For the moment, this rule only deals with "file open" use case in try...catch blocks. When an exception is thrown, a variable (the exception itself) is created in a catch block and it's destruction consumes unnecessary CPU cycles and RAM. Prefer using logical tests in this cases. diff --git a/ecocode-rules-specifications/src/main/rules/EC34/python/EC34.asciidoc b/ecocode-rules-specifications/src/main/rules/EC35/python/EC35.asciidoc similarity index 79% rename from ecocode-rules-specifications/src/main/rules/EC34/python/EC34.asciidoc rename to ecocode-rules-specifications/src/main/rules/EC35/python/EC35.asciidoc index 14b3c3c5b..08fc1f95f 100644 --- a/ecocode-rules-specifications/src/main/rules/EC34/python/EC34.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC35/python/EC35.asciidoc @@ -1,4 +1,5 @@ -Inside complex code parts (for example multiple loops, complex data constructions...), avoid using try...catch...finally. +Inside complex code parts (for example multiple loops, complex data constructions...), avoid using try...catch. +For the moment, this rule only deals with "file open" use case in try...catch blocks. When an exception is thrown, a variable (the exception itself) is created in a catch block, and it's destruction consumes unnecessary CPU cycles and RAM. Prefer using logical tests in this cases. From cba9523db0dc9a77f069835294c4a9ab150d1d30 Mon Sep 17 00:00:00 2001 From: jycr Date: Thu, 29 Jun 2023 19:40:56 +0200 Subject: [PATCH 07/32] chore: Adds rule specifications from: https://github.com/green-code-initiative/ecoCode-android/tree/e5960e0c03566d33d9bec051103b7ff36879301b/android-plugin/src/main/resources/org/sonar/l10n/java/rules/squid --- .../src/main/rules-mobile/EBAT002_java.html | 24 +++++++++++++ .../src/main/rules-mobile/EBAT002_java.json | 17 +++++++++ .../src/main/rules-mobile/EBAT003_java.html | 28 +++++++++++++++ .../src/main/rules-mobile/EBAT003_java.json | 17 +++++++++ .../src/main/rules-mobile/EBOT001_java.html | 12 +++++++ .../src/main/rules-mobile/EBOT001_java.json | 17 +++++++++ .../src/main/rules-mobile/EBOT002_java.html | 19 ++++++++++ .../src/main/rules-mobile/EBOT002_java.json | 17 +++++++++ .../src/main/rules-mobile/EBOT003_java.html | 19 ++++++++++ .../src/main/rules-mobile/EBOT003_java.json | 17 +++++++++ .../src/main/rules-mobile/EIDL001_java.html | 9 +++++ .../src/main/rules-mobile/EIDL001_java.json | 17 +++++++++ .../src/main/rules-mobile/EIDL002_java.html | 9 +++++ .../src/main/rules-mobile/EIDL002_java.json | 17 +++++++++ .../src/main/rules-mobile/EIDL004_java.html | 11 ++++++ .../src/main/rules-mobile/EIDL004_java.json | 17 +++++++++ .../src/main/rules-mobile/EIDL006_java.html | 16 +++++++++ .../src/main/rules-mobile/EIDL006_java.json | 17 +++++++++ .../src/main/rules-mobile/EIDL007_java.html | 35 +++++++++++++++++++ .../src/main/rules-mobile/EIDL007_java.json | 17 +++++++++ .../src/main/rules-mobile/EIDL008_java.html | 17 +++++++++ .../src/main/rules-mobile/EIDL008_java.json | 17 +++++++++ .../src/main/rules-mobile/EIDL009_java.html | 19 ++++++++++ .../src/main/rules-mobile/EIDL009_java.json | 17 +++++++++ .../src/main/rules-mobile/ELEA002_java.html | 17 +++++++++ .../src/main/rules-mobile/ELEA002_java.json | 17 +++++++++ .../src/main/rules-mobile/ELEA003_java.html | 19 ++++++++++ .../src/main/rules-mobile/ELEA003_java.json | 17 +++++++++ .../src/main/rules-mobile/ELEA004_java.html | 21 +++++++++++ .../src/main/rules-mobile/ELEA004_java.json | 17 +++++++++ .../src/main/rules-mobile/ELEA005_java.html | 15 ++++++++ .../src/main/rules-mobile/ELEA005_java.json | 17 +++++++++ .../src/main/rules-mobile/ELEA006_java.html | 15 ++++++++ .../src/main/rules-mobile/ELEA006_java.json | 17 +++++++++ .../src/main/rules-mobile/EOPT001_java.html | 17 +++++++++ .../src/main/rules-mobile/EOPT001_java.json | 17 +++++++++ .../src/main/rules-mobile/EOPT002_java.html | 16 +++++++++ .../src/main/rules-mobile/EOPT002_java.json | 17 +++++++++ .../src/main/rules-mobile/EPOW004_java.html | 23 ++++++++++++ .../src/main/rules-mobile/EPOW004_java.json | 17 +++++++++ .../src/main/rules-mobile/EPOW006_java.html | 22 ++++++++++++ .../src/main/rules-mobile/EPOW006_java.json | 17 +++++++++ .../src/main/rules-mobile/ESOB001_java.html | 16 +++++++++ .../src/main/rules-mobile/ESOB001_java.json | 17 +++++++++ .../src/main/rules-mobile/ESOB002_java.html | 10 ++++++ .../src/main/rules-mobile/ESOB002_java.json | 17 +++++++++ .../src/main/rules-mobile/ESOB005_java.html | 21 +++++++++++ .../src/main/rules-mobile/ESOB005_java.json | 17 +++++++++ .../src/main/rules-mobile/ESOB006_java.html | 23 ++++++++++++ .../src/main/rules-mobile/ESOB006_java.json | 17 +++++++++ .../src/main/rules-mobile/ESOB007_java.html | 18 ++++++++++ .../src/main/rules-mobile/ESOB007_java.json | 17 +++++++++ .../src/main/rules-mobile/ESOB008_java.html | 18 ++++++++++ .../src/main/rules-mobile/ESOB008_java.json | 17 +++++++++ .../src/main/rules-mobile/ESOB010_java.html | 21 +++++++++++ .../src/main/rules-mobile/ESOB010_java.json | 17 +++++++++ .../src/main/rules-mobile/ESOB011_java.html | 16 +++++++++ .../src/main/rules-mobile/ESOB011_java.json | 17 +++++++++ .../src/main/rules-mobile/ESOB012_java.html | 28 +++++++++++++++ .../src/main/rules-mobile/ESOB012_java.json | 17 +++++++++ .../src/main/rules-mobile/ESOB013_java.html | 14 ++++++++ .../src/main/rules-mobile/ESOB013_java.json | 17 +++++++++ .../src/main/rules-mobile/ESOB014_java.html | 14 ++++++++ .../src/main/rules-mobile/ESOB014_java.json | 17 +++++++++ .../src/main/rules-mobile/SPRI002_java.html | 13 +++++++ .../src/main/rules-mobile/SPRI002_java.json | 17 +++++++++ 66 files changed, 1156 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EBAT002_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EBAT002_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EBAT003_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EBAT003_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EBOT001_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EBOT001_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EBOT002_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EBOT002_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EBOT003_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EBOT003_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL001_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL001_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL002_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL002_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL004_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL004_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL006_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL006_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL007_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL007_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL008_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL008_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL009_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EIDL009_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ELEA002_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ELEA002_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ELEA003_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ELEA003_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ELEA004_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ELEA004_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ELEA005_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ELEA005_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ELEA006_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ELEA006_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EOPT001_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EOPT001_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EOPT002_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EOPT002_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EPOW004_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EPOW004_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EPOW006_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/EPOW006_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB001_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB001_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB002_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB002_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB005_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB005_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB006_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB006_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB007_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB007_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB008_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB008_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB010_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB010_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB011_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB011_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB012_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB012_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB013_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB013_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB014_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ESOB014_java.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/SPRI002_java.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/SPRI002_java.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBAT002_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EBAT002_java.html new file mode 100644 index 000000000..fea8c55c4 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EBAT002_java.html @@ -0,0 +1,24 @@ + +

With SensorManager#registerListener(SensorEventListener, Sensor, int) the events are delivered as soon as possible.
+ Instead, SensorManager#registerListener(SensorEventListener, Sensor, int, int maxReportLatencyUs)< + allows events to stay temporarily in the hardware FIFO (queue) before being delivered. + The events can be stored in the hardware FIFO up to maxReportLatencyUs microseconds.
+ Once one of the events in the FIFO needs to be reported, all the events in the FIFO are reported sequentially. + Setting maxReportLatencyUs to a positive value allows to reduce the number of interrupts the AP (Application Processor) receives, + hence reducing power consumption, as the AP can switch to a lower power state while the sensor is capturing the data.

+

Noncompliant Code Example

+
+    SensorEventListener sensorEventListener;
+    SensorManager sensorManager;
+    Sensor sensor;
+
+    sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
+
+

Compliant Code Example

+
+    SensorEventListener sensorEventListener;
+    SensorManager sensorManager;
+    Sensor sensor;
+
+    sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL, 200000);
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBAT002_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EBAT002_java.json new file mode 100644 index 000000000..80d544305 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EBAT002_java.json @@ -0,0 +1,17 @@ +{ + "title": "Batch: Sensor Coalesce", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "batch", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Minor" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBAT003_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EBAT003_java.html new file mode 100644 index 000000000..102d8c225 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EBAT003_java.html @@ -0,0 +1,28 @@ + +

The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the Job Scheduler class.
+ Compared to a custom Sync Adapter or the alarm manager, the Job Scheduler supports batch scheduling of jobs.
+ The Android system can combine jobs so that battery consumption is reduced.

+

Noncompliant Code Example

+
+    void setAlarm(Context context)
+    {
+        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
+        Intent intent = new Intent(context, Alarm.class);
+        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
+        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pendingIntent);
+    }
+
+    Alarm alarm = new Alarm();
+    alarm.setAlarm(this);
+
+

Compliant Code Example

+
+    JobInfo info = new JobInfo.Builder(123, COMPONENT_NAME)
+                .setRequiresCharging(true)
+                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
+                .setPersisted(true)
+                .setPeriodic(10 * 60 * 1000)
+                .build();
+    JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
+    scheduler.schedule(info);
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBAT003_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EBAT003_java.json new file mode 100644 index 000000000..7f7de57cb --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EBAT003_java.json @@ -0,0 +1,17 @@ +{ + "title": "Batch: Job Coalesce", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "3h" + }, + "tags": [ + "batch", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Minor" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBOT001_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EBOT001_java.html new file mode 100644 index 000000000..d21c7998f --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EBOT001_java.html @@ -0,0 +1,12 @@ + +

Opening and closing internet connection continuously is extremely battery-inefficient since HTTP exchange is the most + consuming operation of the network. This bug typically occurs when one obtain a new HttpURLConnection + by calling URL#openConnection() within a loop control structure (while, for, do-while, for-each).
+ Also, this bad practice must be early prevented because it is the root of another evil that consists in polling + data at regular intervals, instead of using push notifications to save a lot of battery power.

+

Noncompliant Code Example

+
+    for (int val : myArray) {
+        URL.openConnection();
+    }
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBOT001_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EBOT001_java.json new file mode 100644 index 000000000..4fcc4a827 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EBOT001_java.json @@ -0,0 +1,17 @@ +{ + "title": "Bottleneck: Internet In The Loop", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "3h" + }, + "tags": [ + "optimized-api", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Critical" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBOT002_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EBOT002_java.html new file mode 100644 index 000000000..117273dad --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EBOT002_java.html @@ -0,0 +1,19 @@ + +

Normally the Wi-Fi stack filters out packets not explicitly addressed to the device. Acquiring a Multicast Lock with + WifiManager.MulticastLock.acquire() will cause the stack to receive packets addressed to multicast + addresses.

+

Processing these extra packets can cause a noticeable battery drain and must be disabled when not needed + with to a call to WifiManager.MulticastLock.release().

+

Noncompliant Code Example

+
+    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
+    WifiManager.MulticastLock lock = wifiManager.createMulticastLock("tag");
+    lock.acquire();
+
+

Compliant Code Example

+
+    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
+    WifiManager.MulticastLock lock = wifiManager.createMulticastLock("tag");
+    lock.acquire();
+    lock.release()
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBOT002_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EBOT002_java.json new file mode 100644 index 000000000..6aa9f5834 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EBOT002_java.json @@ -0,0 +1,17 @@ +{ + "title": "Bottleneck: Wifi Multicast Lock", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "bottleneck", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Minor" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBOT003_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EBOT003_java.html new file mode 100644 index 000000000..d088954fa --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EBOT003_java.html @@ -0,0 +1,19 @@ + +

Transmitting a file over a network infrastructure without compressing + it consumes more energy than with compression. More precisely, + energy efficiency is improved in case the data is compressed at least by 10%, + transmitted and decompressed at the other network node. From the Android client side, + it means making a post HTTP request using a GZIPOutputStream + instead of the classical OutputStream, along with the HttpURLConnection object.

+

Noncompliant Code Example

+
+    Url url = new Url("https://www.ecocode.io/");
+    HttpsUrlConnection con = (HttpsURLConnection) url.openConnection();
+    OutputStream stream = con.getOutputStream();
+
+

Compliant Code Example

+
+    Url url = new Url("https://www.ecocode.io/");
+    HttpsUrlConnection con = (HttpsURLConnection) url.openConnection();
+    OutputStream stream = new GZIPOutputStream(con.getOutputStream());
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBOT003_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EBOT003_java.json new file mode 100644 index 000000000..48d683cfb --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EBOT003_java.json @@ -0,0 +1,17 @@ +{ + "title": "Bottleneck: Uncompressed Data Transmission", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + "bottleneck", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Minor" +} diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL001_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EIDL001_java.html new file mode 100644 index 000000000..649c017ed --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL001_java.html @@ -0,0 +1,9 @@ + +

To avoid draining the battery, an Android device that is left idle quickly falls asleep.
+ Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically + use the FLAG_KEEP_SCREEN_ON in their activity. Another way to implement this is in their application's + layout XML file, by using the android:keepScreenOn attribute.

+

Noncompliant Code Example

+
+    getWindow().addFlags(FLAG_KEEP_SCREEN_ON);
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL001_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EIDL001_java.json new file mode 100644 index 000000000..f34b5b45d --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL001_java.json @@ -0,0 +1,17 @@ +{ + "title": "Idleness: Keep Screen On (addFlags)", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "idleness", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL002_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EIDL002_java.html new file mode 100644 index 000000000..b3b5c4cbe --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL002_java.html @@ -0,0 +1,9 @@ + +

To avoid draining the battery, an Android device that is left idle quickly falls asleep.
+ Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically + use the FLAG_KEEP_SCREEN_ON in their activity. Another way to implement this is in their application's + layout XML file, by using the android:keepScreenOn attribute.

+

Noncompliant Code Example

+
+    getWindow().setFlags(FLAG_KEEP_SCREEN_ON, FLAG_KEEP_SCREEN_ON);
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL002_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EIDL002_java.json new file mode 100644 index 000000000..0c7b6ccfb --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL002_java.json @@ -0,0 +1,17 @@ +{ + "title": "Idleness: Keep Screen On (setFlags)", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "idleness", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL004_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EIDL004_java.html new file mode 100644 index 000000000..5c3caefdd --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL004_java.html @@ -0,0 +1,11 @@ + +

To avoid draining the battery, an Android device that is left idle quickly falls asleep. + Hence, keeping the CPU on should be avoided, unless it is absolutely necessary.
+ If so, developers typically use a Power Manager system service feature called wake locks + by invoking PowerManager.WakeLock#newWakeLock(int levelAndFlags, String tag), + along with the specific permission WAKE_LOCK in their manifest.

+

Noncompliant Code Example

+
+    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
+    WakeLock manager = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG");
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL004_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EIDL004_java.json new file mode 100644 index 000000000..fdbf87a37 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL004_java.json @@ -0,0 +1,17 @@ +{ + "title": "Idleness: Keep Cpu On", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "1h" + }, + "tags": [ + "idleness", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL006_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EIDL006_java.html new file mode 100644 index 000000000..0ee9f1537 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL006_java.html @@ -0,0 +1,16 @@ + +

A wake lock is a mechanism to indicate that your application needs to have the device stay on. + The general principle is to obtain a wake lock, acquire it and finally release it.

+

Hence, the challenge here is to release the lock as soon as possible to avoid running down the device's + battery excessively. Missing call to PowerManager#release() is a built-in check of Android + lint (Wakelock check) but that does not prevent abuse of the lock over too long a period of time.
+ This can be avoided by a call to PowerManager#acquire(long timeout) instead of + PowerManager#acquire(), because the lock will be released for sure after the given timeout expires.

+

Noncompliant Code Example

+
+    PowerManager.acquire()
+
+

Compliant Code Example

+
+    PowerManager.acquire(long timeout)
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL006_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EIDL006_java.json new file mode 100644 index 000000000..c435d3726 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL006_java.json @@ -0,0 +1,17 @@ +{ + "title": "Idleness: Durable Wake Lock", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + "idleness", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL007_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EIDL007_java.html new file mode 100644 index 000000000..d0ce4489f --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL007_java.html @@ -0,0 +1,35 @@ + +

Applications are strongly discouraged from using exact alarms unnecessarily as they reduce the OS's + ability to minimize battery use (i.e. Doze Mode).

+

For most apps prior to API 19, setInexactRepeating() is preferable over + setRepeating(). When you use this method, Android synchronizes multiple inexact + repeating alarms and fires them at the same time, thus reducing the battery drain.

+

Similarly after API 19, setExact() and setExactAndAllowWhileIdle() + can significantly impact the power use of the device when idle, so they should be used with + care. High-frequency alarms are also bad for battery life but this is already checked by Android + lint (ShortAlarm built-in check).

+

Noncompliant Code Example

+
+    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
+    alarmManager.setRepeating(alarmType, triggerAtMillis, intervalMillis, operation);
+
+or +
+    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
+    alarmManager.setExact(type,triggerAtMillis,operation);
+
+or +
+    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
+    alarmManager.setExact(type,triggerAtMillis,tag,listener,targetHandler);
+
+or +
+    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
+    alarmManager.setExactAndAllowWhileIdle(type,triggerAtMilllis,operation);
+
+

Compliant Code Example

+
+    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
+    alarmManager.setInexactRepeating(alarmType, triggerAtMillis, intervalMillis, operation);
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL007_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EIDL007_java.json new file mode 100644 index 000000000..6c58cefd5 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL007_java.json @@ -0,0 +1,17 @@ +{ + "title": "Idleness: Rigid Alarm", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "1d" + }, + "tags": [ + "idleness", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL008_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EIDL008_java.html new file mode 100644 index 000000000..9b791f2c8 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL008_java.html @@ -0,0 +1,17 @@ + +

For developers wishing to display OpenGL rendering, when choosing the rendering mode + with GLSurfaceView#setRenderMode(int renderMode), + using RENDERMODE_WHEN_DIRTY instead of RENDERMODE_CONTINUOUSLY + (by default) can improve battery life and overall system performance by allowing the GPU and CPU + to idle when the view does not need to be updated.

+

Noncompliant Code Example

+
+     GLSurfaceView surfaceView = new GLSurfaceView(this);
+     surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY)
+
+
+

Compliant Code Example

+
+     GLSurfaceView surfaceView = new GLSurfaceView(this);
+     surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY)
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL008_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EIDL008_java.json new file mode 100644 index 000000000..b776395f4 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL008_java.json @@ -0,0 +1,17 @@ +{ + "title": "Idleness: Continuous Rendering", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + "idleness", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Minor" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL009_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EIDL009_java.html new file mode 100644 index 000000000..235510706 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL009_java.html @@ -0,0 +1,19 @@ + +

During a voice interaction session, VoiceInteractionSession#setKeepAwake(boolean keepAwake) allows to decide whether it will keep the + device awake while it is running a voice activity.

+

By default, the system holds a wake lock for it while in this state, so that it can work even if the screen is off. Setting this to + false removes that wake lock, allowing the CPU to go to sleep and hence does not let this continue to drain the battery.

+

Noncompliant Code Example

+
+    VoiceInteractionSession voiceSession = new VoiceInteractionSession(this);
+
+OR +
+    VoiceInteractionSession voiceSession = new VoiceInteractionSession(this);
+    voiceSession.setKeepAwake(true);
+
+

Compliant Code Example

+
+    VoiceInteractionSession voiceSession = new VoiceInteractionSession(this);
+    voiceSession.setKeepAwake(false);
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL009_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EIDL009_java.json new file mode 100644 index 000000000..23099bc7c --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EIDL009_java.json @@ -0,0 +1,17 @@ +{ + "title": "Idleness: Keep Voice Awake", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + "idleness", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA002_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ELEA002_java.html new file mode 100644 index 000000000..a3f60e424 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ELEA002_java.html @@ -0,0 +1,17 @@ + +

Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. + In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS).

+

The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor + unnecessarily process data when the app enters an idle state, typically when paused or stopped.
+ Consequently, calls must be carefully pairwised: Camera.open()/Camera.release(). + Failing to do so can drain the battery in just a few hours.

+

Noncompliant Code Example

+
+    Camera camera = Camera.open();
+
+

Compliant Code Example

+
+    Camera camera = Camera.open();
+    camera.release();
+
+ diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA002_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ELEA002_java.json new file mode 100644 index 000000000..669956a00 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ELEA002_java.json @@ -0,0 +1,17 @@ +{ + "title": "Leakage: Camera Leak", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "leakage", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Critical" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA003_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ELEA003_java.html new file mode 100644 index 000000000..0693ffcc4 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ELEA003_java.html @@ -0,0 +1,19 @@ + +

Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. + In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS).

+

The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor + unnecessarily process data when the app enters an idle state, typically when paused or stopped.
+ Consequently, calls must be carefully pairwised: LocationManager.requestLocationUpdates()/removeUpdates().
+ Failing to do so can drain the battery in just a few hours.

+

Noncompliant Code Example

+
+    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
+    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 1.0f, this);
+
+

Compliant Code Example

+
+    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
+    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 1.0f, this);
+    locationManager.removeUpdates(this);
+
+ diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA003_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ELEA003_java.json new file mode 100644 index 000000000..859c07503 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ELEA003_java.json @@ -0,0 +1,17 @@ +{ + "title": "Leakage: Location Leak", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "leakage", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Minor" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA004_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ELEA004_java.html new file mode 100644 index 000000000..14963a543 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ELEA004_java.html @@ -0,0 +1,21 @@ + +

Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. + In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS).

+

The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor + unnecessarily process data when the app enters an idle state, typically when paused or stopped.
+ Consequently, calls must be carefully pairwised: SensorManager#registerListener()/unregisterListener(). + Failing to do so can drain the battery in just a few hours.

+

Noncompliant Code Example

+
+    SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
+    Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
+    sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
+
+

Compliant Code Example

+
+    SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
+    Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
+    sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
+    sManager.unregisterListener(this);
+
+ diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA004_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ELEA004_java.json new file mode 100644 index 000000000..1074ec034 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ELEA004_java.json @@ -0,0 +1,17 @@ +{ + "title": "Leakage: SensorManager Leak", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "leakage", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA005_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ELEA005_java.html new file mode 100644 index 000000000..a3969a8f7 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ELEA005_java.html @@ -0,0 +1,15 @@ + +

Creation of a Media Recorder object with new MediaRecorder() is used to record + audio and video. Class own a release() method.

+

In addition to unnecessary resources (such as memory and instances of codecs) being held, + failure to call this method immediately if a media object is no longer needed may also lead to + continuous battery consumption for mobile devices.

+

Noncompliant Code Example

+
+    MediaRecorder mr = new MediaRecorder();
+
+

Compliant Solution

+
+    MediaRecorder mr = new MediaRecorder();
+    mr.release();
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA005_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ELEA005_java.json new file mode 100644 index 000000000..ead6d1e38 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ELEA005_java.json @@ -0,0 +1,17 @@ +{ + "title": "Leakage: Media Leak (MediaRecorder)", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "leakage", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA006_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ELEA006_java.html new file mode 100644 index 000000000..729b4cf3a --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ELEA006_java.html @@ -0,0 +1,15 @@ + +

Creation of a Media Player object with new MediaPlayer() can + be used to control playback of audio/video files and streams. Class own arelease() method.

+

In addition to unnecessary resources (such as memory and instances of codecs) being held, + failure to call this method immediately if a media object is no longer needed may also lead to + continuous battery consumption for mobile devices.

+

Noncompliant Code Example

+
+    MediaPlayer mp = new MediaPlayer();
+
+

Compliant Solution

+
+    MediaPlayer mp = new MediaPlayer();
+    mp.release();
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA006_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ELEA006_java.json new file mode 100644 index 000000000..46339b64a --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ELEA006_java.json @@ -0,0 +1,17 @@ +{ + "title": "Leakage: Media Leak (MediaPlayer)", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "leakage", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EOPT001_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EOPT001_java.html new file mode 100644 index 000000000..876fad0aa --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EOPT001_java.html @@ -0,0 +1,17 @@ + +

The fused location provider is one of the location APIs in Google Play + services which combines signals from GPS, Wi-Fi, and cell networks, as well + as accelerometer, gyroscope, magnetometer and other sensors. It is officially + recommended to maximize battery life.
+ Thus, developer has to set up Google Play Service in her gradle file with a dependency to + com.google.android.gms:play-services-location:x.y.z, and then to + import from com.google.android.gms.location instead of the + android.location package of the SDK.

+

Noncompliant Code Example

+
+    import android.location.*;
+
+

Compliant Solution

+
+    import com.google.android.gms.location.*;
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EOPT001_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EOPT001_java.json new file mode 100644 index 000000000..360c92cea --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EOPT001_java.json @@ -0,0 +1,17 @@ +{ + "title": "Optimized API: Fused Location", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "optimized-api", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EOPT002_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EOPT002_java.html new file mode 100644 index 000000000..4af3331ea --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EOPT002_java.html @@ -0,0 +1,16 @@ + +

In contrast to classic Bluetooth, Bluetooth Low Energy (BLE) + is designed to provide significantly lower power consumption. + Its purpose is to save energy on both paired devices but very few + developers are aware of this alternative API. + From the Android client side, it means append android.bluetooth.le.* + imports to android.bluetooth.* imports in order to benefits from + low-energy features.

+

Noncompliant Code Example

+
+    import android.bluetooth.*
+
+

Compliant Solution

+
+    import android.bluetooth.le.*
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EOPT002_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EOPT002_java.json new file mode 100644 index 000000000..75dbedabc --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EOPT002_java.json @@ -0,0 +1,17 @@ +{ + "title": "Optimized API: Bluetooth Low-Energy", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "optimized-api", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EPOW004_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EPOW004_java.html new file mode 100644 index 000000000..5199273a9 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EPOW004_java.html @@ -0,0 +1,23 @@ + +

It's always good that an app has different behavior when device is connected/disconnected + to a power station, or has different battery levels. + One can monitor the changes in charging state with a broadcast receiver registered + on the actions ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED, + or monitor significant changes in battery level with a broadcast receiver registered + on the actions BATTERY_LOW and BATTERY_OKAY.

+

Compliant Code Example

+
+    new IntentFilter(Intent.ACTION_POWER_CONNECTED)
+
+OR +
+    new IntentFilter(Intent.ACTION_POWER_DISCONNECTED)
+
+OR +
+    new IntentFilter(Intent.ACTION_POWER_BATTERY_OKAY)
+
+OR +
+    new IntentFilter(Intent.ACTION_POWER_BATTERY_LOW)
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EPOW004_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EPOW004_java.json new file mode 100644 index 000000000..7c63da47f --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EPOW004_java.json @@ -0,0 +1,17 @@ +{ + "title": "Power: Charge Awareness", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "0min" + }, + "tags": [ + "power", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Info" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EPOW006_java.html b/ecocode-rules-specifications/src/main/rules-mobile/EPOW006_java.html new file mode 100644 index 000000000..50f1de3d4 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EPOW006_java.html @@ -0,0 +1,22 @@ + +

Taking into account when the device is entering or exiting the power save mode is higly desirable for the battery + life. + It implies the existence of a broadcast receiver registered on the action + ACTION_POWER_SAVE_MODE_CHANGED, + or programmaticaly with a call to PowerManager#isPowerSaveMode().

+

Compliant Code Example

+
+    new IntentFilter(Intent.ACTION_POWER_SAVE_MODE_CHANGED)
+
+ OR +
+    intentFilter.addAction(ACTION_POWER_SAVE_MODE_CHANGED)
+
+ OR +
+    IntentFilter.create(ACTION_POWER_SAVE_MODE_CHANGED,"")
+
+ OR +
+    powerManager.isPowerSaveMode()
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EPOW006_java.json b/ecocode-rules-specifications/src/main/rules-mobile/EPOW006_java.json new file mode 100644 index 000000000..8ec94302d --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/EPOW006_java.json @@ -0,0 +1,17 @@ +{ + "title": "Power: Save Mode Awareness", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "0min" + }, + "tags": [ + "power", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Info" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB001_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ESOB001_java.html new file mode 100644 index 000000000..ef2b69305 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB001_java.html @@ -0,0 +1,16 @@ + +

The rotation vector sensor is the most frequently used sensor for motion detection and monitoring. When using + SensorManager#getDefaultSensor(int type), always prefer the + constant TYPE_GEOMAGNETIC_ROTATION_VECTOR which is similar to TYPE_ROTATION_VECTOR, + but using a magnetometer instead of using a gyroscope. This sensor uses lower power than the other rotation vectors, + because it doesn't use the gyroscope. However, it is more noisy and will work best outdoors.

+

Noncompliant Code Example

+
+    SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
+    sManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
+
+

Compliant Code Example

+
+    SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
+    sManager.getDefaultSensor(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR);
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB001_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ESOB001_java.json new file mode 100644 index 000000000..ea4b1caa6 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB001_java.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: Thrifty Motion Sensor", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Minor" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB002_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ESOB002_java.html new file mode 100644 index 000000000..bc3ea4794 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB002_java.html @@ -0,0 +1,10 @@ + +

Introduced in Android 9, the adaptive brightness feature raises or lower the brightness of the screen depending on + the light in the current environment.
+ For some reasons, developers may disable this feature programmatically, setting the field + WindowManager.LayoutParams#screenBrightness with the constant BRIGHTNESS_OVERRIDE_FULL.
+ This feature was introduced to improve battery life, be careful when deactivating it.

+

Noncompliant Code Example

+
+    getWindow().getAttributes().screenBrightness = BRIGHTNESS_OVERRIDE_FULL;
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB002_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ESOB002_java.json new file mode 100644 index 000000000..9317b2968 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB002_java.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: Brightness Override", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Minor" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB005_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ESOB005_java.html new file mode 100644 index 000000000..77a14aee7 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB005_java.html @@ -0,0 +1,21 @@ + +

With a call to + LocationManager#requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener), + the location provider will only send your application an update when the location has changed by at + least minDistance meters AND at least minTime milliseconds have passed. So minTime should + be the primary tool to conserve battery life, and, to a lesser extent, minDistance.
+ These two must imperatively be greater than 0.

+

Noncompliant Code Example

+
+    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
+                0,
+                0,
+                this);
+
+

Compliant Code Example

+
+    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
+                60000L,  // refresh location at least each 60000ms
+                10.0f,   // refresh location at least each 10 meters
+                this);
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB005_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ESOB005_java.json new file mode 100644 index 000000000..4ac87ecda --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB005_java.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: Thrifty Geolocation (minTime)", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB006_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ESOB006_java.html new file mode 100644 index 000000000..d540d3f1e --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB006_java.html @@ -0,0 +1,23 @@ + +

Location awareness is one of the most popular features used by apps. + An important point is to try to get the best possible provider + (LocationManager#getBestProvider()) based on an energy criteria + thanks to Criteria#setPowerRequirement(int level), using constant + POWER_LOW instead of POWER_HIGH or + POWER_MEDIUM.

+

Noncompliant Code Example

+
+    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
+                1000L,
+                1.0f,
+                this);
+
+

Compliant Code Example

+
+    Criteria criteria = new Criteria();
+    criteria.setPowerRequirement(Criteria.POWER_LOW);
+    locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria,true),
+                1000L,
+                1.0f,
+                this);
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB006_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ESOB006_java.json new file mode 100644 index 000000000..ec09a4972 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB006_java.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: Thrifty Geolocation Criteria", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB007_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ESOB007_java.html new file mode 100644 index 000000000..f07b4c6ca --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB007_java.html @@ -0,0 +1,18 @@ + +

With Bluetooth Low Energy technology, a Bluetooth Smart Ready device (the master) + will establish a link with a Bluetooth Smart device (the slave). Most often, + the slave is a GATT server and the master is a GATT client. GATT capable + devices can be discovered using BLE scan process.

+

The default and preferred advertising mode is ADVERTISE_MODE_LOW_POWER + when calling AdvertiseSettings.Builder#setAdvertiseMode(int advertiseMode). +

+

Noncompliant Code Example

+
+    AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
+    builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY);
+
+

Compliant Code Example

+
+    AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
+    builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER);
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB007_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ESOB007_java.json new file mode 100644 index 000000000..9f649544d --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB007_java.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: Thrifty Bluetooth Low Energy (setAdvertiseMode)", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB008_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ESOB008_java.html new file mode 100644 index 000000000..9df3ab6e9 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB008_java.html @@ -0,0 +1,18 @@ + +

With Bluetooth Low Energy technology (see BLE API smell), a Bluetooth Smart Ready + device (the master) will establish a link with a Bluetooth Smart device (the slave). + Most often, the slave is a GATT server and the master is a GATT client. GATT capable + devices can be discovered using BLE scan process.

+

Invoking BluetoothGatt#requestConnectionPriority(int connectionPriority) + with the value CONNECTION_PRIORITY_LOW_POWER, is recommended to reduce + power consumption.

+

Noncompliant Code Example

+
+    BluetoothGatt gatt = new BluetoothGatt();
+    gatt.requestConnectionPriority(CONNECTION_PRIORITY_HIGH);
+
+

Compliant Code Example

+
+    BluetoothGatt gatt = new BluetoothGatt();
+    gatt.requestConnectionPriority(CONNECTION_PRIORITY_LOW_POWER);
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB008_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ESOB008_java.json new file mode 100644 index 000000000..fe89d2f7d --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB008_java.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: Thrifty Bluetooth Low Energy (requestConnectionPriority)", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB010_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ESOB010_java.html new file mode 100644 index 000000000..77a14aee7 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB010_java.html @@ -0,0 +1,21 @@ + +

With a call to + LocationManager#requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener), + the location provider will only send your application an update when the location has changed by at + least minDistance meters AND at least minTime milliseconds have passed. So minTime should + be the primary tool to conserve battery life, and, to a lesser extent, minDistance.
+ These two must imperatively be greater than 0.

+

Noncompliant Code Example

+
+    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
+                0,
+                0,
+                this);
+
+

Compliant Code Example

+
+    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
+                60000L,  // refresh location at least each 60000ms
+                10.0f,   // refresh location at least each 10 meters
+                this);
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB010_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ESOB010_java.json new file mode 100644 index 000000000..e2a6c6d60 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB010_java.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: Thrifty Geolocation (minDistance)", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Minor" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB011_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ESOB011_java.html new file mode 100644 index 000000000..1b7ae5a56 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB011_java.html @@ -0,0 +1,16 @@ + +

Shaking of an Android device is possible in all circumstances with a call to getSystemService(Context.VIBRATOR_SERVICE) (API 26) or + getSystemService(Context.VIBRATOR.MANAGER.SERVICE) (API 31). + Behind this effect stands a specific miniature hardware component, motor or actuator, that consumes power. + As a consequence, its usage must be discouraged, especially since its added value is not clear.

+

Noncompliant Code Example

+API 26: +
+    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
+    v.vibrate(400);
+
+API 31: +
+    VibratorManager vm = (VibratorManager) getSystemService(Context.VIBRATOR_MANAGER_SERVICE);
+    vm.getDefaultVibrator().vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB011_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ESOB011_java.json new file mode 100644 index 000000000..a9f723548 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB011_java.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: Vibration Free", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Minor" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB012_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ESOB012_java.html new file mode 100644 index 000000000..a1ca8dad2 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB012_java.html @@ -0,0 +1,28 @@ + +

Giving information to the end-user through notifications is an important aspect of a modern app.
+ However, a notification does not necessarily need to be loud and vibrant to achieve its purpose. + Default mode is enough. That is why when building a notification with + NotificationManager there should be no extra calls to the methods setSound() nor + setVibrate() (API26) nor setVibrationPattern() (API31) on the builder object.

+

Noncompliant Code Example

+API 26: +
+    Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext(), "42");
+    notificationBuilder.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000});
+    notificationBuilder.setSound(
+        RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
+        Notification.AUDIO_ATTRIBUTES_DEFAULT
+    );
+
+API 31: +
+    NotificationChannel notification = new NotificationChannel("42",
+        "test",
+        NotificationManager.IMPORTANCE_DEFAULT
+    );
+    notification.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
+    notification.setSound(
+        RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
+        Notification.AUDIO_ATTRIBUTES_DEFAULT
+    );
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB012_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ESOB012_java.json new file mode 100644 index 000000000..709f00a23 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB012_java.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: Thrifty Notification", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Minor" +} diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB013_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ESOB013_java.html new file mode 100644 index 000000000..49e670325 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB013_java.html @@ -0,0 +1,14 @@ + +

Turning on the torch mode programmatically with CameraManager#setTorchMode(..., true) must absolutely be + avoided because the flashlight is one of the most energy-intensive component.

+

Noncompliant Code Example

+
+ CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
+        String cameraId = null;
+        try {
+            cameraId = camManager.getCameraIdList()[0];
+            camManager.setTorchMode(cameraId, true);
+        } catch (CameraAccessException e) {
+            e.printStackTrace();
+        }
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB013_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ESOB013_java.json new file mode 100644 index 000000000..ebf33a8d7 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB013_java.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: Torch Free", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB014_java.html b/ecocode-rules-specifications/src/main/rules-mobile/ESOB014_java.html new file mode 100644 index 000000000..c0db8b22c --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB014_java.html @@ -0,0 +1,14 @@ + +

In Android 11 (API level 30) or higher, a call to + Surface#setFrameRate(float frameRate, int compatibility) results in a change to the display refresh + rate. However, a regular app displays 60 frames per second (60Hz). In order to optimize content refreshes and hence + saving energy, this frequency should not be raised to 90Hz or 120Hz, despite this is now supported by many devices. +

+

Noncompliant Code Example

+
+    surface.setFrameRate(120f, FRAME_RATE_COMPATIBILITY_DEFAULT);
+
+

Compliant Code Example

+
+    surface.setFrameRate(60f, FRAME_RATE_COMPATIBILITY_DEFAULT);
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB014_java.json b/ecocode-rules-specifications/src/main/rules-mobile/ESOB014_java.json new file mode 100644 index 000000000..263ee2be7 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ESOB014_java.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: High Frame Rate", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/SPRI002_java.html b/ecocode-rules-specifications/src/main/rules-mobile/SPRI002_java.html new file mode 100644 index 000000000..dd315224f --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/SPRI002_java.html @@ -0,0 +1,13 @@ + +

+ Importing the com.google.android.gms.analytics.Tracker or com.google.firebase.analytics.FirebaseAnalytics classes means that the app sends hits to Google Analytics. It is not necessarily sensitive information, but it is a first step towards Google Ads and hence this practice should be discouraged at early stage. +

+

Noncompliant Code Example

+Import of Google Analytics : +
+    import com.google.android.gms.analytics.Tracker
+
+Import of Firebase Analytics : +
+    import com.google.firebase.analytics
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/SPRI002_java.json b/ecocode-rules-specifications/src/main/rules-mobile/SPRI002_java.json new file mode 100644 index 000000000..9e238f4cd --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/SPRI002_java.json @@ -0,0 +1,17 @@ +{ + "title": "Privacy: Google Tracker", + "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 From 18ff499177bde606dfd4e152ae42a387950049da Mon Sep 17 00:00:00 2001 From: jycr Date: Mon, 3 Jul 2023 01:14:25 +0200 Subject: [PATCH 08/32] chore: Adds rule specifications from: https://github.com/green-code-initiative/ecoCode-android/tree/e5960e0c03566d33d9bec051103b7ff36879301b/android-plugin/src/main/resources/io/ecocode/xml/rules --- CHANGELOG.md | 1 + .../rules-mobile/android-xml/EBAT001.html | 16 ++++++++ .../rules-mobile/android-xml/EBAT001.json | 17 ++++++++ .../rules-mobile/android-xml/EIDL003.html | 19 +++++++++ .../rules-mobile/android-xml/EIDL003.json | 17 ++++++++ .../rules-mobile/android-xml/EIDL005.html | 12 ++++++ .../rules-mobile/android-xml/EIDL005.json | 17 ++++++++ .../rules-mobile/android-xml/EPOW002.html | 10 +++++ .../rules-mobile/android-xml/EPOW002.json | 17 ++++++++ .../rules-mobile/android-xml/EPOW003.html | 12 ++++++ .../rules-mobile/android-xml/EPOW003.json | 17 ++++++++ .../rules-mobile/android-xml/EPOW005.html | 41 +++++++++++++++++++ .../rules-mobile/android-xml/EPOW005.json | 17 ++++++++ .../rules-mobile/android-xml/EPOW007.html | 14 +++++++ .../rules-mobile/android-xml/EPOW007.json | 17 ++++++++ .../rules-mobile/android-xml/ESOB003.html | 12 ++++++ .../rules-mobile/android-xml/ESOB003.json | 17 ++++++++ .../rules-mobile/android-xml/ESOB004.html | 22 ++++++++++ .../rules-mobile/android-xml/ESOB004.json | 17 ++++++++ 19 files changed, 312 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EBAT001.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EBAT001.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL003.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL003.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL005.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL005.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW002.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW002.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW003.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW003.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW005.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW005.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW007.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW007.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB003.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB003.json create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB004.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB004.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 96d824280..1d5111c18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- [android#67](https://github.com/green-code-initiative/ecoCode-android/issues/67) Move Android rules into centralized rules repository - [#216](https://github.com/green-code-initiative/ecoCode/issues/216) Upgrade rule EC2 for Java : Multiple if-else statement improvment - [#106](https://github.com/green-code-initiative/ecoCode/issues/106) Upgrade RULES.md : rule EC67 not relevant neither for Python nor Rust - [#225](https://github.com/green-code-initiative/ecoCode/pull/225) Upgrade licence system and licence headers of Java files diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EBAT001.html b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EBAT001.html new file mode 100644 index 000000000..728486747 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EBAT001.html @@ -0,0 +1,16 @@ + +

Services are long-living operations, as components of the apps. However, they can be started in isolation each time the device is next started, + without the user's acknowledgement.
+ This technique should be discouraged because the accumulation of these silent services results in excessive battery depletion that remains + unexplained from the end-user's point of view.
+ In addition, end-users know how to kill applications, but more rarely how to kill services.
+ Thus, any developer should avoid having a call to Context#startService() from a Broadcast Receiver component that has specified an + intent-filter for the BOOT_COMPLETED action in the manifest.

+

Noncompliant Code Example

+
+    <receiver>
+        <intent-filter>
+            <action android:name= "android.intent.action.BOOT_COMPLETED"/>
+        </intent-filter>
+    </receiver>
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EBAT001.json b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EBAT001.json new file mode 100644 index 000000000..96150d255 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EBAT001.json @@ -0,0 +1,17 @@ +{ + "title": "Batch: Service Boot Time", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + "batch", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL003.html b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL003.html new file mode 100644 index 000000000..3a1b04cc1 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL003.html @@ -0,0 +1,19 @@ + +

To avoid draining the battery, an Android device that is left idle quickly falls asleep.
+ Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically + use the FLAG_KEEP_SCREEN_ON in their activity. Another way to implement this is in their application's + layout XML file, by using the android:keepScreenOn attribute.

+

Noncompliant Code Example

+
+    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:keepScreenOn="true"/>
+
+

Compliant Code Example

+
+    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:keepScreenOn="false"/>
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL003.json b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL003.json new file mode 100644 index 000000000..40788f9f8 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL003.json @@ -0,0 +1,17 @@ +{ + "title": "Idleness: Keep Screen On", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "idleness", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL005.html b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL005.html new file mode 100644 index 000000000..48d4e09f4 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL005.html @@ -0,0 +1,12 @@ + +

To avoid draining the battery, an Android device that is left idle quickly falls asleep. + Hence, keeping the screen on should be avoided, unless it is absolutely necessary.
+ If so, developers typically use a Power Manager system service feature called wake locks + by invoking PowerManager.WakeLock#newWakeLock(int levelAndFlags, String tag), + along with the specific permission WAKE_LOCK in their manifest.

+

Noncompliant Code Example

+
+    <manifest>
+        <uses-permission android:name="android.permission.WAKE_LOCK" />
+    </manifest>
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL005.json b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL005.json new file mode 100644 index 000000000..65f556e46 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL005.json @@ -0,0 +1,17 @@ +{ + "title": "Idleness: Keep CPU On", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "1h" + }, + "tags": [ + "idleness", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW002.html b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW002.html new file mode 100644 index 000000000..90308e6a1 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW002.html @@ -0,0 +1,10 @@ + +

A negative effect on the device s battery is when an app is paired with a companion device (over Bluetooth, BLE, or Wi-Fi) + and that it has been excluded from battery optimizations (run in the background) using the declaration + Manifest.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND.

+

Noncompliant Code Example

+
+    <manifest>
+       <uses-permission android:name="android.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND">
+    </manifest>
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW002.json b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW002.json new file mode 100644 index 000000000..5c157d61a --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW002.json @@ -0,0 +1,17 @@ +{ + "title": "Power: Compagnion In Background", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "20min" + }, + "tags": [ + "power", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW003.html b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW003.html new file mode 100644 index 000000000..3eefe003b --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW003.html @@ -0,0 +1,12 @@ + +

An app holding the permission REQUEST_IGNORE_BATTERY_OPTIMIZATIONS asks the user to allow it to ignore + battery optimizations (that is, put them on the whitelist of apps).

+

Most applications should not use this; there are many facilities provided by the platform for applications to + operate correctly in the various power saving modes.

+ +

Noncompliant Code Example

+
+    <manifest>
+        <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
+    </manifest>
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW003.json b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW003.json new file mode 100644 index 000000000..cbfbf69fb --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW003.json @@ -0,0 +1,17 @@ +{ + "title": "Power: Ignore Battery Optimizations", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "1h" + }, + "tags": [ + "power", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW005.html b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW005.html new file mode 100644 index 000000000..da8e00d62 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW005.html @@ -0,0 +1,41 @@ + +

It's always good that an app has different behavior when device is connected/disconnected + to a power station, or has different battery levels. + One can monitor the changes in charging state with a broadcast receiver registered + on the actions ACTION_POWER_CONNECTED + and ACTION_POWER_DISCONNECTED, or monitor significant + changes in battery level with a broadcast receiver registered + on the actions BATTERY_LOW and BATTERY_OKAY.

+ +

Compliant Code Example

+
+    <receiver android:name="io.ecocode.testapp.core.service.MyReceiver">
+        <intent-filter>
+            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
+        </intent-filter>
+    </receiver>
+
+OR +
+    <receiver android:name="io.ecocode.testapp.core.service.MyReceiver">
+        <intent-filter>
+            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
+        </intent-filter>
+    </receiver>
+
+OR +
+    <receiver android:name="io.ecocode.testapp.core.service.MyReceiver">
+        <intent-filter>
+            <action android:name="android.intent.action.ACTION_POWER_BATTERY_OKAY"/>
+        </intent-filter>
+    </receiver>
+
+OR +
+    <receiver android:name="io.ecocode.testapp.core.service.MyReceiver">
+        <intent-filter>
+            <action android:name="android.intent.action.ACTION_POWER_BATTERY_LOW"/>
+        </intent-filter>
+    </receiver>
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW005.json b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW005.json new file mode 100644 index 000000000..7c63da47f --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW005.json @@ -0,0 +1,17 @@ +{ + "title": "Power: Charge Awareness", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "0min" + }, + "tags": [ + "power", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Info" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW007.html b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW007.html new file mode 100644 index 000000000..9441c3a50 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW007.html @@ -0,0 +1,14 @@ + +

Taking into account when the device is entering or exiting the power save mode is higly desirable for the battery + life. + It implies the existence of a broadcast receiver registered on the action + ACTION_POWER_SAVE_MODE_CHANGED, + or programmaticaly with a call to PowerManager#isPowerSaveMode().

+

Compliant Code Example

+
+    <receiver android:name="io.ecocode.testapp.core.service.MyReceiver">
+        <intent-filter>
+            <action android:name="android.intent.action.BATTERY_CHANGED"/>
+        </intent-filter>
+    </receiver>
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW007.json b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW007.json new file mode 100644 index 000000000..8ec94302d --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW007.json @@ -0,0 +1,17 @@ +{ + "title": "Power: Save Mode Awareness", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "0min" + }, + "tags": [ + "power", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Info" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB003.html b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB003.html new file mode 100644 index 000000000..eb63dc53f --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB003.html @@ -0,0 +1,12 @@ + +

Displaying dark colors is particularly beneficial for mobile devices with (AM)OLED screens.
+ Consequently, custom resources like bright colors values and bitmap images with too high luminance should be avoided.

+

Noncompliant Code Example

+
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:foreground="#FFFFFF"
+        android:foregroundTint="#FFFFFF">
+    </TextView>
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB003.json b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB003.json new file mode 100644 index 000000000..605d0c89d --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB003.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: Dark UI (Bright Colors)", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "1h" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB004.html b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB004.html new file mode 100644 index 000000000..4b85c46cf --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB004.html @@ -0,0 +1,22 @@ + +

Developers are allowed to apply native themes for their app, or derive new ones through inheritance. This decision + has a significant impact on energy consumption since displaying dark colors is particularly beneficial for mobile + devices with (AM)OLED screens.
+ By default, Android will set Holo to the Dark theme (parent style Theme.Holo) and hence switching to the + light theme (parent style Theme.Holo.Light) within the manifest or in xml themes should be avoided.

+

Noncompliant Code Example

+
+    <style name="Theme.MyTheme" parent="@android:style/Theme.Holo.Light"/>
+
+or +
+    <application android:label="@string/app_name" android:theme="@android:style/Theme.Holo.Light"/>
+
+

Compliant Solution

+
+    <style name="Theme.MyTheme" parent="@android:style/Theme.Holo"/>
+
+or +
+    <application android:label="@string/app_name" android:theme="@android:style/Theme.Holo"/>
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB004.json b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB004.json new file mode 100644 index 000000000..329f040d9 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB004.json @@ -0,0 +1,17 @@ +{ + "title": "Sobriety: Dark UI (Theme)", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "1h" + }, + "tags": [ + "sobriety", + "environment", + "ecocode", + "android", + "eco-design" + ], + "defaultSeverity": "Major" +} \ No newline at end of file From e8d214fc58b2b031bda157ec1d028e00901ed0e7 Mon Sep 17 00:00:00 2001 From: jycr Date: Mon, 3 Jul 2023 01:15:51 +0200 Subject: [PATCH 09/32] chore: Adds rule specifications from: https://github.com/green-code-initiative/ecoCode-ios/tree/6298bddbd97f3ec8b6dbc353a89c452a6d3ef0e1/swift-lang/src/main/resources --- CHANGELOG.md | 1 + .../main/rules-mobile/ios-swift/EIDL001.html | 9 +++++++ .../main/rules-mobile/ios-swift/EIDL002.html | 17 +++++++++++++ .../main/rules-mobile/ios-swift/EPOW001.html | 24 +++++++++++++++++++ .../main/rules-mobile/ios-swift/EPOW002.html | 19 +++++++++++++++ .../main/rules-mobile/ios-swift/ESOB001.html | 12 ++++++++++ .../main/rules-mobile/ios-swift/ESOB002.html | 21 ++++++++++++++++ .../main/rules-mobile/ios-swift/ESOB003.html | 9 +++++++ .../main/rules-mobile/ios-swift/ESOB005.html | 9 +++++++ .../main/rules-mobile/ios-swift/ESOB006.html | 14 +++++++++++ 10 files changed, 135 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EIDL001.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EIDL002.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EPOW001.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EPOW002.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB001.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB002.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB003.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB005.html create mode 100644 ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB006.html diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d5111c18..d0cfa8c48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- [ios#3](https://github.com/green-code-initiative/ecoCode-ios/issues/3) Move iOS rules into centralized rules repository - [android#67](https://github.com/green-code-initiative/ecoCode-android/issues/67) Move Android rules into centralized rules repository - [#216](https://github.com/green-code-initiative/ecoCode/issues/216) Upgrade rule EC2 for Java : Multiple if-else statement improvment - [#106](https://github.com/green-code-initiative/ecoCode/issues/106) Upgrade RULES.md : rule EC67 not relevant neither for Python nor Rust diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EIDL001.html b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EIDL001.html new file mode 100644 index 000000000..bce45601b --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EIDL001.html @@ -0,0 +1,9 @@ + +

To avoid draining the battery, an iOS device that is left idle quickly falls asleep.
+ Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically + disable the idle timer with UIApplication.shared.isIdleTimerDisabled = true.

+

Noncompliant Code Example

+
+    UIApplication.shared.isIdleTimerDisabled = true
+
+ diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EIDL002.html b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EIDL002.html new file mode 100644 index 000000000..c2ececa2d --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EIDL002.html @@ -0,0 +1,17 @@ + +

Setting a tolerance for timers will allow them to fire later than the scheduled fire date. + The system will use this flexibility to shift the execution of timers by small amounts of time, + within their tolerances, increasing the ability to optimize power savings. + Your app can set the Timer#tolerance property to specify a tolerance for a timer. + Using this approach dramatically increases the amount of time that the processor spends idling + while users detect no change in system responsiveness. +

+

Noncompliant Code Example

+
+    let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in }
+
+

Compliant Code Example

+
+    let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in }
+    timer.tolerance = 0.5
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EPOW001.html b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EPOW001.html new file mode 100644 index 000000000..75de56f9a --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EPOW001.html @@ -0,0 +1,24 @@ + +

It's always good that an app has different behavior when device is connected/disconnected to a power station, + or has different battery levels. Your app can query the UIDevice#batteryLevel and + UIDevice#batteryState properties at any time to determine the level of charge and + the state of the battery. Your app can also register to receive notifications when the battery level + or state changes, using batteryLevelDidChangeNotification + and batteryStateDidChangeNotification. +

+

Compliant Code Example

+
+    let level = UIDevice.current.batteryLevel
+
+OR +
+    let state = UIDevice.current.batteryState
+
+OR +
+    NotificationCenter.default.addObserver(forName: UIDevice.batteryLevelDidChangeNotification, object: nil, queue: nil) { _ in }
+
+OR +
+    NotificationCenter.default.addObserver(forName: UIDevice.batteryStateDidChangeNotification, object: nil, queue: nil) { _ in }
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EPOW002.html b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EPOW002.html new file mode 100644 index 000000000..31fd61ae6 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EPOW002.html @@ -0,0 +1,19 @@ + +

Use device API to check if low power mode is enabled, and adapt resources usage accordingly. + For example, you can reduce frequency of data update if low power mode is enabled. + Your app can query the ProcessInfo.processInfo.isLowPowerModeEnabled property at any time + to determine whether Low Power Mode is active. Your app can also register to receive notifications + when the Low Power Mode state of a device changes, using NSProcessInfoPowerStateDidChange. +

+

Compliant Code Example

+
+    let isLowPowerModeEnabled = ProcessInfo.processInfo.isLowPowerModeEnabled
+
+OR +
+    NotificationCenter.default.addObserver(forName: .NSProcessInfoPowerStateDidChange, object: nil, queue: nil) { _ in }
+
+OR +
+    NotificationCenter.default.addObserver(forName: Notification.Name.NSProcessInfoPowerStateDidChange, object: nil, queue: nil) { _ in }
+
\ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB001.html b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB001.html new file mode 100644 index 000000000..d8d5374fd --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB001.html @@ -0,0 +1,12 @@ + +

Allowing the system to pause location updates, at times when the location data is unlikely to change, can improve + battery life without sacrificing location data. This is the default behavior. For example, if the user stops for + food while using a navigation app, the location manager might pause updates for a period of time. It is possible + to disable this behavior, by setting the CLLocationManager#pausesLocationUpdatesAutomatically + property to false, but applications are strongly discouraged of doing it.

+

Noncompliant Code Example

+
+    let manager = CLLocationManager()
+    manager.pausesLocationUpdatesAutomatically = false
+
+ diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB002.html b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB002.html new file mode 100644 index 000000000..a148284bf --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB002.html @@ -0,0 +1,21 @@ + +

Location awareness is one of the most popular features used by apps. + By default standard location updates run with the best accuracy level, + but unless your app really needs to know the user’s position within a few meters, + this level of accuracy isn't needed. + Requesting higher accuracy than you need causes the system to power up additional hardware + and waste power for unnecessary precision. You can specify a degree of accuracy by setting + the CLLocationManager#desiredAccuracy property. + Also, setting the CLLocationManager#activityType property will let the system knows what type + of location activity your app is performing and helps it determine + the most appropriate time to perform location updates. + Finally, if your app just needs a quick fix on the user’s location, + it’s best to call the CLLocationManager#requestLocation method, + that will deliver a single location update. +

+

Compliant Code Example

+
+    let manager = CLLocationManager()
+    manager.desiredAccuracy = 2
+
+ diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB003.html b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB003.html new file mode 100644 index 000000000..0b1ea6548 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB003.html @@ -0,0 +1,9 @@ + +

Using the Core Motion API, your app can receive continuous motion updates in the form of accelerometer, gyroscope, and device motion (rotation, acceleration, and more) events. Yet you don't often need these updates as soon as they are generated. Before registering to receive these recurring motion updates, you can specify an interval that meets your app’s needs, using CMMotionManager#accelerometerUpdateInterval, CMMotionManager#gyroUpdateInterval, CMMotionManager#deviceMotionUpdateInterval and CMMotionManager#magnetometerUpdateInterval properties. The larger the interval, the fewer events are delivered to your app, improving battery life.

+

Compliant Code

+
+    CMMotionManager.accelerometerUpdateInterval = 1000
+    CMMotionManager.gyroUpdateInterval = 1000
+    CMMotionManager.deviceMotionUpdateInterval = 1000
+    CMMotionManager.magnetometerUpdateInterval = 1000
+
diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB005.html b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB005.html new file mode 100644 index 000000000..f03eb5a1b --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB005.html @@ -0,0 +1,9 @@ + +

To avoid draining the battery, an iOS device adapt its brightness to the environment .
+ Hence, keeping forcing the screen brightness on should be avoided, unless it is absolutely necessary. If so, developers typically + force the Brightness to 1 with UIScreen.main.brightness = a float.

+

Noncompliant Code Example

+
+    UIScreen.main.brightness = CGFloat(0.3)
+
+ diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB006.html b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB006.html new file mode 100644 index 000000000..6ade65c00 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB006.html @@ -0,0 +1,14 @@ + +

Turning on the torch mode programmatically must absolutely be avoided because the flashlight is one of the most energy-intensive component.

+

Noncompliant Code Example

+
+    AVCaptureTorchMode.on
+
+OR +
+    setTorchModeOn(level: Float)
+
+OR +
+    torchMode = .on
+
\ No newline at end of file From ce9105c126d711016b122902e8c8aa87d7af9fde Mon Sep 17 00:00:00 2001 From: jycr Date: Fri, 30 Jun 2023 01:07:35 +0200 Subject: [PATCH 10/32] refactor(rule/android - java): Modify rule keys --- .../{rules-mobile/EBAT002_java.json => rules/EC500/EC500.json} | 0 .../EBAT002_java.html => rules/EC500/java/EC500.asciidoc} | 0 .../{rules-mobile/EBAT003_java.json => rules/EC501/EC501.json} | 0 .../EBAT003_java.html => rules/EC501/java/EC501.asciidoc} | 0 .../{rules-mobile/EBOT001_java.json => rules/EC502/EC502.json} | 0 .../EBOT001_java.html => rules/EC502/java/EC502.asciidoc} | 0 .../{rules-mobile/EBOT002_java.json => rules/EC503/EC503.json} | 0 .../EBOT002_java.html => rules/EC503/java/EC503.asciidoc} | 0 .../{rules-mobile/EBOT003_java.json => rules/EC504/EC504.json} | 0 .../EBOT003_java.html => rules/EC504/java/EC504.asciidoc} | 0 .../{rules-mobile/EIDL001_java.json => rules/EC505/EC505.json} | 0 .../EIDL001_java.html => rules/EC505/java/EC505.asciidoc} | 0 .../{rules-mobile/EIDL002_java.json => rules/EC506/EC506.json} | 0 .../EIDL002_java.html => rules/EC506/java/EC506.asciidoc} | 0 .../{rules-mobile/EIDL004_java.json => rules/EC507/EC507.json} | 0 .../EIDL004_java.html => rules/EC507/java/EC507.asciidoc} | 0 .../{rules-mobile/EIDL006_java.json => rules/EC508/EC508.json} | 0 .../EIDL006_java.html => rules/EC508/java/EC508.asciidoc} | 0 .../{rules-mobile/EIDL007_java.json => rules/EC509/EC509.json} | 0 .../EIDL007_java.html => rules/EC509/java/EC509.asciidoc} | 0 .../{rules-mobile/EIDL008_java.json => rules/EC510/EC510.json} | 0 .../EIDL008_java.html => rules/EC510/java/EC510.asciidoc} | 0 .../{rules-mobile/EIDL009_java.json => rules/EC511/EC511.json} | 0 .../EIDL009_java.html => rules/EC511/java/EC511.asciidoc} | 0 .../{rules-mobile/ELEA002_java.json => rules/EC512/EC512.json} | 0 .../ELEA002_java.html => rules/EC512/java/EC512.asciidoc} | 0 .../{rules-mobile/ELEA003_java.json => rules/EC513/EC513.json} | 0 .../ELEA003_java.html => rules/EC513/java/EC513.asciidoc} | 0 .../{rules-mobile/ELEA004_java.json => rules/EC514/EC514.json} | 0 .../ELEA004_java.html => rules/EC514/java/EC514.asciidoc} | 0 .../{rules-mobile/ELEA005_java.json => rules/EC515/EC515.json} | 0 .../ELEA005_java.html => rules/EC515/java/EC515.asciidoc} | 0 .../{rules-mobile/ELEA006_java.json => rules/EC516/EC516.json} | 0 .../ELEA006_java.html => rules/EC516/java/EC516.asciidoc} | 0 .../{rules-mobile/EOPT001_java.json => rules/EC517/EC517.json} | 0 .../EOPT001_java.html => rules/EC517/java/EC517.asciidoc} | 0 .../{rules-mobile/EOPT002_java.json => rules/EC518/EC518.json} | 0 .../EOPT002_java.html => rules/EC518/java/EC518.asciidoc} | 0 .../{rules-mobile/EPOW004_java.json => rules/EC519/EC519.json} | 0 .../EPOW004_java.html => rules/EC519/java/EC519.asciidoc} | 0 .../{rules-mobile/EPOW006_java.json => rules/EC520/EC520.json} | 0 .../EPOW006_java.html => rules/EC520/java/EC520.asciidoc} | 0 .../{rules-mobile/ESOB001_java.json => rules/EC521/EC521.json} | 0 .../ESOB001_java.html => rules/EC521/java/EC521.asciidoc} | 0 .../{rules-mobile/ESOB002_java.json => rules/EC522/EC522.json} | 0 .../ESOB002_java.html => rules/EC522/java/EC522.asciidoc} | 0 .../{rules-mobile/ESOB005_java.json => rules/EC523/EC523.json} | 0 .../ESOB005_java.html => rules/EC523/java/EC523.asciidoc} | 0 .../{rules-mobile/ESOB006_java.json => rules/EC524/EC524.json} | 0 .../ESOB006_java.html => rules/EC524/java/EC524.asciidoc} | 0 .../{rules-mobile/ESOB007_java.json => rules/EC525/EC525.json} | 0 .../ESOB007_java.html => rules/EC525/java/EC525.asciidoc} | 0 .../{rules-mobile/ESOB008_java.json => rules/EC526/EC526.json} | 0 .../ESOB008_java.html => rules/EC526/java/EC526.asciidoc} | 0 .../{rules-mobile/ESOB010_java.json => rules/EC527/EC527.json} | 0 .../ESOB010_java.html => rules/EC527/java/EC527.asciidoc} | 0 .../{rules-mobile/ESOB011_java.json => rules/EC528/EC528.json} | 0 .../ESOB011_java.html => rules/EC528/java/EC528.asciidoc} | 0 .../{rules-mobile/ESOB012_java.json => rules/EC529/EC529.json} | 0 .../ESOB012_java.html => rules/EC529/java/EC529.asciidoc} | 0 .../{rules-mobile/ESOB013_java.json => rules/EC530/EC530.json} | 0 .../ESOB013_java.html => rules/EC530/java/EC530.asciidoc} | 0 .../{rules-mobile/ESOB014_java.json => rules/EC531/EC531.json} | 0 .../ESOB014_java.html => rules/EC531/java/EC531.asciidoc} | 0 .../{rules-mobile/SPRI002_java.json => rules/EC532/EC532.json} | 0 .../SPRI002_java.html => rules/EC532/java/EC532.asciidoc} | 0 66 files changed, 0 insertions(+), 0 deletions(-) rename ecocode-rules-specifications/src/main/{rules-mobile/EBAT002_java.json => rules/EC500/EC500.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EBAT002_java.html => rules/EC500/java/EC500.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EBAT003_java.json => rules/EC501/EC501.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EBAT003_java.html => rules/EC501/java/EC501.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EBOT001_java.json => rules/EC502/EC502.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EBOT001_java.html => rules/EC502/java/EC502.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EBOT002_java.json => rules/EC503/EC503.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EBOT002_java.html => rules/EC503/java/EC503.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EBOT003_java.json => rules/EC504/EC504.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EBOT003_java.html => rules/EC504/java/EC504.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL001_java.json => rules/EC505/EC505.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL001_java.html => rules/EC505/java/EC505.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL002_java.json => rules/EC506/EC506.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL002_java.html => rules/EC506/java/EC506.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL004_java.json => rules/EC507/EC507.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL004_java.html => rules/EC507/java/EC507.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL006_java.json => rules/EC508/EC508.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL006_java.html => rules/EC508/java/EC508.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL007_java.json => rules/EC509/EC509.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL007_java.html => rules/EC509/java/EC509.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL008_java.json => rules/EC510/EC510.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL008_java.html => rules/EC510/java/EC510.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL009_java.json => rules/EC511/EC511.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EIDL009_java.html => rules/EC511/java/EC511.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ELEA002_java.json => rules/EC512/EC512.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ELEA002_java.html => rules/EC512/java/EC512.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ELEA003_java.json => rules/EC513/EC513.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ELEA003_java.html => rules/EC513/java/EC513.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ELEA004_java.json => rules/EC514/EC514.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ELEA004_java.html => rules/EC514/java/EC514.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ELEA005_java.json => rules/EC515/EC515.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ELEA005_java.html => rules/EC515/java/EC515.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ELEA006_java.json => rules/EC516/EC516.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ELEA006_java.html => rules/EC516/java/EC516.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EOPT001_java.json => rules/EC517/EC517.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EOPT001_java.html => rules/EC517/java/EC517.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EOPT002_java.json => rules/EC518/EC518.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EOPT002_java.html => rules/EC518/java/EC518.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EPOW004_java.json => rules/EC519/EC519.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EPOW004_java.html => rules/EC519/java/EC519.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EPOW006_java.json => rules/EC520/EC520.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/EPOW006_java.html => rules/EC520/java/EC520.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB001_java.json => rules/EC521/EC521.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB001_java.html => rules/EC521/java/EC521.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB002_java.json => rules/EC522/EC522.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB002_java.html => rules/EC522/java/EC522.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB005_java.json => rules/EC523/EC523.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB005_java.html => rules/EC523/java/EC523.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB006_java.json => rules/EC524/EC524.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB006_java.html => rules/EC524/java/EC524.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB007_java.json => rules/EC525/EC525.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB007_java.html => rules/EC525/java/EC525.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB008_java.json => rules/EC526/EC526.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB008_java.html => rules/EC526/java/EC526.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB010_java.json => rules/EC527/EC527.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB010_java.html => rules/EC527/java/EC527.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB011_java.json => rules/EC528/EC528.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB011_java.html => rules/EC528/java/EC528.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB012_java.json => rules/EC529/EC529.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB012_java.html => rules/EC529/java/EC529.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB013_java.json => rules/EC530/EC530.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB013_java.html => rules/EC530/java/EC530.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB014_java.json => rules/EC531/EC531.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ESOB014_java.html => rules/EC531/java/EC531.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/SPRI002_java.json => rules/EC532/EC532.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/SPRI002_java.html => rules/EC532/java/EC532.asciidoc} (100%) diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBAT002_java.json b/ecocode-rules-specifications/src/main/rules/EC500/EC500.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EBAT002_java.json rename to ecocode-rules-specifications/src/main/rules/EC500/EC500.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBAT002_java.html b/ecocode-rules-specifications/src/main/rules/EC500/java/EC500.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EBAT002_java.html rename to ecocode-rules-specifications/src/main/rules/EC500/java/EC500.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBAT003_java.json b/ecocode-rules-specifications/src/main/rules/EC501/EC501.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EBAT003_java.json rename to ecocode-rules-specifications/src/main/rules/EC501/EC501.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBAT003_java.html b/ecocode-rules-specifications/src/main/rules/EC501/java/EC501.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EBAT003_java.html rename to ecocode-rules-specifications/src/main/rules/EC501/java/EC501.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBOT001_java.json b/ecocode-rules-specifications/src/main/rules/EC502/EC502.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EBOT001_java.json rename to ecocode-rules-specifications/src/main/rules/EC502/EC502.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBOT001_java.html b/ecocode-rules-specifications/src/main/rules/EC502/java/EC502.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EBOT001_java.html rename to ecocode-rules-specifications/src/main/rules/EC502/java/EC502.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBOT002_java.json b/ecocode-rules-specifications/src/main/rules/EC503/EC503.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EBOT002_java.json rename to ecocode-rules-specifications/src/main/rules/EC503/EC503.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBOT002_java.html b/ecocode-rules-specifications/src/main/rules/EC503/java/EC503.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EBOT002_java.html rename to ecocode-rules-specifications/src/main/rules/EC503/java/EC503.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBOT003_java.json b/ecocode-rules-specifications/src/main/rules/EC504/EC504.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EBOT003_java.json rename to ecocode-rules-specifications/src/main/rules/EC504/EC504.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EBOT003_java.html b/ecocode-rules-specifications/src/main/rules/EC504/java/EC504.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EBOT003_java.html rename to ecocode-rules-specifications/src/main/rules/EC504/java/EC504.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL001_java.json b/ecocode-rules-specifications/src/main/rules/EC505/EC505.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL001_java.json rename to ecocode-rules-specifications/src/main/rules/EC505/EC505.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL001_java.html b/ecocode-rules-specifications/src/main/rules/EC505/java/EC505.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL001_java.html rename to ecocode-rules-specifications/src/main/rules/EC505/java/EC505.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL002_java.json b/ecocode-rules-specifications/src/main/rules/EC506/EC506.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL002_java.json rename to ecocode-rules-specifications/src/main/rules/EC506/EC506.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL002_java.html b/ecocode-rules-specifications/src/main/rules/EC506/java/EC506.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL002_java.html rename to ecocode-rules-specifications/src/main/rules/EC506/java/EC506.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL004_java.json b/ecocode-rules-specifications/src/main/rules/EC507/EC507.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL004_java.json rename to ecocode-rules-specifications/src/main/rules/EC507/EC507.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL004_java.html b/ecocode-rules-specifications/src/main/rules/EC507/java/EC507.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL004_java.html rename to ecocode-rules-specifications/src/main/rules/EC507/java/EC507.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL006_java.json b/ecocode-rules-specifications/src/main/rules/EC508/EC508.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL006_java.json rename to ecocode-rules-specifications/src/main/rules/EC508/EC508.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL006_java.html b/ecocode-rules-specifications/src/main/rules/EC508/java/EC508.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL006_java.html rename to ecocode-rules-specifications/src/main/rules/EC508/java/EC508.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL007_java.json b/ecocode-rules-specifications/src/main/rules/EC509/EC509.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL007_java.json rename to ecocode-rules-specifications/src/main/rules/EC509/EC509.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL007_java.html b/ecocode-rules-specifications/src/main/rules/EC509/java/EC509.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL007_java.html rename to ecocode-rules-specifications/src/main/rules/EC509/java/EC509.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL008_java.json b/ecocode-rules-specifications/src/main/rules/EC510/EC510.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL008_java.json rename to ecocode-rules-specifications/src/main/rules/EC510/EC510.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL008_java.html b/ecocode-rules-specifications/src/main/rules/EC510/java/EC510.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL008_java.html rename to ecocode-rules-specifications/src/main/rules/EC510/java/EC510.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL009_java.json b/ecocode-rules-specifications/src/main/rules/EC511/EC511.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL009_java.json rename to ecocode-rules-specifications/src/main/rules/EC511/EC511.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EIDL009_java.html b/ecocode-rules-specifications/src/main/rules/EC511/java/EC511.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EIDL009_java.html rename to ecocode-rules-specifications/src/main/rules/EC511/java/EC511.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA002_java.json b/ecocode-rules-specifications/src/main/rules/EC512/EC512.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ELEA002_java.json rename to ecocode-rules-specifications/src/main/rules/EC512/EC512.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA002_java.html b/ecocode-rules-specifications/src/main/rules/EC512/java/EC512.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ELEA002_java.html rename to ecocode-rules-specifications/src/main/rules/EC512/java/EC512.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA003_java.json b/ecocode-rules-specifications/src/main/rules/EC513/EC513.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ELEA003_java.json rename to ecocode-rules-specifications/src/main/rules/EC513/EC513.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA003_java.html b/ecocode-rules-specifications/src/main/rules/EC513/java/EC513.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ELEA003_java.html rename to ecocode-rules-specifications/src/main/rules/EC513/java/EC513.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA004_java.json b/ecocode-rules-specifications/src/main/rules/EC514/EC514.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ELEA004_java.json rename to ecocode-rules-specifications/src/main/rules/EC514/EC514.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA004_java.html b/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ELEA004_java.html rename to ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA005_java.json b/ecocode-rules-specifications/src/main/rules/EC515/EC515.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ELEA005_java.json rename to ecocode-rules-specifications/src/main/rules/EC515/EC515.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA005_java.html b/ecocode-rules-specifications/src/main/rules/EC515/java/EC515.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ELEA005_java.html rename to ecocode-rules-specifications/src/main/rules/EC515/java/EC515.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA006_java.json b/ecocode-rules-specifications/src/main/rules/EC516/EC516.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ELEA006_java.json rename to ecocode-rules-specifications/src/main/rules/EC516/EC516.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ELEA006_java.html b/ecocode-rules-specifications/src/main/rules/EC516/java/EC516.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ELEA006_java.html rename to ecocode-rules-specifications/src/main/rules/EC516/java/EC516.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EOPT001_java.json b/ecocode-rules-specifications/src/main/rules/EC517/EC517.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EOPT001_java.json rename to ecocode-rules-specifications/src/main/rules/EC517/EC517.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EOPT001_java.html b/ecocode-rules-specifications/src/main/rules/EC517/java/EC517.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EOPT001_java.html rename to ecocode-rules-specifications/src/main/rules/EC517/java/EC517.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EOPT002_java.json b/ecocode-rules-specifications/src/main/rules/EC518/EC518.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EOPT002_java.json rename to ecocode-rules-specifications/src/main/rules/EC518/EC518.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EOPT002_java.html b/ecocode-rules-specifications/src/main/rules/EC518/java/EC518.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EOPT002_java.html rename to ecocode-rules-specifications/src/main/rules/EC518/java/EC518.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EPOW004_java.json b/ecocode-rules-specifications/src/main/rules/EC519/EC519.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EPOW004_java.json rename to ecocode-rules-specifications/src/main/rules/EC519/EC519.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EPOW004_java.html b/ecocode-rules-specifications/src/main/rules/EC519/java/EC519.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EPOW004_java.html rename to ecocode-rules-specifications/src/main/rules/EC519/java/EC519.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EPOW006_java.json b/ecocode-rules-specifications/src/main/rules/EC520/EC520.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EPOW006_java.json rename to ecocode-rules-specifications/src/main/rules/EC520/EC520.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/EPOW006_java.html b/ecocode-rules-specifications/src/main/rules/EC520/java/EC520.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/EPOW006_java.html rename to ecocode-rules-specifications/src/main/rules/EC520/java/EC520.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB001_java.json b/ecocode-rules-specifications/src/main/rules/EC521/EC521.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB001_java.json rename to ecocode-rules-specifications/src/main/rules/EC521/EC521.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB001_java.html b/ecocode-rules-specifications/src/main/rules/EC521/java/EC521.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB001_java.html rename to ecocode-rules-specifications/src/main/rules/EC521/java/EC521.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB002_java.json b/ecocode-rules-specifications/src/main/rules/EC522/EC522.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB002_java.json rename to ecocode-rules-specifications/src/main/rules/EC522/EC522.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB002_java.html b/ecocode-rules-specifications/src/main/rules/EC522/java/EC522.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB002_java.html rename to ecocode-rules-specifications/src/main/rules/EC522/java/EC522.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB005_java.json b/ecocode-rules-specifications/src/main/rules/EC523/EC523.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB005_java.json rename to ecocode-rules-specifications/src/main/rules/EC523/EC523.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB005_java.html b/ecocode-rules-specifications/src/main/rules/EC523/java/EC523.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB005_java.html rename to ecocode-rules-specifications/src/main/rules/EC523/java/EC523.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB006_java.json b/ecocode-rules-specifications/src/main/rules/EC524/EC524.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB006_java.json rename to ecocode-rules-specifications/src/main/rules/EC524/EC524.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB006_java.html b/ecocode-rules-specifications/src/main/rules/EC524/java/EC524.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB006_java.html rename to ecocode-rules-specifications/src/main/rules/EC524/java/EC524.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB007_java.json b/ecocode-rules-specifications/src/main/rules/EC525/EC525.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB007_java.json rename to ecocode-rules-specifications/src/main/rules/EC525/EC525.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB007_java.html b/ecocode-rules-specifications/src/main/rules/EC525/java/EC525.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB007_java.html rename to ecocode-rules-specifications/src/main/rules/EC525/java/EC525.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB008_java.json b/ecocode-rules-specifications/src/main/rules/EC526/EC526.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB008_java.json rename to ecocode-rules-specifications/src/main/rules/EC526/EC526.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB008_java.html b/ecocode-rules-specifications/src/main/rules/EC526/java/EC526.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB008_java.html rename to ecocode-rules-specifications/src/main/rules/EC526/java/EC526.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB010_java.json b/ecocode-rules-specifications/src/main/rules/EC527/EC527.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB010_java.json rename to ecocode-rules-specifications/src/main/rules/EC527/EC527.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB010_java.html b/ecocode-rules-specifications/src/main/rules/EC527/java/EC527.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB010_java.html rename to ecocode-rules-specifications/src/main/rules/EC527/java/EC527.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB011_java.json b/ecocode-rules-specifications/src/main/rules/EC528/EC528.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB011_java.json rename to ecocode-rules-specifications/src/main/rules/EC528/EC528.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB011_java.html b/ecocode-rules-specifications/src/main/rules/EC528/java/EC528.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB011_java.html rename to ecocode-rules-specifications/src/main/rules/EC528/java/EC528.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB012_java.json b/ecocode-rules-specifications/src/main/rules/EC529/EC529.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB012_java.json rename to ecocode-rules-specifications/src/main/rules/EC529/EC529.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB012_java.html b/ecocode-rules-specifications/src/main/rules/EC529/java/EC529.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB012_java.html rename to ecocode-rules-specifications/src/main/rules/EC529/java/EC529.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB013_java.json b/ecocode-rules-specifications/src/main/rules/EC530/EC530.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB013_java.json rename to ecocode-rules-specifications/src/main/rules/EC530/EC530.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB013_java.html b/ecocode-rules-specifications/src/main/rules/EC530/java/EC530.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB013_java.html rename to ecocode-rules-specifications/src/main/rules/EC530/java/EC530.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB014_java.json b/ecocode-rules-specifications/src/main/rules/EC531/EC531.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB014_java.json rename to ecocode-rules-specifications/src/main/rules/EC531/EC531.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ESOB014_java.html b/ecocode-rules-specifications/src/main/rules/EC531/java/EC531.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ESOB014_java.html rename to ecocode-rules-specifications/src/main/rules/EC531/java/EC531.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/SPRI002_java.json b/ecocode-rules-specifications/src/main/rules/EC532/EC532.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/SPRI002_java.json rename to ecocode-rules-specifications/src/main/rules/EC532/EC532.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/SPRI002_java.html b/ecocode-rules-specifications/src/main/rules/EC532/java/EC532.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/SPRI002_java.html rename to ecocode-rules-specifications/src/main/rules/EC532/java/EC532.asciidoc From 29e326d06a7dcfd194949b16c0475724c08acd9e Mon Sep 17 00:00:00 2001 From: jycr Date: Sun, 2 Jul 2023 23:50:16 +0200 Subject: [PATCH 11/32] feat(rule/android - java): Convert HTML to ASCIIDOC --- .../src/main/rules/EC500/java/EC500.asciidoc | 51 +++++++------- .../src/main/rules/EC501/java/EC501.asciidoc | 62 +++++++++-------- .../src/main/rules/EC502/java/EC502.asciidoc | 25 +++---- .../src/main/rules/EC503/java/EC503.asciidoc | 41 +++++------ .../src/main/rules/EC504/java/EC504.asciidoc | 38 +++++------ .../src/main/rules/EC505/java/EC505.asciidoc | 20 +++--- .../src/main/rules/EC506/java/EC506.asciidoc | 20 +++--- .../src/main/rules/EC507/java/EC507.asciidoc | 23 ++++--- .../src/main/rules/EC508/java/EC508.asciidoc | 35 +++++----- .../src/main/rules/EC509/java/EC509.asciidoc | 68 +++++++++---------- .../src/main/rules/EC510/java/EC510.asciidoc | 34 +++++----- .../src/main/rules/EC511/java/EC511.asciidoc | 44 ++++++------ .../src/main/rules/EC512/java/EC512.asciidoc | 37 +++++----- .../src/main/rules/EC513/java/EC513.asciidoc | 42 +++++++----- .../src/main/rules/EC514/java/EC514.asciidoc | 45 ++++++------ .../src/main/rules/EC515/java/EC515.asciidoc | 33 +++++---- .../src/main/rules/EC516/java/EC516.asciidoc | 33 +++++---- .../src/main/rules/EC517/java/EC517.asciidoc | 34 +++++----- .../src/main/rules/EC518/java/EC518.asciidoc | 33 ++++----- .../src/main/rules/EC519/java/EC519.asciidoc | 52 +++++++------- .../src/main/rules/EC520/java/EC520.asciidoc | 51 ++++++++------ .../src/main/rules/EC521/java/EC521.asciidoc | 33 ++++----- .../src/main/rules/EC522/java/EC522.asciidoc | 23 ++++--- .../src/main/rules/EC523/java/EC523.asciidoc | 44 ++++++------ .../src/main/rules/EC524/java/EC524.asciidoc | 48 ++++++------- .../src/main/rules/EC525/java/EC525.asciidoc | 37 +++++----- .../src/main/rules/EC526/java/EC526.asciidoc | 39 ++++++----- .../src/main/rules/EC527/java/EC527.asciidoc | 44 ++++++------ .../src/main/rules/EC528/java/EC528.asciidoc | 29 ++++---- .../src/main/rules/EC529/java/EC529.asciidoc | 56 ++++++++------- .../src/main/rules/EC530/java/EC530.asciidoc | 28 ++++---- .../src/main/rules/EC531/java/EC531.asciidoc | 29 ++++---- .../src/main/rules/EC532/java/EC532.asciidoc | 26 ++++--- 33 files changed, 673 insertions(+), 584 deletions(-) diff --git a/ecocode-rules-specifications/src/main/rules/EC500/java/EC500.asciidoc b/ecocode-rules-specifications/src/main/rules/EC500/java/EC500.asciidoc index fea8c55c4..478643c8b 100644 --- a/ecocode-rules-specifications/src/main/rules/EC500/java/EC500.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC500/java/EC500.asciidoc @@ -1,24 +1,27 @@ - -

With SensorManager#registerListener(SensorEventListener, Sensor, int) the events are delivered as soon as possible.
- Instead, SensorManager#registerListener(SensorEventListener, Sensor, int, int maxReportLatencyUs)< - allows events to stay temporarily in the hardware FIFO (queue) before being delivered. - The events can be stored in the hardware FIFO up to maxReportLatencyUs microseconds.
- Once one of the events in the FIFO needs to be reported, all the events in the FIFO are reported sequentially. - Setting maxReportLatencyUs to a positive value allows to reduce the number of interrupts the AP (Application Processor) receives, - hence reducing power consumption, as the AP can switch to a lower power state while the sensor is capturing the data.

-

Noncompliant Code Example

-
-    SensorEventListener sensorEventListener;
-    SensorManager sensorManager;
-    Sensor sensor;
-
-    sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
-
-

Compliant Code Example

-
-    SensorEventListener sensorEventListener;
-    SensorManager sensorManager;
-    Sensor sensor;
-
-    sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL, 200000);
-
+Score ecoCode: 2sur5 + +With `SensorManager#registerListener(SensorEventListener, Sensor, int)` the events are delivered as soon as possible. + +Instead, `SensorManager#registerListener(SensorEventListener, Sensor, int, int maxReportLatencyUs)` allows events to stay temporarily in the hardware FIFO (queue) before being delivered. The events can be stored in the hardware FIFO up to `maxReportLatencyUs` microseconds. + +Once one of the events in the FIFO needs to be reported, all the events in the FIFO are reported sequentially. Setting `maxReportLatencyUs` to a positive value allows to reduce the number of interrupts the AP (Application Processor) receives, hence reducing power consumption, as the AP can switch to a lower power state while the sensor is capturing the data. + +## Noncompliant Code Example + +```java +SensorEventListener sensorEventListener; +SensorManager sensorManager; +Sensor sensor; + +sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL); +``` + +## Compliant Code Example + +```java +SensorEventListener sensorEventListener; +SensorManager sensorManager; +Sensor sensor; + +sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL, 200000); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC501/java/EC501.asciidoc b/ecocode-rules-specifications/src/main/rules/EC501/java/EC501.asciidoc index 102d8c225..f606520f6 100644 --- a/ecocode-rules-specifications/src/main/rules/EC501/java/EC501.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC501/java/EC501.asciidoc @@ -1,28 +1,34 @@ - -

The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the Job Scheduler class.
- Compared to a custom Sync Adapter or the alarm manager, the Job Scheduler supports batch scheduling of jobs.
- The Android system can combine jobs so that battery consumption is reduced.

-

Noncompliant Code Example

-
-    void setAlarm(Context context)
-    {
-        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
-        Intent intent = new Intent(context, Alarm.class);
-        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
-        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pendingIntent);
-    }
-
-    Alarm alarm = new Alarm();
-    alarm.setAlarm(this);
-
-

Compliant Code Example

-
-    JobInfo info = new JobInfo.Builder(123, COMPONENT_NAME)
-                .setRequiresCharging(true)
-                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
-                .setPersisted(true)
-                .setPeriodic(10 * 60 * 1000)
-                .build();
-    JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
-    scheduler.schedule(info);
-
+Score ecoCode: 2sur5 + +The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the Job Scheduler class. + +Compared to a custom Sync Adapter or the alarm manager, the Job Scheduler supports batch scheduling of jobs. + +The Android system can combine jobs so that battery consumption is reduced. + +## Noncompliant Code Example + +```java +void setAlarm(Context context) { + AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); + Intent intent = new Intent(context, Alarm.class); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pendingIntent); +} + +Alarm alarm = new Alarm(); +alarm.setAlarm(this); +``` + +## Compliant Code Example + +```java +JobInfo info = new JobInfo.Builder(123, COMPONENT_NAME) + .setRequiresCharging(true) + .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) + .setPersisted(true) + .setPeriodic(10 * 60 * 1000) + .build(); +JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); +scheduler.schedule(info); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC502/java/EC502.asciidoc b/ecocode-rules-specifications/src/main/rules/EC502/java/EC502.asciidoc index d21c7998f..d5b12a99e 100644 --- a/ecocode-rules-specifications/src/main/rules/EC502/java/EC502.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC502/java/EC502.asciidoc @@ -1,12 +1,13 @@ - -

Opening and closing internet connection continuously is extremely battery-inefficient since HTTP exchange is the most - consuming operation of the network. This bug typically occurs when one obtain a new HttpURLConnection - by calling URL#openConnection() within a loop control structure (while, for, do-while, for-each).
- Also, this bad practice must be early prevented because it is the root of another evil that consists in polling - data at regular intervals, instead of using push notifications to save a lot of battery power.

-

Noncompliant Code Example

-
-    for (int val : myArray) {
-        URL.openConnection();
-    }
-
+Score ecoCode: 1sur5 + +Opening and closing internet connection continuously is extremely battery-inefficient since HTTP exchange is the most consuming operation of the network. This bug typically occurs when one obtain a new `HttpURLConnection` by calling `URL#openConnection()` within a loop control structure (while, for, do-while, for-each). + +Also, this bad practice must be early prevented because it is the root of another evil that consists in polling data at regular intervals, instead of using push notifications to save a lot of battery power. + +## Noncompliant Code Example + +```java +for (int val : myArray) { + URL.openConnection(); +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC503/java/EC503.asciidoc b/ecocode-rules-specifications/src/main/rules/EC503/java/EC503.asciidoc index 117273dad..678f20e80 100644 --- a/ecocode-rules-specifications/src/main/rules/EC503/java/EC503.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC503/java/EC503.asciidoc @@ -1,19 +1,22 @@ - -

Normally the Wi-Fi stack filters out packets not explicitly addressed to the device. Acquiring a Multicast Lock with - WifiManager.MulticastLock.acquire() will cause the stack to receive packets addressed to multicast - addresses.

-

Processing these extra packets can cause a noticeable battery drain and must be disabled when not needed - with to a call to WifiManager.MulticastLock.release().

-

Noncompliant Code Example

-
-    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
-    WifiManager.MulticastLock lock = wifiManager.createMulticastLock("tag");
-    lock.acquire();
-
-

Compliant Code Example

-
-    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
-    WifiManager.MulticastLock lock = wifiManager.createMulticastLock("tag");
-    lock.acquire();
-    lock.release()
-
+Score ecoCode: 3sur5 + +Normally the Wi-Fi stack filters out packets not explicitly addressed to the device. Acquiring a Multicast Lock with `WifiManager.MulticastLock.acquire()` will cause the stack to receive packets addressed to multicast addresses. + +Processing these extra packets can cause a noticeable battery drain and must be disabled when not needed with to a call to `WifiManager.MulticastLock.release()`. + +## Noncompliant Code Example + +```java +WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); +WifiManager.MulticastLock lock = wifiManager.createMulticastLock("tag"); +lock.acquire(); +``` + +## Compliant Code Example + +```java +WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); +WifiManager.MulticastLock lock = wifiManager.createMulticastLock("tag"); +lock.acquire(); +lock.release() +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC504/java/EC504.asciidoc b/ecocode-rules-specifications/src/main/rules/EC504/java/EC504.asciidoc index d088954fa..4a6396edc 100644 --- a/ecocode-rules-specifications/src/main/rules/EC504/java/EC504.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC504/java/EC504.asciidoc @@ -1,19 +1,19 @@ - -

Transmitting a file over a network infrastructure without compressing - it consumes more energy than with compression. More precisely, - energy efficiency is improved in case the data is compressed at least by 10%, - transmitted and decompressed at the other network node. From the Android client side, - it means making a post HTTP request using a GZIPOutputStream - instead of the classical OutputStream, along with the HttpURLConnection object.

-

Noncompliant Code Example

-
-    Url url = new Url("https://www.ecocode.io/");
-    HttpsUrlConnection con = (HttpsURLConnection) url.openConnection();
-    OutputStream stream = con.getOutputStream();
-
-

Compliant Code Example

-
-    Url url = new Url("https://www.ecocode.io/");
-    HttpsUrlConnection con = (HttpsURLConnection) url.openConnection();
-    OutputStream stream = new GZIPOutputStream(con.getOutputStream());
-
+Score ecoCode: 2sur5 + +Transmitting a file over a network infrastructure without compressing it consumes more energy than with compression. More precisely, energy efficiency is improved in case the data is compressed at least by 10%, transmitted and decompressed at the other network node. From the Android client side, it means making a post HTTP request using a `GZIPOutputStream` instead of the classical OutputStream, along with the `HttpURLConnection` object. + +## Noncompliant Code Example + +```java +Url url = new Url("https://www.ecocode.io/"); +HttpsUrlConnection con = (HttpsURLConnection) url.openConnection(); +OutputStream stream = con.getOutputStream(); +``` + +## Compliant Code Example + +```java +Url url = new Url("https://www.ecocode.io/"); +HttpsUrlConnection con = (HttpsURLConnection) url.openConnection(); +OutputStream stream = new GZIPOutputStream(con.getOutputStream()); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC505/java/EC505.asciidoc b/ecocode-rules-specifications/src/main/rules/EC505/java/EC505.asciidoc index 649c017ed..1c18e73f8 100644 --- a/ecocode-rules-specifications/src/main/rules/EC505/java/EC505.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC505/java/EC505.asciidoc @@ -1,9 +1,11 @@ - -

To avoid draining the battery, an Android device that is left idle quickly falls asleep.
- Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically - use the FLAG_KEEP_SCREEN_ON in their activity. Another way to implement this is in their application's - layout XML file, by using the android:keepScreenOn attribute.

-

Noncompliant Code Example

-
-    getWindow().addFlags(FLAG_KEEP_SCREEN_ON);
-
+Score ecoCode: 3sur5 + +To avoid draining the battery, an Android device that is left idle quickly falls asleep. + +Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically use the `FLAG_KEEP_SCREEN_ON` in their activity. Another way to implement this is in their application's layout XML file, by using the `android:keepScreenOn` attribute. + +## Noncompliant Code Example + +```java +getWindow().addFlags(FLAG_KEEP_SCREEN_ON); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC506/java/EC506.asciidoc b/ecocode-rules-specifications/src/main/rules/EC506/java/EC506.asciidoc index b3b5c4cbe..d90a2b686 100644 --- a/ecocode-rules-specifications/src/main/rules/EC506/java/EC506.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC506/java/EC506.asciidoc @@ -1,9 +1,11 @@ - -

To avoid draining the battery, an Android device that is left idle quickly falls asleep.
- Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically - use the FLAG_KEEP_SCREEN_ON in their activity. Another way to implement this is in their application's - layout XML file, by using the android:keepScreenOn attribute.

-

Noncompliant Code Example

-
-    getWindow().setFlags(FLAG_KEEP_SCREEN_ON, FLAG_KEEP_SCREEN_ON);
-
+Score ecoCode: 3sur5 + +To avoid draining the battery, an Android device that is left idle quickly falls asleep. + +Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically use the `FLAG_KEEP_SCREEN_ON` in their activity. Another way to implement this is in their application's layout XML file, by using the `android:keepScreenOn` attribute. + +## Noncompliant Code Example + +```java +getWindow().setFlags(FLAG_KEEP_SCREEN_ON, FLAG_KEEP_SCREEN_ON); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC507/java/EC507.asciidoc b/ecocode-rules-specifications/src/main/rules/EC507/java/EC507.asciidoc index 5c3caefdd..80d8c0869 100644 --- a/ecocode-rules-specifications/src/main/rules/EC507/java/EC507.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC507/java/EC507.asciidoc @@ -1,11 +1,12 @@ - -

To avoid draining the battery, an Android device that is left idle quickly falls asleep. - Hence, keeping the CPU on should be avoided, unless it is absolutely necessary.
- If so, developers typically use a Power Manager system service feature called wake locks - by invoking PowerManager.WakeLock#newWakeLock(int levelAndFlags, String tag), - along with the specific permission WAKE_LOCK in their manifest.

-

Noncompliant Code Example

-
-    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
-    WakeLock manager = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG");
-
\ No newline at end of file +Score ecoCode: 2sur5 + +To avoid draining the battery, an Android device that is left idle quickly falls asleep. Hence, keeping the CPU on should be avoided, unless it is absolutely necessary. + +If so, developers typically use a Power Manager system service feature called wake locks by invoking `PowerManager.WakeLock#newWakeLock(int levelAndFlags, String tag)`, along with the specific permission `WAKE_LOCK` in their manifest. + +## Noncompliant Code Example + +```java +PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); +WakeLock manager = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG"); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC508/java/EC508.asciidoc b/ecocode-rules-specifications/src/main/rules/EC508/java/EC508.asciidoc index 0ee9f1537..132147229 100644 --- a/ecocode-rules-specifications/src/main/rules/EC508/java/EC508.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC508/java/EC508.asciidoc @@ -1,16 +1,19 @@ - -

A wake lock is a mechanism to indicate that your application needs to have the device stay on. - The general principle is to obtain a wake lock, acquire it and finally release it.

-

Hence, the challenge here is to release the lock as soon as possible to avoid running down the device's - battery excessively. Missing call to PowerManager#release() is a built-in check of Android - lint (Wakelock check) but that does not prevent abuse of the lock over too long a period of time.
- This can be avoided by a call to PowerManager#acquire(long timeout) instead of - PowerManager#acquire(), because the lock will be released for sure after the given timeout expires.

-

Noncompliant Code Example

-
-    PowerManager.acquire()
-
-

Compliant Code Example

-
-    PowerManager.acquire(long timeout)
-
+Score ecoCode: 2sur5 + +A wake lock is a mechanism to indicate that your application needs to have the device stay on. The general principle is to obtain a wake lock, acquire it and finally release it. + +Hence, the challenge here is to release the lock as soon as possible to avoid running down the device's battery excessively. Missing call to `PowerManager#release()` is a built-in check of Android lint (_Wakelock_ check) but that does not prevent abuse of the lock over too long a period of time. + +This can be avoided by a call to `PowerManager#acquire(long timeout)` instead of `PowerManager#acquire()`, because the lock will be released for sure after the given timeout expires. + +## Noncompliant Code Example + +```java +PowerManager.acquire() +``` + +## Compliant Code Example + +```java +PowerManager.acquire(long timeout) +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC509/java/EC509.asciidoc b/ecocode-rules-specifications/src/main/rules/EC509/java/EC509.asciidoc index d0ce4489f..8991b1f5c 100644 --- a/ecocode-rules-specifications/src/main/rules/EC509/java/EC509.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC509/java/EC509.asciidoc @@ -1,35 +1,33 @@ - -

Applications are strongly discouraged from using exact alarms unnecessarily as they reduce the OS's - ability to minimize battery use (i.e. Doze Mode).

-

For most apps prior to API 19, setInexactRepeating() is preferable over - setRepeating(). When you use this method, Android synchronizes multiple inexact - repeating alarms and fires them at the same time, thus reducing the battery drain.

-

Similarly after API 19, setExact() and setExactAndAllowWhileIdle() - can significantly impact the power use of the device when idle, so they should be used with - care. High-frequency alarms are also bad for battery life but this is already checked by Android - lint (ShortAlarm built-in check).

-

Noncompliant Code Example

-
-    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
-    alarmManager.setRepeating(alarmType, triggerAtMillis, intervalMillis, operation);
-
-or -
-    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
-    alarmManager.setExact(type,triggerAtMillis,operation);
-
-or -
-    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
-    alarmManager.setExact(type,triggerAtMillis,tag,listener,targetHandler);
-
-or -
-    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
-    alarmManager.setExactAndAllowWhileIdle(type,triggerAtMilllis,operation);
-
-

Compliant Code Example

-
-    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
-    alarmManager.setInexactRepeating(alarmType, triggerAtMillis, intervalMillis, operation);
-
+Score ecoCode: 2sur5 + +Applications are strongly discouraged from using exact alarms unnecessarily as they reduce the OS's ability to minimize battery use (i.e. Doze Mode). + +For most apps prior to API 19, `setInexactRepeating()` is preferable over `setRepeating()`. When you use this method, Android synchronizes multiple inexact repeating alarms and fires them at the same time, thus reducing the battery drain. + +Similarly after API 19, `setExact()` and `setExactAndAllowWhileIdle()` can significantly impact the power use of the device when idle, so they should be used with care. High-frequency alarms are also bad for battery life but this is already checked by Android lint (ShortAlarm built-in check). + +## Noncompliant Code Example + +```java +AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); +alarmManager.setRepeating(alarmType, triggerAtMillis, intervalMillis, operation); +``` or +```java +AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); +alarmManager.setExact(type,triggerAtMillis,operation); +``` or +```java +AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); +alarmManager.setExact(type,triggerAtMillis,tag,listener,targetHandler); +``` or +```java +AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); +alarmManager.setExactAndAllowWhileIdle(type,triggerAtMilllis,operation); +``` + +## Compliant Code Example + +```java +AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); +alarmManager.setInexactRepeating(alarmType, triggerAtMillis, intervalMillis, operation); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC510/java/EC510.asciidoc b/ecocode-rules-specifications/src/main/rules/EC510/java/EC510.asciidoc index 9b791f2c8..48cfef219 100644 --- a/ecocode-rules-specifications/src/main/rules/EC510/java/EC510.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC510/java/EC510.asciidoc @@ -1,17 +1,17 @@ - -

For developers wishing to display OpenGL rendering, when choosing the rendering mode - with GLSurfaceView#setRenderMode(int renderMode), - using RENDERMODE_WHEN_DIRTY instead of RENDERMODE_CONTINUOUSLY - (by default) can improve battery life and overall system performance by allowing the GPU and CPU - to idle when the view does not need to be updated.

-

Noncompliant Code Example

-
-     GLSurfaceView surfaceView = new GLSurfaceView(this);
-     surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY)
-
-
-

Compliant Code Example

-
-     GLSurfaceView surfaceView = new GLSurfaceView(this);
-     surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY)
-
+Score ecoCode: 2sur5 + +For developers wishing to display OpenGL rendering, when choosing the rendering mode with `GLSurfaceView#setRenderMode(int renderMode)`, using `RENDERMODE_WHEN_DIRTY` instead of `RENDERMODE_CONTINUOUSLY` (by default) can improve battery life and overall system performance by allowing the GPU and CPU to idle when the view does not need to be updated. + +## Noncompliant Code Example + +```java +GLSurfaceView surfaceView = new GLSurfaceView(this); +surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY) +``` + +## Compliant Code Example + +```java +GLSurfaceView surfaceView = new GLSurfaceView(this); +surfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY) +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC511/java/EC511.asciidoc b/ecocode-rules-specifications/src/main/rules/EC511/java/EC511.asciidoc index 235510706..f8d9b8335 100644 --- a/ecocode-rules-specifications/src/main/rules/EC511/java/EC511.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC511/java/EC511.asciidoc @@ -1,19 +1,25 @@ - -

During a voice interaction session, VoiceInteractionSession#setKeepAwake(boolean keepAwake) allows to decide whether it will keep the - device awake while it is running a voice activity.

-

By default, the system holds a wake lock for it while in this state, so that it can work even if the screen is off. Setting this to - false removes that wake lock, allowing the CPU to go to sleep and hence does not let this continue to drain the battery.

-

Noncompliant Code Example

-
-    VoiceInteractionSession voiceSession = new VoiceInteractionSession(this);
-
-OR -
-    VoiceInteractionSession voiceSession = new VoiceInteractionSession(this);
-    voiceSession.setKeepAwake(true);
-
-

Compliant Code Example

-
-    VoiceInteractionSession voiceSession = new VoiceInteractionSession(this);
-    voiceSession.setKeepAwake(false);
-
+Score ecoCode: 2sur5 + +During a voice interaction session, `VoiceInteractionSession#setKeepAwake(boolean keepAwake)` allows to decide whether it will keep the device awake while it is running a voice activity. + +By default, the system holds a wake lock for it while in this state, so that it can work even if the screen is off. Setting this to `false` removes that wake lock, allowing the CPU to go to sleep and hence does not let this continue to drain the battery. + +## Noncompliant Code Example + +```java +VoiceInteractionSession voiceSession = new VoiceInteractionSession(this); +``` + +or + +```java +VoiceInteractionSession voiceSession = new VoiceInteractionSession(this); +voiceSession.setKeepAwake(true); +``` + +## Compliant Code Example + +```java +VoiceInteractionSession voiceSession = new VoiceInteractionSession(this); +voiceSession.setKeepAwake(false); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC512/java/EC512.asciidoc b/ecocode-rules-specifications/src/main/rules/EC512/java/EC512.asciidoc index a3f60e424..b7dcee536 100644 --- a/ecocode-rules-specifications/src/main/rules/EC512/java/EC512.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC512/java/EC512.asciidoc @@ -1,17 +1,22 @@ - -

Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. - In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS).

-

The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor - unnecessarily process data when the app enters an idle state, typically when paused or stopped.
- Consequently, calls must be carefully pairwised: Camera.open()/Camera.release(). - Failing to do so can drain the battery in just a few hours.

-

Noncompliant Code Example

-
-    Camera camera = Camera.open();
-
-

Compliant Code Example

-
-    Camera camera = Camera.open();
-    camera.release();
-
+Score ecoCode: 1sur5 +Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. + In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS). + +The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor unnecessarily process data when the app enters an idle state, typically when paused or stopped. + + Consequently, calls must be carefully pairwised: `Camera.open()/Camera.release()`. + Failing to do so can drain the battery in just a few hours. + +## Noncompliant Code Example + +```java +Camera camera = Camera.open(); +``` + +## Compliant Code Example + +```java +Camera camera = Camera.open(); +camera.release(); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC513/java/EC513.asciidoc b/ecocode-rules-specifications/src/main/rules/EC513/java/EC513.asciidoc index 0693ffcc4..2f49409d8 100644 --- a/ecocode-rules-specifications/src/main/rules/EC513/java/EC513.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC513/java/EC513.asciidoc @@ -1,19 +1,25 @@ - -

Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. - In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS).

-

The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor - unnecessarily process data when the app enters an idle state, typically when paused or stopped.
- Consequently, calls must be carefully pairwised: LocationManager.requestLocationUpdates()/removeUpdates().
- Failing to do so can drain the battery in just a few hours.

-

Noncompliant Code Example

-
-    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
-    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 1.0f, this);
-
-

Compliant Code Example

-
-    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
-    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 1.0f, this);
-    locationManager.removeUpdates(this);
-
+Score ecoCode: 1sur5 +Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. + In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS). + +The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor unnecessarily process data when the app enters an idle state, typically when paused or stopped. + + Consequently, calls must be carefully pairwised: `LocationManager.requestLocationUpdates()/removeUpdates()`. + + Failing to do so can drain the battery in just a few hours. + +## Noncompliant Code Example + +```java +LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); +locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 1.0f, this); +``` + +## Compliant Code Example + +```java +LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); +locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 1.0f, this); +locationManager.removeUpdates(this); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc b/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc index 14963a543..5158edcf3 100644 --- a/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc @@ -1,21 +1,26 @@ - -

Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. - In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS).

-

The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor - unnecessarily process data when the app enters an idle state, typically when paused or stopped.
- Consequently, calls must be carefully pairwised: SensorManager#registerListener()/unregisterListener(). - Failing to do so can drain the battery in just a few hours.

-

Noncompliant Code Example

-
-    SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
-    Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
-    sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
-
-

Compliant Code Example

-
-    SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
-    Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
-    sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
-    sManager.unregisterListener(this);
-
+Score ecoCode: 1sur5 +Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. + In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS). + +The common point of all these sensors is that they are expensive while in use. Their common bug is to let the sensor unnecessarily process data when the app enters an idle state, typically when paused or stopped. + + Consequently, calls must be carefully pairwised: `SensorManager#registerListener()/unregisterListener()`. + Failing to do so can drain the battery in just a few hours. + +## Noncompliant Code Example + +```java +SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); +Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); +sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL); +``` + +## Compliant Code Example + +```java +SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); +Sensor accelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); +sManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL); +sManager.unregisterListener(this); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC515/java/EC515.asciidoc b/ecocode-rules-specifications/src/main/rules/EC515/java/EC515.asciidoc index a3969a8f7..6c1eadef2 100644 --- a/ecocode-rules-specifications/src/main/rules/EC515/java/EC515.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC515/java/EC515.asciidoc @@ -1,15 +1,18 @@ - -

Creation of a Media Recorder object with new MediaRecorder() is used to record - audio and video. Class own a release() method.

-

In addition to unnecessary resources (such as memory and instances of codecs) being held, - failure to call this method immediately if a media object is no longer needed may also lead to - continuous battery consumption for mobile devices.

-

Noncompliant Code Example

-
-    MediaRecorder mr = new MediaRecorder();
-
-

Compliant Solution

-
-    MediaRecorder mr = new MediaRecorder();
-    mr.release();
-
+Score ecoCode: 2sur5 + +Creation of a Media Recorder object with `new MediaRecorder()` is used to record audio and video. Class own a `release()` method. + +In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a media object is no longer needed may also lead to continuous battery consumption for mobile devices. + +## Noncompliant Code Example + +```java +MediaRecorder mr = new MediaRecorder(); +``` + +## Compliant Solution + +```java +MediaRecorder mr = new MediaRecorder(); +mr.release(); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC516/java/EC516.asciidoc b/ecocode-rules-specifications/src/main/rules/EC516/java/EC516.asciidoc index 729b4cf3a..c28e28620 100644 --- a/ecocode-rules-specifications/src/main/rules/EC516/java/EC516.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC516/java/EC516.asciidoc @@ -1,15 +1,18 @@ - -

Creation of a Media Player object with new MediaPlayer() can - be used to control playback of audio/video files and streams. Class own arelease() method.

-

In addition to unnecessary resources (such as memory and instances of codecs) being held, - failure to call this method immediately if a media object is no longer needed may also lead to - continuous battery consumption for mobile devices.

-

Noncompliant Code Example

-
-    MediaPlayer mp = new MediaPlayer();
-
-

Compliant Solution

-
-    MediaPlayer mp = new MediaPlayer();
-    mp.release();
-
+Score ecoCode: 2sur5 + +Creation of a Media Player object with `new MediaPlayer()` can be used to control playback of audio/video files and streams. Class own a`release()` method. + +In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a media object is no longer needed may also lead to continuous battery consumption for mobile devices. + +## Noncompliant Code Example + +```java +MediaPlayer mp = new MediaPlayer(); +``` + +## Compliant Solution + +```java +MediaPlayer mp = new MediaPlayer(); +mp.release(); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC517/java/EC517.asciidoc b/ecocode-rules-specifications/src/main/rules/EC517/java/EC517.asciidoc index 876fad0aa..d8ff2a3c3 100644 --- a/ecocode-rules-specifications/src/main/rules/EC517/java/EC517.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC517/java/EC517.asciidoc @@ -1,17 +1,17 @@ - -

The fused location provider is one of the location APIs in Google Play - services which combines signals from GPS, Wi-Fi, and cell networks, as well - as accelerometer, gyroscope, magnetometer and other sensors. It is officially - recommended to maximize battery life.
- Thus, developer has to set up Google Play Service in her gradle file with a dependency to - com.google.android.gms:play-services-location:x.y.z, and then to - import from com.google.android.gms.location instead of the - android.location package of the SDK.

-

Noncompliant Code Example

-
-    import android.location.*;
-
-

Compliant Solution

-
-    import com.google.android.gms.location.*;
-
\ No newline at end of file +Score ecoCode: 3sur5 + +The fused location provider is one of the location APIs in Google Play services which combines signals from GPS, Wi-Fi, and cell networks, as well as accelerometer, gyroscope, magnetometer and other sensors. It is officially recommended to maximize battery life. + +Thus, developer has to set up Google Play Service in her gradle file with a dependency to `com.google.android.gms:play-services-location:x.y.z`, and then to import from `com.google.android.gms.location` instead of the `android.location` package of the SDK. + +## Noncompliant Code Example + +```java +import android.location.*; +``` + +## Compliant Solution + +```java +import com.google.android.gms.location.*; +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC518/java/EC518.asciidoc b/ecocode-rules-specifications/src/main/rules/EC518/java/EC518.asciidoc index 4af3331ea..26d876be7 100644 --- a/ecocode-rules-specifications/src/main/rules/EC518/java/EC518.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC518/java/EC518.asciidoc @@ -1,16 +1,17 @@ - -

In contrast to classic Bluetooth, Bluetooth Low Energy (BLE) - is designed to provide significantly lower power consumption. - Its purpose is to save energy on both paired devices but very few - developers are aware of this alternative API. - From the Android client side, it means append android.bluetooth.le.* - imports to android.bluetooth.* imports in order to benefits from - low-energy features.

-

Noncompliant Code Example

-
-    import android.bluetooth.*
-
-

Compliant Solution

-
-    import android.bluetooth.le.*
-
\ No newline at end of file +Score ecoCode: 3sur5 + +In contrast to classic Bluetooth, Bluetooth Low Energy (BLE) is designed to provide significantly lower power consumption. Its purpose is to save energy on both paired devices but very few developers are aware of this alternative API. + +From the Android client side, it means append `android.bluetooth.le.\*` imports to `android.bluetooth.*` imports in order to benefits from low-energy features. + +## Noncompliant Code Example + +```java +import android.bluetooth.* +``` + +## Compliant Solution + +```java +import android.bluetooth.le.* +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC519/java/EC519.asciidoc b/ecocode-rules-specifications/src/main/rules/EC519/java/EC519.asciidoc index 5199273a9..3b6cb3fa3 100644 --- a/ecocode-rules-specifications/src/main/rules/EC519/java/EC519.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC519/java/EC519.asciidoc @@ -1,23 +1,29 @@ - -

It's always good that an app has different behavior when device is connected/disconnected - to a power station, or has different battery levels. - One can monitor the changes in charging state with a broadcast receiver registered - on the actions ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED, - or monitor significant changes in battery level with a broadcast receiver registered - on the actions BATTERY_LOW and BATTERY_OKAY.

-

Compliant Code Example

-
-    new IntentFilter(Intent.ACTION_POWER_CONNECTED)
-
-OR -
-    new IntentFilter(Intent.ACTION_POWER_DISCONNECTED)
-
-OR -
-    new IntentFilter(Intent.ACTION_POWER_BATTERY_OKAY)
-
-OR -
-    new IntentFilter(Intent.ACTION_POWER_BATTERY_LOW)
-
\ No newline at end of file +Score ecoCode: 5sur5 + +It's always good that an app has different behavior when device is connected/disconnected to a power station, or has different battery levels. + +One can monitor the changes in charging state with a broadcast receiver registered on the actions `ACTION_POWER_CONNECTED` and `ACTION_POWER_DISCONNECTED`, or monitor significant changes in battery level with a broadcast receiver registered on the actions `BATTERY_LOW` and `BATTERY_OKAY`. + +## Compliant Code Example + +```java +new IntentFilter(Intent.ACTION_POWER_CONNECTED) +``` + +Or + +```java +new IntentFilter(Intent.ACTION_POWER_DISCONNECTED) +``` + +Or + +```java +new IntentFilter(Intent.ACTION_POWER_BATTERY_OKAY) +``` + +Or + +```java +new IntentFilter(Intent.ACTION_POWER_BATTERY_LOW) +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC520/java/EC520.asciidoc b/ecocode-rules-specifications/src/main/rules/EC520/java/EC520.asciidoc index 50f1de3d4..7158e6d92 100644 --- a/ecocode-rules-specifications/src/main/rules/EC520/java/EC520.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC520/java/EC520.asciidoc @@ -1,22 +1,29 @@ - -

Taking into account when the device is entering or exiting the power save mode is higly desirable for the battery - life. - It implies the existence of a broadcast receiver registered on the action - ACTION_POWER_SAVE_MODE_CHANGED, - or programmaticaly with a call to PowerManager#isPowerSaveMode().

-

Compliant Code Example

-
-    new IntentFilter(Intent.ACTION_POWER_SAVE_MODE_CHANGED)
-
- OR -
-    intentFilter.addAction(ACTION_POWER_SAVE_MODE_CHANGED)
-
- OR -
-    IntentFilter.create(ACTION_POWER_SAVE_MODE_CHANGED,"")
-
- OR -
-    powerManager.isPowerSaveMode()
-
\ No newline at end of file +Score ecoCode: 5sur5 + +Taking into account when the device is entering or exiting the power save mode is higly desirable for the battery life. + +It implies the existence of a broadcast receiver registered on the action `ACTION_POWER_SAVE_MODE_CHANGED`, or programmaticaly with a call to `PowerManager#isPowerSaveMode().` + +## Compliant Code Example + +```java +new IntentFilter(Intent.ACTION_POWER_SAVE_MODE_CHANGED) +``` + +Or + +```java +intentFilter.addAction(ACTION_POWER_SAVE_MODE_CHANGED) +``` + +Or + +```java +IntentFilter.create(ACTION_POWER_SAVE_MODE_CHANGED,"") +``` + +Or + +```java +powerManager.isPowerSaveMode() +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC521/java/EC521.asciidoc b/ecocode-rules-specifications/src/main/rules/EC521/java/EC521.asciidoc index ef2b69305..9e8e65462 100644 --- a/ecocode-rules-specifications/src/main/rules/EC521/java/EC521.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC521/java/EC521.asciidoc @@ -1,16 +1,17 @@ - -

The rotation vector sensor is the most frequently used sensor for motion detection and monitoring. When using - SensorManager#getDefaultSensor(int type), always prefer the - constant TYPE_GEOMAGNETIC_ROTATION_VECTOR which is similar to TYPE_ROTATION_VECTOR, - but using a magnetometer instead of using a gyroscope. This sensor uses lower power than the other rotation vectors, - because it doesn't use the gyroscope. However, it is more noisy and will work best outdoors.

-

Noncompliant Code Example

-
-    SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
-    sManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
-
-

Compliant Code Example

-
-    SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
-    sManager.getDefaultSensor(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR);
-
+Score ecoCode: 2sur5 + +The rotation vector sensor is the most frequently used sensor for motion detection and monitoring. When using `SensorManager#getDefaultSensor(int type)`, always prefer the constant `TYPE_GEOMAGNETIC_ROTATION_VECTOR` which is similar to `TYPE_ROTATION_VECTOR`, but using a magnetometer instead of using a gyroscope. This sensor uses lower power than the other rotation vectors, because it doesn't use the gyroscope. However, it is more noisy and will work best outdoors. + +## Noncompliant Code Example + +```java +SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); +sManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR); +``` + +## Compliant Code Example + +```java +SensorManager sManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); +sManager.getDefaultSensor(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC522/java/EC522.asciidoc b/ecocode-rules-specifications/src/main/rules/EC522/java/EC522.asciidoc index bc3ea4794..3be62410f 100644 --- a/ecocode-rules-specifications/src/main/rules/EC522/java/EC522.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC522/java/EC522.asciidoc @@ -1,10 +1,13 @@ - -

Introduced in Android 9, the adaptive brightness feature raises or lower the brightness of the screen depending on - the light in the current environment.
- For some reasons, developers may disable this feature programmatically, setting the field - WindowManager.LayoutParams#screenBrightness with the constant BRIGHTNESS_OVERRIDE_FULL.
- This feature was introduced to improve battery life, be careful when deactivating it.

-

Noncompliant Code Example

-
-    getWindow().getAttributes().screenBrightness = BRIGHTNESS_OVERRIDE_FULL;
-
+Score ecoCode: 2sur5 + +Introduced in Android 9, the adaptive brightness feature raises or lower the brightness of the screen depending on the light in the current environment. + +For some reasons, developers may disable this feature programmatically, setting the field `WindowManager.LayoutParams#screenBrightness` with the constant `BRIGHTNESS_OVERRIDE_FULL`. + +This feature was introduced to improve battery life, be careful when deactivating it. + +## Noncompliant Code Example + +```java +getWindow().getAttributes().screenBrightness = BRIGHTNESS_OVERRIDE_FULL; +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC523/java/EC523.asciidoc b/ecocode-rules-specifications/src/main/rules/EC523/java/EC523.asciidoc index 77a14aee7..60df3ac02 100644 --- a/ecocode-rules-specifications/src/main/rules/EC523/java/EC523.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC523/java/EC523.asciidoc @@ -1,21 +1,23 @@ - -

With a call to - LocationManager#requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener), - the location provider will only send your application an update when the location has changed by at - least minDistance meters AND at least minTime milliseconds have passed. So minTime should - be the primary tool to conserve battery life, and, to a lesser extent, minDistance.
- These two must imperatively be greater than 0.

-

Noncompliant Code Example

-
-    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
-                0,
-                0,
-                this);
-
-

Compliant Code Example

-
-    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
-                60000L,  // refresh location at least each 60000ms
-                10.0f,   // refresh location at least each 10 meters
-                this);
-
\ No newline at end of file +Score ecoCode: 2sur5 + +With a call to `LocationManager#requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)`, the location provider will only send your application an update when the location has changed by at least minDistance meters AND at least minTime milliseconds have passed. So `minTime` should be the primary tool to conserve battery life, and, to a lesser extent, `minDistance`. + +These two must imperatively be greater than 0. + +## Noncompliant Code Example + +```java +locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, + 0, + 0, + this); +``` + +## Compliant Code Example + +```java +locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, + 60000L, // refresh location at least each 60000ms + 10.0f, // refresh location at least each 10 meters + this); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC524/java/EC524.asciidoc b/ecocode-rules-specifications/src/main/rules/EC524/java/EC524.asciidoc index d540d3f1e..53a7f6a97 100644 --- a/ecocode-rules-specifications/src/main/rules/EC524/java/EC524.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC524/java/EC524.asciidoc @@ -1,23 +1,25 @@ - -

Location awareness is one of the most popular features used by apps. - An important point is to try to get the best possible provider - (LocationManager#getBestProvider()) based on an energy criteria - thanks to Criteria#setPowerRequirement(int level), using constant - POWER_LOW instead of POWER_HIGH or - POWER_MEDIUM.

-

Noncompliant Code Example

-
-    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
-                1000L,
-                1.0f,
-                this);
-
-

Compliant Code Example

-
-    Criteria criteria = new Criteria();
-    criteria.setPowerRequirement(Criteria.POWER_LOW);
-    locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria,true),
-                1000L,
-                1.0f,
-                this);
-
+Score ecoCode: 2sur5 + +Location awareness is one of the most popular features used by apps. + +An important point is to try to get the best possible provider (`LocationManager#getBestProvider()`) based on an energy criteria thanks to `Criteria#setPowerRequirement(int level)`, using constant `POWER_LOW` instead of `POWER_HIGH` or `POWER_MEDIUM`. + +## Noncompliant Code Example + +```java +locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, + 1000L, + 1.0f, + this); +``` + +## Compliant Code Example + +```java +Criteria criteria = new Criteria(); +criteria.setPowerRequirement(Criteria.POWER_LOW); +locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria,true), + 1000L, + 1.0f, + this); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC525/java/EC525.asciidoc b/ecocode-rules-specifications/src/main/rules/EC525/java/EC525.asciidoc index f07b4c6ca..2e16266b5 100644 --- a/ecocode-rules-specifications/src/main/rules/EC525/java/EC525.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC525/java/EC525.asciidoc @@ -1,18 +1,19 @@ - -

With Bluetooth Low Energy technology, a Bluetooth Smart Ready device (the master) - will establish a link with a Bluetooth Smart device (the slave). Most often, - the slave is a GATT server and the master is a GATT client. GATT capable - devices can be discovered using BLE scan process.

-

The default and preferred advertising mode is ADVERTISE_MODE_LOW_POWER - when calling AdvertiseSettings.Builder#setAdvertiseMode(int advertiseMode). -

-

Noncompliant Code Example

-
-    AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
-    builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY);
-
-

Compliant Code Example

-
-    AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
-    builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER);
-
\ No newline at end of file +Score ecoCode: 2sur5 + +With Bluetooth Low Energy technology, a Bluetooth Smart Ready device (the master) will establish a link with a Bluetooth Smart device (the slave). Most often, the slave is a GATT server and the master is a GATT client. GATT capable devices can be discovered using BLE scan process. + +The default and preferred advertising mode is `ADVERTISE_MODE_LOW_POWER` when calling `AdvertiseSettings.Builder#setAdvertiseMode(int advertiseMode).` + +## Noncompliant Code Example + +```java +AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder(); +builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY); +``` + +## Compliant Code Example + +```java +AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder(); +builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC526/java/EC526.asciidoc b/ecocode-rules-specifications/src/main/rules/EC526/java/EC526.asciidoc index 9df3ab6e9..c51639c85 100644 --- a/ecocode-rules-specifications/src/main/rules/EC526/java/EC526.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC526/java/EC526.asciidoc @@ -1,18 +1,21 @@ - -

With Bluetooth Low Energy technology (see BLE API smell), a Bluetooth Smart Ready - device (the master) will establish a link with a Bluetooth Smart device (the slave). - Most often, the slave is a GATT server and the master is a GATT client. GATT capable - devices can be discovered using BLE scan process.

-

Invoking BluetoothGatt#requestConnectionPriority(int connectionPriority) - with the value CONNECTION_PRIORITY_LOW_POWER, is recommended to reduce - power consumption.

-

Noncompliant Code Example

-
-    BluetoothGatt gatt = new BluetoothGatt();
-    gatt.requestConnectionPriority(CONNECTION_PRIORITY_HIGH);
-
-

Compliant Code Example

-
-    BluetoothGatt gatt = new BluetoothGatt();
-    gatt.requestConnectionPriority(CONNECTION_PRIORITY_LOW_POWER);
-
\ No newline at end of file +Score ecoCode: 2sur5 + +With Bluetooth Low Energy technology (see BLE API smell), a Bluetooth Smart Ready device (the master) will establish a link with a Bluetooth Smart device (the slave). + +Most often, the slave is a GATT server and the master is a GATT client. GATT capable devices can be discovered using BLE scan process. + +Invoking `BluetoothGatt#requestConnectionPriority(int connectionPriority)` with the value `CONNECTION_PRIORITY_LOW_POWER`, is recommended to reduce power consumption. + +## Noncompliant Code Example + +```java +BluetoothGatt gatt = new BluetoothGatt(); +gatt.requestConnectionPriority(CONNECTION_PRIORITY_HIGH); +``` + +## Compliant Code Example + +```java +BluetoothGatt gatt = new BluetoothGatt(); +gatt.requestConnectionPriority(CONNECTION_PRIORITY_LOW_POWER); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC527/java/EC527.asciidoc b/ecocode-rules-specifications/src/main/rules/EC527/java/EC527.asciidoc index 77a14aee7..60df3ac02 100644 --- a/ecocode-rules-specifications/src/main/rules/EC527/java/EC527.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC527/java/EC527.asciidoc @@ -1,21 +1,23 @@ - -

With a call to - LocationManager#requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener), - the location provider will only send your application an update when the location has changed by at - least minDistance meters AND at least minTime milliseconds have passed. So minTime should - be the primary tool to conserve battery life, and, to a lesser extent, minDistance.
- These two must imperatively be greater than 0.

-

Noncompliant Code Example

-
-    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
-                0,
-                0,
-                this);
-
-

Compliant Code Example

-
-    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
-                60000L,  // refresh location at least each 60000ms
-                10.0f,   // refresh location at least each 10 meters
-                this);
-
\ No newline at end of file +Score ecoCode: 2sur5 + +With a call to `LocationManager#requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)`, the location provider will only send your application an update when the location has changed by at least minDistance meters AND at least minTime milliseconds have passed. So `minTime` should be the primary tool to conserve battery life, and, to a lesser extent, `minDistance`. + +These two must imperatively be greater than 0. + +## Noncompliant Code Example + +```java +locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, + 0, + 0, + this); +``` + +## Compliant Code Example + +```java +locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, + 60000L, // refresh location at least each 60000ms + 10.0f, // refresh location at least each 10 meters + this); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC528/java/EC528.asciidoc b/ecocode-rules-specifications/src/main/rules/EC528/java/EC528.asciidoc index 1b7ae5a56..da380b33a 100644 --- a/ecocode-rules-specifications/src/main/rules/EC528/java/EC528.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC528/java/EC528.asciidoc @@ -1,16 +1,19 @@ - -

Shaking of an Android device is possible in all circumstances with a call to getSystemService(Context.VIBRATOR_SERVICE) (API 26) or - getSystemService(Context.VIBRATOR.MANAGER.SERVICE) (API 31). +Score ecoCode: 2sur5 + +Shaking of an Android device is possible in all circumstances with a call to `getSystemService(Context.VIBRATOR_SERVICE)` (API 26) or `getSystemService(Context.VIBRATOR.MANAGER.SERVICE)` (API 31). Behind this effect stands a specific miniature hardware component, motor or actuator, that consumes power. - As a consequence, its usage must be discouraged, especially since its added value is not clear.

-

Noncompliant Code Example

+ As a consequence, its usage must be discouraged, especially since its added value is not clear. + +## Noncompliant Code Example + API 26: -
-    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
-    v.vibrate(400);
-
+```java +Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); +v.vibrate(400); +``` + API 31: -
-    VibratorManager vm = (VibratorManager) getSystemService(Context.VIBRATOR_MANAGER_SERVICE);
-    vm.getDefaultVibrator().vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
-
\ No newline at end of file +```java +VibratorManager vm = (VibratorManager) getSystemService(Context.VIBRATOR_MANAGER_SERVICE); +vm.getDefaultVibrator().vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC529/java/EC529.asciidoc b/ecocode-rules-specifications/src/main/rules/EC529/java/EC529.asciidoc index a1ca8dad2..1488c7552 100644 --- a/ecocode-rules-specifications/src/main/rules/EC529/java/EC529.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC529/java/EC529.asciidoc @@ -1,28 +1,32 @@ - -

Giving information to the end-user through notifications is an important aspect of a modern app.
- However, a notification does not necessarily need to be loud and vibrant to achieve its purpose. - Default mode is enough. That is why when building a notification with - NotificationManager there should be no extra calls to the methods setSound() nor - setVibrate() (API26) nor setVibrationPattern() (API31) on the builder object.

-

Noncompliant Code Example

+Score ecoCode: 2sur5 + +Giving information to the end-user through notifications is an important aspect of a modern app. + +However, a notification does not necessarily need to be loud and vibrant to achieve its purpose. Default mode is enough. That is why when building a notification with `NotificationManager` there should be no extra calls to the methods `setSound()` nor `setVibrate()` (API26) nor `setVibrationPattern()` (API31) on the builder object. + +## Noncompliant Code Example + API 26: -
-    Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext(), "42");
-    notificationBuilder.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000});
-    notificationBuilder.setSound(
-        RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
-        Notification.AUDIO_ATTRIBUTES_DEFAULT
-    );
-
+ +```java +Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext(), "42"); +notificationBuilder.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000}); +notificationBuilder.setSound( + RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), + Notification.AUDIO_ATTRIBUTES_DEFAULT +); +``` + API 31: -
-    NotificationChannel notification = new NotificationChannel("42",
-        "test",
-        NotificationManager.IMPORTANCE_DEFAULT
-    );
-    notification.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
-    notification.setSound(
-        RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
-        Notification.AUDIO_ATTRIBUTES_DEFAULT
-    );
-
+ +```java +NotificationChannel notification = new NotificationChannel("42", + "test", + NotificationManager.IMPORTANCE_DEFAULT +); +notification.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000}); +notification.setSound( + RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), + Notification.AUDIO_ATTRIBUTES_DEFAULT +); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC530/java/EC530.asciidoc b/ecocode-rules-specifications/src/main/rules/EC530/java/EC530.asciidoc index 49e670325..19c6c1a01 100644 --- a/ecocode-rules-specifications/src/main/rules/EC530/java/EC530.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC530/java/EC530.asciidoc @@ -1,14 +1,16 @@ - -

Turning on the torch mode programmatically with CameraManager#setTorchMode(..., true) must absolutely be - avoided because the flashlight is one of the most energy-intensive component.

-

Noncompliant Code Example

-
+Score ecoCode: 2sur5
+
+Turning on the torch mode programmatically with `CameraManager#setTorchMode(..., true)` must absolutely be avoided because the flashlight is one of the most energy-intensive component.
+
+## Noncompliant Code Example
+
+```java
  CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
-        String cameraId = null;
-        try {
-            cameraId = camManager.getCameraIdList()[0];
-            camManager.setTorchMode(cameraId, true);
-        } catch (CameraAccessException e) {
-            e.printStackTrace();
-        }
-
\ No newline at end of file +String cameraId = null; +try { + cameraId = camManager.getCameraIdList()[0]; + camManager.setTorchMode(cameraId, true); +} catch (CameraAccessException e) { + e.printStackTrace(); +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC531/java/EC531.asciidoc b/ecocode-rules-specifications/src/main/rules/EC531/java/EC531.asciidoc index c0db8b22c..8fb204f53 100644 --- a/ecocode-rules-specifications/src/main/rules/EC531/java/EC531.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC531/java/EC531.asciidoc @@ -1,14 +1,15 @@ - -

In Android 11 (API level 30) or higher, a call to - Surface#setFrameRate(float frameRate, int compatibility) results in a change to the display refresh - rate. However, a regular app displays 60 frames per second (60Hz). In order to optimize content refreshes and hence - saving energy, this frequency should not be raised to 90Hz or 120Hz, despite this is now supported by many devices. -

-

Noncompliant Code Example

-
-    surface.setFrameRate(120f, FRAME_RATE_COMPATIBILITY_DEFAULT);
-
-

Compliant Code Example

-
-    surface.setFrameRate(60f, FRAME_RATE_COMPATIBILITY_DEFAULT);
-
\ No newline at end of file +Score ecoCode: 2sur5 + +In Android 11 (API level 30) or higher, a call to `Surface#setFrameRate(float frameRate, int compatibility)` results in a change to the display refresh rate. However, a regular app displays 60 frames per second (60Hz). In order to optimize content refreshes and hence saving energy, this frequency should not be raised to 90Hz or 120Hz, despite this is now supported by many devices. + +## Noncompliant Code Example + +```java +surface.setFrameRate(120f, FRAME_RATE_COMPATIBILITY_DEFAULT); +``` + +## Compliant Code Example + +```java +surface.setFrameRate(60f, FRAME_RATE_COMPATIBILITY_DEFAULT); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC532/java/EC532.asciidoc b/ecocode-rules-specifications/src/main/rules/EC532/java/EC532.asciidoc index dd315224f..9d8a7a9ff 100644 --- a/ecocode-rules-specifications/src/main/rules/EC532/java/EC532.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC532/java/EC532.asciidoc @@ -1,13 +1,17 @@ - -

- Importing the com.google.android.gms.analytics.Tracker or com.google.firebase.analytics.FirebaseAnalytics classes means that the app sends hits to Google Analytics. It is not necessarily sensitive information, but it is a first step towards Google Ads and hence this practice should be discouraged at early stage. -

-

Noncompliant Code Example

+Score ecoCode: 2sur5 + +Importing the `com.google.android.gms.analytics.Tracker` or `com.google.firebase.analytics.FirebaseAnalytics` classes means that the app sends hits to Google Analytics. It is not necessarily sensitive information, but it is a first step towards Google Ads and hence this practice should be discouraged at early stage. + +## Noncompliant Code Example + Import of Google Analytics : -
-    import com.google.android.gms.analytics.Tracker
-
+ +```java +import com.google.android.gms.analytics.Tracker +``` + Import of Firebase Analytics : -
-    import com.google.firebase.analytics
-
\ No newline at end of file + +```java +import com.google.firebase.analytics +``` From 135f0c9ffd97cfdd1beca64a04501c38eafe716c Mon Sep 17 00:00:00 2001 From: jycr Date: Mon, 3 Jul 2023 00:06:57 +0200 Subject: [PATCH 12/32] refactor(rule/android - java): Move ecoScore to JSON metadata --- ecocode-rules-specifications/src/main/rules/EC500/EC500.json | 1 + .../src/main/rules/EC500/java/EC500.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC501/EC501.json | 1 + .../src/main/rules/EC501/java/EC501.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC502/EC502.json | 1 + .../src/main/rules/EC502/java/EC502.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC503/EC503.json | 1 + .../src/main/rules/EC503/java/EC503.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC504/EC504.json | 1 + .../src/main/rules/EC504/java/EC504.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC505/EC505.json | 1 + .../src/main/rules/EC505/java/EC505.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC506/EC506.json | 1 + .../src/main/rules/EC506/java/EC506.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC507/EC507.json | 1 + .../src/main/rules/EC507/java/EC507.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC508/EC508.json | 1 + .../src/main/rules/EC508/java/EC508.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC509/EC509.json | 1 + .../src/main/rules/EC509/java/EC509.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC510/EC510.json | 1 + .../src/main/rules/EC510/java/EC510.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC511/EC511.json | 1 + .../src/main/rules/EC511/java/EC511.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC512/EC512.json | 1 + .../src/main/rules/EC512/java/EC512.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC513/EC513.json | 1 + .../src/main/rules/EC513/java/EC513.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC514/EC514.json | 1 + .../src/main/rules/EC514/java/EC514.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC515/EC515.json | 1 + .../src/main/rules/EC515/java/EC515.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC516/EC516.json | 1 + .../src/main/rules/EC516/java/EC516.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC517/EC517.json | 1 + .../src/main/rules/EC517/java/EC517.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC518/EC518.json | 1 + .../src/main/rules/EC518/java/EC518.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC519/EC519.json | 1 + .../src/main/rules/EC519/java/EC519.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC520/EC520.json | 1 + .../src/main/rules/EC520/java/EC520.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC521/EC521.json | 1 + .../src/main/rules/EC521/java/EC521.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC522/EC522.json | 1 + .../src/main/rules/EC522/java/EC522.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC523/EC523.json | 1 + .../src/main/rules/EC523/java/EC523.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC524/EC524.json | 1 + .../src/main/rules/EC524/java/EC524.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC525/EC525.json | 1 + .../src/main/rules/EC525/java/EC525.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC526/EC526.json | 1 + .../src/main/rules/EC526/java/EC526.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC527/EC527.json | 1 + .../src/main/rules/EC527/java/EC527.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC528/EC528.json | 1 + .../src/main/rules/EC528/java/EC528.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC529/EC529.json | 1 + .../src/main/rules/EC529/java/EC529.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC530/EC530.json | 1 + .../src/main/rules/EC530/java/EC530.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC531/EC531.json | 1 + .../src/main/rules/EC531/java/EC531.asciidoc | 2 -- ecocode-rules-specifications/src/main/rules/EC532/EC532.json | 1 + .../src/main/rules/EC532/java/EC532.asciidoc | 2 -- 66 files changed, 33 insertions(+), 66 deletions(-) diff --git a/ecocode-rules-specifications/src/main/rules/EC500/EC500.json b/ecocode-rules-specifications/src/main/rules/EC500/EC500.json index 80d544305..87aae031c 100644 --- a/ecocode-rules-specifications/src/main/rules/EC500/EC500.json +++ b/ecocode-rules-specifications/src/main/rules/EC500/EC500.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Minor" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC500/java/EC500.asciidoc b/ecocode-rules-specifications/src/main/rules/EC500/java/EC500.asciidoc index 478643c8b..951822492 100644 --- a/ecocode-rules-specifications/src/main/rules/EC500/java/EC500.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC500/java/EC500.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - With `SensorManager#registerListener(SensorEventListener, Sensor, int)` the events are delivered as soon as possible. Instead, `SensorManager#registerListener(SensorEventListener, Sensor, int, int maxReportLatencyUs)` allows events to stay temporarily in the hardware FIFO (queue) before being delivered. The events can be stored in the hardware FIFO up to `maxReportLatencyUs` microseconds. diff --git a/ecocode-rules-specifications/src/main/rules/EC501/EC501.json b/ecocode-rules-specifications/src/main/rules/EC501/EC501.json index 7f7de57cb..169e1f742 100644 --- a/ecocode-rules-specifications/src/main/rules/EC501/EC501.json +++ b/ecocode-rules-specifications/src/main/rules/EC501/EC501.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Minor" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC501/java/EC501.asciidoc b/ecocode-rules-specifications/src/main/rules/EC501/java/EC501.asciidoc index f606520f6..84062dbab 100644 --- a/ecocode-rules-specifications/src/main/rules/EC501/java/EC501.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC501/java/EC501.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the Job Scheduler class. Compared to a custom Sync Adapter or the alarm manager, the Job Scheduler supports batch scheduling of jobs. diff --git a/ecocode-rules-specifications/src/main/rules/EC502/EC502.json b/ecocode-rules-specifications/src/main/rules/EC502/EC502.json index 4fcc4a827..786b0dc7a 100644 --- a/ecocode-rules-specifications/src/main/rules/EC502/EC502.json +++ b/ecocode-rules-specifications/src/main/rules/EC502/EC502.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.2", "defaultSeverity": "Critical" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC502/java/EC502.asciidoc b/ecocode-rules-specifications/src/main/rules/EC502/java/EC502.asciidoc index d5b12a99e..c645aaf93 100644 --- a/ecocode-rules-specifications/src/main/rules/EC502/java/EC502.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC502/java/EC502.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 1sur5 - Opening and closing internet connection continuously is extremely battery-inefficient since HTTP exchange is the most consuming operation of the network. This bug typically occurs when one obtain a new `HttpURLConnection` by calling `URL#openConnection()` within a loop control structure (while, for, do-while, for-each). Also, this bad practice must be early prevented because it is the root of another evil that consists in polling data at regular intervals, instead of using push notifications to save a lot of battery power. diff --git a/ecocode-rules-specifications/src/main/rules/EC503/EC503.json b/ecocode-rules-specifications/src/main/rules/EC503/EC503.json index 6aa9f5834..27bc96daa 100644 --- a/ecocode-rules-specifications/src/main/rules/EC503/EC503.json +++ b/ecocode-rules-specifications/src/main/rules/EC503/EC503.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.6", "defaultSeverity": "Minor" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC503/java/EC503.asciidoc b/ecocode-rules-specifications/src/main/rules/EC503/java/EC503.asciidoc index 678f20e80..674011019 100644 --- a/ecocode-rules-specifications/src/main/rules/EC503/java/EC503.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC503/java/EC503.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 3sur5 - Normally the Wi-Fi stack filters out packets not explicitly addressed to the device. Acquiring a Multicast Lock with `WifiManager.MulticastLock.acquire()` will cause the stack to receive packets addressed to multicast addresses. Processing these extra packets can cause a noticeable battery drain and must be disabled when not needed with to a call to `WifiManager.MulticastLock.release()`. diff --git a/ecocode-rules-specifications/src/main/rules/EC504/EC504.json b/ecocode-rules-specifications/src/main/rules/EC504/EC504.json index 48d683cfb..ee1782089 100644 --- a/ecocode-rules-specifications/src/main/rules/EC504/EC504.json +++ b/ecocode-rules-specifications/src/main/rules/EC504/EC504.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Minor" } diff --git a/ecocode-rules-specifications/src/main/rules/EC504/java/EC504.asciidoc b/ecocode-rules-specifications/src/main/rules/EC504/java/EC504.asciidoc index 4a6396edc..80f67b532 100644 --- a/ecocode-rules-specifications/src/main/rules/EC504/java/EC504.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC504/java/EC504.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - Transmitting a file over a network infrastructure without compressing it consumes more energy than with compression. More precisely, energy efficiency is improved in case the data is compressed at least by 10%, transmitted and decompressed at the other network node. From the Android client side, it means making a post HTTP request using a `GZIPOutputStream` instead of the classical OutputStream, along with the `HttpURLConnection` object. ## Noncompliant Code Example diff --git a/ecocode-rules-specifications/src/main/rules/EC505/EC505.json b/ecocode-rules-specifications/src/main/rules/EC505/EC505.json index f34b5b45d..13922db0e 100644 --- a/ecocode-rules-specifications/src/main/rules/EC505/EC505.json +++ b/ecocode-rules-specifications/src/main/rules/EC505/EC505.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.6", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC505/java/EC505.asciidoc b/ecocode-rules-specifications/src/main/rules/EC505/java/EC505.asciidoc index 1c18e73f8..8cb61fe1e 100644 --- a/ecocode-rules-specifications/src/main/rules/EC505/java/EC505.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC505/java/EC505.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 3sur5 - To avoid draining the battery, an Android device that is left idle quickly falls asleep. Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically use the `FLAG_KEEP_SCREEN_ON` in their activity. Another way to implement this is in their application's layout XML file, by using the `android:keepScreenOn` attribute. diff --git a/ecocode-rules-specifications/src/main/rules/EC506/EC506.json b/ecocode-rules-specifications/src/main/rules/EC506/EC506.json index 0c7b6ccfb..02149cb73 100644 --- a/ecocode-rules-specifications/src/main/rules/EC506/EC506.json +++ b/ecocode-rules-specifications/src/main/rules/EC506/EC506.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.6", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC506/java/EC506.asciidoc b/ecocode-rules-specifications/src/main/rules/EC506/java/EC506.asciidoc index d90a2b686..b047c41cc 100644 --- a/ecocode-rules-specifications/src/main/rules/EC506/java/EC506.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC506/java/EC506.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 3sur5 - To avoid draining the battery, an Android device that is left idle quickly falls asleep. Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically use the `FLAG_KEEP_SCREEN_ON` in their activity. Another way to implement this is in their application's layout XML file, by using the `android:keepScreenOn` attribute. diff --git a/ecocode-rules-specifications/src/main/rules/EC507/EC507.json b/ecocode-rules-specifications/src/main/rules/EC507/EC507.json index fdbf87a37..e8c97bac2 100644 --- a/ecocode-rules-specifications/src/main/rules/EC507/EC507.json +++ b/ecocode-rules-specifications/src/main/rules/EC507/EC507.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC507/java/EC507.asciidoc b/ecocode-rules-specifications/src/main/rules/EC507/java/EC507.asciidoc index 80d8c0869..a1a8362d5 100644 --- a/ecocode-rules-specifications/src/main/rules/EC507/java/EC507.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC507/java/EC507.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - To avoid draining the battery, an Android device that is left idle quickly falls asleep. Hence, keeping the CPU on should be avoided, unless it is absolutely necessary. If so, developers typically use a Power Manager system service feature called wake locks by invoking `PowerManager.WakeLock#newWakeLock(int levelAndFlags, String tag)`, along with the specific permission `WAKE_LOCK` in their manifest. diff --git a/ecocode-rules-specifications/src/main/rules/EC508/EC508.json b/ecocode-rules-specifications/src/main/rules/EC508/EC508.json index c435d3726..10d100572 100644 --- a/ecocode-rules-specifications/src/main/rules/EC508/EC508.json +++ b/ecocode-rules-specifications/src/main/rules/EC508/EC508.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC508/java/EC508.asciidoc b/ecocode-rules-specifications/src/main/rules/EC508/java/EC508.asciidoc index 132147229..98bf682f7 100644 --- a/ecocode-rules-specifications/src/main/rules/EC508/java/EC508.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC508/java/EC508.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - A wake lock is a mechanism to indicate that your application needs to have the device stay on. The general principle is to obtain a wake lock, acquire it and finally release it. Hence, the challenge here is to release the lock as soon as possible to avoid running down the device's battery excessively. Missing call to `PowerManager#release()` is a built-in check of Android lint (_Wakelock_ check) but that does not prevent abuse of the lock over too long a period of time. diff --git a/ecocode-rules-specifications/src/main/rules/EC509/EC509.json b/ecocode-rules-specifications/src/main/rules/EC509/EC509.json index 6c58cefd5..470bdd9a9 100644 --- a/ecocode-rules-specifications/src/main/rules/EC509/EC509.json +++ b/ecocode-rules-specifications/src/main/rules/EC509/EC509.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC509/java/EC509.asciidoc b/ecocode-rules-specifications/src/main/rules/EC509/java/EC509.asciidoc index 8991b1f5c..a477697e7 100644 --- a/ecocode-rules-specifications/src/main/rules/EC509/java/EC509.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC509/java/EC509.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - Applications are strongly discouraged from using exact alarms unnecessarily as they reduce the OS's ability to minimize battery use (i.e. Doze Mode). For most apps prior to API 19, `setInexactRepeating()` is preferable over `setRepeating()`. When you use this method, Android synchronizes multiple inexact repeating alarms and fires them at the same time, thus reducing the battery drain. diff --git a/ecocode-rules-specifications/src/main/rules/EC510/EC510.json b/ecocode-rules-specifications/src/main/rules/EC510/EC510.json index b776395f4..d09f88864 100644 --- a/ecocode-rules-specifications/src/main/rules/EC510/EC510.json +++ b/ecocode-rules-specifications/src/main/rules/EC510/EC510.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Minor" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC510/java/EC510.asciidoc b/ecocode-rules-specifications/src/main/rules/EC510/java/EC510.asciidoc index 48cfef219..9d12df018 100644 --- a/ecocode-rules-specifications/src/main/rules/EC510/java/EC510.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC510/java/EC510.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - For developers wishing to display OpenGL rendering, when choosing the rendering mode with `GLSurfaceView#setRenderMode(int renderMode)`, using `RENDERMODE_WHEN_DIRTY` instead of `RENDERMODE_CONTINUOUSLY` (by default) can improve battery life and overall system performance by allowing the GPU and CPU to idle when the view does not need to be updated. ## Noncompliant Code Example diff --git a/ecocode-rules-specifications/src/main/rules/EC511/EC511.json b/ecocode-rules-specifications/src/main/rules/EC511/EC511.json index 23099bc7c..14cccf094 100644 --- a/ecocode-rules-specifications/src/main/rules/EC511/EC511.json +++ b/ecocode-rules-specifications/src/main/rules/EC511/EC511.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC511/java/EC511.asciidoc b/ecocode-rules-specifications/src/main/rules/EC511/java/EC511.asciidoc index f8d9b8335..c911421da 100644 --- a/ecocode-rules-specifications/src/main/rules/EC511/java/EC511.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC511/java/EC511.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - During a voice interaction session, `VoiceInteractionSession#setKeepAwake(boolean keepAwake)` allows to decide whether it will keep the device awake while it is running a voice activity. By default, the system holds a wake lock for it while in this state, so that it can work even if the screen is off. Setting this to `false` removes that wake lock, allowing the CPU to go to sleep and hence does not let this continue to drain the battery. diff --git a/ecocode-rules-specifications/src/main/rules/EC512/EC512.json b/ecocode-rules-specifications/src/main/rules/EC512/EC512.json index 669956a00..4f9481425 100644 --- a/ecocode-rules-specifications/src/main/rules/EC512/EC512.json +++ b/ecocode-rules-specifications/src/main/rules/EC512/EC512.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.2", "defaultSeverity": "Critical" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC512/java/EC512.asciidoc b/ecocode-rules-specifications/src/main/rules/EC512/java/EC512.asciidoc index b7dcee536..782c35274 100644 --- a/ecocode-rules-specifications/src/main/rules/EC512/java/EC512.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC512/java/EC512.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 1sur5 - Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS). diff --git a/ecocode-rules-specifications/src/main/rules/EC513/EC513.json b/ecocode-rules-specifications/src/main/rules/EC513/EC513.json index 859c07503..62ae8ab4d 100644 --- a/ecocode-rules-specifications/src/main/rules/EC513/EC513.json +++ b/ecocode-rules-specifications/src/main/rules/EC513/EC513.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.2", "defaultSeverity": "Minor" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC513/java/EC513.asciidoc b/ecocode-rules-specifications/src/main/rules/EC513/java/EC513.asciidoc index 2f49409d8..cdc8b59a1 100644 --- a/ecocode-rules-specifications/src/main/rules/EC513/java/EC513.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC513/java/EC513.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 1sur5 - Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS). diff --git a/ecocode-rules-specifications/src/main/rules/EC514/EC514.json b/ecocode-rules-specifications/src/main/rules/EC514/EC514.json index 1074ec034..7d3fbc451 100644 --- a/ecocode-rules-specifications/src/main/rules/EC514/EC514.json +++ b/ecocode-rules-specifications/src/main/rules/EC514/EC514.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.2", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc b/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc index 5158edcf3..2f06dd287 100644 --- a/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 1sur5 - Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. In addition to these are the image sensor (a.k.a. Camera) and the geo-positioning sensor (a.k.a. GPS). diff --git a/ecocode-rules-specifications/src/main/rules/EC515/EC515.json b/ecocode-rules-specifications/src/main/rules/EC515/EC515.json index ead6d1e38..8fd81725b 100644 --- a/ecocode-rules-specifications/src/main/rules/EC515/EC515.json +++ b/ecocode-rules-specifications/src/main/rules/EC515/EC515.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC515/java/EC515.asciidoc b/ecocode-rules-specifications/src/main/rules/EC515/java/EC515.asciidoc index 6c1eadef2..2053d051b 100644 --- a/ecocode-rules-specifications/src/main/rules/EC515/java/EC515.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC515/java/EC515.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - Creation of a Media Recorder object with `new MediaRecorder()` is used to record audio and video. Class own a `release()` method. In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a media object is no longer needed may also lead to continuous battery consumption for mobile devices. diff --git a/ecocode-rules-specifications/src/main/rules/EC516/EC516.json b/ecocode-rules-specifications/src/main/rules/EC516/EC516.json index 46339b64a..b6ebfa5f2 100644 --- a/ecocode-rules-specifications/src/main/rules/EC516/EC516.json +++ b/ecocode-rules-specifications/src/main/rules/EC516/EC516.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC516/java/EC516.asciidoc b/ecocode-rules-specifications/src/main/rules/EC516/java/EC516.asciidoc index c28e28620..e59c5a2ed 100644 --- a/ecocode-rules-specifications/src/main/rules/EC516/java/EC516.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC516/java/EC516.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - Creation of a Media Player object with `new MediaPlayer()` can be used to control playback of audio/video files and streams. Class own a`release()` method. In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a media object is no longer needed may also lead to continuous battery consumption for mobile devices. diff --git a/ecocode-rules-specifications/src/main/rules/EC517/EC517.json b/ecocode-rules-specifications/src/main/rules/EC517/EC517.json index 360c92cea..7c263b9f8 100644 --- a/ecocode-rules-specifications/src/main/rules/EC517/EC517.json +++ b/ecocode-rules-specifications/src/main/rules/EC517/EC517.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.6", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC517/java/EC517.asciidoc b/ecocode-rules-specifications/src/main/rules/EC517/java/EC517.asciidoc index d8ff2a3c3..03a404866 100644 --- a/ecocode-rules-specifications/src/main/rules/EC517/java/EC517.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC517/java/EC517.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 3sur5 - The fused location provider is one of the location APIs in Google Play services which combines signals from GPS, Wi-Fi, and cell networks, as well as accelerometer, gyroscope, magnetometer and other sensors. It is officially recommended to maximize battery life. Thus, developer has to set up Google Play Service in her gradle file with a dependency to `com.google.android.gms:play-services-location:x.y.z`, and then to import from `com.google.android.gms.location` instead of the `android.location` package of the SDK. diff --git a/ecocode-rules-specifications/src/main/rules/EC518/EC518.json b/ecocode-rules-specifications/src/main/rules/EC518/EC518.json index 75dbedabc..87d28f50a 100644 --- a/ecocode-rules-specifications/src/main/rules/EC518/EC518.json +++ b/ecocode-rules-specifications/src/main/rules/EC518/EC518.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.6", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC518/java/EC518.asciidoc b/ecocode-rules-specifications/src/main/rules/EC518/java/EC518.asciidoc index 26d876be7..805dd1e46 100644 --- a/ecocode-rules-specifications/src/main/rules/EC518/java/EC518.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC518/java/EC518.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 3sur5 - In contrast to classic Bluetooth, Bluetooth Low Energy (BLE) is designed to provide significantly lower power consumption. Its purpose is to save energy on both paired devices but very few developers are aware of this alternative API. From the Android client side, it means append `android.bluetooth.le.\*` imports to `android.bluetooth.*` imports in order to benefits from low-energy features. diff --git a/ecocode-rules-specifications/src/main/rules/EC519/EC519.json b/ecocode-rules-specifications/src/main/rules/EC519/EC519.json index 7c63da47f..65127ad4a 100644 --- a/ecocode-rules-specifications/src/main/rules/EC519/EC519.json +++ b/ecocode-rules-specifications/src/main/rules/EC519/EC519.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "1.0", "defaultSeverity": "Info" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC519/java/EC519.asciidoc b/ecocode-rules-specifications/src/main/rules/EC519/java/EC519.asciidoc index 3b6cb3fa3..1bdc27c6e 100644 --- a/ecocode-rules-specifications/src/main/rules/EC519/java/EC519.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC519/java/EC519.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 5sur5 - It's always good that an app has different behavior when device is connected/disconnected to a power station, or has different battery levels. One can monitor the changes in charging state with a broadcast receiver registered on the actions `ACTION_POWER_CONNECTED` and `ACTION_POWER_DISCONNECTED`, or monitor significant changes in battery level with a broadcast receiver registered on the actions `BATTERY_LOW` and `BATTERY_OKAY`. diff --git a/ecocode-rules-specifications/src/main/rules/EC520/EC520.json b/ecocode-rules-specifications/src/main/rules/EC520/EC520.json index 8ec94302d..a5649c6ea 100644 --- a/ecocode-rules-specifications/src/main/rules/EC520/EC520.json +++ b/ecocode-rules-specifications/src/main/rules/EC520/EC520.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "1.0", "defaultSeverity": "Info" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC520/java/EC520.asciidoc b/ecocode-rules-specifications/src/main/rules/EC520/java/EC520.asciidoc index 7158e6d92..d4e3c166c 100644 --- a/ecocode-rules-specifications/src/main/rules/EC520/java/EC520.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC520/java/EC520.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 5sur5 - Taking into account when the device is entering or exiting the power save mode is higly desirable for the battery life. It implies the existence of a broadcast receiver registered on the action `ACTION_POWER_SAVE_MODE_CHANGED`, or programmaticaly with a call to `PowerManager#isPowerSaveMode().` diff --git a/ecocode-rules-specifications/src/main/rules/EC521/EC521.json b/ecocode-rules-specifications/src/main/rules/EC521/EC521.json index ea4b1caa6..55cd4ed43 100644 --- a/ecocode-rules-specifications/src/main/rules/EC521/EC521.json +++ b/ecocode-rules-specifications/src/main/rules/EC521/EC521.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Minor" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC521/java/EC521.asciidoc b/ecocode-rules-specifications/src/main/rules/EC521/java/EC521.asciidoc index 9e8e65462..4d370f0a4 100644 --- a/ecocode-rules-specifications/src/main/rules/EC521/java/EC521.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC521/java/EC521.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - The rotation vector sensor is the most frequently used sensor for motion detection and monitoring. When using `SensorManager#getDefaultSensor(int type)`, always prefer the constant `TYPE_GEOMAGNETIC_ROTATION_VECTOR` which is similar to `TYPE_ROTATION_VECTOR`, but using a magnetometer instead of using a gyroscope. This sensor uses lower power than the other rotation vectors, because it doesn't use the gyroscope. However, it is more noisy and will work best outdoors. ## Noncompliant Code Example diff --git a/ecocode-rules-specifications/src/main/rules/EC522/EC522.json b/ecocode-rules-specifications/src/main/rules/EC522/EC522.json index 9317b2968..8711bfb45 100644 --- a/ecocode-rules-specifications/src/main/rules/EC522/EC522.json +++ b/ecocode-rules-specifications/src/main/rules/EC522/EC522.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Minor" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC522/java/EC522.asciidoc b/ecocode-rules-specifications/src/main/rules/EC522/java/EC522.asciidoc index 3be62410f..a7286af84 100644 --- a/ecocode-rules-specifications/src/main/rules/EC522/java/EC522.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC522/java/EC522.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - Introduced in Android 9, the adaptive brightness feature raises or lower the brightness of the screen depending on the light in the current environment. For some reasons, developers may disable this feature programmatically, setting the field `WindowManager.LayoutParams#screenBrightness` with the constant `BRIGHTNESS_OVERRIDE_FULL`. diff --git a/ecocode-rules-specifications/src/main/rules/EC523/EC523.json b/ecocode-rules-specifications/src/main/rules/EC523/EC523.json index 4ac87ecda..351c51238 100644 --- a/ecocode-rules-specifications/src/main/rules/EC523/EC523.json +++ b/ecocode-rules-specifications/src/main/rules/EC523/EC523.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC523/java/EC523.asciidoc b/ecocode-rules-specifications/src/main/rules/EC523/java/EC523.asciidoc index 60df3ac02..2fb54ab84 100644 --- a/ecocode-rules-specifications/src/main/rules/EC523/java/EC523.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC523/java/EC523.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - With a call to `LocationManager#requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)`, the location provider will only send your application an update when the location has changed by at least minDistance meters AND at least minTime milliseconds have passed. So `minTime` should be the primary tool to conserve battery life, and, to a lesser extent, `minDistance`. These two must imperatively be greater than 0. diff --git a/ecocode-rules-specifications/src/main/rules/EC524/EC524.json b/ecocode-rules-specifications/src/main/rules/EC524/EC524.json index ec09a4972..215a161f0 100644 --- a/ecocode-rules-specifications/src/main/rules/EC524/EC524.json +++ b/ecocode-rules-specifications/src/main/rules/EC524/EC524.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Major" } diff --git a/ecocode-rules-specifications/src/main/rules/EC524/java/EC524.asciidoc b/ecocode-rules-specifications/src/main/rules/EC524/java/EC524.asciidoc index 53a7f6a97..356629524 100644 --- a/ecocode-rules-specifications/src/main/rules/EC524/java/EC524.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC524/java/EC524.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - Location awareness is one of the most popular features used by apps. An important point is to try to get the best possible provider (`LocationManager#getBestProvider()`) based on an energy criteria thanks to `Criteria#setPowerRequirement(int level)`, using constant `POWER_LOW` instead of `POWER_HIGH` or `POWER_MEDIUM`. diff --git a/ecocode-rules-specifications/src/main/rules/EC525/EC525.json b/ecocode-rules-specifications/src/main/rules/EC525/EC525.json index 9f649544d..820a920f9 100644 --- a/ecocode-rules-specifications/src/main/rules/EC525/EC525.json +++ b/ecocode-rules-specifications/src/main/rules/EC525/EC525.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC525/java/EC525.asciidoc b/ecocode-rules-specifications/src/main/rules/EC525/java/EC525.asciidoc index 2e16266b5..a1e2af352 100644 --- a/ecocode-rules-specifications/src/main/rules/EC525/java/EC525.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC525/java/EC525.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - With Bluetooth Low Energy technology, a Bluetooth Smart Ready device (the master) will establish a link with a Bluetooth Smart device (the slave). Most often, the slave is a GATT server and the master is a GATT client. GATT capable devices can be discovered using BLE scan process. The default and preferred advertising mode is `ADVERTISE_MODE_LOW_POWER` when calling `AdvertiseSettings.Builder#setAdvertiseMode(int advertiseMode).` diff --git a/ecocode-rules-specifications/src/main/rules/EC526/EC526.json b/ecocode-rules-specifications/src/main/rules/EC526/EC526.json index fe89d2f7d..5257b74de 100644 --- a/ecocode-rules-specifications/src/main/rules/EC526/EC526.json +++ b/ecocode-rules-specifications/src/main/rules/EC526/EC526.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC526/java/EC526.asciidoc b/ecocode-rules-specifications/src/main/rules/EC526/java/EC526.asciidoc index c51639c85..36b25cef1 100644 --- a/ecocode-rules-specifications/src/main/rules/EC526/java/EC526.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC526/java/EC526.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - With Bluetooth Low Energy technology (see BLE API smell), a Bluetooth Smart Ready device (the master) will establish a link with a Bluetooth Smart device (the slave). Most often, the slave is a GATT server and the master is a GATT client. GATT capable devices can be discovered using BLE scan process. diff --git a/ecocode-rules-specifications/src/main/rules/EC527/EC527.json b/ecocode-rules-specifications/src/main/rules/EC527/EC527.json index e2a6c6d60..3a290b995 100644 --- a/ecocode-rules-specifications/src/main/rules/EC527/EC527.json +++ b/ecocode-rules-specifications/src/main/rules/EC527/EC527.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Minor" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC527/java/EC527.asciidoc b/ecocode-rules-specifications/src/main/rules/EC527/java/EC527.asciidoc index 60df3ac02..2fb54ab84 100644 --- a/ecocode-rules-specifications/src/main/rules/EC527/java/EC527.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC527/java/EC527.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - With a call to `LocationManager#requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)`, the location provider will only send your application an update when the location has changed by at least minDistance meters AND at least minTime milliseconds have passed. So `minTime` should be the primary tool to conserve battery life, and, to a lesser extent, `minDistance`. These two must imperatively be greater than 0. diff --git a/ecocode-rules-specifications/src/main/rules/EC528/EC528.json b/ecocode-rules-specifications/src/main/rules/EC528/EC528.json index a9f723548..15b9d9905 100644 --- a/ecocode-rules-specifications/src/main/rules/EC528/EC528.json +++ b/ecocode-rules-specifications/src/main/rules/EC528/EC528.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Minor" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC528/java/EC528.asciidoc b/ecocode-rules-specifications/src/main/rules/EC528/java/EC528.asciidoc index da380b33a..5698168e1 100644 --- a/ecocode-rules-specifications/src/main/rules/EC528/java/EC528.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC528/java/EC528.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - Shaking of an Android device is possible in all circumstances with a call to `getSystemService(Context.VIBRATOR_SERVICE)` (API 26) or `getSystemService(Context.VIBRATOR.MANAGER.SERVICE)` (API 31). Behind this effect stands a specific miniature hardware component, motor or actuator, that consumes power. As a consequence, its usage must be discouraged, especially since its added value is not clear. diff --git a/ecocode-rules-specifications/src/main/rules/EC529/EC529.json b/ecocode-rules-specifications/src/main/rules/EC529/EC529.json index 709f00a23..53a758843 100644 --- a/ecocode-rules-specifications/src/main/rules/EC529/EC529.json +++ b/ecocode-rules-specifications/src/main/rules/EC529/EC529.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Minor" } diff --git a/ecocode-rules-specifications/src/main/rules/EC529/java/EC529.asciidoc b/ecocode-rules-specifications/src/main/rules/EC529/java/EC529.asciidoc index 1488c7552..9dcff316a 100644 --- a/ecocode-rules-specifications/src/main/rules/EC529/java/EC529.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC529/java/EC529.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - Giving information to the end-user through notifications is an important aspect of a modern app. However, a notification does not necessarily need to be loud and vibrant to achieve its purpose. Default mode is enough. That is why when building a notification with `NotificationManager` there should be no extra calls to the methods `setSound()` nor `setVibrate()` (API26) nor `setVibrationPattern()` (API31) on the builder object. diff --git a/ecocode-rules-specifications/src/main/rules/EC530/EC530.json b/ecocode-rules-specifications/src/main/rules/EC530/EC530.json index ebf33a8d7..89baa698a 100644 --- a/ecocode-rules-specifications/src/main/rules/EC530/EC530.json +++ b/ecocode-rules-specifications/src/main/rules/EC530/EC530.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Major" } diff --git a/ecocode-rules-specifications/src/main/rules/EC530/java/EC530.asciidoc b/ecocode-rules-specifications/src/main/rules/EC530/java/EC530.asciidoc index 19c6c1a01..9357810a6 100644 --- a/ecocode-rules-specifications/src/main/rules/EC530/java/EC530.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC530/java/EC530.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - Turning on the torch mode programmatically with `CameraManager#setTorchMode(..., true)` must absolutely be avoided because the flashlight is one of the most energy-intensive component. ## Noncompliant Code Example diff --git a/ecocode-rules-specifications/src/main/rules/EC531/EC531.json b/ecocode-rules-specifications/src/main/rules/EC531/EC531.json index 263ee2be7..7d74efb32 100644 --- a/ecocode-rules-specifications/src/main/rules/EC531/EC531.json +++ b/ecocode-rules-specifications/src/main/rules/EC531/EC531.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Major" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC531/java/EC531.asciidoc b/ecocode-rules-specifications/src/main/rules/EC531/java/EC531.asciidoc index 8fb204f53..a12e6a75f 100644 --- a/ecocode-rules-specifications/src/main/rules/EC531/java/EC531.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC531/java/EC531.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - In Android 11 (API level 30) or higher, a call to `Surface#setFrameRate(float frameRate, int compatibility)` results in a change to the display refresh rate. However, a regular app displays 60 frames per second (60Hz). In order to optimize content refreshes and hence saving energy, this frequency should not be raised to 90Hz or 120Hz, despite this is now supported by many devices. ## Noncompliant Code Example diff --git a/ecocode-rules-specifications/src/main/rules/EC532/EC532.json b/ecocode-rules-specifications/src/main/rules/EC532/EC532.json index 9e238f4cd..c73038cf1 100644 --- a/ecocode-rules-specifications/src/main/rules/EC532/EC532.json +++ b/ecocode-rules-specifications/src/main/rules/EC532/EC532.json @@ -13,5 +13,6 @@ "android", "eco-design" ], + "ecoScore": "0.4", "defaultSeverity": "Minor" } \ No newline at end of file diff --git a/ecocode-rules-specifications/src/main/rules/EC532/java/EC532.asciidoc b/ecocode-rules-specifications/src/main/rules/EC532/java/EC532.asciidoc index 9d8a7a9ff..8cec4d695 100644 --- a/ecocode-rules-specifications/src/main/rules/EC532/java/EC532.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC532/java/EC532.asciidoc @@ -1,5 +1,3 @@ -Score ecoCode: 2sur5 - Importing the `com.google.android.gms.analytics.Tracker` or `com.google.firebase.analytics.FirebaseAnalytics` classes means that the app sends hits to Google Analytics. It is not necessarily sensitive information, but it is a first step towards Google Ads and hence this practice should be discouraged at early stage. ## Noncompliant Code Example From 8b4a68559c0bf6ee476cc93bb3162a2531979392 Mon Sep 17 00:00:00 2001 From: jycr Date: Mon, 3 Jul 2023 01:29:35 +0200 Subject: [PATCH 13/32] refactor(rule/android - xml): Modify rule keys --- .../android-xml/EBAT001.json => rules/EC540/EC540.json} | 0 .../android-xml/EBAT001.html => rules/EC540/xml/EC540.asciidoc} | 0 .../android-xml/EIDL003.json => rules/EC541/EC541.json} | 0 .../android-xml/EIDL003.html => rules/EC541/xml/EC541.asciidoc} | 0 .../android-xml/EIDL005.json => rules/EC542/EC542.json} | 0 .../android-xml/EIDL005.html => rules/EC542/xml/EC542.asciidoc} | 0 .../android-xml/EPOW002.json => rules/EC543/EC543.json} | 0 .../android-xml/EPOW002.html => rules/EC543/xml/EC543.asciidoc} | 0 .../android-xml/EPOW003.json => rules/EC544/EC544.json} | 0 .../android-xml/EPOW003.html => rules/EC544/xml/EC544.asciidoc} | 0 .../android-xml/EPOW005.json => rules/EC545/EC545.json} | 0 .../android-xml/EPOW005.html => rules/EC545/xml/EC545.asciidoc} | 0 .../android-xml/EPOW007.json => rules/EC546/EC546.json} | 0 .../android-xml/EPOW007.html => rules/EC546/xml/EC546.asciidoc} | 0 .../android-xml/ESOB003.json => rules/EC547/EC547.json} | 0 .../android-xml/ESOB003.html => rules/EC547/xml/EC547.asciidoc} | 0 .../android-xml/ESOB004.json => rules/EC548/EC548.json} | 0 .../android-xml/ESOB004.html => rules/EC548/xml/EC548.asciidoc} | 0 18 files changed, 0 insertions(+), 0 deletions(-) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EBAT001.json => rules/EC540/EC540.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EBAT001.html => rules/EC540/xml/EC540.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EIDL003.json => rules/EC541/EC541.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EIDL003.html => rules/EC541/xml/EC541.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EIDL005.json => rules/EC542/EC542.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EIDL005.html => rules/EC542/xml/EC542.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EPOW002.json => rules/EC543/EC543.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EPOW002.html => rules/EC543/xml/EC543.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EPOW003.json => rules/EC544/EC544.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EPOW003.html => rules/EC544/xml/EC544.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EPOW005.json => rules/EC545/EC545.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EPOW005.html => rules/EC545/xml/EC545.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EPOW007.json => rules/EC546/EC546.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/EPOW007.html => rules/EC546/xml/EC546.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/ESOB003.json => rules/EC547/EC547.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/ESOB003.html => rules/EC547/xml/EC547.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/ESOB004.json => rules/EC548/EC548.json} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/android-xml/ESOB004.html => rules/EC548/xml/EC548.asciidoc} (100%) diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EBAT001.json b/ecocode-rules-specifications/src/main/rules/EC540/EC540.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EBAT001.json rename to ecocode-rules-specifications/src/main/rules/EC540/EC540.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EBAT001.html b/ecocode-rules-specifications/src/main/rules/EC540/xml/EC540.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EBAT001.html rename to ecocode-rules-specifications/src/main/rules/EC540/xml/EC540.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL003.json b/ecocode-rules-specifications/src/main/rules/EC541/EC541.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL003.json rename to ecocode-rules-specifications/src/main/rules/EC541/EC541.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL003.html b/ecocode-rules-specifications/src/main/rules/EC541/xml/EC541.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL003.html rename to ecocode-rules-specifications/src/main/rules/EC541/xml/EC541.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL005.json b/ecocode-rules-specifications/src/main/rules/EC542/EC542.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL005.json rename to ecocode-rules-specifications/src/main/rules/EC542/EC542.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL005.html b/ecocode-rules-specifications/src/main/rules/EC542/xml/EC542.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EIDL005.html rename to ecocode-rules-specifications/src/main/rules/EC542/xml/EC542.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW002.json b/ecocode-rules-specifications/src/main/rules/EC543/EC543.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW002.json rename to ecocode-rules-specifications/src/main/rules/EC543/EC543.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW002.html b/ecocode-rules-specifications/src/main/rules/EC543/xml/EC543.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW002.html rename to ecocode-rules-specifications/src/main/rules/EC543/xml/EC543.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW003.json b/ecocode-rules-specifications/src/main/rules/EC544/EC544.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW003.json rename to ecocode-rules-specifications/src/main/rules/EC544/EC544.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW003.html b/ecocode-rules-specifications/src/main/rules/EC544/xml/EC544.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW003.html rename to ecocode-rules-specifications/src/main/rules/EC544/xml/EC544.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW005.json b/ecocode-rules-specifications/src/main/rules/EC545/EC545.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW005.json rename to ecocode-rules-specifications/src/main/rules/EC545/EC545.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW005.html b/ecocode-rules-specifications/src/main/rules/EC545/xml/EC545.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW005.html rename to ecocode-rules-specifications/src/main/rules/EC545/xml/EC545.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW007.json b/ecocode-rules-specifications/src/main/rules/EC546/EC546.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW007.json rename to ecocode-rules-specifications/src/main/rules/EC546/EC546.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW007.html b/ecocode-rules-specifications/src/main/rules/EC546/xml/EC546.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/EPOW007.html rename to ecocode-rules-specifications/src/main/rules/EC546/xml/EC546.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB003.json b/ecocode-rules-specifications/src/main/rules/EC547/EC547.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB003.json rename to ecocode-rules-specifications/src/main/rules/EC547/EC547.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB003.html b/ecocode-rules-specifications/src/main/rules/EC547/xml/EC547.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB003.html rename to ecocode-rules-specifications/src/main/rules/EC547/xml/EC547.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB004.json b/ecocode-rules-specifications/src/main/rules/EC548/EC548.json similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB004.json rename to ecocode-rules-specifications/src/main/rules/EC548/EC548.json diff --git a/ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB004.html b/ecocode-rules-specifications/src/main/rules/EC548/xml/EC548.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/android-xml/ESOB004.html rename to ecocode-rules-specifications/src/main/rules/EC548/xml/EC548.asciidoc From a95712edf856db392c8546e839aa786dfdc0e0e1 Mon Sep 17 00:00:00 2001 From: jycr Date: Mon, 3 Jul 2023 01:29:55 +0200 Subject: [PATCH 14/32] refactor(rule/ios - swift): Modify rule keys --- .../ios-swift/EIDL001.html => rules/EC505/swift/EC505.asciidoc} | 0 .../ios-swift/EIDL002.html => rules/EC506/swift/EC506.asciidoc} | 0 .../ios-swift/ESOB001.html => rules/EC521/swift/EC521.asciidoc} | 0 .../ios-swift/ESOB002.html => rules/EC522/swift/EC522.asciidoc} | 0 .../ios-swift/ESOB005.html => rules/EC523/swift/EC523.asciidoc} | 0 .../ios-swift/ESOB006.html => rules/EC524/swift/EC524.asciidoc} | 0 .../ios-swift/EPOW002.html => rules/EC543/swift/EC543.asciidoc} | 0 .../ios-swift/ESOB003.html => rules/EC547/swift/EC547.asciidoc} | 0 .../ios-swift/EPOW001.html => rules/EC602/swift/EC602.asciidoc} | 0 9 files changed, 0 insertions(+), 0 deletions(-) rename ecocode-rules-specifications/src/main/{rules-mobile/ios-swift/EIDL001.html => rules/EC505/swift/EC505.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ios-swift/EIDL002.html => rules/EC506/swift/EC506.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ios-swift/ESOB001.html => rules/EC521/swift/EC521.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ios-swift/ESOB002.html => rules/EC522/swift/EC522.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ios-swift/ESOB005.html => rules/EC523/swift/EC523.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ios-swift/ESOB006.html => rules/EC524/swift/EC524.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ios-swift/EPOW002.html => rules/EC543/swift/EC543.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ios-swift/ESOB003.html => rules/EC547/swift/EC547.asciidoc} (100%) rename ecocode-rules-specifications/src/main/{rules-mobile/ios-swift/EPOW001.html => rules/EC602/swift/EC602.asciidoc} (100%) diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EIDL001.html b/ecocode-rules-specifications/src/main/rules/EC505/swift/EC505.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EIDL001.html rename to ecocode-rules-specifications/src/main/rules/EC505/swift/EC505.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EIDL002.html b/ecocode-rules-specifications/src/main/rules/EC506/swift/EC506.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EIDL002.html rename to ecocode-rules-specifications/src/main/rules/EC506/swift/EC506.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB001.html b/ecocode-rules-specifications/src/main/rules/EC521/swift/EC521.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB001.html rename to ecocode-rules-specifications/src/main/rules/EC521/swift/EC521.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB002.html b/ecocode-rules-specifications/src/main/rules/EC522/swift/EC522.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB002.html rename to ecocode-rules-specifications/src/main/rules/EC522/swift/EC522.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB005.html b/ecocode-rules-specifications/src/main/rules/EC523/swift/EC523.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB005.html rename to ecocode-rules-specifications/src/main/rules/EC523/swift/EC523.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB006.html b/ecocode-rules-specifications/src/main/rules/EC524/swift/EC524.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB006.html rename to ecocode-rules-specifications/src/main/rules/EC524/swift/EC524.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EPOW002.html b/ecocode-rules-specifications/src/main/rules/EC543/swift/EC543.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EPOW002.html rename to ecocode-rules-specifications/src/main/rules/EC543/swift/EC543.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB003.html b/ecocode-rules-specifications/src/main/rules/EC547/swift/EC547.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ios-swift/ESOB003.html rename to ecocode-rules-specifications/src/main/rules/EC547/swift/EC547.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EPOW001.html b/ecocode-rules-specifications/src/main/rules/EC602/swift/EC602.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules-mobile/ios-swift/EPOW001.html rename to ecocode-rules-specifications/src/main/rules/EC602/swift/EC602.asciidoc From 5a28f8d7b3b53e84b3d12d246c86ceec52f758b0 Mon Sep 17 00:00:00 2001 From: jycr Date: Mon, 3 Jul 2023 01:52:41 +0200 Subject: [PATCH 15/32] feat(rule/ios - swift): Convert HTML to ASCIIDOC --- ecocode-rules-specifications/pom.xml | 26 ++++++++- .../src/main/assembly/swift.xml | 18 +++++++ .../src/main/rules/EC505/swift/EC505.asciidoc | 20 ++++--- .../src/main/rules/EC506/swift/EC506.asciidoc | 33 ++++++------ .../src/main/rules/EC521/swift/EC521.asciidoc | 21 ++++---- .../src/main/rules/EC522/swift/EC522.asciidoc | 36 ++++++------- .../src/main/rules/EC523/swift/EC523.asciidoc | 19 ++++--- .../src/main/rules/EC524/swift/EC524.asciidoc | 33 +++++++----- .../src/main/rules/EC543/swift/EC543.asciidoc | 44 ++++++++------- .../src/main/rules/EC547/swift/EC547.asciidoc | 21 ++++---- .../src/main/rules/EC602/swift/EC602.asciidoc | 53 ++++++++++--------- 11 files changed, 193 insertions(+), 131 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/assembly/swift.xml diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index 81977d3d7..e2a351865 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 @@ -11,7 +12,9 @@ ecocode-rules-specifications ecoCode Rules Specifications repository - Repository that contains the specifications of every static-analysis rules available in ecoCode plugins. + Repository that contains the specifications of every static-analysis rules available in ecoCode + plugins. + https://github.com/green-code-initiative/ecoCode/tree/main/ecocode-rules-specifications @@ -147,6 +150,13 @@ + + + + + + + @@ -228,6 +238,18 @@ + + assembly-swift + prepare-package + + single + + + + ${project.basedir}/src/main/assembly/swift.xml + + + true diff --git a/ecocode-rules-specifications/src/main/assembly/swift.xml b/ecocode-rules-specifications/src/main/assembly/swift.xml new file mode 100644 index 000000000..2ea8b63a9 --- /dev/null +++ b/ecocode-rules-specifications/src/main/assembly/swift.xml @@ -0,0 +1,18 @@ + + swift + + jar + + false + + + ${project.build.outputDirectory} + + io/ecocode/rules/swift/*.* + + + + + diff --git a/ecocode-rules-specifications/src/main/rules/EC505/swift/EC505.asciidoc b/ecocode-rules-specifications/src/main/rules/EC505/swift/EC505.asciidoc index bce45601b..0346e3c1f 100644 --- a/ecocode-rules-specifications/src/main/rules/EC505/swift/EC505.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC505/swift/EC505.asciidoc @@ -1,9 +1,13 @@ - -

To avoid draining the battery, an iOS device that is left idle quickly falls asleep.
- Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically - disable the idle timer with UIApplication.shared.isIdleTimerDisabled = true.

-

Noncompliant Code Example

-
-    UIApplication.shared.isIdleTimerDisabled = true
-
+Score ecoCode: 2sur5 + +To avoid draining the battery, an iOS device that is left idle quickly falls asleep. + +Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically +disable the idle timer with `UIApplication.shared.isIdleTimerDisabled = true`. + +## Noncompliant Code Example + +```swift +UIApplication.shared.isIdleTimerDisabled = true +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC506/swift/EC506.asciidoc b/ecocode-rules-specifications/src/main/rules/EC506/swift/EC506.asciidoc index c2ececa2d..f672a2088 100644 --- a/ecocode-rules-specifications/src/main/rules/EC506/swift/EC506.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC506/swift/EC506.asciidoc @@ -1,17 +1,16 @@ - -

Setting a tolerance for timers will allow them to fire later than the scheduled fire date. - The system will use this flexibility to shift the execution of timers by small amounts of time, - within their tolerances, increasing the ability to optimize power savings. - Your app can set the Timer#tolerance property to specify a tolerance for a timer. - Using this approach dramatically increases the amount of time that the processor spends idling - while users detect no change in system responsiveness. -

-

Noncompliant Code Example

-
-    let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in }
-
-

Compliant Code Example

-
-    let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in }
-    timer.tolerance = 0.5
-
\ No newline at end of file +Score ecoCode: 2sur5 + +Setting a tolerance for timers will allow them to fire later than the scheduled fire date. The system will use this flexibility to shift the execution of timers by small amounts of time, within their tolerances, increasing the ability to optimize power savings. Your app can set the `Timer#tolerance` property to specify a tolerance for a timer. Using this approach dramatically increases the amount of time that the processor spends idling while users detect no change in system responsiveness. + +## Noncompliant Code Example + +```swift +let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in } +``` + +## Compliant Code Example + +```swift +let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in } +timer.tolerance = 0.5 +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC521/swift/EC521.asciidoc b/ecocode-rules-specifications/src/main/rules/EC521/swift/EC521.asciidoc index d8d5374fd..1ee38ee48 100644 --- a/ecocode-rules-specifications/src/main/rules/EC521/swift/EC521.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC521/swift/EC521.asciidoc @@ -1,12 +1,11 @@ - -

Allowing the system to pause location updates, at times when the location data is unlikely to change, can improve - battery life without sacrificing location data. This is the default behavior. For example, if the user stops for - food while using a navigation app, the location manager might pause updates for a period of time. It is possible - to disable this behavior, by setting the CLLocationManager#pausesLocationUpdatesAutomatically - property to false, but applications are strongly discouraged of doing it.

-

Noncompliant Code Example

-
-    let manager = CLLocationManager()
-    manager.pausesLocationUpdatesAutomatically = false
-
+Score ecoCode: 2sur5 + +Allowing the system to pause location updates, at times when the location data is unlikely to change, can improve battery life without sacrificing location data. This is the default behavior. For example, if the user stops for food while using a navigation app, the location manager might pause updates for a period of time. It is possible to disable this behavior, by setting the `CLLocationManager#pausesLocationUpdatesAutomatically` property to `false`, but applications are strongly discouraged of doing it. + +## Noncompliant Code Example + +```swift +let manager = CLLocationManager() +manager.pausesLocationUpdatesAutomatically = false +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC522/swift/EC522.asciidoc b/ecocode-rules-specifications/src/main/rules/EC522/swift/EC522.asciidoc index a148284bf..010df13d8 100644 --- a/ecocode-rules-specifications/src/main/rules/EC522/swift/EC522.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC522/swift/EC522.asciidoc @@ -1,21 +1,17 @@ - -

Location awareness is one of the most popular features used by apps. - By default standard location updates run with the best accuracy level, - but unless your app really needs to know the user’s position within a few meters, - this level of accuracy isn't needed. - Requesting higher accuracy than you need causes the system to power up additional hardware - and waste power for unnecessary precision. You can specify a degree of accuracy by setting - the CLLocationManager#desiredAccuracy property. - Also, setting the CLLocationManager#activityType property will let the system knows what type - of location activity your app is performing and helps it determine - the most appropriate time to perform location updates. - Finally, if your app just needs a quick fix on the user’s location, - it’s best to call the CLLocationManager#requestLocation method, - that will deliver a single location update. -

-

Compliant Code Example

-
-    let manager = CLLocationManager()
-    manager.desiredAccuracy = 2
-
+Score ecoCode: 2sur5 + +Location awareness is one of the most popular features used by apps. + +By default, standard location updates run with the best accuracy level, but unless your app really needs to know the user’s position within a few meters, this level of accuracy isn't needed. + +Requesting higher accuracy than you need causes the system to power up additional hardware and waste power for unnecessary precision. You can specify a degree of accuracy by setting the `CLLocationManager#desiredAccuracy` property. + +Also, setting the `CLLocationManager#activityType` property will let the system knows what type of location activity your app is performing and helps it determine the most appropriate time to perform location updates. Finally, if your app just needs a quick fix on the user’s location, it’s best to call the `CLLocationManager#requestLocation` method, that will deliver a single location update. + +## Compliant Code Example + +```swift +let manager = CLLocationManager() +manager.desiredAccuracy = 2 +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC523/swift/EC523.asciidoc b/ecocode-rules-specifications/src/main/rules/EC523/swift/EC523.asciidoc index f03eb5a1b..530e2ff7f 100644 --- a/ecocode-rules-specifications/src/main/rules/EC523/swift/EC523.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC523/swift/EC523.asciidoc @@ -1,9 +1,12 @@ - -

To avoid draining the battery, an iOS device adapt its brightness to the environment .
- Hence, keeping forcing the screen brightness on should be avoided, unless it is absolutely necessary. If so, developers typically - force the Brightness to 1 with UIScreen.main.brightness = a float.

-

Noncompliant Code Example

-
-    UIScreen.main.brightness = CGFloat(0.3)
-
+Score ecoCode: 2sur5 + +To avoid draining the battery, an iOS device adapt its brightness to the environment . + +Hence, keeping forcing the screen brightness on should be avoided, unless it is absolutely necessary. If so, developers typically force the Brightness to 1 with `UIScreen.main.brightness = a float`. + +## Noncompliant Code Example + +```swift +UIScreen.main.brightness = CGFloat(0.3) +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC524/swift/EC524.asciidoc b/ecocode-rules-specifications/src/main/rules/EC524/swift/EC524.asciidoc index 6ade65c00..18b4651a1 100644 --- a/ecocode-rules-specifications/src/main/rules/EC524/swift/EC524.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC524/swift/EC524.asciidoc @@ -1,14 +1,21 @@ - -

Turning on the torch mode programmatically must absolutely be avoided because the flashlight is one of the most energy-intensive component.

-

Noncompliant Code Example

-
-    AVCaptureTorchMode.on
-
-OR -
+Score ecoCode: 2sur5
+
+Turning on the torch mode programmatically must absolutely be avoided because the flashlight is one of the most energy-intensive component.
+
+## Noncompliant Code Example
+
+```swift
+AVCaptureTorchMode.on
+```
+
+or
+
+```swift
     setTorchModeOn(level: Float)
-
-OR -
-    torchMode = .on
-
\ No newline at end of file +``` + +or + +```swift +torchMode = .on +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC543/swift/EC543.asciidoc b/ecocode-rules-specifications/src/main/rules/EC543/swift/EC543.asciidoc index 31fd61ae6..818fea1ad 100644 --- a/ecocode-rules-specifications/src/main/rules/EC543/swift/EC543.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC543/swift/EC543.asciidoc @@ -1,19 +1,25 @@ - -

Use device API to check if low power mode is enabled, and adapt resources usage accordingly. - For example, you can reduce frequency of data update if low power mode is enabled. - Your app can query the ProcessInfo.processInfo.isLowPowerModeEnabled property at any time - to determine whether Low Power Mode is active. Your app can also register to receive notifications - when the Low Power Mode state of a device changes, using NSProcessInfoPowerStateDidChange. -

-

Compliant Code Example

-
-    let isLowPowerModeEnabled = ProcessInfo.processInfo.isLowPowerModeEnabled
-
-OR -
-    NotificationCenter.default.addObserver(forName: .NSProcessInfoPowerStateDidChange, object: nil, queue: nil) { _ in }
-
-OR -
-    NotificationCenter.default.addObserver(forName: Notification.Name.NSProcessInfoPowerStateDidChange, object: nil, queue: nil) { _ in }
-
\ No newline at end of file +Score ecoCode: 5sur5 + +Use device API to check if low power mode is enabled, and adapt resources usage accordingly. + +For example, you can reduce frequency of data update if low power mode is enabled. + +Your app can query the `ProcessInfo.processInfo.isLowPowerModeEnabled` property at any time to determine whether Low Power Mode is active. Your app can also register to receive notifications when the Low Power Mode state of a device changes, using `NSProcessInfoPowerStateDidChange`. + +## Compliant Code Example + +```swift +let isLowPowerModeEnabled = ProcessInfo.processInfo.isLowPowerModeEnabled +``` + +or + +```swift +NotificationCenter.default.addObserver(forName: .NSProcessInfoPowerStateDidChange, object: nil, queue: nil) { _ in } +``` + +or + +```swift +NotificationCenter.default.addObserver(forName: Notification.Name.NSProcessInfoPowerStateDidChange, object: nil, queue: nil) { _ in } +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC547/swift/EC547.asciidoc b/ecocode-rules-specifications/src/main/rules/EC547/swift/EC547.asciidoc index 0b1ea6548..10fe4aaa1 100644 --- a/ecocode-rules-specifications/src/main/rules/EC547/swift/EC547.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC547/swift/EC547.asciidoc @@ -1,9 +1,12 @@ - -

Using the Core Motion API, your app can receive continuous motion updates in the form of accelerometer, gyroscope, and device motion (rotation, acceleration, and more) events. Yet you don't often need these updates as soon as they are generated. Before registering to receive these recurring motion updates, you can specify an interval that meets your app’s needs, using CMMotionManager#accelerometerUpdateInterval, CMMotionManager#gyroUpdateInterval, CMMotionManager#deviceMotionUpdateInterval and CMMotionManager#magnetometerUpdateInterval properties. The larger the interval, the fewer events are delivered to your app, improving battery life.

-

Compliant Code

-
-    CMMotionManager.accelerometerUpdateInterval = 1000
-    CMMotionManager.gyroUpdateInterval = 1000
-    CMMotionManager.deviceMotionUpdateInterval = 1000
-    CMMotionManager.magnetometerUpdateInterval = 1000
-
+Score ecoCode: 2sur5 + +Using the Core Motion API, your app can receive continuous motion updates in the form of accelerometer, gyroscope, and device motion (rotation, acceleration, and more) events. Yet you don't often need these updates as soon as they are generated. Before registering to receive these recurring motion updates, you can specify an interval that meets your app’s needs, using `CMMotionManager#accelerometerUpdateInterval`, `CMMotionManager#gyroUpdateInterval`, `CMMotionManager#deviceMotionUpdateInterval` and `CMMotionManager#magnetometerUpdateInterval` properties. The larger the interval, the fewer events are delivered to your app, improving battery life. + +## Compliant Code + +```swift +CMMotionManager.accelerometerUpdateInterval = 1000 +CMMotionManager.gyroUpdateInterval = 1000 +CMMotionManager.deviceMotionUpdateInterval = 1000 +CMMotionManager.magnetometerUpdateInterval = 1000 +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC602/swift/EC602.asciidoc b/ecocode-rules-specifications/src/main/rules/EC602/swift/EC602.asciidoc index 75de56f9a..629d5ad5b 100644 --- a/ecocode-rules-specifications/src/main/rules/EC602/swift/EC602.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC602/swift/EC602.asciidoc @@ -1,24 +1,29 @@ - -

It's always good that an app has different behavior when device is connected/disconnected to a power station, - or has different battery levels. Your app can query the UIDevice#batteryLevel and - UIDevice#batteryState properties at any time to determine the level of charge and - the state of the battery. Your app can also register to receive notifications when the battery level - or state changes, using batteryLevelDidChangeNotification - and batteryStateDidChangeNotification. -

-

Compliant Code Example

-
-    let level = UIDevice.current.batteryLevel
-
-OR -
-    let state = UIDevice.current.batteryState
-
-OR -
-    NotificationCenter.default.addObserver(forName: UIDevice.batteryLevelDidChangeNotification, object: nil, queue: nil) { _ in }
-
-OR -
-    NotificationCenter.default.addObserver(forName: UIDevice.batteryStateDidChangeNotification, object: nil, queue: nil) { _ in }
-
\ No newline at end of file +Score ecoCode: 5sur5 + +It's always good that an app has different behavior when device is connected/disconnected to a power station, or has different battery levels. Your app can query the `UIDevice#batteryLevel` and `UIDevice#batteryState` properties at any time to determine the level of charge and the state of the battery. + +Your app can also register to receive notifications when the battery level or state changes, using `batteryLevelDidChangeNotification` and `batteryStateDidChangeNotification`. + +## Compliant Code Example + +```swift +let level = UIDevice.current.batteryLevel +``` + +or + +```swift +let state = UIDevice.current.batteryState +``` + +or + +```swift +NotificationCenter.default.addObserver(forName: UIDevice.batteryLevelDidChangeNotification, object: nil, queue: nil) { _ in } +``` + +or + +```swift +NotificationCenter.default.addObserver(forName: UIDevice.batteryStateDidChangeNotification, object: nil, queue: nil) { _ in } +``` From a7519830e7551572d6b8897653d5c9881061376d Mon Sep 17 00:00:00 2001 From: jycr Date: Mon, 3 Jul 2023 01:52:51 +0200 Subject: [PATCH 16/32] feat(rule/android - xml): Convert HTML to ASCIIDOC --- ecocode-rules-specifications/pom.xml | 22 ++++- .../src/main/assembly/xml.xml | 18 ++++ .../src/main/rules/EC540/xml/EC540.asciidoc | 35 ++++---- .../src/main/rules/EC541/xml/EC541.asciidoc | 42 +++++----- .../src/main/rules/EC542/xml/EC542.asciidoc | 29 ++++--- .../src/main/rules/EC543/xml/EC543.asciidoc | 21 ++--- .../src/main/rules/EC544/xml/EC544.asciidoc | 25 +++--- .../src/main/rules/EC545/xml/EC545.asciidoc | 84 ++++++++++--------- .../src/main/rules/EC546/xml/EC546.asciidoc | 29 +++---- .../src/main/rules/EC547/xml/EC547.asciidoc | 28 ++++--- .../src/main/rules/EC548/xml/EC548.asciidoc | 47 ++++++----- 11 files changed, 222 insertions(+), 158 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/assembly/xml.xml diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index e2a351865..ce2caae13 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -1,6 +1,5 @@ - + 4.0.0 @@ -157,6 +156,13 @@ + + + + + + +
@@ -250,6 +256,18 @@ + + assembly-xml + prepare-package + + single + + + + ${project.basedir}/src/main/assembly/xml.xml + + + true diff --git a/ecocode-rules-specifications/src/main/assembly/xml.xml b/ecocode-rules-specifications/src/main/assembly/xml.xml new file mode 100644 index 000000000..bffbcd328 --- /dev/null +++ b/ecocode-rules-specifications/src/main/assembly/xml.xml @@ -0,0 +1,18 @@ + + xml + + jar + + false + + + ${project.build.outputDirectory} + + io/ecocode/rules/xml/*.* + + + + + diff --git a/ecocode-rules-specifications/src/main/rules/EC540/xml/EC540.asciidoc b/ecocode-rules-specifications/src/main/rules/EC540/xml/EC540.asciidoc index 728486747..722d5d683 100644 --- a/ecocode-rules-specifications/src/main/rules/EC540/xml/EC540.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC540/xml/EC540.asciidoc @@ -1,16 +1,19 @@ - -

Services are long-living operations, as components of the apps. However, they can be started in isolation each time the device is next started, - without the user's acknowledgement.
- This technique should be discouraged because the accumulation of these silent services results in excessive battery depletion that remains - unexplained from the end-user's point of view.
- In addition, end-users know how to kill applications, but more rarely how to kill services.
- Thus, any developer should avoid having a call to Context#startService() from a Broadcast Receiver component that has specified an - intent-filter for the BOOT_COMPLETED action in the manifest.

-

Noncompliant Code Example

-
-    <receiver>
-        <intent-filter>
-            <action android:name= "android.intent.action.BOOT_COMPLETED"/>
-        </intent-filter>
-    </receiver>
-
\ No newline at end of file +Score ecoCode: 2sur5 + +Services are long-living operations, as components of the apps. However, they can be started in isolation each time the device is next started, without the user's acknowledgement. + +This technique should be discouraged because the accumulation of these silent services results in excessive battery depletion that remains unexplained from the end-user's point of view. + +In addition, end-users know how to kill applications, but more rarely how to kill services. + +Thus, any developer should avoid having a call to `Context#startService()` from a Broadcast Receiver component that has specified an intent-filter for the `BOOT_COMPLETED` action in the manifest. + +## Noncompliant Code Example + +```xml + + + + + +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC541/xml/EC541.asciidoc b/ecocode-rules-specifications/src/main/rules/EC541/xml/EC541.asciidoc index 3a1b04cc1..ddb1cf0c0 100644 --- a/ecocode-rules-specifications/src/main/rules/EC541/xml/EC541.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC541/xml/EC541.asciidoc @@ -1,19 +1,23 @@ - -

To avoid draining the battery, an Android device that is left idle quickly falls asleep.
- Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically - use the FLAG_KEEP_SCREEN_ON in their activity. Another way to implement this is in their application's - layout XML file, by using the android:keepScreenOn attribute.

-

Noncompliant Code Example

-
-    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:keepScreenOn="true"/>
-
-

Compliant Code Example

-
-    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:keepScreenOn="false"/>
-
+Score ecoCode: 2sur5 + +To avoid draining the battery, an Android device that is left idle quickly falls asleep. + +Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically use the `FLAG_KEEP_SCREEN_ON` in their activity. Another way to implement this is in their application's layout XML file, by using the `android:keepScreenOn` attribute. + +## Noncompliant Code Example + +```xml + +``` + +## Compliant Code Example + +```xml + +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC542/xml/EC542.asciidoc b/ecocode-rules-specifications/src/main/rules/EC542/xml/EC542.asciidoc index 48d4e09f4..b3b66f946 100644 --- a/ecocode-rules-specifications/src/main/rules/EC542/xml/EC542.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC542/xml/EC542.asciidoc @@ -1,12 +1,17 @@ - -

To avoid draining the battery, an Android device that is left idle quickly falls asleep. - Hence, keeping the screen on should be avoided, unless it is absolutely necessary.
- If so, developers typically use a Power Manager system service feature called wake locks - by invoking PowerManager.WakeLock#newWakeLock(int levelAndFlags, String tag), - along with the specific permission WAKE_LOCK in their manifest.

-

Noncompliant Code Example

-
-    <manifest>
-        <uses-permission android:name="android.permission.WAKE_LOCK" />
-    </manifest>
-
+Score ecoCode: 2sur5 + +To avoid draining the battery, an Android device that is left idle quickly falls asleep. + +Hence, keeping the screen on should be avoided, unless it is absolutely necessary. + +If so, developers typically use a Power Manager system service feature called wake locks +by invoking `PowerManager.WakeLock#newWakeLock(int levelAndFlags, String tag)`, +along with the specific permission `WAKE_LOCK` in their manifest. + +## Noncompliant Code Example + +```xml + + + +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC543/xml/EC543.asciidoc b/ecocode-rules-specifications/src/main/rules/EC543/xml/EC543.asciidoc index 90308e6a1..5eec80ead 100644 --- a/ecocode-rules-specifications/src/main/rules/EC543/xml/EC543.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC543/xml/EC543.asciidoc @@ -1,10 +1,11 @@ - -

A negative effect on the device s battery is when an app is paired with a companion device (over Bluetooth, BLE, or Wi-Fi) - and that it has been excluded from battery optimizations (run in the background) using the declaration - Manifest.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND.

-

Noncompliant Code Example

-
-    <manifest>
-       <uses-permission android:name="android.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND">
-    </manifest>
-
\ No newline at end of file +Score ecoCode: 2sur5 + +A negative effect on the device s battery is when an app is paired with a companion device (over Bluetooth, BLE, or Wi-Fi) and that it has been excluded from battery optimizations (run in the background) using the declaration `Manifest.permission.REQUEST_COMPANION_RUN_IN_BACKGROUND`. + +## Noncompliant Code Example + +```xml + + + +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC544/xml/EC544.asciidoc b/ecocode-rules-specifications/src/main/rules/EC544/xml/EC544.asciidoc index 3eefe003b..3abdafaf2 100644 --- a/ecocode-rules-specifications/src/main/rules/EC544/xml/EC544.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC544/xml/EC544.asciidoc @@ -1,12 +1,13 @@ - -

An app holding the permission REQUEST_IGNORE_BATTERY_OPTIMIZATIONS asks the user to allow it to ignore - battery optimizations (that is, put them on the whitelist of apps).

-

Most applications should not use this; there are many facilities provided by the platform for applications to - operate correctly in the various power saving modes.

- -

Noncompliant Code Example

-
-    <manifest>
-        <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
-    </manifest>
-
+Score ecoCode: 2sur5 + +An app holding the permission `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` asks the user to allow it to ignore battery optimizations (that is, put them on the whitelist of apps). + +Most applications should not use this; there are many facilities provided by the platform for applications to operate correctly in the various power saving modes. + +## Noncompliant Code Example + +```xml + + + +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC545/xml/EC545.asciidoc b/ecocode-rules-specifications/src/main/rules/EC545/xml/EC545.asciidoc index da8e00d62..9d1ebf6cb 100644 --- a/ecocode-rules-specifications/src/main/rules/EC545/xml/EC545.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC545/xml/EC545.asciidoc @@ -1,41 +1,43 @@ - -

It's always good that an app has different behavior when device is connected/disconnected - to a power station, or has different battery levels. - One can monitor the changes in charging state with a broadcast receiver registered - on the actions ACTION_POWER_CONNECTED - and ACTION_POWER_DISCONNECTED, or monitor significant - changes in battery level with a broadcast receiver registered - on the actions BATTERY_LOW and BATTERY_OKAY.

- -

Compliant Code Example

-
-    <receiver android:name="io.ecocode.testapp.core.service.MyReceiver">
-        <intent-filter>
-            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
-        </intent-filter>
-    </receiver>
-
-OR -
-    <receiver android:name="io.ecocode.testapp.core.service.MyReceiver">
-        <intent-filter>
-            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
-        </intent-filter>
-    </receiver>
-
-OR -
-    <receiver android:name="io.ecocode.testapp.core.service.MyReceiver">
-        <intent-filter>
-            <action android:name="android.intent.action.ACTION_POWER_BATTERY_OKAY"/>
-        </intent-filter>
-    </receiver>
-
-OR -
-    <receiver android:name="io.ecocode.testapp.core.service.MyReceiver">
-        <intent-filter>
-            <action android:name="android.intent.action.ACTION_POWER_BATTERY_LOW"/>
-        </intent-filter>
-    </receiver>
-
\ No newline at end of file +Score ecoCode: 5sur5 + +It's always good that an app has different behavior when device is connected/disconnected to a power station, or has different battery levels. One can monitor the changes in charging state with a broadcast receiver registered on the actions `ACTION_POWER_CONNECTED` and `ACTION_POWER_DISCONNECTED`, or monitor significant changes in battery level with a broadcast receiver registered on the actions `BATTERY_LOW` and `BATTERY_OKAY`. + +## Compliant Code Example + +```xml + + + + + +``` + +or + +```xml + + + + + +``` + +or + +```xml + + + + + +``` + +or + +```xml + + + + + +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC546/xml/EC546.asciidoc b/ecocode-rules-specifications/src/main/rules/EC546/xml/EC546.asciidoc index 9441c3a50..a342353a4 100644 --- a/ecocode-rules-specifications/src/main/rules/EC546/xml/EC546.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC546/xml/EC546.asciidoc @@ -1,14 +1,15 @@ - -

Taking into account when the device is entering or exiting the power save mode is higly desirable for the battery - life. - It implies the existence of a broadcast receiver registered on the action - ACTION_POWER_SAVE_MODE_CHANGED, - or programmaticaly with a call to PowerManager#isPowerSaveMode().

-

Compliant Code Example

-
-    <receiver android:name="io.ecocode.testapp.core.service.MyReceiver">
-        <intent-filter>
-            <action android:name="android.intent.action.BATTERY_CHANGED"/>
-        </intent-filter>
-    </receiver>
-
\ No newline at end of file +Score ecoCode: 5sur5 + +Taking into account when the device is entering or exiting the power save mode is higly desirable for the battery life. + +It implies the existence of a broadcast receiver registered on the action `ACTION_POWER_SAVE_MODE_CHANGED`, or programmatically with a call to `PowerManager#isPowerSaveMode().` + +## Compliant Code Example + +```xml + + + + + +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC547/xml/EC547.asciidoc b/ecocode-rules-specifications/src/main/rules/EC547/xml/EC547.asciidoc index eb63dc53f..4b8bcc02e 100644 --- a/ecocode-rules-specifications/src/main/rules/EC547/xml/EC547.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC547/xml/EC547.asciidoc @@ -1,12 +1,16 @@ - -

Displaying dark colors is particularly beneficial for mobile devices with (AM)OLED screens.
- Consequently, custom resources like bright colors values and bitmap images with too high luminance should be avoided.

-

Noncompliant Code Example

-
-    <TextView
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:foreground="#FFFFFF"
-        android:foregroundTint="#FFFFFF">
-    </TextView>
-
\ No newline at end of file +Score ecoCode: 2sur5 + +Displaying dark colors is particularly beneficial for mobile devices with (AM)OLED screens. + +Consequently, custom resources like bright colors values and bitmap images with too high luminance should be avoided. + +## Noncompliant Code Example + +```xml + + +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC548/xml/EC548.asciidoc b/ecocode-rules-specifications/src/main/rules/EC548/xml/EC548.asciidoc index 4b85c46cf..5ea8017df 100644 --- a/ecocode-rules-specifications/src/main/rules/EC548/xml/EC548.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC548/xml/EC548.asciidoc @@ -1,22 +1,29 @@ - -

Developers are allowed to apply native themes for their app, or derive new ones through inheritance. This decision - has a significant impact on energy consumption since displaying dark colors is particularly beneficial for mobile - devices with (AM)OLED screens.
- By default, Android will set Holo to the Dark theme (parent style Theme.Holo) and hence switching to the - light theme (parent style Theme.Holo.Light) within the manifest or in xml themes should be avoided.

-

Noncompliant Code Example

-
-    <style name="Theme.MyTheme" parent="@android:style/Theme.Holo.Light"/>
-
+Score ecoCode: 2sur5 + +Developers are allowed to apply native themes for their app, or derive new ones through inheritance. This decision has a significant impact on energy consumption since displaying dark colors is particularly beneficial for mobile devices with (AM)OLED screens. + +By default, Android will set Holo to the Dark theme (parent style `Theme.Holo`) and hence switching to the light theme (parent style `Theme.Holo.Light`) within the manifest or in xml themes should be avoided. + +## Noncompliant Code Example + +```xml + + +---- + +== Further details + +This recommendation is made by the CNUMR: https://github.com/cnumr/best-practices/blob/main/chapters/BP_027_en.md[Provide a print CSS] From 9ea9efa6b8150a651fb00d174c74ae7aa0581a47 Mon Sep 17 00:00:00 2001 From: utarwyn Date: Mon, 18 Sep 2023 20:52:23 +0200 Subject: [PATCH 26/32] Move all Javascript rules --- .../src/main/rules/EC11/EC11.json | 4 +- .../main/rules/EC11/javascript/EC11.asciidoc | 17 +++++---- .../src/main/rules/EC12/EC12.json | 4 +- .../main/rules/EC12/javascript/EC12.asciidoc | 14 +++---- .../src/main/rules/EC13/EC13.json | 6 +-- .../main/rules/EC13/javascript/EC13.asciidoc | 14 +++---- .../EC24/{js => javascript}/EC24.asciidoc | 0 .../EC25/{js => javascript}/EC25.asciidoc | 0 .../EC26/{js => javascript}/EC26.asciidoc | 0 .../EC29/{js => javascript}/EC29.asciidoc | 0 .../EC30/{js => javascript}/EC30.asciidoc | 0 .../src/main/rules/EC8/EC8.json | 4 +- .../main/rules/EC8/javascript/EC8.asciidoc | 19 +++++----- .../src/main/rules/EC9/EC9.json | 4 +- .../main/rules/EC9/javascript/EC9.asciidoc | 38 +++++-------------- 15 files changed, 54 insertions(+), 70 deletions(-) rename ecocode-rules-specifications/src/main/rules/EC24/{js => javascript}/EC24.asciidoc (100%) rename ecocode-rules-specifications/src/main/rules/EC25/{js => javascript}/EC25.asciidoc (100%) rename ecocode-rules-specifications/src/main/rules/EC26/{js => javascript}/EC26.asciidoc (100%) rename ecocode-rules-specifications/src/main/rules/EC29/{js => javascript}/EC29.asciidoc (100%) rename ecocode-rules-specifications/src/main/rules/EC30/{js => javascript}/EC30.asciidoc (100%) diff --git a/ecocode-rules-specifications/src/main/rules/EC11/EC11.json b/ecocode-rules-specifications/src/main/rules/EC11/EC11.json index 878a34665..1e878d469 100644 --- a/ecocode-rules-specifications/src/main/rules/EC11/EC11.json +++ b/ecocode-rules-specifications/src/main/rules/EC11/EC11.json @@ -7,9 +7,9 @@ "constantCost": "5min" }, "tags": [ + "ecocode", "eco-design", - "performance", - "ecocode" + "performance" ], "defaultSeverity": "Major", "compatibleLanguages": [ diff --git a/ecocode-rules-specifications/src/main/rules/EC11/javascript/EC11.asciidoc b/ecocode-rules-specifications/src/main/rules/EC11/javascript/EC11.asciidoc index b5a2a8649..a932fa16c 100644 --- a/ecocode-rules-specifications/src/main/rules/EC11/javascript/EC11.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC11/javascript/EC11.asciidoc @@ -1,20 +1,21 @@ -## Rule details +This rule aims to reduce DOM access assigning its object to variable when access multiple time. +It saves CPU cycles. -This rule aims to reduce DOM access assigning its object to variable when access multiple time. It saves CPU cycles. - -## Examples +== Examples Examples of **incorrect** code for this rule: -```js +[source,js] +---- var el1 = document.getElementById("block1").test1; var el2 = document.getElementById("block1").test2; -``` +---- Examples of **correct** code for this rule: -```js +[source,js] +---- var blockElement = document.getElementById("block1"); var el1 = blockElement.test1; var el2 = blockElement.test2; -``` +---- diff --git a/ecocode-rules-specifications/src/main/rules/EC12/EC12.json b/ecocode-rules-specifications/src/main/rules/EC12/EC12.json index 67c6010cf..06c452765 100644 --- a/ecocode-rules-specifications/src/main/rules/EC12/EC12.json +++ b/ecocode-rules-specifications/src/main/rules/EC12/EC12.json @@ -7,9 +7,9 @@ "constantCost": "10min" }, "tags": [ + "ecocode", "eco-design", - "performance", - "ecocode" + "performance" ], "defaultSeverity": "Major", "compatibleLanguages": [ diff --git a/ecocode-rules-specifications/src/main/rules/EC12/javascript/EC12.asciidoc b/ecocode-rules-specifications/src/main/rules/EC12/javascript/EC12.asciidoc index 5bd836997..7793d7389 100644 --- a/ecocode-rules-specifications/src/main/rules/EC12/javascript/EC12.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC12/javascript/EC12.asciidoc @@ -1,24 +1,24 @@ -## Rule Details - This rule aims to disallow batching multiple style changes at once. To limit the number of repaint/reflow, it is advised to batch style modifications by adding a class containing all style changes that will generate a unique reflow. -## Examples +== Examples Examples of **non-compliant** code for this rule: -```html +[source,html] +---- -``` +---- Examples of **compliant** code for this rule: -```html +[source,html] +----