Register with AudioManager only once for AudioServer state

Register only once with AudioManager to know when
AudioServer is up/down. Registering twice leads to
an exception while moving to AudioOn state

CRs-Fixed: 2737162
Change-Id: Ic996c5588637b575a48f601216d1cfcb6b68eb02
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 9c0e9db..ceef453 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);
         }
     }
 
@@ -1919,14 +1922,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.
      */