diff --git a/res/layout/shake_pref.xml b/res/layout/shake_pref.xml index 3e1d0cda..9d387307 100644 --- a/res/layout/shake_pref.xml +++ b/res/layout/shake_pref.xml @@ -32,6 +32,7 @@ THE SOFTWARE. android:gravity="right" /> diff --git a/res/values/translatable.xml b/res/values/translatable.xml index 94085490..0c714bc8 100644 --- a/res/values/translatable.xml +++ b/res/values/translatable.xml @@ -112,8 +112,6 @@ THE SOFTWARE. %s (Paused) - Note: volume does not increase past 100% on some devices - 1 second %d seconds diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index dbda9e36..15238ab4 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -24,8 +24,11 @@ THE SOFTWARE. xmlns:android="http://schemas.android.com/apk/res/android" android:persistent="true"> - - diff --git a/src/org/kreed/vanilla/CompatEq.java b/src/org/kreed/vanilla/CompatEq.java new file mode 100644 index 00000000..0d1fd0f6 --- /dev/null +++ b/src/org/kreed/vanilla/CompatEq.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2012 Christopher Eby + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.kreed.vanilla; + +import android.media.MediaPlayer; +import android.media.audiofx.Equalizer; + +/** + * Gingerbread equalizer compatibility. + */ +public class CompatEq { + private final Equalizer mEq; + + /** + * Create the equalizer and attach it to the given MediaPlayer's audio + * session. + */ + public CompatEq(MediaPlayer player) + { + Equalizer eq = new Equalizer(0, player.getAudioSessionId()); + eq.setEnabled(true); + mEq = eq; + } + + /** + * Call {@link Equalizer#getNumberOfBands()} + */ + public short getNumberOfBands() + { + return mEq.getNumberOfBands(); + } + + /** + * Call {@link Equalizer#setBandLevel(short, short)}. + */ + public void setBandLevel(short band, short level) + { + mEq.setBandLevel(band, level); + } +} diff --git a/src/org/kreed/vanilla/PlaybackService.java b/src/org/kreed/vanilla/PlaybackService.java index 0011eff1..94854dfe 100644 --- a/src/org/kreed/vanilla/PlaybackService.java +++ b/src/org/kreed/vanilla/PlaybackService.java @@ -278,6 +278,10 @@ public final class PlaybackService extends Service * The SensorManager service. */ private SensorManager mSensorManager; + /** + * The equalizer wrapper. + */ + private CompatEq mEqualizer; SongTimeline mTimeline; private Song mCurrentSong; @@ -299,14 +303,17 @@ public final class PlaybackService extends Service public InCallListener mCallListener; private String mErrorMessage; /** - * The volume set by the user in the preferences. + * The volume adjustment set in the volume preference. */ - private float mUserVolume = 1.0f; + private float mUserVolume; /** - * The actual volume of the media player. Will differ from the user volume - * when fading the volume. + * The linear scale volume set on the MediaPlayer. */ - private float mCurrentVolume = 1.0f; + private float mBaseVolume; + /** + * If true, the volume is being reduced for the idle fade-out. + */ + private boolean mFadeInProgress; /** * Elapsed realtime at which playback was paused by idle timeout. -1 * indicates that no timeout has occurred. @@ -358,6 +365,10 @@ public final class PlaybackService extends Service mMediaPlayer.setOnCompletionListener(this); mMediaPlayer.setOnErrorListener(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { + mEqualizer = new CompatEq(mMediaPlayer); + } + mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE); mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); @@ -370,11 +381,7 @@ public final class PlaybackService extends Service settings.registerOnSharedPreferenceChangeListener(this); mNotificationMode = Integer.parseInt(settings.getString("notification_mode", "1")); mScrobble = settings.getBoolean("scrobble", false); - float volume = settings.getFloat("volume", 1.0f); - if (volume != 1.0f) { - mCurrentVolume = mUserVolume = volume; - mMediaPlayer.setVolume(volume, volume); - } + mUserVolume = (float)Math.pow(settings.getInt("volume_int", 100) / 100.0, 3); mIdleTimeout = settings.getBoolean("use_idle_timeout", false) ? settings.getInt("idle_timeout", 3600) : 0; Song.mDisableCoverArt = settings.getBoolean("disable_cover_art", false); mHeadsetOnly = settings.getBoolean("headset_only", false); @@ -386,6 +393,8 @@ public final class PlaybackService extends Service mShakeAction = settings.getBoolean("enable_shake", false) ? Action.getAction(settings, "shake_action", Action.NextSong) : Action.Nothing; mShakeThreshold = settings.getInt("shake_threshold", 80) / 10.0f; + updateVolume(); + PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE); mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "VanillaMusicLock"); @@ -529,6 +538,29 @@ public final class PlaybackService extends Service mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI); } + /** + * Set the volume gain on the MediaPlayer/Equalizer + */ + private void updateVolume() + { + float base = mUserVolume; + + if (base > 1.0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { + // In Gingerbread and above, MediaPlayer no longer accepts volumes + // > 1.0. So we use an equalizer instead. + CompatEq eq = mEqualizer; + short gain = (short)(2000 * Math.log10(base)); + for (short i = eq.getNumberOfBands(); --i != -1; ) { + eq.setBandLevel(i, gain); + } + base = 1.0f; + } + + mBaseVolume = base; + if (mMediaPlayer != null) + mMediaPlayer.setVolume(base, base); + } + private void loadPreference(String key) { SharedPreferences settings = getSettings(this); @@ -549,11 +581,9 @@ public final class PlaybackService extends Service updateNotification(); } else if ("scrobble".equals(key)) { mScrobble = settings.getBoolean("scrobble", false); - } else if ("volume".equals(key)) { - float volume = settings.getFloat("volume", 1.0f); - mCurrentVolume = mUserVolume = volume; - if (mMediaPlayer != null) - mMediaPlayer.setVolume(volume, volume); + } else if ("volume_int".equals(key)) { + mUserVolume = (float)Math.pow(settings.getInt(key, 100) / 100.0, 3); + updateVolume(); } else if ("media_button".equals(key)) { MediaButtonReceiver.reloadPreference(this); } else if ("use_idle_timeout".equals(key) || "idle_timeout".equals(key)) { @@ -1117,24 +1147,29 @@ public final class PlaybackService extends Service runQuery(message.arg1, (QueryTask)message.obj, message.arg2); break; case IDLE_TIMEOUT: - if ((mState & FLAG_PLAYING) != 0) + if ((mState & FLAG_PLAYING) != 0) { mHandler.sendMessage(mHandler.obtainMessage(FADE_OUT, 100, 0)); + mFadeInProgress = true; + } break; - case FADE_OUT: + case FADE_OUT: { int progress = message.arg1 - 1; + float volume; if (progress == 0) { mIdleStart = SystemClock.elapsedRealtime(); unsetFlag(FLAG_PLAYING); - mCurrentVolume = mUserVolume; + volume = mBaseVolume; + mFadeInProgress = false; } else { // Approximate an exponential curve with x^4 // http://www.dr-lex.be/info-stuff/volumecontrols.html - mCurrentVolume = Math.max((float)(Math.pow(progress / 100f, 4) * mUserVolume), .01f); + volume = Math.max((float)(Math.pow(progress / 100f, 4) * mBaseVolume), .01f); mHandler.sendMessageDelayed(mHandler.obtainMessage(FADE_OUT, progress, 0), 50); } if (mMediaPlayer != null) - mMediaPlayer.setVolume(mCurrentVolume, mCurrentVolume); + mMediaPlayer.setVolume(volume, volume); break; + } case PROCESS_STATE: processNewState(message.arg1, message.arg2); break; @@ -1259,16 +1294,16 @@ public final class PlaybackService extends Service if (mIdleTimeout != 0) mHandler.sendEmptyMessageDelayed(IDLE_TIMEOUT, mIdleTimeout * 1000); + if (mFadeInProgress) { + mMediaPlayer.setVolume(mBaseVolume, mBaseVolume); + mFadeInProgress = false; + } + long idleStart = mIdleStart; if (idleStart != -1 && SystemClock.elapsedRealtime() - idleStart < IDLE_GRACE_PERIOD) { mIdleStart = -1; setFlag(FLAG_PLAYING); } - - if (mCurrentVolume != mUserVolume) { - mCurrentVolume = mUserVolume; - mMediaPlayer.setVolume(mCurrentVolume, mCurrentVolume); - } } /** diff --git a/src/org/kreed/vanilla/ShakeThresholdPreference.java b/src/org/kreed/vanilla/SeekBarPreference.java similarity index 73% rename from src/org/kreed/vanilla/ShakeThresholdPreference.java rename to src/org/kreed/vanilla/SeekBarPreference.java index 4773c1e0..e34a98f3 100644 --- a/src/org/kreed/vanilla/ShakeThresholdPreference.java +++ b/src/org/kreed/vanilla/SeekBarPreference.java @@ -23,6 +23,7 @@ package org.kreed.vanilla; import android.content.Context; +import android.content.res.TypedArray; import android.preference.DialogPreference; import android.util.AttributeSet; import android.view.View; @@ -32,13 +33,17 @@ import android.widget.TextView; /** * SeekBar preference to set the shake force threshold. */ -public class ShakeThresholdPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener { +public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener { + /** + * The current value. + */ + private int mValue; /** * TextView to display current threshold. */ private TextView mValueText; - public ShakeThresholdPreference(Context context, AttributeSet attrs) + public SeekBarPreference(Context context, AttributeSet attrs) { super(context, attrs); } @@ -46,33 +51,49 @@ public class ShakeThresholdPreference extends DialogPreference implements SeekBa @Override public CharSequence getSummary() { - return getSummary(getPersistedInt(80)); + return getSummary(mValue); } + @Override + protected Object onGetDefaultValue(TypedArray a, int index) + { + return a.getInt(index, 100); + } + + @Override + protected void onSetInitialValue(boolean restoreValue, Object defaultValue) + { + mValue = restoreValue ? getPersistedInt(mValue) : (Integer)defaultValue; + } + /** * Create the summary for the given value. * * @param value The force threshold. * @return A string representation of the threshold. */ - private static String getSummary(int value) + private String getSummary(int value) { - return String.valueOf(value / 10.0f); + if ("shake_threshold".equals(getKey())) { + return String.valueOf(value / 10.0f); + } else { + return String.format("%d%% (%+.1fdB)", value, 20 * Math.log10(Math.pow(value / 100.0, 3))); + } } @Override protected View onCreateDialogView() { View view = super.onCreateDialogView(); - int value = getPersistedInt(80); mValueText = (TextView)view.findViewById(R.id.value); - mValueText.setText(getSummary(value)); + mValueText.setText(getSummary(mValue)); SeekBar seekBar = (SeekBar)view.findViewById(R.id.seek_bar); seekBar.setMax(150); - seekBar.setProgress(value); + seekBar.setProgress(mValue); seekBar.setOnSeekBarChangeListener(this); + return view; } @@ -86,6 +107,7 @@ public class ShakeThresholdPreference extends DialogPreference implements SeekBa public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { + mValue = progress; mValueText.setText(getSummary(progress)); persistInt(progress); } diff --git a/src/org/kreed/vanilla/VolumePreference.java b/src/org/kreed/vanilla/VolumePreference.java deleted file mode 100644 index 95a6650c..00000000 --- a/src/org/kreed/vanilla/VolumePreference.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (C) 2010 Christopher Eby - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package org.kreed.vanilla; - -import android.app.AlertDialog.Builder; -import android.content.Context; -import android.preference.DialogPreference; -import android.util.AttributeSet; -import android.view.Gravity; -import android.widget.LinearLayout; -import android.widget.SeekBar; -import android.widget.TextView; - -/** - * A preference that provides a volume slider dialog. - * - * The position in the slider is saved as float ranging from 0 to 1 on a - * roughly exponential scale. - */ -public class VolumePreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener { - private static final float DEFAULT_VALUE = 1.0f; - /** - * The view to display the current volume percentage. - */ - private TextView mPercent; - - public VolumePreference(Context context, AttributeSet attrs) - { - super(context, attrs); - } - - @Override - public CharSequence getSummary() - { - return String.format("%.0f%%", Math.pow(getPersistedFloat(DEFAULT_VALUE), 0.33f) * 100); - } - - @Override - protected void onPrepareDialogBuilder(Builder builder) - { - // setting is applied instantly; no way to cancel - builder.setNegativeButton(null, null); - - Context context = getContext(); - - LinearLayout layout = new LinearLayout(context); - layout.setOrientation(LinearLayout.VERTICAL); - - TextView percent = new TextView(context); - percent.setText(getSummary()); - percent.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT); - percent.setPadding(10, 0, 10, 0); - layout.addView(percent); - mPercent = percent; - - SeekBar seekBar = new SeekBar(context); - seekBar.setPadding(20, 10, 20, 10); - seekBar.setMax(150); - seekBar.setProgress((int)(Math.pow(getPersistedFloat(DEFAULT_VALUE), 0.33f) * 100)); - seekBar.setOnSeekBarChangeListener(this); - layout.addView(seekBar); - - TextView message = new TextView(context); - message.setText(R.string.volume_warning); - message.setGravity(Gravity.CENTER_VERTICAL); - message.setPadding(10, 0, 10, 0); - layout.addView(message); - - builder.setView(layout); - } - - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) - { - // Approximate an exponential curve with x^3. Produces a value from 0.0 - 1.0. - if (fromUser && shouldPersist()) { - persistFloat((float)Math.pow(seekBar.getProgress() / 100.0f, 3)); - mPercent.setText(progress + "%"); - } - } - - @Override - protected void onDialogClosed(boolean positiveResult) - { - notifyChanged(); - } - - @Override - public void onStartTrackingTouch(SeekBar seekBar) - { - } - - @Override - public void onStopTrackingTouch(SeekBar seekBar) - { - } -}