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

Commit 14ac9300 authored by Pavlin Radoslavov's avatar Pavlin Radoslavov Committed by android-build-merger
Browse files

Merge "Refactor usage of synchronized inside A2dpService"

am: 05d1eb97

Change-Id: I0b64abccb11c896741dbb4e85bec0e7e89503e09
parents e1967d59 05d1eb97
Loading
Loading
Loading
Loading
+77 −72
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.media.AudioManager;
import android.os.HandlerThread;
import android.os.ParcelUuid;
import android.provider.Settings;
import android.support.annotation.GuardedBy;
import android.support.annotation.VisibleForTesting;
import android.util.Log;

@@ -64,10 +65,10 @@ public class A2dpService extends ProfileService {
    @VisibleForTesting
    A2dpNativeInterface mA2dpNativeInterface;
    private AudioManager mAudioManager;

    private A2dpCodecConfig mA2dpCodecConfig;
    private BluetoothDevice mActiveDevice;

    @GuardedBy("mStateMachines")
    private BluetoothDevice mActiveDevice;
    private final ConcurrentMap<BluetoothDevice, A2dpStateMachine> mStateMachines =
            new ConcurrentHashMap<>();

@@ -293,10 +294,9 @@ public class A2dpService extends ProfileService {

    List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        synchronized (mStateMachines) {
        List<BluetoothDevice> devices = new ArrayList<>();
        Set<BluetoothDevice> bondedDevices = mAdapter.getBondedDevices();

        synchronized (mStateMachines) {
            for (BluetoothDevice device : bondedDevices) {
                ParcelUuid[] featureUuids = device.getUuids();
                if (!BluetoothUuid.isUuidPresent(featureUuids, BluetoothUuid.AudioSink)) {
@@ -334,8 +334,9 @@ public class A2dpService extends ProfileService {
     * @param device the active device
     * @return true on success, otherwise false
     */
    public synchronized boolean setActiveDevice(BluetoothDevice device) {
    public boolean setActiveDevice(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission");
        synchronized (mStateMachines) {
            BluetoothDevice previousActiveDevice = mActiveDevice;
            if (DBG) {
                Log.d(TAG, "setActiveDevice(" + device + "): previous is " + previousActiveDevice);
@@ -347,15 +348,13 @@ public class A2dpService extends ProfileService {
                if (previousActiveDevice != null) {
                    // Make sure the Audio Manager knows the previous Active device is disconnected
                    mAudioManager.setBluetoothA2dpDeviceConnectionState(
                        previousActiveDevice,
                        BluetoothProfile.STATE_DISCONNECTED,
                            previousActiveDevice, BluetoothProfile.STATE_DISCONNECTED,
                            BluetoothProfile.A2DP);
                }
                return true;
            }

            BluetoothCodecStatus codecStatus = null;
        synchronized (mStateMachines) {
            A2dpStateMachine sm = mStateMachines.get(device);
            if (sm == null) {
                Log.e(TAG, "setActiveDevice(" + device + "): Cannot set as active: "
@@ -372,7 +371,6 @@ public class A2dpService extends ProfileService {
                return false;
            }
            codecStatus = sm.getCodecStatus();
        }

            boolean deviceChanged = !Objects.equals(device, mActiveDevice);
            mActiveDevice = device;
@@ -390,12 +388,14 @@ public class A2dpService extends ProfileService {
                            BluetoothProfile.A2DP, true);
                }
                mAudioManager.setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent(
                    mActiveDevice, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP, true);
                        mActiveDevice, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP,
                        true);
                // Inform the Audio Service about the codec configuration
                // change, so the Audio Service can reset accordingly the audio
                // feeding parameters in the Audio HAL to the Bluetooth stack.
                mAudioManager.handleBluetoothA2dpDeviceConfigChange(mActiveDevice);
            }
        }
        return true;
    }

@@ -404,14 +404,18 @@ public class A2dpService extends ProfileService {
     *
     * @return the active device or null if no device is active
     */
    public synchronized BluetoothDevice getActiveDevice() {
    public BluetoothDevice getActiveDevice() {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        synchronized (mStateMachines) {
            return mActiveDevice;
        }
    }

    private synchronized boolean isActiveDevice(BluetoothDevice device) {
    private boolean isActiveDevice(BluetoothDevice device) {
        synchronized (mStateMachines) {
            return (device != null) && Objects.equals(device, mActiveDevice);
        }
    }

    public boolean setPriority(BluetoothDevice device, int priority) {
        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission");
@@ -454,7 +458,7 @@ public class A2dpService extends ProfileService {
        }
    }

    synchronized boolean isA2dpPlaying(BluetoothDevice device) {
    boolean isA2dpPlaying(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        if (DBG) {
            Log.d(TAG, "isA2dpPlaying(" + device + ")");
@@ -481,13 +485,13 @@ public class A2dpService extends ProfileService {
        if (DBG) {
            Log.d(TAG, "getCodecStatus(" + device + ")");
        }
        synchronized (mStateMachines) {
            if (device == null) {
                device = mActiveDevice;
            }
            if (device == null) {
                return null;
            }
        synchronized (mStateMachines) {
            A2dpStateMachine sm = mStateMachines.get(device);
            if (sm != null) {
                return sm.getCodecStatus();
@@ -751,11 +755,11 @@ public class A2dpService extends ProfileService {
        }
    }

    private synchronized void connectionStateChanged(BluetoothDevice device, int fromState,
                                                     int toState) {
    private void connectionStateChanged(BluetoothDevice device, int fromState, int toState) {
        if ((device == null) || (fromState == toState)) {
            return;
        }
        synchronized (mStateMachines) {
            if (toState == BluetoothProfile.STATE_CONNECTED) {
                // Each time a device connects, we want to re-check if it supports optional
                // codecs (perhaps it's had a firmware update, etc.) and save that state if
@@ -771,6 +775,7 @@ public class A2dpService extends ProfileService {
                setActiveDevice(null);
            }
        }
    }

    // Update codec support per device when device is (re)connected.
    private class ConnectionStateChangedReceiver extends BroadcastReceiver {