green-code-initiative / ecoCode-challenge

Emboard in the hackhatons serie for improving ecoCode
3 stars 4 forks source link

[React-Native][23cm] Optimize Mobile Sensor Usage #59

Open shemtovsamuel opened 1 year ago

shemtovsamuel commented 1 year ago

Energy Efficiency: Optimize Mobile Sensor Usage

Platform

OS OS version Language
Android/iOS All React Native

Main characteristics

ID Title Category Sub-category
- Optimize Mobile Sensor Usage Energy Efficiency Mobile Sensors

Severity / Remediation Costs

Severity Remediation Cost
Minor Minor

Rule short description

Limit the use of mobile sensors to necessary situations and disable them when their functionality is not required.

Rule complete description

To reduce energy consumption and make mobile applications more eco-friendly, it is crucial to limit the use of mobile sensors, such as accelerometers and gyroscopes, to necessary situations and disable them when their functionality is not required. This prolongs battery life and enhances the user experience.

HTML

<p>
  In order to reduce energy consumption and make mobile applications more
  eco-friendly, it is crucial to limit the use of mobile sensors, such as
  accelerometers and gyroscopes, to necessary situations and disable them when
  their functionality is not required. This prolongs battery life and enhances
  the user experience.
</p>
<h2>Noncompliant Code Example</h2>
<pre>
import {SensorManager} from 'react-native-sensors';

const sensorManager = new SensorManager();
sensorManager.start('Accelerometer', 1000);

// The sensor is not disabled when the functionality is no longer in use
</pre>
<h2>Compliant Solution</h2>
<pre>
import {SensorManager} from 'react-native-sensors';

const sensorManager = new SensorManager();

componentDidMount() {
  sensorManager.start('Accelerometer', 1000);
}

componentWillUnmount() {
  sensorManager.stop('Accelerometer'); // The sensor is properly disabled
}
</pre>

Implementation principle

  1. Analyze dependencies: Examine the package.json file to identify sensor-related libraries (e.g., react-native-sensors, react-native-geolocation, react-native-barometer, react-native-fingerprint-scanner).
  2. Locate imports of sensor-related libraries.
  3. Search for function calls that interact with sensors and identify the components or features that depend on them.
  4. Assess whether the use of sensors in each feature is relevant and necessary.
  5. Check if the application properly handles the activation and deactivation of sensors. Sensors should be enabled only when necessary and disabled when no longer in use.
  6. Look for component lifecycle management methods, such as componentDidMount and componentWillUnmount.
  7. If calls to sensors do not appear necessary, remove/disable them.
olegoaer commented 7 months ago

Obviously a good practice, similar to Sensor Leak in the ecoCode plugin for native Android (see here).

In the absence of a plugin dedicated to react-native technology, this proposal is in a pending state.

utarwyn commented 4 months ago

Hello! Sorry, I've only just seen this issue. I think it's a very good idea and the rule seems implementable in the ecoCode-javascript plugin because React's native code base is in javascript.

jhertout commented 2 months ago

I agree but I do not see this rule in the javascript project but in a dedicated react-native plugin. Or may be at least in the javascript plugin but in a dedicated integrated profile. I think we should set this rule as ok to be implemented.