[Audiosharing] Check profile readiness before adding source

Test: atest
Flag: com.android.settingslib.flags.enable_le_audio_sharing
Bug: 362858921
Change-Id: I9ee6226d1bd16225adf1756678ef7565941d7c60
diff --git a/src/com/android/settings/bluetooth/BluetoothDevicePairingDetailBase.java b/src/com/android/settings/bluetooth/BluetoothDevicePairingDetailBase.java
index 36a14aa..33eba10 100644
--- a/src/com/android/settings/bluetooth/BluetoothDevicePairingDetailBase.java
+++ b/src/com/android/settings/bluetooth/BluetoothDevicePairingDetailBase.java
@@ -48,6 +48,8 @@
 import com.android.settingslib.bluetooth.HearingAidStatsLogUtils;
 import com.android.settingslib.utils.ThreadUtils;
 
+import com.google.common.collect.ImmutableList;
+
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -58,8 +60,11 @@
  * device pairing detail page.
  */
 public abstract class BluetoothDevicePairingDetailBase extends DeviceListPreferenceFragment {
-    private static final long AUTO_DISMISS_TIME_THRESHOLD_MS = TimeUnit.SECONDS.toMillis(10);
+    private static final long AUTO_DISMISS_TIME_THRESHOLD_MS = TimeUnit.SECONDS.toMillis(15);
     private static final int AUTO_DISMISS_MESSAGE_ID = 1001;
+    private static final ImmutableList<Integer> AUDIO_SHARING_PROFILES = ImmutableList.of(
+            BluetoothProfile.LE_AUDIO,
+            BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT, BluetoothProfile.VOLUME_CONTROL);
 
     protected boolean mInitialScanStarted;
     @VisibleForTesting
@@ -229,12 +234,13 @@
             if (device != null
                     && mSelectedList.contains(device)) {
                 if (BluetoothUtils.isAudioSharingUIAvailable(getContext())) {
-                    if (bluetoothProfile == BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT
+                    if (mShouldTriggerAudioSharingShareThenPairFlow
                             && state == BluetoothAdapter.STATE_CONNECTED
                             && device.equals(mJustBonded)
-                            && mShouldTriggerAudioSharingShareThenPairFlow) {
+                            && AUDIO_SHARING_PROFILES.contains(bluetoothProfile)
+                            && isReadyForAudioSharing(cachedDevice, bluetoothProfile)) {
                         Log.d(getLogTag(),
-                                "onProfileConnectionStateChanged, assistant profile connected");
+                                "onProfileConnectionStateChanged, ready for audio sharing");
                         dismissConnectingDialog();
                         mHandler.removeMessages(AUTO_DISMISS_MESSAGE_ID);
                         finishFragmentWithResultForAudioSharing(device);
@@ -322,6 +328,35 @@
         return false;
     }
 
+    private boolean isReadyForAudioSharing(@NonNull CachedBluetoothDevice cachedDevice,
+            int justConnectedProfile) {
+        for (int profile : AUDIO_SHARING_PROFILES) {
+            // Skip checking connection state for just connected profile
+            if (profile == justConnectedProfile) continue;
+            switch (profile) {
+                case BluetoothProfile.LE_AUDIO -> {
+                    if (!cachedDevice.isConnectedLeAudioDevice()) {
+                        Log.d(getLogTag(), "isReadyForAudioSharing, LE_AUDIO not ready");
+                        return false;
+                    }
+                }
+                case BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT -> {
+                    if (!cachedDevice.isConnectedLeAudioBroadcastAssistantDevice()) {
+                        Log.d(getLogTag(), "isReadyForAudioSharing, ASSISTANT not ready");
+                        return false;
+                    }
+                }
+                case BluetoothProfile.VOLUME_CONTROL -> {
+                    if (!cachedDevice.isConnectedVolumeControlDevice()) {
+                        Log.d(getLogTag(), "isReadyForAudioSharing, VC not ready");
+                        return false;
+                    }
+                }
+            }
+        }
+        return true;
+    }
+
     private void addOnMetadataChangedListener(@Nullable BluetoothDevice device) {
         var unused = ThreadUtils.postOnBackgroundThread(() -> {
             if (mBluetoothAdapter != null && device != null
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePairingDetailBaseTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePairingDetailBaseTest.java
index 949b3d8..bd6ac4b 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePairingDetailBaseTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDevicePairingDetailBaseTest.java
@@ -308,6 +308,9 @@
         shadowOf(Looper.getMainLooper()).idle();
 
         when(mCachedBluetoothDevice.isConnected()).thenReturn(true);
+        when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
+        when(mCachedBluetoothDevice.isConnectedLeAudioBroadcastAssistantDevice()).thenReturn(true);
+        when(mCachedBluetoothDevice.isConnectedVolumeControlDevice()).thenReturn(true);
 
         mFragment.onProfileConnectionStateChanged(mCachedBluetoothDevice,
                 BluetoothAdapter.STATE_CONNECTED, BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT);