From 2a3151d26804f26c0459b377785679d3748dd628 Mon Sep 17 00:00:00 2001 From: hughchen Date: Wed, 20 Mar 2019 14:46:12 +0800 Subject: [PATCH] Fix UI incorrect when switch active device to hearing aid 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. If Hearing aid device is active device then return Hearing aid device id, otherwise return phone device id. Bug: 128956186 Test: RunSettingsLibRoboTests Change-Id: I1ba9249d7a7f634b56df019450643cfe147a5fb8 --- .../media/BluetoothMediaManager.java | 31 +++++++++++++++++-- .../settingslib/media/MediaDeviceUtils.java | 12 +++++++ .../media/BluetoothMediaManagerTest.java | 30 ++++++++++++++++++ .../media/MediaDeviceUtilsTest.java | 21 ++++++++++--- 4 files changed, 87 insertions(+), 7 deletions(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaManager.java b/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaManager.java index 3a738d23f4521..151aa8d0cf6fd 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 class BluetoothMediaManager extends MediaManager implements BluetoothCall 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 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 bdddaf394f897..f181150de513a 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; @@ -33,6 +35,16 @@ public class MediaDeviceUtils { return cachedDevice.getAddress(); } + /** + * 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 * 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 48449f20699d5..70b04ab40c34b 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 @@ public class BluetoothMediaManagerTest { verify(mCallback, never()).onConnectedDeviceChanged(any()); } + + @Test + public void onActiveDeviceChanged_hearingAidDeviceIsActive_returnHearingAidDeviceId() { + final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class); + final List 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 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 6e29e137c9f23..1e5545f8539fb 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 com.google.common.truth.Truth.assertThat; 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 @@ public class MediaDeviceUtilsTest { 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 @@ public class MediaDeviceUtilsTest { } @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); }