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

This commit is contained in:
Mike Lockwood
2009-03-24 21:08:33 -07:00
committed by The Android Open Source Project
parent 4087f5425c
commit a0de355ce0

View File

@@ -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,8 +375,23 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
}
private synchronized void onSinkDisconnected(String path) {
mAudioManager.setBluetoothA2dpOn(false);
updateState(path, BluetoothA2dp.STATE_DISCONNECTED);
// 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) {
@@ -432,6 +459,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);
@@ -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);