From 383d2c165e59a53019781a7ad496d23f66864e66 Mon Sep 17 00:00:00 2001 From: Yiyi Shen Date: Tue, 18 Mar 2025 14:30:31 +0800 Subject: [PATCH] [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 --- .../AudioSharingSwitchBarController.java | 57 ++++++++++++------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java index 92ebc57bf52..f1fcd694b0a 100644 --- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java +++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java @@ -192,7 +192,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController Log.d(TAG, "Skip handleOnBroadcastReady, not in starting process"); return; } - handleOnBroadcastReady(); + handleOnBroadcastReady(metadata); } @Override @@ -273,7 +273,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController + mSinksInAdding); if (mSinksToWaitFor.contains(sink)) { mSinksToWaitFor.remove(sink); - if (mSinksToWaitFor.isEmpty()) { + if (mSinksToWaitFor.isEmpty() && mBroadcast != null) { // To avoid users advance to share then pair flow before the // primary/active sinks successfully join the audio sharing, // popup dialog till adding source complete for mSinksToWaitFor. @@ -284,7 +284,8 @@ public class AudioSharingSwitchBarController extends BasePreferenceController /* userTriggered= */ false, /* deviceCountInSharing= */ 1, /* candidateDeviceCount= */ 0); - showAudioSharingDialog(eventData); + showJoinAudioSharingDialog(eventData, + mBroadcast.getLatestBluetoothLeBroadcastMetadata()); } } } @@ -501,9 +502,10 @@ public class AudioSharingSwitchBarController extends BasePreferenceController mBtManager == null ? null : mBtManager.getCachedDeviceManager(); CachedBluetoothDevice cachedDevice = deviceManager == null ? null : deviceManager.findDevice(device); - if (cachedDevice != null) { + if (cachedDevice != null && mBroadcast != null) { 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(); } - private void handleOnBroadcastReady() { + private void handleOnBroadcastReady(@NonNull BluetoothLeBroadcastMetadata metadata) { List targetActiveSinks = mTargetActiveItem == null ? ImmutableList.of() : mGroupedConnectedDevices.getOrDefault( mTargetActiveItem.getGroupId(), ImmutableList.of()); @@ -656,7 +658,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController // Auto add primary/active sinks w/o user interactions. if (!targetActiveSinks.isEmpty() && mTargetActiveItem != null) { 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 // successfully join the audio sharing, save the primary/active sinks in mSinksToWaitFor // and popup dialog till adding source complete for these sinks. @@ -677,7 +679,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController AudioSharingDeviceItem target = mDeviceItemsForSharing.get(0); List targetSinks = mGroupedConnectedDevices.getOrDefault( target.getGroupId(), ImmutableList.of()); - addSourceToTargetSinks(targetSinks, target.getName()); + addSourceToTargetSinks(targetSinks, target.getName(), metadata); cleanUpStatesForStartSharing(); // TODO: Add metric for auto add by intent return; @@ -698,20 +700,21 @@ public class AudioSharingSwitchBarController extends BasePreferenceController // successfully join the audio sharing, popup dialog till adding source complete for // mSinksToWaitFor. if (mSinksToWaitFor.isEmpty() && !mStoppingSharing.get()) { - showAudioSharingDialog(eventData); + showJoinAudioSharingDialog(eventData, metadata); } } - private void showAudioSharingDialog(Pair[] eventData) { + private void showJoinAudioSharingDialog(Pair[] eventData, + @Nullable BluetoothLeBroadcastMetadata metadata) { if (!BluetoothUtils.isBroadcasting(mBtManager)) { - Log.d(TAG, "Skip showAudioSharingDialog, broadcast is stopped"); + Log.d(TAG, "Skip showJoinAudioSharingDialog, broadcast is stopped"); return; } AudioSharingDialogFragment.DialogEventListener listener = new AudioSharingDialogFragment.DialogEventListener() { @Override 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(); cleanUpStatesForStartSharing(); } @@ -720,19 +723,17 @@ public class AudioSharingSwitchBarController extends BasePreferenceController public void onItemClick(@NonNull AudioSharingDeviceItem item) { List targetSinks = mGroupedConnectedDevices.getOrDefault( item.getGroupId(), ImmutableList.of()); - addSourceToTargetSinks(targetSinks, item.getName()); + addSourceToTargetSinks(targetSinks, item.getName(), metadata); cleanUpStatesForStartSharing(); } @Override 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(); cleanUpStatesForStartSharing(); } }; - BluetoothLeBroadcastMetadata metadata = mBroadcast == null ? null - : mBroadcast.getLatestBluetoothLeBroadcastMetadata(); AudioSharingUtils.postOnMainThread( mContext, () -> AudioSharingDialogFragment.show( @@ -828,13 +829,27 @@ public class AudioSharingSwitchBarController extends BasePreferenceController }); } - private void addSourceToTargetSinks(List targetActiveSinks, - @NonNull String sinkName) { - mSinksInAdding.addAll(targetActiveSinks); + private void addSourceToTargetSinks(List targetGroupedSinks, + @NonNull String targetSinkName, @Nullable BluetoothLeBroadcastMetadata metadata) { + 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( - R.string.audio_sharing_progress_dialog_add_source_content, sinkName); + R.string.audio_sharing_progress_dialog_add_source_content, targetSinkName); showProgressDialog(progressMessage); - AudioSharingUtils.addSourceToTargetSinks(targetActiveSinks, mBtManager); + for (BluetoothDevice sink : targetGroupedSinks) { + mAssistant.addSource(sink, metadata, /* isGroupOp= */ false); + } } private void showProgressDialog(@NonNull String progressMessage) {