Merge "Update SCO disconnection to MM audio early." into q-keystone-qcom-dev
diff --git a/src/com/android/bluetooth/a2dp/A2dpService.java b/src/com/android/bluetooth/a2dp/A2dpService.java
index 38ba2d6..d722e67 100755
--- a/src/com/android/bluetooth/a2dp/A2dpService.java
+++ b/src/com/android/bluetooth/a2dp/A2dpService.java
@@ -575,6 +575,7 @@
     private boolean connectionAllowedCheckMaxDevices(BluetoothDevice device) {
         int connected = 0;
         int tws_device = 0;
+
         // Count devices that are in the process of connecting or already connected
         synchronized (mBtA2dpLock) {
              for (A2dpStateMachine sm : mStateMachines.values()) {
@@ -597,14 +598,16 @@
         }
         Log.d(TAG,"connectionAllowedCheckMaxDevices connected = " + connected +
               "tws connected = " + tws_device);
-        synchronized (mVariableLock) {
-            if (mAdapterService != null &&  mAdapterService.isVendorIntfEnabled() &&
-                ((tws_device > 0) || mAdapterService.isTwsPlusDevice(device) ||
-                ((tws_device > 0) && connected == mMaxConnectedAudioDevices &&
-                !mAdapterService.isTwsPlusDevice(device)))) {
-                return isConnectionAllowed(device, tws_device, connected);
+        synchronized (mBtA2dpLock) {
+            Log.d(TAG, "Going to acquire mVariableLock");
+            synchronized (mVariableLock) {
+                if (mAdapterService != null &&  mAdapterService.isVendorIntfEnabled() &&
+                    ((tws_device > 0) || mAdapterService.isTwsPlusDevice(device))) {
+                    return isConnectionAllowed(device, tws_device, connected);
+                }
             }
         }
+
         if (mSetMaxConnectedAudioDevices == 1 &&
             connected == mSetMaxConnectedAudioDevices) {
             disconnectExisting = true;
diff --git a/src/com/android/bluetooth/btservice/AdapterService.java b/src/com/android/bluetooth/btservice/AdapterService.java
index 2f0d6ab..562a32e 100644
--- a/src/com/android/bluetooth/btservice/AdapterService.java
+++ b/src/com/android/bluetooth/btservice/AdapterService.java
@@ -1789,10 +1789,6 @@
             if (service == null) {
                 return false;
             }
-            if ((getState() == BluetoothAdapter.STATE_BLE_ON) ||
-                (getState() == BluetoothAdapter.STATE_BLE_TURNING_ON)) {
-                service.onBrEdrDown();
-            }
             return service.factoryReset();
         }
 
diff --git a/src/com/android/bluetooth/hfp/HeadsetService.java b/src/com/android/bluetooth/hfp/HeadsetService.java
index 421ec73..4488dc8 100755
--- a/src/com/android/bluetooth/hfp/HeadsetService.java
+++ b/src/com/android/bluetooth/hfp/HeadsetService.java
@@ -129,8 +129,6 @@
     private boolean mIsTwsPlusEnabled = false;
     private boolean mIsTwsPlusShoEnabled = false;
     private vendorhfservice  mVendorHf;
-    private Context mContext = null;
-    private AudioServerStateCallback mServerStateCallback = new AudioServerStateCallback();
 
     @Override
     public IProfileServiceBinder initBinder() {
@@ -229,11 +227,6 @@
             mVendorHf.enableSwb(isSwbEnabled());
         }
 
-        Log.d(TAG, "registering audio server state callback");
-        mContext = getApplicationContext();
-        Executor exec = mContext.getMainExecutor();
-        mSystemInterface.getAudioManager().setAudioServerStateCallback(exec, mServerStateCallback);
-
         Log.i(TAG, " HeadsetService Started ");
         return true;
     }
@@ -432,30 +425,6 @@
         }
     }
 
-    private class AudioServerStateCallback extends AudioManager.AudioServerStateCallback {
-        @Override
-        public void onAudioServerDown() {
-            Log.d(TAG, "notifying onAudioServerDown");
-        }
-
-        @Override
-        public void onAudioServerUp() {
-            Log.d(TAG, "notifying onAudioServerUp");
-            if (isAudioOn()) {
-                Log.d(TAG, "onAudioServerUp: Audio is On, Notify HeadsetStateMachine");
-                synchronized (mStateMachines) {
-                    for (HeadsetStateMachine stateMachine : mStateMachines.values()) {
-                        if (stateMachine.getAudioState()
-                                == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
-                            stateMachine.onAudioServerUp();
-                            break;
-                        }
-                    }
-                }
-            }
-        }
-    }
-
     private final BroadcastReceiver mHeadsetReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
diff --git a/src/com/android/bluetooth/hfp/HeadsetStateMachine.java b/src/com/android/bluetooth/hfp/HeadsetStateMachine.java
index 8f44525..d8a64be 100644
--- a/src/com/android/bluetooth/hfp/HeadsetStateMachine.java
+++ b/src/com/android/bluetooth/hfp/HeadsetStateMachine.java
@@ -1592,13 +1592,16 @@
     class MyAudioServerStateCallback extends AudioManager.AudioServerStateCallback {
         @Override
         public void onAudioServerDown() {
-            logi("onAudioServerDown");
+            Log.i(TAG, "onAudioServerDown");
         }
 
         @Override
         public void onAudioServerUp() {
-            logi("onAudioServerUp restoring audio parameters");
+            Log.i(TAG, "onAudioSeverUp: restoring audio parameters");
+            mSystemInterface.getAudioManager().setBluetoothScoOn(false);
+            mSystemInterface.getAudioManager().setParameters("A2dpSuspended=true");
             setAudioParameters();
+            mSystemInterface.getAudioManager().setBluetoothScoOn(true);
         }
     }
 
@@ -1921,14 +1924,6 @@
         return true;
     }
 
-    public void onAudioServerUp() {
-        Log.i(TAG, "onAudioSeverUp: restore audio parameters");
-        mSystemInterface.getAudioManager().setBluetoothScoOn(false);
-        mSystemInterface.getAudioManager().setParameters("A2dpSuspended=true");
-        setAudioParameters();
-        mSystemInterface.getAudioManager().setBluetoothScoOn(true);
-    }
-
     /*
      * Put the AT command, company ID, arguments, and device in an Intent and broadcast it.
      */