From 344db238ffb52cd1d67e074b09d3b9be3d1cdc27 Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Wed, 29 May 2024 15:16:34 +0200 Subject: [PATCH 1/4] Added EC514 for Swift --- .../src/main/rules/EC514/java/EC514.asciidoc | 48 +++++++++++++------ .../src/main/rules/EC514/swift/EC514.asciidoc | 24 ++++++++++ 2 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc 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 2f06dd287..67acb5c89 100644 --- a/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC514/java/EC514.asciidoc @@ -1,24 +1,44 @@ -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). +Most iOS devices have built-in sensors that measure motion, orientation, and various environmental conditions. Additionally, they have image sensors (a.k.a. Camera) and geo-positioning sensors (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. +The common point of all these sensors is that they consume significant power while in use. Their common issue is processing data unnecessarily when the app is in an idle state, typically when it enters the background or becomes inactive. - Consequently, calls must be carefully pairwised: `SensorManager#registerListener()/unregisterListener()`. - Failing to do so can drain the battery in just a few hours. + Consequently, calls to start and stop sensor updates must be carefully managed for motion sensor: CMMotionManager#startAccelerometerUpdates()/CMMotionManager#stopAccelerometerUpdates(). + Failing to do so can drain the battery quickly. ## 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); +```swift +import CoreMotion + +let motionManager = CMMotionManager() + +func startMotionUpdates() { + if motionManager.isAccelerometerAvailable { + motionManager.startAccelerometerUpdates(to: .main) { data, error in + // Handle accelerometer updates + } + } +} ``` ## 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); +```swift +import CoreMotion + +let motionManager = CMMotionManager() + +func startMotionUpdates() { + if motionManager.isAccelerometerAvailable { + motionManager.startAccelerometerUpdates(to: .main) { data, error in + // Handle accelerometer updates + } + } +} + +func stopMotionUpdates() { + if motionManager.isAccelerometerActive { + motionManager.stopAccelerometerUpdates() + } +} ``` diff --git a/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc new file mode 100644 index 000000000..2f06dd287 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc @@ -0,0 +1,24 @@ +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); +``` From b93f39953bf7447165833ef2fe4caa8e1f6b0e92 Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Wed, 29 May 2024 15:18:52 +0200 Subject: [PATCH 2/4] Corrected EC514 content --- .../src/main/rules/EC514/java/EC514.asciidoc | 48 ++++++------------- .../src/main/rules/EC514/swift/EC514.asciidoc | 48 +++++++++++++------ 2 files changed, 48 insertions(+), 48 deletions(-) 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 67acb5c89..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,44 +1,24 @@ -Most iOS devices have built-in sensors that measure motion, orientation, and various environmental conditions. Additionally, they have image sensors (a.k.a. Camera) and geo-positioning sensors (a.k.a. GPS). +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 consume significant power while in use. Their common issue is processing data unnecessarily when the app is in an idle state, typically when it enters the background or becomes inactive. +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 to start and stop sensor updates must be carefully managed for motion sensor: CMMotionManager#startAccelerometerUpdates()/CMMotionManager#stopAccelerometerUpdates(). - Failing to do so can drain the battery quickly. + 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 -```swift -import CoreMotion - -let motionManager = CMMotionManager() - -func startMotionUpdates() { - if motionManager.isAccelerometerAvailable { - motionManager.startAccelerometerUpdates(to: .main) { data, error in - // Handle accelerometer updates - } - } -} +```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 -```swift -import CoreMotion - -let motionManager = CMMotionManager() - -func startMotionUpdates() { - if motionManager.isAccelerometerAvailable { - motionManager.startAccelerometerUpdates(to: .main) { data, error in - // Handle accelerometer updates - } - } -} - -func stopMotionUpdates() { - if motionManager.isAccelerometerActive { - motionManager.stopAccelerometerUpdates() - } -} +```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/EC514/swift/EC514.asciidoc b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc index 2f06dd287..67acb5c89 100644 --- a/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc @@ -1,24 +1,44 @@ -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). +Most iOS devices have built-in sensors that measure motion, orientation, and various environmental conditions. Additionally, they have image sensors (a.k.a. Camera) and geo-positioning sensors (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. +The common point of all these sensors is that they consume significant power while in use. Their common issue is processing data unnecessarily when the app is in an idle state, typically when it enters the background or becomes inactive. - Consequently, calls must be carefully pairwised: `SensorManager#registerListener()/unregisterListener()`. - Failing to do so can drain the battery in just a few hours. + Consequently, calls to start and stop sensor updates must be carefully managed for motion sensor: CMMotionManager#startAccelerometerUpdates()/CMMotionManager#stopAccelerometerUpdates(). + Failing to do so can drain the battery quickly. ## 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); +```swift +import CoreMotion + +let motionManager = CMMotionManager() + +func startMotionUpdates() { + if motionManager.isAccelerometerAvailable { + motionManager.startAccelerometerUpdates(to: .main) { data, error in + // Handle accelerometer updates + } + } +} ``` ## 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); +```swift +import CoreMotion + +let motionManager = CMMotionManager() + +func startMotionUpdates() { + if motionManager.isAccelerometerAvailable { + motionManager.startAccelerometerUpdates(to: .main) { data, error in + // Handle accelerometer updates + } + } +} + +func stopMotionUpdates() { + if motionManager.isAccelerometerActive { + motionManager.stopAccelerometerUpdates() + } +} ``` From 4cfe35165df3a91b6a9f10fcd0871deec27b9026 Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Wed, 29 May 2024 15:25:21 +0200 Subject: [PATCH 3/4] Updated CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21a7ee4bb..a5ba9f1d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- [#306] (https://github.com/green-code-initiative/ecoCode/issues/306) Swift port of rule EC514 + ### Changed ### Deleted From 07ebadb693a83886d5447a772ae1a9418f585e08 Mon Sep 17 00:00:00 2001 From: Gilles Grousset Date: Thu, 30 May 2024 13:57:18 +0200 Subject: [PATCH 4/4] Update EC514.asciidoc --- .../src/main/rules/EC514/swift/EC514.asciidoc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc index 67acb5c89..37eb6dcb4 100644 --- a/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC514/swift/EC514.asciidoc @@ -5,9 +5,10 @@ The common point of all these sensors is that they consume significant power whi Consequently, calls to start and stop sensor updates must be carefully managed for motion sensor: CMMotionManager#startAccelerometerUpdates()/CMMotionManager#stopAccelerometerUpdates(). Failing to do so can drain the battery quickly. -## Noncompliant Code Example +== Noncompliant Code Example -```swift +[source,swift] +---- import CoreMotion let motionManager = CMMotionManager() @@ -19,11 +20,12 @@ func startMotionUpdates() { } } } -``` +---- -## Compliant Code Example +== Compliant Code Example -```swift +[source,swift] +---- import CoreMotion let motionManager = CMMotionManager() @@ -41,4 +43,4 @@ func stopMotionUpdates() { motionManager.stopAccelerometerUpdates() } } -``` +----