Fixed proximity sensor not working
This commit is contained in:
parent
8303f2e062
commit
c699ddc1e9
1 changed files with 17 additions and 62 deletions
|
@ -20,58 +20,29 @@
|
||||||
package org.linphone.activities.call
|
package org.linphone.activities.call
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.hardware.Sensor
|
|
||||||
import android.hardware.SensorEvent
|
|
||||||
import android.hardware.SensorEventListener
|
|
||||||
import android.hardware.SensorManager
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
|
import android.os.PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY
|
||||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||||
import org.linphone.activities.GenericActivity
|
import org.linphone.activities.GenericActivity
|
||||||
import org.linphone.core.tools.Log
|
import org.linphone.core.tools.Log
|
||||||
|
|
||||||
abstract class ProximitySensorActivity : GenericActivity() {
|
abstract class ProximitySensorActivity : GenericActivity() {
|
||||||
private lateinit var sensorManager: SensorManager
|
|
||||||
private lateinit var proximitySensor: Sensor
|
|
||||||
private lateinit var proximityWakeLock: PowerManager.WakeLock
|
private lateinit var proximityWakeLock: PowerManager.WakeLock
|
||||||
private var proximitySensorFound = false
|
|
||||||
private val proximityListener: SensorEventListener = object : SensorEventListener {
|
|
||||||
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) { }
|
|
||||||
|
|
||||||
override fun onSensorChanged(event: SensorEvent) {
|
|
||||||
if (event.timestamp == 0L || !proximitySensorEnabled) return
|
|
||||||
if (isProximitySensorNearby(event)) {
|
|
||||||
if (!proximityWakeLock.isHeld) {
|
|
||||||
Log.i("[Proximity Sensor Activity] Acquiring proximity wake lock")
|
|
||||||
proximityWakeLock.acquire()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (proximityWakeLock.isHeld) {
|
|
||||||
Log.i("[Proximity Sensor Activity] Releasing proximity wake lock")
|
|
||||||
proximityWakeLock.release()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private var proximitySensorEnabled = false
|
private var proximitySensorEnabled = false
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
try {
|
val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||||
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
if (!powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
|
||||||
val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)
|
Log.w("[Proximity Sensor Activity] PROXIMITY_SCREEN_OFF_WAKE_LOCK isn't supported on this device!")
|
||||||
sensor ?: return
|
|
||||||
proximitySensor = sensor
|
|
||||||
proximityWakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager)
|
|
||||||
.newWakeLock(
|
|
||||||
PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK,
|
|
||||||
"$packageName;proximity_sensor"
|
|
||||||
)
|
|
||||||
proximitySensorFound = true
|
|
||||||
} catch (ise: IllegalStateException) {
|
|
||||||
Log.e("[Proximity Sensor Activity] Failed to get proximity sensor: $ise")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
proximityWakeLock = powerManager.newWakeLock(
|
||||||
|
PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK,
|
||||||
|
"$packageName;proximity_sensor"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
|
@ -96,40 +67,24 @@ abstract class ProximitySensorActivity : GenericActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun enableProximitySensor(enable: Boolean) {
|
protected fun enableProximitySensor(enable: Boolean) {
|
||||||
if (!proximitySensorFound) {
|
|
||||||
Log.w("[Proximity Sensor Activity] Couldn't find proximity sensor in this device, skipping")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (enable) {
|
if (enable) {
|
||||||
if (!proximitySensorEnabled) {
|
if (!proximitySensorEnabled) {
|
||||||
Log.i("[Proximity Sensor Activity] Enabling proximity sensor listener")
|
Log.i("[Proximity Sensor Activity] Enabling proximity sensor turning off screen")
|
||||||
sensorManager.registerListener(proximityListener, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL)
|
if (!proximityWakeLock.isHeld) {
|
||||||
|
Log.i("[Proximity Sensor Activity] Acquiring PROXIMITY_SCREEN_OFF_WAKE_LOCK")
|
||||||
|
proximityWakeLock.acquire()
|
||||||
|
}
|
||||||
proximitySensorEnabled = true
|
proximitySensorEnabled = true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (proximitySensorEnabled) {
|
if (proximitySensorEnabled) {
|
||||||
Log.i("[Proximity Sensor Activity] Disabling proximity sensor listener")
|
Log.i("[Proximity Sensor Activity] Disabling proximity sensor turning off screen")
|
||||||
sensorManager.unregisterListener(proximityListener)
|
|
||||||
if (proximityWakeLock.isHeld) {
|
if (proximityWakeLock.isHeld) {
|
||||||
proximityWakeLock.release()
|
Log.i("[Proximity Sensor Activity] Releasing PROXIMITY_SCREEN_OFF_WAKE_LOCK")
|
||||||
|
proximityWakeLock.release(RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY)
|
||||||
}
|
}
|
||||||
proximitySensorEnabled = false
|
proximitySensorEnabled = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isProximitySensorNearby(event: SensorEvent): Boolean {
|
|
||||||
var threshold = 4.001f // <= 4 cm is near
|
|
||||||
|
|
||||||
val distanceInCm = event.values[0]
|
|
||||||
val maxDistance = event.sensor.maximumRange
|
|
||||||
Log.d("[Proximity Sensor Activity] Proximity sensor report [$distanceInCm] , for max range [$maxDistance]")
|
|
||||||
|
|
||||||
if (maxDistance <= threshold) {
|
|
||||||
// Case binary 0/1 and short sensors
|
|
||||||
threshold = maxDistance
|
|
||||||
}
|
|
||||||
return distanceInCm < threshold
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue