diff options
author | Eric Laurent <elaurent@google.com> | 2013-04-02 00:32:32 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2013-04-02 00:32:32 +0000 |
commit | 742647c809622703207c34171d482c562e34b10b (patch) | |
tree | 6d351f39fbb110a931d31ba1a5f61125baa895c2 /media | |
parent | d42ae93c44a28f63d8f266277644864bfd0f4a16 (diff) | |
parent | 42b041ed4f184e3af7f788eb07307f556a2a6616 (diff) | |
download | frameworks_base-742647c809622703207c34171d482c562e34b10b.zip frameworks_base-742647c809622703207c34171d482c562e34b10b.tar.gz frameworks_base-742647c809622703207c34171d482c562e34b10b.tar.bz2 |
Merge "AudioService: removed last audible volume index" into jb-mr2-dev
Diffstat (limited to 'media')
-rw-r--r-- | media/java/android/media/AudioService.java | 498 |
1 files changed, 136 insertions, 362 deletions
diff --git a/media/java/android/media/AudioService.java b/media/java/android/media/AudioService.java index 9ded922..e35f47b 100644 --- a/media/java/android/media/AudioService.java +++ b/media/java/android/media/AudioService.java @@ -169,11 +169,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished { private static final int MSG_RCC_NEW_PLAYBACK_STATE = 32; - // flags for MSG_PERSIST_VOLUME indicating if current and/or last audible volume should be - // persisted - private static final int PERSIST_CURRENT = 0x1; - private static final int PERSIST_LAST_AUDIBLE = 0x2; - private static final int BTA2DP_DOCK_TIMEOUT_MILLIS = 8000; // Timeout for connection to bluetooth headset service private static final int BT_HEADSET_CNCT_TIMEOUT_MS = 3000; @@ -582,14 +577,10 @@ public class AudioService extends IAudioService.Stub implements OnFinished { for (int streamType = 0; streamType < numStreamTypes; streamType++) { if (streamType != mStreamVolumeAlias[streamType]) { mStreamStates[streamType]. - setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]], - false /*lastAudible*/); - mStreamStates[streamType]. - setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]], - true /*lastAudible*/); + setAllIndexes(mStreamStates[mStreamVolumeAlias[streamType]]); } // apply stream volume - if (mStreamStates[streamType].muteCount() == 0) { + if (!mStreamStates[streamType].isMuted()) { mStreamStates[streamType].applyAllVolumes(); } } @@ -633,10 +624,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { } mStreamVolumeAlias[AudioSystem.STREAM_DTMF] = dtmfStreamAlias; if (updateVolumes) { - mStreamStates[AudioSystem.STREAM_DTMF].setAllIndexes(mStreamStates[dtmfStreamAlias], - false /*lastAudible*/); - mStreamStates[AudioSystem.STREAM_DTMF].setAllIndexes(mStreamStates[dtmfStreamAlias], - true /*lastAudible*/); + mStreamStates[AudioSystem.STREAM_DTMF].setAllIndexes(mStreamStates[dtmfStreamAlias]); sendMsg(mAudioHandler, MSG_SET_ALL_VOLUMES, SENDMSG_QUEUE, @@ -836,14 +824,9 @@ public class AudioService extends IAudioService.Stub implements OnFinished { final int device = getDeviceForStream(streamTypeAlias); - // get last audible index if stream is muted, current index otherwise - int aliasIndex = streamState.getIndex(device, - (streamState.muteCount() != 0) /* lastAudible */); + int aliasIndex = streamState.getIndex(device); boolean adjustVolume = true; - int step; - int index; - int oldIndex; // reset any pending volume command synchronized (mSafeMediaVolumeState) { @@ -872,64 +855,40 @@ public class AudioService extends IAudioService.Stub implements OnFinished { step = rescaleIndex(10, streamType, streamTypeAlias); } - if ((direction == AudioManager.ADJUST_RAISE) && - !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { - index = mStreamStates[streamType].getIndex(device, - (streamState.muteCount() != 0) /* lastAudible */); - oldIndex = index; - mVolumePanel.postDisplaySafeVolumeWarning(flags); - } else { - // If either the client forces allowing ringer modes for this adjustment, - // or the stream type is one that is affected by ringer modes - if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || - (streamTypeAlias == getMasterStreamType())) { - int ringerMode = getRingerMode(); - // do not vibrate if already in vibrate mode - if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { - flags &= ~AudioManager.FLAG_VIBRATE; - } - // Check if the ringer mode changes with this volume adjustment. If - // it does, it will handle adjusting the volume, so we won't below - adjustVolume = checkForRingerModeChange(aliasIndex, direction, step); - if ((streamTypeAlias == getMasterStreamType()) && - (mRingerMode == AudioManager.RINGER_MODE_SILENT)) { - streamState.setLastAudibleIndex(0, device); - } - } - - // If stream is muted, adjust last audible index only - oldIndex = mStreamStates[streamType].getIndex(device, - (mStreamStates[streamType].muteCount() != 0) /* lastAudible */); - - if (streamState.muteCount() != 0) { - if (adjustVolume) { - // Post a persist volume msg - // no need to persist volume on all streams sharing the same alias - streamState.adjustLastAudibleIndex(direction * step, device); - sendMsg(mAudioHandler, - MSG_PERSIST_VOLUME, - SENDMSG_QUEUE, - PERSIST_LAST_AUDIBLE, - device, - streamState, - PERSIST_DELAY); - } - index = mStreamStates[streamType].getIndex(device, true /* lastAudible */); - } else { - if (adjustVolume && streamState.adjustIndex(direction * step, device)) { - // Post message to set system volume (it in turn will post a message - // to persist). Do not change volume if stream is muted. - sendMsg(mAudioHandler, - MSG_SET_DEVICE_VOLUME, - SENDMSG_QUEUE, - device, - 0, - streamState, - 0); - } - index = mStreamStates[streamType].getIndex(device, false /* lastAudible */); + // If either the client forces allowing ringer modes for this adjustment, + // or the stream type is one that is affected by ringer modes + if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || + (streamTypeAlias == getMasterStreamType())) { + int ringerMode = getRingerMode(); + // do not vibrate if already in vibrate mode + if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) { + flags &= ~AudioManager.FLAG_VIBRATE; + } + // Check if the ringer mode changes with this volume adjustment. If + // it does, it will handle adjusting the volume, so we won't below + adjustVolume = checkForRingerModeChange(aliasIndex, direction, step); + } + + int oldIndex = mStreamStates[streamType].getIndex(device); + + if (adjustVolume && (direction != AudioManager.ADJUST_SAME)) { + if ((direction == AudioManager.ADJUST_RAISE) && + !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) { + Log.e(TAG, "adjustStreamVolume() safe volume index = "+oldIndex); + mVolumePanel.postDisplaySafeVolumeWarning(flags); + } else if (streamState.adjustIndex(direction * step, device)) { + // Post message to set system volume (it in turn will post a message + // to persist). Do not change volume if stream is muted. + sendMsg(mAudioHandler, + MSG_SET_DEVICE_VOLUME, + SENDMSG_QUEUE, + device, + 0, + streamState, + 0); } } + int index = mStreamStates[streamType].getIndex(device); sendVolumeUpdate(streamType, oldIndex, index, flags); } @@ -969,6 +928,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { }; private void onSetStreamVolume(int streamType, int index, int flags, int device) { + setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, false); // setting volume on master stream type also controls silent mode if (((flags & AudioManager.FLAG_ALLOW_RINGER_MODES) != 0) || (mStreamVolumeAlias[streamType] == getMasterStreamType())) { @@ -976,18 +936,11 @@ public class AudioService extends IAudioService.Stub implements OnFinished { if (index == 0) { newRingerMode = mHasVibrator ? AudioManager.RINGER_MODE_VIBRATE : AudioManager.RINGER_MODE_SILENT; - setStreamVolumeInt(mStreamVolumeAlias[streamType], - index, - device, - false, - true); } else { newRingerMode = AudioManager.RINGER_MODE_NORMAL; } setRingerMode(newRingerMode); } - - setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, false, true); } /** @see AudioManager#setStreamVolume(int, int, int) */ @@ -1006,9 +959,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { // reset any pending volume command mPendingVolumeCommand = null; - // get last audible index if stream is muted, current index otherwise - oldIndex = streamState.getIndex(device, - (streamState.muteCount() != 0) /* lastAudible */); + oldIndex = streamState.getIndex(device); index = rescaleIndex(index * 10, streamType, mStreamVolumeAlias[streamType]); @@ -1034,9 +985,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { streamType, index, flags, device); } else { onSetStreamVolume(streamType, index, flags, device); - // get last audible index if stream is muted, current index otherwise - index = mStreamStates[streamType].getIndex(device, - (mStreamStates[streamType].muteCount() != 0) /* lastAudible */); + index = mStreamStates[streamType].getIndex(device); } } sendVolumeUpdate(streamType, oldIndex, index, flags); @@ -1198,41 +1147,23 @@ public class AudioService extends IAudioService.Stub implements OnFinished { * @param device the device whose volume must be changed * @param force If true, set the volume even if the desired volume is same * as the current volume. - * @param lastAudible If true, stores new index as last audible one */ private void setStreamVolumeInt(int streamType, int index, int device, - boolean force, - boolean lastAudible) { + boolean force) { VolumeStreamState streamState = mStreamStates[streamType]; - // If stream is muted, set last audible index only - if (streamState.muteCount() != 0) { - // Do not allow last audible index to be 0 - if (index != 0) { - streamState.setLastAudibleIndex(index, device); - // Post a persist volume msg - sendMsg(mAudioHandler, - MSG_PERSIST_VOLUME, - SENDMSG_QUEUE, - PERSIST_LAST_AUDIBLE, - device, - streamState, - PERSIST_DELAY); - } - } else { - if (streamState.setIndex(index, device, lastAudible) || force) { - // Post message to set system volume (it in turn will post a message - // to persist). - sendMsg(mAudioHandler, - MSG_SET_DEVICE_VOLUME, - SENDMSG_QUEUE, - device, - 0, - streamState, - 0); - } + if (streamState.setIndex(index, device) || force) { + // Post message to set system volume (it in turn will post a message + // to persist). + sendMsg(mAudioHandler, + MSG_SET_DEVICE_VOLUME, + SENDMSG_QUEUE, + device, + 0, + streamState, + 0); } } @@ -1244,7 +1175,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished { for (int stream = 0; stream < mStreamStates.length; stream++) { if (!isStreamAffectedByMute(stream) || stream == streamType) continue; - // Bring back last audible volume mStreamStates[stream].mute(cb, state); } } @@ -1262,7 +1192,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { /** get stream mute state. */ public boolean isStreamMute(int streamType) { - return (mStreamStates[streamType].muteCount() != 0); + return mStreamStates[streamType].isMuted(); } /** @see AudioManager#setMasterMute(boolean, int) */ @@ -1289,8 +1219,12 @@ public class AudioService extends IAudioService.Stub implements OnFinished { public int getStreamVolume(int streamType) { ensureValidStreamType(streamType); int device = getDeviceForStream(streamType); - int index = mStreamStates[streamType].getIndex(device, false /* lastAudible */); + int index = mStreamStates[streamType].getIndex(device); + // by convention getStreamVolume() returns 0 when a stream is muted. + if (mStreamStates[streamType].isMuted()) { + index = 0; + } if (index != 0 && (mStreamVolumeAlias[streamType] == AudioSystem.STREAM_MUSIC) && (device & mFixedVolumeDevices) != 0) { index = mStreamStates[streamType].getMaxIndex(); @@ -1347,7 +1281,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { public int getLastAudibleStreamVolume(int streamType) { ensureValidStreamType(streamType); int device = getDeviceForStream(streamType); - return (mStreamStates[streamType].getIndex(device, true /* lastAudible */) + 5) / 10; + return (mStreamStates[streamType].getIndex(device) + 5) / 10; } /** Get last audible master volume before it was muted. */ @@ -1412,7 +1346,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { if (mVoiceCapable && mStreamVolumeAlias[streamType] == AudioSystem.STREAM_RING) { synchronized (mStreamStates[streamType]) { - Set set = mStreamStates[streamType].mLastAudibleIndex.entrySet(); + Set set = mStreamStates[streamType].mIndex.entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry entry = (Map.Entry)i.next(); @@ -1661,8 +1595,8 @@ public class AudioService extends IAudioService.Stub implements OnFinished { streamType = AudioManager.STREAM_MUSIC; } int device = getDeviceForStream(streamType); - int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device, false); - setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true, false); + int index = mStreamStates[mStreamVolumeAlias[streamType]].getIndex(device); + setStreamVolumeInt(mStreamVolumeAlias[streamType], index, device, true); updateStreamVolumeAlias(true /*updateVolumes*/); } @@ -1896,7 +1830,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { streamState.readSettings(); // unmute stream that was muted but is not affect by mute anymore - if (streamState.muteCount() != 0 && ((!isStreamAffectedByMute(streamType) && + if (streamState.isMuted() && ((!isStreamAffectedByMute(streamType) && !isStreamMutedByRingerMode(streamType)) || mUseFixedVolume)) { int size = streamState.mDeathHandlers.size(); for (int i = 0; i < size; i++) { @@ -2396,8 +2330,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { 0, null, MUSIC_ACTIVE_POLL_PERIOD_MS); - int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device, - false /*lastAudible*/); + int index = mStreamStates[AudioSystem.STREAM_MUSIC].getIndex(device); if (AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) && (index > mSafeMediaVolumeIndex)) { // Approximate cumulative active music time @@ -2742,18 +2675,14 @@ public class AudioService extends IAudioService.Stub implements OnFinished { private final int mStreamType; private String mVolumeIndexSettingName; - private String mLastAudibleVolumeIndexSettingName; private int mIndexMax; 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) { mVolumeIndexSettingName = settingName; - mLastAudibleVolumeIndexSettingName = settingName + System.APPEND_FOR_LAST_AUDIBLE; mStreamType = streamType; mIndexMax = MAX_STREAM_VOLUME[streamType]; @@ -2766,10 +2695,8 @@ public class AudioService extends IAudioService.Stub implements OnFinished { readSettings(); } - public String getSettingNameForDevice(boolean lastAudible, int device) { - String name = lastAudible ? - mLastAudibleVolumeIndexSettingName : - mVolumeIndexSettingName; + public String getSettingNameForDevice(int device) { + String name = mVolumeIndexSettingName; String suffix = AudioSystem.getDeviceName(device); if (suffix.isEmpty()) { return name; @@ -2781,14 +2708,11 @@ public class AudioService extends IAudioService.Stub implements OnFinished { // force maximum volume on all streams if fixed volume property is set if (mUseFixedVolume) { mIndex.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); - mLastAudibleIndex.put(AudioSystem.DEVICE_OUT_DEFAULT, mIndexMax); return; } // do not read system stream volume from settings: this stream is always aliased // to another stream type and its volume is never persisted. Values in settings can // only be stale values - // on first call to readSettings() at init time, muteCount() is always 0 so we will - // always create entries for default device if ((mStreamType == AudioSystem.STREAM_SYSTEM) || (mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED)) { int index = 10 * AudioManager.DEFAULT_STREAM_VOLUME[mStreamType]; @@ -2797,10 +2721,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { index = mIndexMax; } } - if (muteCount() == 0) { - mIndex.put(AudioSystem.DEVICE_OUT_DEFAULT, index); - } - mLastAudibleIndex.put(AudioSystem.DEVICE_OUT_DEFAULT, index); + mIndex.put(AudioSystem.DEVICE_OUT_DEFAULT, index); return; } @@ -2814,7 +2735,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { remainingDevices &= ~device; // retrieve current volume for device - String name = getSettingNameForDevice(false /* lastAudible */, device); + String name = getSettingNameForDevice(device); // if no volume stored for current stream and device, use default volume if default // device, continue otherwise int defaultIndex = (device == AudioSystem.DEVICE_OUT_DEFAULT) ? @@ -2828,72 +2749,33 @@ public class AudioService extends IAudioService.Stub implements OnFinished { // ignore settings for fixed volume devices: volume should always be at max or 0 if ((mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_MUSIC) && ((device & mFixedVolumeDevices) != 0)) { - if ((muteCount()) == 0 && (index != 0)) { - mIndex.put(device, mIndexMax); - } else { - mIndex.put(device, 0); - } - mLastAudibleIndex.put(device, mIndexMax); - continue; - } - - // retrieve last audible volume for device - name = getSettingNameForDevice(true /* lastAudible */, device); - // use stored last audible index if present, otherwise use current index if not 0 - // or default index - defaultIndex = (index > 0) ? - index : AudioManager.DEFAULT_STREAM_VOLUME[mStreamType]; - int lastAudibleIndex = Settings.System.getIntForUser( - mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT); - - // a last audible index of 0 should never be stored for ring and notification - // streams on phones (voice capable devices). - if ((lastAudibleIndex == 0) && mVoiceCapable && - (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_RING)) { - lastAudibleIndex = AudioManager.DEFAULT_STREAM_VOLUME[mStreamType]; - // Correct the data base - sendMsg(mAudioHandler, - MSG_PERSIST_VOLUME, - SENDMSG_QUEUE, - PERSIST_LAST_AUDIBLE, - device, - this, - PERSIST_DELAY); - } - mLastAudibleIndex.put(device, getValidIndex(10 * lastAudibleIndex)); - // the initial index should never be 0 for ring and notification streams on phones - // (voice capable devices) if not in silent or vibrate mode. - if ((index == 0) && (mRingerMode == AudioManager.RINGER_MODE_NORMAL) && - mVoiceCapable && - (mStreamVolumeAlias[mStreamType] == AudioSystem.STREAM_RING)) { - index = lastAudibleIndex; - // Correct the data base - sendMsg(mAudioHandler, - MSG_PERSIST_VOLUME, - SENDMSG_QUEUE, - PERSIST_CURRENT, - device, - this, - PERSIST_DELAY); - } - if (muteCount() == 0) { + mIndex.put(device, (index != 0) ? mIndexMax : 0); + } else { mIndex.put(device, getValidIndex(10 * index)); } } } public void applyDeviceVolume(int device) { - AudioSystem.setStreamVolumeIndex(mStreamType, - (getIndex(device, false /* lastAudible */) + 5)/10, - device); + int index; + if (isMuted()) { + index = 0; + } else { + index = (getIndex(device) + 5)/10; + } + AudioSystem.setStreamVolumeIndex(mStreamType, index, device); } 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, - (getIndex(AudioSystem.DEVICE_OUT_DEFAULT, false /* lastAudible */) + 5)/10, - AudioSystem.DEVICE_OUT_DEFAULT); + int index; + if (isMuted()) { + index = 0; + } else { + index = (getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5)/10; + } + AudioSystem.setStreamVolumeIndex(mStreamType, index, AudioSystem.DEVICE_OUT_DEFAULT); // then apply device specific volumes Set set = mIndex.entrySet(); Iterator i = set.iterator(); @@ -2901,22 +2783,23 @@ public class AudioService extends IAudioService.Stub implements OnFinished { Map.Entry entry = (Map.Entry)i.next(); int device = ((Integer)entry.getKey()).intValue(); if (device != AudioSystem.DEVICE_OUT_DEFAULT) { - AudioSystem.setStreamVolumeIndex(mStreamType, - ((Integer)entry.getValue() + 5)/10, - device); + if (isMuted()) { + index = 0; + } else { + index = ((Integer)entry.getValue() + 5)/10; + } + AudioSystem.setStreamVolumeIndex(mStreamType, index, device); } } } public boolean adjustIndex(int deltaIndex, int device) { - return setIndex(getIndex(device, - false /* lastAudible */) + deltaIndex, - device, - true /* lastAudible */); + return setIndex(getIndex(device) + deltaIndex, + device); } - public synchronized boolean setIndex(int index, int device, boolean lastAudible) { - int oldIndex = getIndex(device, false /* lastAudible */); + public synchronized boolean setIndex(int index, int device) { + int oldIndex = getIndex(device); index = getValidIndex(index); synchronized (mCameraSoundForced) { if ((mStreamType == AudioSystem.STREAM_SYSTEM_ENFORCED) && mCameraSoundForced) { @@ -2926,9 +2809,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished { mIndex.put(device, index); if (oldIndex != index) { - if (lastAudible) { - mLastAudibleIndex.put(device, index); - } // Apply change to all streams using this one as alias // if changing volume of current device, also change volume of current // device on aliased stream @@ -2939,12 +2819,10 @@ public class AudioService extends IAudioService.Stub implements OnFinished { mStreamVolumeAlias[streamType] == mStreamType) { int scaledIndex = rescaleIndex(index, mStreamType, streamType); mStreamStates[streamType].setIndex(scaledIndex, - device, - lastAudible); + device); if (currentDevice) { mStreamStates[streamType].setIndex(scaledIndex, - getDeviceForStream(streamType), - lastAudible); + getDeviceForStream(streamType)); } } } @@ -2954,63 +2832,21 @@ public class AudioService extends IAudioService.Stub implements OnFinished { } } - public synchronized int getIndex(int device, boolean lastAudible) { - ConcurrentHashMap <Integer, Integer> indexes; - if (lastAudible) { - indexes = mLastAudibleIndex; - } else { - indexes = mIndex; - } - Integer index = indexes.get(device); + public synchronized int getIndex(int device) { + Integer index = mIndex.get(device); if (index == null) { // there is always an entry for AudioSystem.DEVICE_OUT_DEFAULT - index = indexes.get(AudioSystem.DEVICE_OUT_DEFAULT); + index = mIndex.get(AudioSystem.DEVICE_OUT_DEFAULT); } return index.intValue(); } - public synchronized void setLastAudibleIndex(int index, int device) { - // Apply change to all streams using this one as alias - // if changing volume of current device, also change volume of current - // device on aliased stream - boolean currentDevice = (device == getDeviceForStream(mStreamType)); - int numStreamTypes = AudioSystem.getNumStreamTypes(); - for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) { - if (streamType != mStreamType && - mStreamVolumeAlias[streamType] == mStreamType) { - int scaledIndex = rescaleIndex(index, mStreamType, streamType); - mStreamStates[streamType].setLastAudibleIndex(scaledIndex, device); - if (currentDevice) { - mStreamStates[streamType].setLastAudibleIndex(scaledIndex, - getDeviceForStream(streamType)); - } - } - } - mLastAudibleIndex.put(device, getValidIndex(index)); - } - - public synchronized void adjustLastAudibleIndex(int deltaIndex, int device) { - setLastAudibleIndex(getIndex(device, - true /* lastAudible */) + deltaIndex, - device); - } - public int getMaxIndex() { return mIndexMax; } - // only called by setAllIndexes() which is already synchronized - public ConcurrentHashMap <Integer, Integer> getAllIndexes(boolean lastAudible) { - if (lastAudible) { - return mLastAudibleIndex; - } else { - return mIndex; - } - } - - public synchronized void setAllIndexes(VolumeStreamState srcStream, boolean lastAudible) { - ConcurrentHashMap <Integer, Integer> indexes = srcStream.getAllIndexes(lastAudible); - Set set = indexes.entrySet(); + public synchronized void setAllIndexes(VolumeStreamState srcStream) { + Set set = srcStream.mIndex.entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry entry = (Map.Entry)i.next(); @@ -3018,11 +2854,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { int index = ((Integer)entry.getValue()).intValue(); index = rescaleIndex(index, srcStream.getStreamType(), mStreamType); - if (lastAudible) { - setLastAudibleIndex(index, device); - } else { - setIndex(index, device, false /* lastAudible */); - } + setIndex(index, device); } } @@ -3033,12 +2865,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished { Map.Entry entry = (Map.Entry)i.next(); entry.setValue(mIndexMax); } - set = mLastAudibleIndex.entrySet(); - i = set.iterator(); - while (i.hasNext()) { - Map.Entry entry = (Map.Entry)i.next(); - entry.setValue(mIndexMax); - } } public synchronized void mute(IBinder cb, boolean state) { @@ -3074,6 +2900,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { // must be called while synchronized on parent VolumeStreamState public void mute(boolean state) { + boolean updateVolume = false; if (state) { if (mMuteCount == 0) { // Register for client death notification @@ -3082,22 +2909,10 @@ public class AudioService extends IAudioService.Stub implements OnFinished { if (mICallback != null) { mICallback.linkToDeath(this, 0); } - mDeathHandlers.add(this); + VolumeStreamState.this.mDeathHandlers.add(this); // If the stream is not yet muted by any client, set level to 0 - if (muteCount() == 0) { - Set set = mIndex.entrySet(); - Iterator i = set.iterator(); - while (i.hasNext()) { - Map.Entry entry = (Map.Entry)i.next(); - int device = ((Integer)entry.getKey()).intValue(); - setIndex(0, device, false /* lastAudible */); - } - sendMsg(mAudioHandler, - MSG_SET_ALL_VOLUMES, - SENDMSG_QUEUE, - 0, - 0, - VolumeStreamState.this, 0); + if (!VolumeStreamState.this.isMuted()) { + updateVolume = true; } } catch (RemoteException e) { // Client has died! @@ -3115,37 +2930,25 @@ public class AudioService extends IAudioService.Stub implements OnFinished { mMuteCount--; if (mMuteCount == 0) { // Unregister from client death notification - mDeathHandlers.remove(this); + VolumeStreamState.this.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 its volume if - // ringer mode allows it - if (!isStreamAffectedByRingerMode(mStreamType) || - mRingerMode == AudioManager.RINGER_MODE_NORMAL) { - Set set = mIndex.entrySet(); - Iterator i = set.iterator(); - while (i.hasNext()) { - Map.Entry entry = (Map.Entry)i.next(); - int device = ((Integer)entry.getKey()).intValue(); - setIndex(getIndex(device, - true /* lastAudible */), - device, - false /* lastAudible */); - } - sendMsg(mAudioHandler, - MSG_SET_ALL_VOLUMES, - SENDMSG_QUEUE, - 0, - 0, - VolumeStreamState.this, 0); - } + if (!VolumeStreamState.this.isMuted()) { + updateVolume = true; } } } } + if (updateVolume) { + sendMsg(mAudioHandler, + MSG_SET_ALL_VOLUMES, + SENDMSG_QUEUE, + 0, + 0, + VolumeStreamState.this, 0); + } } public void binderDied() { @@ -3167,6 +2970,10 @@ public class AudioService extends IAudioService.Stub implements OnFinished { return count; } + private synchronized boolean isMuted() { + return muteCount() != 0; + } + // only called by mute() which is already synchronized private VolumeDeathHandler getDeathHandler(IBinder cb, boolean state) { VolumeDeathHandler handler; @@ -3199,14 +3006,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished { pw.print(Integer.toHexString(((Integer)entry.getKey()).intValue()) + ": " + ((((Integer)entry.getValue()).intValue() + 5) / 10)+", "); } - pw.print("\n Last audible: "); - set = mLastAudibleIndex.entrySet(); - i = set.iterator(); - while (i.hasNext()) { - Map.Entry entry = (Map.Entry)i.next(); - pw.print(Integer.toHexString(((Integer)entry.getKey()).intValue()) - + ": " + ((((Integer)entry.getValue()).intValue() + 5) / 10)+", "); - } } } @@ -3254,8 +3053,8 @@ public class AudioService extends IAudioService.Stub implements OnFinished { sendMsg(mAudioHandler, MSG_PERSIST_VOLUME, SENDMSG_QUEUE, - PERSIST_CURRENT|PERSIST_LAST_AUDIBLE, device, + 0, streamState, PERSIST_DELAY); @@ -3276,24 +3075,14 @@ public class AudioService extends IAudioService.Stub implements OnFinished { } } - private void persistVolume(VolumeStreamState streamState, - int persistType, - int device) { + private void persistVolume(VolumeStreamState streamState, int device) { if (mUseFixedVolume) { return; } - if ((persistType & PERSIST_CURRENT) != 0) { - System.putIntForUser(mContentResolver, - streamState.getSettingNameForDevice(false /* lastAudible */, device), - (streamState.getIndex(device, false /* lastAudible */) + 5)/ 10, - UserHandle.USER_CURRENT); - } - if ((persistType & PERSIST_LAST_AUDIBLE) != 0) { - System.putIntForUser(mContentResolver, - streamState.getSettingNameForDevice(true /* lastAudible */, device), - (streamState.getIndex(device, true /* lastAudible */) + 5) / 10, - UserHandle.USER_CURRENT); - } + System.putIntForUser(mContentResolver, + streamState.getSettingNameForDevice(device), + (streamState.getIndex(device) + 5)/ 10, + UserHandle.USER_CURRENT); } private void persistRingerMode(int ringerMode) { @@ -3545,7 +3334,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { break; case MSG_PERSIST_VOLUME: - persistVolume((VolumeStreamState) msg.obj, msg.arg1, msg.arg2); + persistVolume((VolumeStreamState) msg.obj, msg.arg1); break; case MSG_PERSIST_MASTER_VOLUME: @@ -6391,10 +6180,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished { mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_SYSTEM_ENFORCED); } else { - s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM], - false /*lastAudible*/); - s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM], - true /*lastAudible*/); + s.setAllIndexes(mStreamStates[AudioSystem.STREAM_SYSTEM]); mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_SYSTEM_ENFORCED); } @@ -6540,7 +6326,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished { private void enforceSafeMediaVolume() { VolumeStreamState streamState = mStreamStates[AudioSystem.STREAM_MUSIC]; - boolean lastAudible = (streamState.muteCount() != 0); int devices = mSafeMediaVolumeDevices; int i = 0; @@ -6549,27 +6334,16 @@ public class AudioService extends IAudioService.Stub implements OnFinished { if ((device & devices) == 0) { continue; } - int index = streamState.getIndex(device, lastAudible); + int index = streamState.getIndex(device); if (index > mSafeMediaVolumeIndex) { - if (lastAudible) { - streamState.setLastAudibleIndex(mSafeMediaVolumeIndex, device); - sendMsg(mAudioHandler, - MSG_PERSIST_VOLUME, - SENDMSG_QUEUE, - PERSIST_LAST_AUDIBLE, - device, - streamState, - PERSIST_DELAY); - } else { - streamState.setIndex(mSafeMediaVolumeIndex, device, true); - sendMsg(mAudioHandler, - MSG_SET_DEVICE_VOLUME, - SENDMSG_QUEUE, - device, - 0, - streamState, - 0); - } + streamState.setIndex(mSafeMediaVolumeIndex, device); + sendMsg(mAudioHandler, + MSG_SET_DEVICE_VOLUME, + SENDMSG_QUEUE, + device, + 0, + streamState, + 0); } devices &= ~device; } |