[Audiosharing] Returns BluetoothDevice when fetchConnectedDevicesByGroupId

CachedBluetoothDevice can change its member variable mDevice, so we can
not rely on the CachedBluetoothDevice#getDevice when we add source to
it. This change will return BluetoothDevice instead of
CachedBluetoothDevice when fetchConnectedDevicesByGroupId, so that we
won't add source to unintended BluetoothDevice via
CachedBluetoothDevice#getDevice.

Fix: 350877510
Test: atest
Flag: com.android.settingslib.flags.enable_le_audio_sharing
Change-Id: I4ee41b8f1449e7176f0a05a7dd4e59034c161824
This commit is contained in:
Yiyi Shen
2024-07-31 20:28:09 +08:00
parent b6e0cd99c1
commit 7edad1c84a
5 changed files with 107 additions and 98 deletions

View File

@@ -48,7 +48,6 @@ import com.android.settings.core.BasePreferenceController;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.SettingsMainSwitchBar;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -63,17 +62,15 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
public class AudioSharingSwitchBarController extends BasePreferenceController
implements DefaultLifecycleObserver,
OnCheckedChangeListener,
LocalBluetoothProfileManager.ServiceListener {
private static final String TAG = "AudioSharingSwitchBarCtl";
OnCheckedChangeListener,
LocalBluetoothProfileManager.ServiceListener {
private static final String TAG = "AudioSharingSwitchCtlr";
private static final String PREF_KEY = "audio_sharing_main_switch";
interface OnAudioSharingStateChangedListener {
@@ -103,7 +100,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
private final Executor mExecutor;
private final MetricsFeatureProvider mMetricsFeatureProvider;
private final OnAudioSharingStateChangedListener mListener;
private Map<Integer, List<CachedBluetoothDevice>> mGroupedConnectedDevices = new HashMap<>();
private Map<Integer, List<BluetoothDevice>> mGroupedConnectedDevices = new HashMap<>();
private List<BluetoothDevice> mTargetActiveSinks = new ArrayList<>();
private List<AudioSharingDeviceItem> mDeviceItemsForSharing = new ArrayList<>();
@VisibleForTesting IntentFilter mIntentFilter;
@@ -341,8 +338,8 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
// FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST is always true in
// prod. We can turn off the flag for debug purpose.
if (FeatureFlagUtils.isEnabled(
mContext,
FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST)
mContext,
FeatureFlagUtils.SETTINGS_NEED_CONNECTED_BLE_DEVICE_FOR_BROADCAST)
&& mAssistant.getAllConnectedDevices().isEmpty()) {
// Pop up dialog to ask users to connect at least one lea buds before audio sharing.
AudioSharingUtils.postOnMainThread(
@@ -454,13 +451,11 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
mDeviceItemsForSharing = new ArrayList<>(deviceItems);
mTargetActiveSinks = new ArrayList<>();
if (!deviceItems.isEmpty() && deviceItems.get(0).isActive()) {
for (CachedBluetoothDevice device :
// If active device exists for audio sharing, share to it
// automatically once the broadcast is started.
mTargetActiveSinks =
mGroupedConnectedDevices.getOrDefault(
deviceItems.get(0).getGroupId(), ImmutableList.of())) {
// If active device exists for audio sharing, share to it
// automatically once the broadcast is started.
mTargetActiveSinks.add(device.getDevice());
}
deviceItems.get(0).getGroupId(), ImmutableList.of());
mDeviceItemsForSharing.remove(0);
}
if (mBroadcast != null) {
@@ -488,7 +483,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
boolean isStateReady =
isBluetoothOn()
&& AudioSharingUtils.isAudioSharingProfileReady(
mProfileManager);
mProfileManager);
AudioSharingUtils.postOnMainThread(
mContext,
() -> {
@@ -541,12 +536,8 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
@Override
public void onItemClick(@NonNull AudioSharingDeviceItem item) {
AudioSharingUtils.addSourceToTargetSinks(
mGroupedConnectedDevices
.getOrDefault(item.getGroupId(), ImmutableList.of())
.stream()
.map(CachedBluetoothDevice::getDevice)
.filter(Objects::nonNull)
.collect(Collectors.toList()),
mGroupedConnectedDevices.getOrDefault(
item.getGroupId(), ImmutableList.of()),
mBtManager);
mGroupedConnectedDevices.clear();
mDeviceItemsForSharing.clear();
@@ -575,8 +566,8 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
@NonNull ViewGroup host, @NonNull View view, @NonNull AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED
&& (event.getContentChangeTypes()
& AccessibilityEvent.CONTENT_CHANGE_TYPE_ENABLED)
!= 0) {
& AccessibilityEvent.CONTENT_CHANGE_TYPE_ENABLED)
!= 0) {
Log.d(TAG, "Skip accessibility event for CONTENT_CHANGE_TYPE_ENABLED");
return false;
}