Merge "[Audiosharing] Dismiss all dialogs before show a new one." into main
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDevicePreferenceController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDevicePreferenceController.java
index 483a74b..9329cc29 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDevicePreferenceController.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDevicePreferenceController.java
@@ -27,6 +27,8 @@
 import android.util.Log;
 
 import androidx.annotation.NonNull;
+import androidx.fragment.app.DialogFragment;
+import androidx.fragment.app.Fragment;
 import androidx.lifecycle.DefaultLifecycleObserver;
 import androidx.lifecycle.LifecycleOwner;
 import androidx.preference.Preference;
@@ -39,6 +41,7 @@
 import com.android.settings.core.BasePreferenceController;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settingslib.bluetooth.BluetoothCallback;
+import com.android.settingslib.bluetooth.BluetoothUtils;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
 import com.android.settingslib.bluetooth.LeAudioProfile;
 import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
@@ -54,6 +57,7 @@
 
 public class AudioSharingDevicePreferenceController extends BasePreferenceController
         implements DefaultLifecycleObserver, DevicePreferenceCallback, BluetoothCallback {
+    private static final boolean DEBUG = BluetoothUtils.D;
 
     private static final String TAG = "AudioSharingDevicePrefController";
     private static final String KEY = "audio_sharing_device_list";
@@ -372,19 +376,31 @@
                             + " non le audio device");
             return;
         }
+        if (DEBUG) {
+            Log.d(
+                    TAG,
+                    "Start handling onProfileConnectionStateChanged for "
+                            + cachedDevice.getDevice().getAnonymizedAddress());
+        }
         if (!isLeAudioSupported) {
             // Handle connected ineligible (non LE audio) remote device
             if (isBroadcasting()) {
                 // Show stop audio sharing dialog when an ineligible (non LE audio) remote device
                 // connected during a sharing session.
+                closeOpeningDialogs();
                 AudioSharingStopDialogFragment.show(
                         mFragment,
                         cachedDevice.getName(),
-                        () -> {
-                            mBroadcast.stopBroadcast(mBroadcast.getLatestBroadcastId());
-                        });
+                        () -> mBroadcast.stopBroadcast(mBroadcast.getLatestBroadcastId()));
+            } else {
+                // Do nothing for ineligible (non LE audio) remote device when no sharing session.
+                if (DEBUG) {
+                    Log.d(
+                            TAG,
+                            "Ignore onProfileConnectionStateChanged for non LE audio without"
+                                + " sharing session");
+                }
             }
-            // Do nothing for ineligible (non LE audio) remote device when no sharing session.
         } else {
             Map<Integer, List<CachedBluetoothDevice>> groupedDevices =
                     AudioSharingUtils.fetchConnectedDevicesByGroupId(mLocalBtManager);
@@ -398,6 +414,7 @@
                 // Show audio sharing switch dialog when the third eligible (LE audio) remote device
                 // connected during a sharing session.
                 if (deviceItemsInSharingSession.size() >= 2) {
+                    closeOpeningDialogs();
                     AudioSharingDisconnectDialogFragment.show(
                             mFragment,
                             deviceItemsInSharingSession,
@@ -421,6 +438,7 @@
                 } else {
                     // Show audio sharing join dialog when the first or second eligible (LE audio)
                     // remote device connected during a sharing session.
+                    closeOpeningDialogs();
                     AudioSharingJoinDialogFragment.show(
                             mFragment,
                             deviceItemsInSharingSession,
@@ -447,6 +465,7 @@
                 // Show audio sharing join dialog when the second eligible (LE audio) remote device
                 // connect and no sharing session.
                 if (deviceItems.size() == 1) {
+                    closeOpeningDialogs();
                     AudioSharingJoinDialogFragment.show(
                             mFragment,
                             deviceItems,
@@ -524,4 +543,15 @@
             mAssistant.addSource(sink, broadcastMetadata, /* isGroupOp= */ false);
         }
     }
+
+    private void closeOpeningDialogs() {
+        if (mFragment == null) return;
+        List<Fragment> fragments = mFragment.getChildFragmentManager().getFragments();
+        for (Fragment fragment : fragments) {
+            if (fragment instanceof DialogFragment) {
+                Log.d(TAG, "Remove staled opening dialog " + fragment.getTag());
+                ((DialogFragment) fragment).dismiss();
+            }
+        }
+    }
 }
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDisconnectDialogFragment.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDisconnectDialogFragment.java
index 365aed5..461c230 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDisconnectDialogFragment.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDisconnectDialogFragment.java
@@ -77,15 +77,12 @@
         if (!AudioSharingUtils.isFeatureEnabled()) return;
         final FragmentManager manager = host.getChildFragmentManager();
         sListener = listener;
-        if (manager.findFragmentByTag(TAG) == null) {
-            final Bundle bundle = new Bundle();
-            bundle.putParcelableArrayList(BUNDLE_KEY_DEVICE_TO_DISCONNECT_ITEMS, deviceItems);
-            bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDeviceName);
-            AudioSharingDisconnectDialogFragment dialog =
-                    new AudioSharingDisconnectDialogFragment();
-            dialog.setArguments(bundle);
-            dialog.show(manager, TAG);
-        }
+        final Bundle bundle = new Bundle();
+        bundle.putParcelableArrayList(BUNDLE_KEY_DEVICE_TO_DISCONNECT_ITEMS, deviceItems);
+        bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDeviceName);
+        AudioSharingDisconnectDialogFragment dialog = new AudioSharingDisconnectDialogFragment();
+        dialog.setArguments(bundle);
+        dialog.show(manager, TAG);
     }
 
     @Override
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingJoinDialogFragment.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingJoinDialogFragment.java
index 589c955..2d7b4c4 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingJoinDialogFragment.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingJoinDialogFragment.java
@@ -71,14 +71,12 @@
         if (!AudioSharingUtils.isFeatureEnabled()) return;
         final FragmentManager manager = host.getChildFragmentManager();
         sListener = listener;
-        if (manager.findFragmentByTag(TAG) == null) {
-            final Bundle bundle = new Bundle();
-            bundle.putParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS, deviceItems);
-            bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDeviceName);
-            final AudioSharingJoinDialogFragment dialog = new AudioSharingJoinDialogFragment();
-            dialog.setArguments(bundle);
-            dialog.show(manager, TAG);
-        }
+        final Bundle bundle = new Bundle();
+        bundle.putParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS, deviceItems);
+        bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDeviceName);
+        final AudioSharingJoinDialogFragment dialog = new AudioSharingJoinDialogFragment();
+        dialog.setArguments(bundle);
+        dialog.show(manager, TAG);
     }
 
     @Override
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingStopDialogFragment.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingStopDialogFragment.java
index 13416aa..31125de 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingStopDialogFragment.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingStopDialogFragment.java
@@ -56,13 +56,11 @@
         if (!AudioSharingUtils.isFeatureEnabled()) return;
         final FragmentManager manager = host.getChildFragmentManager();
         sListener = listener;
-        if (manager.findFragmentByTag(TAG) == null) {
-            final Bundle bundle = new Bundle();
-            bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDeviceName);
-            AudioSharingStopDialogFragment dialog = new AudioSharingStopDialogFragment();
-            dialog.setArguments(bundle);
-            dialog.show(manager, TAG);
-        }
+        final Bundle bundle = new Bundle();
+        bundle.putString(BUNDLE_KEY_NEW_DEVICE_NAME, newDeviceName);
+        AudioSharingStopDialogFragment dialog = new AudioSharingStopDialogFragment();
+        dialog.setArguments(bundle);
+        dialog.show(manager, TAG);
     }
 
     @Override