[Android] Equalizer: fix get and set from preferences

Thomas Guillem git at videolan.org
Wed Jul 1 15:14:32 CEST 2015


vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Wed Jul  1 09:30:42 2015 +0200| [2524eeac2cf623d42bf070c07ea8d680a4a7e5d7] | committer: Thomas Guillem

Equalizer: fix get and set from preferences

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=2524eeac2cf623d42bf070c07ea8d680a4a7e5d7
---

 .../src/org/videolan/vlc/PlaybackService.java      |    2 +-
 .../videolan/vlc/gui/audio/EqualizerFragment.java  |   35 +++++++++-----------
 .../src/org/videolan/vlc/util/VLCOptions.java      |   26 ++++++++++-----
 3 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index 41b430c..44f084b 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -1522,7 +1522,7 @@ public class PlaybackService extends Service {
         media.setEventListener(mMediaListener);
         mMediaPlayer.setMedia(media);
         media.release();
-        mMediaPlayer.setEqualizer(VLCOptions.getEqualizer());
+        mMediaPlayer.setEqualizer(VLCOptions.getEqualizer(this));
         mMediaPlayer.setVideoTitleDisplay(MediaPlayer.Position.Disable, 0);
         changeAudioFocus(true);
         mMediaPlayer.setEventListener(mMediaPlayerListener);
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.java
index c24151b..dfbda3f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.java
@@ -19,10 +19,10 @@
  *****************************************************************************/
 package org.videolan.vlc.gui.audio;
 
+import android.content.Context;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
-import android.support.v4.app.Fragment;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.SwitchCompat;
 import android.view.LayoutInflater;
@@ -39,6 +39,7 @@ import android.widget.SeekBar;
 import android.widget.SeekBar.OnSeekBarChangeListener;
 import android.widget.Spinner;
 
+import org.videolan.vlc.PlaybackService;
 import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.gui.PlaybackServiceFragment;
@@ -80,29 +81,33 @@ public class EqualizerFragment extends PlaybackServiceFragment {
         bands_layout = (LinearLayout) v.findViewById(R.id.equalizer_bands);
     }
 
+
     @Override
-    public void onResume() {
-        super.onResume();
+    public void onConnected(PlaybackService service) {
+        super.onConnected(service);
         fillViews();
     }
 
     private void fillViews() {
-        if (mService == null)
+        final Context context = getActivity();
+
+        if (mService == null || context == null)
             return;
 
-        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext());
+        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
         final float[] bands;
         final String[] presets;
 
         bands = mService.getBands();
         presets = mService.getPresets();
+        final float[] equalizerOption = VLCOptions.getEqualizer(context);
         if (equalizer == null)
-            equalizer = Preferences.getFloatArray(preferences, "equalizer_values");
+            equalizer = equalizerOption;
         if (equalizer == null)
             equalizer = new float[bands.length + 1];
 
         // on/off
-        button.setChecked(VLCOptions.getEqualizer() != null);
+        button.setChecked(equalizerOption != null);
         button.setOnCheckedChangeListener(new OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@@ -115,7 +120,7 @@ public class EqualizerFragment extends PlaybackServiceFragment {
         equalizer_presets.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, presets));
 
         // Set the default selection asynchronously to prevent a layout initialization bug.
-        final int equalizer_preset_pref = preferences.getInt("equalizer_preset", 0);
+        final int equalizer_preset_pref = VLCOptions.getEqualizerPreset(context);
         equalizer_presets.post(new Runnable() {
             @Override
             public void run() {
@@ -153,11 +158,7 @@ public class EqualizerFragment extends PlaybackServiceFragment {
         preamp.setOnSeekBarChangeListener(null);
         bands_layout.removeAllViews();
 
-        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext()).edit();
-        editor.putBoolean("equalizer_enabled", button.isChecked());
-        Preferences.putFloatArray(editor, "equalizer_values", equalizer);
-        editor.putInt("equalizer_preset", equalizer_presets.getSelectedItemPosition());
-        Util.commitPreferences(editor);
+        VLCOptions.setEqualizer(getActivity(), button.isChecked(), equalizer, equalizer_presets.getSelectedItemPosition());
     }
 
     private final OnItemSelectedListener mPresetListener = new OnItemSelectedListener() {
@@ -197,10 +198,8 @@ public class EqualizerFragment extends PlaybackServiceFragment {
                 return;
 
             equalizer[0] = progress - 20;
-            if (button.isChecked()) {
-                VLCOptions.setEqualizer(equalizer);
+            if (button.isChecked())
                 mService.setEqualizer(equalizer);
-            }
         }
     };
 
@@ -214,10 +213,8 @@ public class EqualizerFragment extends PlaybackServiceFragment {
         @Override
         public void onProgressChanged(float value) {
             equalizer[index] = value;
-            if (button.isChecked() && mService != null) {
-                VLCOptions.setEqualizer(equalizer);
+            if (button.isChecked() && mService != null)
                 mService.setEqualizer(equalizer);
-            }
         }
     }
 }
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/util/VLCOptions.java b/vlc-android/src/org/videolan/vlc/util/VLCOptions.java
index a07d91e..c95dbea 100644
--- a/vlc-android/src/org/videolan/vlc/util/VLCOptions.java
+++ b/vlc-android/src/org/videolan/vlc/util/VLCOptions.java
@@ -52,7 +52,6 @@ public class VLCOptions {
 
     private static final String DEFAULT_CODEC_LIST = "mediacodec_ndk,mediacodec_jni,iomx,all";
 
-    private static float[] sEqualizer = null;
     private static boolean sHdmiAudioEnabled = false;
 
     public static ArrayList<String> getLibOptions(SharedPreferences pref) {
@@ -66,9 +65,6 @@ public class VLCOptions {
         chroma = chroma.equals("YV12") && !AndroidUtil.isGingerbreadOrLater() ? "" : chroma;
         final boolean verboseMode = pref.getBoolean("enable_verbose_mode", true);
 
-        if (pref.getBoolean("equalizer_enabled", false))
-            setEqualizer(Preferences.getFloatArray(pref, "equalizer_values"));
-
         int deblocking = -1;
         try {
             deblocking = getDeblocking(Integer.parseInt(pref.getString("deblocking", "-1")));
@@ -195,12 +191,26 @@ public class VLCOptions {
     }
 
     // Equalizer
-    public static synchronized float[] getEqualizer() {
-        return sEqualizer;
+    public static float[] getEqualizer(Context context) {
+        final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
+        if (pref.getBoolean("equalizer_enabled", false))
+            return Preferences.getFloatArray(pref, "equalizer_values");
+        else
+            return null;
+    }
+
+    public static int getEqualizerPreset(Context context) {
+        final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
+        return pref.getInt("equalizer_preset", 0);
     }
 
-    public static synchronized void setEqualizer(float[] equalizer) {
-        sEqualizer = equalizer;
+    public static void setEqualizer(Context context, boolean enabled, float[] values, int preset) {
+        final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
+        SharedPreferences.Editor editor = pref.edit();
+        editor.putBoolean("equalizer_enabled", enabled);
+        Preferences.putFloatArray(editor, "equalizer_values", values);
+        editor.putInt("equalizer_preset", preset);
+        Util.commitPreferences(editor);
     }
 
     public static synchronized void setAudioHdmiEnabled(boolean enabled) {



More information about the Android mailing list