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

Commit fbc6e44d authored by Chienyuan's avatar Chienyuan Committed by Chienyuan Huang
Browse files

Use String.split() for parsing in HeadsetStateMachine.processAtBind

* Current code using HeadsetStateMachine.findChar() is a bit complex.
* Use String.split() instead to simply logic.

Bug: 112686323
Test: runtest bluetooth
Change-Id: I9da57d50957cb8533a49d14044138af6608203af
parent 6cf83850
Loading
Loading
Loading
Loading
+4 −10
Original line number Diff line number Diff line
@@ -1877,19 +1877,15 @@ public class HeadsetStateMachine extends StateMachine {
    private void processAtBind(String atString, BluetoothDevice device) {
        log("processAtBind: " + atString);

        // Parse the AT String to find the Indicator Ids that are supported
        int indId = 0;
        int iter = 0;
        int iter1 = 0;
        for (String id : atString.split(",")) {

        while (iter < atString.length()) {
            iter1 = findChar(',', atString, iter);
            String id = atString.substring(iter, iter1);
            int indId;

            try {
                indId = Integer.valueOf(id);
                indId = Integer.parseInt(id);
            } catch (NumberFormatException e) {
                Log.e(TAG, Log.getStackTraceString(new Throwable()));
                continue;
            }

            switch (indId) {
@@ -1905,8 +1901,6 @@ public class HeadsetStateMachine extends StateMachine {
                    log("Invalid HF Indicator Received");
                    break;
            }

            iter = iter1 + 1; // move past comma
        }
    }

+69 −0
Original line number Diff line number Diff line
@@ -940,6 +940,75 @@ public class HeadsetStateMachineTest {
        verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).disconnectAudio(mTestDevice);
    }

    /**
     * A test to verfiy that we correctly handles AT+BIND event with driver safety case from HF
     */
    @Test
    public void testAtBindWithDriverSafetyEventWhenConnecting() {
        setUpConnectingState();

        String atString = "1";
        mHeadsetStateMachine.sendMessage(HeadsetStateMachine.STACK_EVENT,
                new HeadsetStackEvent(HeadsetStackEvent.EVENT_TYPE_BIND, atString, mTestDevice));
        ArgumentCaptor<Intent> intentArgument = ArgumentCaptor.forClass(Intent.class);
        verify(mHeadsetService, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).sendBroadcast(
                intentArgument.capture(), eq(HeadsetService.BLUETOOTH_PERM));
        verify(mHeadsetService, times(1)).sendBroadcast(any(), any());
        Assert.assertEquals(mTestDevice, intentArgument.getValue().getExtra(
                BluetoothDevice.EXTRA_DEVICE, null));
        Assert.assertEquals(HeadsetHalConstants.HF_INDICATOR_ENHANCED_DRIVER_SAFETY,
                intentArgument.getValue().getIntExtra(
                        BluetoothHeadset.EXTRA_HF_INDICATORS_IND_ID, -1));
        Assert.assertEquals(-1, intentArgument.getValue().getIntExtra(
                BluetoothHeadset.EXTRA_HF_INDICATORS_IND_VALUE, -2));
    }

    /**
     * A test to verfiy that we correctly handles AT+BIND event with battery level case from HF
     */
    @Test
    public void testAtBindEventWithBatteryLevelEventWhenConnecting() {
        setUpConnectingState();

        String atString = "2";
        mHeadsetStateMachine.sendMessage(HeadsetStateMachine.STACK_EVENT,
                new HeadsetStackEvent(HeadsetStackEvent.EVENT_TYPE_BIND, atString, mTestDevice));
        ArgumentCaptor<Intent> intentArgument = ArgumentCaptor.forClass(Intent.class);
        verify(mHeadsetService, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).sendBroadcast(
                intentArgument.capture(), eq(HeadsetService.BLUETOOTH_PERM));
        verify(mHeadsetService, times(1)).sendBroadcast(any(), any());
        Assert.assertEquals(mTestDevice, intentArgument.getValue().getExtra(
                BluetoothDevice.EXTRA_DEVICE, null));
        Assert.assertEquals(HeadsetHalConstants.HF_INDICATOR_BATTERY_LEVEL_STATUS,
                intentArgument.getValue().getIntExtra(
                        BluetoothHeadset.EXTRA_HF_INDICATORS_IND_ID, -1));
        Assert.assertEquals(-1, intentArgument.getValue().getIntExtra(
                BluetoothHeadset.EXTRA_HF_INDICATORS_IND_VALUE, -2));
    }

    /**
     * A test to verfiy that we correctly handles AT+BIND event with error case from HF
     */
    @Test
    public void testAtBindEventWithErrorEventWhenConnecting() {
        setUpConnectingState();

        String atString = "err,A,123,,1";
        mHeadsetStateMachine.sendMessage(HeadsetStateMachine.STACK_EVENT,
                new HeadsetStackEvent(HeadsetStackEvent.EVENT_TYPE_BIND, atString, mTestDevice));
        ArgumentCaptor<Intent> intentArgument = ArgumentCaptor.forClass(Intent.class);
        verify(mHeadsetService, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).sendBroadcast(
                intentArgument.capture(), eq(HeadsetService.BLUETOOTH_PERM));
        verify(mHeadsetService, times(1)).sendBroadcast(any(), any());
        Assert.assertEquals(mTestDevice, intentArgument.getValue().getExtra(
                BluetoothDevice.EXTRA_DEVICE, null));
        Assert.assertEquals(HeadsetHalConstants.HF_INDICATOR_ENHANCED_DRIVER_SAFETY,
                intentArgument.getValue().getIntExtra(
                        BluetoothHeadset.EXTRA_HF_INDICATORS_IND_ID, -1));
        Assert.assertEquals(-1, intentArgument.getValue().getIntExtra(
                BluetoothHeadset.EXTRA_HF_INDICATORS_IND_VALUE, -2));
    }

    /**
     * Setup Connecting State
     * @return number of times mHeadsetService.sendBroadcastAsUser() has been invoked