diff --git a/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java index 14c20f196ab..3d8e148d742 100644 --- a/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java +++ b/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java @@ -66,7 +66,7 @@ public class AvailableMediaBluetoothDeviceUpdater extends BluetoothDeviceUpdater } boolean isFilterMatched = false; - if (isDeviceConnected(cachedDevice)) { + if (isDeviceConnected(cachedDevice) && isDeviceInCachedDevicesList(cachedDevice)) { if (DBG) { Log.d(TAG, "isFilterMatched() current audio profile : " + currentAudioProfile); } @@ -74,6 +74,8 @@ public class AvailableMediaBluetoothDeviceUpdater extends BluetoothDeviceUpdater // It would show in Available Devices group. if (cachedDevice.isConnectedHearingAidDevice() || cachedDevice.isConnectedLeAudioDevice()) { + Log.d(TAG, "isFilterMatched() device : " + + cachedDevice.getName() + ", the profile is connected."); return true; } // According to the current audio profile type, diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java index d65500be27a..89346768409 100644 --- a/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java +++ b/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java @@ -326,4 +326,8 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback, ((BluetoothDevicePreference) preference).onPreferenceAttributesChanged(); } } + + protected boolean isDeviceInCachedDevicesList(CachedBluetoothDevice cachedDevice){ + return mLocalManager.getCachedDeviceManager().getCachedDevicesCopy().contains(cachedDevice); + } } diff --git a/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java index d1c45b61f45..5c3dda362ff 100644 --- a/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java +++ b/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java @@ -66,7 +66,7 @@ public class ConnectedBluetoothDeviceUpdater extends BluetoothDeviceUpdater { } boolean isFilterMatched = false; - if (isDeviceConnected(cachedDevice)) { + if (isDeviceConnected(cachedDevice) && isDeviceInCachedDevicesList(cachedDevice)) { if (DBG) { Log.d(TAG, "isFilterMatched() current audio profile : " + currentAudioProfile); } diff --git a/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java index f5bc279b8a3..17b379238e4 100644 --- a/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java +++ b/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java @@ -105,7 +105,8 @@ public class SavedBluetoothDeviceUpdater extends BluetoothDeviceUpdater + cachedDevice.isConnected()); } return device.getBondState() == BluetoothDevice.BOND_BONDED - && (mDisplayConnected || !device.isConnected()); + && (mDisplayConnected || (!device.isConnected() && isDeviceInCachedDevicesList( + cachedDevice))); } @Override diff --git a/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java index 3cdff6e1eb6..4427127cc65 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java @@ -86,6 +86,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { mShadowCachedBluetoothDeviceManager = Shadow.extract( Utils.getLocalBtManager(mContext).getCachedDeviceManager()); mCachedDevices = new ArrayList<>(); + mCachedDevices.add(mCachedBluetoothDevice); mShadowCachedBluetoothDeviceManager.setCachedDevicesCopy(mCachedDevices); Pair pairs = new Pair<>(mDrawable, "fake_device"); @@ -109,7 +110,6 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); - mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -122,7 +122,6 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); - mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -135,7 +134,6 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); - mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -148,7 +146,6 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); - mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -260,6 +257,35 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); } + @Test + public void + onProfileConnectionStateChanged_deviceIsNotInList_notInCall_invokesRemovePreference() { + mAudioManager.setMode(AudioManager.MODE_NORMAL); + when(mBluetoothDeviceUpdater + .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); + when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); + mCachedDevices.clear(); + + mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, + BluetoothProfile.STATE_CONNECTED, BluetoothProfile.LE_AUDIO); + + verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); + } + + @Test + public void onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovePreference() { + mAudioManager.setMode(AudioManager.MODE_IN_CALL); + when(mBluetoothDeviceUpdater + .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); + when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); + mCachedDevices.clear(); + + mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, + BluetoothProfile.STATE_CONNECTED, BluetoothProfile.LE_AUDIO); + + verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); + } + @Test public void onProfileConnectionStateChanged_deviceDisconnected_removePreference() { mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, diff --git a/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java index 98f1fe37fad..7472dc148f2 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java @@ -90,6 +90,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { Utils.getLocalBtManager(mContext).getCachedDeviceManager()); doReturn(mContext).when(mDashboardFragment).getContext(); mCachedDevices = new ArrayList<>(); + mCachedDevices.add(mCachedBluetoothDevice); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS); @@ -108,7 +109,6 @@ public class ConnectedBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); - mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -121,7 +121,6 @@ public class ConnectedBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); - mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -134,7 +133,6 @@ public class ConnectedBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); - mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -147,7 +145,6 @@ public class ConnectedBluetoothDeviceUpdaterTest { when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); - mCachedDevices.add(mCachedBluetoothDevice); mBluetoothDeviceUpdater.onAudioModeChanged(); @@ -167,6 +164,20 @@ public class ConnectedBluetoothDeviceUpdaterTest { verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); } + @Test + public void onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovePreference() { + mAudioManager.setMode(AudioManager.MODE_IN_CALL); + when(mBluetoothDeviceUpdater. + isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); + when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); + mCachedDevices.clear(); + + mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, + BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); + + verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); + } + @Test public void onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_removePreference() { mAudioManager.setMode(AudioManager.MODE_NORMAL); @@ -260,6 +271,35 @@ public class ConnectedBluetoothDeviceUpdaterTest { verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); } + @Test + public void onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovesPreference() + { + mAudioManager.setMode(AudioManager.MODE_IN_CALL); + when(mBluetoothDeviceUpdater + .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); + when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); + mCachedDevices.clear(); + + mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, + BluetoothProfile.STATE_CONNECTED, BluetoothProfile.LE_AUDIO); + + verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); + } + + @Test + public void onProfileConnectionStateChanged_deviceIsNotInList_notInCall_invokesRemovesPreference + () { + mAudioManager.setMode(AudioManager.MODE_NORMAL); + when(mBluetoothDeviceUpdater + .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); + when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); + mCachedDevices.clear(); + + mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, + BluetoothProfile.STATE_CONNECTED, BluetoothProfile.LE_AUDIO); + + verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); + } @Test public void onProfileConnectionStateChanged_deviceDisconnected_removePreference() { diff --git a/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java index da7516b1fab..255a0be5773 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java @@ -77,6 +77,7 @@ public class SavedBluetoothDeviceUpdaterTest { private Context mContext; private SavedBluetoothDeviceUpdater mBluetoothDeviceUpdater; private BluetoothDevicePreference mPreference; + private List mCachedDevices = new ArrayList<>(); @Before public void setUp() { @@ -99,6 +100,10 @@ public class SavedBluetoothDeviceUpdaterTest { false, BluetoothDevicePreference.SortType.TYPE_DEFAULT); doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); doNothing().when(mBluetoothDeviceUpdater).removePreference(any()); + mCachedDevices.add(mCachedBluetoothDevice); + when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); + when(mDeviceManager.getCachedDevicesCopy()).thenReturn(mCachedDevices); + } @Test @@ -143,6 +148,31 @@ public class SavedBluetoothDeviceUpdaterTest { BluetoothDevicePreference.SortType.TYPE_NO_SORT); } + @Test + public void + onProfileConnectionStateChanged_leDeviceDisconnected_inDeviceList_invokesAddPreference() + { + when(mBluetoothDevice.isConnected()).thenReturn(false); + + mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, + BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.LE_AUDIO); + + verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice, + BluetoothDevicePreference.SortType.TYPE_NO_SORT); + } + + @Test + public void + onProfileConnectionStateChanged_deviceDisconnected_notInDeviceList_invokesRemovePreference() { + when(mBluetoothDevice.isConnected()).thenReturn(false); + mCachedDevices.clear(); + + mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, + BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.LE_AUDIO); + + verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); + } + @Test public void onClick_Preference_setConnect() { mBluetoothDeviceUpdater.onPreferenceClick(mPreference);