[Audiosharing] Fix main sharing flow.

Fetch the active device before starting the sharing. Because once the
sharing is started, all devices connected will switch to inactive.

Flagged with enable_le_audio_sharing

Bug: 305620450
Test: manual
Change-Id: Ie287d1094fe40dbcb42c121ea79409fc48e0eec7
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java
index dc04bee..69a18e8 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java
@@ -45,12 +45,16 @@
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
 import com.android.settingslib.utils.ThreadUtils;
 
+import com.google.common.collect.ImmutableList;
+
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
+import java.util.stream.Collectors;
 
 public class AudioSharingSwitchBarController extends BasePreferenceController
         implements DefaultLifecycleObserver, OnCheckedChangeListener {
@@ -69,6 +73,9 @@
     private final Executor mExecutor;
     private final OnSwitchBarChangedListener mListener;
     private DashboardFragment mFragment;
+    private Map<Integer, List<CachedBluetoothDevice>> mGroupedConnectedDevices = new HashMap<>();
+    private List<BluetoothDevice> mTargetActiveSinks = new ArrayList<>();
+    private ArrayList<AudioSharingDeviceItem> mDeviceItemsForSharing = new ArrayList<>();
     @VisibleForTesting IntentFilter mIntentFilter;
 
     @VisibleForTesting
@@ -114,24 +121,7 @@
                                     + broadcastId
                                     + ", metadata = "
                                     + metadata.getBroadcastName());
-                    Map<Integer, List<CachedBluetoothDevice>> groupedConnectedDevices =
-                            AudioSharingUtils.fetchConnectedDevicesByGroupId(mBtManager);
-                    ArrayList<AudioSharingDeviceItem> deviceItems =
-                            AudioSharingUtils.buildOrderedConnectedLeadAudioSharingDeviceItem(
-                                    mBtManager,
-                                    groupedConnectedDevices,
-                                    /* filterByInSharing= */ false);
-                    // deviceItems is ordered. The active device is the first place if exits.
-                    ArrayList<AudioSharingDeviceItem> deviceItemsForSharing = deviceItems;
-                    if (!deviceItems.isEmpty() && deviceItems.get(0).isActive()) {
-                        for (CachedBluetoothDevice device :
-                                groupedConnectedDevices.get(deviceItems.get(0).getGroupId())) {
-                            // If active device exists for audio sharing, share to it
-                            // automatically once the broadcast is started.
-                            addSourceToSink(device.getDevice());
-                        }
-                        deviceItemsForSharing.remove(0);
-                    }
+                    addSourceToTargetSinks(mTargetActiveSinks);
                     if (mFragment == null) {
                         Log.w(TAG, "Dialog fail to show due to null fragment.");
                         return;
@@ -140,16 +130,16 @@
                             () -> {
                                 AudioSharingDialogFragment.show(
                                         mFragment,
-                                        deviceItemsForSharing,
+                                        mDeviceItemsForSharing,
                                         item -> {
-                                            if (groupedConnectedDevices.containsKey(
-                                                    item.getGroupId())) {
-                                                for (CachedBluetoothDevice device :
-                                                        groupedConnectedDevices.get(
-                                                                item.getGroupId())) {
-                                                    addSourceToSink(device.getDevice());
-                                                }
-                                            }
+                                            addSourceToTargetSinks(
+                                                    mGroupedConnectedDevices
+                                                            .getOrDefault(
+                                                                    item.getGroupId(),
+                                                                    ImmutableList.of())
+                                                            .stream()
+                                                            .map(CachedBluetoothDevice::getDevice)
+                                                            .collect(Collectors.toList()));
                                         });
                             });
                 }
@@ -333,6 +323,22 @@
             mSwitchBar.setEnabled(true);
             return;
         }
+        mGroupedConnectedDevices = AudioSharingUtils.fetchConnectedDevicesByGroupId(mBtManager);
+        ArrayList<AudioSharingDeviceItem> deviceItems =
+                AudioSharingUtils.buildOrderedConnectedLeadAudioSharingDeviceItem(
+                        mBtManager, mGroupedConnectedDevices, /* filterByInSharing= */ false);
+        // deviceItems is ordered. The active device is the first place if exits.
+        mDeviceItemsForSharing = new ArrayList<>(deviceItems);
+        if (!deviceItems.isEmpty() && deviceItems.get(0).isActive()) {
+            for (CachedBluetoothDevice device :
+                    mGroupedConnectedDevices.getOrDefault(
+                            deviceItems.get(0).getGroupId(), ImmutableList.of())) {
+                // If active device exists for audio sharing, share to it
+                // automatically once the broadcast is started.
+                mTargetActiveSinks.add(device.getDevice());
+            }
+            mDeviceItemsForSharing.remove(0);
+        }
         // TODO: start broadcast with new API
         mBroadcast.startBroadcast("test", null);
     }
@@ -367,8 +373,8 @@
         return mBroadcast != null && mBroadcast.isEnabled(null);
     }
 
-    private void addSourceToSink(BluetoothDevice sink) {
-        if (mBroadcast == null || mAssistant == null) {
+    private void addSourceToTargetSinks(List<BluetoothDevice> sinks) {
+        if (sinks.isEmpty() || mBroadcast == null || mAssistant == null) {
             Log.d(TAG, "Skip adding source to target.");
             return;
         }
@@ -378,12 +384,14 @@
             Log.e(TAG, "Error: There is no broadcastMetadata.");
             return;
         }
-        Log.d(
-                TAG,
-                "Add broadcast with broadcastId: "
-                        + broadcastMetadata.getBroadcastId()
-                        + " to the device: "
-                        + sink.getAnonymizedAddress());
-        mAssistant.addSource(sink, broadcastMetadata, /* isGroupOp= */ false);
+        for (BluetoothDevice sink : sinks) {
+            Log.d(
+                    TAG,
+                    "Add broadcast with broadcastId: "
+                            + broadcastMetadata.getBroadcastId()
+                            + "to the device: "
+                            + sink.getAnonymizedAddress());
+            mAssistant.addSource(sink, broadcastMetadata, /* isGroupOp= */ false);
+        }
     }
 }