[Audiosharing] Use metadata from callback to add source

Instead of query metadata from LocalBluetoothLeBroadcast. In some corner
case, LocalBluetoothLeBroadcast receive callbacks later than Settings
app so it can not return latest metadata.

Test: atest
Flag: EXEMPT small fix
Bug: 402199690
Change-Id: I0d1d36d0e594dc997b05b2f1d302ce1e2787990a
This commit is contained in:
Yiyi Shen
2025-03-18 14:30:31 +08:00
parent b96eb497da
commit 383d2c165e

View File

@@ -192,7 +192,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
Log.d(TAG, "Skip handleOnBroadcastReady, not in starting process"); Log.d(TAG, "Skip handleOnBroadcastReady, not in starting process");
return; return;
} }
handleOnBroadcastReady(); handleOnBroadcastReady(metadata);
} }
@Override @Override
@@ -273,7 +273,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
+ mSinksInAdding); + mSinksInAdding);
if (mSinksToWaitFor.contains(sink)) { if (mSinksToWaitFor.contains(sink)) {
mSinksToWaitFor.remove(sink); mSinksToWaitFor.remove(sink);
if (mSinksToWaitFor.isEmpty()) { if (mSinksToWaitFor.isEmpty() && mBroadcast != null) {
// To avoid users advance to share then pair flow before the // To avoid users advance to share then pair flow before the
// primary/active sinks successfully join the audio sharing, // primary/active sinks successfully join the audio sharing,
// popup dialog till adding source complete for mSinksToWaitFor. // popup dialog till adding source complete for mSinksToWaitFor.
@@ -284,7 +284,8 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
/* userTriggered= */ false, /* userTriggered= */ false,
/* deviceCountInSharing= */ 1, /* deviceCountInSharing= */ 1,
/* candidateDeviceCount= */ 0); /* candidateDeviceCount= */ 0);
showAudioSharingDialog(eventData); showJoinAudioSharingDialog(eventData,
mBroadcast.getLatestBluetoothLeBroadcastMetadata());
} }
} }
} }
@@ -501,9 +502,10 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
mBtManager == null ? null : mBtManager.getCachedDeviceManager(); mBtManager == null ? null : mBtManager.getCachedDeviceManager();
CachedBluetoothDevice cachedDevice = CachedBluetoothDevice cachedDevice =
deviceManager == null ? null : deviceManager.findDevice(device); deviceManager == null ? null : deviceManager.findDevice(device);
if (cachedDevice != null) { if (cachedDevice != null && mBroadcast != null) {
Log.d(TAG, "handleAutoAddSourceAfterPair, device = " + device.getAnonymizedAddress()); Log.d(TAG, "handleAutoAddSourceAfterPair, device = " + device.getAnonymizedAddress());
addSourceToTargetSinks(ImmutableList.of(device), cachedDevice.getName()); addSourceToTargetSinks(ImmutableList.of(device), cachedDevice.getName(),
mBroadcast.getLatestBluetoothLeBroadcastMetadata());
} }
} }
@@ -642,7 +644,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
return mAssistant != null && mAssistant.getAllConnectedDevices().isEmpty(); return mAssistant != null && mAssistant.getAllConnectedDevices().isEmpty();
} }
private void handleOnBroadcastReady() { private void handleOnBroadcastReady(@NonNull BluetoothLeBroadcastMetadata metadata) {
List<BluetoothDevice> targetActiveSinks = mTargetActiveItem == null ? ImmutableList.of() List<BluetoothDevice> targetActiveSinks = mTargetActiveItem == null ? ImmutableList.of()
: mGroupedConnectedDevices.getOrDefault( : mGroupedConnectedDevices.getOrDefault(
mTargetActiveItem.getGroupId(), ImmutableList.of()); mTargetActiveItem.getGroupId(), ImmutableList.of());
@@ -656,7 +658,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
// Auto add primary/active sinks w/o user interactions. // Auto add primary/active sinks w/o user interactions.
if (!targetActiveSinks.isEmpty() && mTargetActiveItem != null) { if (!targetActiveSinks.isEmpty() && mTargetActiveItem != null) {
Log.d(TAG, "handleOnBroadcastReady: automatically add source to active sinks."); Log.d(TAG, "handleOnBroadcastReady: automatically add source to active sinks.");
addSourceToTargetSinks(targetActiveSinks, mTargetActiveItem.getName()); addSourceToTargetSinks(targetActiveSinks, mTargetActiveItem.getName(), metadata);
// To avoid users advance to share then pair flow before the primary/active sinks // To avoid users advance to share then pair flow before the primary/active sinks
// successfully join the audio sharing, save the primary/active sinks in mSinksToWaitFor // successfully join the audio sharing, save the primary/active sinks in mSinksToWaitFor
// and popup dialog till adding source complete for these sinks. // and popup dialog till adding source complete for these sinks.
@@ -677,7 +679,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
AudioSharingDeviceItem target = mDeviceItemsForSharing.get(0); AudioSharingDeviceItem target = mDeviceItemsForSharing.get(0);
List<BluetoothDevice> targetSinks = mGroupedConnectedDevices.getOrDefault( List<BluetoothDevice> targetSinks = mGroupedConnectedDevices.getOrDefault(
target.getGroupId(), ImmutableList.of()); target.getGroupId(), ImmutableList.of());
addSourceToTargetSinks(targetSinks, target.getName()); addSourceToTargetSinks(targetSinks, target.getName(), metadata);
cleanUpStatesForStartSharing(); cleanUpStatesForStartSharing();
// TODO: Add metric for auto add by intent // TODO: Add metric for auto add by intent
return; return;
@@ -698,20 +700,21 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
// successfully join the audio sharing, popup dialog till adding source complete for // successfully join the audio sharing, popup dialog till adding source complete for
// mSinksToWaitFor. // mSinksToWaitFor.
if (mSinksToWaitFor.isEmpty() && !mStoppingSharing.get()) { if (mSinksToWaitFor.isEmpty() && !mStoppingSharing.get()) {
showAudioSharingDialog(eventData); showJoinAudioSharingDialog(eventData, metadata);
} }
} }
private void showAudioSharingDialog(Pair<Integer, Object>[] eventData) { private void showJoinAudioSharingDialog(Pair<Integer, Object>[] eventData,
@Nullable BluetoothLeBroadcastMetadata metadata) {
if (!BluetoothUtils.isBroadcasting(mBtManager)) { if (!BluetoothUtils.isBroadcasting(mBtManager)) {
Log.d(TAG, "Skip showAudioSharingDialog, broadcast is stopped"); Log.d(TAG, "Skip showJoinAudioSharingDialog, broadcast is stopped");
return; return;
} }
AudioSharingDialogFragment.DialogEventListener listener = AudioSharingDialogFragment.DialogEventListener listener =
new AudioSharingDialogFragment.DialogEventListener() { new AudioSharingDialogFragment.DialogEventListener() {
@Override @Override
public void onPositiveClick() { public void onPositiveClick() {
// Could go to other pages, dismiss the progress dialog. // Could go to other pages (pair new device), dismiss the progress dialog.
dismissProgressDialogIfNeeded(); dismissProgressDialogIfNeeded();
cleanUpStatesForStartSharing(); cleanUpStatesForStartSharing();
} }
@@ -720,19 +723,17 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
public void onItemClick(@NonNull AudioSharingDeviceItem item) { public void onItemClick(@NonNull AudioSharingDeviceItem item) {
List<BluetoothDevice> targetSinks = mGroupedConnectedDevices.getOrDefault( List<BluetoothDevice> targetSinks = mGroupedConnectedDevices.getOrDefault(
item.getGroupId(), ImmutableList.of()); item.getGroupId(), ImmutableList.of());
addSourceToTargetSinks(targetSinks, item.getName()); addSourceToTargetSinks(targetSinks, item.getName(), metadata);
cleanUpStatesForStartSharing(); cleanUpStatesForStartSharing();
} }
@Override @Override
public void onCancelClick() { public void onCancelClick() {
// Could go to other pages, dismiss the progress dialog. // Could go to other pages (show qr code), dismiss the progress dialog.
dismissProgressDialogIfNeeded(); dismissProgressDialogIfNeeded();
cleanUpStatesForStartSharing(); cleanUpStatesForStartSharing();
} }
}; };
BluetoothLeBroadcastMetadata metadata = mBroadcast == null ? null
: mBroadcast.getLatestBluetoothLeBroadcastMetadata();
AudioSharingUtils.postOnMainThread( AudioSharingUtils.postOnMainThread(
mContext, mContext,
() -> AudioSharingDialogFragment.show( () -> AudioSharingDialogFragment.show(
@@ -828,13 +829,27 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
}); });
} }
private void addSourceToTargetSinks(List<BluetoothDevice> targetActiveSinks, private void addSourceToTargetSinks(List<BluetoothDevice> targetGroupedSinks,
@NonNull String sinkName) { @NonNull String targetSinkName, @Nullable BluetoothLeBroadcastMetadata metadata) {
mSinksInAdding.addAll(targetActiveSinks); if (targetGroupedSinks.isEmpty()) {
Log.d(TAG, "Skip addSourceToTargetSinks, no sinks.");
return;
}
if (metadata == null) {
Log.d(TAG, "Skip addSourceToTargetSinks, metadata is null");
return;
}
if (mAssistant == null) {
Log.d(TAG, "skip addSourceToTargetDevices, assistant profile is null.");
return;
}
mSinksInAdding.addAll(targetGroupedSinks);
String progressMessage = mContext.getString( String progressMessage = mContext.getString(
R.string.audio_sharing_progress_dialog_add_source_content, sinkName); R.string.audio_sharing_progress_dialog_add_source_content, targetSinkName);
showProgressDialog(progressMessage); showProgressDialog(progressMessage);
AudioSharingUtils.addSourceToTargetSinks(targetActiveSinks, mBtManager); for (BluetoothDevice sink : targetGroupedSinks) {
mAssistant.addSource(sink, metadata, /* isGroupOp= */ false);
}
} }
private void showProgressDialog(@NonNull String progressMessage) { private void showProgressDialog(@NonNull String progressMessage) {