Skip to content

Commit

Permalink
Audio focus fix for pre Oreo devices (#44)
Browse files Browse the repository at this point in the history
* 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 <tejas@blocksglobal.com>
  • Loading branch information
John Qualls and tejas-n authored Jul 29, 2020
1 parent dd90e43 commit 33c074f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,7 +62,7 @@ internal class AudioDeviceManager(
audioRequest?.let { audioManager.requestAudioFocus(it) }
} else {
audioManager.requestAudioFocus(
{},
audioFocusChangeListener,
AudioManager.STREAM_VOICE_CALL,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
}
Expand Down Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,8 +35,9 @@ open class BaseTest {
internal val audioDeviceChangeListener = mock<AudioDeviceChangeListener>()
internal val buildWrapper = mock<BuildWrapper>()
internal val audioFocusRequest = mock<AudioFocusRequestWrapper>()
internal val audioFocusChangeListener = mock<AudioManager.OnAudioFocusChangeListener>()
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down Expand Up @@ -291,7 +291,7 @@ class AudioDeviceSelectorTest : BaseTest() {
audioDeviceSelector.activate()
audioDeviceSelector.stop()

verify(audioManager).abandonAudioFocus(isA())
verify(audioManager).abandonAudioFocus(audioFocusChangeListener)
}

@Test
Expand Down

0 comments on commit 33c074f

Please sign in to comment.