private void initBarsAndFrequencies(int bandCount) { clearBarsAndFrequencies(); min = Equalizer.getMinBandLevel(); max = Equalizer.getMaxBandLevel(); frequencies = new int[bandCount]; bars = new BgSeekBar[bandCount]; for (int i = bandCount - 1; i >= 0; i--) frequencies[i] = Equalizer.getBandFrequency(i); }
@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 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; }
@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; } } } }
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); } }