Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 549ff71b authored by Joseph Pirozzo's avatar Joseph Pirozzo
Browse files

A2DP Sink: Focus gain while transient loss

While Bluetooth is under a transient focus loss if the user presses the
play button, Bluetooth should attempt to regain audio focus rather than
just wait passive for the transient holder to release it.

Bug: 143916073
Test: atest A2dpSinkStreamHandlerTest
Change-Id: I7b958a9ff06c9f1fcb9383a3a40ca147f4ad9399
parent 1701d5f8
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -183,8 +183,9 @@ public class A2dpSinkStreamHandler extends Handler {
                break;

            case AUDIO_FOCUS_CHANGE:
                mAudioFocus = (int) message.obj;
                // message.obj is the newly granted audio focus.
                switch ((int) message.obj) {
                switch (mAudioFocus) {
                    case AudioManager.AUDIOFOCUS_GAIN:
                        removeMessages(DELAYED_PAUSE);
                        // Begin playing audio, if we paused the remote, send a play now.
@@ -245,7 +246,7 @@ public class A2dpSinkStreamHandler extends Handler {
     */
    private void requestAudioFocusIfNone() {
        if (DBG) Log.d(TAG, "requestAudioFocusIfNone()");
        if (mAudioFocus == AudioManager.AUDIOFOCUS_NONE) {
        if (mAudioFocus != AudioManager.AUDIOFOCUS_GAIN) {
            requestAudioFocus();
        }
        // On the off change mMediaPlayer errors out and dies, we want to make sure we retry this.
+15 −0
Original line number Diff line number Diff line
@@ -194,6 +194,21 @@ public class A2dpSinkStreamHandlerTest {
        verify(mMockA2dpSink, times(1)).informAudioTrackGainNative(0);
    }

    @Test
    public void testFocusRerequest() {
        // Focus was lost transiently, expect streaming to stop.
        testSnkPlay();
        mStreamHandler.handleMessage(
                mStreamHandler.obtainMessage(A2dpSinkStreamHandler.AUDIO_FOCUS_CHANGE,
                        AudioManager.AUDIOFOCUS_LOSS_TRANSIENT));
        verify(mMockAudioManager, times(0)).abandonAudioFocus(any());
        verify(mMockA2dpSink, times(0)).informAudioFocusStateNative(0);
        verify(mMockA2dpSink, times(1)).informAudioTrackGainNative(0);
        mStreamHandler.handleMessage(
                mStreamHandler.obtainMessage(A2dpSinkStreamHandler.REQUEST_FOCUS, true));
        verify(mMockAudioManager, times(2)).requestAudioFocus(any());
    }

    @Test
    public void testFocusGainTransient() {
        // Focus was lost then regained.