Loading packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java +43 −1 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ import android.media.AudioManager; import android.net.Uri; import android.provider.DeviceConfig; import android.provider.MediaStore; import android.provider.Settings; import android.text.TextUtils; import android.util.Log; import android.util.Pair; Loading Loading @@ -808,7 +809,8 @@ public class BluetoothUtils { * <p>If CachedBluetoothDevice#getGroupId is invalid, fetch group id from * LeAudioProfile#getGroupId. */ public static int getGroupId(@NonNull CachedBluetoothDevice cachedDevice) { public static int getGroupId(@Nullable CachedBluetoothDevice cachedDevice) { if (cachedDevice == null) return BluetoothCsipSetCoordinator.GROUP_ID_INVALID; int groupId = cachedDevice.getGroupId(); String anonymizedAddress = cachedDevice.getDevice().getAnonymizedAddress(); if (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) { Loading @@ -824,4 +826,44 @@ public class BluetoothUtils { Log.d(TAG, "getGroupId return invalid id for device: " + anonymizedAddress); return BluetoothCsipSetCoordinator.GROUP_ID_INVALID; } /** Get primary device Uri in broadcast. */ @NonNull public static String getPrimaryGroupIdUriForBroadcast() { return "bluetooth_le_broadcast_fallback_active_group_id"; } /** Get primary device group id in broadcast. */ @WorkerThread public static int getPrimaryGroupIdForBroadcast(@NonNull Context context) { return Settings.Secure.getInt( context.getContentResolver(), getPrimaryGroupIdUriForBroadcast(), BluetoothCsipSetCoordinator.GROUP_ID_INVALID); } /** Get secondary {@link CachedBluetoothDevice} in broadcast. */ @Nullable @WorkerThread public static CachedBluetoothDevice getSecondaryDeviceForBroadcast( @NonNull Context context, @Nullable LocalBluetoothManager localBtManager) { if (localBtManager == null) return null; int primaryGroupId = getPrimaryGroupIdForBroadcast(context); if (primaryGroupId == BluetoothCsipSetCoordinator.GROUP_ID_INVALID) return null; LocalBluetoothLeBroadcastAssistant assistant = localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile(); CachedBluetoothDeviceManager deviceManager = localBtManager.getCachedDeviceManager(); List<BluetoothDevice> devices = assistant.getAllConnectedDevices(); for (BluetoothDevice device : devices) { CachedBluetoothDevice cachedDevice = deviceManager.findDevice(device); if (hasConnectedBroadcastSource(cachedDevice, localBtManager)) { int groupId = getGroupId(cachedDevice); if (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID && groupId != primaryGroupId) { return cachedDevice; } } } return null; } } packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothUtilsTest.java +78 −0 Original line number Diff line number Diff line Loading @@ -22,11 +22,13 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothCsipSetCoordinator; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothLeBroadcastReceiveState; import android.content.Context; Loading @@ -36,10 +38,13 @@ import android.graphics.drawable.Drawable; import android.media.AudioManager; import android.net.Uri; import android.platform.test.flag.junit.SetFlagsRule; import android.provider.Settings; import android.util.Pair; import com.android.settingslib.widget.AdaptiveIcon; import com.google.common.collect.ImmutableList; import org.junit.Before; import org.junit.Rule; import org.junit.Test; Loading Loading @@ -562,4 +567,77 @@ public class BluetoothUtilsTest { assertThat(BluetoothUtils.isAvailableHearingDevice(mCachedBluetoothDevice)).isEqualTo(true); } @Test public void getGroupId_getCsipProfileId() { when(mCachedBluetoothDevice.getGroupId()).thenReturn(1); assertThat(BluetoothUtils.getGroupId(mCachedBluetoothDevice)).isEqualTo(1); } @Test public void getGroupId_getLeAudioProfileId() { when(mCachedBluetoothDevice.getGroupId()) .thenReturn(BluetoothCsipSetCoordinator.GROUP_ID_INVALID); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); LeAudioProfile leAudio = mock(LeAudioProfile.class); when(leAudio.getGroupId(mBluetoothDevice)).thenReturn(1); when(mCachedBluetoothDevice.getProfiles()).thenReturn(ImmutableList.of(leAudio)); assertThat(BluetoothUtils.getGroupId(mCachedBluetoothDevice)).isEqualTo(1); } @Test public void getSecondaryDeviceForBroadcast_errorState_returnNull() { assertThat(BluetoothUtils.getSecondaryDeviceForBroadcast(mContext, mLocalBluetoothManager)) .isNull(); } @Test public void getSecondaryDeviceForBroadcast_noSecondary_returnNull() { Settings.Secure.putInt( mContext.getContentResolver(), BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), 1); CachedBluetoothDeviceManager deviceManager = mock(CachedBluetoothDeviceManager.class); when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(deviceManager); when(deviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); when(mCachedBluetoothDevice.getGroupId()).thenReturn(1); BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class); when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(ImmutableList.of(state)); when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mBluetoothDevice)); assertThat(BluetoothUtils.getSecondaryDeviceForBroadcast(mContext, mLocalBluetoothManager)) .isNull(); } @Test public void getSecondaryDeviceForBroadcast_returnCorrectDevice() { Settings.Secure.putInt( mContext.getContentResolver(), BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), 1); CachedBluetoothDeviceManager deviceManager = mock(CachedBluetoothDeviceManager.class); when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(deviceManager); CachedBluetoothDevice cachedBluetoothDevice = mock(CachedBluetoothDevice.class); BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class); when(cachedBluetoothDevice.getDevice()).thenReturn(bluetoothDevice); when(cachedBluetoothDevice.getGroupId()).thenReturn(1); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); when(mCachedBluetoothDevice.getGroupId()).thenReturn(2); when(deviceManager.findDevice(bluetoothDevice)).thenReturn(cachedBluetoothDevice); when(deviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice); BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class); List<Long> bisSyncState = new ArrayList<>(); bisSyncState.add(1L); when(state.getBisSyncState()).thenReturn(bisSyncState); when(mAssistant.getAllSources(any(BluetoothDevice.class))) .thenReturn(ImmutableList.of(state)); when(mAssistant.getAllConnectedDevices()) .thenReturn(ImmutableList.of(mBluetoothDevice, bluetoothDevice)); assertThat(BluetoothUtils.getSecondaryDeviceForBroadcast(mContext, mLocalBluetoothManager)) .isEqualTo(mCachedBluetoothDevice); } } Loading
packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java +43 −1 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ import android.media.AudioManager; import android.net.Uri; import android.provider.DeviceConfig; import android.provider.MediaStore; import android.provider.Settings; import android.text.TextUtils; import android.util.Log; import android.util.Pair; Loading Loading @@ -808,7 +809,8 @@ public class BluetoothUtils { * <p>If CachedBluetoothDevice#getGroupId is invalid, fetch group id from * LeAudioProfile#getGroupId. */ public static int getGroupId(@NonNull CachedBluetoothDevice cachedDevice) { public static int getGroupId(@Nullable CachedBluetoothDevice cachedDevice) { if (cachedDevice == null) return BluetoothCsipSetCoordinator.GROUP_ID_INVALID; int groupId = cachedDevice.getGroupId(); String anonymizedAddress = cachedDevice.getDevice().getAnonymizedAddress(); if (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) { Loading @@ -824,4 +826,44 @@ public class BluetoothUtils { Log.d(TAG, "getGroupId return invalid id for device: " + anonymizedAddress); return BluetoothCsipSetCoordinator.GROUP_ID_INVALID; } /** Get primary device Uri in broadcast. */ @NonNull public static String getPrimaryGroupIdUriForBroadcast() { return "bluetooth_le_broadcast_fallback_active_group_id"; } /** Get primary device group id in broadcast. */ @WorkerThread public static int getPrimaryGroupIdForBroadcast(@NonNull Context context) { return Settings.Secure.getInt( context.getContentResolver(), getPrimaryGroupIdUriForBroadcast(), BluetoothCsipSetCoordinator.GROUP_ID_INVALID); } /** Get secondary {@link CachedBluetoothDevice} in broadcast. */ @Nullable @WorkerThread public static CachedBluetoothDevice getSecondaryDeviceForBroadcast( @NonNull Context context, @Nullable LocalBluetoothManager localBtManager) { if (localBtManager == null) return null; int primaryGroupId = getPrimaryGroupIdForBroadcast(context); if (primaryGroupId == BluetoothCsipSetCoordinator.GROUP_ID_INVALID) return null; LocalBluetoothLeBroadcastAssistant assistant = localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile(); CachedBluetoothDeviceManager deviceManager = localBtManager.getCachedDeviceManager(); List<BluetoothDevice> devices = assistant.getAllConnectedDevices(); for (BluetoothDevice device : devices) { CachedBluetoothDevice cachedDevice = deviceManager.findDevice(device); if (hasConnectedBroadcastSource(cachedDevice, localBtManager)) { int groupId = getGroupId(cachedDevice); if (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID && groupId != primaryGroupId) { return cachedDevice; } } } return null; } }
packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothUtilsTest.java +78 −0 Original line number Diff line number Diff line Loading @@ -22,11 +22,13 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothCsipSetCoordinator; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothLeBroadcastReceiveState; import android.content.Context; Loading @@ -36,10 +38,13 @@ import android.graphics.drawable.Drawable; import android.media.AudioManager; import android.net.Uri; import android.platform.test.flag.junit.SetFlagsRule; import android.provider.Settings; import android.util.Pair; import com.android.settingslib.widget.AdaptiveIcon; import com.google.common.collect.ImmutableList; import org.junit.Before; import org.junit.Rule; import org.junit.Test; Loading Loading @@ -562,4 +567,77 @@ public class BluetoothUtilsTest { assertThat(BluetoothUtils.isAvailableHearingDevice(mCachedBluetoothDevice)).isEqualTo(true); } @Test public void getGroupId_getCsipProfileId() { when(mCachedBluetoothDevice.getGroupId()).thenReturn(1); assertThat(BluetoothUtils.getGroupId(mCachedBluetoothDevice)).isEqualTo(1); } @Test public void getGroupId_getLeAudioProfileId() { when(mCachedBluetoothDevice.getGroupId()) .thenReturn(BluetoothCsipSetCoordinator.GROUP_ID_INVALID); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); LeAudioProfile leAudio = mock(LeAudioProfile.class); when(leAudio.getGroupId(mBluetoothDevice)).thenReturn(1); when(mCachedBluetoothDevice.getProfiles()).thenReturn(ImmutableList.of(leAudio)); assertThat(BluetoothUtils.getGroupId(mCachedBluetoothDevice)).isEqualTo(1); } @Test public void getSecondaryDeviceForBroadcast_errorState_returnNull() { assertThat(BluetoothUtils.getSecondaryDeviceForBroadcast(mContext, mLocalBluetoothManager)) .isNull(); } @Test public void getSecondaryDeviceForBroadcast_noSecondary_returnNull() { Settings.Secure.putInt( mContext.getContentResolver(), BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), 1); CachedBluetoothDeviceManager deviceManager = mock(CachedBluetoothDeviceManager.class); when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(deviceManager); when(deviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); when(mCachedBluetoothDevice.getGroupId()).thenReturn(1); BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class); when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(ImmutableList.of(state)); when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mBluetoothDevice)); assertThat(BluetoothUtils.getSecondaryDeviceForBroadcast(mContext, mLocalBluetoothManager)) .isNull(); } @Test public void getSecondaryDeviceForBroadcast_returnCorrectDevice() { Settings.Secure.putInt( mContext.getContentResolver(), BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), 1); CachedBluetoothDeviceManager deviceManager = mock(CachedBluetoothDeviceManager.class); when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(deviceManager); CachedBluetoothDevice cachedBluetoothDevice = mock(CachedBluetoothDevice.class); BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class); when(cachedBluetoothDevice.getDevice()).thenReturn(bluetoothDevice); when(cachedBluetoothDevice.getGroupId()).thenReturn(1); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); when(mCachedBluetoothDevice.getGroupId()).thenReturn(2); when(deviceManager.findDevice(bluetoothDevice)).thenReturn(cachedBluetoothDevice); when(deviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice); BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class); List<Long> bisSyncState = new ArrayList<>(); bisSyncState.add(1L); when(state.getBisSyncState()).thenReturn(bisSyncState); when(mAssistant.getAllSources(any(BluetoothDevice.class))) .thenReturn(ImmutableList.of(state)); when(mAssistant.getAllConnectedDevices()) .thenReturn(ImmutableList.of(mBluetoothDevice, bluetoothDevice)); assertThat(BluetoothUtils.getSecondaryDeviceForBroadcast(mContext, mLocalBluetoothManager)) .isEqualTo(mCachedBluetoothDevice); } }