Unifies constants accross hearing aid related profiles (1/2)

Hearing aid constants defined differently accross different hearing aid
related profiles. For hearing aid device mode, HearingAidProfile and
HapClientProfile have different value for mode definition and there is also a new BANDED hearing aid type in HapClientProfile spec. For hearing aid device side, HearingAidProfile has only 2 kinds of side which is left and right whereas BLE hearing aid can retrieve 27 different kinds of audio location.

We therefore introduce a new class HearingAidInfo for mapping these
different constants accross these profiles into a single unified set of
constants.

Bug: 253192350
Test: make RunSettingsLibRoboTests ROBOTEST_FILTER=CachedBluetoothDeviceManagerTest
Test: make RunSettingsLibRoboTests ROBOTEST_FILTER=CachedBluetoothDeviceTest
Test: make RunSettingsLibRoboTests ROBOTEST_FILTER=HearingAidDeviceManagerTest
Change-Id: I769e94c13b56f457b5ebee38b1ed168e3981ad34
This commit is contained in:
Angela Wang
2022-11-14 16:01:29 +00:00
parent c8b05529fe
commit 19ef1c3a10
7 changed files with 373 additions and 80 deletions

View File

@@ -77,9 +77,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
private final LocalBluetoothProfileManager mProfileManager;
private final Object mProfileLock = new Object();
BluetoothDevice mDevice;
private int mDeviceSide;
private int mDeviceMode;
private long mHiSyncId;
private HearingAidInfo mHearingAidInfo;
private int mGroupId;
// Need this since there is no method for getting RSSI
@@ -160,9 +158,6 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
mProfileManager = profileManager;
mDevice = device;
fillData();
mHiSyncId = BluetoothHearingAid.HI_SYNC_ID_INVALID;
mDeviceSide = HearingAidProfile.DeviceSide.SIDE_INVALID;
mDeviceMode = HearingAidProfile.DeviceMode.MODE_INVALID;
mGroupId = BluetoothCsipSetCoordinator.GROUP_ID_INVALID;
initDrawableCache();
mUnpairing = false;
@@ -341,28 +336,34 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
connectDevice();
}
public int getDeviceSide() {
return mDeviceSide;
public HearingAidInfo getHearingAidInfo() {
return mHearingAidInfo;
}
public void setDeviceSide(int side) {
mDeviceSide = side;
public void setHearingAidInfo(HearingAidInfo hearingAidInfo) {
mHearingAidInfo = hearingAidInfo;
}
/**
* @return {@code true} if {@code cachedBluetoothDevice} is hearing aid device
*/
public boolean isHearingAidDevice() {
return mHearingAidInfo != null;
}
public int getDeviceSide() {
return mHearingAidInfo != null
? mHearingAidInfo.getSide() : HearingAidInfo.DeviceSide.SIDE_INVALID;
}
public int getDeviceMode() {
return mDeviceMode;
}
public void setDeviceMode(int mode) {
mDeviceMode = mode;
return mHearingAidInfo != null
? mHearingAidInfo.getMode() : HearingAidInfo.DeviceMode.MODE_INVALID;
}
public long getHiSyncId() {
return mHiSyncId;
}
public void setHiSyncId(long id) {
mHiSyncId = id;
return mHearingAidInfo != null
? mHearingAidInfo.getHiSyncId() : BluetoothHearingAid.HI_SYNC_ID_INVALID;
}
/**
@@ -1169,9 +1170,10 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
if (subDevice != null && subDevice.isConnected()) {
stringRes = R.string.bluetooth_hearing_aid_left_and_right_active;
} else {
if (mDeviceSide == HearingAidProfile.DeviceSide.SIDE_LEFT) {
int deviceSide = getDeviceSide();
if (deviceSide == HearingAidInfo.DeviceSide.SIDE_LEFT) {
stringRes = R.string.bluetooth_hearing_aid_left_active;
} else if (mDeviceSide == HearingAidProfile.DeviceSide.SIDE_RIGHT) {
} else if (deviceSide == HearingAidInfo.DeviceSide.SIDE_RIGHT) {
stringRes = R.string.bluetooth_hearing_aid_right_active;
} else {
stringRes = R.string.bluetooth_active_no_battery_level;
@@ -1397,7 +1399,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
* The device may be an ASHA hearing aid that supports {@link HearingAidProfile} or a LeAudio
* hearing aid that supports {@link HapClientProfile} and {@link LeAudioProfile}.
*/
public boolean isHearingAidDevice() {
public boolean isConnectedHearingAidDevice() {
return isConnectedAshaHearingAidDevice() || isConnectedLeAudioHearingAidDevice();
}
@@ -1429,17 +1431,17 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
BluetoothDevice tmpDevice = mDevice;
final short tmpRssi = mRssi;
final boolean tmpJustDiscovered = mJustDiscovered;
final int tmpDeviceSide = mDeviceSide;
final HearingAidInfo tmpHearingAidInfo = mHearingAidInfo;
// Set main device from sub device
mDevice = mSubDevice.mDevice;
mRssi = mSubDevice.mRssi;
mJustDiscovered = mSubDevice.mJustDiscovered;
mDeviceSide = mSubDevice.mDeviceSide;
mHearingAidInfo = mSubDevice.mHearingAidInfo;
// Set sub device from backup
mSubDevice.mDevice = tmpDevice;
mSubDevice.mRssi = tmpRssi;
mSubDevice.mJustDiscovered = tmpJustDiscovered;
mSubDevice.mDeviceSide = tmpDeviceSide;
mSubDevice.mHearingAidInfo = tmpHearingAidInfo;
fetchActiveDevices();
}

View File

@@ -27,7 +27,7 @@ import java.util.List;
import java.util.Set;
/**
* HearingAidDeviceManager manages the set of remote HearingAid Bluetooth devices.
* HearingAidDeviceManager manages the set of remote HearingAid(ASHA) Bluetooth devices.
*/
public class HearingAidDeviceManager {
private static final String TAG = "HearingAidDeviceManager";
@@ -44,12 +44,12 @@ public class HearingAidDeviceManager {
void initHearingAidDeviceIfNeeded(CachedBluetoothDevice newDevice) {
long hiSyncId = getHiSyncId(newDevice.getDevice());
if (isValidHiSyncId(hiSyncId)) {
// Once hiSyncId is valid, assign hiSyncId
newDevice.setHiSyncId(hiSyncId);
final int side = getDeviceSide(newDevice.getDevice());
final int mode = getDeviceMode(newDevice.getDevice());
newDevice.setDeviceSide(side);
newDevice.setDeviceMode(mode);
// Once hiSyncId is valid, assign hearing aid info
final HearingAidInfo.Builder infoBuilder = new HearingAidInfo.Builder()
.setAshaDeviceSide(getDeviceSide(newDevice.getDevice()))
.setAshaDeviceMode(getDeviceMode(newDevice.getDevice()))
.setHiSyncId(hiSyncId);
newDevice.setHearingAidInfo(infoBuilder.build());
}
}
@@ -123,12 +123,14 @@ public class HearingAidDeviceManager {
final long newHiSyncId = getHiSyncId(cachedDevice.getDevice());
// Do nothing if there is no HiSyncId on Bluetooth device
if (isValidHiSyncId(newHiSyncId)) {
cachedDevice.setHiSyncId(newHiSyncId);
// Once hiSyncId is valid, assign hearing aid info
final HearingAidInfo.Builder infoBuilder = new HearingAidInfo.Builder()
.setAshaDeviceSide(getDeviceSide(cachedDevice.getDevice()))
.setAshaDeviceMode(getDeviceMode(cachedDevice.getDevice()))
.setHiSyncId(newHiSyncId);
cachedDevice.setHearingAidInfo(infoBuilder.build());
newSyncIdSet.add(newHiSyncId);
final int side = getDeviceSide(cachedDevice.getDevice());
final int mode = getDeviceMode(cachedDevice.getDevice());
cachedDevice.setDeviceSide(side);
cachedDevice.setDeviceMode(mode);
}
}
}

View File

@@ -0,0 +1,263 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.bluetooth;
import android.annotation.IntDef;
import android.bluetooth.BluetoothHearingAid;
import android.bluetooth.BluetoothLeAudio;
import android.util.SparseIntArray;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Objects;
/** Hearing aids information and constants that shared within hearing aids related profiles */
public class HearingAidInfo {
@Retention(RetentionPolicy.SOURCE)
@IntDef({
DeviceSide.SIDE_INVALID,
DeviceSide.SIDE_LEFT,
DeviceSide.SIDE_RIGHT,
DeviceSide.SIDE_LEFT_AND_RIGHT,
})
/** Side definition for hearing aids. */
public @interface DeviceSide {
int SIDE_INVALID = -1;
int SIDE_LEFT = 0;
int SIDE_RIGHT = 1;
int SIDE_LEFT_AND_RIGHT = 2;
}
@Retention(java.lang.annotation.RetentionPolicy.SOURCE)
@IntDef({
DeviceMode.MODE_INVALID,
DeviceMode.MODE_MONAURAL,
DeviceMode.MODE_BINAURAL,
DeviceMode.MODE_BANDED,
})
/** Mode definition for hearing aids. */
public @interface DeviceMode {
int MODE_INVALID = -1;
int MODE_MONAURAL = 0;
int MODE_BINAURAL = 1;
int MODE_BANDED = 2;
}
private final int mSide;
private final int mMode;
private final long mHiSyncId;
private HearingAidInfo(int side, int mode, long hiSyncId) {
mSide = side;
mMode = mode;
mHiSyncId = hiSyncId;
}
@DeviceSide
public int getSide() {
return mSide;
}
@DeviceMode
public int getMode() {
return mMode;
}
public long getHiSyncId() {
return mHiSyncId;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof HearingAidInfo)) {
return false;
}
HearingAidInfo that = (HearingAidInfo) o;
return mSide == that.mSide && mMode == that.mMode && mHiSyncId == that.mHiSyncId;
}
@Override
public int hashCode() {
return Objects.hash(mSide, mMode, mHiSyncId);
}
@Override
public String toString() {
return "HearingAidInfo{"
+ "mSide=" + mSide
+ ", mMode=" + mMode
+ ", mHiSyncId=" + mHiSyncId
+ '}';
}
@DeviceSide
private static int convertAshaDeviceSideToInternalSide(int ashaDeviceSide) {
return ASHA_DEVICE_SIDE_TO_INTERNAL_SIDE_MAPPING.get(
ashaDeviceSide, DeviceSide.SIDE_INVALID);
}
@DeviceMode
private static int convertAshaDeviceModeToInternalMode(int ashaDeviceMode) {
return ASHA_DEVICE_MODE_TO_INTERNAL_MODE_MAPPING.get(
ashaDeviceMode, DeviceMode.MODE_INVALID);
}
@DeviceSide
private static int convertLeAudioLocationToInternalSide(int leAudioLocation) {
boolean isLeft = (leAudioLocation & LE_AUDIO_LOCATION_LEFT) != 0;
boolean isRight = (leAudioLocation & LE_AUDIO_LOCATION_RIGHT) != 0;
if (isLeft && isRight) {
return DeviceSide.SIDE_LEFT_AND_RIGHT;
} else if (isLeft) {
return DeviceSide.SIDE_LEFT;
} else if (isRight) {
return DeviceSide.SIDE_RIGHT;
}
return DeviceSide.SIDE_INVALID;
}
@DeviceMode
private static int convertHapDeviceTypeToInternalMode(int hapDeviceType) {
return HAP_DEVICE_TYPE_TO_INTERNAL_MODE_MAPPING.get(hapDeviceType, DeviceMode.MODE_INVALID);
}
/** Builder class for constructing {@link HearingAidInfo} objects. */
public static final class Builder {
private int mSide = DeviceSide.SIDE_INVALID;
private int mMode = DeviceMode.MODE_INVALID;
private long mHiSyncId = BluetoothHearingAid.HI_SYNC_ID_INVALID;
/**
* Configure the hearing device mode.
* @param ashaDeviceMode one of the hearing aid device modes defined in HearingAidProfile
* {@link HearingAidProfile.DeviceMode}
*/
public Builder setAshaDeviceMode(int ashaDeviceMode) {
mMode = convertAshaDeviceModeToInternalMode(ashaDeviceMode);
return this;
}
/**
* Configure the hearing device mode.
* @param hapDeviceType one of the hearing aid device types defined in HapClientProfile
* {@link HapClientProfile.HearingAidType}
*/
public Builder setHapDeviceType(int hapDeviceType) {
mMode = convertHapDeviceTypeToInternalMode(hapDeviceType);
return this;
}
/**
* Configure the hearing device side.
* @param ashaDeviceSide one of the hearing aid device sides defined in HearingAidProfile
* {@link HearingAidProfile.DeviceSide}
*/
public Builder setAshaDeviceSide(int ashaDeviceSide) {
mSide = convertAshaDeviceSideToInternalSide(ashaDeviceSide);
return this;
}
/**
* Configure the hearing device side.
* @param leAudioLocation one of the audio location defined in BluetoothLeAudio
* {@link BluetoothLeAudio.AudioLocation}
*/
public Builder setLeAudioLocation(int leAudioLocation) {
mSide = convertLeAudioLocationToInternalSide(leAudioLocation);
return this;
}
/**
* Configure the hearing aid hiSyncId.
* @param hiSyncId the ASHA hearing aid id
*/
public Builder setHiSyncId(long hiSyncId) {
mHiSyncId = hiSyncId;
return this;
}
/** Build the configured {@link HearingAidInfo} */
public HearingAidInfo build() {
return new HearingAidInfo(mSide, mMode, mHiSyncId);
}
}
private static final int LE_AUDIO_LOCATION_LEFT =
BluetoothLeAudio.AUDIO_LOCATION_FRONT_LEFT
| BluetoothLeAudio.AUDIO_LOCATION_BACK_LEFT
| BluetoothLeAudio.AUDIO_LOCATION_FRONT_LEFT_OF_CENTER
| BluetoothLeAudio.AUDIO_LOCATION_SIDE_LEFT
| BluetoothLeAudio.AUDIO_LOCATION_TOP_FRONT_LEFT
| BluetoothLeAudio.AUDIO_LOCATION_TOP_BACK_LEFT
| BluetoothLeAudio.AUDIO_LOCATION_TOP_SIDE_LEFT
| BluetoothLeAudio.AUDIO_LOCATION_BOTTOM_FRONT_LEFT
| BluetoothLeAudio.AUDIO_LOCATION_FRONT_LEFT_WIDE
| BluetoothLeAudio.AUDIO_LOCATION_LEFT_SURROUND;
private static final int LE_AUDIO_LOCATION_RIGHT =
BluetoothLeAudio.AUDIO_LOCATION_FRONT_RIGHT
| BluetoothLeAudio.AUDIO_LOCATION_BACK_RIGHT
| BluetoothLeAudio.AUDIO_LOCATION_FRONT_RIGHT_OF_CENTER
| BluetoothLeAudio.AUDIO_LOCATION_SIDE_RIGHT
| BluetoothLeAudio.AUDIO_LOCATION_TOP_FRONT_RIGHT
| BluetoothLeAudio.AUDIO_LOCATION_TOP_BACK_RIGHT
| BluetoothLeAudio.AUDIO_LOCATION_TOP_SIDE_RIGHT
| BluetoothLeAudio.AUDIO_LOCATION_BOTTOM_FRONT_RIGHT
| BluetoothLeAudio.AUDIO_LOCATION_FRONT_RIGHT_WIDE
| BluetoothLeAudio.AUDIO_LOCATION_RIGHT_SURROUND;
private static final SparseIntArray ASHA_DEVICE_SIDE_TO_INTERNAL_SIDE_MAPPING;
private static final SparseIntArray ASHA_DEVICE_MODE_TO_INTERNAL_MODE_MAPPING;
private static final SparseIntArray HAP_DEVICE_TYPE_TO_INTERNAL_MODE_MAPPING;
static {
ASHA_DEVICE_SIDE_TO_INTERNAL_SIDE_MAPPING = new SparseIntArray();
ASHA_DEVICE_SIDE_TO_INTERNAL_SIDE_MAPPING.put(
HearingAidProfile.DeviceSide.SIDE_INVALID, DeviceSide.SIDE_INVALID);
ASHA_DEVICE_SIDE_TO_INTERNAL_SIDE_MAPPING.put(
HearingAidProfile.DeviceSide.SIDE_LEFT, DeviceSide.SIDE_LEFT);
ASHA_DEVICE_SIDE_TO_INTERNAL_SIDE_MAPPING.put(
HearingAidProfile.DeviceSide.SIDE_RIGHT, DeviceSide.SIDE_RIGHT);
ASHA_DEVICE_MODE_TO_INTERNAL_MODE_MAPPING = new SparseIntArray();
ASHA_DEVICE_MODE_TO_INTERNAL_MODE_MAPPING.put(
HearingAidProfile.DeviceMode.MODE_INVALID, DeviceMode.MODE_INVALID);
ASHA_DEVICE_MODE_TO_INTERNAL_MODE_MAPPING.put(
HearingAidProfile.DeviceMode.MODE_MONAURAL, DeviceMode.MODE_MONAURAL);
ASHA_DEVICE_MODE_TO_INTERNAL_MODE_MAPPING.put(
HearingAidProfile.DeviceMode.MODE_BINAURAL, DeviceMode.MODE_BINAURAL);
HAP_DEVICE_TYPE_TO_INTERNAL_MODE_MAPPING = new SparseIntArray();
HAP_DEVICE_TYPE_TO_INTERNAL_MODE_MAPPING.put(
HapClientProfile.HearingAidType.TYPE_INVALID, DeviceMode.MODE_INVALID);
HAP_DEVICE_TYPE_TO_INTERNAL_MODE_MAPPING.put(
HapClientProfile.HearingAidType.TYPE_BINAURAL, DeviceMode.MODE_BINAURAL);
HAP_DEVICE_TYPE_TO_INTERNAL_MODE_MAPPING.put(
HapClientProfile.HearingAidType.TYPE_MONAURAL, DeviceMode.MODE_MONAURAL);
HAP_DEVICE_TYPE_TO_INTERNAL_MODE_MAPPING.put(
HapClientProfile.HearingAidType.TYPE_BANDED, DeviceMode.MODE_BANDED);
HAP_DEVICE_TYPE_TO_INTERNAL_MODE_MAPPING.put(
HapClientProfile.HearingAidType.TYPE_RFU, DeviceMode.MODE_INVALID);
}
}

View File

@@ -345,19 +345,20 @@ public class LocalBluetoothProfileManager {
Log.i(TAG, "Failed to connect " + mProfile + " device");
}
if (getHearingAidProfile() != null &&
mProfile instanceof HearingAidProfile &&
(newState == BluetoothProfile.STATE_CONNECTED)) {
final int side = getHearingAidProfile().getDeviceSide(cachedDevice.getDevice());
final int mode = getHearingAidProfile().getDeviceMode(cachedDevice.getDevice());
cachedDevice.setDeviceSide(side);
cachedDevice.setDeviceMode(mode);
if (getHearingAidProfile() != null
&& mProfile instanceof HearingAidProfile
&& (newState == BluetoothProfile.STATE_CONNECTED)) {
// Check if the HiSyncID has being initialized
if (cachedDevice.getHiSyncId() == BluetoothHearingAid.HI_SYNC_ID_INVALID) {
long newHiSyncId = getHearingAidProfile().getHiSyncId(cachedDevice.getDevice());
if (newHiSyncId != BluetoothHearingAid.HI_SYNC_ID_INVALID) {
cachedDevice.setHiSyncId(newHiSyncId);
final BluetoothDevice device = cachedDevice.getDevice();
final HearingAidInfo.Builder infoBuilder = new HearingAidInfo.Builder()
.setAshaDeviceSide(getHearingAidProfile().getDeviceSide(device))
.setAshaDeviceMode(getHearingAidProfile().getDeviceMode(device))
.setHiSyncId(newHiSyncId);
cachedDevice.setHearingAidInfo(infoBuilder.build());
}
}

View File

@@ -327,8 +327,10 @@ public class CachedBluetoothDeviceManagerTest {
when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
cachedDevice1.setHiSyncId(HISYNCID1);
cachedDevice2.setHiSyncId(HISYNCID1);
cachedDevice1.setHearingAidInfo(
new HearingAidInfo.Builder().setHiSyncId(HISYNCID1).build());
cachedDevice2.setHearingAidInfo(
new HearingAidInfo.Builder().setHiSyncId(HISYNCID1).build());
cachedDevice1.setSubDevice(cachedDevice2);
// Call onDeviceUnpaired for the one in mCachedDevices.

View File

@@ -358,7 +358,7 @@ public class CachedBluetoothDeviceTest {
assertThat(mCachedDevice.getConnectionSummary()).isNull();
// Set device as Active for Hearing Aid and test connection state summary
mCachedDevice.setDeviceSide(HearingAidProfile.DeviceSide.SIDE_LEFT);
mCachedDevice.setHearingAidInfo(getLeftAshaHearingAidInfo());
mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID);
assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active, left only");
@@ -403,7 +403,7 @@ public class CachedBluetoothDeviceTest {
// 1. Profile: {HEARING_AID, Connected, Active, Right ear}
// 2. Audio Manager: In Call
updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
mCachedDevice.setDeviceSide(HearingAidProfile.DeviceSide.SIDE_RIGHT);
mCachedDevice.setHearingAidInfo(getRightAshaHearingAidInfo());
mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID);
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
@@ -417,9 +417,9 @@ public class CachedBluetoothDeviceTest {
// Arrange:
// 1. Profile: {HEARING_AID, Connected, Active, Both ear}
// 2. Audio Manager: In Call
mCachedDevice.setDeviceSide(HearingAidProfile.DeviceSide.SIDE_RIGHT);
mCachedDevice.setHearingAidInfo(getRightAshaHearingAidInfo());
updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
mSubCachedDevice.setDeviceSide(HearingAidProfile.DeviceSide.SIDE_LEFT);
mSubCachedDevice.setHearingAidInfo(getLeftAshaHearingAidInfo());
updateSubDeviceProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
mCachedDevice.setSubDevice(mSubCachedDevice);
mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID);
@@ -969,10 +969,10 @@ public class CachedBluetoothDeviceTest {
mCachedDevice.mRssi = RSSI_1;
mCachedDevice.mJustDiscovered = JUSTDISCOVERED_1;
mCachedDevice.setDeviceSide(HearingAidProfile.DeviceSide.SIDE_LEFT);
mCachedDevice.setHearingAidInfo(getLeftAshaHearingAidInfo());
mSubCachedDevice.mRssi = RSSI_2;
mSubCachedDevice.mJustDiscovered = JUSTDISCOVERED_2;
mSubCachedDevice.setDeviceSide(HearingAidProfile.DeviceSide.SIDE_RIGHT);
mSubCachedDevice.setHearingAidInfo(getRightAshaHearingAidInfo());
mCachedDevice.setSubDevice(mSubCachedDevice);
mCachedDevice.switchSubDeviceContent();
@@ -980,13 +980,11 @@ public class CachedBluetoothDeviceTest {
assertThat(mCachedDevice.mRssi).isEqualTo(RSSI_2);
assertThat(mCachedDevice.mJustDiscovered).isEqualTo(JUSTDISCOVERED_2);
assertThat(mCachedDevice.mDevice).isEqualTo(mSubDevice);
assertThat(mCachedDevice.getDeviceSide()).isEqualTo(
HearingAidProfile.DeviceSide.SIDE_RIGHT);
assertThat(mCachedDevice.getDeviceSide()).isEqualTo(HearingAidInfo.DeviceSide.SIDE_RIGHT);
assertThat(mSubCachedDevice.mRssi).isEqualTo(RSSI_1);
assertThat(mSubCachedDevice.mJustDiscovered).isEqualTo(JUSTDISCOVERED_1);
assertThat(mSubCachedDevice.mDevice).isEqualTo(mDevice);
assertThat(mSubCachedDevice.getDeviceSide()).isEqualTo(
HearingAidProfile.DeviceSide.SIDE_LEFT);
assertThat(mSubCachedDevice.getDeviceSide()).isEqualTo(HearingAidInfo.DeviceSide.SIDE_LEFT);
}
@Test
@@ -1080,7 +1078,7 @@ public class CachedBluetoothDeviceTest {
updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
assertThat(mCachedDevice.isHearingAidDevice()).isTrue();
assertThat(mCachedDevice.isConnectedHearingAidDevice()).isTrue();
}
@Test
@@ -1091,7 +1089,7 @@ public class CachedBluetoothDeviceTest {
updateProfileStatus(mHapClientProfile, BluetoothProfile.STATE_CONNECTED);
updateProfileStatus(mLeAudioProfile, BluetoothProfile.STATE_CONNECTED);
assertThat(mCachedDevice.isHearingAidDevice()).isTrue();
assertThat(mCachedDevice.isConnectedHearingAidDevice()).isTrue();
}
@Test
@@ -1104,6 +1102,17 @@ public class CachedBluetoothDeviceTest {
updateProfileStatus(mHapClientProfile, BluetoothProfile.STATE_DISCONNECTED);
updateProfileStatus(mLeAudioProfile, BluetoothProfile.STATE_DISCONNECTED);
assertThat(mCachedDevice.isHearingAidDevice()).isFalse();
assertThat(mCachedDevice.isConnectedHearingAidDevice()).isFalse();
}
private HearingAidInfo getLeftAshaHearingAidInfo() {
return new HearingAidInfo.Builder()
.setAshaDeviceSide(HearingAidProfile.DeviceSide.SIDE_LEFT)
.build();
}
private HearingAidInfo getRightAshaHearingAidInfo() {
return new HearingAidInfo.Builder()
.setAshaDeviceSide(HearingAidProfile.DeviceSide.SIDE_RIGHT)
.build();
}
}

View File

@@ -17,6 +17,7 @@ package com.android.settingslib.bluetooth;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
@@ -106,7 +107,7 @@ public class HearingAidDeviceManagerTest {
* deviceSide, deviceMode.
*/
@Test
public void initHearingAidDeviceIfNeeded_validHiSyncId_setHearingAidInfos() {
public void initHearingAidDeviceIfNeeded_validHiSyncId_setHearingAidInfo() {
when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(HISYNCID1);
when(mHearingAidProfile.getDeviceMode(mDevice1)).thenReturn(
HearingAidProfile.DeviceMode.MODE_BINAURAL);
@@ -118,21 +119,22 @@ public class HearingAidDeviceManagerTest {
assertThat(mCachedDevice1.getHiSyncId()).isEqualTo(HISYNCID1);
assertThat(mCachedDevice1.getDeviceMode()).isEqualTo(
HearingAidProfile.DeviceMode.MODE_BINAURAL);
HearingAidInfo.DeviceMode.MODE_BINAURAL);
assertThat(mCachedDevice1.getDeviceSide()).isEqualTo(
HearingAidProfile.DeviceSide.SIDE_RIGHT);
HearingAidInfo.DeviceSide.SIDE_RIGHT);
}
/**
* Test initHearingAidDeviceIfNeeded, an invalid HiSyncId will not be assigned
*/
@Test
public void initHearingAidDeviceIfNeeded_invalidHiSyncId_notToSetHiSyncId() {
public void initHearingAidDeviceIfNeeded_invalidHiSyncId_notToSetHearingAidInfo() {
when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(
BluetoothHearingAid.HI_SYNC_ID_INVALID);
mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1);
verify(mCachedDevice1, never()).setHiSyncId(anyLong());
verify(mCachedDevice1, never()).setHearingAidInfo(any(HearingAidInfo.class));
}
/**
@@ -140,9 +142,10 @@ public class HearingAidDeviceManagerTest {
*/
@Test
public void setSubDeviceIfNeeded_sameHiSyncId_setSubDevice() {
mCachedDevice1.setHiSyncId(HISYNCID1);
mCachedDevice2.setHiSyncId(HISYNCID1);
mCachedDevice1.setHearingAidInfo(getLeftAshaHearingAidInfo(HISYNCID1));
mCachedDevice2.setHearingAidInfo(getRightAshaHearingAidInfo(HISYNCID1));
mCachedDeviceManager.mCachedDevices.add(mCachedDevice1);
mHearingAidDeviceManager.setSubDeviceIfNeeded(mCachedDevice2);
assertThat(mCachedDevice1.getSubDevice()).isEqualTo(mCachedDevice2);
@@ -153,9 +156,10 @@ public class HearingAidDeviceManagerTest {
*/
@Test
public void setSubDeviceIfNeeded_differentHiSyncId_notSetSubDevice() {
mCachedDevice1.setHiSyncId(HISYNCID1);
mCachedDevice2.setHiSyncId(HISYNCID2);
mCachedDevice1.setHearingAidInfo(getLeftAshaHearingAidInfo(HISYNCID1));
mCachedDevice2.setHearingAidInfo(getRightAshaHearingAidInfo(HISYNCID2));
mCachedDeviceManager.mCachedDevices.add(mCachedDevice1);
mHearingAidDeviceManager.setSubDeviceIfNeeded(mCachedDevice2);
assertThat(mCachedDevice1.getSubDevice()).isNull();
@@ -276,9 +280,9 @@ public class HearingAidDeviceManagerTest {
assertThat(mCachedDevice1.getHiSyncId()).isEqualTo(HISYNCID1);
assertThat(mCachedDevice1.getDeviceMode()).isEqualTo(
HearingAidProfile.DeviceMode.MODE_BINAURAL);
HearingAidInfo.DeviceMode.MODE_BINAURAL);
assertThat(mCachedDevice1.getDeviceSide()).isEqualTo(
HearingAidProfile.DeviceSide.SIDE_RIGHT);
HearingAidInfo.DeviceSide.SIDE_RIGHT);
verify(mHearingAidDeviceManager).onHiSyncIdChanged(HISYNCID1);
}
@@ -389,11 +393,9 @@ public class HearingAidDeviceManagerTest {
@Test
public void onProfileConnectionStateChanged_disconnected_mainDevice_subDeviceConnected_switch()
{
when(mCachedDevice1.getHiSyncId()).thenReturn(HISYNCID1);
mCachedDevice1.setDeviceSide(HearingAidProfile.DeviceSide.SIDE_LEFT);
when(mCachedDevice2.getHiSyncId()).thenReturn(HISYNCID1);
mCachedDevice1.setHearingAidInfo(getLeftAshaHearingAidInfo(HISYNCID1));
mCachedDevice2.setHearingAidInfo(getRightAshaHearingAidInfo(HISYNCID1));
when(mCachedDevice2.isConnected()).thenReturn(true);
mCachedDevice2.setDeviceSide(HearingAidProfile.DeviceSide.SIDE_RIGHT);
mCachedDeviceManager.mCachedDevices.add(mCachedDevice1);
mCachedDevice1.setSubDevice(mCachedDevice2);
@@ -404,10 +406,8 @@ public class HearingAidDeviceManagerTest {
assertThat(mCachedDevice1.mDevice).isEqualTo(mDevice2);
assertThat(mCachedDevice2.mDevice).isEqualTo(mDevice1);
assertThat(mCachedDevice1.getDeviceSide()).isEqualTo(
HearingAidProfile.DeviceSide.SIDE_RIGHT);
assertThat(mCachedDevice2.getDeviceSide()).isEqualTo(
HearingAidProfile.DeviceSide.SIDE_LEFT);
assertThat(mCachedDevice1.getDeviceSide()).isEqualTo(HearingAidInfo.DeviceSide.SIDE_RIGHT);
assertThat(mCachedDevice2.getDeviceSide()).isEqualTo(HearingAidInfo.DeviceSide.SIDE_LEFT);
verify(mCachedDevice1).refresh();
}
@@ -455,4 +455,18 @@ public class HearingAidDeviceManagerTest {
assertThat(mHearingAidDeviceManager.findMainDevice(mCachedDevice2)).
isEqualTo(mCachedDevice1);
}
private HearingAidInfo getLeftAshaHearingAidInfo(long hiSyncId) {
return new HearingAidInfo.Builder()
.setAshaDeviceSide(HearingAidInfo.DeviceSide.SIDE_LEFT)
.setHiSyncId(hiSyncId)
.build();
}
private HearingAidInfo getRightAshaHearingAidInfo(long hiSyncId) {
return new HearingAidInfo.Builder()
.setAshaDeviceSide(HearingAidInfo.DeviceSide.SIDE_RIGHT)
.setHiSyncId(hiSyncId)
.build();
}
}