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

Commit 797096e8 authored by Eric Shih's avatar Eric Shih
Browse files

Only send default audio policy once

Previously we put the logic when entering Connected State. "setAudioRouteAllowed" will also set the audio policy. If HFPC goes crazy and switch from AudioOn to Connected, this will affect what "setAudioRouteAllowed" wants to do.

Bug: 288187749
Test: atest BluetoothInstrumentationTests
Change-Id: I4543f4dcef9b48f2bec58007a70008cd032cf6f1
parent 775ae5a0
Loading
Loading
Loading
Loading
+9 −11
Original line number Diff line number Diff line
@@ -1349,17 +1349,6 @@ public class HeadsetClientStateMachine extends StateMachine {
                        + " to Connected, mCurrentDevice=" + mCurrentDevice);
            }
            mService.updateBatteryLevel();

            // Send default policies to the remote if
            //   1. need to set audio policy from system props
            //   2. remote device supports audio policy
            if (mForceSetAudioPolicyProperty
                    && getAudioPolicyRemoteSupported() == BluetoothStatusCodes.FEATURE_SUPPORTED) {
                setAudioPolicy(new BluetoothSinkAudioPolicy.Builder(mHsClientAudioPolicy)
                        .setActiveDevicePolicyAfterConnection(mConnectingTimePolicyProperty)
                        .setInBandRingtonePolicy(mInBandRingtonePolicyProperty)
                        .build());
            }
        }

        @Override
@@ -2061,6 +2050,15 @@ public class HeadsetClientStateMachine extends StateMachine {
            Log.i(TAG, "processAndroidAtFeature:"
                    + BluetoothSinkAudioPolicy.HFP_SET_SINK_AUDIO_POLICY_ID + " supported");
            setAudioPolicyRemoteSupported(true);

            // Send default policies to the remote if it supports
            if (getForceSetAudioPolicyProperty()) {
                setAudioPolicy(
                        new BluetoothSinkAudioPolicy.Builder(mHsClientAudioPolicy)
                                .setActiveDevicePolicyAfterConnection(mConnectingTimePolicyProperty)
                                .setInBandRingtonePolicy(mInBandRingtonePolicyProperty)
                                .build());
            }
        }
    }

+39 −0
Original line number Diff line number Diff line
@@ -1211,6 +1211,45 @@ public class HeadsetClientStateMachineTest {
        Assert.assertEquals(0, mHeadsetClientStateMachine.mQueuedActions.size());
    }

    @Test
    public void testTestDefaultAudioPolicy() {
        mHeadsetClientStateMachine.setForceSetAudioPolicyProperty(true);
        initToConnectedState();

        // Check if set default policy when Connecting -> Connected
        // The testing sys prop is 0. It is ok to check if set audio policy while leaving connecting
        // state.
        verify(mNativeInterface, times(1))
                .sendAndroidAt(mTestDevice, "+ANDROID=SINKAUDIOPOLICY,0,0,0");

        // Check if won't set default policy when AudioOn -> Connected
        // Transit to AudioOn
        mHeadsetClientStateMachine.setAudioRouteAllowed(true);
        StackEvent event = new StackEvent(StackEvent.EVENT_TYPE_AUDIO_STATE_CHANGED);
        event.valueInt = HeadsetClientHalConstants.AUDIO_STATE_CONNECTED;
        event.device = mTestDevice;
        mHeadsetClientStateMachine.sendMessage(
                mHeadsetClientStateMachine.obtainMessage(StackEvent.STACK_EVENT, event));
        TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
        Assert.assertThat(
                mHeadsetClientStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(HeadsetClientStateMachine.AudioOn.class));

        // Back to Connected
        event = new StackEvent(StackEvent.EVENT_TYPE_AUDIO_STATE_CHANGED);
        event.valueInt = HeadsetClientHalConstants.AUDIO_STATE_DISCONNECTED;
        event.device = mTestDevice;
        mHeadsetClientStateMachine.sendMessage(
                mHeadsetClientStateMachine.obtainMessage(StackEvent.STACK_EVENT, event));
        TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
        Assert.assertThat(
                mHeadsetClientStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(HeadsetClientStateMachine.Connected.class));

        verify(mNativeInterface, times(1))
                .sendAndroidAt(mTestDevice, "+ANDROID=SINKAUDIOPOLICY,0,0,0");
    }

    @Test
    public void testDumpDoesNotCrash() {
        mHeadsetClientStateMachine.dump(new StringBuilder());