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

Commit ee7cb720 authored by William Escande's avatar William Escande
Browse files

VolumeControl: Enforce synchronized annotation

Also remove double flagging usage (both flagged in the service and in the framework)

Bug: 324492914
Test: m Bluetooth | Existing test
Flag: Exempt, mechanical no-op
Change-Id: Ib5090517f505926d21c9819fa8510c41a2931dcc
parent 628c911e
Loading
Loading
Loading
Loading
+40 −29
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.util.CloseGuard;
import android.util.Log;

import com.android.bluetooth.flags.Flags;
import com.android.internal.annotations.GuardedBy;
import com.android.modules.utils.SynchronousResultReceiver;

import java.util.ArrayList;
@@ -64,6 +65,8 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    private static final boolean VDBG = false;

    private CloseGuard mCloseGuard;

    @GuardedBy("mCallbackExecutorMap")
    private final Map<Callback, Executor> mCallbackExecutorMap = new HashMap<>();

    /**
@@ -166,16 +169,20 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
                public void onVolumeOffsetChanged(
                        @NonNull BluetoothDevice device, int instanceId, int volumeOffset) {
                    Attributable.setAttributionSource(device, mAttributionSource);
                    synchronized (mCallbackExecutorMap) {
                        for (Map.Entry<BluetoothVolumeControl.Callback, Executor>
                                callbackExecutorEntry : mCallbackExecutorMap.entrySet()) {
                        BluetoothVolumeControl.Callback callback = callbackExecutorEntry.getKey();
                            BluetoothVolumeControl.Callback callback =
                                    callbackExecutorEntry.getKey();
                            Executor executor = callbackExecutorEntry.getValue();

                            // The old API operates on the first instance only
                            if (instanceId == 1) {
                                try {
                                    executor.execute(
                                        () -> callback.onVolumeOffsetChanged(device, volumeOffset));
                                            () ->
                                                    callback.onVolumeOffsetChanged(
                                                            device, volumeOffset));
                                } finally {
                                    // As this deprecated callback, might not be defined; continue
                                }
@@ -188,12 +195,13 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
                            }
                        }
                    }
                }

                @Override
                public void onVolumeOffsetAudioLocationChanged(
                        @NonNull BluetoothDevice device, int instanceId, int audioLocation) {
                    if (Flags.leaudioMultipleVocsInstancesApi()) {
                    Attributable.setAttributionSource(device, mAttributionSource);
                    synchronized (mCallbackExecutorMap) {
                        for (Map.Entry<BluetoothVolumeControl.Callback, Executor>
                                callbackExecutorEntry : mCallbackExecutorMap.entrySet()) {
                            BluetoothVolumeControl.Callback callback =
@@ -210,8 +218,8 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
                @Override
                public void onVolumeOffsetAudioDescriptionChanged(
                        @NonNull BluetoothDevice device, int instanceId, String audioDescription) {
                    if (Flags.leaudioMultipleVocsInstancesApi()) {
                    Attributable.setAttributionSource(device, mAttributionSource);
                    synchronized (mCallbackExecutorMap) {
                        for (Map.Entry<BluetoothVolumeControl.Callback, Executor>
                                callbackExecutorEntry : mCallbackExecutorMap.entrySet()) {
                            BluetoothVolumeControl.Callback callback =
@@ -228,13 +236,16 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
                @Override
                public void onDeviceVolumeChanged(@NonNull BluetoothDevice device, int volume) {
                    Attributable.setAttributionSource(device, mAttributionSource);
                    synchronized (mCallbackExecutorMap) {
                        for (Map.Entry<BluetoothVolumeControl.Callback, Executor>
                                callbackExecutorEntry : mCallbackExecutorMap.entrySet()) {
                        BluetoothVolumeControl.Callback callback = callbackExecutorEntry.getKey();
                            BluetoothVolumeControl.Callback callback =
                                    callbackExecutorEntry.getKey();
                            Executor executor = callbackExecutorEntry.getValue();
                            executor.execute(() -> callback.onDeviceVolumeChanged(device, volume));
                        }
                    }
                }
            };

    /**
@@ -602,11 +613,11 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
            if (mCallbackExecutorMap.remove(callback) == null) {
                throw new IllegalArgumentException("This callback has not been registered");
            }
        }

            if (!mCallbackExecutorMap.isEmpty()) {
                return;
            }
        }

        // If the callback map is empty, we unregister the service-to-app callback
        try {