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

Commit 6dd736f6 authored by Eric Laurent's avatar Eric Laurent
Browse files

AudioDeviceBroker: mute call when switching HFP device

When a Bluetooth HFP device is disconnected as part of a device switch
and SCO audio is connected, mute call until either BT SCO audio is reconnected
or a 2 second timeout happens.

Bug: 385816977
Test: repro steps in bug
Flag: com.android.media.audio.optimize_bt_device_switch
Change-Id: I77c2f5bdd4c5f8434cbd641d137584427be6b10a
parent 90f8a313
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -124,6 +124,9 @@ public class AudioDeviceBroker {
    // Delay before checking it music should be unmuted after processing an A2DP message
    private static final int BTA2DP_MUTE_CHECK_DELAY_MS = 200;

    // Delay before unmuting call after HFP device switch
    private static final int HFP_SWITCH_CALL_UNMUTE_DELAY_MS = 2000;

    private final @NonNull AudioService mAudioService;
    private final @NonNull Context mContext;
    private final @NonNull AudioSystemAdapter mAudioSystem;
@@ -313,6 +316,14 @@ public class AudioDeviceBroker {

    @GuardedBy("mDeviceStateLock")
    /*package*/ void onSetBtScoActiveDevice(BluetoothDevice btDevice, boolean deviceSwitch) {
        if (deviceSwitch && isBluetoothScoActive()) {
            mAudioService.setCallMute(true);
            sendIMsg(MSG_I_MUTE_CALL, SENDMSG_REPLACE,
                    0 /*unmute*/, HFP_SWITCH_CALL_UNMUTE_DELAY_MS);
        } else if (btDevice == null) {
            sendIMsg(MSG_I_MUTE_CALL, SENDMSG_REPLACE,
                    0 /*unmute*/, 0 /*delay */);
        }
        mBtHelper.onSetBtScoActiveDevice(btDevice, deviceSwitch);
    }

@@ -1222,6 +1233,10 @@ public class AudioDeviceBroker {
            if (!mScoManagedByAudio) {
                postUpdateCommunicationRouteClient(btScoRequesterAS, eventSource);
            }
            if (on) {
                sendIMsg(MSG_I_MUTE_CALL, SENDMSG_REPLACE,
                        0 /*unmute*/, 0);
            }
        }
    }

@@ -2125,6 +2140,11 @@ public class AudioDeviceBroker {
                        checkMessagesMuteMusic(0);
                    }
                    break;
                case MSG_I_MUTE_CALL:
                    synchronized (mDeviceStateLock) {
                        mAudioService.setCallMute(msg.arg1 == 1);
                    }
                    break;
                case MSG_L_NOTIFY_PREFERRED_AUDIOPROFILE_APPLIED: {
                    final BluetoothDevice btDevice = (BluetoothDevice) msg.obj;
                    BtHelper.onNotifyPreferredAudioProfileApplied(btDevice);
@@ -2232,6 +2252,9 @@ public class AudioDeviceBroker {
    private static final int MSG_L_SET_FORCE_BT_A2DP_USE_NO_MUTE = 60;
    private static final int MSG_IL_BT_HEARING_AID_TIMEOUT = 61;

    private static final int MSG_I_MUTE_CALL = 62;


    private static boolean isMessageHandledUnderWakelock(int msgId) {
        switch(msgId) {
            case MSG_L_SET_WIRED_DEVICE_CONNECTION_STATE:
@@ -2262,6 +2285,10 @@ public class AudioDeviceBroker {
        sendIILMsg(msg, existingMsgPolicy, 0, 0, null, delay);
    }

    private void sendIMsg(int msg, int existingMsgPolicy, int arg, int delay) {
        sendIILMsg(msg, existingMsgPolicy, arg, 0, 0, delay);
    }

    private void sendILMsg(int msg, int existingMsgPolicy, int arg, Object obj, int delay) {
        sendIILMsg(msg, existingMsgPolicy, arg, 0, obj, delay);
    }
+8 −0
Original line number Diff line number Diff line
@@ -8859,6 +8859,14 @@ public class AudioService extends IAudioService.Stub
        getVssForStreamOrDefault(AudioSystem.STREAM_MUSIC).muteInternally(mute);
    }
    /** Mute or unmute call audio */
    /*package*/ void setCallMute(boolean mute) {
        getVssForStreamOrDefault(AudioSystem.STREAM_VOICE_CALL).muteInternally(mute);
        if (!replaceStreamBtSco()) {
            getVssForStreamOrDefault(AudioSystem.STREAM_BLUETOOTH_SCO).muteInternally(mute);
        }
    }
    private static final Set<Integer> DEVICE_MEDIA_UNMUTED_ON_PLUG_SET;
    static {
        DEVICE_MEDIA_UNMUTED_ON_PLUG_SET = new HashSet<>();
+1 −1
Original line number Diff line number Diff line
@@ -399,7 +399,7 @@ public class BtHelper {
            }
            boolean deviceSwitch = optimizeBtDeviceSwitch()
                    && btDevice != null && mBluetoothHeadsetDevice != null;
            onSetBtScoActiveDevice(btDevice, deviceSwitch);
            mDeviceBroker.onSetBtScoActiveDevice(btDevice, deviceSwitch);
        } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
            int btState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
            onScoAudioStateChanged(btState);