Most iOS devices come equipped with a variety of sensors that measure motion, orientation, and various environmental
+ conditions. Additionally, these devices include advanced sensors such as the image sensor (commonly referred to as
+ the Camera) and the geo-positioning sensor (commonly referred to as GPS).
+
+ The common point of all these sensors is that they are power-intensive while in use. A typical issue arises when
+ these sensors continue to process data unnecessarily after the application enters an idle state, like when it is
+ backgrounded or the user stops interacting with it.
+
+ As a result, calls to manage these sensors must be carefully paired: AVCaptureSession.startRunning()
and
+ AVCaptureSession.stopRunning()
. Failure to properly manage these calls can lead to significant battery drain within
+ a few hours.
+import AVFoundation + +class CameraManager { + var captureSession: AVCaptureSession? + + func activateCamera() { + captureSession = AVCaptureSession() + captureSession?.startRunning() // Camera starts capturing + // Missing corresponding stopRunning + } +} ++
+import AVFoundation + +class CameraManager { + var captureSession: AVCaptureSession? + + func activateCamera() { + captureSession = AVCaptureSession() + captureSession?.startRunning() // Camera starts capturing + } + + func deactivateCamera() { + captureSession?.stopRunning() // Camera stops capturing + } +} ++ diff --git a/swift-lang/src/main/resources/io/ecocode/rules/swift/EC512.json b/swift-lang/src/main/resources/io/ecocode/rules/swift/EC512.json new file mode 100644 index 0000000..0e532c2 --- /dev/null +++ b/swift-lang/src/main/resources/io/ecocode/rules/swift/EC512.json @@ -0,0 +1,18 @@ +{ + "key": "EC512", + "title": "Camera Leakage", + "defaultSeverity": "Major", + "description": "Any started capture session should be stopped.", + "status": "ready", + "remediation": { + "func": "Constant/Issue", + "constantCost": "5min" + }, + "tags": [ + "ecocode", + "environment", + "sobriety", + "eco-design" + ], + "type": "CODE_SMELL" +} diff --git a/swift-lang/src/test/java/io/ecocode/ios/swift/EcoCodeSwiftRulesDefinitionTest.java b/swift-lang/src/test/java/io/ecocode/ios/swift/EcoCodeSwiftRulesDefinitionTest.java index 2b88cee..646cdc8 100644 --- a/swift-lang/src/test/java/io/ecocode/ios/swift/EcoCodeSwiftRulesDefinitionTest.java +++ b/swift-lang/src/test/java/io/ecocode/ios/swift/EcoCodeSwiftRulesDefinitionTest.java @@ -54,8 +54,8 @@ public void testMetadata() { } @Test - public void testRegisteredRules() { - assertThat(repository.rules()).hasSize(10); + public void testRegistredRules() { + assertThat(repository.rules()).hasSize(11); } @Test diff --git a/swift-lang/src/test/java/io/ecocode/ios/swift/checks/camera/CameraLeakCheckTest.java b/swift-lang/src/test/java/io/ecocode/ios/swift/checks/camera/CameraLeakCheckTest.java new file mode 100644 index 0000000..2365df6 --- /dev/null +++ b/swift-lang/src/test/java/io/ecocode/ios/swift/checks/camera/CameraLeakCheckTest.java @@ -0,0 +1,51 @@ +/* + * ecoCode iOS plugin - Help the earth, adopt this green plugin for your applications + * Copyright © 2023 green-code-initiative (https://www.ecocode.io/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see