Code cleanup

This commit is contained in:
Benoit Marty 2020-09-04 17:19:11 +02:00
parent e790c35270
commit 8045d61e1f
2 changed files with 18 additions and 18 deletions

View File

@ -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<PowerManager>()!!
private val sensorManager = context.getSystemService<SensorManager>()!!
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() {

View File

@ -172,9 +172,7 @@ class VectorCallViewModel @AssistedInject constructor(
}
init {
initialState.callId?.let {
webRtcPeerConnectionManager.addCurrentCallListener(currentCallListener)
session.callSignalingService().getCallWithId(it)?.let { mxCall ->