From c9c088ae34b2e0a1f2be4feeee6e96a44cdcbd6b Mon Sep 17 00:00:00 2001 From: Yiyi Shen Date: Mon, 9 Dec 2024 12:01:49 +0800 Subject: [PATCH] [Audiosharing] Check profile readiness before adding source Test: atest Flag: com.android.settingslib.flags.enable_le_audio_sharing Bug: 362858921 Change-Id: I9ee6226d1bd16225adf1756678ef7565941d7c60 --- .../BluetoothDevicePairingDetailBase.java | 43 +++++++++++++++++-- .../BluetoothDevicePairingDetailBaseTest.java | 3 ++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/com/android/settings/bluetooth/BluetoothDevicePairingDetailBase.java b/src/com/android/settings/bluetooth/BluetoothDevicePairingDetailBase.java index 36a14aa54fc..33eba1074c4 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.CachedBluetoothDevice; 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 @@ import java.util.concurrent.TimeUnit; * 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 AUDIO_SHARING_PROFILES = ImmutableList.of( + BluetoothProfile.LE_AUDIO, + BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT, BluetoothProfile.VOLUME_CONTROL); protected boolean mInitialScanStarted; @VisibleForTesting @@ -229,12 +234,13 @@ public abstract class BluetoothDevicePairingDetailBase extends DeviceListPrefere 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 @@ public abstract class BluetoothDevicePairingDetailBase extends DeviceListPrefere 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 949b3d83809..bd6ac4bf2c0 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 @@ public class BluetoothDevicePairingDetailBaseTest { 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);