Update UI when BT device is rename

* Using getAliasName() instead of getName()
* Add BluetoothDevice isConnected check, when BluetoothDevice is
  disconnected. Do not add to the show list.

Bug: 77783217
Test: make -j50 RunSettingsRoboTests
Change-Id: I183fc693eaefd690568ba2f1ec740258b2bb62c8
diff --git a/src/com/android/settings/sound/AudioSwitchPreferenceController.java b/src/com/android/settings/sound/AudioSwitchPreferenceController.java
index dde2b30..7ed3db9 100644
--- a/src/com/android/settings/sound/AudioSwitchPreferenceController.java
+++ b/src/com/android/settings/sound/AudioSwitchPreferenceController.java
@@ -131,7 +131,7 @@
             final BluetoothDevice btDevice = mConnectedDevices.get(connectedDeviceIndex);
             mSelectedIndex = connectedDeviceIndex;
             setActiveBluetoothDevice(btDevice);
-            listPreference.setSummary(btDevice.getName());
+            listPreference.setSummary(btDevice.getAliasName());
         }
         return true;
     }
@@ -319,7 +319,7 @@
         mediaValues[mSelectedIndex] = mContext.getText(R.string.media_output_default_summary);
         for (int i = 0, size = mConnectedDevices.size(); i < size; i++) {
             final BluetoothDevice btDevice = mConnectedDevices.get(i);
-            mediaOutputs[i] = btDevice.getName();
+            mediaOutputs[i] = btDevice.getAliasName();
             mediaValues[i] = btDevice.getAddress();
             if (btDevice.equals(activeDevice)) {
                 // select the active connected device.
diff --git a/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceController.java b/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceController.java
index 2039913..f7f547a 100644
--- a/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceController.java
+++ b/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceController.java
@@ -66,7 +66,11 @@
         if (numDevices == 0) {
             // No connected devices, disable switch entry.
             mPreference.setVisible(false);
-            preference.setSummary(mContext.getText(R.string.media_output_default_summary));
+            final CharSequence summary = mContext.getText(R.string.media_output_default_summary);
+            final CharSequence[] defaultMediaOutput = new CharSequence[]{summary};
+            mSelectedIndex = getDefaultDeviceIndex();
+            preference.setSummary(summary);
+            setPreference(defaultMediaOutput, defaultMediaOutput, preference);
             return;
         }
 
diff --git a/src/com/android/settings/sound/MediaOutputPreferenceController.java b/src/com/android/settings/sound/MediaOutputPreferenceController.java
index 79f3c9d..b456263 100644
--- a/src/com/android/settings/sound/MediaOutputPreferenceController.java
+++ b/src/com/android/settings/sound/MediaOutputPreferenceController.java
@@ -78,7 +78,11 @@
         if (numDevices == 0) {
             // Disable switch entry if there is no connected devices.
             mPreference.setVisible(false);
-            preference.setSummary(mContext.getText(R.string.media_output_default_summary));
+            final CharSequence summary = mContext.getText(R.string.media_output_default_summary);
+            final CharSequence[] defaultMediaOutput = new CharSequence[]{summary};
+            mSelectedIndex = getDefaultDeviceIndex();
+            preference.setSummary(summary);
+            setPreference(defaultMediaOutput, defaultMediaOutput, preference);
             return;
         }
 
diff --git a/tests/robotests/src/com/android/settings/sound/AudioOutputSwitchPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/sound/AudioOutputSwitchPreferenceControllerTest.java
index 272fd20..7eccd42 100644
--- a/tests/robotests/src/com/android/settings/sound/AudioOutputSwitchPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/sound/AudioOutputSwitchPreferenceControllerTest.java
@@ -226,7 +226,7 @@
 
         mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_1);
 
-        assertThat(mPreference.getSummary()).isEqualTo(mBluetoothDevice.getName());
+        assertThat(mPreference.getSummary()).isEqualTo(TEST_DEVICE_NAME_1);
     }
 
     /**
@@ -246,7 +246,7 @@
 
         mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_2);
 
-        assertThat(mPreference.getSummary()).isEqualTo(secondBluetoothDevice.getName());
+        assertThat(mPreference.getSummary()).isEqualTo(TEST_DEVICE_NAME_2);
     }
 
     /**
diff --git a/tests/robotests/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceControllerTest.java
index db09eab..314dbfd 100644
--- a/tests/robotests/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceControllerTest.java
@@ -251,7 +251,7 @@
         mController.updateState(mPreference);
 
         assertThat(mPreference.isVisible()).isTrue();
-        assertThat(mPreference.getSummary()).isEqualTo(mBluetoothDevice.getName());
+        assertThat(mPreference.getSummary()).isEqualTo(TEST_DEVICE_NAME_1);
     }
 
     /**
@@ -273,7 +273,7 @@
         mController.updateState(mPreference);
 
         assertThat(mPreference.isVisible()).isTrue();
-        assertThat(mPreference.getSummary()).isEqualTo(mSecondBluetoothDevice.getName());
+        assertThat(mPreference.getSummary()).isEqualTo(TEST_DEVICE_NAME_2);
     }
 
     /**
diff --git a/tests/robotests/src/com/android/settings/sound/MediaOutputPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/sound/MediaOutputPreferenceControllerTest.java
index b777239..e886936 100644
--- a/tests/robotests/src/com/android/settings/sound/MediaOutputPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/sound/MediaOutputPreferenceControllerTest.java
@@ -302,7 +302,7 @@
         mController.updateState(mPreference);
 
         assertThat(mPreference.isVisible()).isTrue();
-        assertThat(mPreference.getSummary()).isEqualTo(mBluetoothDevice.getName());
+        assertThat(mPreference.getSummary()).isEqualTo(TEST_DEVICE_NAME_1);
     }
 
     /**
@@ -323,7 +323,7 @@
         mController.updateState(mPreference);
 
         assertThat(mPreference.isVisible()).isTrue();
-        assertThat(mPreference.getSummary()).isEqualTo(mSecondBluetoothDevice.getName());
+        assertThat(mPreference.getSummary()).isEqualTo(TEST_DEVICE_NAME_2);
     }
 
     /**