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

Commit 3dbf4777 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Integrate BluetoothSinkAudioPolicy to setAudioRouteAllowed" am: c93f4b10

parents c524c3a0 c93f4b10
Loading
Loading
Loading
Loading
+60 −1
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;
@@ -884,6 +887,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;
@@ -1330,6 +1339,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
@@ -2170,10 +2190,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;
@@ -2242,4 +2274,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;
    }
}
+42 −0
Original line number Diff line number Diff line
@@ -844,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
@@ -1424,6 +1446,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) {
@@ -1438,5 +1461,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;
        }
    }
}