[Audiosharing] Impl audio sharing main switch.

Start/stop broadcast when >=1 eligible buds connected.

Flagged with enable_le_audio_sharing

Bug: 305620450
Test: Manual
Change-Id: Ic982571f49ab79c39d0503929df4bb8be64b720e
This commit is contained in:
Yiyi Shen
2023-11-06 15:57:47 +08:00
parent cded970a4b
commit 87372de071
4 changed files with 274 additions and 31 deletions

View File

@@ -16,8 +16,13 @@
package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastAssistant;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;
import android.widget.Switch;
@@ -31,12 +36,21 @@ import com.android.settings.core.BasePreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.flags.Flags;
import com.android.settings.widget.SettingsMainSwitchBar;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.utils.ThreadUtils;
import com.android.settingslib.widget.OnMainSwitchChangeListener;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@@ -47,8 +61,10 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
private final SettingsMainSwitchBar mSwitchBar;
private final LocalBluetoothManager mBtManager;
private final LocalBluetoothLeBroadcast mBroadcast;
private final LocalBluetoothLeBroadcastAssistant mAssistant;
private final Executor mExecutor;
private DashboardFragment mFragment;
private List<BluetoothDevice> mTargetSinks = new ArrayList<>();
private final BluetoothLeBroadcast.Callback mBroadcastCallback =
new BluetoothLeBroadcast.Callback() {
@@ -79,7 +95,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
+ broadcastId
+ ", metadata = "
+ metadata);
// TODO: handle add sink if there are connected lea devices.
addSourceToTargetDevices(mTargetSinks);
}
@Override
@@ -113,11 +129,79 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
public void onPlaybackStopped(int reason, int broadcastId) {}
};
private BluetoothLeBroadcastAssistant.Callback mBroadcastAssistantCallback =
new BluetoothLeBroadcastAssistant.Callback() {
@Override
public void onSearchStarted(int reason) {}
@Override
public void onSearchStartFailed(int reason) {}
@Override
public void onSearchStopped(int reason) {}
@Override
public void onSearchStopFailed(int reason) {}
@Override
public void onSourceFound(@NonNull BluetoothLeBroadcastMetadata source) {}
@Override
public void onSourceAdded(@NonNull BluetoothDevice sink, int sourceId, int reason) {
Log.d(
TAG,
"onSourceAdded(), sink = "
+ sink
+ ", sourceId = "
+ sourceId
+ ", reason = "
+ reason);
}
@Override
public void onSourceAddFailed(
@NonNull BluetoothDevice sink,
@NonNull BluetoothLeBroadcastMetadata source,
int reason) {
Log.d(
TAG,
"onSourceAddFailed(), sink = "
+ sink
+ ", source = "
+ source
+ ", reason = "
+ reason);
}
@Override
public void onSourceModified(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceModifyFailed(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceRemoved(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onSourceRemoveFailed(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onReceiveStateChanged(
BluetoothDevice sink,
int sourceId,
BluetoothLeBroadcastReceiveState state) {}
};
AudioSharingSwitchBarController(Context context, SettingsMainSwitchBar switchBar) {
super(context, PREF_KEY);
mSwitchBar = switchBar;
mBtManager = Utils.getLocalBtManager(context);
mBroadcast = mBtManager.getProfileManager().getLeAudioBroadcastProfile();
mAssistant = mBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
mExecutor = Executors.newSingleThreadExecutor();
mSwitchBar.setChecked(isBroadcasting());
}
@@ -128,6 +212,9 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
if (mBroadcast != null) {
mBroadcast.registerServiceCallBack(mExecutor, mBroadcastCallback);
}
if (mAssistant != null) {
mAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
}
}
@Override
@@ -136,6 +223,9 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
if (mBroadcast != null) {
mBroadcast.unregisterServiceCallBack(mBroadcastCallback);
}
if (mAssistant != null) {
mAssistant.unregisterServiceCallBack(mBroadcastAssistantCallback);
}
}
@Override
@@ -175,18 +265,49 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
mSwitchBar.setEnabled(true);
return;
}
ArrayList<String> deviceNames = new ArrayList<>();
Map<Integer, List<CachedBluetoothDevice>> groupedDevices = fetchConnectedDevicesByGroupId();
ArrayList<AudioSharingDeviceItem> deviceItems = new ArrayList<>();
Optional<Integer> activeGroupId = Optional.empty();
for (List<CachedBluetoothDevice> devices : groupedDevices.values()) {
// Use random device in the group to represent the group.
CachedBluetoothDevice device = devices.get(0);
// TODO: add BluetoothUtils.isActiveLeAudioDevice to avoid directly using isActiveDevice
if (device.isActiveDevice(BluetoothProfile.LE_AUDIO)) {
activeGroupId = Optional.of(device.getGroupId());
} else {
AudioSharingDeviceItem item =
new AudioSharingDeviceItem(device.getName(), device.getGroupId());
deviceItems.add(item);
}
}
mTargetSinks = new ArrayList<>();
activeGroupId.ifPresent(
gId -> {
if (groupedDevices.containsKey(gId)) {
for (CachedBluetoothDevice device : groupedDevices.get(gId)) {
mTargetSinks.add(device.getDevice());
}
}
});
AudioSharingDialogFragment.show(
mFragment,
deviceNames,
deviceItems,
new AudioSharingDialogFragment.DialogEventListener() {
@Override
public void onItemClick(int position) {
// TODO: handle broadcast based on the dialog device item clicked
public void onItemClick(AudioSharingDeviceItem item) {
if (groupedDevices.containsKey(item.getGroupId())) {
for (CachedBluetoothDevice device :
groupedDevices.get(item.getGroupId())) {
mTargetSinks.add(device.getDevice());
}
}
// TODO: handle app source name for broadcasting.
mBroadcast.startBroadcast("test", /* language= */ null);
}
@Override
public void onCancelClick() {
// TODO: handle app source name for broadcasting.
mBroadcast.startBroadcast("test", /* language= */ null);
}
});
@@ -213,4 +334,51 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
private boolean isBroadcasting() {
return mBroadcast != null && mBroadcast.isEnabled(null);
}
private Map<Integer, List<CachedBluetoothDevice>> fetchConnectedDevicesByGroupId() {
// TODO: filter out devices with le audio disabled.
List<BluetoothDevice> connectedDevices =
mAssistant == null ? ImmutableList.of() : mAssistant.getConnectedDevices();
Map<Integer, List<CachedBluetoothDevice>> groupedDevices = new HashMap<>();
CachedBluetoothDeviceManager cacheManager = mBtManager.getCachedDeviceManager();
for (BluetoothDevice device : connectedDevices) {
CachedBluetoothDevice cachedDevice = cacheManager.findDevice(device);
if (cachedDevice == null) {
Log.d(TAG, "Skip device due to not being cached: " + device.getAnonymizedAddress());
continue;
}
int groupId = cachedDevice.getGroupId();
if (groupId == BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
Log.d(TAG, "Skip device due to no valid group id");
continue;
}
if (!groupedDevices.containsKey(groupId)) {
groupedDevices.put(groupId, new ArrayList<>());
}
groupedDevices.get(groupId).add(cachedDevice);
}
return groupedDevices;
}
private void addSourceToTargetDevices(List<BluetoothDevice> sinks) {
if (sinks.isEmpty() || mBroadcast == null || mAssistant == null) {
Log.d(TAG, "Skip adding source to target.");
return;
}
BluetoothLeBroadcastMetadata broadcastMetadata =
mBroadcast.getLatestBluetoothLeBroadcastMetadata();
if (broadcastMetadata == null) {
Log.e(TAG, "Error: There is no broadcastMetadata.");
return;
}
for (BluetoothDevice sink : sinks) {
Log.d(
TAG,
"Add broadcast with broadcastId: "
+ broadcastMetadata.getBroadcastId()
+ "to the device: "
+ sink.getAnonymizedAddress());
mAssistant.addSource(sink, broadcastMetadata, /* isGroupOp= */ false);
}
}
}