AudioManager: restore setBluetoothA2dpOn() method

Remove deprecation on setBluetoothA2dpOn() method so that applications
can override the default audio policy which is to use A2DP
for media whenever connected.

The request is not persistent and the default policy is restored when a
new A2DP i ro wired headset connection occurs.

Bug 6485897.

Change-Id: I2a4b6b6bdba55f7b133e64f86d27c03eb86acfa4
diff --git a/media/java/android/media/AudioService.java b/media/java/android/media/AudioService.java
index da01c44..5e338ab 100644
--- a/media/java/android/media/AudioService.java
+++ b/media/java/android/media/AudioService.java
@@ -392,6 +392,10 @@
 
     private int mDeviceOrientation = Configuration.ORIENTATION_UNDEFINED;
 
+    // Request to override default use of A2DP for media.
+    private boolean mBluetoothA2dpEnabled;
+    private final Object mBluetoothA2dpEnabledLock = new Object();
+
     ///////////////////////////////////////////////////////////////////////////
     // Construction
     ///////////////////////////////////////////////////////////////////////////
@@ -481,6 +485,7 @@
 
         mMasterVolumeRamp = context.getResources().getIntArray(
                 com.android.internal.R.array.config_masterVolumeRamp);
+
     }
 
     private void createAudioSystemThread() {
@@ -1651,6 +1656,21 @@
         return (mForcedUseForComm == AudioSystem.FORCE_BT_SCO);
     }
 
+    /** @see AudioManager#setBluetoothA2dpOn() */
+    public void setBluetoothA2dpOn(boolean on) {
+        if (!checkAudioSettingsPermission("setBluetoothA2dpOn()")) {
+            return;
+        }
+        setBluetoothA2dpOnInt(on);
+    }
+
+    /** @see AudioManager#isBluetoothA2dpOn() */
+    public boolean isBluetoothA2dpOn() {
+        synchronized (mBluetoothA2dpEnabledLock) {
+            return mBluetoothA2dpEnabled;
+        }
+    }
+
     /** @see AudioManager#startBluetoothSco() */
     public void startBluetoothSco(IBinder cb){
         if (!checkAudioSettingsPermission("startBluetoothSco()") ||
@@ -1673,6 +1693,7 @@
         }
     }
 
+
     private class ScoClient implements IBinder.DeathRecipient {
         private IBinder mCb; // To be notified of client's death
         private int mCreatorPid;
@@ -2894,6 +2915,11 @@
                         setOrientationForAudioSystem();
                     }
 
+                    synchronized (mBluetoothA2dpEnabledLock) {
+                        AudioSystem.setForceUse(AudioSystem.FOR_MEDIA,
+                                mBluetoothA2dpEnabled ?
+                                        AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP);
+                    }
                     // indicate the end of reconfiguration phase to audio HAL
                     AudioSystem.setParameters("restarting=false");
                     break;
@@ -2976,6 +3002,9 @@
 
     // must be called synchronized on mConnectedDevices
     private void makeA2dpDeviceAvailable(String address) {
+        // enable A2DP before notifying A2DP connection to avoid unecessary processing in
+        // audio policy manager
+        setBluetoothA2dpOnInt(true);
         AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP,
                 AudioSystem.DEVICE_STATE_AVAILABLE,
                 address);
@@ -3177,7 +3206,15 @@
                 } else {
                     device = AudioSystem.DEVICE_OUT_WIRED_HEADPHONE;
                 }
+                // enable A2DP before notifying headset disconnection to avoid glitches
+                if (state == 0) {
+                    setBluetoothA2dpOnInt(true);
+                }
                 handleDeviceConnection((state == 1), device, "");
+                // disable A2DP after notifying headset connection to avoid glitches
+                if (state != 0) {
+                    setBluetoothA2dpOnInt(false);
+                }
             } else if (action.equals(Intent.ACTION_ANALOG_AUDIO_DOCK_PLUG)) {
                 state = intent.getIntExtra("state", 0);
                 Log.v(TAG, "Broadcast Receiver: Got ACTION_ANALOG_AUDIO_DOCK_PLUG, state = "+state);
@@ -4655,6 +4692,17 @@
     }
 
 
+    // Handles request to override default use of A2DP for media.
+    public void setBluetoothA2dpOnInt(boolean on) {
+        synchronized (mBluetoothA2dpEnabledLock) {
+            mBluetoothA2dpEnabled = on;
+            sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE,
+                    AudioSystem.FOR_MEDIA,
+                    mBluetoothA2dpEnabled ? AudioSystem.FORCE_NONE : AudioSystem.FORCE_NO_BT_A2DP,
+                    null, 0);
+        }
+    }
+
     @Override
     public void setRingtonePlayer(IRingtonePlayer player) {
         mContext.enforceCallingOrSelfPermission(REMOTE_AUDIO_PLAYBACK, null);