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

Commit 0aa792c9 authored by William Escande's avatar William Escande
Browse files

Avrcp: call audioManager on non-Blocking thread

AudioManager has implemented setDeviceVolumeBehavior as a blocking call
In order to make sure we do not block Bluetooth, we need to make the
call in an async way.

Bug: 324420709
Flag: Exempt, mandatory fix to land unflaggable aosp/3009913
Test: atest pts-bot:AVRCP
Change-Id: Ibf1cf8602d15b9329c10f340f741737908ae9c11
parent bd6c0d1a
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -88,6 +88,8 @@ import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public final class Utils {
@@ -111,6 +113,9 @@ public final class Utils {
    static final int BD_ADDR_LEN = 6; // bytes
    static final int BD_UUID_LEN = 16; // bytes

    /** Thread pool to handle background and outgoing blocking task */
    public static final ExecutorService BackgroundExecutor = Executors.newSingleThreadExecutor();

    /*
     * Special character
     *
+22 −5
Original line number Diff line number Diff line
@@ -29,11 +29,13 @@ import android.media.AudioManager;
import android.util.Log;

import com.android.bluetooth.BluetoothEventLogger;
import com.android.bluetooth.Utils;
import com.android.internal.annotations.VisibleForTesting;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

class AvrcpVolumeManager extends AudioDeviceCallback {
    public static final String TAG = AvrcpVolumeManager.class.getSimpleName();
@@ -78,11 +80,26 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
    private void switchVolumeDevice(@NonNull BluetoothDevice device) {
        // Inform the audio manager that the device has changed
        d("switchVolumeDevice: Set Absolute volume support to " + mDeviceMap.get(device));
        mAudioManager.setDeviceVolumeBehavior(new AudioDeviceAttributes(
                    AudioDeviceAttributes.ROLE_OUTPUT, AudioDeviceInfo.TYPE_BLUETOOTH_A2DP,
                    device.getAddress()),
                 mDeviceMap.get(device) ? AudioManager.DEVICE_VOLUME_BEHAVIOR_ABSOLUTE
                 : AudioManager.DEVICE_VOLUME_BEHAVIOR_VARIABLE);
        final AudioDeviceAttributes deviceAttributes =
                new AudioDeviceAttributes(
                        AudioDeviceAttributes.ROLE_OUTPUT,
                        AudioDeviceInfo.TYPE_BLUETOOTH_A2DP,
                        device.getAddress());
        final int deviceVolumeBehavior =
                mDeviceMap.get(device)
                        ? AudioManager.DEVICE_VOLUME_BEHAVIOR_ABSOLUTE
                        : AudioManager.DEVICE_VOLUME_BEHAVIOR_VARIABLE;

        CompletableFuture.runAsync(
                        () ->
                                mAudioManager.setDeviceVolumeBehavior(
                                        deviceAttributes, deviceVolumeBehavior),
                        Utils.BackgroundExecutor)
                .exceptionally(
                        e -> {
                            Log.e(TAG, "switchVolumeDevice has thrown an Exception", e);
                            return null;
                        });

        // Get the current system volume and try to get the preference volume
        int savedVolume = getVolume(device, sNewDeviceVolume);