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));
+        }
+    }
 }