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

Commit dd8fa73a authored by Jakub Pawłowski's avatar Jakub Pawłowski Committed by Gerrit Code Review
Browse files

Merge "[le audio] BassClient cleanup gatt for all disconnection cases" into main

parents d1bcfbd4 397c5e25
Loading
Loading
Loading
Loading
+13 −5
Original line number Diff line number Diff line
@@ -457,6 +457,14 @@ public class BassClientStateMachine extends StateMachine {
        mNoStopScanOffload = false;
    }

    private void resetBluetoothGatt() {
        // cleanup mBluetoothGatt
        if (mBluetoothGatt != null) {
            mBluetoothGatt.close();
            mBluetoothGatt = null;
        }
    }

    private BluetoothLeBroadcastMetadata getBroadcastMetadataFromBaseData(BaseData baseData,
            BluetoothDevice device) {
        return getBroadcastMetadataFromBaseData(baseData, device, false);
@@ -1261,6 +1269,7 @@ public class BassClientStateMachine extends StateMachine {
                        transitionTo(mConnected);
                    } else {
                        Log.w(TAG, "Connection failed to " + mDevice);
                        resetBluetoothGatt();
                        transitionTo(mDisconnected);
                    }
                    break;
@@ -1271,6 +1280,7 @@ public class BassClientStateMachine extends StateMachine {
                        Log.e(TAG, "Unknown device timeout " + device);
                        break;
                    }
                    resetBluetoothGatt();
                    transitionTo(mDisconnected);
                    break;
                case PSYNC_ACTIVE_TIMEOUT:
@@ -1527,11 +1537,7 @@ public class BassClientStateMachine extends StateMachine {
                        Log.w(TAG, "device is already connected to Bass" + mDevice);
                    } else {
                        Log.w(TAG, "unexpected disconnected from " + mDevice);
                        // cleanup mBluetoothGatt
                        if (mBluetoothGatt != null) {
                            mBluetoothGatt.close();
                            mBluetoothGatt = null;
                        }
                        resetBluetoothGatt();
                        cancelActiveSync(null);
                        transitionTo(mDisconnected);
                    }
@@ -1812,6 +1818,8 @@ public class BassClientStateMachine extends StateMachine {
                        Log.w(TAG, "should never happen from this state");
                    } else {
                        Log.w(TAG, "Unexpected disconnection " + mDevice);
                        resetBluetoothGatt();
                        cancelActiveSync(null);
                        transitionTo(mDisconnected);
                    }
                    break;
+20 −0
Original line number Diff line number Diff line
@@ -919,7 +919,12 @@ public class BassClientStateMachineTest {

        Message msg = mBassClientStateMachine.obtainMessage(CONNECTION_STATE_CHANGED);
        msg.obj = BluetoothProfile.STATE_CONNECTING;
        BassClientStateMachine.BluetoothGattTestableWrapper btGatt =
                Mockito.mock(BassClientStateMachine.BluetoothGattTestableWrapper.class);
        mBassClientStateMachine.mBluetoothGatt = btGatt;
        sendMessageAndVerifyTransition(msg, BassClientStateMachine.Disconnected.class);
        verify(btGatt).close();
        assertNull(mBassClientStateMachine.mBluetoothGatt);
    }

    @Test
@@ -941,8 +946,13 @@ public class BassClientStateMachineTest {
        TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
        verify(mBassClientService, never()).sendBroadcast(any(Intent.class), anyString(), any());

        BassClientStateMachine.BluetoothGattTestableWrapper btGatt =
                Mockito.mock(BassClientStateMachine.BluetoothGattTestableWrapper.class);
        mBassClientStateMachine.mBluetoothGatt = btGatt;
        Message msg = mBassClientStateMachine.obtainMessage(CONNECT_TIMEOUT, mTestDevice);
        sendMessageAndVerifyTransition(msg, BassClientStateMachine.Disconnected.class);
        verify(btGatt).close();
        assertNull(mBassClientStateMachine.mBluetoothGatt);
    }

    @Test
@@ -991,9 +1001,14 @@ public class BassClientStateMachineTest {
        TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
        verify(mBassClientService, never()).sendBroadcast(any(Intent.class), anyString(), any());

        BassClientStateMachine.BluetoothGattTestableWrapper btGatt =
                Mockito.mock(BassClientStateMachine.BluetoothGattTestableWrapper.class);
        mBassClientStateMachine.mBluetoothGatt = btGatt;
        Message noneConnectedMsg = mBassClientStateMachine.obtainMessage(CONNECTION_STATE_CHANGED);
        noneConnectedMsg.obj = BluetoothProfile.STATE_DISCONNECTING;
        sendMessageAndVerifyTransition(noneConnectedMsg, BassClientStateMachine.Disconnected.class);
        verify(btGatt).close();
        assertNull(mBassClientStateMachine.mBluetoothGatt);
    }

    @Test
@@ -1379,11 +1394,16 @@ public class BassClientStateMachineTest {
        TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
        verify(mBassClientService, never()).sendBroadcast(any(Intent.class), anyString(), any());

        BassClientStateMachine.BluetoothGattTestableWrapper btGatt =
                Mockito.mock(BassClientStateMachine.BluetoothGattTestableWrapper.class);
        mBassClientStateMachine.mBluetoothGatt = btGatt;
        Message msgToNoneConnectedState =
                mBassClientStateMachine.obtainMessage(CONNECTION_STATE_CHANGED);
        msgToNoneConnectedState.obj = BluetoothProfile.STATE_DISCONNECTING;
        sendMessageAndVerifyTransition(
                msgToNoneConnectedState, BassClientStateMachine.Disconnected.class);
        verify(btGatt).close();
        assertNull(mBassClientStateMachine.mBluetoothGatt);
    }

    /**