Skip to content

Commit

Permalink
added custom preset option, bug fix, layout fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearfog committed May 20, 2024
1 parent b3fced2 commit 61101fe
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 23 deletions.
16 changes: 10 additions & 6 deletions app/src/main/java/org/nuclearfog/apollo/player/AudioEffects.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,17 @@ public AudioPreset getPreset() {
/**
* set new preset
*
* @param preset preset to set
* @param preset preset to set or null to set custom preset
*/
public void setPreset(AudioPreset preset) {
setBassLevel(preset.getBassLevel());
setReverbLevel(preset.getReverbLevel());
setBandLevel(preset.getBands());
prefs.setPresetName(preset.getName());
public void setPreset(@Nullable AudioPreset preset) {
if (preset != null) {
setBassLevel(preset.getBassLevel());
setReverbLevel(preset.getReverbLevel());
setBandLevel(preset.getBands());
prefs.setPresetName(preset.getName());
} else {
prefs.setPresetName("");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class PresetStore extends AppStore {
PresetColumns.EQUALIZER
};

/**
* default sort order
*/
private static final String SORT = PresetColumns.TIME + " DESC";

/**
* database name
*/
Expand Down Expand Up @@ -74,6 +79,7 @@ public synchronized void savePreset(AudioPreset preset) {
column.put(PresetColumns.NAME, preset.getName());
column.put(PresetColumns.BASS, preset.getBassLevel());
column.put(PresetColumns.REVERB, preset.getReverbLevel());
column.put(PresetColumns.TIME, System.currentTimeMillis());
StringBuilder buf = new StringBuilder();
for (int band : preset.getBands()) {
buf.append(band).append(";");
Expand All @@ -91,7 +97,7 @@ public synchronized void savePreset(AudioPreset preset) {
*/
public synchronized List<AudioPreset> loadPresets() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(PresetColumns.TABLE, COLUMNS, null, null, null, null, PresetColumns.TIME + " DESC");
Cursor cursor = db.query(PresetColumns.TABLE, COLUMNS, null, null, null, null, SORT);
List<AudioPreset> result = new LinkedList<>();
if (cursor.moveToFirst()) {
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ protected void onCreate(Bundle savedInstanceState) {
enableFx.setOnCheckedChangeListener(this);
bassBoost.setOnSeekBarChangeListener(this);
reverb.setOnSeekBarChangeListener(this);
presetSelector.setOnItemSelectedListener(this);
}


Expand Down Expand Up @@ -150,6 +149,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
@Override
public void onBandLevelChange(int pos, int level) {
audioEffects.setBandLevel(pos, level);
setCustomPreset();
}


Expand Down Expand Up @@ -184,6 +184,9 @@ public void onStartTrackingTouch(SeekBar seekBar) {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (seekBar.getId() == R.id.audiofx_bass_boost || seekBar.getId() == R.id.audiofx_reverb) {
setCustomPreset();
}
}


Expand All @@ -198,8 +201,8 @@ public void onResult(@NonNull List<AudioPreset> audioPresets) {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.audiofx_preset) {
AudioPreset preset = presetAdapter.getItem(position);
audioEffects.setPreset(preset);
if (preset != null) {
audioEffects.setPreset(preset);
setViews();
}
}
Expand Down Expand Up @@ -227,7 +230,11 @@ private void setViews() {
eqAdapter.setBands(audioEffects.getBandLevel());
}


/**
* set visibity of the views
*
* @param enable true to make views visible
*/
private void setVisibility(boolean enable) {
// enable views only if effect is enabled
reverb.setEnabled(enable);
Expand All @@ -237,17 +244,29 @@ private void setVisibility(boolean enable) {
presetSelector.setEnabled(enable);
}

/**
* set selector to custom after any change
*/
private void setCustomPreset() {
if (presetSelector.getCount() > 0 && presetSelector.getSelectedItemPosition() > 0) {
presetSelector.setSelection(0, false);
}
}

/**
* select current selected preset
*/
private void setPreset() {
String presetName = audioEffects.getPreset().getName();
int index = 0;
for (int i = 0; i < presetAdapter.getCount(); i++) {
AudioPreset preset = presetAdapter.getItem(i);
if (preset != null && preset.getName().equals(presetName)) {
presetSelector.setSelection(i);
index = i;
break;
}
}
presetSelector.setSelection(index, false);
presetSelector.setOnItemSelectedListener(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.nuclearfog.apollo.ui.adapters.listview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
Expand All @@ -8,6 +9,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.nuclearfog.apollo.R;
import org.nuclearfog.apollo.model.AudioPreset;

import java.util.ArrayList;
Expand All @@ -26,21 +28,25 @@ public class PresetAdapter extends BaseAdapter {
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
TextView tv;
if (convertView instanceof TextView) {
tv = (TextView) convertView;
} else {
tv = new TextView(parent.getContext());
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_dropdown, parent, false);
}
tv.setText(items.get(position).getName());
return tv;
TextView tv = convertView.findViewById(R.id.list_item_dropdown_text);
AudioPreset preset = getItem(position);
if (preset != null)
tv.setText(preset.getName());
else
tv.setText(R.string.preset_custom);
return convertView;
}


@Nullable
@Override
public AudioPreset getItem(int position) {
return items.get(position);
if (position == 0)
return null;
return items.get(position - 1);
}


Expand All @@ -52,7 +58,7 @@ public long getItemId(int position) {

@Override
public int getCount() {
return items.size();
return items.size() + 1;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ public void afterTextChanged(Editable s) {
preset.setName(s.toString());
}


/**
* create an instance of this dialog
*
* @param preset preset configuration to show
* @return instance of this dialog
*/
public static PresetDialog newInstance(AudioPreset preset) {
PresetDialog dialog = new PresetDialog();
Bundle args = new Bundle();
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_audiofx.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
android:layout_height="wrap_content"
android:lines="1"
android:text="@string/label_preset"
android:textSize="@dimen/text_size_small"
android:textSize="@dimen/text_size_medium"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
app:layout_constraintHorizontal_chainStyle="packed"
Expand All @@ -75,7 +75,8 @@
app:layout_constraintStart_toEndOf="@id/audiofx_preset_label"
app:layout_constraintTop_toBottomOf="@id/audiofx_enable"
app:layout_constraintBottom_toTopOf="@id/audiofx_eq_scroll"
app:layout_constraintEnd_toEndOf="parent" />
app:layout_constraintEnd_toEndOf="parent"
tools:listitem="@layout/list_item_dropdown" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/audiofx_eq_scroll"
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/list_item_dropdown.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/list_item_dropdown_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/text_size_medium"
android:lines="1"
android:layout_margin="5dp" />

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,5 @@
<string name="preset_name_hint">Name eingeben</string>
<string name="empty_preset_name">Name darf nicht leer sein!</string>
<string name="label_preset">Voreinstellung:</string>
<string name="preset_custom">Benutzerdefiniert</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,6 @@
<!--other-->
<string name="lastfm_api_key">LastFM API key</string>
<string name="preset_name_hint">enter preset name</string>
<string name="preset_custom">custom</string>

</resources>

0 comments on commit 61101fe

Please sign in to comment.