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

Commit 5507bace authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Call setActiveDevice(null) when disconnected

When A2DP is disconnected in call mode, and when HFP is disconnected in
media mode, the disconnection of the audio device is not propagated to
the A2dpService or HeadsetService via setActiveDevice(null).
Thus, when the same audio device reconnected after, the audio is not came
from the audio device.

Bug: 261366232
Tag: #refactor
Test: atest BluetoothInstrumentationTests
Change-Id: Ic80b4ac436d61f0c8129e9bd61e086fe4c5d4644
parent 5e781fa5
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -287,7 +287,7 @@ class ActiveDeviceManager {
                                    setA2dpActiveDevice(null);
                                }
                            } else {
                                mA2dpActiveDevice = null;
                                setA2dpActiveDevice(null);
                            }
                        }
                    }
@@ -366,7 +366,7 @@ class ActiveDeviceManager {
                                    setHfpActiveDevice(null);
                                }
                            } else {
                                mHfpActiveDevice = null;
                                setHfpActiveDevice(null);
                            }
                        }
                    }
+33 −8
Original line number Diff line number Diff line
@@ -133,16 +133,14 @@ public class ActiveDeviceManagerTest {
        when(mLeAudioService.setActiveDevice(any(), anyBoolean())).thenReturn(true);

        when(mA2dpService.getFallbackDevice()).thenAnswer(invocation -> {
            if (mDeviceConnectionStack.contains(mA2dpDevice)) {
                return mA2dpDevice;
            }
            return null;
            int index = Math.max(mDeviceConnectionStack.indexOf(mA2dpDevice),
                    mDeviceConnectionStack.indexOf(mA2dpHeadsetDevice));
            return (index == -1) ? null : mDeviceConnectionStack.get(index);
        });
        when(mHeadsetService.getFallbackDevice()).thenAnswer(invocation -> {
            if (mDeviceConnectionStack.contains(mHeadsetDevice)) {
                return mHeadsetDevice;
            }
            return null;
            int index = Math.max(mDeviceConnectionStack.indexOf(mHeadsetDevice),
                    mDeviceConnectionStack.indexOf(mA2dpHeadsetDevice));
            return (index == -1) ? null : mDeviceConnectionStack.get(index);
        });
        when(mDatabaseManager.getMostRecentlyConnectedDevicesInList(any())).thenAnswer(
                invocation -> {
@@ -320,6 +318,33 @@ public class ActiveDeviceManagerTest {
        verify(mHeadsetService, timeout(TIMEOUT_MS)).setActiveDevice(mHeadsetDevice);
    }

    /**
     * Test setActiveDevice(null) for both A2dpService and HeadsetService are called when an
     * activated combo (A2DP + Headset) device is disconnected while in call.
     */
    @Test
    public void a2dpHeadsetDisconnected_callsSetActiveDeviceNull() {
        when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_NORMAL);
        // A2dpHeadset connected
        headsetConnected(mA2dpHeadsetDevice);
        a2dpConnected(mA2dpHeadsetDevice);
        // Verify activation of A2DP in media mode
        verify(mA2dpService, timeout(TIMEOUT_MS)).setActiveDevice(mA2dpHeadsetDevice, false);

        // Mode changed to call mode
        when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_IN_CALL);
        mAudioModeChangedListener.onModeChanged(AudioManager.MODE_IN_CALL);
        // Verify activation of HFP in call mode
        verify(mHeadsetService, timeout(TIMEOUT_MS)).setActiveDevice(mA2dpHeadsetDevice);

        // A2dpHeadset disconnected
        headsetDisconnected(mA2dpHeadsetDevice);
        a2dpDisconnected(mA2dpHeadsetDevice);
        // Verify setActiveDevice(null) called
        verify(mHeadsetService, timeout(TIMEOUT_MS)).setActiveDevice(null);
        verify(mA2dpService, timeout(TIMEOUT_MS)).setActiveDevice(null, false);
    }

    /**
     * A combo (A2DP + Headset) device is connected. Then a Hearing Aid is connected.
     */