[Audiosharing] Fix main sharing flow.
Fetch the active device before starting the sharing. Because once the sharing is started, all devices connected will switch to inactive. Flagged with enable_le_audio_sharing Bug: 305620450 Test: manual Change-Id: Ie287d1094fe40dbcb42c121ea79409fc48e0eec7
This commit is contained in:
@@ -45,12 +45,16 @@ import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
|
|||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
import com.android.settingslib.utils.ThreadUtils;
|
import com.android.settingslib.utils.ThreadUtils;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class AudioSharingSwitchBarController extends BasePreferenceController
|
public class AudioSharingSwitchBarController extends BasePreferenceController
|
||||||
implements DefaultLifecycleObserver, OnCheckedChangeListener {
|
implements DefaultLifecycleObserver, OnCheckedChangeListener {
|
||||||
@@ -69,6 +73,9 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
|
|||||||
private final Executor mExecutor;
|
private final Executor mExecutor;
|
||||||
private final OnSwitchBarChangedListener mListener;
|
private final OnSwitchBarChangedListener mListener;
|
||||||
private DashboardFragment mFragment;
|
private DashboardFragment mFragment;
|
||||||
|
private Map<Integer, List<CachedBluetoothDevice>> mGroupedConnectedDevices = new HashMap<>();
|
||||||
|
private List<BluetoothDevice> mTargetActiveSinks = new ArrayList<>();
|
||||||
|
private ArrayList<AudioSharingDeviceItem> mDeviceItemsForSharing = new ArrayList<>();
|
||||||
@VisibleForTesting IntentFilter mIntentFilter;
|
@VisibleForTesting IntentFilter mIntentFilter;
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
@@ -114,24 +121,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
|
|||||||
+ broadcastId
|
+ broadcastId
|
||||||
+ ", metadata = "
|
+ ", metadata = "
|
||||||
+ metadata.getBroadcastName());
|
+ metadata.getBroadcastName());
|
||||||
Map<Integer, List<CachedBluetoothDevice>> groupedConnectedDevices =
|
addSourceToTargetSinks(mTargetActiveSinks);
|
||||||
AudioSharingUtils.fetchConnectedDevicesByGroupId(mBtManager);
|
|
||||||
ArrayList<AudioSharingDeviceItem> deviceItems =
|
|
||||||
AudioSharingUtils.buildOrderedConnectedLeadAudioSharingDeviceItem(
|
|
||||||
mBtManager,
|
|
||||||
groupedConnectedDevices,
|
|
||||||
/* filterByInSharing= */ false);
|
|
||||||
// deviceItems is ordered. The active device is the first place if exits.
|
|
||||||
ArrayList<AudioSharingDeviceItem> deviceItemsForSharing = deviceItems;
|
|
||||||
if (!deviceItems.isEmpty() && deviceItems.get(0).isActive()) {
|
|
||||||
for (CachedBluetoothDevice device :
|
|
||||||
groupedConnectedDevices.get(deviceItems.get(0).getGroupId())) {
|
|
||||||
// If active device exists for audio sharing, share to it
|
|
||||||
// automatically once the broadcast is started.
|
|
||||||
addSourceToSink(device.getDevice());
|
|
||||||
}
|
|
||||||
deviceItemsForSharing.remove(0);
|
|
||||||
}
|
|
||||||
if (mFragment == null) {
|
if (mFragment == null) {
|
||||||
Log.w(TAG, "Dialog fail to show due to null fragment.");
|
Log.w(TAG, "Dialog fail to show due to null fragment.");
|
||||||
return;
|
return;
|
||||||
@@ -140,16 +130,16 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
|
|||||||
() -> {
|
() -> {
|
||||||
AudioSharingDialogFragment.show(
|
AudioSharingDialogFragment.show(
|
||||||
mFragment,
|
mFragment,
|
||||||
deviceItemsForSharing,
|
mDeviceItemsForSharing,
|
||||||
item -> {
|
item -> {
|
||||||
if (groupedConnectedDevices.containsKey(
|
addSourceToTargetSinks(
|
||||||
item.getGroupId())) {
|
mGroupedConnectedDevices
|
||||||
for (CachedBluetoothDevice device :
|
.getOrDefault(
|
||||||
groupedConnectedDevices.get(
|
item.getGroupId(),
|
||||||
item.getGroupId())) {
|
ImmutableList.of())
|
||||||
addSourceToSink(device.getDevice());
|
.stream()
|
||||||
}
|
.map(CachedBluetoothDevice::getDevice)
|
||||||
}
|
.collect(Collectors.toList()));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -333,6 +323,22 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
|
|||||||
mSwitchBar.setEnabled(true);
|
mSwitchBar.setEnabled(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
mGroupedConnectedDevices = AudioSharingUtils.fetchConnectedDevicesByGroupId(mBtManager);
|
||||||
|
ArrayList<AudioSharingDeviceItem> deviceItems =
|
||||||
|
AudioSharingUtils.buildOrderedConnectedLeadAudioSharingDeviceItem(
|
||||||
|
mBtManager, mGroupedConnectedDevices, /* filterByInSharing= */ false);
|
||||||
|
// deviceItems is ordered. The active device is the first place if exits.
|
||||||
|
mDeviceItemsForSharing = new ArrayList<>(deviceItems);
|
||||||
|
if (!deviceItems.isEmpty() && deviceItems.get(0).isActive()) {
|
||||||
|
for (CachedBluetoothDevice device :
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
mDeviceItemsForSharing.remove(0);
|
||||||
|
}
|
||||||
// TODO: start broadcast with new API
|
// TODO: start broadcast with new API
|
||||||
mBroadcast.startBroadcast("test", null);
|
mBroadcast.startBroadcast("test", null);
|
||||||
}
|
}
|
||||||
@@ -367,8 +373,8 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
|
|||||||
return mBroadcast != null && mBroadcast.isEnabled(null);
|
return mBroadcast != null && mBroadcast.isEnabled(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addSourceToSink(BluetoothDevice sink) {
|
private void addSourceToTargetSinks(List<BluetoothDevice> sinks) {
|
||||||
if (mBroadcast == null || mAssistant == null) {
|
if (sinks.isEmpty() || mBroadcast == null || mAssistant == null) {
|
||||||
Log.d(TAG, "Skip adding source to target.");
|
Log.d(TAG, "Skip adding source to target.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -378,12 +384,14 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
|
|||||||
Log.e(TAG, "Error: There is no broadcastMetadata.");
|
Log.e(TAG, "Error: There is no broadcastMetadata.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
for (BluetoothDevice sink : sinks) {
|
||||||
Log.d(
|
Log.d(
|
||||||
TAG,
|
TAG,
|
||||||
"Add broadcast with broadcastId: "
|
"Add broadcast with broadcastId: "
|
||||||
+ broadcastMetadata.getBroadcastId()
|
+ broadcastMetadata.getBroadcastId()
|
||||||
+ " to the device: "
|
+ "to the device: "
|
||||||
+ sink.getAnonymizedAddress());
|
+ sink.getAnonymizedAddress());
|
||||||
mAssistant.addSource(sink, broadcastMetadata, /* isGroupOp= */ false);
|
mAssistant.addSource(sink, broadcastMetadata, /* isGroupOp= */ false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user