Merge "Fix UI incorrect when switch active device to hearing aid"
diff --git a/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaManager.java b/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaManager.java
index 3a738d2..151aa8d 100644
--- a/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaManager.java
@@ -277,14 +277,39 @@
public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) {
Log.d(TAG, "onActiveDeviceChanged : device : "
+ activeDevice + ", profile : " + bluetoothProfile);
- if (BluetoothProfile.HEARING_AID == bluetoothProfile
- || BluetoothProfile.A2DP == bluetoothProfile) {
+
+ if (BluetoothProfile.HEARING_AID == bluetoothProfile) {
+ if (activeDevice != null) {
+ dispatchConnectedDeviceChanged(MediaDeviceUtils.getId(activeDevice));
+ }
+ } else if (BluetoothProfile.A2DP == bluetoothProfile) {
+ // When active device change to Hearing Aid,
+ // BluetoothEventManager also send onActiveDeviceChanged() to notify that active device
+ // of A2DP profile is null. To handle this case, check hearing aid device
+ // is active device or not
+ final MediaDevice activeHearingAidDevice = findActiveHearingAidDevice();
final String id = activeDevice == null
- ? PhoneMediaDevice.ID : MediaDeviceUtils.getId(activeDevice);
+ ? activeHearingAidDevice == null
+ ? PhoneMediaDevice.ID : activeHearingAidDevice.getId()
+ : MediaDeviceUtils.getId(activeDevice);
dispatchConnectedDeviceChanged(id);
}
}
+ private MediaDevice findActiveHearingAidDevice() {
+ final HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile();
+
+ if (hearingAidProfile != null) {
+ final List<BluetoothDevice> activeDevices = hearingAidProfile.getActiveDevices();
+ for (BluetoothDevice btDevice : activeDevices) {
+ if (btDevice != null) {
+ return findMediaDevice(MediaDeviceUtils.getId(btDevice));
+ }
+ }
+ }
+ return null;
+ }
+
@Override
public void onServiceConnected() {
if (!mIsA2dpProfileReady || !mIsHearingAidProfileReady) {
diff --git a/packages/SettingsLib/src/com/android/settingslib/media/MediaDeviceUtils.java b/packages/SettingsLib/src/com/android/settingslib/media/MediaDeviceUtils.java
index bdddaf3..f181150 100644
--- a/packages/SettingsLib/src/com/android/settingslib/media/MediaDeviceUtils.java
+++ b/packages/SettingsLib/src/com/android/settingslib/media/MediaDeviceUtils.java
@@ -15,6 +15,8 @@
*/
package com.android.settingslib.media;
+import android.bluetooth.BluetoothDevice;
+
import androidx.mediarouter.media.MediaRouter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -34,6 +36,16 @@
}
/**
+ * Use BluetoothDevice address to represent unique id
+ *
+ * @param bluetoothDevice the BluetoothDevice
+ * @return BluetoothDevice address
+ */
+ public static String getId(BluetoothDevice bluetoothDevice) {
+ return bluetoothDevice.getAddress();
+ }
+
+ /**
* Use RouteInfo id to represent unique id
*
* @param route the RouteInfo
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/BluetoothMediaManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/BluetoothMediaManagerTest.java
index 48449f2..70b04ab 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/BluetoothMediaManagerTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/BluetoothMediaManagerTest.java
@@ -425,4 +425,34 @@
verify(mCallback, never()).onConnectedDeviceChanged(any());
}
+
+ @Test
+ public void onActiveDeviceChanged_hearingAidDeviceIsActive_returnHearingAidDeviceId() {
+ final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
+ final List<BluetoothDevice> devices = new ArrayList<>();
+ devices.add(bluetoothDevice);
+ final BluetoothMediaDevice bluetoothMediaDevice = mock(BluetoothMediaDevice.class);
+ mMediaManager.mMediaDevices.add(bluetoothMediaDevice);
+
+ when(bluetoothDevice.getAddress()).thenReturn(TEST_ADDRESS);
+ when(mHapProfile.getActiveDevices()).thenReturn(devices);
+ when(bluetoothMediaDevice.getId()).thenReturn(TEST_ADDRESS);
+
+ mMediaManager.registerCallback(mCallback);
+ mMediaManager.onActiveDeviceChanged(null, BluetoothProfile.A2DP);
+
+ verify(mCallback).onConnectedDeviceChanged(TEST_ADDRESS);
+ }
+
+ @Test
+ public void onActiveDeviceChanged_hearingAidDeviceNotActive_returnPhoneDeviceId() {
+ final List<BluetoothDevice> devices = new ArrayList<>();
+
+ when(mHapProfile.getActiveDevices()).thenReturn(devices);
+
+ mMediaManager.registerCallback(mCallback);
+ mMediaManager.onActiveDeviceChanged(null, BluetoothProfile.A2DP);
+
+ verify(mCallback).onConnectedDeviceChanged(PhoneMediaDevice.ID);
+ }
}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceUtilsTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceUtilsTest.java
index 6e29e13..1e5545f 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceUtilsTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceUtilsTest.java
@@ -20,6 +20,8 @@
import static org.mockito.Mockito.when;
+import android.bluetooth.BluetoothDevice;
+
import androidx.mediarouter.media.MediaRouter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -38,7 +40,9 @@
private static final String TEST_ROUTE_ID = "test_route_id";
@Mock
- private CachedBluetoothDevice mDevice;
+ private CachedBluetoothDevice mCachedDevice;
+ @Mock
+ private BluetoothDevice mBluetoothDevice;
@Mock
private MediaRouter.RouteInfo mRouteInfo;
@@ -48,10 +52,19 @@
}
@Test
- public void getId_returnBluetoothDeviceAddress() {
- when(mDevice.getAddress()).thenReturn(TEST_ADDRESS);
+ public void getId_returnCachedBluetoothDeviceAddress() {
+ when(mCachedDevice.getAddress()).thenReturn(TEST_ADDRESS);
- final String id = MediaDeviceUtils.getId(mDevice);
+ final String id = MediaDeviceUtils.getId(mCachedDevice);
+
+ assertThat(id).isEqualTo(TEST_ADDRESS);
+ }
+
+ @Test
+ public void getId_returnBluetoothDeviceAddress() {
+ when(mBluetoothDevice.getAddress()).thenReturn(TEST_ADDRESS);
+
+ final String id = MediaDeviceUtils.getId(mBluetoothDevice);
assertThat(id).isEqualTo(TEST_ADDRESS);
}