Code symmetry

This commit is contained in:
Benoit Marty 2020-09-04 18:45:49 +02:00
parent 8ead371603
commit f689871fc0
1 changed files with 10 additions and 6 deletions

View File

@ -46,13 +46,15 @@ class CallProximityManager @Inject constructor(
private val sensorManager = context.getSystemService<SensorManager>()!! private val sensorManager = context.getSystemService<SensorManager>()!!
private var wakeLock: PowerManager.WakeLock? = null private var wakeLock: PowerManager.WakeLock? = null
private var sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY) private val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)
private val isSupported = sensor != null && powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)
/** /**
* Start listening the proximity sensor. [stop] function should be called to release the sensor and the WakeLock. * Start listening the proximity sensor. [stop] function should be called to release the sensor and the WakeLock.
*/ */
fun start() { fun start() {
if (sensor != null && powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) { if (isSupported) {
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL) sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL)
} }
} }
@ -61,10 +63,12 @@ class CallProximityManager @Inject constructor(
* Stop listening proximity sensor changes and release the WakeLock. * Stop listening proximity sensor changes and release the WakeLock.
*/ */
fun stop() { fun stop() {
sensorManager.unregisterListener(this) if (isSupported) {
wakeLock sensorManager.unregisterListener(this)
?.takeIf { it.isHeld } wakeLock
?.release() ?.takeIf { it.isHeld }
?.release()
}
} }
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) { override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {