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

Commit 3b08f598 authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Revert "Refactor ActiveDeviceManager with isMediaMode"

This reverts commit 4c21415a.

Reason for revert: b/262155254

Bug: 262155254
Bug: 254569327
Test: atest BluetoothInstrumentationTests
Change-Id: Ic23977b52d7e6123f3282946f301a1d889bf613b
parent 6546570d
Loading
Loading
Loading
Loading
+76 −42
Original line number Diff line number Diff line
@@ -255,9 +255,27 @@ class ActiveDeviceManager {
                            // Lazy active A2DP if it is not being used.
                            // This will prevent the deactivation of LE audio
                            // earlier than the activation of HFP.
                            if (!isMediaMode(mAudioManager.getMode()) && mHfpActiveDevice == null) {
                            switch (mAudioManager.getMode()) {
                                case AudioManager.MODE_NORMAL:
                                    break;
                                case AudioManager.MODE_RINGTONE: {
                                    HeadsetService headsetService = mFactory.getHeadsetService();
                                    if (mHfpActiveDevice == null && headsetService != null
                                            && headsetService.isInbandRingingEnabled()) {
                                        mPendingA2dpActiveDevice = device;
                                    }
                                    break;
                                }
                                case AudioManager.MODE_IN_CALL:
                                case AudioManager.MODE_IN_COMMUNICATION:
                                case AudioManager.MODE_CALL_SCREENING:
                                case AudioManager.MODE_CALL_REDIRECT:
                                case AudioManager.MODE_COMMUNICATION_REDIRECT: {
                                    if (mHfpActiveDevice == null) {
                                        mPendingA2dpActiveDevice = device;
                                    }
                                }
                            }
                            if (mPendingA2dpActiveDevice == null) {
                                // select the device as active if not lazy active
                                setA2dpActiveDevice(device);
@@ -328,9 +346,21 @@ class ActiveDeviceManager {
                            // Lazy active HFP if it is not being used.
                            // This will prevent the deactivation of LE audio
                            // earlier than the activation of A2DP.
                            if (isMediaMode(mAudioManager.getMode()) && mA2dpActiveDevice == null) {
                            switch (mAudioManager.getMode()) {
                                case AudioManager.MODE_NORMAL:
                                    if (mA2dpActiveDevice == null) {
                                        mPendingHfpActiveDevice = device;
                                    }
                                    break;
                                case AudioManager.MODE_RINGTONE: {
                                    HeadsetService headsetService = mFactory.getHeadsetService();
                                    if (headsetService == null
                                            || !headsetService.isInbandRingingEnabled()) {
                                        mPendingHfpActiveDevice = device;
                                    }
                                    break;
                                }
                            }
                            if (mPendingHfpActiveDevice == null) {
                                // select the device as active if not lazy active
                                setHfpActiveDevice(device);
@@ -592,16 +622,34 @@ class ActiveDeviceManager {

    private class AudioManagerOnModeChangedListener implements AudioManager.OnModeChangedListener {
        public void onModeChanged(int mode) {
            if (isMediaMode(mode)) {
            switch (mode) {
                case AudioManager.MODE_NORMAL: {
                    if (mPendingA2dpActiveDevice != null) {
                        setA2dpActiveDevice(mPendingA2dpActiveDevice);
                        setLeAudioActiveDevice(null, true);
                    }
            } else {
                    break;
                }
                case AudioManager.MODE_RINGTONE: {
                    final HeadsetService headsetService = mFactory.getHeadsetService();
                    if (headsetService != null && headsetService.isInbandRingingEnabled()
                            && mPendingHfpActiveDevice != null) {
                        setHfpActiveDevice(mPendingHfpActiveDevice);
                        setLeAudioActiveDevice(null);
                    }
                    break;
                }
                case AudioManager.MODE_IN_CALL:
                case AudioManager.MODE_IN_COMMUNICATION:
                case AudioManager.MODE_CALL_SCREENING:
                case AudioManager.MODE_CALL_REDIRECT:
                case AudioManager.MODE_COMMUNICATION_REDIRECT:  {
                    if (mPendingHfpActiveDevice != null) {
                        setHfpActiveDevice(mPendingHfpActiveDevice);
                        setLeAudioActiveDevice(null);
                    }
                    break;
                }
            }
        }
    }
@@ -809,25 +857,6 @@ class ActiveDeviceManager {
        }
    }

    private boolean isMediaMode(int mode) {
        switch (mode) {
            case AudioManager.MODE_RINGTONE:
                final HeadsetService headsetService = mFactory.getHeadsetService();
                if (headsetService != null && headsetService.isInbandRingingEnabled()) {
                    return false;
                }
                return true;
            case AudioManager.MODE_IN_CALL:
            case AudioManager.MODE_IN_COMMUNICATION:
            case AudioManager.MODE_CALL_SCREENING:
            case AudioManager.MODE_CALL_REDIRECT:
            case AudioManager.MODE_COMMUNICATION_REDIRECT:
                return false;
            default:
                return true;
        }
    }

    private boolean setFallbackDeviceActive() {
        if (DBG) {
            Log.d(TAG, "setFallbackDeviceActive");
@@ -882,20 +911,25 @@ class ActiveDeviceManager {

        List<BluetoothDevice> connectedDevices = new ArrayList<>();
        connectedDevices.addAll(mLeAudioConnectedDevices);
        boolean isMediaMode = isMediaMode(mAudioManager.getMode());
        if (isMediaMode) {
        switch (mAudioManager.getMode()) {
            case AudioManager.MODE_NORMAL:
                if (a2dpFallbackDevice != null) {
                    connectedDevices.add(a2dpFallbackDevice);
                }
        } else {
                break;
            case AudioManager.MODE_RINGTONE:
                if (headsetFallbackDevice != null && headsetService.isInbandRingingEnabled()) {
                    connectedDevices.add(headsetFallbackDevice);
                }
                break;
            default:
                if (headsetFallbackDevice != null) {
                    connectedDevices.add(headsetFallbackDevice);
                }
        }

        BluetoothDevice device = dbManager.getMostRecentlyConnectedDevicesInList(connectedDevices);
        if (device != null) {
            if (isMediaMode) {
            if (mAudioManager.getMode() == AudioManager.MODE_NORMAL) {
                if (Objects.equals(a2dpFallbackDevice, device)) {
                    if (DBG) {
                        Log.d(TAG, "set A2DP device active: " + device);