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

Commit 337a5d36 authored by Yiyi Shen's avatar Yiyi Shen Committed by Android (Google) Code Review
Browse files

Merge "[Audiosharing] Remove source for sinks disconnected/unpaired from Settings" into main

parents e2b73a03 11a0ee96
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHearingAid;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUuid;
import android.content.Context;
@@ -58,6 +59,7 @@ import com.android.settingslib.flags.Flags;
import com.android.settingslib.utils.ThreadUtils;
import com.android.settingslib.widget.AdaptiveOutlineDrawable;

import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
@@ -397,6 +399,9 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
                }
            }
            Log.d(TAG, "Disconnect " + this);
            if (Flags.enableLeAudioSharing()) {
                removeBroadcastSource(ImmutableSet.of(mDevice));
            }
            mDevice.disconnect();
        }
        // Disconnect  PBAP server in case its connected
@@ -609,6 +614,16 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
            final BluetoothDevice dev = mDevice;
            if (dev != null) {
                mUnpairing = true;
                if (Flags.enableLeAudioSharing()) {
                    Set<BluetoothDevice> devicesToRemoveSource = new HashSet<>();
                    devicesToRemoveSource.add(dev);
                    if (getGroupId() != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
                        for (CachedBluetoothDevice member : getMemberDevice()) {
                            devicesToRemoveSource.add(member.getDevice());
                        }
                    }
                    removeBroadcastSource(devicesToRemoveSource);
                }
                final boolean successful = dev.removeBond();
                if (successful) {
                    releaseLruCache();
@@ -623,6 +638,25 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        }
    }

    @WorkerThread
    private void removeBroadcastSource(Set<BluetoothDevice> devices) {
        if (mProfileManager == null || devices.isEmpty()) return;
        LocalBluetoothLeBroadcast broadcast = mProfileManager.getLeAudioBroadcastProfile();
        LocalBluetoothLeBroadcastAssistant assistant =
                mProfileManager.getLeAudioBroadcastAssistantProfile();
        if (broadcast != null && assistant != null && broadcast.isEnabled(null)) {
            for (BluetoothDevice device : devices) {
                for (BluetoothLeBroadcastReceiveState state : assistant.getAllSources(device)) {
                    if (BluetoothUtils.D) {
                        Log.d(TAG, "Remove broadcast source " + state.getBroadcastId()
                                + " from device " + device.getAnonymizedAddress());
                    }
                    assistant.removeSource(device, state.getSourceId());
                }
            }
        }
    }

    public int getProfileConnectionState(LocalBluetoothProfile profile) {
        return profile != null
                ? profile.getConnectionStatus(mDevice)
+46 −0
Original line number Diff line number Diff line
@@ -2383,6 +2383,52 @@ public class CachedBluetoothDeviceTest {
                Integer.parseInt(TWS_BATTERY_LEFT));
    }

    @Test
    public void disconnect_removeBroadcastSource() {
        when(mCachedDevice.getGroupId()).thenReturn(1);
        when(mSubCachedDevice.getGroupId()).thenReturn(1);
        mCachedDevice.addMemberDevice(mSubCachedDevice);
        LocalBluetoothLeBroadcast broadcast = mock(LocalBluetoothLeBroadcast.class);
        LocalBluetoothLeBroadcastAssistant assistant = mock(
                LocalBluetoothLeBroadcastAssistant.class);
        when(mProfileManager.getLeAudioBroadcastProfile()).thenReturn(broadcast);
        when(mProfileManager.getLeAudioBroadcastAssistantProfile()).thenReturn(assistant);
        when(broadcast.isEnabled(null)).thenReturn(true);
        BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class);
        when(state.getSourceId()).thenReturn(1);
        when(assistant.getAllSources(mDevice)).thenReturn(ImmutableList.of(state));
        when(assistant.getAllSources(mSubDevice)).thenReturn(ImmutableList.of(state));

        mCachedDevice.disconnect();
        verify(assistant).removeSource(mDevice, /* sourceId= */1);
        verify(assistant).removeSource(mSubDevice, /* sourceId= */1);
        verify(mDevice).disconnect();
        verify(mSubDevice).disconnect();
    }

    @Test
    public void unpair_removeBroadcastSource() {
        when(mCachedDevice.getGroupId()).thenReturn(1);
        when(mSubCachedDevice.getGroupId()).thenReturn(1);
        mCachedDevice.addMemberDevice(mSubCachedDevice);
        when(mCachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        LocalBluetoothLeBroadcast broadcast = mock(LocalBluetoothLeBroadcast.class);
        LocalBluetoothLeBroadcastAssistant assistant = mock(
                LocalBluetoothLeBroadcastAssistant.class);
        when(mProfileManager.getLeAudioBroadcastProfile()).thenReturn(broadcast);
        when(mProfileManager.getLeAudioBroadcastAssistantProfile()).thenReturn(assistant);
        when(broadcast.isEnabled(null)).thenReturn(true);
        BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class);
        when(state.getSourceId()).thenReturn(1);
        when(assistant.getAllSources(mDevice)).thenReturn(ImmutableList.of(state));
        when(assistant.getAllSources(mSubDevice)).thenReturn(ImmutableList.of(state));

        mCachedDevice.unpair();
        verify(assistant).removeSource(mDevice, /* sourceId= */1);
        verify(assistant).removeSource(mSubDevice, /* sourceId= */1);
        verify(mDevice).removeBond();
    }

    private void updateProfileStatus(LocalBluetoothProfile profile, int status) {
        doReturn(status).when(profile).getConnectionStatus(mDevice);
        mCachedDevice.onProfileStateChanged(profile, status);