From f689871fc0cf3a99b7b7bfab63571fe5ed2d9b65 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 4 Sep 2020 18:45:49 +0200 Subject: [PATCH] Code symmetry --- .../app/features/call/CallProximityManager.kt | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/call/CallProximityManager.kt b/vector/src/main/java/im/vector/app/features/call/CallProximityManager.kt index 1b561f7990..74e6c40783 100644 --- a/vector/src/main/java/im/vector/app/features/call/CallProximityManager.kt +++ b/vector/src/main/java/im/vector/app/features/call/CallProximityManager.kt @@ -46,13 +46,15 @@ class CallProximityManager @Inject constructor( private val sensorManager = context.getSystemService()!! 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. */ fun start() { - if (sensor != null && powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) { + if (isSupported) { 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. */ fun stop() { - sensorManager.unregisterListener(this) - wakeLock - ?.takeIf { it.isHeld } - ?.release() + if (isSupported) { + sensorManager.unregisterListener(this) + wakeLock + ?.takeIf { it.isHeld } + ?.release() + } } override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {