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

Commit ca3aa2a5 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Clean up Settings to allow a2dp, hfp, and le audio to be simultaneously...

Merge "Clean up Settings to allow a2dp, hfp, and le audio to be simultaneously connected on the same device"
parents 993f582f b1e25950
Loading
Loading
Loading
Loading
+35 −57
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.SystemProperties;
import android.provider.DeviceConfig;
import android.text.TextUtils;
import android.util.Log;
@@ -66,6 +67,9 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
    @VisibleForTesting
    static final String HIGH_QUALITY_AUDIO_PREF_TAG = "A2dpProfileHighQualityAudio";

    private static final String ENABLE_DUAL_MODE_AUDIO =
            "persist.bluetooth.enable_dual_mode_audio";

    private LocalBluetoothManager mManager;
    private LocalBluetoothProfileManager mProfileManager;
    private CachedBluetoothDevice mCachedDevice;
@@ -118,41 +122,18 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll

    /**
     * Refreshes the state for an existing SwitchPreference for a profile.
     * If the LeAudio profile is enabled on the LeAudio devices, then the SwitchPreferences of
     * A2dp profile and Headset profile are graied out.
     */
    private void refreshProfilePreference(SwitchPreference profilePref,
            LocalBluetoothProfile profile) {
        BluetoothDevice device = mCachedDevice.getDevice();
        boolean isLeAudioEnabled = isLeAudioEnabled();
        if (profile instanceof A2dpProfile
                || profile instanceof HeadsetProfile) {
            if (isLeAudioEnabled) {
                // If the LeAudio profile is enabled on the LeAudio devices, then the
                // SwitchPreferences of A2dp profile and Headset profile are grayed out.
                Log.d(TAG, "LE is enabled, gray out " + profile.toString());
                profilePref.setEnabled(false);
            } else {
        if (profile instanceof A2dpProfile || profile instanceof HeadsetProfile
                || profile instanceof LeAudioProfile) {
            List<CachedBluetoothDevice> deviceList = mProfileDeviceMap.get(
                    profile.toString());
            boolean isBusy = deviceList != null
                    && deviceList.stream().anyMatch(item -> item.isBusy());
            profilePref.setEnabled(!isBusy);
            }
        } else if (profile instanceof LeAudioProfile) {
            List<CachedBluetoothDevice> leAudioDeviceList = mProfileDeviceMap.get(
                    profile.toString());
            boolean isBusy = leAudioDeviceList != null
                    && leAudioDeviceList.stream().anyMatch(item -> item.isBusy());
            if (isLeAudioEnabled && !isBusy) {
                LocalBluetoothProfile a2dp = mProfileManager.getA2dpProfile();
                LocalBluetoothProfile headset = mProfileManager.getHeadsetProfile();
                // If the LeAudio profile is enabled on the LeAudio devices, then the
                // SwitchPreferences of A2dp profile and Headset profile are graied out.
                grayOutPreferenceWhenLeAudioIsEnabled(a2dp);
                grayOutPreferenceWhenLeAudioIsEnabled(headset);
            }
            profilePref.setEnabled(!isBusy);
        } else if (profile instanceof PbapServerProfile
                && isLeAudioEnabled
                && !mIsLeContactSharingEnabled) {
@@ -183,7 +164,7 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
                    highQualityPref.setVisible(true);
                    highQualityPref.setTitle(a2dp.getHighQualityAudioOptionLabel(device));
                    highQualityPref.setChecked(a2dp.isHighQualityAudioEnabled(device));
                    highQualityPref.setEnabled(!mCachedDevice.isBusy() && !isLeAudioEnabled);
                    highQualityPref.setEnabled(!mCachedDevice.isBusy());
                } else {
                    highQualityPref.setVisible(false);
                }
@@ -205,16 +186,6 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
        return false;
    }

    private void grayOutPreferenceWhenLeAudioIsEnabled(LocalBluetoothProfile profile) {
        if (profile != null) {
            SwitchPreference pref = mProfilesContainer.findPreference(profile.toString());
            if (pref != null) {
                Log.d(TAG, "LE is enabled, gray out " + profile.toString());
                pref.setEnabled(false);
            }
        }
    }

    /**
     * Helper method to enable a profile for a device.
     */
@@ -321,6 +292,17 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
        if (mapPermission != BluetoothDevice.ACCESS_UNKNOWN) {
            result.add(mapProfile);
        }

        // Removes phone calls & media audio toggles for dual mode devices
        boolean leAudioSupported = result.contains(
                mManager.getProfileManager().getLeAudioProfile());
        boolean classicAudioSupported = result.contains(
                mManager.getProfileManager().getA2dpProfile()) || result.contains(
                mManager.getProfileManager().getHeadsetProfile());
        if (leAudioSupported && classicAudioSupported) {
            result.remove(mManager.getProfileManager().getA2dpProfile());
            result.remove(mManager.getProfileManager().getHeadsetProfile());
        }
        Log.d(TAG, "getProfiles:result:" + result);
        return result;
    }
@@ -340,10 +322,7 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
    }

    /**
     * When user disable the Le Audio profile, the system needs to do two things.
     * 1) Disable the Le Audio profile, VCP and CSIP for each of the Le Audio devices.
     * 2) Enable the A2dp profile and Headset profile for the associated device. The system
     * can't enable the A2dp profile and Headset profile if the Le Audio profile is enabled.
     * Disable the Le Audio profile, VCP, and CSIP for each of the Le Audio devices.
     *
     * @param profile the LeAudio profile
     */
@@ -354,8 +333,6 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
        }
        LocalBluetoothProfile vcp = mProfileManager.getVolumeControlProfile();
        LocalBluetoothProfile csip = mProfileManager.getCsipSetCoordinatorProfile();
        LocalBluetoothProfile a2dp = mProfileManager.getA2dpProfile();
        LocalBluetoothProfile headset = mProfileManager.getHeadsetProfile();

        for (CachedBluetoothDevice leAudioDevice : mProfileDeviceMap.get(profile.toString())) {
            Log.d(TAG,
@@ -369,15 +346,15 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
            }
        }

        enableProfileAfterUserDisablesLeAudio(a2dp);
        enableProfileAfterUserDisablesLeAudio(headset);
        if (!SystemProperties.getBoolean(ENABLE_DUAL_MODE_AUDIO, false)) {
            Log.i(TAG, "Enabling classic audio profiles because dual mode is disabled");
            enableProfileAfterUserDisablesLeAudio(mProfileManager.getA2dpProfile());
            enableProfileAfterUserDisablesLeAudio(mProfileManager.getHeadsetProfile());
        }
    }

    /**
     * When user enable the Le Audio profile, the system needs to do two things.
     * 1) Disable the A2dp profile and Headset profile for the associated device. The system
     * can't enable the Le Audio if the A2dp profile and Headset profile are enabled.
     * 2) Enable the Le Audio profile, VCP and CSIP for each of the Le Audio devices.
     * Enable the Le Audio profile, VCP, and CSIP for each of the Le Audio devices.
     *
     * @param profile the LeAudio profile
     */
@@ -386,14 +363,15 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
            Log.e(TAG, "There is no the LE profile or no device in mProfileDeviceMap. Do nothing.");
            return;
        }
        LocalBluetoothProfile a2dp = mProfileManager.getA2dpProfile();
        LocalBluetoothProfile headset = mProfileManager.getHeadsetProfile();
        LocalBluetoothProfile vcp = mProfileManager.getVolumeControlProfile();
        LocalBluetoothProfile csip = mProfileManager.getCsipSetCoordinatorProfile();

        disableProfileBeforeUserEnablesLeAudio(a2dp);
        disableProfileBeforeUserEnablesLeAudio(headset);
        if (!SystemProperties.getBoolean(ENABLE_DUAL_MODE_AUDIO, false)) {
            Log.i(TAG, "Disabling classic audio profiles because dual mode is disabled");
            disableProfileBeforeUserEnablesLeAudio(mProfileManager.getA2dpProfile());
            disableProfileBeforeUserEnablesLeAudio(mProfileManager.getHeadsetProfile());
        }

        LocalBluetoothProfile vcp = mProfileManager.getVolumeControlProfile();
        LocalBluetoothProfile csip = mProfileManager.getCsipSetCoordinatorProfile();
        for (CachedBluetoothDevice leAudioDevice : mProfileDeviceMap.get(profile.toString())) {
            Log.d(TAG,
                    "User enable LE device: " + leAudioDevice.getDevice().getAnonymizedAddress());