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

Commit 66cf562d authored by William Escande's avatar William Escande
Browse files

VolumeControl: remove duplicate Callback code

Both mCallback and the notified callback implement the same code.
But instead of an instance, it was duplicated.

Bug: 324492914
Test: m Bluetooth | There isn't remote callback test
Flag: Exempt, mechanical no-op
Change-Id: I90eecd0bae715d90b89813712353ac38c09562bc
parent 9d90ad6b
Loading
Loading
Loading
Loading
+51 −115
Original line number Diff line number Diff line
@@ -165,16 +165,19 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose

    @SuppressLint("AndroidFrameworkBluetoothPermission")
    private final IBluetoothVolumeControlCallback mCallback =
            new IBluetoothVolumeControlCallback.Stub() {
            new VolumeControlNotifyCallback(mCallbackExecutorMap);

                private void notify(Consumer<BluetoothVolumeControl.Callback> consumer) {
                    synchronized (mCallbackExecutorMap) {
                        for (Map.Entry<BluetoothVolumeControl.Callback, Executor> entry :
                                mCallbackExecutorMap.entrySet()) {
                            BluetoothVolumeControl.Callback callback = entry.getKey();
                            Executor executor = entry.getValue();
                            executor.execute(() -> consumer.accept(callback));
    private class VolumeControlNotifyCallback extends IBluetoothVolumeControlCallback.Stub {
        private final Map<Callback, Executor> mCallbackMap;

        VolumeControlNotifyCallback(Map<Callback, Executor> callbackMap) {
            mCallbackMap = callbackMap;
        }

        private void forEach(Consumer<BluetoothVolumeControl.Callback> consumer) {
            synchronized (mCallbackMap) {
                mCallbackMap.forEach(
                        (callback, executor) -> executor.execute(() -> consumer.accept(callback)));
            }
        }

@@ -184,10 +187,10 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
            Attributable.setAttributionSource(device, mAttributionSource);

            if (instanceId == 1) {
                        notify((cb) -> cb.onVolumeOffsetChanged(device, volumeOffset));
                forEach((cb) -> cb.onVolumeOffsetChanged(device, volumeOffset));
            }
            if (Flags.leaudioMultipleVocsInstancesApi()) {
                        notify((cb) -> cb.onVolumeOffsetChanged(device, instanceId, volumeOffset));
                forEach((cb) -> cb.onVolumeOffsetChanged(device, instanceId, volumeOffset));
            }
        }

@@ -195,7 +198,7 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
        public void onVolumeOffsetAudioLocationChanged(
                @NonNull BluetoothDevice device, int instanceId, int audioLocation) {
            Attributable.setAttributionSource(device, mAttributionSource);
                    notify(
            forEach(
                    (cb) ->
                            cb.onVolumeOffsetAudioLocationChanged(
                                    device, instanceId, audioLocation));
@@ -205,7 +208,7 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
        public void onVolumeOffsetAudioDescriptionChanged(
                @NonNull BluetoothDevice device, int instanceId, String audioDescription) {
            Attributable.setAttributionSource(device, mAttributionSource);
                    notify(
            forEach(
                    (cb) ->
                            cb.onVolumeOffsetAudioDescriptionChanged(
                                    device, instanceId, audioDescription));
@@ -214,9 +217,9 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
        @Override
        public void onDeviceVolumeChanged(@NonNull BluetoothDevice device, int volume) {
            Attributable.setAttributionSource(device, mAttributionSource);
                    notify((cb) -> cb.onDeviceVolumeChanged(device, volume));
            forEach((cb) -> cb.onDeviceVolumeChanged(device, volume));
        }
    }
            };

    /**
     * Intent used to broadcast the change in connection state of the Volume Control profile.
@@ -475,74 +478,7 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
                    service.registerCallback(mCallback, mAttributionSource, recv);
                } else {
                    service.notifyNewRegisteredCallback(
                            new IBluetoothVolumeControlCallback.Stub() {
                                @Override
                                public void onVolumeOffsetChanged(
                                        BluetoothDevice device, int instanceId, int volumeOffset)
                                        throws RemoteException {
                                    Attributable.setAttributionSource(device, mAttributionSource);

                                    // The old API operates on the first instance only
                                    if (instanceId == 1) {
                                        try {
                                            executor.execute(
                                                    () ->
                                                            callback.onVolumeOffsetChanged(
                                                                    device, volumeOffset));
                                        } finally {
                                            // As this deprecated callback, might not be
                                            // defined; continue
                                        }
                                    }
                                    if (Flags.leaudioMultipleVocsInstancesApi()) {
                                        executor.execute(
                                                () ->
                                                        callback.onVolumeOffsetChanged(
                                                                device, instanceId, volumeOffset));
                                    }
                                }

                                @Override
                                public void onVolumeOffsetAudioLocationChanged(
                                        BluetoothDevice device, int instanceId, int location)
                                        throws RemoteException {
                                    if (Flags.leaudioMultipleVocsInstancesApi()) {
                                        Attributable.setAttributionSource(
                                                device, mAttributionSource);
                                        executor.execute(
                                                () ->
                                                        callback.onVolumeOffsetAudioLocationChanged(
                                                                device, instanceId, location));
                                    }
                                }

                                @Override
                                public void onVolumeOffsetAudioDescriptionChanged(
                                        BluetoothDevice device,
                                        int instanceId,
                                        String audioDescription)
                                        throws RemoteException {
                                    if (Flags.leaudioMultipleVocsInstancesApi()) {
                                        Attributable.setAttributionSource(
                                                device, mAttributionSource);
                                        executor.execute(
                                                () ->
                                                        callback
                                                                .onVolumeOffsetAudioDescriptionChanged(
                                                                        device,
                                                                        instanceId,
                                                                        audioDescription));
                                    }
                                }

                                @Override
                                public void onDeviceVolumeChanged(
                                        BluetoothDevice device, int volume) throws RemoteException {
                                    Attributable.setAttributionSource(device, mAttributionSource);
                                    executor.execute(
                                            () -> callback.onDeviceVolumeChanged(device, volume));
                                }
                            },
                            new VolumeControlNotifyCallback(Map.of(callback, executor)),
                            mAttributionSource,
                            recv);
                }