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

Commit 55eaa785 authored by Henri Chataing's avatar Henri Chataing
Browse files

ActiveDeviceManager: Factorize logic around LE Audio active device

See https://r.android.com/3338420 for original change.

Bug: 367917846
Flag: EXEMPT, no logical change
Test: atest ActiveDeviceManagerTest
Change-Id: I6e568191afa8ab430b6ba54b7aa40b5e7165ca79
parent e1acd8fb
Loading
Loading
Loading
Loading
+40 −42
Original line number Diff line number Diff line
@@ -593,6 +593,41 @@ public class ActiveDeviceManager implements AdapterService.BluetoothStateCallbac
        }
    }

    /**
     * Update the LE Audio active device following a change of (dual mode compatible) active device
     * in a classic audio profile such as A2DP or HFP.
     *
     * @param previousActiveDevice previous active device of the classic profile
     * @param nextActiveDevice current active device of the classic profile
     */
    private void updateLeAudioActiveDeviceIfDualMode(
            @Nullable BluetoothDevice previousActiveDevice,
            @Nullable BluetoothDevice nextActiveDevice) {
        if (!Utils.isDualModeAudioEnabled()) {
            return;
        }

        if (nextActiveDevice != null) {
            boolean isDualModeDevice =
                    mAdapterService.isAllSupportedClassicAudioProfilesActive(nextActiveDevice);
            if (isDualModeDevice) {
                // If the active device for a classic audio profile is changed
                // to a dual mode compatible device, then also update the
                // active device for LE Audio.
                setLeAudioActiveDevice(nextActiveDevice);
            }
        } else {
            boolean wasDualModeDevice =
                    mAdapterService.isAllSupportedClassicAudioProfilesActive(previousActiveDevice);
            if (wasDualModeDevice) {
                // If the active device for a classic audio profile was a
                // dual mode compatible device, then also update the
                // active device for LE Audio.
                setLeAudioActiveDevice(null, true);
            }
        }
    }

    /**
     * Handles the active device logic for when the A2DP active device changes. Does the following:
     * 1. Clear the active hearing aid. 2. If dual mode is enabled and all supported classic audio
@@ -614,26 +649,9 @@ public class ActiveDeviceManager implements AdapterService.BluetoothStateCallbac
                if (device != null) {
                    setHearingAidActiveDevice(null, true);
                }

                if (Utils.isDualModeAudioEnabled()) {
                    if (device != null) {
                        boolean isDualModeDevice =
                                mAdapterService.isAllSupportedClassicAudioProfilesActive(device);
                        if (isDualModeDevice) {
                            setLeAudioActiveDevice(device);
                        }
                    } else {
                        boolean wasDualModeDevice =
                                mAdapterService.isAllSupportedClassicAudioProfilesActive(
                                        mA2dpActiveDevice);
                        if (wasDualModeDevice) {
                            // remove LE audio active device when it was actived as dual mode device
                            // before
                            setLeAudioActiveDevice(null, true);
                        }
                    }
                }
                updateLeAudioActiveDeviceIfDualMode(mA2dpActiveDevice, device);
            }

            // Just assign locally the new value
            mA2dpActiveDevice = device;

@@ -696,29 +714,9 @@ public class ActiveDeviceManager implements AdapterService.BluetoothStateCallbac
                    setHearingAidActiveDevice(null, true);
                }

                if (Utils.isDualModeAudioEnabled()) {
                    if (device != null) {
                        boolean isDualModeDevice =
                                mAdapterService.isAllSupportedClassicAudioProfilesActive(device);
                        if (isDualModeDevice) {
                            setLeAudioActiveDevice(device);
                        }
                    } else {
                        boolean wasDualModeDevice =
                                mAdapterService.isAllSupportedClassicAudioProfilesActive(
                                        mA2dpActiveDevice);
                        if (wasDualModeDevice) {
                            // remove LE audio active device when it was actived as dual mode device
                            // before
                            setLeAudioActiveDevice(null, true);
                        }
                updateLeAudioActiveDeviceIfDualMode(mHfpActiveDevice, device);

                        Log.d(TAG, "HFP active device is null. Try to fallback to le audio device");
                        synchronized (mLock) {
                            setFallbackDeviceActiveLocked(null);
                        }
                    }
                } else {
                if (!Utils.isDualModeAudioEnabled() || device == null) {
                    Log.d(TAG, "HFP active device is null. Try to fallback to le audio device");
                    synchronized (mLock) {
                        setFallbackDeviceActiveLocked(null);