@@ -159,6 +159,9 @@ public class AudioService extends IAudioService.Stub {
159159 // but to support integer based AudioManager API we translate it to 0 - 100
160160 private static final int MAX_MASTER_VOLUME = 100 ;
161161
162+ // Maximum volume adjust steps allowed in a single batch call.
163+ private static final int MAX_BATCH_VOLUME_ADJUST_STEPS = 4 ;
164+
162165 /* Sound effect file names */
163166 private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/" ;
164167 private static final String [] SOUND_EFFECT_FILES = new String [] {
@@ -619,38 +622,19 @@ public void adjustStreamVolume(int streamType, int direction, int flags) {
619622 }
620623
621624 /** @see AudioManager#adjustMasterVolume(int) */
622- public void adjustMasterVolume (int direction , int flags ) {
623- ensureValidDirection ( direction );
625+ public void adjustMasterVolume (int steps , int flags ) {
626+ ensureValidSteps ( steps );
624627 int volume = Math .round (AudioSystem .getMasterVolume () * MAX_MASTER_VOLUME );
625628 int delta = 0 ;
626-
627- if (direction == AudioManager .ADJUST_RAISE ) {
628- // This is the default value if we make it to the end
629- delta = mMasterVolumeRamp [1 ];
630- // If we're raising the volume move down the ramp array until we
631- // find the volume we're above and use that groups delta.
632- for (int i = mMasterVolumeRamp .length - 1 ; i > 1 ; i -= 2 ) {
633- if (volume >= mMasterVolumeRamp [i - 1 ]) {
634- delta = mMasterVolumeRamp [i ];
635- break ;
636- }
637- }
638- } else if (direction == AudioManager .ADJUST_LOWER ){
639- int length = mMasterVolumeRamp .length ;
640- // This is the default value if we make it to the end
641- delta = -mMasterVolumeRamp [length - 1 ];
642- // If we're lowering the volume move up the ramp array until we
643- // find the volume we're below and use the group below it's delta
644- for (int i = 2 ; i < length ; i += 2 ) {
645- if (volume <= mMasterVolumeRamp [i ]) {
646- delta = -mMasterVolumeRamp [i - 1 ];
647- break ;
648- }
649- }
629+ int numSteps = Math .abs (steps );
630+ int direction = steps > 0 ? AudioManager .ADJUST_RAISE : AudioManager .ADJUST_LOWER ;
631+ for (int i = 0 ; i < numSteps ; ++i ) {
632+ delta = findVolumeDelta (direction , volume );
633+ volume += delta ;
650634 }
651635
652- // Log.d(TAG, "adjustMasterVolume volume: " + volume + " delta : " + delta + " direction: " + direction );
653- setMasterVolume (volume + delta , flags );
636+ // Log.d(TAG, "adjustMasterVolume volume: " + volume + " steps : " + steps );
637+ setMasterVolume (volume , flags );
654638 }
655639
656640 /** @see AudioManager#setStreamVolume(int, int, int) */
@@ -691,6 +675,41 @@ public void setStreamVolume(int streamType, int index, int flags) {
691675 sendVolumeUpdate (streamType , oldIndex , index , flags );
692676 }
693677
678+ private int findVolumeDelta (int direction , int volume ) {
679+ int delta = 0 ;
680+ if (direction == AudioManager .ADJUST_RAISE ) {
681+ if (volume == MAX_MASTER_VOLUME ) {
682+ return 0 ;
683+ }
684+ // This is the default value if we make it to the end
685+ delta = mMasterVolumeRamp [1 ];
686+ // If we're raising the volume move down the ramp array until we
687+ // find the volume we're above and use that groups delta.
688+ for (int i = mMasterVolumeRamp .length - 1 ; i > 1 ; i -= 2 ) {
689+ if (volume >= mMasterVolumeRamp [i - 1 ]) {
690+ delta = mMasterVolumeRamp [i ];
691+ break ;
692+ }
693+ }
694+ } else if (direction == AudioManager .ADJUST_LOWER ){
695+ if (volume == 0 ) {
696+ return 0 ;
697+ }
698+ int length = mMasterVolumeRamp .length ;
699+ // This is the default value if we make it to the end
700+ delta = -mMasterVolumeRamp [length - 1 ];
701+ // If we're lowering the volume move up the ramp array until we
702+ // find the volume we're below and use the group below it's delta
703+ for (int i = 2 ; i < length ; i += 2 ) {
704+ if (volume <= mMasterVolumeRamp [i ]) {
705+ delta = -mMasterVolumeRamp [i - 1 ];
706+ break ;
707+ }
708+ }
709+ }
710+ return delta ;
711+ }
712+
694713 // UI update and Broadcast Intent
695714 private void sendVolumeUpdate (int streamType , int oldIndex , int index , int flags ) {
696715 if (!mVoiceCapable && (streamType == AudioSystem .STREAM_RING )) {
@@ -1871,6 +1890,12 @@ private void ensureValidDirection(int direction) {
18711890 }
18721891 }
18731892
1893+ private void ensureValidSteps (int steps ) {
1894+ if (Math .abs (steps ) > MAX_BATCH_VOLUME_ADJUST_STEPS ) {
1895+ throw new IllegalArgumentException ("Bad volume adjust steps " + steps );
1896+ }
1897+ }
1898+
18741899 private void ensureValidStreamType (int streamType ) {
18751900 if (streamType < 0 || streamType >= mStreamStates .length ) {
18761901 throw new IllegalArgumentException ("Bad stream type " + streamType );
0 commit comments