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

Commit faf23358 authored by Eric Laurent's avatar Eric Laurent Committed by Android Git Automerger
Browse files

am 24938df9: Merge "AudioService: synchronized access to volume index" into jb-dev

* commit '24938df9':
  AudioService: synchronized access to volume index
parents 42a419b4 24938df9
Loading
Loading
Loading
Loading
+118 −113
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ import java.io.FileDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -515,6 +516,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                int index = rescaleIndex(streams[i].getIndex(device, false  /* lastAudible */),
                                mStreamVolumeAlias[i],
                                i);
                synchronized (streams[i]) {
                    streams[i].mIndex.put(device, streams[i].getValidIndex(index));
                    streams[i].applyDeviceVolume(device);
                    index = rescaleIndex(streams[i].getIndex(device, true  /* lastAudible */),
@@ -524,6 +526,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                }
            }
        }
    }

    private void dumpStreamStates(PrintWriter pw) {
        pw.println("\nStream volumes (device: index)");
@@ -1112,6 +1115,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                    // on voice capable devices
                    if (mVoiceCapable &&
                            mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) {
                        synchronized (mStreamStates[streamType]) {
                            Set set = mStreamStates[streamType].mLastAudibleIndex.entrySet();
                            Iterator i = set.iterator();
                            while (i.hasNext()) {
@@ -1121,6 +1125,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                                }
                            }
                        }
                    }
                    mStreamStates[streamType].mute(null, false);
                    mRingerModeMutedStreams &= ~(1 << streamType);
                }
@@ -1583,6 +1588,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
        for (int streamType = 0; streamType < numStreamTypes; streamType++) {
            VolumeStreamState streamState = mStreamStates[streamType];

            synchronized (streamState) {
                streamState.readSettings();

                // unmute stream that was muted but is not affect by mute anymore
@@ -1598,6 +1604,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                    streamState.applyAllVolumes();
                }
            }
        }

        // apply new ringer mode
        setRingerModeInt(getRingerMode(), false);
@@ -2232,9 +2239,10 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
        private String mVolumeIndexSettingName;
        private String mLastAudibleVolumeIndexSettingName;
        private int mIndexMax;
        private final HashMap <Integer, Integer> mIndex = new HashMap <Integer, Integer>();
        private final HashMap <Integer, Integer> mLastAudibleIndex =
                                                                new HashMap <Integer, Integer>();
        private final ConcurrentHashMap<Integer, Integer> mIndex =
                                            new ConcurrentHashMap<Integer, Integer>(8, 0.75f, 4);
        private final ConcurrentHashMap<Integer, Integer> mLastAudibleIndex =
                                            new ConcurrentHashMap<Integer, Integer>(8, 0.75f, 4);
        private ArrayList<VolumeDeathHandler> mDeathHandlers; //handles mute/solo clients death

        private VolumeStreamState(String settingName, int streamType) {
@@ -2265,7 +2273,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            return name + "_" + suffix;
        }

        public void readSettings() {
        public synchronized void readSettings() {
            int remainingDevices = AudioSystem.DEVICE_OUT_ALL;

            for (int i = 0; remainingDevices != 0; i++) {
@@ -2339,7 +2347,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                                             device);
        }

        public void applyAllVolumes() {
        public synchronized void applyAllVolumes() {
            // apply default volume first: by convention this will reset all
            // devices volumes in audio policy manager to the supplied value
            AudioSystem.setStreamVolumeIndex(mStreamType,
@@ -2366,7 +2374,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                            true  /* lastAudible */);
        }

        public boolean setIndex(int index, int device, boolean lastAudible) {
        public synchronized boolean setIndex(int index, int device, boolean lastAudible) {
            int oldIndex = getIndex(device, false  /* lastAudible */);
            index = getValidIndex(index);
            mIndex.put(device, getValidIndex(index));
@@ -2400,8 +2408,8 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            }
        }

        public int getIndex(int device, boolean lastAudible) {
            HashMap <Integer, Integer> indexes;
        public synchronized int getIndex(int device, boolean lastAudible) {
            ConcurrentHashMap <Integer, Integer> indexes;
            if (lastAudible) {
                indexes = mLastAudibleIndex;
            } else {
@@ -2415,11 +2423,11 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            return index.intValue();
        }

        public void setLastAudibleIndex(int index, int device) {
        public synchronized void setLastAudibleIndex(int index, int device) {
            mLastAudibleIndex.put(device, getValidIndex(index));
        }

        public void adjustLastAudibleIndex(int deltaIndex, int device) {
        public synchronized void adjustLastAudibleIndex(int deltaIndex, int device) {
            setLastAudibleIndex(getIndex(device,
                                         true  /* lastAudible */) + deltaIndex * 10,
                                device);
@@ -2429,7 +2437,8 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            return mIndexMax;
        }

        public HashMap <Integer, Integer> getAllIndexes(boolean lastAudible) {
        // only called by setAllIndexes() which is already synchronized
        public ConcurrentHashMap <Integer, Integer> getAllIndexes(boolean lastAudible) {
            if (lastAudible) {
                return mLastAudibleIndex;
            } else {
@@ -2437,8 +2446,8 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            }
        }

        public void setAllIndexes(VolumeStreamState srcStream, boolean lastAudible) {
            HashMap <Integer, Integer> indexes = srcStream.getAllIndexes(lastAudible);
        public synchronized void setAllIndexes(VolumeStreamState srcStream, boolean lastAudible) {
            ConcurrentHashMap <Integer, Integer> indexes = srcStream.getAllIndexes(lastAudible);
            Set set = indexes.entrySet();
            Iterator i = set.iterator();
            while (i.hasNext()) {
@@ -2450,7 +2459,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            }
        }

        public void mute(IBinder cb, boolean state) {
        public synchronized void mute(IBinder cb, boolean state) {
            VolumeDeathHandler handler = getDeathHandler(cb, state);
            if (handler == null) {
                Log.e(TAG, "Could not get client death handler for stream: "+mStreamType);
@@ -2481,8 +2490,8 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                mICallback = cb;
            }

            // must be called while synchronized on parent VolumeStreamState
            public void mute(boolean state) {
                synchronized(mDeathHandlers) {
                if (state) {
                    if (mMuteCount == 0) {
                        // Register for client death notification
@@ -2511,7 +2520,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                        } catch (RemoteException e) {
                            // Client has died!
                            binderDied();
                                mDeathHandlers.notify();
                            return;
                        }
                    } else {
@@ -2524,14 +2532,14 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                    } else {
                        mMuteCount--;
                        if (mMuteCount == 0) {
                                // Unregistr from client death notification
                            // Unregister from client death notification
                            mDeathHandlers.remove(this);
                            // mICallback can be 0 if muted by AudioService
                            if (mICallback != null) {
                                mICallback.unlinkToDeath(this, 0);
                            }
                            if (muteCount() == 0) {
                                    // If the stream is not muted any more, restore it's volume if
                                // If the stream is not muted any more, restore its volume if
                                // ringer mode allows it
                                if (!isStreamAffectedByRingerMode(mStreamType) ||
                                        mRingerMode == AudioManager.RINGER_MODE_NORMAL) {
@@ -2556,8 +2564,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                        }
                    }
                }
                    mDeathHandlers.notify();
                }
            }

            public void binderDied() {
@@ -2570,7 +2576,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            }
        }

        private int muteCount() {
        private synchronized int muteCount() {
            int count = 0;
            int size = mDeathHandlers.size();
            for (int i = 0; i < size; i++) {
@@ -2579,8 +2585,8 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            return count;
        }

        // only called by mute() which is already synchronized
        private VolumeDeathHandler getDeathHandler(IBinder cb, boolean state) {
            synchronized(mDeathHandlers) {
            VolumeDeathHandler handler;
            int size = mDeathHandlers.size();
            for (int i = 0; i < size; i++) {
@@ -2599,7 +2605,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            }
            return handler;
        }
        }

        private void dump(PrintWriter pw) {
            pw.print("   Current: ");