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 99719236bb..bcbf757ad0 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 @@ -16,14 +16,13 @@ package im.vector.app.features.call -import android.annotation.SuppressLint import android.content.Context import android.hardware.Sensor import android.hardware.SensorEvent import android.hardware.SensorEventListener import android.hardware.SensorManager import android.os.PowerManager -import androidx.core.content.ContextCompat.getSystemService +import androidx.core.content.getSystemService import im.vector.app.R import im.vector.app.core.resources.StringProvider import javax.inject.Inject @@ -31,21 +30,23 @@ import javax.inject.Inject /** * Manages the proximity sensor and turns the screen off when the proximity sensor activates. */ -class CallProximityManager @Inject constructor(val context: Context, - val stringProvider: StringProvider +class CallProximityManager @Inject constructor( + context: Context, + private val stringProvider: StringProvider ) : SensorEventListener { - private val PROXIMITY_WAKE_LOCK_TAG = "PROXIMITY_WAKE_LOCK_TAG" + companion object { + private const val PROXIMITY_WAKE_LOCK_TAG = "PROXIMITY_WAKE_LOCK_TAG" - private var powerManager: PowerManager = getSystemService(context, PowerManager::class.java)!! - private var sensorManager: SensorManager = getSystemService(context, SensorManager::class.java)!! + // 1 hour + private const val WAKE_LOCK_TIMEOUT_MILLIS = 3_600_000L + } + + private val powerManager = context.getSystemService()!! + private val sensorManager = context.getSystemService()!! private var wakeLock: PowerManager.WakeLock? = null - private var sensor: Sensor? = null - - init { - sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY) - } + private var sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY) /** * Start listening the proximity sensor. [stop] function should be called to release the sensor and the WakeLock. @@ -70,7 +71,7 @@ class CallProximityManager @Inject constructor(val context: Context, override fun onSensorChanged(event: SensorEvent) { val distanceInCentimeters = event.values[0] - if (distanceInCentimeters < sensor?.maximumRange ?: 0f) { + if (distanceInCentimeters < sensor?.maximumRange ?: 20f) { onProximityNear() } else { onProximityFar() @@ -82,10 +83,11 @@ class CallProximityManager @Inject constructor(val context: Context, */ private fun generateWakeLockTag() = "${stringProvider.getString(R.string.app_name)}:$PROXIMITY_WAKE_LOCK_TAG" - @SuppressLint("WakelockTimeout") private fun onProximityNear() { - wakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, generateWakeLockTag()) - wakeLock?.acquire() + if (wakeLock == null) { + wakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, generateWakeLockTag()) + } + wakeLock?.acquire(WAKE_LOCK_TIMEOUT_MILLIS) } private fun onProximityFar() { diff --git a/vector/src/main/java/im/vector/app/features/call/VectorCallViewModel.kt b/vector/src/main/java/im/vector/app/features/call/VectorCallViewModel.kt index 1fb9f83e1f..cee67cd818 100644 --- a/vector/src/main/java/im/vector/app/features/call/VectorCallViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/call/VectorCallViewModel.kt @@ -172,9 +172,7 @@ class VectorCallViewModel @AssistedInject constructor( } init { - initialState.callId?.let { - webRtcPeerConnectionManager.addCurrentCallListener(currentCallListener) session.callSignalingService().getCallWithId(it)?.let { mxCall ->