Skip to content

Commit bbaa5f8

Browse files
authored
Merge pull request #59 from ahmedcove1/main
[iOs][ESOB002][ESOB003] Implement 2 sobriety rules
2 parents 3405184 + 66e7c57 commit bbaa5f8

File tree

14 files changed

+473
-98
lines changed

14 files changed

+473
-98
lines changed

ios-plugin/RULES.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ Some rules have been already implemented in the plugin. Table of unimplemented r
88

99
| # | **Rule Name** | **Scanner** | **Observation** |
1010
|---|:----------------|:-------------:|:-------------:|
11-
| ESOB002 | Thrifty Geolocation | Swift | |
12-
| ESOB003 | Motion Sensor Update Rate | Swift | |
1311
| ESOB004 | Disabled Dark Mode | Xml | Plist scanning |
1412
| ESOB007 | Animation-free | Swift | |
1513

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* ecoCode iOS plugin - Help the earth, adopt this green plugin for your applications
3+
* Copyright © 2022 Green code Initiative (https://www.ecocode.io/)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package io.ecocode.ios.swift.checks;
19+
20+
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
21+
import org.antlr.v4.runtime.tree.ParseTree;
22+
23+
public class CheckHelper {
24+
private CheckHelper() {
25+
}
26+
27+
public static boolean isImportExisting(ParseTree tree, String importName) {
28+
return (tree instanceof Swift5Parser.Import_declarationContext && tree.getText().contains(importName));
29+
}
30+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* ecoCode iOS plugin - Help the earth, adopt this green plugin for your applications
3+
* Copyright © 2022 Green code Initiative (https://www.ecocode.io/)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package io.ecocode.ios.swift.checks.geolocalisation;
19+
20+
import io.ecocode.ios.checks.RuleCheck;
21+
import io.ecocode.ios.swift.RegisterRule;
22+
import io.ecocode.ios.swift.Swift;
23+
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
24+
import org.antlr.v4.runtime.tree.ParseTree;
25+
import org.antlr.v4.runtime.tree.TerminalNodeImpl;
26+
27+
import static io.ecocode.ios.swift.checks.CheckHelper.isImportExisting;
28+
29+
@RegisterRule
30+
public class ThriftyGeolocation extends RuleCheck {
31+
Swift5Parser.Import_declarationContext importTree = null;
32+
private boolean geolocationUpdated = false;
33+
protected boolean importExist = false;
34+
35+
public ThriftyGeolocation() {
36+
super("ESOB002", Swift.RULES_PATH, Swift.REPOSITORY_KEY);
37+
}
38+
39+
@Override
40+
public void apply(ParseTree tree) {
41+
if (isImportExisting(tree, "CLLocationManager")) {
42+
importTree = (Swift5Parser.Import_declarationContext) tree;
43+
importExist = true;
44+
}
45+
46+
if (!geolocationUpdated
47+
&& tree instanceof Swift5Parser.ExpressionContext
48+
&& (tree.getText().contains("desiredAccuracy") || tree.getText().contains("CLActivityType"))) {
49+
geolocationUpdated = true;
50+
}
51+
52+
if (tree instanceof TerminalNodeImpl && tree.getText().equals("<EOF>")) {
53+
if (importExist && !geolocationUpdated) {
54+
this.recordIssue(ruleId, importTree.getStart().getStartIndex());
55+
}
56+
importExist = false;
57+
geolocationUpdated = false;
58+
}
59+
}
60+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* ecoCode iOS plugin - Help the earth, adopt this green plugin for your applications
3+
* Copyright © 2022 Green code Initiative (https://www.ecocode.io/)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package io.ecocode.ios.swift.checks.motionsensor;
19+
20+
import io.ecocode.ios.checks.RuleCheck;
21+
import io.ecocode.ios.swift.RegisterRule;
22+
import io.ecocode.ios.swift.Swift;
23+
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
24+
import org.antlr.v4.runtime.tree.ParseTree;
25+
import org.antlr.v4.runtime.tree.TerminalNodeImpl;
26+
27+
import java.util.Arrays;
28+
import java.util.List;
29+
30+
import static io.ecocode.ios.swift.checks.CheckHelper.isImportExisting;
31+
32+
@RegisterRule
33+
public class MotionSensorUpdateRateCheck extends RuleCheck {
34+
Swift5Parser.Import_declarationContext importTree = null;
35+
private boolean sensorRateUpdated = false;
36+
private boolean importExist = false;
37+
38+
private final List<String> sensorRateUpdateExpressions = Arrays.asList("desiredAccuracy", "activityType", "requestLocation", "magnetometerUpdateInterval");
39+
40+
public MotionSensorUpdateRateCheck() {
41+
super("ESOB003", Swift.RULES_PATH, Swift.REPOSITORY_KEY);
42+
}
43+
44+
@Override
45+
public void apply(ParseTree tree) {
46+
if (isImportExisting(tree, "CoreMotion")) {
47+
importTree = (Swift5Parser.Import_declarationContext) tree;
48+
importExist = true;
49+
}
50+
51+
if (!sensorRateUpdated && tree instanceof Swift5Parser.ExpressionContext) {
52+
sensorRateUpdated = sensorRateUpdateExpressions.stream().anyMatch(exp -> tree.getText().contains(exp));
53+
}
54+
55+
if (tree instanceof TerminalNodeImpl && tree.getText().equals("<EOF>")) {
56+
if (importExist && !sensorRateUpdated) {
57+
this.recordIssue(ruleId, importTree.getStart().getStartIndex());
58+
}
59+
importExist = false;
60+
sensorRateUpdated = false;
61+
}
62+
}
63+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<img src="http://www.neomades.com/extern/partage/ecoCode/2sur5_1x.png">
2+
<p>Location awareness is one of the most popular features used by apps.
3+
By default standard location updates run with the best accuracy level,
4+
but unless your app really needs to know the user’s position within a few meters,
5+
this level of accuracy isn't needed.
6+
Requesting higher accuracy than you need causes the system to power up additional hardware
7+
and waste power for unnecessary precision. You can specify a degree of accuracy by setting
8+
the <code>CLLocationManager#desiredAccuracy<code> property.
9+
Also, setting the <code>CLLocationManager#activityType</code> property will let the system knows what type
10+
of location activity your app is performing and helps it determine
11+
the most appropriate time to perform location updates.
12+
Finally, if your app just needs a quick fix on the user’s location,
13+
it’s best to call the <code>CLLocationManager#requestLocation</code> method,
14+
that will deliver a single location update.</p>
15+
<h2>Compliant Code Example</h2>
16+
<pre>
17+
let manager = CLLocationManager()
18+
manager.desiredAccuracy = 2
19+
</pre>
20+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<img src="http://www.neomades.com/extern/partage/ecoCode/2sur5_1x.png">
2+
<p>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 <code>CMMotionManager#accelerometerUpdateInterval</code>, <code>CMMotionManager#gyroUpdateInterval</code>, <code>CMMotionManager#deviceMotionUpdateInterval</code> and <code>CMMotionManager#magnetometerUpdateInterval</code> properties. The larger the interval, the fewer events are delivered to your app, improving battery life.</p>
3+
<h2>Compliant Code</h2>
4+
<pre>
5+
CMMotionManager.accelerometerUpdateInterval = 1000
6+
CMMotionManager.gyroUpdateInterval = 1000
7+
CMMotionManager.deviceMotionUpdateInterval = 1000
8+
CMMotionManager.magnetometerUpdateInterval = 1000
9+
</pre>

0 commit comments

Comments
 (0)