Change the title when in call/media state

* For fix the "Automatic merge failed" in pi-dev, cherry pick the ag/3909960 in
  master. Change android.support.* to androidx.*
* Change the title when in call/media state.
  Example: call state show "Available call devices"
           media state show "Available media devices"
* Use isAudioModeOngoingCall() utility function for checking if it is in call status
* Add register test to verify when in onStart() and onStop()
  the BluetoothCallback can be register and unregister.
* Add title string test to verify when in call or media state,
  the title can be changed to corresponding string

Bug: 78150641
Test: make -j40 RunSettingsRoboTests
Change-Id: I6be72cf0cae75525084ac3c5be8524c709f51f7d
diff --git a/src/com/android/settings/connecteddevice/AvailableMediaDeviceGroupController.java b/src/com/android/settings/connecteddevice/AvailableMediaDeviceGroupController.java
index 9c15282..1e5224a 100644
--- a/src/com/android/settings/connecteddevice/AvailableMediaDeviceGroupController.java
+++ b/src/com/android/settings/connecteddevice/AvailableMediaDeviceGroupController.java
@@ -15,6 +15,8 @@
  */
 package com.android.settings.connecteddevice;
 
+import static com.android.settingslib.Utils.isAudioModeOngoingCall;
+
 import android.content.Context;
 import android.content.pm.PackageManager;
 import androidx.annotation.VisibleForTesting;
@@ -23,8 +25,13 @@
 import androidx.preference.PreferenceScreen;
 import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater;
 import com.android.settings.bluetooth.BluetoothDeviceUpdater;
+import com.android.settings.bluetooth.Utils;
 import com.android.settings.core.BasePreferenceController;
 import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.R;
+import com.android.settingslib.bluetooth.BluetoothCallback;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
 import com.android.settingslib.core.lifecycle.LifecycleObserver;
 import com.android.settingslib.core.lifecycle.events.OnStart;
 import com.android.settingslib.core.lifecycle.events.OnStop;
@@ -35,26 +42,30 @@
  * to add/remove {@link Preference}
  */
 public class AvailableMediaDeviceGroupController extends BasePreferenceController
-        implements LifecycleObserver, OnStart, OnStop, DevicePreferenceCallback {
+        implements LifecycleObserver, OnStart, OnStop, DevicePreferenceCallback, BluetoothCallback {
 
     private static final String KEY = "available_device_list";
 
     @VisibleForTesting
     PreferenceGroup mPreferenceGroup;
     private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
+    private final LocalBluetoothManager mLocalBluetoothManager;
 
     public AvailableMediaDeviceGroupController(Context context) {
         super(context, KEY);
+        mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
     }
 
     @Override
     public void onStart() {
         mBluetoothDeviceUpdater.registerCallback();
+        mLocalBluetoothManager.getEventManager().registerCallback(this);
     }
 
     @Override
     public void onStop() {
         mBluetoothDeviceUpdater.unregisterCallback();
+        mLocalBluetoothManager.getEventManager().unregisterCallback(this);
     }
 
     @Override
@@ -63,6 +74,7 @@
         if (isAvailable()) {
             mPreferenceGroup = (PreferenceGroup) screen.findPreference(KEY);
             mPreferenceGroup.setVisible(false);
+            updateTitle();
             mBluetoothDeviceUpdater.setPrefContext(screen.getContext());
             mBluetoothDeviceUpdater.forceUpdate();
         }
@@ -105,4 +117,56 @@
     public void setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater) {
         mBluetoothDeviceUpdater  = bluetoothDeviceUpdater;
     }
+
+    @Override
+    public void onBluetoothStateChanged(int bluetoothState) {
+        // do nothing
+    }
+
+    @Override
+    public void onScanningStateChanged(boolean started) {
+        // do nothing
+    }
+
+    @Override
+    public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
+        // do nothing
+    }
+
+    @Override
+    public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
+        // do nothing
+    }
+
+    @Override
+    public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
+        // do nothing
+    }
+
+    @Override
+    public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
+        // do nothing
+    }
+
+    @Override
+    public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) {
+        // do nothing
+    }
+
+    @Override
+    public void onAudioModeChanged() {
+        updateTitle();
+    }
+
+    private void updateTitle() {
+        if (isAudioModeOngoingCall(mContext)) {
+            // in phone call
+            mPreferenceGroup.
+                    setTitle(mContext.getString(R.string.connected_device_available_call_title));
+        } else {
+            // without phone call
+            mPreferenceGroup.
+                    setTitle(mContext.getString(R.string.connected_device_available_media_title));
+        }
+    }
 }
diff --git a/src/com/android/settings/sound/AudioSwitchPreferenceController.java b/src/com/android/settings/sound/AudioSwitchPreferenceController.java
index cae7b0c..24d23ab 100644
--- a/src/com/android/settings/sound/AudioSwitchPreferenceController.java
+++ b/src/com/android/settings/sound/AudioSwitchPreferenceController.java
@@ -198,13 +198,6 @@
         return mAudioManager.getDevicesForStream(streamType) == device;
     }
 
-    protected boolean isOngoingCallStatus() {
-        final int audioMode = mAudioManager.getMode();
-        return audioMode == AudioManager.MODE_RINGTONE
-                || audioMode == AudioManager.MODE_IN_CALL
-                || audioMode == AudioManager.MODE_IN_COMMUNICATION;
-    }
-
     int getDefaultDeviceIndex() {
         // Default device is after all connected devices.
         return ArrayUtils.size(mConnectedDevices);
diff --git a/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceController.java b/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceController.java
index 7e91865..732f0ef 100644
--- a/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceController.java
+++ b/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceController.java
@@ -19,6 +19,8 @@
 import static android.media.AudioManager.STREAM_VOICE_CALL;
 import static android.media.AudioSystem.DEVICE_OUT_USB_HEADSET;
 
+import com.android.settingslib.Utils;
+
 import android.bluetooth.BluetoothDevice;
 import android.content.Context;
 import androidx.preference.Preference;
@@ -45,7 +47,7 @@
             return;
         }
 
-        if (!isOngoingCallStatus()) {
+        if (!Utils.isAudioModeOngoingCall(mContext)) {
             // Without phone call, disable the switch entry.
             mPreference.setVisible(false);
             preference.setSummary(mContext.getText(R.string.media_output_default_summary));
@@ -90,7 +92,7 @@
 
     @Override
     public void setActiveBluetoothDevice(BluetoothDevice device) {
-        if (isOngoingCallStatus()) {
+        if (Utils.isAudioModeOngoingCall(mContext)) {
             mProfileManager.getHeadsetProfile().setActiveDevice(device);
         }
     }
diff --git a/src/com/android/settings/sound/MediaOutputPreferenceController.java b/src/com/android/settings/sound/MediaOutputPreferenceController.java
index 5ac0848..89ee412 100644
--- a/src/com/android/settings/sound/MediaOutputPreferenceController.java
+++ b/src/com/android/settings/sound/MediaOutputPreferenceController.java
@@ -20,6 +20,8 @@
 import static android.media.AudioSystem.DEVICE_OUT_REMOTE_SUBMIX;
 import static android.media.AudioSystem.DEVICE_OUT_USB_HEADSET;
 
+import com.android.settingslib.Utils;
+
 import android.bluetooth.BluetoothDevice;
 import android.content.Context;
 import android.media.AudioManager;
@@ -57,7 +59,7 @@
             return;
         }
 
-        if (isOngoingCallStatus()) {
+        if (Utils.isAudioModeOngoingCall(mContext)) {
             // Ongoing call status, switch entry for media will be disabled.
             mPreference.setVisible(false);
             preference.setSummary(