Fix NPE in proximity sensor management.
If a device either does not have a proximity sensor or has a non-functioning sensor, we can hit an NPE as soon as we hit MainActivity. This fix ensures proper handling if a sensor is unavailable.
This commit is contained in:
parent
c9597ef8dc
commit
de2c7d38bf
1 changed files with 11 additions and 5 deletions
|
@ -36,7 +36,7 @@ class VoiceNoteProximityWakeLockManager(
|
|||
}
|
||||
|
||||
private val sensorManager: SensorManager = ServiceUtil.getSensorManager(activity)
|
||||
private val proximitySensor: Sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)
|
||||
private val proximitySensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)
|
||||
|
||||
private val mediaControllerCallback = MediaControllerCallback()
|
||||
private val hardwareSensorEventListener = HardwareSensorEventListener()
|
||||
|
@ -44,15 +44,21 @@ class VoiceNoteProximityWakeLockManager(
|
|||
private var startTime: Long = -1
|
||||
|
||||
init {
|
||||
activity.lifecycle.addObserver(this)
|
||||
if (proximitySensor != null) {
|
||||
activity.lifecycle.addObserver(this)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume(owner: LifecycleOwner) {
|
||||
mediaController.registerCallback(mediaControllerCallback)
|
||||
if (proximitySensor != null) {
|
||||
mediaController.registerCallback(mediaControllerCallback)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPause(owner: LifecycleOwner) {
|
||||
unregisterCallbacksAndRelease()
|
||||
if (proximitySensor != null) {
|
||||
unregisterCallbacksAndRelease()
|
||||
}
|
||||
}
|
||||
|
||||
fun unregisterCallbacksAndRelease() {
|
||||
|
@ -110,7 +116,7 @@ class VoiceNoteProximityWakeLockManager(
|
|||
return
|
||||
}
|
||||
|
||||
val newStreamType = if (event.values[0] < PROXIMITY_THRESHOLD && event.values[0] != proximitySensor.maximumRange) {
|
||||
val newStreamType = if (event.values[0] < PROXIMITY_THRESHOLD && event.values[0] != proximitySensor?.maximumRange) {
|
||||
AudioManager.STREAM_VOICE_CALL
|
||||
} else {
|
||||
AudioManager.STREAM_MUSIC
|
||||
|
|
Loading…
Add table
Reference in a new issue