Merge "[Audiosharing] Use metadata from callback to add source" into main
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java
index 92ebc57..f1fcd69 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java
@@ -192,7 +192,7 @@
                         Log.d(TAG, "Skip handleOnBroadcastReady, not in starting process");
                         return;
                     }
-                    handleOnBroadcastReady();
+                    handleOnBroadcastReady(metadata);
                 }
 
                 @Override
@@ -273,7 +273,7 @@
                             + 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 @@
                                             /* userTriggered= */ false,
                                             /* deviceCountInSharing= */ 1,
                                             /* candidateDeviceCount= */ 0);
-                            showAudioSharingDialog(eventData);
+                            showJoinAudioSharingDialog(eventData,
+                                    mBroadcast.getLatestBluetoothLeBroadcastMetadata());
                         }
                     }
                 }
@@ -501,9 +502,10 @@
                 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 @@
         return mAssistant != null && mAssistant.getAllConnectedDevices().isEmpty();
     }
 
-    private void handleOnBroadcastReady() {
+    private void handleOnBroadcastReady(@NonNull BluetoothLeBroadcastMetadata metadata) {
         List<BluetoothDevice> targetActiveSinks = mTargetActiveItem == null ? ImmutableList.of()
                 : mGroupedConnectedDevices.getOrDefault(
                         mTargetActiveItem.getGroupId(), ImmutableList.of());
@@ -656,7 +658,7 @@
         // 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 @@
                 AudioSharingDeviceItem target = mDeviceItemsForSharing.get(0);
                 List<BluetoothDevice> 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 @@
         // 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<Integer, Object>[] eventData) {
+    private void showJoinAudioSharingDialog(Pair<Integer, Object>[] 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 void onItemClick(@NonNull AudioSharingDeviceItem item) {
                         List<BluetoothDevice> 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 @@
                         });
     }
 
-    private void addSourceToTargetSinks(List<BluetoothDevice> targetActiveSinks,
-            @NonNull String sinkName) {
-        mSinksInAdding.addAll(targetActiveSinks);
+    private void addSourceToTargetSinks(List<BluetoothDevice> 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) {