@@ -392,6 +392,10 @@ public void onError(int error) {
392392
393393 private int mDeviceOrientation = Configuration .ORIENTATION_UNDEFINED ;
394394
395+ // Request to override default use of A2DP for media.
396+ private boolean mBluetoothA2dpEnabled ;
397+ private final Object mBluetoothA2dpEnabledLock = new Object ();
398+
395399 ///////////////////////////////////////////////////////////////////////////
396400 // Construction
397401 ///////////////////////////////////////////////////////////////////////////
@@ -481,6 +485,7 @@ public AudioService(Context context) {
481485
482486 mMasterVolumeRamp = context .getResources ().getIntArray (
483487 com .android .internal .R .array .config_masterVolumeRamp );
488+
484489 }
485490
486491 private void createAudioSystemThread () {
@@ -1651,6 +1656,21 @@ public boolean isBluetoothScoOn() {
16511656 return (mForcedUseForComm == AudioSystem .FORCE_BT_SCO );
16521657 }
16531658
1659+ /** @see AudioManager#setBluetoothA2dpOn() */
1660+ public void setBluetoothA2dpOn (boolean on ) {
1661+ if (!checkAudioSettingsPermission ("setBluetoothA2dpOn()" )) {
1662+ return ;
1663+ }
1664+ setBluetoothA2dpOnInt (on );
1665+ }
1666+
1667+ /** @see AudioManager#isBluetoothA2dpOn() */
1668+ public boolean isBluetoothA2dpOn () {
1669+ synchronized (mBluetoothA2dpEnabledLock ) {
1670+ return mBluetoothA2dpEnabled ;
1671+ }
1672+ }
1673+
16541674 /** @see AudioManager#startBluetoothSco() */
16551675 public void startBluetoothSco (IBinder cb ){
16561676 if (!checkAudioSettingsPermission ("startBluetoothSco()" ) ||
@@ -1673,6 +1693,7 @@ public void stopBluetoothSco(IBinder cb){
16731693 }
16741694 }
16751695
1696+
16761697 private class ScoClient implements IBinder .DeathRecipient {
16771698 private IBinder mCb ; // To be notified of client's death
16781699 private int mCreatorPid ;
@@ -2894,6 +2915,11 @@ public void handleMessage(Message msg) {
28942915 setOrientationForAudioSystem ();
28952916 }
28962917
2918+ synchronized (mBluetoothA2dpEnabledLock ) {
2919+ AudioSystem .setForceUse (AudioSystem .FOR_MEDIA ,
2920+ mBluetoothA2dpEnabled ?
2921+ AudioSystem .FORCE_NONE : AudioSystem .FORCE_NO_BT_A2DP );
2922+ }
28972923 // indicate the end of reconfiguration phase to audio HAL
28982924 AudioSystem .setParameters ("restarting=false" );
28992925 break ;
@@ -2976,6 +3002,9 @@ public void onChange(boolean selfChange) {
29763002
29773003 // must be called synchronized on mConnectedDevices
29783004 private void makeA2dpDeviceAvailable (String address ) {
3005+ // enable A2DP before notifying A2DP connection to avoid unecessary processing in
3006+ // audio policy manager
3007+ setBluetoothA2dpOnInt (true );
29793008 AudioSystem .setDeviceConnectionState (AudioSystem .DEVICE_OUT_BLUETOOTH_A2DP ,
29803009 AudioSystem .DEVICE_STATE_AVAILABLE ,
29813010 address );
@@ -3177,7 +3206,15 @@ public void onReceive(Context context, Intent intent) {
31773206 } else {
31783207 device = AudioSystem .DEVICE_OUT_WIRED_HEADPHONE ;
31793208 }
3209+ // enable A2DP before notifying headset disconnection to avoid glitches
3210+ if (state == 0 ) {
3211+ setBluetoothA2dpOnInt (true );
3212+ }
31803213 handleDeviceConnection ((state == 1 ), device , "" );
3214+ // disable A2DP after notifying headset connection to avoid glitches
3215+ if (state != 0 ) {
3216+ setBluetoothA2dpOnInt (false );
3217+ }
31813218 } else if (action .equals (Intent .ACTION_ANALOG_AUDIO_DOCK_PLUG )) {
31823219 state = intent .getIntExtra ("state" , 0 );
31833220 Log .v (TAG , "Broadcast Receiver: Got ACTION_ANALOG_AUDIO_DOCK_PLUG, state = " +state );
@@ -4655,6 +4692,17 @@ private void setOrientationForAudioSystem() {
46554692 }
46564693
46574694
4695+ // Handles request to override default use of A2DP for media.
4696+ public void setBluetoothA2dpOnInt (boolean on ) {
4697+ synchronized (mBluetoothA2dpEnabledLock ) {
4698+ mBluetoothA2dpEnabled = on ;
4699+ sendMsg (mAudioHandler , MSG_SET_FORCE_USE , SENDMSG_QUEUE ,
4700+ AudioSystem .FOR_MEDIA ,
4701+ mBluetoothA2dpEnabled ? AudioSystem .FORCE_NONE : AudioSystem .FORCE_NO_BT_A2DP ,
4702+ null , 0 );
4703+ }
4704+ }
4705+
46584706 @ Override
46594707 public void setRingtonePlayer (IRingtonePlayer player ) {
46604708 mContext .enforceCallingOrSelfPermission (REMOTE_AUDIO_PLAYBACK , null );
0 commit comments