Skip to content

Commit f96901f

Browse files
Mike LockwoodAndroid (Google) Code Review
authored andcommitted
Merge "Add support for non-linear ramping of master volume adjustment"
2 parents 8fdaf78 + 9760647 commit f96901f

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

core/res/res/values/config.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@
6565
master volume stream and nothing else . -->
6666
<bool name="config_useMasterVolume">false</bool>
6767

68+
<!-- Array of integer pairs controlling the rate at which the master volume changes
69+
in response to volume up and down key events.
70+
The first integer of each pair is compared against the current master volume
71+
(in range 0 to 100).
72+
The last pair with first integer <= the current volume is chosen,
73+
and the second integer of the pair indicates the amount to increase the master volume
74+
when volume up is pressed. -->
75+
<integer-array name="config_masterVolumeRamp">
76+
<item>0</item> <item>5</item> <!-- default: always increase volume by 5% -->
77+
</integer-array>
78+
6879
<!-- Flag indicating whether the AUDIO_BECOMING_NOISY notification should
6980
be sent during an change to the audio output device. -->
7081
<bool name="config_sendAudioBecomingNoisy">true</bool>

core/res/res/values/public.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@
862862
<java-symbol type="array" name="preloaded_drawables" />
863863
<java-symbol type="array" name="special_locale_codes" />
864864
<java-symbol type="array" name="special_locale_names" />
865+
<java-symbol type="array" name="config_masterVolumeRamp" />
865866

866867
<java-symbol type="drawable" name="default_wallpaper" />
867868
<java-symbol type="drawable" name="ic_suggestions_add" />

media/java/android/media/AudioService.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,6 @@ public class AudioService extends IAudioService.Stub {
137137
// Timeout for connection to bluetooth headset service
138138
private static final int BT_HEADSET_CNCT_TIMEOUT_MS = 3000;
139139

140-
// Amount to raise/lower master volume
141-
// FIXME - this should probably be in a resource
142-
private static final float MASTER_VOLUME_INCREMENT = 0.05f;
143-
144140
/** @see AudioSystemThread */
145141
private AudioSystemThread mAudioSystemThread;
146142
/** @see AudioHandler */
@@ -286,6 +282,8 @@ public void onError(int error) {
286282
// True if we have master volume support
287283
private final boolean mUseMasterVolume;
288284

285+
private final int[] mMasterVolumeRamp;
286+
289287
// List of binder death handlers for setMode() client processes.
290288
// The last process to have called setMode() is at the top of the list.
291289
private final ArrayList <SetModeDeathHandler> mSetModeDeathHandlers = new ArrayList <SetModeDeathHandler>();
@@ -416,6 +414,9 @@ public AudioService(Context context) {
416414
mUseMasterVolume = context.getResources().getBoolean(
417415
com.android.internal.R.bool.config_useMasterVolume);
418416
restoreMasterVolume();
417+
418+
mMasterVolumeRamp = context.getResources().getIntArray(
419+
com.android.internal.R.array.config_masterVolumeRamp);
419420
}
420421

421422
private void createAudioSystemThread() {
@@ -620,19 +621,27 @@ public void adjustStreamVolume(int streamType, int direction, int flags) {
620621
/** @see AudioManager#adjustMasterVolume(int) */
621622
public void adjustMasterVolume(int direction, int flags) {
622623
ensureValidDirection(direction);
623-
624-
float volume = AudioSystem.getMasterVolume();
625-
if (volume >= 0.0) {
626-
// get current master volume adjusted to 0 to 100
624+
int volume = Math.round(AudioSystem.getMasterVolume() * MAX_MASTER_VOLUME);
625+
int delta = 0;
626+
for (int i = 0; i < mMasterVolumeRamp.length; i += 2) {
627+
int testVolume = mMasterVolumeRamp[i];
628+
int testDelta = mMasterVolumeRamp[i + 1];
627629
if (direction == AudioManager.ADJUST_RAISE) {
628-
volume += MASTER_VOLUME_INCREMENT;
629-
if (volume > 1.0f) volume = 1.0f;
630+
if (volume >= testVolume) {
631+
delta = testDelta;
632+
} else {
633+
break;
634+
}
630635
} else if (direction == AudioManager.ADJUST_LOWER) {
631-
volume -= MASTER_VOLUME_INCREMENT;
632-
if (volume < 0.0f) volume = 0.0f;
636+
if (volume - testDelta >= testVolume) {
637+
delta = -testDelta;
638+
} else {
639+
break;
640+
}
633641
}
634-
doSetMasterVolume(volume, flags);
635642
}
643+
// Log.d(TAG, "adjustMasterVolume volume: " + volume + " delta: " + delta + " direction: " + direction);
644+
setMasterVolume(volume + delta, flags);
636645
}
637646

638647
/** @see AudioManager#setStreamVolume(int, int, int) */
@@ -805,6 +814,11 @@ public int getMasterVolume() {
805814
}
806815

807816
public void setMasterVolume(int volume, int flags) {
817+
if (volume < 0) {
818+
volume = 0;
819+
} else if (volume > MAX_MASTER_VOLUME) {
820+
volume = MAX_MASTER_VOLUME;
821+
}
808822
doSetMasterVolume((float)volume / MAX_MASTER_VOLUME, flags);
809823
}
810824

0 commit comments

Comments
 (0)