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

Commit 7d7428c9 authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Revert "Suppress noisy intent when there is a fallback BT device"

This reverts commit 48808058.

Reason for revert: b/262155254

Bug: 262155254
Bug: 254569327
Test: atest BluetoothInstrumentationTests
Change-Id: I6f9deb1735df740f83d165fc6ccc038ce91ec7aa
parent 3b08f598
Loading
Loading
Loading
Loading
+26 −18
Original line number Diff line number Diff line
@@ -500,13 +500,32 @@ public class A2dpService extends ProfileService {
                previousActiveDevice = mActiveDevice;
            }

            int prevActiveConnectionState = getConnectionState(previousActiveDevice);

            // As per b/202602952, if we remove the active device due to a disconnection,
            // we need to check if another device is connected and set it active instead.
            // Calling this before any other active related calls has the same effect as
            // a classic active device switch.
            BluetoothDevice fallbackdevice = getFallbackDevice();
            if (fallbackdevice != null && prevActiveConnectionState
                    != BluetoothProfile.STATE_CONNECTED) {
                setActiveDevice(fallbackdevice);
                return;
            }

            // This needs to happen before we inform the audio manager that the device
            // disconnected. Please see comment in updateAndBroadcastActiveDevice() for why.
            updateAndBroadcastActiveDevice(null);

            // Make sure the Audio Manager knows the previous Active device is removed.
            // Make sure the Audio Manager knows the previous Active device is disconnected.
            // However, if A2DP is still connected and not forcing stop audio for that remote
            // device, the user has explicitly switched the output to the local device and music
            // should continue playing. Otherwise, the remote device has been indeed disconnected
            // and audio should be suspended before switching the output to the local device.
            boolean stopAudio = forceStopPlayingAudio || (prevActiveConnectionState
                        != BluetoothProfile.STATE_CONNECTED);
            mAudioManager.handleBluetoothActiveDeviceChanged(null, previousActiveDevice,
                    BluetoothProfileConnectionInfo.createA2dpInfo(!forceStopPlayingAudio, -1));
                    BluetoothProfileConnectionInfo.createA2dpInfo(!stopAudio, -1));

            synchronized (mStateMachines) {
                // Make sure the Active device in native layer is set to null and audio is off
@@ -550,22 +569,10 @@ public class A2dpService extends ProfileService {
     * @return true on success, otherwise false
     */
    public boolean setActiveDevice(BluetoothDevice device) {
        return setActiveDevice(device, false);
    }

    /**
     * Set the active device.
     *
     * @param device the active device
     * @param hasFallbackDevice whether it has fallback device when the {@code device}
     *                          is {@code null}.
     * @return true on success, otherwise false
     */
    public boolean setActiveDevice(BluetoothDevice device, boolean hasFallbackDevice) {
        synchronized (mActiveSwitchingGuard) {
            if (device == null) {
                // Remove active device and continue playing audio only if necessary.
                removeActiveDevice(!hasFallbackDevice);
                removeActiveDevice(false);
                return true;
            }

@@ -1252,9 +1259,10 @@ public class A2dpService extends ProfileService {
        if (toState == BluetoothProfile.STATE_CONNECTED && (mMaxConnectedAudioDevices == 1)) {
            setActiveDevice(device);
        }

        // When disconnected, ActiveDeviceManager will call setActiveDevice(null)

        // Check if the active device is not connected anymore
        if (isActiveDevice(device) && (fromState == BluetoothProfile.STATE_CONNECTED)) {
            setActiveDevice(null);
        }
        // Check if the device is disconnected - if unbond, remove the state machine
        if (toState == BluetoothProfile.STATE_DISCONNECTED) {
            if (mAdapterService.getBondState(device) == BluetoothDevice.BOND_NONE) {
+44 −64
Original line number Diff line number Diff line
@@ -279,7 +279,7 @@ class ActiveDeviceManager {
                            if (mPendingA2dpActiveDevice == null) {
                                // select the device as active if not lazy active
                                setA2dpActiveDevice(device);
                                setLeAudioActiveDevice(null, true);
                                setLeAudioActiveDevice(null);
                            }
                        }
                        break;
@@ -292,10 +292,12 @@ class ActiveDeviceManager {
                                    + "device " + device + " disconnected");
                        }
                        mA2dpConnectedDevices.remove(device);
                        if (Objects.equals(mA2dpActiveDevice, device)
                                && !setFallbackDeviceActive() && mA2dpConnectedDevices.isEmpty()) {
                        if (Objects.equals(mA2dpActiveDevice, device)) {
                            if (mA2dpConnectedDevices.isEmpty()) {
                                setA2dpActiveDevice(null);
                            }
                            setFallbackDeviceActive();
                        }
                    }
                }
                break;
@@ -310,7 +312,7 @@ class ActiveDeviceManager {
                    }
                    if (device != null && !Objects.equals(mA2dpActiveDevice, device)) {
                        setHearingAidActiveDevice(null);
                        setLeAudioActiveDevice(null, true);
                        setLeAudioActiveDevice(null);
                    }
                    if (mHfpConnectedDevices.contains(device)) {
                        setHfpActiveDevice(device);
@@ -377,10 +379,12 @@ class ActiveDeviceManager {
                                    + "device " + device + " disconnected");
                        }
                        mHfpConnectedDevices.remove(device);
                        if (Objects.equals(mHfpActiveDevice, device)
                                && !setFallbackDeviceActive() && mHfpConnectedDevices.isEmpty()) {
                        if (Objects.equals(mHfpActiveDevice, device)) {
                            if (mHfpConnectedDevices.isEmpty()) {
                                setHfpActiveDevice(null);
                            }
                            setFallbackDeviceActive();
                        }
                    }
                }
                break;
@@ -427,9 +431,9 @@ class ActiveDeviceManager {
                        mHearingAidConnectedDevices.add(device);
                        // New connected hearing aid device: select it as active
                        setHearingAidActiveDevice(device);
                        setA2dpActiveDevice(null, true);
                        setA2dpActiveDevice(null);
                        setHfpActiveDevice(null);
                        setLeAudioActiveDevice(null, true);
                        setLeAudioActiveDevice(null);
                        break;
                    }
                    if (prevState == BluetoothProfile.STATE_CONNECTED) {
@@ -439,11 +443,12 @@ class ActiveDeviceManager {
                                    + "_CHANGED): device " + device + " disconnected");
                        }
                        mHearingAidConnectedDevices.remove(device);
                        if (Objects.equals(mHearingAidActiveDevice, device)
                                && !setFallbackDeviceActive()
                                && mHearingAidConnectedDevices.isEmpty()) {
                        if (Objects.equals(mHearingAidActiveDevice, device)) {
                            if (mHearingAidConnectedDevices.isEmpty()) {
                                setHearingAidActiveDevice(null);
                            }
                            setFallbackDeviceActive();
                        }
                    }
                }
                break;
@@ -459,9 +464,9 @@ class ActiveDeviceManager {
                    // Just assign locally the new value
                    mHearingAidActiveDevice = device;
                    if (device != null) {
                        setA2dpActiveDevice(null, true);
                        setA2dpActiveDevice(null);
                        setHfpActiveDevice(null);
                        setLeAudioActiveDevice(null, true);
                        setLeAudioActiveDevice(null);
                    }
                }
                break;
@@ -476,17 +481,12 @@ class ActiveDeviceManager {
                        // Nothing has changed
                        break;
                    }
                    final LeAudioService leAudioService = mFactory.getLeAudioService();

                    if (nextState == BluetoothProfile.STATE_CONNECTED) {
                        // Device connected
                        if (DBG) {
                            Log.d(TAG, "handleMessage(MESSAGE_LE_AUDIO_ACTION_CONNECTION_STATE"
                                    + "_CHANGED): device " + device + " connected");
                        }
                        if (leAudioService != null && device != null) {
                            leAudioService.deviceConnected(device);
                        }
                        if (mLeAudioConnectedDevices.contains(device)) {
                            break;      // The device is already connected
                        }
@@ -495,12 +495,12 @@ class ActiveDeviceManager {
                                && mPendingLeHearingAidActiveDevice.isEmpty()) {
                            // New connected LE audio device: select it as active
                            setLeAudioActiveDevice(device);
                            setA2dpActiveDevice(null, true);
                            setA2dpActiveDevice(null);
                            setHfpActiveDevice(null);
                        } else if (mPendingLeHearingAidActiveDevice.contains(device)) {
                            setLeHearingAidActiveDevice(device);
                            setHearingAidActiveDevice(null);
                            setA2dpActiveDevice(null, true);
                            setA2dpActiveDevice(null);
                            setHfpActiveDevice(null);
                        }
                        break;
@@ -513,15 +513,11 @@ class ActiveDeviceManager {
                        }
                        mLeAudioConnectedDevices.remove(device);
                        mLeHearingAidConnectedDevices.remove(device);
                        boolean hasFallbackDevice = false;
                        if (Objects.equals(mLeAudioActiveDevice, device)) {
                            hasFallbackDevice = setFallbackDeviceActive();
                            if (!hasFallbackDevice && mLeAudioConnectedDevices.isEmpty()) {
                            if (mLeAudioConnectedDevices.isEmpty()) {
                                setLeAudioActiveDevice(null);
                            }
                        }
                        if (leAudioService != null && device != null) {
                            leAudioService.deviceDisconnected(device, hasFallbackDevice);
                            setFallbackDeviceActive();
                        }
                    }
                }
@@ -540,7 +536,7 @@ class ActiveDeviceManager {
                    }
                    // Just assign locally the new value
                    if (device != null && !Objects.equals(mLeAudioActiveDevice, device)) {
                        setA2dpActiveDevice(null, true);
                        setA2dpActiveDevice(null);
                        setHfpActiveDevice(null);
                        setHearingAidActiveDevice(null);
                    }
@@ -576,7 +572,7 @@ class ActiveDeviceManager {
                            // New connected LE hearing aid device: select it as active
                            setLeHearingAidActiveDevice(device);
                            setHearingAidActiveDevice(null);
                            setA2dpActiveDevice(null, true);
                            setA2dpActiveDevice(null);
                            setHfpActiveDevice(null);
                        }
                        break;
@@ -609,7 +605,7 @@ class ActiveDeviceManager {
                    }
                    // Just assign locally the new value
                    if (device != null && !Objects.equals(mLeHearingAidActiveDevice, device)) {
                        setA2dpActiveDevice(null, true);
                        setA2dpActiveDevice(null);
                        setHfpActiveDevice(null);
                        setHearingAidActiveDevice(null);
                    }
@@ -626,7 +622,7 @@ class ActiveDeviceManager {
                case AudioManager.MODE_NORMAL: {
                    if (mPendingA2dpActiveDevice != null) {
                        setA2dpActiveDevice(mPendingA2dpActiveDevice);
                        setLeAudioActiveDevice(null, true);
                        setLeAudioActiveDevice(null);
                    }
                    break;
                }
@@ -764,10 +760,6 @@ class ActiveDeviceManager {
    }

    private void setA2dpActiveDevice(BluetoothDevice device) {
        setA2dpActiveDevice(device, false);
    }

    private void setA2dpActiveDevice(BluetoothDevice device, boolean hasFallbackDevice) {
        if (DBG) {
            Log.d(TAG, "setA2dpActiveDevice(" + device + ")");
        }
@@ -775,7 +767,7 @@ class ActiveDeviceManager {
        if (a2dpService == null) {
            return;
        }
        if (!a2dpService.setActiveDevice(device, hasFallbackDevice)) {
        if (!a2dpService.setActiveDevice(device)) {
            return;
        }
        mA2dpActiveDevice = device;
@@ -805,10 +797,6 @@ class ActiveDeviceManager {
    }

    private void setHearingAidActiveDevice(BluetoothDevice device) {
        setHearingAidActiveDevice(device, false);
    }

    private void setHearingAidActiveDevice(BluetoothDevice device, boolean hasFallbackDevice) {
        if (DBG) {
            Log.d(TAG, "setHearingAidActiveDevice(" + device + ")");
        }
@@ -816,16 +804,13 @@ class ActiveDeviceManager {
        if (hearingAidService == null) {
            return;
        }
        if (!hearingAidService.setActiveDevice(device, hasFallbackDevice)) {
        if (!hearingAidService.setActiveDevice(device)) {
            return;
        }
        mHearingAidActiveDevice = device;
    }
    private void setLeAudioActiveDevice(BluetoothDevice device) {
        setLeAudioActiveDevice(device, false);
    }

    private void setLeAudioActiveDevice(BluetoothDevice device, boolean hasFallbackDevice) {
    private void setLeAudioActiveDevice(BluetoothDevice device) {
        if (DBG) {
            Log.d(TAG, "setLeAudioActiveDevice(" + device + ")");
        }
@@ -833,7 +818,7 @@ class ActiveDeviceManager {
        if (leAudioService == null) {
            return;
        }
        if (!leAudioService.setActiveDevice(device, hasFallbackDevice)) {
        if (!leAudioService.setActiveDevice(device)) {
            return;
        }
        mLeAudioActiveDevice = device;
@@ -844,9 +829,6 @@ class ActiveDeviceManager {
    }

    private void setLeHearingAidActiveDevice(BluetoothDevice device) {
        if (DBG) {
            Log.d(TAG, "setLeHearingAidActiveDevice(" + device + ")");
        }
        if (!Objects.equals(mLeAudioActiveDevice, device)) {
            setLeAudioActiveDevice(device);
        }
@@ -857,13 +839,13 @@ class ActiveDeviceManager {
        }
    }

    private boolean setFallbackDeviceActive() {
    private void setFallbackDeviceActive() {
        if (DBG) {
            Log.d(TAG, "setFallbackDeviceActive");
        }
        DatabaseManager dbManager = mAdapterService.getDatabase();
        if (dbManager == null) {
            return false;
            return;
        }
        List<BluetoothDevice> connectedHearingAidDevices = new ArrayList<>();
        if (!mHearingAidConnectedDevices.isEmpty()) {
@@ -881,19 +863,19 @@ class ActiveDeviceManager {
                        Log.d(TAG, "set hearing aid device active: " + device);
                    }
                    setHearingAidActiveDevice(device);
                    setA2dpActiveDevice(null, true);
                    setA2dpActiveDevice(null);
                    setHfpActiveDevice(null);
                    setLeAudioActiveDevice(null, true);
                    setLeAudioActiveDevice(null);
                } else {
                    if (DBG) {
                        Log.d(TAG, "set LE hearing aid device active: " + device);
                    }
                    setLeHearingAidActiveDevice(device);
                    setHearingAidActiveDevice(null, true);
                    setA2dpActiveDevice(null, true);
                    setHearingAidActiveDevice(null);
                    setA2dpActiveDevice(null);
                    setHfpActiveDevice(null);
                }
                return true;
                return;
            }
        }

@@ -937,14 +919,14 @@ class ActiveDeviceManager {
                    setA2dpActiveDevice(device);
                    if (headsetFallbackDevice != null) {
                        setHfpActiveDevice(device);
                        setLeAudioActiveDevice(null, true);
                        setLeAudioActiveDevice(null);
                    }
                } else {
                    if (DBG) {
                        Log.d(TAG, "set LE audio device active: " + device);
                    }
                    setLeAudioActiveDevice(device);
                    setA2dpActiveDevice(null, true);
                    setA2dpActiveDevice(null);
                    setHfpActiveDevice(null);
                }
            } else {
@@ -955,20 +937,18 @@ class ActiveDeviceManager {
                    setHfpActiveDevice(device);
                    if (a2dpFallbackDevice != null) {
                        setA2dpActiveDevice(a2dpFallbackDevice);
                        setLeAudioActiveDevice(null, true);
                        setLeAudioActiveDevice(null);
                    }
                } else {
                    if (DBG) {
                        Log.d(TAG, "set LE audio device active: " + device);
                    }
                    setLeAudioActiveDevice(device);
                    setA2dpActiveDevice(null, true);
                    setA2dpActiveDevice(null);
                    setHfpActiveDevice(null);
                }
            }
            return true;
        }
        return false;
    }

    private void resetState() {
+12 −16
Original line number Diff line number Diff line
@@ -585,24 +585,13 @@ public class HearingAidService extends ProfileService {
     * @return true on success, otherwise false
     */
    public boolean setActiveDevice(BluetoothDevice device) {
        return setActiveDevice(device, false);
    }

    /**
     * Set the active device.
     * @param device the new active device
     * @param hasFallbackDevice whether it has fallback device when the {@code device}
     *                          is {@code null}.
     * @return true on success, otherwise false
     */
    public boolean setActiveDevice(BluetoothDevice device, boolean hasFallbackDevice) {
        if (DBG) {
            Log.d(TAG, "setActiveDevice:" + device);
        }
        synchronized (mStateMachines) {
            if (device == null) {
                if (mActiveDeviceHiSyncId != BluetoothHearingAid.HI_SYNC_ID_INVALID) {
                    reportActiveDevice(null, hasFallbackDevice);
                    reportActiveDevice(null);
                    mActiveDeviceHiSyncId = BluetoothHearingAid.HI_SYNC_ID_INVALID;
                }
                return true;
@@ -621,7 +610,7 @@ public class HearingAidService extends ProfileService {
                    BluetoothHearingAid.HI_SYNC_ID_INVALID);
            if (deviceHiSyncId != mActiveDeviceHiSyncId) {
                mActiveDeviceHiSyncId = deviceHiSyncId;
                reportActiveDevice(device, false);
                reportActiveDevice(device);
            }
        }
        return true;
@@ -772,7 +761,7 @@ public class HearingAidService extends ProfileService {
     * Report the active device change to the active device manager and the media framework.
     * @param device the new active device; or null if no active device
     */
    private void reportActiveDevice(BluetoothDevice device, boolean hasFallbackDevice) {
    private void reportActiveDevice(BluetoothDevice device) {
        if (DBG) {
            Log.d(TAG, "reportActiveDevice(" + device + ")");
        }
@@ -783,7 +772,14 @@ public class HearingAidService extends ProfileService {
                BluetoothProfile.HEARING_AID, mAdapterService.obfuscateAddress(device),
                mAdapterService.getMetricId(device));

        boolean stopAudio = device == null && !hasFallbackDevice;
        Intent intent = new Intent(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
                | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        sendBroadcast(intent, BLUETOOTH_CONNECT, Utils.getTempAllowlistBroadcastOptions());

        boolean stopAudio = device == null
                && (getConnectionState(mPreviousAudioDevice) != BluetoothProfile.STATE_CONNECTED);
        if (DBG) {
            Log.d(TAG, "Hearing Aid audio: " + mPreviousAudioDevice + " -> " + device
                    + ". Stop audio: " + stopAudio);
@@ -899,7 +895,7 @@ public class HearingAidService extends ProfileService {
            }
        }
        if (fromState == BluetoothProfile.STATE_CONNECTED && getConnectedDevices().isEmpty()) {
            // When disconnected, ActiveDeviceManager will call setActiveDevice(null)
            setActiveDevice(null);
            long myHiSyncId = getHiSyncId(device);
            mHiSyncIdConnectedMap.put(myHiSyncId, false);
        }
+113 −108

File changed.

Preview size limit exceeded, changes collapsed.

+64 −66

File changed.

Preview size limit exceeded, changes collapsed.

Loading