Check the device is connected when receive onProfileConnectionStateChanged()

Use isFilterMatched() to decide the prefernce should be added
or removed when receive onProfileConnectionStateChanged()

Bug: 80161203
Test: make -j42 RunSettingsRoboTests
Change-Id: Icccdb9007b587d3f481a23856edd7b2f7c9b04e0
diff --git a/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java
index 44ef4be..2b9c2cb 100644
--- a/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java
+++ b/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java
@@ -60,25 +60,6 @@
     }
 
     @Override
-    public void onProfileConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state,
-            int bluetoothProfile) {
-        if (DBG) {
-            Log.d(TAG, "onProfileConnectionStateChanged() device: " +
-                    cachedDevice.getName() + ", state: " + state + ", bluetoothProfile: "
-                    + bluetoothProfile);
-        }
-        if (state == BluetoothProfile.STATE_CONNECTED) {
-            if (isFilterMatched(cachedDevice)) {
-                addPreference(cachedDevice);
-            } else {
-                removePreference(cachedDevice);
-            }
-        } else if (state == BluetoothProfile.STATE_DISCONNECTED) {
-            removePreference(cachedDevice);
-        }
-    }
-
-    @Override
     public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
         final int audioMode = mAudioManager.getMode();
         final int currentAudioProfile;
diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java
index 0c0c5ea..dece0cc 100644
--- a/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java
+++ b/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java
@@ -162,6 +162,11 @@
     @Override
     public void onProfileConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state,
             int bluetoothProfile) {
+        if (DBG) {
+            Log.d(TAG, "onProfileConnectionStateChanged() device: " + cachedDevice.getName()
+                    + ", state: " + state + ", bluetoothProfile: " + bluetoothProfile);
+        }
+        update(cachedDevice);
     }
 
     @Override
diff --git a/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java
index 500fb06..259a403 100644
--- a/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java
+++ b/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java
@@ -60,25 +60,6 @@
     }
 
     @Override
-    public void onProfileConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state,
-            int bluetoothProfile) {
-        if (DBG) {
-            Log.d(TAG, "onProfileConnectionStateChanged() device: " +
-                    cachedDevice.getName() + ", state: " + state + ", bluetoothProfile: "
-                    + bluetoothProfile);
-        }
-        if (state == BluetoothProfile.STATE_CONNECTED) {
-            if (isFilterMatched(cachedDevice)) {
-                addPreference(cachedDevice);
-            } else {
-                removePreference(cachedDevice);
-            }
-        } else if (state == BluetoothProfile.STATE_DISCONNECTED) {
-            removePreference(cachedDevice);
-        }
-    }
-
-    @Override
     public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
         final int audioMode = mAudioManager.getMode();
         final int currentAudioProfile;
diff --git a/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java
index db11164..059a920 100644
--- a/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java
+++ b/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java
@@ -49,16 +49,6 @@
     }
 
     @Override
-    public void onProfileConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state,
-            int bluetoothProfile) {
-        if (state == BluetoothProfile.STATE_CONNECTED) {
-            removePreference(cachedDevice);
-        } else if (state == BluetoothProfile.STATE_DISCONNECTED) {
-            addPreference(cachedDevice);
-        }
-    }
-
-    @Override
     public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
         final BluetoothDevice device = cachedDevice.getDevice();
         if (DBG) {
diff --git a/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java
index a88d8ff..def8c4d 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java
@@ -68,6 +68,7 @@
         doReturn(mContext).when(mDashboardFragment).getContext();
         when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
         when(mLocalManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
+        when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
 
         mBluetoothDeviceUpdater = spy(new SavedBluetoothDeviceUpdater(mDashboardFragment,
                 mDevicePreferenceCallback, mLocalManager));
@@ -99,6 +100,8 @@
 
     @Test
     public void onProfileConnectionStateChanged_deviceConnected_removePreference() {
+        when(mBluetoothDevice.isConnected()).thenReturn(true);
+
         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
                 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
 
@@ -107,6 +110,8 @@
 
     @Test
     public void onProfileConnectionStateChanged_deviceDisconnected_addPreference() {
+        when(mBluetoothDevice.isConnected()).thenReturn(false);
+
         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice,
                 BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.A2DP);