Merge "Fix issue to avoid Hearing Aid show duplication device."

This commit is contained in:
TreeHugger Robot
2018-12-20 18:38:28 +00:00
committed by Android (Google) Code Review
4 changed files with 78 additions and 0 deletions

View File

@@ -468,6 +468,16 @@ public class BluetoothEventManager {
private class AclStateChangedHandler implements Handler {
@Override
public void onReceive(Context context, Intent intent, BluetoothDevice device) {
if (device == null) {
Log.w(TAG, "AclStateChangedHandler: device is null");
return;
}
// Avoid to notify Settings UI for Hearing Aid sub device.
if (mDeviceManager.isSubDevice(device)) {
return;
}
final String action = intent.getAction();
if (action == null) {
Log.w(TAG, "AclStateChangedHandler: action is null");

View File

@@ -126,6 +126,25 @@ public class CachedBluetoothDeviceManager {
return null;
}
/**
* Search for existing sub device {@link CachedBluetoothDevice}.
*
* @param device the address of the Bluetooth device
* @return true for found sub device or false.
*/
public synchronized boolean isSubDevice(BluetoothDevice device) {
for (CachedBluetoothDevice cachedDevice : mCachedDevices) {
if (!cachedDevice.getDevice().equals(device)) {
// Check sub devices if it exists
CachedBluetoothDevice subDevice = cachedDevice.getSubDevice();
if (subDevice != null && subDevice.getDevice().equals(device)) {
return true;
}
}
}
return false;
}
/**
* Updates the Hearing Aid devices; specifically the HiSyncId's. This routine is called when the
* Hearing Aid Service is connected and the HiSyncId's are now available.

View File

@@ -18,6 +18,7 @@ package com.android.settingslib.bluetooth;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -154,4 +155,30 @@ public class BluetoothEventManagerTest {
verify(mBluetoothCallback).onAclConnectionStateChanged(mCachedBluetoothDevice,
BluetoothAdapter.STATE_CONNECTED);
}
@Test
public void dispatchAclConnectionStateChanged_aclDisconnected_shouldNotCallbackSubDevice() {
when(mCachedDeviceManager.isSubDevice(mBluetoothDevice)).thenReturn(true);
mBluetoothEventManager.registerCallback(mBluetoothCallback);
mIntent = new Intent(BluetoothDevice.ACTION_ACL_DISCONNECTED);
mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);
mContext.sendBroadcast(mIntent);
verify(mBluetoothCallback, never()).onAclConnectionStateChanged(mCachedBluetoothDevice,
BluetoothAdapter.STATE_DISCONNECTED);
}
@Test
public void dispatchAclConnectionStateChanged_aclConnected_shouldNotCallbackSubDevice() {
when(mCachedDeviceManager.isSubDevice(mBluetoothDevice)).thenReturn(true);
mBluetoothEventManager.registerCallback(mBluetoothCallback);
mIntent = new Intent(BluetoothDevice.ACTION_ACL_CONNECTED);
mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);
mContext.sendBroadcast(mIntent);
verify(mBluetoothCallback, never()).onAclConnectionStateChanged(mCachedBluetoothDevice,
BluetoothAdapter.STATE_CONNECTED);
}
}

View File

@@ -18,6 +18,7 @@ package com.android.settingslib.bluetooth;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -333,6 +334,27 @@ public class CachedBluetoothDeviceManagerTest {
verify(mCachedDevice2).getConnectionSummary();
}
/**
* Test to verify isSubDevice_validSubDevice().
*/
@Test
public void isSubDevice_validSubDevice() {
doReturn(HISYNCID1).when(mHearingAidProfile).getHiSyncId(mDevice1);
mCachedDeviceManager.addDevice(mDevice1);
// Both device are not sub device in default value.
assertThat(mCachedDeviceManager.isSubDevice(mDevice1)).isFalse();
assertThat(mCachedDeviceManager.isSubDevice(mDevice2)).isFalse();
// Add Device-2 as sub device of Device-1 with same HiSyncId.
doReturn(HISYNCID1).when(mHearingAidProfile).getHiSyncId(mDevice2);
mCachedDeviceManager.addDevice(mDevice2);
// Verify Device-2 is sub device, but Device-1 is not.
assertThat(mCachedDeviceManager.isSubDevice(mDevice2)).isTrue();
assertThat(mCachedDeviceManager.isSubDevice(mDevice1)).isFalse();
}
/**
* Test to verify updateHearingAidsDevices().
*/