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

Commit b4374980 authored by Jiabin Huang's avatar Jiabin Huang Committed by Android (Google) Code Review
Browse files

Merge "Cache sco type when connecting." into main

parents 01fc16e6 a96b63c9
Loading
Loading
Loading
Loading
+22 −2
Original line number Diff line number Diff line
@@ -44,7 +44,9 @@ import com.android.server.utils.EventLogger;

import java.io.PrintWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
@@ -66,6 +68,8 @@ public class BtHelper {

    // Bluetooth headset device
    private @Nullable BluetoothDevice mBluetoothHeadsetDevice;
    private final Map<BluetoothDevice, AudioDeviceAttributes> mResolvedScoAudioDevices =
            new HashMap<>();

    private @Nullable BluetoothHearingAid mHearingAid;

@@ -590,7 +594,16 @@ public class BtHelper {
        if (mBluetoothHeadsetDevice == null) {
            return null;
        }
        return btHeadsetDeviceToAudioDevice(mBluetoothHeadsetDevice);
        return getHeadsetAudioDevice(mBluetoothHeadsetDevice);
    }

    private @NonNull AudioDeviceAttributes getHeadsetAudioDevice(BluetoothDevice btDevice) {
        AudioDeviceAttributes deviceAttr = mResolvedScoAudioDevices.get(btDevice);
        if (deviceAttr != null) {
            // Returns the cached device attributes so that it is consistent as the previous one.
            return deviceAttr;
        }
        return btHeadsetDeviceToAudioDevice(btDevice);
    }

    private static AudioDeviceAttributes btHeadsetDeviceToAudioDevice(BluetoothDevice btDevice) {
@@ -648,6 +661,13 @@ public class BtHelper {
        result = mDeviceBroker.handleDeviceConnection(new AudioDeviceAttributes(
                        inDevice, audioDevice.getAddress(), audioDevice.getName()),
                isActive, btDevice) && result;
        if (result) {
            if (isActive) {
                mResolvedScoAudioDevices.put(btDevice, audioDevice);
            } else {
                mResolvedScoAudioDevices.remove(btDevice);
            }
        }
        return result;
    }