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

Commit 5112f491 authored by Eric Shih's avatar Eric Shih
Browse files

Integrate BluetoothSinkAudioPolicy to setAudioRouteAllowed

We need to add new system props to enable this change and set default values of the last two policies to false after sending "AT+ANDROID=?" and receiving "OK".

Bug: 270294700
Test: atest GoogleBluetoothInstrumentationTests
Tag: #feature
Change-Id: I1e202df5f00ab137b696caf02289b70c28d0ea8d
parent 6089760e
Loading
Loading
Loading
Loading
+61 −2
Original line number Diff line number Diff line
@@ -190,6 +190,9 @@ public class HeadsetClientStateMachine extends StateMachine {

    public int mAudioPolicyRemoteSupported;
    private BluetoothSinkAudioPolicy mHsClientAudioPolicy;
    private final int mConnectingTimePolicyProperty;
    private final int mInBandRingtonePolicyProperty;
    private final boolean mForceSetAudioPolicyProperty;

    private boolean mAudioWbs;
    private int mVoiceRecognitionActive;
@@ -883,6 +886,12 @@ public class HeadsetClientStateMachine extends StateMachine {
            R.bool.headset_client_initial_audio_route_allowed);

        mHsClientAudioPolicy = new BluetoothSinkAudioPolicy.Builder().build();
        mConnectingTimePolicyProperty = getAudioPolicySystemProp(
            "bluetooth.headset_client.audio_policy.connecting_time.config");
        mInBandRingtonePolicyProperty = getAudioPolicySystemProp(
            "bluetooth.headset_client.audio_policy.in_band_ringtone.config");
        mForceSetAudioPolicyProperty = SystemProperties.getBoolean(
            "bluetooth.headset_client.audio_policy.force_enabled", false);

        mIndicatorNetworkState = HeadsetClientHalConstants.NETWORK_STATE_NOT_AVAILABLE;
        mIndicatorNetworkType = HeadsetClientHalConstants.SERVICE_TYPE_HOME;
@@ -1329,6 +1338,17 @@ 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()
                        .setActiveDevicePolicyAfterConnection(mConnectingTimePolicyProperty)
                        .setInBandRingtonePolicy(mInBandRingtonePolicyProperty)
                        .build());
            }
        }

        @Override
@@ -2169,10 +2189,22 @@ public class HeadsetClientStateMachine extends StateMachine {

        /*
         * Backward compatibility for mAudioRouteAllowed
         *
         * Set default policies if
         *  1. need to set audio policy from system props
         *  2. remote device supports audio policy
         */
        if (getForceSetAudioPolicyProperty()) {
            setAudioPolicy(new BluetoothSinkAudioPolicy.Builder(mHsClientAudioPolicy)
                    .setCallEstablishPolicy(establishPolicy)
                    .setActiveDevicePolicyAfterConnection(getConnectingTimePolicyProperty())
                    .setInBandRingtonePolicy(getInBandRingtonePolicyProperty())
                    .build());
        } else {
            setAudioPolicy(new BluetoothSinkAudioPolicy.Builder(mHsClientAudioPolicy)
                .setCallEstablishPolicy(establishPolicy).build());
        }
    }

    public boolean getAudioRouteAllowed() {
        return mAudioRouteAllowed;
@@ -2197,7 +2229,7 @@ public class HeadsetClientStateMachine extends StateMachine {
        logD("setAudioPolicy: " + policies);
        mHsClientAudioPolicy = policies;

        if (mAudioPolicyRemoteSupported != BluetoothStatusCodes.FEATURE_SUPPORTED) {
        if (getAudioPolicyRemoteSupported() != BluetoothStatusCodes.FEATURE_SUPPORTED) {
            Log.e(TAG, "Audio Policy feature not supported!");
            return;
        }
@@ -2239,4 +2271,31 @@ public class HeadsetClientStateMachine extends StateMachine {
    public int getAudioPolicyRemoteSupported() {
        return mAudioPolicyRemoteSupported;
    }

    /**
     * handles the value of {@link BluetoothSinkAudioPolicy} from system property
     */
    private int getAudioPolicySystemProp(String propKey) {
        int mProp = SystemProperties.getInt(propKey, BluetoothSinkAudioPolicy.POLICY_UNCONFIGURED);
        if (mProp < BluetoothSinkAudioPolicy.POLICY_UNCONFIGURED
                || mProp > BluetoothSinkAudioPolicy.POLICY_NOT_ALLOWED) {
            mProp = BluetoothSinkAudioPolicy.POLICY_UNCONFIGURED;
        }
        return mProp;
    }

    @VisibleForTesting
    boolean getForceSetAudioPolicyProperty() {
        return mForceSetAudioPolicyProperty;
    }

    @VisibleForTesting
    int getConnectingTimePolicyProperty() {
        return mConnectingTimePolicyProperty;
    }

    @VisibleForTesting
    int getInBandRingtonePolicyProperty() {
        return mInBandRingtonePolicyProperty;
    }
}
+43 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.bluetooth.BluetoothAssignedNumbers;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadsetClient;
import android.bluetooth.BluetoothSinkAudioPolicy;
import android.bluetooth.BluetoothStatusCodes;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
@@ -843,6 +844,28 @@ public class HeadsetClientStateMachineTest {
        mHeadsetClientStateMachine.setAudioRouteAllowed(true);

        Assert.assertTrue(mHeadsetClientStateMachine.getAudioRouteAllowed());

        // Case 1: if remote is not supported
        // Expect: Should not send +ANDROID to remote
        mHeadsetClientStateMachine.mCurrentDevice = mTestDevice;
        mHeadsetClientStateMachine.setAudioPolicyRemoteSupported(false);
        verify(mNativeInterface, never()).sendAndroidAt(mTestDevice, "+ANDROID=1,1,0,0");

        // Case 2: if remote is supported and mForceSetAudioPolicyProperty is false
        // Expect: Should send +ANDROID:1,1,0,0 to remote
        mHeadsetClientStateMachine.setAudioPolicyRemoteSupported(true);
        mHeadsetClientStateMachine.setForceSetAudioPolicyProperty(false);
        mHeadsetClientStateMachine.setAudioRouteAllowed(true);
        verify(mNativeInterface).sendAndroidAt(mTestDevice, "+ANDROID=1,1,0,0");

        mHeadsetClientStateMachine.setAudioRouteAllowed(false);
        verify(mNativeInterface).sendAndroidAt(mTestDevice, "+ANDROID=1,2,0,0");

        // Case 3: if remote is supported and mForceSetAudioPolicyProperty is true
        // Expect: Should send +ANDROID:1,1,2,1 to remote
        mHeadsetClientStateMachine.setForceSetAudioPolicyProperty(true);
        mHeadsetClientStateMachine.setAudioRouteAllowed(true);
        verify(mNativeInterface).sendAndroidAt(mTestDevice, "+ANDROID=1,1,2,1");
    }

    @Test
@@ -1408,6 +1431,7 @@ public class HeadsetClientStateMachineTest {
    public static class TestHeadsetClientStateMachine extends HeadsetClientStateMachine {

        Boolean allowConnect = null;
        boolean mForceSetAudioPolicyProperty = false;

        TestHeadsetClientStateMachine(HeadsetClientService context, HeadsetService headsetService,
                Looper looper, NativeInterface nativeInterface) {
@@ -1422,5 +1446,24 @@ public class HeadsetClientStateMachineTest {
        public boolean okToConnect(BluetoothDevice device) {
            return allowConnect != null ? allowConnect : super.okToConnect(device);
        }

        @Override
        public int getConnectingTimePolicyProperty() {
            return 2;
        }

        @Override
        public int getInBandRingtonePolicyProperty() {
            return 1;
        }

        void setForceSetAudioPolicyProperty(boolean flag){
            mForceSetAudioPolicyProperty = flag;
        }

        @Override
        boolean getForceSetAudioPolicyProperty() {
            return mForceSetAudioPolicyProperty;
        }
    }
}