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

Commit 78a3681b authored by William Escande's avatar William Escande
Browse files

Database: Reduce scope of the cache lock

We do not need to lock the cache when checking parameter.
This is no-op change that will help if there is any unseen deadlock. It
also just a good practice to not lock when we do not need it

Bug: 310837813
Test: atest DatabaseManagerTest | current test should still pass
Change-Id: Ifa029c35196bfe0da3daa83e3b229d9048e92518
parent ee38aead
Loading
Loading
Loading
Loading
+102 −100
Original line number Diff line number Diff line
@@ -265,7 +265,6 @@ public class DatabaseManager {
     * Get customized metadata from database with requested key
     */
    public byte[] getCustomMeta(BluetoothDevice device, int key) {
        synchronized (mMetadataCache) {
        if (device == null) {
            Log.e(TAG, "getCustomMeta: device is null");
            return null;
@@ -277,6 +276,7 @@ public class DatabaseManager {

        String address = device.getAddress();

        synchronized (mMetadataCache) {
            if (!mMetadataCache.containsKey(address)) {
                Log.d(TAG, "getCustomMeta: device " + device + " is not in cache");
                return null;
@@ -293,13 +293,13 @@ public class DatabaseManager {
    @VisibleForTesting
    public boolean setAudioPolicyMetadata(BluetoothDevice device,
            BluetoothSinkAudioPolicy policies) {
        synchronized (mMetadataCache) {
        if (device == null) {
            Log.e(TAG, "setAudioPolicyMetadata: device is null");
            return false;
        }

        String address = device.getAddress();
        synchronized (mMetadataCache) {
            if (!mMetadataCache.containsKey(address)) {
                createMetadata(address, false);
            }
@@ -319,7 +319,6 @@ public class DatabaseManager {
     */
    @VisibleForTesting
    public BluetoothSinkAudioPolicy getAudioPolicyMetadata(BluetoothDevice device) {
        synchronized (mMetadataCache) {
        if (device == null) {
            Log.e(TAG, "getAudioPolicyMetadata: device is null");
            return null;
@@ -327,6 +326,7 @@ public class DatabaseManager {

        String address = device.getAddress();

        synchronized (mMetadataCache) {
            if (!mMetadataCache.containsKey(address)) {
                Log.d(TAG, "getAudioPolicyMetadata: device " + device + " is not in cache");
                return null;
@@ -362,7 +362,6 @@ public class DatabaseManager {
    @VisibleForTesting
    public boolean setProfileConnectionPolicy(BluetoothDevice device, int profile,
            int newConnectionPolicy) {
        synchronized (mMetadataCache) {
        if (device == null) {
            Log.e(TAG, "setProfileConnectionPolicy: device is null");
            return false;
@@ -371,12 +370,15 @@ public class DatabaseManager {
        if (newConnectionPolicy != BluetoothProfile.CONNECTION_POLICY_UNKNOWN
                && newConnectionPolicy != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN
                && newConnectionPolicy != BluetoothProfile.CONNECTION_POLICY_ALLOWED) {
                Log.e(TAG, "setProfileConnectionPolicy: invalid connection policy "
                        + newConnectionPolicy);
            Log.e(
                    TAG,
                    "setProfileConnectionPolicy: invalid connection policy " + newConnectionPolicy);
            return false;
        }

        String address = device.getAddress();

        synchronized (mMetadataCache) {
            if (!mMetadataCache.containsKey(address)) {
                if (newConnectionPolicy == BluetoothProfile.CONNECTION_POLICY_UNKNOWN) {
                    return true;
@@ -421,7 +423,6 @@ public class DatabaseManager {
     * {@link BluetoothProfile.CONNECTION_POLICY_ALLOWED}
     */
    public int getProfileConnectionPolicy(BluetoothDevice device, int profile) {
        synchronized (mMetadataCache) {
        if (device == null) {
            Log.e(TAG, "getProfileConnectionPolicy: device is null");
            return BluetoothProfile.CONNECTION_POLICY_UNKNOWN;
@@ -429,6 +430,7 @@ public class DatabaseManager {

        String address = device.getAddress();

        synchronized (mMetadataCache) {
            if (!mMetadataCache.containsKey(address)) {
                Log.d(TAG, "getProfileConnectionPolicy: device " + device.getAnonymizedAddress()
                        + " is not in cache");
@@ -456,7 +458,6 @@ public class DatabaseManager {
     */
    @VisibleForTesting
    public void setA2dpSupportsOptionalCodecs(BluetoothDevice device, int newValue) {
        synchronized (mMetadataCache) {
        if (device == null) {
            Log.e(TAG, "setA2dpOptionalCodec: device is null");
            return;
@@ -470,6 +471,7 @@ public class DatabaseManager {

        String address = device.getAddress();

        synchronized (mMetadataCache) {
            if (!mMetadataCache.containsKey(address)) {
                return;
            }
@@ -498,7 +500,6 @@ public class DatabaseManager {
    @VisibleForTesting
    @OptionalCodecsSupportStatus
    public int getA2dpSupportsOptionalCodecs(BluetoothDevice device) {
        synchronized (mMetadataCache) {
        if (device == null) {
            Log.e(TAG, "setA2dpOptionalCodec: device is null");
            return BluetoothA2dp.OPTIONAL_CODECS_SUPPORT_UNKNOWN;
@@ -506,6 +507,7 @@ public class DatabaseManager {

        String address = device.getAddress();

        synchronized (mMetadataCache) {
            if (!mMetadataCache.containsKey(address)) {
                Log.d(TAG, "getA2dpOptionalCodec: device " + device + " is not in cache");
                return BluetoothA2dp.OPTIONAL_CODECS_SUPPORT_UNKNOWN;
@@ -527,7 +529,6 @@ public class DatabaseManager {
     */
    @VisibleForTesting
    public void setA2dpOptionalCodecsEnabled(BluetoothDevice device, int newValue) {
        synchronized (mMetadataCache) {
        if (device == null) {
            Log.e(TAG, "setA2dpOptionalCodecEnabled: device is null");
            return;
@@ -541,6 +542,7 @@ public class DatabaseManager {

        String address = device.getAddress();

        synchronized (mMetadataCache) {
            if (!mMetadataCache.containsKey(address)) {
                return;
            }
@@ -569,14 +571,13 @@ public class DatabaseManager {
    @VisibleForTesting
    @OptionalCodecsPreferenceStatus
    public int getA2dpOptionalCodecsEnabled(BluetoothDevice device) {
        synchronized (mMetadataCache) {
        if (device == null) {
            Log.e(TAG, "getA2dpOptionalCodecEnabled: device is null");
            return BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN;
        }

        String address = device.getAddress();

        synchronized (mMetadataCache) {
            if (!mMetadataCache.containsKey(address)) {
                Log.d(TAG, "getA2dpOptionalCodecEnabled: device " + device + " is not in cache");
                return BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN;
@@ -978,22 +979,25 @@ public class DatabaseManager {
     * @return a Bundle containing the preferred audio profiles
     */
    public Bundle getPreferredAudioProfiles(BluetoothDevice device) {
        synchronized (mMetadataCache) {
        if (device == null) {
            Log.e(TAG, "getPreferredAudioProfiles: device is null");
            throw new IllegalArgumentException("getPreferredAudioProfiles: device is null");
        }

        String address = device.getAddress();
        final int outputOnlyProfile;
        final int duplexProfile;

        synchronized (mMetadataCache) {
            if (!mMetadataCache.containsKey(address)) {
                return Bundle.EMPTY;
            }

            // Gets the preferred audio profiles for each audio mode
            Metadata metadata = mMetadataCache.get(address);
            int outputOnlyProfile = metadata.preferred_output_only_profile;
            int duplexProfile = metadata.preferred_duplex_profile;
            outputOnlyProfile = metadata.preferred_output_only_profile;
            duplexProfile = metadata.preferred_duplex_profile;
        }

        // Checks if the default values are present (aka no explicit preference)
        if (outputOnlyProfile == 0 && duplexProfile == 0) {
@@ -1002,8 +1006,7 @@ public class DatabaseManager {

        Bundle modeToProfileBundle = new Bundle();
        if (outputOnlyProfile != 0) {
                modeToProfileBundle.putInt(
                        BluetoothAdapter.AUDIO_MODE_OUTPUT_ONLY, outputOnlyProfile);
            modeToProfileBundle.putInt(BluetoothAdapter.AUDIO_MODE_OUTPUT_ONLY, outputOnlyProfile);
        }
        if (duplexProfile != 0) {
            modeToProfileBundle.putInt(BluetoothAdapter.AUDIO_MODE_DUPLEX, duplexProfile);
@@ -1011,7 +1014,6 @@ public class DatabaseManager {

        return modeToProfileBundle;
    }
    }

    /**
     * Get the {@link Looper} for the handler thread. This is used in testing and helper