All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.java.S6889.html Maven / Gradle / Ivy

There is a newer version: 8.6.0.37351
Show newest version

Why is this an issue?

Optimizing resource usage and preventing unnecessary battery drain are critical considerations in Android development. Failing to release sensor resources when they are no longer needed can lead to prolonged device activity, negatively impacting battery life. Common Android sensors, such as cameras, GPS, and microphones, provide a method to release resources after they are not in use anymore.

This rule identifies situations where a sensor is not released after being utilized, helping developers maintain efficient and battery-friendly applications.

  • Missing call to release() method:
    • android.os.PowerManager.WakeLock
    • android.net.wifi.WifiManager$MulticastLock
    • android.hardware.Camera
    • android.media.MediaPlayer
    • android.media.MediaRecorder
    • android.media.SoundPool
    • android.media.audiofx.Visualizer
    • android.hardware.display.VirtualDisplay
  • Missing call to close() method
    • android.hardware.camera2.CameraDevice
  • Missing call to removeUpdates() method:
    • android.location.LocationManager
  • Missing call to unregisterListener() method:
    • android.hardware.SensorManager

How to fix it

Ensure that resources are released when they are no longer needed. This can be done by calling the appropriate release method, such as release(), removeUpdates(), unregisterListener(), or stop().

Code examples

  • android.os.PowerManager.WakeLock

Noncompliant code example

public void method() {
  PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
  PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Wake Lock");
  wakeLock.acquire(); // Noncompliant
  // do some work...
}

Compliant solution

public void method() {
  PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
  PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Wake Lock");
  wakeLock.acquire(); // Compliant
  // do some work...
  wakeLock.release();
}
  • android.media.MediaPlayer

Noncompliant code example

public void method() {
  MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
  mediaPlayer.start(); // Noncompliant
  // do some work...
}

Compliant solution

public void onCreate() {
  MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
  mediaPlayer.start(); // Compliant
  // do some work...
  wakeLock.release();
}
  • android.hardware.SensorManager

Noncompliant code example

public void method() {
  SensorManager sensorManager = getSystemService(SENSOR_SERVICE);
  Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); // Noncompliant
  // do some work...
}

Compliant solution

public void method() {
  SensorManager sensorManager = getSystemService(SENSOR_SERVICE);
  Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); // Compliant
  // do some work...
  sensorManager.unregisterListener(this);
}

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy