Code cleanup
This commit is contained in:
parent
e790c35270
commit
8045d61e1f
|
@ -16,14 +16,13 @@
|
||||||
|
|
||||||
package im.vector.app.features.call
|
package im.vector.app.features.call
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.hardware.Sensor
|
import android.hardware.Sensor
|
||||||
import android.hardware.SensorEvent
|
import android.hardware.SensorEvent
|
||||||
import android.hardware.SensorEventListener
|
import android.hardware.SensorEventListener
|
||||||
import android.hardware.SensorManager
|
import android.hardware.SensorManager
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
import androidx.core.content.ContextCompat.getSystemService
|
import androidx.core.content.getSystemService
|
||||||
import im.vector.app.R
|
import im.vector.app.R
|
||||||
import im.vector.app.core.resources.StringProvider
|
import im.vector.app.core.resources.StringProvider
|
||||||
import javax.inject.Inject
|
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.
|
* Manages the proximity sensor and turns the screen off when the proximity sensor activates.
|
||||||
*/
|
*/
|
||||||
class CallProximityManager @Inject constructor(val context: Context,
|
class CallProximityManager @Inject constructor(
|
||||||
val stringProvider: StringProvider
|
context: Context,
|
||||||
|
private val stringProvider: StringProvider
|
||||||
) : SensorEventListener {
|
) : 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)!!
|
// 1 hour
|
||||||
private var sensorManager: SensorManager = getSystemService(context, SensorManager::class.java)!!
|
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 wakeLock: PowerManager.WakeLock? = null
|
||||||
private var sensor: Sensor? = null
|
private var sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)
|
||||||
|
|
||||||
init {
|
|
||||||
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
@ -70,7 +71,7 @@ class CallProximityManager @Inject constructor(val context: Context,
|
||||||
|
|
||||||
override fun onSensorChanged(event: SensorEvent) {
|
override fun onSensorChanged(event: SensorEvent) {
|
||||||
val distanceInCentimeters = event.values[0]
|
val distanceInCentimeters = event.values[0]
|
||||||
if (distanceInCentimeters < sensor?.maximumRange ?: 0f) {
|
if (distanceInCentimeters < sensor?.maximumRange ?: 20f) {
|
||||||
onProximityNear()
|
onProximityNear()
|
||||||
} else {
|
} else {
|
||||||
onProximityFar()
|
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"
|
private fun generateWakeLockTag() = "${stringProvider.getString(R.string.app_name)}:$PROXIMITY_WAKE_LOCK_TAG"
|
||||||
|
|
||||||
@SuppressLint("WakelockTimeout")
|
|
||||||
private fun onProximityNear() {
|
private fun onProximityNear() {
|
||||||
|
if (wakeLock == null) {
|
||||||
wakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, generateWakeLockTag())
|
wakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, generateWakeLockTag())
|
||||||
wakeLock?.acquire()
|
}
|
||||||
|
wakeLock?.acquire(WAKE_LOCK_TIMEOUT_MILLIS)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onProximityFar() {
|
private fun onProximityFar() {
|
||||||
|
|
|
@ -172,9 +172,7 @@ class VectorCallViewModel @AssistedInject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
|
||||||
initialState.callId?.let {
|
initialState.callId?.let {
|
||||||
|
|
||||||
webRtcPeerConnectionManager.addCurrentCallListener(currentCallListener)
|
webRtcPeerConnectionManager.addCurrentCallListener(currentCallListener)
|
||||||
|
|
||||||
session.callSignalingService().getCallWithId(it)?.let { mxCall ->
|
session.callSignalingService().getCallWithId(it)?.let { mxCall ->
|
||||||
|
|
Loading…
Reference in New Issue