From 33c074f7d7c50fcb146302a8ea719f77afcd5131 Mon Sep 17 00:00:00 2001 From: John Qualls Date: Wed, 29 Jul 2020 11:03:39 -0600 Subject: [PATCH] Audio focus fix for pre Oreo devices (#44) * Fix audio focus issue encountered on pre Oreo devices Store a reference to onAudioFocusChangeListener that was used to request audio focus and use it with abandonAudioFocus(..) so that the framework can give back the audio focus to the previous owner. This fix is relevant to pre Oreo devices. * Fix test for abandoning audio focus on pre Oreo devices * Add to Changelog * Inject OnAudioFocusChangeListener in constructor and avoid use of argument capturer for test consistency * Update changelog Co-authored-by: Tejas Nandanikar --- CHANGELOG.md | 6 ++++++ .../twilio/audioswitch/selection/AudioDeviceManager.kt | 8 +++++--- .../java/com/twilio/audioswitch/bluetooth/BaseTest.kt | 4 +++- .../audioswitch/selection/AudioDeviceSelectorTest.kt | 8 ++++---- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebadd206..7847ec15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +### 0.2.1 + +Bug Fixes + +- Fixed a bug where the audio focus wasn't being returned to the previous audio focus owner on pre Oreo devices. + ### 0.2.0 Enhancements diff --git a/audioswitch/src/main/java/com/twilio/audioswitch/selection/AudioDeviceManager.kt b/audioswitch/src/main/java/com/twilio/audioswitch/selection/AudioDeviceManager.kt index 6a33c00a..37a45a7d 100644 --- a/audioswitch/src/main/java/com/twilio/audioswitch/selection/AudioDeviceManager.kt +++ b/audioswitch/src/main/java/com/twilio/audioswitch/selection/AudioDeviceManager.kt @@ -17,7 +17,9 @@ internal class AudioDeviceManager( private val logger: LogWrapper, private val audioManager: AudioManager, private val build: BuildWrapper, - private val audioFocusRequest: AudioFocusRequestWrapper + private val audioFocusRequest: AudioFocusRequestWrapper, + private val audioFocusChangeListener: AudioManager.OnAudioFocusChangeListener = + AudioManager.OnAudioFocusChangeListener { } ) { private var savedAudioMode = 0 @@ -60,7 +62,7 @@ internal class AudioDeviceManager( audioRequest?.let { audioManager.requestAudioFocus(it) } } else { audioManager.requestAudioFocus( - {}, + audioFocusChangeListener, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) } @@ -100,7 +102,7 @@ internal class AudioDeviceManager( if (build.getVersion() >= Build.VERSION_CODES.O) { audioRequest?.let { audioManager.abandonAudioFocusRequest(it) } } else { - audioManager.abandonAudioFocus { } + audioManager.abandonAudioFocus(audioFocusChangeListener) } } } diff --git a/audioswitch/src/test/java/com/twilio/audioswitch/bluetooth/BaseTest.kt b/audioswitch/src/test/java/com/twilio/audioswitch/bluetooth/BaseTest.kt index 02568299..c5972d66 100644 --- a/audioswitch/src/test/java/com/twilio/audioswitch/bluetooth/BaseTest.kt +++ b/audioswitch/src/test/java/com/twilio/audioswitch/bluetooth/BaseTest.kt @@ -5,6 +5,7 @@ import android.bluetooth.BluetoothClass import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothHeadset import android.content.Context +import android.media.AudioManager import com.nhaarman.mockitokotlin2.mock import com.nhaarman.mockitokotlin2.whenever import com.twilio.audioswitch.DEVICE_NAME @@ -34,8 +35,9 @@ open class BaseTest { internal val audioDeviceChangeListener = mock() internal val buildWrapper = mock() internal val audioFocusRequest = mock() + internal val audioFocusChangeListener = mock() internal val audioDeviceManager = AudioDeviceManager(context, logger, audioManager, buildWrapper, - audioFocusRequest) + audioFocusRequest, audioFocusChangeListener) internal val wiredHeadsetReceiver = WiredHeadsetReceiver(context, logger) internal var handler = setupScoHandlerMock() internal var systemClockWrapper = setupSystemClockMock() diff --git a/audioswitch/src/test/java/com/twilio/audioswitch/selection/AudioDeviceSelectorTest.kt b/audioswitch/src/test/java/com/twilio/audioswitch/selection/AudioDeviceSelectorTest.kt index 65fc2daa..7451af79 100644 --- a/audioswitch/src/test/java/com/twilio/audioswitch/selection/AudioDeviceSelectorTest.kt +++ b/audioswitch/src/test/java/com/twilio/audioswitch/selection/AudioDeviceSelectorTest.kt @@ -254,9 +254,9 @@ class AudioDeviceSelectorTest : BaseTest() { audioDeviceSelector.activate() verify(audioManager).requestAudioFocus( - isA(), - eq(AudioManager.STREAM_VOICE_CALL), - eq(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) + audioFocusChangeListener, + AudioManager.STREAM_VOICE_CALL, + AudioManager.AUDIOFOCUS_GAIN_TRANSIENT ) } @@ -291,7 +291,7 @@ class AudioDeviceSelectorTest : BaseTest() { audioDeviceSelector.activate() audioDeviceSelector.stop() - verify(audioManager).abandonAudioFocus(isA()) + verify(audioManager).abandonAudioFocus(audioFocusChangeListener) } @Test