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

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

HAP: Enable it by default if device support

Bug: 345826775
Bug: 343526503
Test: Manual: Pair an HAP device and check LeAudio toggle
Flag: com.android.bluetooth.flags.enable_hap_by_default
Change-Id: Id89b121248fe9454e2419a82dcbf710b721c0c34
parent 883e9dc2
Loading
Loading
Loading
Loading
+24 −7
Original line number Original line Diff line number Diff line
@@ -175,10 +175,7 @@ public class PhonePolicy implements AdapterService.BluetoothStateCallback {


    PhonePolicy(AdapterService service, ServiceFactory factory) {
    PhonePolicy(AdapterService service, ServiceFactory factory) {
        mAdapterService = service;
        mAdapterService = service;
        mDatabaseManager =
        mDatabaseManager = Objects.requireNonNull(service.getDatabase());
                Objects.requireNonNull(
                        mAdapterService.getDatabase(),
                        "DatabaseManager cannot be null when PhonePolicy starts");
        mFactory = factory;
        mFactory = factory;
        mHandler = new PhonePolicyHandler(service.getMainLooper());
        mHandler = new PhonePolicyHandler(service.getMainLooper());
        mAutoConnectProfilesSupported =
        mAutoConnectProfilesSupported =
@@ -285,6 +282,23 @@ public class PhonePolicy implements AdapterService.BluetoothStateCallback {
        return isLeAudioOnlyGroup(device);
        return isLeAudioOnlyGroup(device);
    }
    }


    // return true if device support Hearing Access Service and it has not been manually disabled
    private boolean shouldEnableHapByDefault(BluetoothDevice device, ParcelUuid[] uuids) {
        if (!Flags.enableHapByDefault()) {
            Log.i(TAG, "shouldDefaultToHap: Flag enableHapByDefault is disabled");
            return false;
        }

        HapClientService hap = mFactory.getHapClientService();
        if (hap == null) {
            Log.e(TAG, "shouldDefaultToHap: HapClient is null");
            return false;
        }

        return Utils.arrayContains(uuids, BluetoothUuid.HAS)
                && hap.getConnectionPolicy(device) != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
    }

    // Policy implementation, all functions MUST be private
    // Policy implementation, all functions MUST be private
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
    private void processInitProfilePriorities(BluetoothDevice device, ParcelUuid[] uuids) {
    private void processInitProfilePriorities(BluetoothDevice device, ParcelUuid[] uuids) {
@@ -306,6 +320,7 @@ public class PhonePolicy implements AdapterService.BluetoothStateCallback {
                SystemProperties.getBoolean(BYPASS_LE_AUDIO_ALLOWLIST_PROPERTY, false);
                SystemProperties.getBoolean(BYPASS_LE_AUDIO_ALLOWLIST_PROPERTY, false);


        boolean isLeAudioOnly = isLeAudioOnlyDevice(device, uuids);
        boolean isLeAudioOnly = isLeAudioOnlyDevice(device, uuids);
        boolean shouldEnableHapByDefault = shouldEnableHapByDefault(device, uuids);
        boolean isLeAudioProfileAllowed =
        boolean isLeAudioProfileAllowed =
                (leAudioService != null)
                (leAudioService != null)
                        && Utils.arrayContains(uuids, BluetoothUuid.LE_AUDIO)
                        && Utils.arrayContains(uuids, BluetoothUuid.LE_AUDIO)
@@ -313,9 +328,9 @@ public class PhonePolicy implements AdapterService.BluetoothStateCallback {
                                != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN)
                                != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN)
                        && (mLeAudioEnabledByDefault || isDualModeAudioEnabled())
                        && (mLeAudioEnabledByDefault || isDualModeAudioEnabled())
                        && (isBypassLeAudioAllowlist
                        && (isBypassLeAudioAllowlist
                                || shouldEnableHapByDefault
                                || mAdapterService.isLeAudioAllowed(device)
                                || mAdapterService.isLeAudioAllowed(device)
                                || isLeAudioOnly);
                                || isLeAudioOnly);

        debugLog(
        debugLog(
                "mLeAudioEnabledByDefault: "
                "mLeAudioEnabledByDefault: "
                        + mLeAudioEnabledByDefault
                        + mLeAudioEnabledByDefault
@@ -328,7 +343,9 @@ public class PhonePolicy implements AdapterService.BluetoothStateCallback {
                        + ", isLeAudioProfileAllowed: "
                        + ", isLeAudioProfileAllowed: "
                        + isLeAudioProfileAllowed
                        + isLeAudioProfileAllowed
                        + ", isLeAudioOnly: "
                        + ", isLeAudioOnly: "
                        + isLeAudioOnly);
                        + isLeAudioOnly
                        + ", shouldEnableHapByDefault: "
                        + shouldEnableHapByDefault);


        // Set profile priorities only for the profiles discovered on the remote device.
        // Set profile priorities only for the profiles discovered on the remote device.
        // This avoids needless auto-connect attempts to profiles non-existent on the remote device
        // This avoids needless auto-connect attempts to profiles non-existent on the remote device
@@ -516,7 +533,7 @@ public class PhonePolicy implements AdapterService.BluetoothStateCallback {
                && (hearingAidService.getConnectionPolicy(device)
                && (hearingAidService.getConnectionPolicy(device)
                        == BluetoothProfile.CONNECTION_POLICY_UNKNOWN)) {
                        == BluetoothProfile.CONNECTION_POLICY_UNKNOWN)) {
            if (isLeAudioProfileAllowed) {
            if (isLeAudioProfileAllowed) {
                debugLog("LE Audio preferred over ASHA for device " + device);
                Log.i(TAG, "LE Audio preferred over ASHA for device " + device);
                mAdapterService
                mAdapterService
                        .getDatabase()
                        .getDatabase()
                        .setProfileConnectionPolicy(
                        .setProfileConnectionPolicy(