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

Commit a96b63c9 authored by jiabin's avatar jiabin
Browse files

Cache sco type when connecting.

Cache sco type when connecting for later use. This can help keep
consistent sco type when communicating to the native audio server.

Bug: 305089560
Test: make phone call over BT
Change-Id: I8094691656ad6f2b1afcd5accfc2a176abb6a019
parent f862be37
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;
    }