[Audiosharing] Use setBroadcastToUnicastFallbackGroup to set primary

Flag: com.android.settingslib.flags.adopt_primary_group_management_api
Test: atest
Bug: 381946931
Change-Id: Ib06dd2e202b07b9cdb25b1a671ee4d57246415ba
This commit is contained in:
Yiyi Shen
2025-01-15 15:25:32 +08:00
parent e87733be63
commit 27e4fcc6c6
7 changed files with 88 additions and 29 deletions

View File

@@ -16,6 +16,8 @@
package com.android.settings.connecteddevice.audiosharing;
import static com.android.settingslib.Utils.isAudioModeOngoingCall;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCsipSetCoordinator;
@@ -48,6 +50,7 @@ import com.android.settingslib.bluetooth.BluetoothEventManager;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LeAudioProfile;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
@@ -91,6 +94,7 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
Map<Integer, List<BluetoothDevice>> mGroupedConnectedDevices = new HashMap<>();
private List<AudioSharingDeviceItem> mDeviceItemsInSharingSession = new ArrayList<>();
private final AtomicBoolean mCallbacksRegistered = new AtomicBoolean(false);
private AtomicBoolean mIsAudioModeOngoingCall = new AtomicBoolean(false);
@VisibleForTesting
final BluetoothLeBroadcastAssistant.Callback mBroadcastAssistantCallback =
@@ -202,28 +206,15 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
mDeviceItemsInSharingSession,
pair == null ? -1 : pair.first,
(AudioSharingDeviceItem item) -> {
int currentGroupId =
int currentCallAudioGroupId =
BluetoothUtils.getPrimaryGroupIdForBroadcast(
mContext.getContentResolver());
int clickedGroupId = item.getGroupId();
if (clickedGroupId == currentGroupId) {
if (clickedGroupId == currentCallAudioGroupId) {
Log.d(TAG, "Skip set call audio device: unchanged");
return;
}
List<BluetoothDevice> devices =
mGroupedConnectedDevices.getOrDefault(
clickedGroupId, ImmutableList.of());
CachedBluetoothDevice lead =
AudioSharingUtils.getLeadDevice(
mCacheManager, devices);
if (lead != null) {
String addr = lead.getDevice().getAnonymizedAddress();
Log.d(TAG, "Set call audio device: " + addr);
AudioSharingUtils.setPrimary(mContext, lead);
logCallAudioDeviceChange(currentGroupId, lead);
} else {
Log.d(TAG, "Skip set call audio device: no lead");
}
setCallAudioGroup(clickedGroupId);
});
}
return true;
@@ -269,6 +260,11 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
}
}
@Override
public void onAudioModeChanged() {
mIsAudioModeOngoingCall.set(isAudioModeOngoingCall(mContext));
}
/**
* Initialize the controller.
*
@@ -311,6 +307,7 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
false,
mSettingsObserver);
mAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
mIsAudioModeOngoingCall.set(isAudioModeOngoingCall(mContext));
mCallbacksRegistered.set(true);
}
}
@@ -333,6 +330,32 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
}
}
private void setCallAudioGroup(int groupId) {
List<BluetoothDevice> devices =
mGroupedConnectedDevices.getOrDefault(
groupId, ImmutableList.of());
CachedBluetoothDevice lead =
AudioSharingUtils.getLeadDevice(
mCacheManager, devices);
if (lead != null) {
String addr = lead.getDevice().getAnonymizedAddress();
Log.d(TAG, "Set call audio device: " + addr);
if (Flags.adoptPrimaryGroupManagementApi() && !mIsAudioModeOngoingCall.get()) {
LeAudioProfile leaProfile = mBtManager == null ? null
: mBtManager.getProfileManager().getLeAudioProfile();
if (leaProfile != null) {
leaProfile.setBroadcastToUnicastFallbackGroup(groupId);
}
} else {
lead.setActive();
}
AudioSharingUtils.setUserPreferredPrimary(mContext, lead);
logCallAudioDeviceChange(groupId, lead);
} else {
Log.d(TAG, "Skip set call audio device: no lead");
}
}
/**
* Update the preference summary: current headset for call audio.
*

View File

@@ -389,7 +389,8 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
Log.d(TAG, "onDeviceClick, set active in call mode");
CachedBluetoothDevice cachedDevice =
((BluetoothDevicePreference) preference).getBluetoothDevice();
AudioSharingUtils.setPrimary(mContext, cachedDevice);
cachedDevice.setActive();
AudioSharingUtils.setUserPreferredPrimary(mContext, cachedDevice);
}
mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_AUDIO_SHARING_DEVICE_CLICK,
isCallMode);

View File

@@ -192,7 +192,8 @@ public class AudioSharingDialogHandler {
// If this method is called with user triggered, e.g. manual click on the
// "Connected devices" page, we need call setActive for the device, since user
// intend to switch active device for the call.
AudioSharingUtils.setPrimary(mContext, cachedDevice);
cachedDevice.setActive();
AudioSharingUtils.setUserPreferredPrimary(mContext, cachedDevice);
}
return;
}

View File

@@ -346,11 +346,10 @@ public class AudioSharingUtils {
return vc != null && vc.isProfileReady();
}
/** Set {@link CachedBluetoothDevice} as primary device for call audio */
public static void setPrimary(
/** Set {@link CachedBluetoothDevice} as user preferred primary device for call audio */
public static void setUserPreferredPrimary(
@NonNull Context context, @Nullable CachedBluetoothDevice cachedDevice) {
if (cachedDevice == null) return;
cachedDevice.setActive();
if (BluetoothUtils.isAudioSharingHysteresisModeFixAvailable(context)) {
int groupId = BluetoothUtils.getGroupId(cachedDevice);
// TODO: use real key name in SettingsProvider