[Audiosharing] Avoid dialog flickering for same bt device group.

Before this change, the dialog will dismiss and show up again if the
second bud of the same group is connected.

In this change, StopDialog and JoinDialog will only update the message
and event listener when the dialog is triggered by another device with the same group id or the new bt device with different group id. (No dismiss and show); DisconnectDialog will only dismiss and show again when a new bt device with different group id is connected and update the event listener when another device with the same group id connected.

Also dismiss the dialog when the triggering device disconnect or join
the sharing.

Test: atest
Bug: 305620450
Change-Id: I798812101cefb24185d76d56f78eae27712dd7a6
This commit is contained in:
Yiyi Shen
2024-02-05 20:47:28 +08:00
parent 4e0315d2a3
commit 10bddfd334
8 changed files with 464 additions and 97 deletions

View File

@@ -17,6 +17,7 @@
package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastAssistant;
@@ -27,6 +28,7 @@ import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.DefaultLifecycleObserver;
@@ -43,6 +45,7 @@ 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.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LeAudioProfile;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
@@ -68,9 +71,10 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
"connected_device_audio_sharing_settings";
private final LocalBluetoothManager mLocalBtManager;
private final LocalBluetoothLeBroadcast mBroadcast;
private final LocalBluetoothLeBroadcastAssistant mAssistant;
private final Executor mExecutor;
private CachedBluetoothDeviceManager mDeviceManager;
private LocalBluetoothLeBroadcast mBroadcast;
private LocalBluetoothLeBroadcastAssistant mAssistant;
private PreferenceGroup mPreferenceGroup;
private Preference mAudioSharingSettingsPreference;
private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
@@ -165,6 +169,12 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
+ ", reason = "
+ reason);
mBluetoothDeviceUpdater.forceUpdate();
if (mDeviceManager != null) {
CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(sink);
if (cachedDevice != null) {
closeOpeningDialogsForLeaDevice(cachedDevice);
}
}
}
@Override
@@ -241,8 +251,11 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
public AudioSharingDevicePreferenceController(Context context) {
super(context, KEY);
mLocalBtManager = Utils.getLocalBtManager(mContext);
mBroadcast = mLocalBtManager.getProfileManager().getLeAudioBroadcastProfile();
mAssistant = mLocalBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
if (mLocalBtManager != null) {
mDeviceManager = mLocalBtManager.getCachedDeviceManager();
mBroadcast = mLocalBtManager.getProfileManager().getLeAudioBroadcastProfile();
mAssistant = mLocalBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
}
mExecutor = Executors.newSingleThreadExecutor();
}
@@ -344,6 +357,17 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
@NonNull CachedBluetoothDevice cachedDevice,
@ConnectionState int state,
int bluetoothProfile) {
if (state == BluetoothAdapter.STATE_DISCONNECTED) {
boolean isLeAudio = isLeAudioSupported(cachedDevice);
if (isLeAudio && bluetoothProfile == BluetoothProfile.LE_AUDIO) {
closeOpeningDialogsForLeaDevice(cachedDevice);
return;
}
if (!isLeAudio && !cachedDevice.isConnected()) {
closeOpeningDialogsForNonLeaDevice(cachedDevice);
return;
}
}
if (state != BluetoothAdapter.STATE_CONNECTED || !cachedDevice.getDevice().isConnected()) {
Log.d(TAG, "Ignore onProfileConnectionStateChanged, not connected state");
return;
@@ -420,10 +444,10 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
// connected during a sharing session.
postOnMainThread(
() -> {
closeOpeningDialogs();
closeOpeningDialogsOtherThan(AudioSharingStopDialogFragment.tag());
AudioSharingStopDialogFragment.show(
mFragment,
cachedDevice.getName(),
cachedDevice,
() -> mBroadcast.stopBroadcast(mBroadcast.getLatestBroadcastId()));
});
} else {
@@ -466,11 +490,12 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
if (deviceItemsInSharingSession.size() >= 2) {
postOnMainThread(
() -> {
closeOpeningDialogs();
closeOpeningDialogsOtherThan(
AudioSharingDisconnectDialogFragment.tag());
AudioSharingDisconnectDialogFragment.show(
mFragment,
deviceItemsInSharingSession,
cachedDevice.getName(),
cachedDevice,
(AudioSharingDeviceItem item) -> {
// Remove all sources from the device user clicked
if (groupedDevices.containsKey(item.getGroupId())) {
@@ -497,11 +522,11 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
// remote device connected during a sharing session.
postOnMainThread(
() -> {
closeOpeningDialogs();
closeOpeningDialogsOtherThan(AudioSharingJoinDialogFragment.tag());
AudioSharingJoinDialogFragment.show(
mFragment,
deviceItemsInSharingSession,
cachedDevice.getName(),
cachedDevice,
() -> {
// Add current broadcast to the latest connected device
mAssistant.addSource(
@@ -527,11 +552,11 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
if (deviceItems.size() == 1) {
postOnMainThread(
() -> {
closeOpeningDialogs();
closeOpeningDialogsOtherThan(AudioSharingJoinDialogFragment.tag());
AudioSharingJoinDialogFragment.show(
mFragment,
deviceItems,
cachedDevice.getName(),
cachedDevice,
() -> {
mTargetSinks = new ArrayList<>();
for (List<CachedBluetoothDevice> devices :
@@ -591,17 +616,61 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
}
}
private void closeOpeningDialogs() {
private void closeOpeningDialogsOtherThan(String tag) {
if (mFragment == null) return;
List<Fragment> fragments = mFragment.getChildFragmentManager().getFragments();
for (Fragment fragment : fragments) {
if (fragment instanceof DialogFragment) {
if (fragment instanceof DialogFragment && !fragment.getTag().equals(tag)) {
Log.d(TAG, "Remove staled opening dialog " + fragment.getTag());
((DialogFragment) fragment).dismiss();
}
}
}
private void closeOpeningDialogsForLeaDevice(@NonNull CachedBluetoothDevice cachedDevice) {
if (mFragment == null) return;
int groupId = AudioSharingUtils.getGroupId(cachedDevice);
List<Fragment> fragments = mFragment.getChildFragmentManager().getFragments();
for (Fragment fragment : fragments) {
CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment);
if (device != null
&& groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID
&& AudioSharingUtils.getGroupId(device) == groupId) {
Log.d(TAG, "Remove staled opening dialog for group " + groupId);
((DialogFragment) fragment).dismiss();
}
}
}
private void closeOpeningDialogsForNonLeaDevice(@NonNull CachedBluetoothDevice cachedDevice) {
if (mFragment == null) return;
String address = cachedDevice.getAddress();
List<Fragment> fragments = mFragment.getChildFragmentManager().getFragments();
for (Fragment fragment : fragments) {
CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment);
if (device != null && address != null && address.equals(device.getAddress())) {
Log.d(
TAG,
"Remove staled opening dialog for device "
+ cachedDevice.getDevice().getAnonymizedAddress());
((DialogFragment) fragment).dismiss();
}
}
}
@Nullable
private CachedBluetoothDevice getCachedBluetoothDeviceFromDialog(Fragment fragment) {
CachedBluetoothDevice device = null;
if (fragment instanceof AudioSharingJoinDialogFragment) {
device = ((AudioSharingJoinDialogFragment) fragment).getDevice();
} else if (fragment instanceof AudioSharingStopDialogFragment) {
device = ((AudioSharingStopDialogFragment) fragment).getDevice();
} else if (fragment instanceof AudioSharingDisconnectDialogFragment) {
device = ((AudioSharingDisconnectDialogFragment) fragment).getDevice();
}
return device;
}
private void postOnMainThread(@NonNull Runnable runnable) {
mContext.getMainExecutor().execute(runnable);
}