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

Commit 043494b0 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "LeAudio: Improve setting active Le Audio group" am: 3a62bb68

Original change: https://android-review.googlesource.com/c/platform/packages/services/Telecomm/+/1928166

Change-Id: I9995c9707713067b256162506d6d47b2a066b2c0
parents b71e3c9f 3a62bb68
Loading
Loading
Loading
Loading
+12 −24
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

public class BluetoothDeviceManager {
@@ -168,8 +169,15 @@ public class BluetoothDeviceManager {

    private List<BluetoothDevice> getLeAudioConnectedDevices() {
        synchronized (mLock) {
            // Filter out disconnected devices and/or those that have no group assigned
            ArrayList<BluetoothDevice> devices = new ArrayList<>(mGroupsByDevice.keySet());
            // Let's get devices which are a group leaders
            ArrayList<BluetoothDevice> devices = new ArrayList<>();

            for (LinkedHashMap.Entry<BluetoothDevice, Integer> entry : mGroupsByDevice.entrySet()) {
               if (Objects.equals(entry.getKey(),
                        mBluetoothLeAudioService.getConnectedGroupLeadDevice(entry.getValue()))) {
                   devices.add(entry.getKey());
               }
            }
            devices.removeIf(device -> !mLeAudioDevicesByAddress.containsValue(device));
            return devices;
        }
@@ -223,28 +231,8 @@ public class BluetoothDeviceManager {
            }
        }

        Set<Integer> seenGroupIds = new LinkedHashSet<>();
        if (mBluetoothAdapter != null) {
            for (BluetoothDevice device : mBluetoothAdapter.getActiveDevices(
                        BluetoothProfile.LE_AUDIO)) {
                if (device != null) {
                    result.add(device);
                    seenGroupIds.add(mGroupsByDevice.getOrDefault(device, -1));
                    break;
                }
            }
        }
        synchronized (mLock) {
            for (BluetoothDevice d : getLeAudioConnectedDevices()) {
                int groupId = mGroupsByDevice.getOrDefault(d,
                        BluetoothLeAudio.GROUP_ID_INVALID);
                if (groupId == BluetoothLeAudio.GROUP_ID_INVALID
                        || seenGroupIds.contains(groupId)) {
                    continue;
                }
                result.add(d);
                seenGroupIds.add(groupId);
            }
        if (mBluetoothLeAudioService != null) {
            result.addAll(getLeAudioConnectedDevices());
        }

        return Collections.unmodifiableCollection(result);
+5 −1
Original line number Diff line number Diff line
@@ -164,6 +164,8 @@ public class BluetoothDeviceManagerTest extends TelecomTestCase {
                        BluetoothDeviceManager.DEVICE_TYPE_LE_AUDIO));
        receiverUnderTest.onReceive(mContext,
                buildGroupNodeStatusChangedIntent(1, device5, BluetoothLeAudio.GROUP_NODE_ADDED));
        when(mBluetoothLeAudio.getConnectedGroupLeadDevice(1)).thenReturn(device5);

        receiverUnderTest.onReceive(mContext,
                buildConnectionActionIntent(BluetoothHeadset.STATE_DISCONNECTED, device1,
                        BluetoothDeviceManager.DEVICE_TYPE_HEADSET));
@@ -172,6 +174,7 @@ public class BluetoothDeviceManagerTest extends TelecomTestCase {
                        BluetoothDeviceManager.DEVICE_TYPE_LE_AUDIO));
        receiverUnderTest.onReceive(mContext,
                buildGroupNodeStatusChangedIntent(2, device6, BluetoothLeAudio.GROUP_NODE_ADDED));
        when(mBluetoothLeAudio.getConnectedGroupLeadDevice(2)).thenReturn(device6);
        receiverUnderTest.onReceive(mContext,
                buildConnectionActionIntent(BluetoothHeadset.STATE_CONNECTED, device3,
                        BluetoothDeviceManager.DEVICE_TYPE_HEADSET));
@@ -220,7 +223,8 @@ public class BluetoothDeviceManagerTest extends TelecomTestCase {
                        BluetoothDeviceManager.DEVICE_TYPE_LE_AUDIO));
        receiverUnderTest.onReceive(mContext,
                buildGroupNodeStatusChangedIntent(1, device6, BluetoothLeAudio.GROUP_NODE_ADDED));
        assertEquals(3, mBluetoothDeviceManager.getNumConnectedDevices());
        when(mBluetoothLeAudio.getConnectedGroupLeadDevice(1)).thenReturn(device5);
        assertEquals(2, mBluetoothDeviceManager.getNumConnectedDevices());
        assertEquals(2, mBluetoothDeviceManager.getUniqueConnectedDevices().size());
    }