Loading packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java +39 −0 Original line number Diff line number Diff line package com.android.settingslib.bluetooth; import static com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast.UNKNOWN_VALUE_PLACEHOLDER; import static com.android.settingslib.widget.AdaptiveOutlineDrawable.ICON_TYPE_ADVANCED; import android.annotation.SuppressLint; Loading Loading @@ -704,12 +705,50 @@ public class BluetoothUtils { return !sourceList.isEmpty() && sourceList.stream().anyMatch(BluetoothUtils::isConnected); } /** * Check if {@link BluetoothDevice} has a active local broadcast source. * * @param device The bluetooth device to check. * @param localBtManager The BT manager to provide BT functions. * @return Whether the device has a active local broadcast source. */ @WorkerThread public static boolean hasActiveLocalBroadcastSourceForBtDevice( @Nullable BluetoothDevice device, @Nullable LocalBluetoothManager localBtManager) { LocalBluetoothLeBroadcastAssistant assistant = localBtManager == null ? null : localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile(); LocalBluetoothLeBroadcast broadcast = localBtManager == null ? null : localBtManager.getProfileManager().getLeAudioBroadcastProfile(); if (device == null || assistant == null || broadcast == null) { Log.d(TAG, "Skip check hasActiveLocalBroadcastSourceForBtDevice due to arg is null"); return false; } List<BluetoothLeBroadcastReceiveState> sourceList = assistant.getAllSources(device); int broadcastId = broadcast.getLatestBroadcastId(); return !sourceList.isEmpty() && broadcastId != UNKNOWN_VALUE_PLACEHOLDER && sourceList.stream() .anyMatch( source -> isSourceMatched(source, broadcastId)); } /** Checks the connectivity status based on the provided broadcast receive state. */ @WorkerThread public static boolean isConnected(BluetoothLeBroadcastReceiveState state) { return state.getBisSyncState().stream().anyMatch(bitmap -> bitmap != 0); } /** Checks if the broadcast id is matched based on the provided broadcast receive state. */ @WorkerThread public static boolean isSourceMatched( @Nullable BluetoothLeBroadcastReceiveState state, int broadcastId) { return state != null && state.getBroadcastId() == broadcastId; } /** * Checks if the Bluetooth device is an available hearing device, which means: 1) currently * connected 2) is Hearing Aid 3) connected profile match hearing aid related profiles (e.g. Loading packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothLeBroadcast.java +1 −1 Original line number Diff line number Diff line Loading @@ -101,7 +101,7 @@ public class LocalBluetoothLeBroadcast implements LocalBluetoothProfile { private static final int DEFAULT_CODE_MIN = 1000; // Order of this profile in device profiles list private static final int ORDINAL = 1; private static final int UNKNOWN_VALUE_PLACEHOLDER = -1; static final int UNKNOWN_VALUE_PLACEHOLDER = -1; private static final Uri[] SETTINGS_URIS = new Uri[] { Settings.Secure.getUriFor(Settings.Secure.BLUETOOTH_LE_BROADCAST_NAME), Loading packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothUtilsTest.java +30 −0 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ package com.android.settingslib.bluetooth; import static com.android.settingslib.bluetooth.BluetoothUtils.isAvailableAudioSharingMediaBluetoothDevice; import static com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast.UNKNOWN_VALUE_PLACEHOLDER; import static com.android.settingslib.flags.Flags.FLAG_ENABLE_DETERMINING_ADVANCED_DETAILS_HEADER_WITH_METADATA; import static com.google.common.truth.Truth.assertThat; Loading Loading @@ -96,6 +97,7 @@ public class BluetoothUtilsTest { + "</HEARABLE_CONTROL_SLICE_WITH_WIDTH>"; private static final String TEST_EXCLUSIVE_MANAGER_PACKAGE = "com.test.manager"; private static final String TEST_EXCLUSIVE_MANAGER_COMPONENT = "com.test.manager/.component"; private static final int TEST_BROADCAST_ID = 25; @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); Loading Loading @@ -670,6 +672,34 @@ public class BluetoothUtilsTest { .isFalse(); } @Test public void testHasActiveLocalBroadcastSourceForBtDevice_hasActiveLocalSource() { when(mBroadcast.getLatestBroadcastId()).thenReturn(TEST_BROADCAST_ID); when(mLeBroadcastReceiveState.getBroadcastId()).thenReturn(TEST_BROADCAST_ID); List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>(); sourceList.add(mLeBroadcastReceiveState); when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(sourceList); assertThat( BluetoothUtils.hasActiveLocalBroadcastSourceForBtDevice( mBluetoothDevice, mLocalBluetoothManager)) .isTrue(); } @Test public void testHasActiveLocalBroadcastSourceForBtDevice_noActiveLocalSource() { when(mLeBroadcastReceiveState.getBroadcastId()).thenReturn(UNKNOWN_VALUE_PLACEHOLDER); List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>(); sourceList.add(mLeBroadcastReceiveState); when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(sourceList); assertThat( BluetoothUtils.hasActiveLocalBroadcastSourceForBtDevice( mBluetoothDevice, mLocalBluetoothManager)) .isFalse(); } @Test public void isAvailableHearingDevice_isConnectedHearingAid_returnTure() { when(mCachedBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true); Loading Loading
packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java +39 −0 Original line number Diff line number Diff line package com.android.settingslib.bluetooth; import static com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast.UNKNOWN_VALUE_PLACEHOLDER; import static com.android.settingslib.widget.AdaptiveOutlineDrawable.ICON_TYPE_ADVANCED; import android.annotation.SuppressLint; Loading Loading @@ -704,12 +705,50 @@ public class BluetoothUtils { return !sourceList.isEmpty() && sourceList.stream().anyMatch(BluetoothUtils::isConnected); } /** * Check if {@link BluetoothDevice} has a active local broadcast source. * * @param device The bluetooth device to check. * @param localBtManager The BT manager to provide BT functions. * @return Whether the device has a active local broadcast source. */ @WorkerThread public static boolean hasActiveLocalBroadcastSourceForBtDevice( @Nullable BluetoothDevice device, @Nullable LocalBluetoothManager localBtManager) { LocalBluetoothLeBroadcastAssistant assistant = localBtManager == null ? null : localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile(); LocalBluetoothLeBroadcast broadcast = localBtManager == null ? null : localBtManager.getProfileManager().getLeAudioBroadcastProfile(); if (device == null || assistant == null || broadcast == null) { Log.d(TAG, "Skip check hasActiveLocalBroadcastSourceForBtDevice due to arg is null"); return false; } List<BluetoothLeBroadcastReceiveState> sourceList = assistant.getAllSources(device); int broadcastId = broadcast.getLatestBroadcastId(); return !sourceList.isEmpty() && broadcastId != UNKNOWN_VALUE_PLACEHOLDER && sourceList.stream() .anyMatch( source -> isSourceMatched(source, broadcastId)); } /** Checks the connectivity status based on the provided broadcast receive state. */ @WorkerThread public static boolean isConnected(BluetoothLeBroadcastReceiveState state) { return state.getBisSyncState().stream().anyMatch(bitmap -> bitmap != 0); } /** Checks if the broadcast id is matched based on the provided broadcast receive state. */ @WorkerThread public static boolean isSourceMatched( @Nullable BluetoothLeBroadcastReceiveState state, int broadcastId) { return state != null && state.getBroadcastId() == broadcastId; } /** * Checks if the Bluetooth device is an available hearing device, which means: 1) currently * connected 2) is Hearing Aid 3) connected profile match hearing aid related profiles (e.g. Loading
packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothLeBroadcast.java +1 −1 Original line number Diff line number Diff line Loading @@ -101,7 +101,7 @@ public class LocalBluetoothLeBroadcast implements LocalBluetoothProfile { private static final int DEFAULT_CODE_MIN = 1000; // Order of this profile in device profiles list private static final int ORDINAL = 1; private static final int UNKNOWN_VALUE_PLACEHOLDER = -1; static final int UNKNOWN_VALUE_PLACEHOLDER = -1; private static final Uri[] SETTINGS_URIS = new Uri[] { Settings.Secure.getUriFor(Settings.Secure.BLUETOOTH_LE_BROADCAST_NAME), Loading
packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothUtilsTest.java +30 −0 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ package com.android.settingslib.bluetooth; import static com.android.settingslib.bluetooth.BluetoothUtils.isAvailableAudioSharingMediaBluetoothDevice; import static com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast.UNKNOWN_VALUE_PLACEHOLDER; import static com.android.settingslib.flags.Flags.FLAG_ENABLE_DETERMINING_ADVANCED_DETAILS_HEADER_WITH_METADATA; import static com.google.common.truth.Truth.assertThat; Loading Loading @@ -96,6 +97,7 @@ public class BluetoothUtilsTest { + "</HEARABLE_CONTROL_SLICE_WITH_WIDTH>"; private static final String TEST_EXCLUSIVE_MANAGER_PACKAGE = "com.test.manager"; private static final String TEST_EXCLUSIVE_MANAGER_COMPONENT = "com.test.manager/.component"; private static final int TEST_BROADCAST_ID = 25; @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); Loading Loading @@ -670,6 +672,34 @@ public class BluetoothUtilsTest { .isFalse(); } @Test public void testHasActiveLocalBroadcastSourceForBtDevice_hasActiveLocalSource() { when(mBroadcast.getLatestBroadcastId()).thenReturn(TEST_BROADCAST_ID); when(mLeBroadcastReceiveState.getBroadcastId()).thenReturn(TEST_BROADCAST_ID); List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>(); sourceList.add(mLeBroadcastReceiveState); when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(sourceList); assertThat( BluetoothUtils.hasActiveLocalBroadcastSourceForBtDevice( mBluetoothDevice, mLocalBluetoothManager)) .isTrue(); } @Test public void testHasActiveLocalBroadcastSourceForBtDevice_noActiveLocalSource() { when(mLeBroadcastReceiveState.getBroadcastId()).thenReturn(UNKNOWN_VALUE_PLACEHOLDER); List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>(); sourceList.add(mLeBroadcastReceiveState); when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(sourceList); assertThat( BluetoothUtils.hasActiveLocalBroadcastSourceForBtDevice( mBluetoothDevice, mLocalBluetoothManager)) .isFalse(); } @Test public void isAvailableHearingDevice_isConnectedHearingAid_returnTure() { when(mCachedBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true); Loading