import com.android.internal.annotations.GuardedBy;
import java.io.PrintWriter;
-import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Called when the active state of audio player is changed.
*
- * @param config The audio playback configuration for the audio player of which active state
- * was changed. If {@param isRemoved} is {@code true}, this hold outdated
- * information.
+ * @param config The audio playback configuration for the audio player for which active
+ * state was changed. If {@param isRemoved} is {@code true}, this holds
+ * outdated information.
* @param isRemoved {@code true} if the audio player is removed.
*/
void onAudioPlayerActiveStateChanged(
private final OnAudioPlayerActiveStateChangedListener mListener;
- public MessageHandler(Looper looper, OnAudioPlayerActiveStateChangedListener listener) {
+ MessageHandler(Looper looper, OnAudioPlayerActiveStateChangedListener listener) {
super(looper);
mListener = listener;
}
}
}
- public void sendAudioPlayerActiveStateChangedMessage(
+ void sendAudioPlayerActiveStateChangedMessage(
final AudioPlaybackConfiguration config, final boolean isRemoved) {
obtainMessage(MSG_AUDIO_PLAYER_ACTIVE_STATE_CHANGED,
isRemoved ? 1 : 0, 0 /* unused */, config).sendToTarget();
private final Map<OnAudioPlayerActiveStateChangedListener, MessageHandler> mListenerMap =
new ArrayMap<>();
@GuardedBy("mLock")
- private final Set<Integer> mActiveAudioUids = new ArraySet();
+ private final Set<Integer> mActiveAudioUids = new ArraySet<>();
@GuardedBy("mLock")
private ArrayMap<Integer, AudioPlaybackConfiguration> mPrevActiveAudioPlaybackConfigs =
new ArrayMap<>();
private final IAudioService mAudioService;
private final AudioPlayerStateMonitor mAudioPlayerStateMonitor;
private final Handler mHandler = new Handler();
- private final AudioRoutesInfo mCurAudioRoutesInfo = new AudioRoutesInfo();
+ private final AudioRoutesInfo mAudioRoutesInfo = new AudioRoutesInfo();
private final IntArray mActivePlayerMinPriorityQueue = new IntArray();
private final IntArray mActivePlayerUidMinPriorityQueue = new IntArray();
} else {
mHandler.postDelayed(mRestoreBluetoothA2dpRunnable, WAIT_MS);
if (DEBUG) {
- Slog.d(TAG, "onAudioPlayerACTIVEStateChanged: " + "uid=" + uid
+ Slog.d(TAG, "onAudioPlayerActiveStateChanged: " + "uid=" + uid
+ ", active=" + active + ", delaying");
}
}
@Override
public void dispatchAudioRoutesChanged(final AudioRoutesInfo newRoutes) {
synchronized (mLock) {
- if (newRoutes.mainType != mCurAudioRoutesInfo.mainType) {
+ if (newRoutes.mainType != mAudioRoutesInfo.mainType) {
if ((newRoutes.mainType & (AudioRoutesInfo.MAIN_HEADSET
| AudioRoutesInfo.MAIN_HEADPHONES
| AudioRoutesInfo.MAIN_USB)) == 0) {
// headset was plugged in.
mGlobalBluetoothA2dpOn = false;
}
- mCurAudioRoutesInfo.mainType = newRoutes.mainType;
+ mAudioRoutesInfo.mainType = newRoutes.mainType;
}
if (!TextUtils.equals(
- newRoutes.bluetoothName, mCurAudioRoutesInfo.bluetoothName)) {
+ newRoutes.bluetoothName, mAudioRoutesInfo.bluetoothName)) {
if (newRoutes.bluetoothName == null) {
// BT was disconnected.
mGlobalBluetoothA2dpOn = false;
// BT was connected or changed.
mGlobalBluetoothA2dpOn = true;
}
- mCurAudioRoutesInfo.bluetoothName = newRoutes.bluetoothName;
+ mAudioRoutesInfo.bluetoothName = newRoutes.bluetoothName;
}
+ // Although a Bluetooth device is connected before a new audio playback is
+ // started, dispatchAudioRoutChanged() can be called after
+ // onAudioPlayerActiveStateChanged(). That causes restoreBluetoothA2dp()
+ // is called before mGlobalBluetoothA2dpOn is updated.
+ // Calling restoreBluetoothA2dp() here could prevent that.
+ restoreBluetoothA2dp();
}
}
});
void restoreBluetoothA2dp() {
try {
+ boolean btConnected = false;
boolean a2dpOn = false;
synchronized (mLock) {
+ btConnected = mAudioRoutesInfo.bluetoothName != null;
a2dpOn = mGlobalBluetoothA2dpOn;
}
- Slog.v(TAG, "restoreBluetoothA2dp(" + a2dpOn + ")");
- mAudioService.setBluetoothA2dpOn(a2dpOn);
+ // We don't need to change a2dp status when bluetooth is not connected.
+ if (btConnected) {
+ Slog.v(TAG, "restoreBluetoothA2dp(" + a2dpOn + ")");
+ mAudioService.setBluetoothA2dpOn(a2dpOn);
+ }
} catch (RemoteException e) {
Slog.w(TAG, "RemoteException while calling setBluetoothA2dpOn.");
}