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

Commit 13371a80 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Remove switching BT devices computation flag" into main

parents 0e5a2a83 2ae52b01
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -84,14 +84,6 @@ flag {
  }
}

# OWNER=pmadapurmath TARGET=24Q3
flag {
  name: "resolve_switching_bt_devices_computation"
  namespace: "telecom"
  description: "Update switching bt devices based on arbitrary device chosen if no device is specified."
  bug: "333751408"
}

# OWNER=pmadapurmath TARGET=24Q3
flag {
  name: "early_update_internal_call_audio_state"
+54 −183
Original line number Diff line number Diff line
@@ -170,7 +170,6 @@ public class BluetoothRouteManager extends StateMachine {
                    case CONNECT_BT:
                        String actualAddress;
                        boolean connected;
                        if (mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
                        Pair<String, Boolean> addressInfo = computeAddressToConnectTo(
                                (String) args.arg2, false, null);
                        // See if we need to transition route if the device is already
@@ -179,10 +178,6 @@ public class BluetoothRouteManager extends StateMachine {
                        actualAddress = addressInfo.first;
                        connected = connectBtAudio(actualAddress, 0,
                                false /* switchingBtDevices*/);
                        } else {
                            actualAddress = connectBtAudioLegacy((String) args.arg2, false);
                            connected = actualAddress != null;
                        }

                        if (connected) {
                            transitionTo(getConnectingStateForAddress(actualAddress,
@@ -199,7 +194,6 @@ public class BluetoothRouteManager extends StateMachine {
                        Log.i(LOG_TAG, "Retrying BT connection to %s", (String) args.arg2);
                        String retryAddress;
                        boolean retrySuccessful;
                        if (mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
                        Pair<String, Boolean> retryAddressInfo = computeAddressToConnectTo(
                                (String) args.arg2, false, null);
                        // See if we need to transition route if the device is already
@@ -208,11 +202,6 @@ public class BluetoothRouteManager extends StateMachine {
                        retryAddress = retryAddressInfo.first;
                        retrySuccessful = connectBtAudio(retryAddress, args.argi1,
                                false /* switchingBtDevices*/);
                        } else {
                            retryAddress = connectBtAudioLegacy((String) args.arg2, args.argi1,
                                    false /* switchingBtDevices*/);
                            retrySuccessful = retryAddress != null;
                        }

                        if (retrySuccessful) {
                            transitionTo(getConnectingStateForAddress(retryAddress,
@@ -303,10 +292,8 @@ public class BluetoothRouteManager extends StateMachine {
                        }
                    }
                }
                if (mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
                switchingBtDevices &= (mDeviceAddress != null);
            }
            }
            try {
                switch (msg.what) {
                    case NEW_DEVICE_CONNECTED:
@@ -321,7 +308,6 @@ public class BluetoothRouteManager extends StateMachine {
                        break;
                    case CONNECT_BT:
                        String actualAddress = null;
                        if (mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
                        Pair<String, Boolean> addressInfo = computeAddressToConnectTo(address,
                                switchingBtDevices, mDeviceAddress);
                        // See if we need to transition route if the device is already
@@ -329,20 +315,14 @@ public class BluetoothRouteManager extends StateMachine {
                        addressInfo = handleDeviceAlreadyConnected(addressInfo);
                        actualAddress = addressInfo.first;
                        switchingBtDevices = addressInfo.second;
                        }

                        if (!switchingBtDevices) {
                            // Ignore repeated connection attempts to the same device
                            break;
                        }

                        if (!mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
                            actualAddress = connectBtAudioLegacy(address,
                        boolean connected = connectBtAudio(actualAddress, 0,
                                true /* switchingBtDevices*/);
                        }
                        boolean connected = mFeatureFlags.resolveSwitchingBtDevicesComputation()
                                ? connectBtAudio(actualAddress, 0, true /* switchingBtDevices*/)
                                : actualAddress != null;
                        if (connected) {
                            transitionTo(getConnectingStateForAddress(actualAddress,
                                    "AudioConnecting/CONNECT_BT"));
@@ -356,7 +336,6 @@ public class BluetoothRouteManager extends StateMachine {
                        break;
                    case RETRY_BT_CONNECTION:
                        String retryAddress = null;
                        if (mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
                        Pair<String, Boolean> retryAddressInfo = computeAddressToConnectTo(
                                address, switchingBtDevices, mDeviceAddress);
                        // See if we need to transition route if the device is already
@@ -364,22 +343,14 @@ public class BluetoothRouteManager extends StateMachine {
                        retryAddressInfo = handleDeviceAlreadyConnected(retryAddressInfo);
                        retryAddress = retryAddressInfo.first;
                        switchingBtDevices = retryAddressInfo.second;
                        }

                        if (!switchingBtDevices) {
                            Log.d(LOG_TAG, "Retry message came through while connecting.");
                            break;
                        }

                        if (!mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
                            retryAddress = connectBtAudioLegacy(address, args.argi1,
                        boolean retrySuccessful = connectBtAudio(retryAddress, args.argi1,
                                true /* switchingBtDevices*/);
                        }
                        boolean retrySuccessful = mFeatureFlags
                                .resolveSwitchingBtDevicesComputation()
                                ? connectBtAudio(retryAddress, args.argi1,
                                        true /* switchingBtDevices*/)
                                : retryAddress != null;
                        if (retrySuccessful) {
                            transitionTo(getConnectingStateForAddress(retryAddress,
                                    "AudioConnecting/RETRY_BT_CONNECTION"));
@@ -459,9 +430,7 @@ public class BluetoothRouteManager extends StateMachine {
            SomeArgs args = (SomeArgs) msg.obj;
            String address = (String) args.arg2;
            boolean switchingBtDevices = !Objects.equals(mDeviceAddress, address);
            if (mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
            switchingBtDevices &= (mDeviceAddress != null);
            }

            try {
                switch (msg.what) {
@@ -476,7 +445,6 @@ public class BluetoothRouteManager extends StateMachine {
                        break;
                    case CONNECT_BT:
                        String actualAddress = null;
                        if (mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
                        Pair<String, Boolean> addressInfo = computeAddressToConnectTo(address,
                                switchingBtDevices, mDeviceAddress);
                        // See if we need to transition route if the device is already
@@ -484,7 +452,6 @@ public class BluetoothRouteManager extends StateMachine {
                        addressInfo = handleDeviceAlreadyConnected(addressInfo);
                        actualAddress = addressInfo.first;
                        switchingBtDevices = addressInfo.second;
                        }

                        if (!switchingBtDevices) {
                            // Ignore connection to already connected device but still notify
@@ -494,13 +461,8 @@ public class BluetoothRouteManager extends StateMachine {
                            break;
                        }

                        if (!mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
                            actualAddress = connectBtAudioLegacy(address,
                        boolean connected = connectBtAudio(actualAddress, 0,
                                true /* switchingBtDevices*/);
                        }
                        boolean connected = mFeatureFlags.resolveSwitchingBtDevicesComputation()
                                ? connectBtAudio(actualAddress, 0, true /* switchingBtDevices*/)
                                : actualAddress != null;
                        if (connected) {
                            if (mFeatureFlags.useActualAddressToEnterConnectingState()) {
                                transitionTo(getConnectingStateForAddress(actualAddress,
@@ -519,7 +481,6 @@ public class BluetoothRouteManager extends StateMachine {
                        break;
                    case RETRY_BT_CONNECTION:
                        String retryAddress = null;
                        if (mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
                        Pair<String, Boolean> retryAddressInfo = computeAddressToConnectTo(
                                address, switchingBtDevices, mDeviceAddress);
                        // See if we need to transition route if the device is already
@@ -527,22 +488,14 @@ public class BluetoothRouteManager extends StateMachine {
                        retryAddressInfo = handleDeviceAlreadyConnected(retryAddressInfo);
                        retryAddress = retryAddressInfo.first;
                        switchingBtDevices = retryAddressInfo.second;
                        }

                        if (!switchingBtDevices) {
                            Log.d(LOG_TAG, "Retry message came through while connected.");
                            break;
                        }

                        if (!mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
                            retryAddress = connectBtAudioLegacy(address, args.argi1,
                        boolean retrySuccessful = connectBtAudio(retryAddress, args.argi1,
                                true /* switchingBtDevices*/);
                        }
                        boolean retrySuccessful = mFeatureFlags
                                .resolveSwitchingBtDevicesComputation()
                                ? connectBtAudio(retryAddress, args.argi1,
                                        true /* switchingBtDevices*/)
                                : retryAddress != null;
                        if (retrySuccessful) {
                            transitionTo(getConnectingStateForAddress(retryAddress,
                                    "AudioConnected/RETRY_BT_CONNECTION"));
@@ -928,8 +881,6 @@ public class BluetoothRouteManager extends StateMachine {
     * @param switchingBtDevices Used when there is existing audio connection to other Bt device.
     * @return {@code true} if the connection to the address was successful, otherwise {@code false}
     *          if the connection fails.
     *
     * Note: This should only be used in par with the resolveSwitchingBtDevicesComputation flag.
     */
    private boolean connectBtAudio(String address, int retryCount, boolean switchingBtDevices) {
        if (address == null) {
@@ -960,86 +911,6 @@ public class BluetoothRouteManager extends StateMachine {
        return true;
    }

    private String connectBtAudioLegacy(String address, boolean switchingBtDevices) {
        return connectBtAudioLegacy(address, 0, switchingBtDevices);
    }

    /**
     * Initiates a connection to the BT address specified.
     * Note: This method is not synchronized on the Telecom lock, so don't try and call back into
     * Telecom from within it.
     * @param address The address that should be tried first. May be null.
     * @param retryCount The number of times this connection attempt has been retried.
     * @param switchingBtDevices Used when there is existing audio connection to other Bt device.
     * @return The address of the device that's actually being connected to, or null if no
     * connection was successful.
     */
    private String connectBtAudioLegacy(String address, int retryCount,
            boolean switchingBtDevices) {
        Collection<BluetoothDevice> deviceList = mDeviceManager.getConnectedDevices();
        Optional<BluetoothDevice> matchingDevice = deviceList.stream()
                .filter(d -> Objects.equals(d.getAddress(), address))
                .findAny();

        if (switchingBtDevices) {
            /* When new Bluetooth connects audio, make sure previous one has disconnected audio. */
            mDeviceManager.disconnectAudio();
        }

        String actualAddress = matchingDevice.isPresent()
                ? address : getActiveDeviceAddress();
        if (actualAddress == null) {
            Log.i(this, "No device specified and BT stack has no active device."
                    + " Using arbitrary device - except watch");
            if (deviceList.size() > 0) {
                for (BluetoothDevice device : deviceList) {
                    if (mFeatureFlags.ignoreAutoRouteToWatchDevice() && isWatch(device)) {
                        Log.i(this, "Skipping a watch device: " + device);
                        continue;
                    }
                    actualAddress = device.getAddress();
                    break;
                }
            }

            if (actualAddress == null) {
                Log.i(this, "No devices available at all. Not connecting.");
                return null;
            }
        }
        if (!matchingDevice.isPresent()) {
            Log.i(this, "No device with address %s available. Using %s instead.",
                    address, actualAddress);
        }

        BluetoothDevice alreadyConnectedDevice = getBluetoothAudioConnectedDevice();
        if (alreadyConnectedDevice != null && alreadyConnectedDevice.getAddress().equals(
                actualAddress)) {
            Log.i(this, "trying to connect to already connected device -- skipping connection"
                    + " and going into the actual connected state.");
            transitionToActualState();
            return null;
        }

        if (!mDeviceManager.connectAudio(actualAddress, switchingBtDevices)) {
            boolean shouldRetry = retryCount < MAX_CONNECTION_RETRIES;
            Log.w(LOG_TAG, "Could not connect to %s. Will %s", actualAddress,
                    shouldRetry ? "retry" : "not retry");
            if (shouldRetry) {
                SomeArgs args = SomeArgs.obtain();
                args.arg1 = Log.createSubsession();
                args.arg2 = actualAddress;
                args.argi1 = retryCount + 1;
                sendMessageDelayed(RETRY_BT_CONNECTION, args,
                        mTimeoutsAdapter.getRetryBluetoothConnectAudioBackoffMillis(
                                mContext, mFeatureFlags));
            }
            return null;
        }

        return actualAddress;
    }

    private String getActiveDeviceAddress() {
        if (mHfpActiveDeviceCache != null) {
            return mHfpActiveDeviceCache.getAddress();
+1 −8
Original line number Diff line number Diff line
@@ -258,7 +258,6 @@ public class BluetoothRouteManagerTest extends TelecomTestCase {
    @SmallTest
    @Test
    public void testConnectBtWithoutAddress_SwitchingBtDeviceFlag() {
        when(mFeatureFlags.resolveSwitchingBtDevicesComputation()).thenReturn(true);
        verifyConnectBtWithoutAddress();
    }

@@ -287,13 +286,7 @@ public class BluetoothRouteManagerTest extends TelecomTestCase {
        waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);
        // We should not expect explicit connection attempt (BluetoothDeviceManager#connectAudio)
        // as the device is already "connected" as per how the state machine was initialized.
        if (mFeatureFlags.resolveSwitchingBtDevicesComputation()) {
        verify(mDeviceManager, never()).disconnectAudio();
        } else {
            // Legacy behavior
            verifyConnectionAttempt(DEVICE1, 1);
            verify(mDeviceManager, times(1)).disconnectAudio();
        }
        assertEquals(BluetoothRouteManager.AUDIO_CONNECTED_STATE_NAME_PREFIX
                        + ":" + DEVICE1.getAddress(),
                sm.getCurrentState().getName());