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

Commit acadef1c authored by Pavlin Radoslavov's avatar Pavlin Radoslavov Committed by android-build-merger
Browse files

Merge "Allow outgoing A2DP connection when pairing via NFC" am: cddccf24 am: 44ea0cbc

am: d567a640

Change-Id: I3289c41a55ae1b098bfabde14d92c6a0408636be
parents 6cb8cb4f d567a640
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -332,12 +332,15 @@ public class A2dpService extends ProfileService {
     * The check considers a number of factors during the evaluation.
     *
     * @param device the peer device to connect to
     * @param isOutgoingRequest if true, the check is for outgoing connection
     * request, otherwise is for incoming connection request
     * @return true if connection is allowed, otherwise false
     */
    @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
    public boolean okToConnect(BluetoothDevice device) {
    public boolean okToConnect(BluetoothDevice device, boolean isOutgoingRequest) {
        Log.i(TAG, "okToConnect: device " + device + " isOutgoingRequest: " + isOutgoingRequest);
        // Check if this is an incoming connection in Quiet mode.
        if (mAdapterService.isQuietModeEnabled()) {
        if (mAdapterService.isQuietModeEnabled() && !isOutgoingRequest) {
            Log.e(TAG, "okToConnect: cannot connect to " + device + " : quiet mode enabled");
            return false;
        }
+5 −5
Original line number Diff line number Diff line
@@ -186,7 +186,7 @@ final class A2dpStateMachine extends StateMachine {
                        Log.e(TAG, "Disconnected: error connecting to " + mDevice);
                        break;
                    }
                    if (mA2dpService.okToConnect(mDevice)) {
                    if (mA2dpService.okToConnect(mDevice, true)) {
                        transitionTo(mConnecting);
                    } else {
                        // Reject the request and stay in Disconnected state
@@ -227,7 +227,7 @@ final class A2dpStateMachine extends StateMachine {
                    Log.w(TAG, "Ignore A2DP DISCONNECTED event: " + mDevice);
                    break;
                case A2dpStackEvent.CONNECTION_STATE_CONNECTING:
                    if (mA2dpService.okToConnect(mDevice)) {
                    if (mA2dpService.okToConnect(mDevice, false)) {
                        Log.i(TAG, "Incoming A2DP Connecting request accepted: " + mDevice);
                        transitionTo(mConnecting);
                    } else {
@@ -238,7 +238,7 @@ final class A2dpStateMachine extends StateMachine {
                    break;
                case A2dpStackEvent.CONNECTION_STATE_CONNECTED:
                    Log.w(TAG, "A2DP Connected from Disconnected state: " + mDevice);
                    if (mA2dpService.okToConnect(mDevice)) {
                    if (mA2dpService.okToConnect(mDevice, false)) {
                        Log.i(TAG, "Incoming A2DP Connected request accepted: " + mDevice);
                        transitionTo(mConnected);
                    } else {
@@ -429,7 +429,7 @@ final class A2dpStateMachine extends StateMachine {
                    transitionTo(mDisconnected);
                    break;
                case A2dpStackEvent.CONNECTION_STATE_CONNECTED:
                    if (mA2dpService.okToConnect(mDevice)) {
                    if (mA2dpService.okToConnect(mDevice, false)) {
                        Log.w(TAG, "Disconnecting interrupted: device is connected: " + mDevice);
                        transitionTo(mConnected);
                    } else {
@@ -439,7 +439,7 @@ final class A2dpStateMachine extends StateMachine {
                    }
                    break;
                case A2dpStackEvent.CONNECTION_STATE_CONNECTING:
                    if (mA2dpService.okToConnect(mDevice)) {
                    if (mA2dpService.okToConnect(mDevice, false)) {
                        Log.i(TAG, "Disconnecting interrupted: try to reconnect: " + mDevice);
                        transitionTo(mConnecting);
                    } else {
+18 −7
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ public class A2dpServiceTest {

        TestUtils.setAdapterService(mAdapterService);
        doReturn(MAX_CONNECTED_AUDIO_DEVICES).when(mAdapterService).getMaxConnectedAudioDevices();
        doReturn(false).when(mAdapterService).isQuietModeEnabled();

        mAdapter = BluetoothAdapter.getDefaultAdapter();

@@ -851,7 +852,7 @@ public class A2dpServiceTest {
    }

    /**
     *  Helper function to test okToConnect() method
     * Helper function to test okToConnect() method.
     *
     * @param device test device
     * @param bondState bond state value, could be invalid
@@ -862,7 +863,17 @@ public class A2dpServiceTest {
            boolean expected) {
        doReturn(bondState).when(mAdapterService).getBondState(device);
        Assert.assertTrue(mA2dpService.setPriority(device, priority));
        Assert.assertEquals(expected, mA2dpService.okToConnect(device));
    }

        // Test when the AdapterService is in non-quiet mode: the result should not depend
        // on whether the connection request is outgoing or incoming.
        doReturn(false).when(mAdapterService).isQuietModeEnabled();
        Assert.assertEquals(expected, mA2dpService.okToConnect(device, true));  // Outgoing
        Assert.assertEquals(expected, mA2dpService.okToConnect(device, false)); // Incoming

        // Test when the AdapterService is in quiet mode: the result should always be
        // false when the connection request is incoming.
        doReturn(true).when(mAdapterService).isQuietModeEnabled();
        Assert.assertEquals(expected, mA2dpService.okToConnect(device, true));  // Outgoing
        Assert.assertEquals(false, mA2dpService.okToConnect(device, false)); // Incoming
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -107,7 +107,8 @@ public class A2dpStateMachineTest {
     * @param allow if true, connection is allowed
     */
    private void allowConnection(boolean allow) {
        doReturn(allow).when(mA2dpService).okToConnect(any(BluetoothDevice.class));
        doReturn(allow).when(mA2dpService).okToConnect(any(BluetoothDevice.class),
                                                       anyBoolean());
    }

    /**