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

Commit 2c17c2fc authored by Eric Laurent's avatar Eric Laurent
Browse files

AudioService: update ring mute state and call volume synchronously

Update ring mute state and call volumes synchronously in AudioDeviceBroker
message handler when the communication device is updated to avoid audio leaks
due to delays in processing messages.

Note: the ring mute update was made asynchronous to fix b/124193282. But
AudioDeviceBroker was calling setUpdateRingerModeServiceInt() when BT SCO audio
was connected or disconnected although only the ring mute state update done
by muteRingerModeStreams() is necessary in this case as the ringer mode does
not change. muteRingerModeStreams() can be called synchronously.

Bug: 409236185
Test: repro steps in bug
Flag: EXEMPT bug fix.
Change-Id: I882ca87cf116dcb2c17b68dc97aabb734f97b262
parent 5733f2c4
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -1524,9 +1524,9 @@ public class AudioDeviceBroker {
            } else if (equalScoHaVcIndexRange() && isBluetoothHaActive()) {
                btCommDeviceActiveType = BT_COMM_DEVICE_ACTIVE_HA;
            }
            mAudioService.postBtCommDeviceActive(btCommDeviceActiveType);
            mAudioService.updateBtCommDeviceActive(btCommDeviceActiveType);
        } else {
            mAudioService.postBtCommDeviceActive(
            mAudioService.updateBtCommDeviceActive(
                    isBluetoothScoActive() ? BT_COMM_DEVICE_ACTIVE_SCO : btCommDeviceActiveType);
        }

@@ -2842,7 +2842,7 @@ public class AudioDeviceBroker {
            }
        }
        dispatchCommunicationDevice();
        mAudioService.postUpdateRingerModeServiceInt();
        mAudioService.updateRingerModeMutedStreams();
    }

    @GuardedBy("mDeviceStateLock")
+24 −39
Original line number Diff line number Diff line
@@ -463,7 +463,6 @@ public class AudioService extends IAudioService.Stub
    private static final int MSG_NOTIFY_VOL_EVENT = 22;
    private static final int MSG_DISPATCH_AUDIO_SERVER_STATE = 23;
    private static final int MSG_ENABLE_SURROUND_FORMATS = 24;
    private static final int MSG_UPDATE_RINGER_MODE = 25;
    private static final int MSG_SET_DEVICE_STREAM_VOLUME = 26;
    private static final int MSG_OBSERVE_DEVICES_FOR_ALL_STREAMS = 27;
    private static final int MSG_HDMI_VOLUME_CHECK = 28;
@@ -493,7 +492,6 @@ public class AudioService extends IAudioService.Stub
    private static final int MSG_CONFIGURATION_CHANGED = 54;
    private static final int MSG_BROADCAST_MASTER_MUTE = 55;
    private static final int MSG_UPDATE_CONTEXTUAL_VOLUMES = 56;
    private static final int MSG_BT_COMM_DEVICE_ACTIVE_UPDATE = 57;
    /**
     * Messages handled by the {@link SoundDoseHelper}, do not exceed
@@ -6688,6 +6686,12 @@ public class AudioService extends IAudioService.Stub
        broadcastRingerMode(AudioManager.RINGER_MODE_CHANGED_ACTION, ringerMode);
    }
    /* package */ void updateRingerModeMutedStreams() {
        synchronized (mSettingsLock) {
            muteRingerModeStreams();
        }
    }
    @GuardedBy("mSettingsLock")
    private void muteRingerModeStreams() {
        // Mute stream if not previously muted by ringer mode and (ringer mode
@@ -6709,7 +6713,8 @@ public class AudioService extends IAudioService.Stub
                && (mBtCommDeviceActive.get() == BT_COMM_DEVICE_ACTIVE_BLE_HEADSET
                || mBtCommDeviceActive.get() == BT_COMM_DEVICE_ACTIVE_BLE_SPEAKER);
        // Ask audio policy engine to force use Bluetooth SCO/BLE channel if needed
        final String eventSource = "muteRingerModeStreams() from u/pid:" + Binder.getCallingUid()
        final String eventSource =
                "muteRingerModeStreams() from u/pid:" + Binder.getCallingUid()
                        + "/" + Binder.getCallingPid();
        int forceUse = AudioSystem.FORCE_NONE;
        if (shouldRingSco) {
@@ -6717,7 +6722,8 @@ public class AudioService extends IAudioService.Stub
        } else if (shouldRingBle) {
            forceUse = AudioSystem.FORCE_BT_BLE;
        }
        sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE, AudioSystem.FOR_VIBRATE_RINGING,
        sendMsg(mAudioHandler, MSG_SET_FORCE_USE, SENDMSG_QUEUE,
                AudioSystem.FOR_VIBRATE_RINGING,
                forceUse, eventSource, 0);
        for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) {
@@ -6727,7 +6733,8 @@ public class AudioService extends IAudioService.Stub
            }
            final boolean isMuted = isStreamMutedByRingerOrZenMode(streamType);
            final boolean muteAllowedBySco =
                    !((shouldRingSco || shouldRingBle) && streamType == AudioSystem.STREAM_RING);
                    !((shouldRingSco || shouldRingBle)
                            && streamType == AudioSystem.STREAM_RING);
            final boolean shouldZenMute = isStreamAffectedByCurrentZen(streamType);
            final boolean shouldMute = shouldZenMute || (ringerModeMute
                    && isStreamAffectedByRingerMode(streamType) && muteAllowedBySco);
@@ -6736,7 +6743,8 @@ public class AudioService extends IAudioService.Stub
                // unmute
                // ring and notifications volume should never be 0 when not silenced
                if (sStreamVolumeAlias.get(streamType) == AudioSystem.STREAM_RING
                        || sStreamVolumeAlias.get(streamType) == AudioSystem.STREAM_NOTIFICATION) {
                        || sStreamVolumeAlias.get(streamType)
                        == AudioSystem.STREAM_NOTIFICATION) {
                    synchronized (mVolumeStateLock) {
                        for (int i = 0; i < vss.mIndexMap.size(); i++) {
                            int device = vss.mIndexMap.keyAt(i);
@@ -6805,14 +6813,6 @@ public class AudioService extends IAudioService.Stub
        }
    }
    /*package*/ void postUpdateRingerModeServiceInt() {
        sendMsg(mAudioHandler, MSG_UPDATE_RINGER_MODE, SENDMSG_QUEUE, 0, 0, null, 0);
    }
    private void onUpdateRingerModeServiceInt() {
        setRingerModeInt(getRingerModeInternal(), false);
    }
    /** @see AudioManager#shouldVibrate(int) */
    public boolean shouldVibrate(int vibrateType) {
        if (!mHasVibrator) return false;
@@ -8655,14 +8655,7 @@ public class AudioService extends IAudioService.Stub
                0 /*delay*/);
    }
    /*package*/ void postBtCommDeviceActive(@BtCommDeviceActiveType int btCommDeviceActive) {
        sendMsg(mAudioHandler,
                MSG_BT_COMM_DEVICE_ACTIVE_UPDATE,
                SENDMSG_QUEUE, btCommDeviceActive /*arg1*/, 0 /*arg2*/, null /*obj*/,
                0 /*delay*/);
    }
    private void onUpdateBtCommDeviceActive(@BtCommDeviceActiveType int btCommDeviceActive) {
    /* package */ void updateBtCommDeviceActive(@BtCommDeviceActiveType int btCommDeviceActive) {
        if (mBtCommDeviceActive.getAndSet(btCommDeviceActive) != btCommDeviceActive) {
            final VolumeStreamState vss = getVssForStreamOrDefault(AudioSystem.STREAM_VOICE_CALL);
            vss.updateIndexFactors();
@@ -11068,10 +11061,6 @@ public class AudioService extends IAudioService.Stub
                    onEnableSurroundFormats((ArrayList<Integer>) msg.obj);
                    break;
                case MSG_UPDATE_RINGER_MODE:
                    onUpdateRingerModeServiceInt();
                    break;
                case MSG_SET_DEVICE_STREAM_VOLUME:
                    onSetVolumeIndexOnDevice((DeviceVolumeUpdate) msg.obj);
                    break;
@@ -11206,10 +11195,6 @@ public class AudioService extends IAudioService.Stub
                    onUpdateContextualVolumes();
                    break;
                case MSG_BT_COMM_DEVICE_ACTIVE_UPDATE:
                    onUpdateBtCommDeviceActive(msg.arg1);
                    break;
                case MusicFxHelper.MSG_EFFECT_CLIENT_GONE:
                    mMusicFxHelper.handleMessage(msg);
                    break;