mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 00:59:49 +01:00
Disable voice note proximity sensor when using bluetooth headset. (#2448)
This commit is contained in:
committed by
Cody Henthorne
parent
2022dae37a
commit
9c7a5e3cc8
@@ -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 ||
|
||||
|
||||
Reference in New Issue
Block a user