diff options
4 files changed, 78 insertions, 0 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java index 7914a0b9d6fd..177ba00c8a31 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java @@ -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"); diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java index 3a6283812e0c..5b4a8b4f259e 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java @@ -127,6 +127,25 @@ public class CachedBluetoothDeviceManager { } /** + * 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. * @param LocalBluetoothProfileManager profileManager diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java index 27b8dfc28448..0dcdaed67cd6 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java @@ -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); + } } diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java index 47b12103e772..43b289464e1d 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java @@ -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; @@ -334,6 +335,27 @@ public class CachedBluetoothDeviceManagerTest { } /** + * 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(). */ @Test |