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

Commit 789cc0d0 authored by Atneya Nair's avatar Atneya Nair
Browse files

[audio] Move sending volume intent to under lock

Modification of this intent is not thread safe. Since dispatching of the
intent should be in-order wrt the actual set change, move all of the
intent generation and enqueuing code underneath the VSS lock.

Test: compiles
Bug: 374206971
Flag: EXEMPT bugfix
Change-Id: Id023eeed49db2e473e92e3e0d1b328054eacf2c7
parent 289ca076
Loading
Loading
Loading
Loading
+66 −60
Original line number Diff line number Diff line
@@ -9708,8 +9708,6 @@ public class AudioService extends IAudioService.Stub
                            }
                        }
                    }
                }
            }
                    if (changed) {
                        // If associated to volume group, update group cache
                        updateVolumeGroupIndex(device, /* forceMuteState= */ false);
@@ -9717,24 +9715,27 @@ public class AudioService extends IAudioService.Stub
                        oldIndex = (oldIndex + 5) / 10;
                        index = (index + 5) / 10;
                        // log base stream changes to the event log
                if (sStreamVolumeAlias.get(mStreamType, /*valueIfKeyNotFound=*/-1) == mStreamType) {
                        if (sStreamVolumeAlias.get(mStreamType, /*valueIfKeyNotFound=*/-1)
                                == mStreamType) {
                            if (caller == null) {
                                Log.w(TAG, "No caller for volume_changed event", new Throwable());
                            }
                    EventLogTags.writeVolumeChanged(mStreamType, oldIndex, index, mIndexMax / 10,
                            caller);
                            EventLogTags.writeVolumeChanged(
                                    mStreamType, oldIndex, index, mIndexMax / 10, caller);
                        }
                // fire changed intents for all streams, but only when the device it changed on
                        // fire changed intents for all streams, but only when the device it changed
                        // on
                        //  is the current device
                        if ((index != oldIndex) && isCurrentDevice) {
                            // for single volume devices, only send the volume change broadcast
                            // on the alias stream
                    final int streamAlias = sStreamVolumeAlias.get(
                            mStreamType, /*valueIfKeyNotFound=*/-1);
                            final int streamAlias =
                                    sStreamVolumeAlias.get(mStreamType, /*valueIfKeyNotFound=*/-1);
                            if (!mIsSingleVolume || streamAlias == mStreamType) {
                        mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index);
                        mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE,
                                oldIndex);
                                mVolumeChanged.putExtra(
                                        AudioManager.EXTRA_VOLUME_STREAM_VALUE, index);
                                mVolumeChanged.putExtra(
                                        AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex);
                                int extraStreamType = mStreamType;
                                // TODO: remove this when deprecating STREAM_BLUETOOTH_SCO
                                if (isStreamBluetoothSco(mStreamType)) {
@@ -9742,11 +9743,11 @@ public class AudioService extends IAudioService.Stub
                                            AudioSystem.STREAM_BLUETOOTH_SCO);
                                    extraStreamType = AudioSystem.STREAM_BLUETOOTH_SCO;
                                } else {
                            mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE,
                                    mStreamType);
                                    mVolumeChanged.putExtra(
                                            AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType);
                                }
                        mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS,
                                streamAlias);
                                mVolumeChanged.putExtra(
                                        AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS, streamAlias);
                                if (mStreamType == streamAlias) {
                                    String aliasStreamIndexesString = "";
@@ -9754,18 +9755,21 @@ public class AudioService extends IAudioService.Stub
                                        aliasStreamIndexesString =
                                                " aliased streams: " + aliasStreamIndexes;
                                    }
                            AudioService.sVolumeLogger.enqueue(new VolChangedBroadcastEvent(
                                    extraStreamType, aliasStreamIndexesString, index, oldIndex));
                                    AudioService.sVolumeLogger.enqueue(
                                            new VolChangedBroadcastEvent(extraStreamType,
                                                    aliasStreamIndexesString, index, oldIndex));
                                    if (extraStreamType != mStreamType) {
                                AudioService.sVolumeLogger.enqueue(new VolChangedBroadcastEvent(
                                        mStreamType, aliasStreamIndexesString, index, oldIndex));
                                        AudioService.sVolumeLogger.enqueue(
                                                new VolChangedBroadcastEvent(mStreamType,
                                                        aliasStreamIndexesString, index, oldIndex));
                                    }
                                }
                                sendBroadcastToAll(mVolumeChanged, mVolumeChangedOptions);
                                if (extraStreamType != mStreamType) {
                            // send multiple intents in case we merged voice call and bt sco streams
                            mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE,
                                    mStreamType);
                                    // send multiple intents in case we merged voice call and bt sco
                                    // streams
                                    mVolumeChanged.putExtra(
                                            AudioManager.EXTRA_VOLUME_STREAM_TYPE, mStreamType);
                                    // do not use the options in thid case which could discard
                                    // the previous intent
                                    sendBroadcastToAll(mVolumeChanged, null);
@@ -9775,6 +9779,8 @@ public class AudioService extends IAudioService.Stub
                    }
                    return changed;
                }
            }
        }
        public int getIndex(int device) {
            synchronized (VolumeStreamState.class) {