@Override
 public void run() {
   // the effects have just been reset!
   enablingEffect = false;
   chkEqualizer.setChecked(Equalizer.isEnabled(audioSink));
   chkBass.setChecked(BassBoost.isEnabled(audioSink));
   chkVirtualizer.setChecked(Virtualizer.isEnabled(audioSink));
 }
 @Override
 public void onFileSelected(int id, String path, String name) {
   if (id == MNU_LOADPRESET) {
     final SerializableMap opts = SerializableMap.deserialize(getApplication(), path);
     if (opts != null) {
       Equalizer.deserialize(opts, audioSink);
       BassBoost.deserialize(opts, audioSink);
       Virtualizer.deserialize(opts, audioSink);
       Player.commitAllEffects(audioSink);
       updateEffects();
     }
   } else {
     final SerializableMap opts = new SerializableMap(Equalizer.getBandCount() << 1);
     Equalizer.serialize(opts, audioSink);
     BassBoost.serialize(opts, audioSink);
     Virtualizer.serialize(opts, audioSink);
     opts.serialize(getApplication(), path);
   }
 }
 @Override
 public void onClick(View view) {
   if (view == btnGoBack) {
     finish(0, view, true);
   } else if (view == btnMenu) {
     CustomContextMenu.openContextMenu(btnMenu, this);
   } else if (view == btnChangeEffect) {
     Player.bassBoostMode = !Player.bassBoostMode;
     prepareViewForMode(false);
   } else if (view == chkEqualizer) {
     if (enablingEffect) return;
     if (!Equalizer.isSupported()) {
       chkEqualizer.setChecked(false);
       UI.toast(getApplication(), R.string.effect_not_supported);
       return;
     }
     enablingEffect = true;
     Player.enableEffects(
         chkEqualizer.isChecked(),
         chkBass.isChecked(),
         chkVirtualizer.isChecked(),
         audioSink,
         this);
   } else if (view == chkBass) {
     if (enablingEffect) return;
     if (!BassBoost.isSupported()) {
       chkBass.setChecked(false);
       UI.toast(getApplication(), R.string.effect_not_supported);
       return;
     }
     enablingEffect = true;
     Player.enableEffects(
         chkEqualizer.isChecked(),
         chkBass.isChecked(),
         chkVirtualizer.isChecked(),
         audioSink,
         this);
   } else if (view == chkVirtualizer) {
     if (enablingEffect) return;
     if (!Virtualizer.isSupported()) {
       chkVirtualizer.setChecked(false);
       UI.toast(getApplication(), R.string.effect_not_supported);
       return;
     }
     enablingEffect = true;
     Player.enableEffects(
         chkEqualizer.isChecked(),
         chkBass.isChecked(),
         chkVirtualizer.isChecked(),
         audioSink,
         this);
   }
 }
 private void updateEffects() {
   chkEqualizer.setChecked(Equalizer.isEnabled(audioSink));
   if (bars != null && frequencies != null) {
     for (int i = bars.length - 1; i >= 0; i--) {
       final BgSeekBar bar = bars[i];
       if (bar != null) {
         final int level = Equalizer.getBandLevel(i, audioSink);
         bars[i].setText(format(frequencies[i], level));
         bars[i].setValue(
             ((level <= LevelThreshold) && (level >= -LevelThreshold))
                 ? (-min / 10)
                 : ((level - min) / 10));
       }
     }
   }
   chkBass.setChecked(BassBoost.isEnabled(audioSink));
   barBass.setValue(BassBoost.getStrength(audioSink));
   barBass.setText(format(BassBoost.getStrength(audioSink)));
   chkVirtualizer.setChecked(Virtualizer.isEnabled(audioSink));
   barVirtualizer.setValue(Virtualizer.getStrength(audioSink));
   barVirtualizer.setText(format(Virtualizer.getStrength(audioSink)));
 }
 @Override
 public void onValueChanged(BgSeekBar seekBar, int value, boolean fromUser, boolean usingKeys) {
   if (!fromUser) return;
   if (seekBar == barBass) {
     BassBoost.setStrength(value, audioSink);
     seekBar.setText(format(BassBoost.getStrength(audioSink)));
   } else if (seekBar == barVirtualizer) {
     Virtualizer.setStrength(value, audioSink);
     seekBar.setText(format(Virtualizer.getStrength(audioSink)));
   } else if (bars != null && frequencies != null) {
     for (int i = bars.length - 1; i >= 0; i--) {
       if (seekBar == bars[i]) {
         int level = (10 * value) + min;
         if (!usingKeys && (level <= LevelThreshold) && (level >= -LevelThreshold)) {
           level = 0;
           seekBar.setValue(-min / 10);
         }
         Equalizer.setBandLevel(i, level, audioSink);
         seekBar.setText(format(frequencies[i], Equalizer.getBandLevel(i, audioSink)));
         return;
       }
     }
   }
 }
 @Override
 public boolean onMenuItemClick(MenuItem item) {
   switch (item.getItemId()) {
     case MNU_ZEROPRESET:
       for (int i = Equalizer.getBandCount() - 1; i >= 0; i--)
         Equalizer.setBandLevel(i, 0, audioSink);
       BassBoost.setStrength(0, audioSink);
       Virtualizer.setStrength(0, audioSink);
       Player.commitAllEffects(audioSink);
       updateEffects();
       break;
     case MNU_LOADPRESET:
       startActivity(
           new ActivityFileSelection(
               getText(R.string.load_preset),
               MNU_LOADPRESET,
               false,
               false,
               getText(R.string.item_preset).toString(),
               "#pset",
               this),
           0,
           null,
           false);
       break;
     case MNU_SAVEPRESET:
       startActivity(
           new ActivityFileSelection(
               getText(R.string.save_preset),
               MNU_SAVEPRESET,
               true,
               false,
               getText(R.string.item_preset).toString(),
               "#pset",
               this),
           0,
           null,
           false);
       break;
   }
   return true;
 }
 @SuppressWarnings("deprecation")
 @Override
 protected void onCreateLayout(boolean firstCreation) {
   setContentView(R.layout.activity_effects);
   audioSink = Player.localAudioSinkUsedInEffects;
   panelControls = (LinearLayout) findViewById(R.id.panelControls);
   panelControls.setBackgroundDrawable(new ColorDrawable(UI.color_list));
   panelEqualizer = (LinearLayout) findViewById(R.id.panelEqualizer);
   panelSecondary = (ViewGroup) findViewById(R.id.panelSecondary);
   if (panelSecondary instanceof ScrollView) {
     panelSecondary.setHorizontalFadingEdgeEnabled(false);
     panelSecondary.setVerticalFadingEdgeEnabled(false);
     panelSecondary.setFadingEdgeLength(0);
   }
   btnGoBack = (BgButton) findViewById(R.id.btnGoBack);
   btnGoBack.setOnClickListener(this);
   btnGoBack.setIcon(UI.ICON_GOBACK);
   chkEqualizer = (BgButton) findViewById(R.id.chkEqualizer);
   chkEqualizer.setOnClickListener(this);
   chkEqualizer.setTextColor(UI.colorState_text_listitem_reactive);
   chkEqualizer.formatAsLabelledCheckBox();
   chkBass = (BgButton) findViewById(R.id.chkBass);
   chkBass.setOnClickListener(this);
   chkBass.setTextColor(UI.colorState_text_listitem_reactive);
   chkBass.formatAsLabelledCheckBox();
   chkVirtualizer = (BgButton) findViewById(R.id.chkVirtualizer);
   chkVirtualizer.setOnClickListener(this);
   chkVirtualizer.setTextColor(UI.colorState_text_listitem_reactive);
   chkVirtualizer.formatAsLabelledCheckBox();
   btnMenu = (BgButton) findViewById(R.id.btnMenu);
   btnMenu.setOnClickListener(this);
   btnMenu.setIcon(UI.ICON_MENU);
   btnChangeEffect = (BgButton) findViewById(R.id.btnChangeEffect);
   if (btnChangeEffect != null) {
     btnChangeEffect.setOnClickListener(this);
     btnChangeEffect.setCompoundDrawables(
         new TextIconDrawable(
             UI.ICON_EQUALIZER, UI.color_text_listitem, UI.defaultControlContentsSize),
         null,
         null,
         null);
     btnChangeEffect.setMinimumHeight(UI.defaultControlSize);
     btnChangeEffect.setTextColor(UI.colorState_text_listitem_reactive);
   } else {
     UI.isLargeScreen = true;
     Player.bassBoostMode = false;
   }
   barBass = (BgSeekBar) findViewById(R.id.barBass);
   barBass.setMax(BassBoost.getMaxStrength());
   barBass.setValue(BassBoost.getStrength(audioSink));
   barBass.setKeyIncrement(BassBoost.getMaxStrength() / 50);
   barBass.setOnBgSeekBarChangeListener(this);
   barBass.setInsideList(true);
   barBass.setAdditionalContentDescription(getText(R.string.bass_boost).toString());
   barVirtualizer = (BgSeekBar) findViewById(R.id.barVirtualizer);
   barVirtualizer.setMax(Virtualizer.getMaxStrength());
   barVirtualizer.setValue(Virtualizer.getStrength(audioSink));
   barVirtualizer.setKeyIncrement(Virtualizer.getMaxStrength() / 50);
   barVirtualizer.setOnBgSeekBarChangeListener(this);
   barVirtualizer.setInsideList(true);
   barVirtualizer.setAdditionalContentDescription(getText(R.string.virtualization).toString());
   LinearLayout.LayoutParams lp;
   if (!UI.isLargeScreen && UI.isLandscape) {
     if (btnChangeEffect != null) {
       lp = (LinearLayout.LayoutParams) btnChangeEffect.getLayoutParams();
       lp.topMargin = 0;
       btnChangeEffect.setLayoutParams(lp);
     }
     lp = (LinearLayout.LayoutParams) chkEqualizer.getLayoutParams();
     lp.bottomMargin = 0;
     chkEqualizer.setLayoutParams(lp);
     lp = (LinearLayout.LayoutParams) chkBass.getLayoutParams();
     lp.bottomMargin = 0;
     chkBass.setLayoutParams(lp);
     lp = (LinearLayout.LayoutParams) chkVirtualizer.getLayoutParams();
     lp.bottomMargin = 0;
     chkVirtualizer.setLayoutParams(lp);
     lp = (LinearLayout.LayoutParams) barBass.getLayoutParams();
     lp.bottomMargin = UI.controlMargin;
     barBass.setLayoutParams(lp);
     lp = (LinearLayout.LayoutParams) barVirtualizer.getLayoutParams();
     lp.bottomMargin = UI.controlMargin;
     barVirtualizer.setLayoutParams(lp);
     panelControls.setPadding(
         UI.controlLargeMargin, UI.thickDividerSize, UI.controlLargeMargin, 0);
   } else if (UI.isLargeScreen) {
     UI.prepareViewPaddingForLargeScreen(
         panelControls,
         0,
         (screenNotSoLarge && !UI.isLandscape) ? UI.controlMargin : UI.controlLargeMargin);
     if (!UI.isLandscape && (panelControls instanceof LinearLayout)) {
       panelControls.setOrientation(LinearLayout.VERTICAL);
       panelControls.setWeightSum(0);
       final int margin = (screenNotSoLarge ? UI.controlMargin : (UI.controlLargeMargin << 1));
       lp =
           new LinearLayout.LayoutParams(
               LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
       lp.leftMargin = margin;
       lp.topMargin = margin;
       lp.rightMargin = margin;
       lp.bottomMargin = margin;
       panelSecondary.setLayoutParams(lp);
       lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
       lp.weight = 1;
       lp.leftMargin = margin;
       lp.topMargin = (screenNotSoLarge ? UI.controlLargeMargin : margin);
       lp.rightMargin = margin;
       lp.bottomMargin = margin;
       panelEqualizer.setLayoutParams(lp);
       if (screenNotSoLarge) {
         lp = (LinearLayout.LayoutParams) barBass.getLayoutParams();
         lp.bottomMargin = UI.controlLargeMargin;
         barBass.setLayoutParams(lp);
         lp = (LinearLayout.LayoutParams) barVirtualizer.getLayoutParams();
         lp.bottomMargin = UI.controlLargeMargin;
         barVirtualizer.setLayoutParams(lp);
       }
     }
   } else {
     panelControls.setPadding(UI.controlMargin, UI.thickDividerSize, UI.controlMargin, 0);
   }
   UI.prepareControlContainer(findViewById(R.id.panelTop), false, true);
   prepareViewForMode(true);
 }