Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a0de355c authored by Mike Lockwood's avatar Mike Lockwood Committed by The Android Open Source Project
Browse files

Automated import from //branches/master/...@141912,141912

parent 4087f542
Loading
Loading
Loading
Loading
+38 −3
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
    private static final String BLUETOOTH_ENABLED = "bluetooth_enabled";

    private static final int MESSAGE_CONNECT_TO = 1;
    private static final int MESSAGE_DISCONNECT = 2;

    private final Context mContext;
    private final IntentFilter mIntentFilter;
@@ -65,6 +66,11 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
    private final AudioManager mAudioManager;
    private final BluetoothDevice mBluetooth;

    // list of disconnected sinks to process after a delay
    private final ArrayList<String> mPendingDisconnects = new ArrayList<String>();
    // number of active sinks
    private int mSinkCount = 0; 

    private class SinkState {
        public String address;
        public int state;
@@ -164,6 +170,9 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
                    connectSink(address);
                }
                break;
            case MESSAGE_DISCONNECT:
                handleDeferredDisconnect((String)msg.obj);
                break;
            }
        }
    };
@@ -338,6 +347,9 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
    }

    private synchronized void onSinkConnected(String path) {
        // if we are reconnected, do not process previous disconnect event.
        mPendingDisconnects.remove(path);

        if (mAudioDevices == null) return;
        // bluez 3.36 quietly disconnects the previous sink when a new sink
        // is connected, so we need to mark all previously connected sinks as
@@ -363,9 +375,24 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
    }

    private synchronized void onSinkDisconnected(String path) {
        // This is to work around a problem in bluez that results 
        // sink disconnect events being sent, immediately followed by a reconnect.
        // To avoid unnecessary audio routing changes, we defer handling
        // sink disconnects until after a short delay.
        mPendingDisconnects.add(path);
        Message msg = Message.obtain(mHandler, MESSAGE_DISCONNECT, path);
        mHandler.sendMessageDelayed(msg, 2000);
    }

    private synchronized void handleDeferredDisconnect(String path) {
        if (mPendingDisconnects.contains(path)) {
            mPendingDisconnects.remove(path);
            if (mSinkCount == 1) {
                mAudioManager.setBluetoothA2dpOn(false);
            }
            updateState(path, BluetoothA2dp.STATE_DISCONNECTED);
        }
    }

    private synchronized void onSinkPlaying(String path) {
        updateState(path, BluetoothA2dp.STATE_PLAYING);
@@ -433,6 +460,13 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
        if (state != prevState) {
            if (DBG) log("state " + address + " (" + path + ") " + prevState + "->" + state);
            
            // keep track of the number of active sinks
            if (prevState == BluetoothA2dp.STATE_DISCONNECTED) {
                mSinkCount++;
            } else if (state == BluetoothA2dp.STATE_DISCONNECTED) {
                mSinkCount--;
            }

            Intent intent = new Intent(BluetoothA2dp.SINK_STATE_CHANGED_ACTION);
            intent.putExtra(BluetoothIntent.ADDRESS, address);
            intent.putExtra(BluetoothA2dp.SINK_PREVIOUS_STATE, prevState);
@@ -441,7 +475,8 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {

            if ((prevState == BluetoothA2dp.STATE_CONNECTED ||
                 prevState == BluetoothA2dp.STATE_PLAYING) &&
                    (state != BluetoothA2dp.STATE_CONNECTED &&
                    (state != BluetoothA2dp.STATE_CONNECTING &&
                     state != BluetoothA2dp.STATE_CONNECTED &&
                     state != BluetoothA2dp.STATE_PLAYING)) {
                // disconnected
                intent = new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY);