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

Commit eb21ea10 authored by David Rim's avatar David Rim Committed by Gerrit Code Review
Browse files

Merge "Enable bluetooth a2dpsink to request audio focus from source"

parents dde194c5 bac9bb86
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.bluetooth.a2dpsink;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.AudioAttributes;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
@@ -135,7 +136,16 @@ public class A2dpSinkStreamHandler extends Handler {
                break;

            case SRC_PLAY:
                // Remote play command, if we have audio focus update avrcp, otherwise send pause.
                // Remote play command.
                // If is an iot device gain focus and start avrcp updates.
                if (isIotDevice()) {
                    if (mAudioFocus == AudioManager.AUDIOFOCUS_NONE) {
                        requestAudioFocus();
                    }
                    startAvrcpUpdates();
                    break;
                }
                // Otherwise, pause if we don't have focus
                if (mAudioFocus == AudioManager.AUDIOFOCUS_NONE) {
                    sendAvrcpPause();
                } else {
@@ -337,4 +347,8 @@ public class A2dpSinkStreamHandler extends Handler {
            Log.e(TAG, "Passthrough not sent, connection un-available.");
        }
    }

    private boolean isIotDevice() {
        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_EMBEDDED);
    }
}
+15 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
@@ -58,6 +59,8 @@ public class A2dpSinkStreamHandlerTest extends AndroidTestCase {

    @Mock Resources mockResources;

    @Mock PackageManager mockPackageManager;

    @Before
    public void setUp() {
        // Mock the looper
@@ -79,6 +82,8 @@ public class A2dpSinkStreamHandlerTest extends AndroidTestCase {
        when(mockAudioManager.abandonAudioFocus(any())).thenReturn(AudioManager.AUDIOFOCUS_GAIN);
        doNothing().when(mockA2dpSink).informAudioTrackGainNative(anyFloat());
        when(mockContext.getMainLooper()).thenReturn(mHandlerThread.getLooper());
        when(mockContext.getPackageManager()).thenReturn(mockPackageManager);
        when(mockPackageManager.hasSystemFeature(any())).thenReturn(false);

        streamHandler = spy(new A2dpSinkStreamHandler(mockA2dpSink, mockContext));
    }
@@ -139,6 +144,16 @@ public class A2dpSinkStreamHandlerTest extends AndroidTestCase {
        verify(mockA2dpSink, times(0)).informAudioTrackGainNative(1.0f);
    }

    @Test
    public void testSrcPlayIot() {
        // Play was pressed remotely for an iot device, expect streaming to start.
        when(mockPackageManager.hasSystemFeature(any())).thenReturn(true);
        streamHandler.handleMessage(streamHandler.obtainMessage(A2dpSinkStreamHandler.SRC_PLAY));
        verify(mockAudioManager, times(1)).requestAudioFocus(any(), anyInt(), anyInt());
        verify(mockA2dpSink, times(1)).informAudioFocusStateNative(1);
        verify(mockA2dpSink, times(1)).informAudioTrackGainNative(1.0f);
    }

    @Test
    public void testSrcPause() {
        // Play was pressed locally, expect streaming to start.