Merge "Try select preset independently after group operation failed" into main

This commit is contained in:
Angela Wang
2024-04-11 01:04:51 +00:00
committed by Android (Google) Code Review
2 changed files with 76 additions and 40 deletions

View File

@@ -110,46 +110,25 @@ public class BluetoothDetailsHearingAidsPresetsController extends
&& preference instanceof final ListPreference listPreference) { && preference instanceof final ListPreference listPreference) {
final int index = listPreference.findIndexOfValue(value); final int index = listPreference.findIndexOfValue(value);
final String presetName = listPreference.getEntries()[index].toString(); final String presetName = listPreference.getEntries()[index].toString();
final int presetIndex = Integer.parseInt( final int presetIndex = Integer.parseInt(value);
listPreference.getEntryValues()[index].toString());
listPreference.setSummary(presetName); listPreference.setSummary(presetName);
if (DEBUG) {
Log.d(TAG, "onPreferenceChange"
+ ", presetIndex: " + presetIndex
+ ", presetName: " + presetName);
}
boolean supportSynchronizedPresets = mHapClientProfile.supportsSynchronizedPresets( boolean supportSynchronizedPresets = mHapClientProfile.supportsSynchronizedPresets(
mCachedDevice.getDevice()); mCachedDevice.getDevice());
int hapGroupId = mHapClientProfile.getHapGroup(mCachedDevice.getDevice()); int hapGroupId = mHapClientProfile.getHapGroup(mCachedDevice.getDevice());
if (supportSynchronizedPresets if (supportSynchronizedPresets) {
&& hapGroupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) { if (hapGroupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
if (DEBUG) { selectPresetSynchronously(hapGroupId, presetIndex);
Log.d(TAG, "onPreferenceChange, selectPresetForGroup "
+ ", presetName: " + presetName
+ ", presetIndex: " + presetIndex
+ ", hapGroupId: " + hapGroupId
+ ", device: " + mCachedDevice.getAddress());
}
mHapClientProfile.selectPresetForGroup(hapGroupId, presetIndex);
} else { } else {
if (DEBUG) { Log.w(TAG, "supportSynchronizedPresets but hapGroupId is invalid.");
Log.d(TAG, "onPreferenceChange, selectPreset " selectPresetIndependently(presetIndex);
+ ", presetName: " + presetName
+ ", presetIndex: " + presetIndex
+ ", device: " + mCachedDevice.getAddress());
}
mHapClientProfile.selectPreset(mCachedDevice.getDevice(), presetIndex);
final CachedBluetoothDevice subDevice = mCachedDevice.getSubDevice();
if (subDevice != null) {
if (DEBUG) {
Log.d(TAG, "onPreferenceChange, selectPreset for subDevice"
+ ", device: " + subDevice.getAddress());
}
mHapClientProfile.selectPreset(subDevice.getDevice(), presetIndex);
}
for (final CachedBluetoothDevice memberDevice :
mCachedDevice.getMemberDevice()) {
if (DEBUG) {
Log.d(TAG, "onPreferenceChange, selectPreset for memberDevice"
+ ", device: " + memberDevice.getAddress());
}
mHapClientProfile.selectPreset(memberDevice.getDevice(), presetIndex);
} }
} else {
selectPresetIndependently(presetIndex);
} }
return true; return true;
} }
@@ -181,6 +160,9 @@ public class BluetoothDetailsHearingAidsPresetsController extends
loadAllPresetInfo(); loadAllPresetInfo();
if (mPreference.getEntries().length == 0) { if (mPreference.getEntries().length == 0) {
if (DEBUG) {
Log.w(TAG, "Disable the preference since preset info size = 0");
}
mPreference.setEnabled(false); mPreference.setEnabled(false);
} else { } else {
int activePresetIndex = mHapClientProfile.getActivePresetIndex( int activePresetIndex = mHapClientProfile.getActivePresetIndex(
@@ -235,10 +217,10 @@ public class BluetoothDetailsHearingAidsPresetsController extends
Log.d(TAG, "onPresetSelectionForGroupFailed, group: " + hapGroupId Log.d(TAG, "onPresetSelectionForGroupFailed, group: " + hapGroupId
+ ", reason: " + reason); + ", reason: " + reason);
} }
mContext.getMainExecutor().execute(() -> { // Try to set the preset independently if group operation failed
refresh(); if (mPreference != null) {
showErrorToast(); selectPresetIndependently(Integer.parseInt(mPreference.getValue()));
}); }
} }
} }
@@ -248,8 +230,10 @@ public class BluetoothDetailsHearingAidsPresetsController extends
if (device.equals(mCachedDevice.getDevice())) { if (device.equals(mCachedDevice.getDevice())) {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "onPresetInfoChanged, device: " + device.getAddress() Log.d(TAG, "onPresetInfoChanged, device: " + device.getAddress()
+ ", reason: " + reason + ", reason: " + reason);
+ ", infoList: " + presetInfoList); for (BluetoothHapPresetInfo info: presetInfoList) {
Log.d(TAG, " preset " + info.getIndex() + ": " + info.getName());
}
} }
mContext.getMainExecutor().execute(this::refresh); mContext.getMainExecutor().execute(this::refresh);
} }
@@ -304,6 +288,9 @@ public class BluetoothDetailsHearingAidsPresetsController extends
for (int i = 0; i < infoList.size(); i++) { for (int i = 0; i < infoList.size(); i++) {
presetNames[i] = infoList.get(i).getName(); presetNames[i] = infoList.get(i).getName();
presetIndexes[i] = Integer.toString(infoList.get(i).getIndex()); presetIndexes[i] = Integer.toString(infoList.get(i).getIndex());
if (DEBUG) {
Log.d(TAG, "loadAllPresetInfo, preset " + presetIndexes[i] + ": " + presetNames[i]);
}
} }
mPreference.setEntries(presetNames); mPreference.setEntries(presetNames);
mPreference.setEntryValues(presetIndexes); mPreference.setEntryValues(presetIndexes);
@@ -356,4 +343,42 @@ public class BluetoothDetailsHearingAidsPresetsController extends
public void onServiceDisconnected() { public void onServiceDisconnected() {
// Do nothing // Do nothing
} }
private void selectPresetSynchronously(int groupId, int presetIndex) {
if (mPreference == null) {
return;
}
if (DEBUG) {
Log.d(TAG, "selectPresetSynchronously"
+ ", presetIndex: " + presetIndex
+ ", groupId: " + groupId
+ ", device: " + mCachedDevice.getAddress());
}
mHapClientProfile.selectPresetForGroup(groupId, presetIndex);
}
private void selectPresetIndependently(int presetIndex) {
if (mPreference == null) {
return;
}
if (DEBUG) {
Log.d(TAG, "selectPresetIndependently"
+ ", presetIndex: " + presetIndex
+ ", device: " + mCachedDevice.getAddress());
}
mHapClientProfile.selectPreset(mCachedDevice.getDevice(), presetIndex);
final CachedBluetoothDevice subDevice = mCachedDevice.getSubDevice();
if (subDevice != null) {
if (DEBUG) {
Log.d(TAG, "selectPreset for subDevice, device: " + subDevice);
}
mHapClientProfile.selectPreset(subDevice.getDevice(), presetIndex);
}
for (final CachedBluetoothDevice memberDevice :
mCachedDevice.getMemberDevice()) {
if (DEBUG) {
Log.d(TAG, "selectPreset for memberDevice, device: " + memberDevice);
}
mHapClientProfile.selectPreset(memberDevice.getDevice(), presetIndex);
}
}
} }

View File

@@ -87,6 +87,7 @@ public class BluetoothDetailsHearingAidsPresetsControllerTest extends
when(mLocalManager.getProfileManager()).thenReturn(mProfileManager); when(mLocalManager.getProfileManager()).thenReturn(mProfileManager);
when(mProfileManager.getHapClientProfile()).thenReturn(mHapClientProfile); when(mProfileManager.getHapClientProfile()).thenReturn(mHapClientProfile);
when(mCachedDevice.getDevice()).thenReturn(mDevice);
when(mCachedDevice.getProfiles()).thenReturn(List.of(mHapClientProfile)); when(mCachedDevice.getProfiles()).thenReturn(List.of(mHapClientProfile));
when(mCachedDevice.isConnectedHapClientDevice()).thenReturn(true); when(mCachedDevice.isConnectedHapClientDevice()).thenReturn(true);
when(mCachedChildDevice.getDevice()).thenReturn(mChildDevice); when(mCachedChildDevice.getDevice()).thenReturn(mChildDevice);
@@ -251,6 +252,16 @@ public class BluetoothDetailsHearingAidsPresetsControllerTest extends
assertThat(mController.getPreference().getSummary()).isNotNull(); assertThat(mController.getPreference().getSummary()).isNotNull();
} }
@Test
public void onPresetSelectionForGroupFailed_selectPresetIsCalled() {
when(mHapClientProfile.getHapGroup(mDevice)).thenReturn(TEST_HAP_GROUP_ID);
mController.getPreference().setValue(String.valueOf(TEST_PRESET_INDEX));
mController.onPresetSelectionForGroupFailed(TEST_HAP_GROUP_ID, TEST_PRESET_INDEX);
verify(mHapClientProfile).selectPreset(mDevice, TEST_PRESET_INDEX);
}
private BluetoothHapPresetInfo getTestPresetInfo() { private BluetoothHapPresetInfo getTestPresetInfo() {
BluetoothHapPresetInfo info = mock(BluetoothHapPresetInfo.class); BluetoothHapPresetInfo info = mock(BluetoothHapPresetInfo.class);
when(info.getName()).thenReturn(TEST_PRESET_NAME); when(info.getName()).thenReturn(TEST_PRESET_NAME);