Merge "Initialize hearing aid info if the device matches hearing aid scan filters" into udc-qpr-dev

This commit is contained in:
Angela Wang
2023-07-21 02:38:08 +00:00
committed by Android (Google) Code Review
4 changed files with 70 additions and 10 deletions

View File

@@ -356,11 +356,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
connectDevice();
}
public HearingAidInfo getHearingAidInfo() {
return mHearingAidInfo;
}
public void setHearingAidInfo(HearingAidInfo hearingAidInfo) {
void setHearingAidInfo(HearingAidInfo hearingAidInfo) {
mHearingAidInfo = hearingAidInfo;
}

View File

@@ -20,6 +20,7 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.ScanFilter;
import android.content.Context;
import android.util.Log;
@@ -114,10 +115,21 @@ public class CachedBluetoothDeviceManager {
/**
* Create and return a new {@link CachedBluetoothDevice}. This assumes
* that {@link #findDevice} has already been called and returned null.
* @param device the address of the new Bluetooth device
* @param device the new Bluetooth device
* @return the newly created CachedBluetoothDevice object
*/
public CachedBluetoothDevice addDevice(BluetoothDevice device) {
return addDevice(device, /*leScanFilters=*/null);
}
/**
* Create and return a new {@link CachedBluetoothDevice}. This assumes
* that {@link #findDevice} has already been called and returned null.
* @param device the new Bluetooth device
* @param leScanFilters the BLE scan filters which the device matched
* @return the newly created CachedBluetoothDevice object
*/
public CachedBluetoothDevice addDevice(BluetoothDevice device, List<ScanFilter> leScanFilters) {
CachedBluetoothDevice newDevice;
final LocalBluetoothProfileManager profileManager = mBtManager.getProfileManager();
synchronized (this) {
@@ -125,7 +137,7 @@ public class CachedBluetoothDeviceManager {
if (newDevice == null) {
newDevice = new CachedBluetoothDevice(mContext, profileManager, device);
mCsipDeviceManager.initCsipDeviceIfNeeded(newDevice);
mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(newDevice);
mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(newDevice, leScanFilters);
if (!mCsipDeviceManager.setMemberDeviceIfNeeded(newDevice)
&& !mHearingAidDeviceManager.setSubDeviceIfNeeded(newDevice)) {
mCachedDevices.add(newDevice);

View File

@@ -18,10 +18,13 @@ package com.android.settingslib.bluetooth;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHearingAid;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUuid;
import android.bluetooth.le.ScanFilter;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioDeviceAttributes;
import android.media.audiopolicy.AudioProductStrategy;
import android.os.ParcelUuid;
import android.provider.Settings;
import android.util.Log;
@@ -59,7 +62,8 @@ public class HearingAidDeviceManager {
mRoutingHelper = routingHelper;
}
void initHearingAidDeviceIfNeeded(CachedBluetoothDevice newDevice) {
void initHearingAidDeviceIfNeeded(CachedBluetoothDevice newDevice,
List<ScanFilter> leScanFilters) {
long hiSyncId = getHiSyncId(newDevice.getDevice());
if (isValidHiSyncId(hiSyncId)) {
// Once hiSyncId is valid, assign hearing aid info
@@ -68,6 +72,21 @@ public class HearingAidDeviceManager {
.setAshaDeviceMode(getDeviceMode(newDevice.getDevice()))
.setHiSyncId(hiSyncId);
newDevice.setHearingAidInfo(infoBuilder.build());
} else if (leScanFilters != null && !newDevice.isHearingAidDevice()) {
// If the device is added with hearing aid scan filter during pairing, set an empty
// hearing aid info to indicate it's a hearing aid device. The info will be updated
// when corresponding profiles connected.
for (ScanFilter leScanFilter: leScanFilters) {
final ParcelUuid serviceUuid = leScanFilter.getServiceUuid();
final ParcelUuid serviceDataUuid = leScanFilter.getServiceDataUuid();
if (BluetoothUuid.HEARING_AID.equals(serviceUuid)
|| BluetoothUuid.HAS.equals(serviceUuid)
|| BluetoothUuid.HEARING_AID.equals(serviceDataUuid)
|| BluetoothUuid.HAS.equals(serviceDataUuid)) {
newDevice.setHearingAidInfo(new HearingAidInfo.Builder().build());
break;
}
}
}
}

View File

@@ -33,6 +33,8 @@ import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHearingAid;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUuid;
import android.bluetooth.le.ScanFilter;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioDeviceAttributes;
@@ -147,7 +149,7 @@ public class HearingAidDeviceManagerTest {
HearingAidProfile.DeviceSide.SIDE_RIGHT);
assertThat(mCachedDevice1.getHiSyncId()).isNotEqualTo(HISYNCID1);
mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1);
mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1, null);
assertThat(mCachedDevice1.getHiSyncId()).isEqualTo(HISYNCID1);
assertThat(mCachedDevice1.getDeviceMode()).isEqualTo(
@@ -164,11 +166,42 @@ public class HearingAidDeviceManagerTest {
when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(
BluetoothHearingAid.HI_SYNC_ID_INVALID);
mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1);
mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1, null);
verify(mCachedDevice1, never()).setHearingAidInfo(any(HearingAidInfo.class));
}
/**
* Test initHearingAidDeviceIfNeeded, an invalid HiSyncId and hearing aid scan filter, set an
* empty hearing aid info on the device.
*/
@Test
public void initHearingAidDeviceIfNeeded_hearingAidScanFilter_setHearingAidInfo() {
when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(
BluetoothHearingAid.HI_SYNC_ID_INVALID);
final ScanFilter scanFilter = new ScanFilter.Builder()
.setServiceData(BluetoothUuid.HEARING_AID, new byte[]{0}).build();
mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1, List.of(scanFilter));
assertThat(mCachedDevice1.isHearingAidDevice()).isTrue();
}
/**
* Test initHearingAidDeviceIfNeeded, an invalid HiSyncId and random scan filter, not to set
* hearing aid info on the device.
*/
@Test
public void initHearingAidDeviceIfNeeded_randomScanFilter_notToSetHearingAidInfo() {
when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(
BluetoothHearingAid.HI_SYNC_ID_INVALID);
final ScanFilter scanFilter = new ScanFilter.Builder().build();
mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1, List.of(scanFilter));
assertThat(mCachedDevice1.isHearingAidDevice()).isFalse();
}
/**
* Test setSubDeviceIfNeeded, a device with same HiSyncId will be set as sub device
*/