summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Haijie Hong <hahong@google.com> 2024-10-30 09:17:29 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2024-10-30 09:17:29 +0000
commitd51f2c6b98c27fb5fca89e723bf87621d5e4e816 (patch)
treef5247b244f7881f4a79ca6d409509c56cdc72e52
parent367acd637eae354ffb2a2817ebf7b2f0d6f82b99 (diff)
parente2c0f568b5f90fea0d4d52ab3f60501472b53f64 (diff)
Merge "Fix null pointer exception when set preferred transport" into main
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java27
-rw-r--r--packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java5
2 files changed, 18 insertions, 14 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
index 8845d2ee5369..8641f7036c50 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
@@ -322,18 +322,27 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
}
private void updatePreferredTransport() {
- if (mProfiles.stream().noneMatch(p -> p instanceof LeAudioProfile)
- || mProfiles.stream().noneMatch(p -> p instanceof HidProfile)) {
+ LeAudioProfile leAudioProfile =
+ (LeAudioProfile)
+ mProfiles.stream()
+ .filter(p -> p instanceof LeAudioProfile)
+ .findFirst()
+ .orElse(null);
+ HidProfile hidProfile =
+ (HidProfile)
+ mProfiles.stream()
+ .filter(p -> p instanceof HidProfile)
+ .findFirst()
+ .orElse(null);
+ if (leAudioProfile == null || hidProfile == null) {
return;
}
// Both LeAudioProfile and HidProfile are connectable.
- if (!mProfileManager
- .getHidProfile()
- .setPreferredTransport(
- mDevice,
- mProfileManager.getLeAudioProfile().isEnabled(mDevice)
- ? BluetoothDevice.TRANSPORT_LE
- : BluetoothDevice.TRANSPORT_BREDR)) {
+ if (!hidProfile.setPreferredTransport(
+ mDevice,
+ leAudioProfile.isEnabled(mDevice)
+ ? BluetoothDevice.TRANSPORT_LE
+ : BluetoothDevice.TRANSPORT_BREDR)) {
Log.w(TAG, "Fail to set preferred transport");
}
}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java
index 70cb2ef016ec..30f8a798b674 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java
@@ -1952,9 +1952,6 @@ public class CachedBluetoothDeviceTest {
@Test
public void leAudioHidDevice_leAudioEnabled_setPreferredTransportToLE() {
-
- when(mProfileManager.getHidProfile()).thenReturn(mHidProfile);
- when(mProfileManager.getLeAudioProfile()).thenReturn(mLeAudioProfile);
when(mLeAudioProfile.isEnabled(mDevice)).thenReturn(true);
updateProfileStatus(mHidProfile, BluetoothProfile.STATE_CONNECTED);
@@ -1965,8 +1962,6 @@ public class CachedBluetoothDeviceTest {
@Test
public void leAudioHidDevice_leAudioDisabled_setPreferredTransportToBredr() {
- when(mProfileManager.getHidProfile()).thenReturn(mHidProfile);
- when(mProfileManager.getLeAudioProfile()).thenReturn(mLeAudioProfile);
when(mLeAudioProfile.isEnabled(mDevice)).thenReturn(false);
updateProfileStatus(mLeAudioProfile, BluetoothProfile.STATE_CONNECTED);