Disable voice note proximity sensor when using bluetooth headset. (#2448)

This commit is contained in:
Alex Hart
2022-06-17 16:08:02 -03:00
committed by Cody Henthorne
parent 2022dae37a
commit 9c7a5e3cc8
4 changed files with 42 additions and 1 deletions

View File

@@ -14,6 +14,8 @@ import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import com.twilio.audioswitch.AudioDevice
import com.twilio.audioswitch.AudioSwitch
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.util.ServiceUtil
import java.util.concurrent.TimeUnit
@@ -40,6 +42,7 @@ class VoiceNoteProximityWakeLockManager(
private val mediaControllerCallback = MediaControllerCallback()
private val hardwareSensorEventListener = HardwareSensorEventListener()
private val audioSwitch: AudioSwitch? = if (Build.VERSION.SDK_INT < 31) AudioSwitch(activity.applicationContext) else null
private var startTime: Long = -1
@@ -50,12 +53,25 @@ class VoiceNoteProximityWakeLockManager(
}
override fun onResume(owner: LifecycleOwner) {
if (audioSwitch == null) {
startListening()
} else {
audioSwitch.start { _, selectedAudioDevice -> onFocusedAudioDeviceChanged(selectedAudioDevice) }
}
}
override fun onPause(owner: LifecycleOwner) {
audioSwitch?.stop()
stopListening()
}
private fun startListening() {
if (proximitySensor != null) {
mediaController.registerCallback(mediaControllerCallback)
}
}
override fun onPause(owner: LifecycleOwner) {
private fun stopListening() {
if (proximitySensor != null) {
unregisterCallbacksAndRelease()
}
@@ -68,10 +84,19 @@ class VoiceNoteProximityWakeLockManager(
fun unregisterFromLifecycle() {
if (proximitySensor != null) {
stopListening()
activity.lifecycle.removeObserver(this)
}
}
private fun onFocusedAudioDeviceChanged(audioDevice: AudioDevice?) {
if (audioDevice is AudioDevice.BluetoothHeadset) {
stopListening()
} else {
startListening()
}
}
private fun isActivityResumed() = activity.lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)
private fun isPlayerActive() = mediaController.playbackState.state == PlaybackStateCompat.STATE_BUFFERING ||