@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 String format(int frequency, int level) {
   if (txtBuilder == null) return "";
   txtBuilder.delete(0, txtBuilder.length());
   if (frequency < 1000) {
     txtBuilder.append(frequency);
   } else {
     UI.formatIntAsFloat(txtBuilder, frequency / 100, false, true);
     txtBuilder.append('k');
   }
   txtBuilder.append("Hz ");
   if (level > 0) txtBuilder.append('+');
   UI.formatIntAsFloat(txtBuilder, level / 10, false, false);
   txtBuilder.append("dB");
   return txtBuilder.toString();
 }
 private String format(int strength) {
   if (txtBuilder == null) return "";
   txtBuilder.delete(0, txtBuilder.length());
   UI.formatIntAsFloat(txtBuilder, strength, false, false);
   txtBuilder.append("%");
   return txtBuilder.toString();
 }
 @Override
 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
   UI.prepare(menu);
   menu.add(0, MNU_ZEROPRESET, 0, R.string.zero_preset)
       .setOnMenuItemClickListener(this)
       .setIcon(new TextIconDrawable(UI.ICON_REMOVE));
   menu.add(0, MNU_LOADPRESET, 1, R.string.load_preset)
       .setOnMenuItemClickListener(this)
       .setIcon(new TextIconDrawable(UI.ICON_LOAD));
   menu.add(0, MNU_SAVEPRESET, 2, R.string.save_preset)
       .setOnMenuItemClickListener(this)
       .setIcon(new TextIconDrawable(UI.ICON_SAVE));
 }
 @Override
 protected void onCleanupLayout() {
   UI.animationReset();
   panelControls = null;
   panelEqualizer = null;
   panelSecondary = null;
   panelBars = null;
   chkEqualizer = null;
   chkBass = null;
   chkVirtualizer = null;
   btnGoBack = null;
   btnMenu = null;
   btnChangeEffect = null;
   if (bars != null) {
     for (int i = bars.length - 1; i >= 0; i--) bars[i] = null;
   }
 }
  private void prepareViewForMode(boolean isCreatingLayout) {
    LinearLayout.LayoutParams lp;
    final Context ctx = getApplication();
    updateEffects();
    if (Player.bassBoostMode) {
      UI.animationReset();
      UI.animationAddViewToHide(panelEqualizer);
      UI.animationAddViewToShow(panelSecondary);
      UI.animationCommit(isCreatingLayout, null);
      btnGoBack.setNextFocusDownId(R.id.chkBass);
      btnMenu.setNextFocusRightId(R.id.chkBass);
      btnMenu.setNextFocusDownId(R.id.chkBass);
      UI.setNextFocusForwardId(btnMenu, R.id.chkBass);
      btnChangeEffect.setNextFocusUpId(R.id.barVirtualizer);
      btnChangeEffect.setNextFocusLeftId(R.id.barVirtualizer);
    } else {
      if (btnChangeEffect != null) {
        UI.animationReset();
        UI.animationAddViewToHide(panelSecondary);
        UI.animationAddViewToShow(panelEqualizer);
        UI.animationCommit(isCreatingLayout, null);
      }
      chkEqualizer.setChecked(Equalizer.isEnabled(audioSink));

      final int bandCount = Equalizer.getBandCount();
      if (bars == null
          || frequencies == null
          || bars.length < bandCount
          || frequencies.length < bandCount) initBarsAndFrequencies(bandCount);
      int availableScreenW = getDecorViewWidth();
      int hMargin = getDecorViewHeight();
      // some times, when rotating the device, the decorview takes a little longer to be updated
      if (UI.isLandscape != (availableScreenW >= hMargin)) availableScreenW = hMargin;
      hMargin = ((UI.isLandscape || UI.isLargeScreen) ? UI.spToPxI(32) : UI.spToPxI(16));
      if (UI.usableScreenWidth > 0 && availableScreenW > UI.usableScreenWidth)
        availableScreenW = UI.usableScreenWidth;
      if (UI.isLargeScreen) {
        availableScreenW -=
            ((UI.getViewPaddingForLargeScreen() << 1) + (UI.controlLargeMargin << 1));
        if (UI.isLandscape) availableScreenW >>= 1;
      } else {
        availableScreenW -= (UI.controlMargin << 1);
      }
      while (hMargin > UI._1dp
          && ((bandCount * UI.defaultControlSize) + ((bandCount - 1) * hMargin)) > availableScreenW)
        hMargin--;
      int size = 0, textSize = 0, bgY = 0, y = 0;
      if (hMargin <= UI._1dp) {
        // oops... the bars didn't fit inside the screen... we must adjust everything!
        hMargin = ((bandCount >= 10) ? UI._1dp : UI.controlSmallMargin);
        size = UI.defaultControlSize - 1;
        while (size > UI._4dp
            && ((bandCount * size) + ((bandCount - 1) * hMargin)) > availableScreenW) size--;
        textSize = size - (UI.controlMargin << 1) - (UI.strokeSize << 1) - (UI._1dp << 1);
        if (textSize < UI._4dp) textSize = UI._4dp;
        UI.textPaint.setTextSize(textSize);
        final FontMetrics fm = UI.textPaint.getFontMetrics();
        final int box = (int) (fm.descent - fm.ascent + 0.5f);
        final int yInBox = box - (int) (fm.descent);
        bgY = (size - box) >> 1;
        y = bgY + yInBox;
      }
      if (panelBars == null) {
        panelBars = new LinearLayout(ctx);
        lp =
            new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        panelBars.setLayoutParams(lp);
        panelBars.setOrientation(LinearLayout.HORIZONTAL);

        for (int i = 0; i < bandCount; i++) {
          final int level = Equalizer.getBandLevel(i, audioSink);
          final BgSeekBar bar = new BgSeekBar(ctx);
          bar.setVertical(true);
          final LinearLayout.LayoutParams p =
              new LinearLayout.LayoutParams(
                  LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
          if (i > 0) p.leftMargin = hMargin;
          bar.setLayoutParams(p);
          bar.setMax((max - min) / 10);
          bar.setText(format(frequencies[i], level));
          bar.setKeyIncrement(10);
          bar.setValue(
              ((level <= LevelThreshold) && (level >= -LevelThreshold))
                  ? (-min / 10)
                  : ((level - min) / 10));
          bar.setOnBgSeekBarChangeListener(this);
          bar.setInsideList(true);
          if (size != 0) bar.setSize(size, textSize, bgY, y);
          bars[i] = bar;
          bar.setId(i + 1);
          bar.setNextFocusLeftId(i);
          bar.setNextFocusRightId(i + 2);
          UI.setNextFocusForwardId(bar, i + 2);
          bar.setNextFocusDownId(R.id.btnChangeEffect);
          bar.setNextFocusUpId(R.id.chkEqualizer);

          panelBars.addView(bar);
        }
        if (bars != null && bars.length > 0) bars[0].setNextFocusLeftId(R.id.chkEqualizer);
        panelEqualizer.addView(panelBars);
      }
      if (btnChangeEffect != null) {
        if (bars != null && bars.length > 0) {
          bars[bandCount - 1].setNextFocusRightId(R.id.btnChangeEffect);
          UI.setNextFocusForwardId(bars[bandCount - 1], R.id.btnChangeEffect);
        }
        btnGoBack.setNextFocusDownId(R.id.chkEqualizer);
        btnMenu.setNextFocusRightId(R.id.chkEqualizer);
        btnMenu.setNextFocusDownId(R.id.chkEqualizer);
        UI.setNextFocusForwardId(btnMenu, R.id.chkEqualizer);
        btnChangeEffect.setNextFocusUpId(bandCount);
        btnChangeEffect.setNextFocusLeftId(bandCount);
      } else {
        if (bars != null && bars.length > 0) {
          bars[bandCount - 1].setNextFocusRightId(R.id.chkBass);
          UI.setNextFocusForwardId(bars[bandCount - 1], R.id.chkBass);
        }
        chkBass.setNextFocusLeftId(bandCount);
      }
      chkEqualizer.setNextFocusRightId(1);
      chkEqualizer.setNextFocusDownId(1);
      UI.setNextFocusForwardId(chkEqualizer, 1);
    }
  }
 @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);
 }
 @Override
 protected void onCreate() {
   txtBuilder = new StringBuilder(32);
   final int _600dp = UI.dpToPxI(600);
   screenNotSoLarge = ((UI.screenWidth < _600dp) || (UI.screenHeight < _600dp));
 }