commit 3fcc56ae397f10d64b94cf4efe19d6ee51a5ec4b
parent ce7f2028b9cdbc57e35142f77b70e9ff2814f6d9
Author: gstraube <gstraube@users.noreply.github.com>
Date: Sun, 11 Oct 2020 16:41:21 +0200
Merge pull request #42 from gstraube/feature/issue_40
Feature/issue 40
Diffstat:
10 files changed, 177 insertions(+), 42 deletions(-)
diff --git a/app/src/main/java/com/github/cythara/CanvasPainter.java b/app/src/main/java/com/github/cythara/CanvasPainter.java
@@ -17,6 +17,7 @@ import java.util.Objects;
import androidx.core.content.ContextCompat;
import static android.graphics.Paint.ANTI_ALIAS_FLAG;
+import static com.github.cythara.MainActivity.*;
class CanvasPainter {
@@ -42,7 +43,6 @@ class CanvasPainter {
private float x;
private float y;
private boolean useScientificNotation;
- private boolean showExactDeviation;
private int referencePitch;
private CanvasPainter(Context context) {
@@ -61,22 +61,20 @@ class CanvasPainter {
void on(Canvas canvas) {
SharedPreferences preferences = context.getSharedPreferences(
- MainActivity.PREFS_FILE, Context.MODE_PRIVATE);
+ PREFS_FILE, Context.MODE_PRIVATE);
useScientificNotation = preferences.getBoolean(
- MainActivity.USE_SCIENTIFIC_NOTATION, true);
-
- showExactDeviation = preferences.getBoolean(MainActivity.SHOW_EXACT_DEVIATION, false);
+ USE_SCIENTIFIC_NOTATION, true);
referencePitch = preferences.getInt(
- MainActivity.REFERENCE_PITCH, 440);
+ REFERENCE_PITCH, 440);
this.canvas = canvas;
redBackground = R.color.red_light;
greenBackground = R.color.green_light;
textColor = Color.BLACK;
- if (MainActivity.isDarkModeEnabled()) {
+ if (isDarkModeEnabled()) {
int color = context.getResources().getColor(R.color.colorPrimaryDark);
this.canvas.drawColor(color);
@@ -95,21 +93,34 @@ class CanvasPainter {
drawGauge();
- if (pitchDifference != null && Math.abs(getNearestDeviation()) <= MAX_DEVIATION) {
- setBackground();
+ if (!isAutoModeEnabled()) {
+ Note[] tuningNotes = getCurrentTuning().getNotes();
+ Note note = tuningNotes[getReferencePosition()];
+ drawText(x, y / 4F, note, symbolPaint);
+ }
- drawGauge();
+ if (pitchDifference != null) {
+ int abs = Math.abs(getNearestDeviation());
+ boolean shouldDraw = abs <= MAX_DEVIATION ||
+ (abs <= MAX_DEVIATION * 2 && !isAutoModeEnabled());
+ if (shouldDraw) {
+ setBackground();
- drawIndicator();
+ drawGauge();
- if (showExactDeviation) {
- drawDeviation();
- }
+ drawIndicator();
- float x = canvas.getWidth() / 2F;
- float y = canvas.getHeight() * 0.75f;
+ if (!isAutoModeEnabled()) {
+ drawDeviation();
+ }
- drawText(x, y, pitchDifference.closest, textPaint);
+ float x = canvas.getWidth() / 2F;
+ float y = canvas.getHeight() * 0.75f;
+
+ drawText(x, y, pitchDifference.closest, textPaint);
+ } else {
+ drawListeningIndicator();
+ }
} else {
drawListeningIndicator();
}
diff --git a/app/src/main/java/com/github/cythara/MainActivity.java b/app/src/main/java/com/github/cythara/MainActivity.java
@@ -38,13 +38,14 @@ public class MainActivity extends AppCompatActivity implements TaskCallbacks,
public static final String PREFS_FILE = "prefs_file";
public static final String USE_SCIENTIFIC_NOTATION = "use_scientific_notation";
public static final String CURRENT_TUNING = "current_tuning";
- public static final String SHOW_EXACT_DEVIATION = "show_exact_deviation";
protected static final String REFERENCE_PITCH = "reference_pitch";
private static final String TAG_LISTENER_FRAGMENT = "listener_fragment";
private static final String USE_DARK_MODE = "use_dark_mode";
private static int tuningPosition = 0;
private static boolean isDarkModeEnabled;
private static int referencePitch;
+ private static int referencePosition;
+ private static boolean isAutoModeEnabled = true;
public static Tuning getCurrentTuning() {
return TuningMapper.getTuningFromPosition(tuningPosition);
@@ -58,6 +59,14 @@ public class MainActivity extends AppCompatActivity implements TaskCallbacks,
return referencePitch;
}
+ public static boolean isAutoModeEnabled() {
+ return isAutoModeEnabled;
+ }
+
+ public static int getReferencePosition() {
+ return referencePosition - 1; //to account for the position of the AUTO option
+ }
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -154,21 +163,23 @@ public class MainActivity extends AppCompatActivity implements TaskCallbacks,
dialog.setArguments(bundle);
dialog.setValueChangeListener(this);
- dialog.show(getSupportFragmentManager(), "number_picker");
+ dialog.show(getSupportFragmentManager(), "reference_pitch_picker");
break;
}
- case R.id.show_exact_deviation: {
+ case R.id.choose_tuning_mode: {
final SharedPreferences preferences = getSharedPreferences(PREFS_FILE,
MODE_PRIVATE);
- boolean currentlyShowingExactDeviation = preferences.getBoolean(
- SHOW_EXACT_DEVIATION, false);
+ NotePickerDialog dialog = new NotePickerDialog();
- SharedPreferences.Editor editor = preferences.edit();
- editor.putBoolean(SHOW_EXACT_DEVIATION, !currentlyShowingExactDeviation);
- editor.apply();
+ Bundle bundle = new Bundle();
+ bundle.putBoolean("use_scientific_notation", preferences.getBoolean(
+ MainActivity.USE_SCIENTIFIC_NOTATION, true));
+ bundle.putInt("current_value", referencePosition);
+ dialog.setArguments(bundle);
- recreate();
+ dialog.setValueChangeListener(this);
+ dialog.show(getSupportFragmentManager(), "note_picker");
}
}
@@ -224,21 +235,35 @@ public class MainActivity extends AppCompatActivity implements TaskCallbacks,
editor.apply();
tuningPosition = position;
+
+ isAutoModeEnabled = true;
+ referencePosition = 0;
+
+ recreate();
}
@Override
public void onValueChange(NumberPicker picker, int oldValue, int newValue) {
- final SharedPreferences preferences = getSharedPreferences(PREFS_FILE,
- MODE_PRIVATE);
+ String tag = String.valueOf(picker.getTag());
+ if ("reference_pitch_picker".equalsIgnoreCase(tag)) {
+ final SharedPreferences preferences = getSharedPreferences(PREFS_FILE,
+ MODE_PRIVATE);
- SharedPreferences.Editor editor = preferences.edit();
- editor.putInt(REFERENCE_PITCH, newValue);
- editor.apply();
+ SharedPreferences.Editor editor = preferences.edit();
+ editor.putInt(REFERENCE_PITCH, newValue);
+ editor.apply();
- setReferencePitch();
+ setReferencePitch();
- TunerView tunerView = this.findViewById(R.id.pitch);
- tunerView.invalidate();
+ TunerView tunerView = this.findViewById(R.id.pitch);
+ tunerView.invalidate();
+ } else if ("note_picker".equalsIgnoreCase(tag)) {
+ isAutoModeEnabled = newValue == 0;
+
+ referencePosition = newValue;
+
+ recreate();
+ }
}
private void startRecording() {
diff --git a/app/src/main/java/com/github/cythara/NotePickerDialog.java b/app/src/main/java/com/github/cythara/NotePickerDialog.java
@@ -0,0 +1,93 @@
+package com.github.cythara;
+
+import android.app.Dialog;
+import android.os.Bundle;
+import android.view.ContextThemeWrapper;
+
+import androidx.appcompat.app.AlertDialog;
+import androidx.fragment.app.DialogFragment;
+
+import com.shawnlin.numberpicker.NumberPicker;
+
+public class NotePickerDialog extends DialogFragment {
+
+ private NumberPicker.OnValueChangeListener valueChangeListener;
+
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ final NumberPicker numberPicker = new NumberPicker(getActivity());
+ numberPicker.setTag("note_picker");
+
+ Bundle arguments = getArguments();
+ boolean useScientificNotation = arguments.getBoolean("use_scientific_notation", true);
+ int currentValue = arguments.getInt("current_value", 0);
+
+ Note[] notes = MainActivity.getCurrentTuning().getNotes();
+
+ numberPicker.setMinValue(0);
+ numberPicker.setMaxValue(notes.length);
+ if (currentValue < notes.length) {
+ numberPicker.setValue(currentValue);
+ } else {
+ numberPicker.setValue(0);
+ }
+
+ String[] displayedValues = new String[notes.length + 1];
+
+ displayedValues[0] = "Auto";
+ for (int i = 0; i < notes.length; i++) {
+ Note note = notes[i];
+ NoteName name = note.getName();
+ String noteName = name.getScientific();
+ int octave = note.getOctave();
+ if (!useScientificNotation) {
+ noteName = name.getSol();
+
+ //TODO Extract method
+ if (octave <= 1) {
+ octave = octave - 2;
+ }
+
+ octave = octave - 1;
+ }
+ displayedValues[i + 1] = noteName + note.getSign() + octave;
+ }
+
+ numberPicker.setDisplayedValues(displayedValues);
+
+ if (MainActivity.isDarkModeEnabled()) {
+ int color = getResources().getColor(R.color.colorTextDark);
+ numberPicker.setTextColor(color);
+ numberPicker.setDividerColor(color);
+ numberPicker.setSelectedTextColor(color);
+ }
+
+ AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(),
+ R.style.AppTheme));
+
+ builder.setPositiveButton("OK",
+ (dialog, which) -> valueChangeListener.onValueChange(numberPicker,
+ numberPicker.getValue(), numberPicker.getValue()));
+
+ builder.setNegativeButton("CANCEL", (dialog, which) -> {
+ });
+
+ builder.setNeutralButton("AUTO",
+ (dialog, which) -> valueChangeListener.onValueChange(numberPicker,
+ 0, 0));
+
+ builder.setView(numberPicker);
+ return builder.create();
+ }
+
+ @Override
+ public void onPause() {
+ super.onPause();
+
+ this.dismiss();
+ }
+
+ void setValueChangeListener(NumberPicker.OnValueChangeListener valueChangeListener) {
+ this.valueChangeListener = valueChangeListener;
+ }
+}
diff --git a/app/src/main/java/com/github/cythara/NumberPickerDialog.java b/app/src/main/java/com/github/cythara/NumberPickerDialog.java
@@ -16,6 +16,7 @@ public class NumberPickerDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final NumberPicker numberPicker = new NumberPicker(getActivity());
+ numberPicker.setTag("reference_pitch_picker");
Bundle arguments = getArguments();
int currentValue = arguments.getInt("current_value", 440);
diff --git a/app/src/main/java/com/github/cythara/PitchComparator.java b/app/src/main/java/com/github/cythara/PitchComparator.java
@@ -10,7 +10,15 @@ class PitchComparator {
Tuning tuning = MainActivity.getCurrentTuning();
int referencePitch = MainActivity.getReferencePitch();
- Note[] notes = tuning.getNotes();
+ Note[] tuningNotes = tuning.getNotes();
+ Note[] notes;
+
+ if (MainActivity.isAutoModeEnabled()) {
+ notes = tuningNotes;
+ } else {
+ notes = new Note[]{tuningNotes[MainActivity.getReferencePosition()]};
+ }
+
NoteFrequencyCalculator noteFrequencyCalculator =
new NoteFrequencyCalculator(referencePitch);
diff --git a/app/src/main/res/menu/toolbar_menu.xml b/app/src/main/res/menu/toolbar_menu.xml
@@ -23,7 +23,7 @@
app:showAsAction="never" />
<item
- android:id="@+id/show_exact_deviation"
- android:title="@string/show_exact_deviation"
+ android:id="@+id/choose_tuning_mode"
+ android:title="@string/choose_tuning_mode"
app:showAsAction="never" />
</menu>
\ No newline at end of file
diff --git a/app/src/main/res/values-de-rDE/strings.xml b/app/src/main/res/values-de-rDE/strings.xml
@@ -12,7 +12,6 @@
Eine Mikrofongenehmigung ist erforderlich. App wird geschlossen.
</string>
<string name="ok">OK</string>
- <string name="show_exact_deviation">Genaue Abweichung ein-/ausblenden</string>
<string-array name="tunings">
<item>Chromatisch</item>
diff --git a/app/src/main/res/values-eu/strings.xml b/app/src/main/res/values-eu/strings.xml
@@ -9,5 +9,4 @@
<string name="choose_a_frequency">Maiztasun bat hautatu:</string>
<string name="microphone_permission_required">Mikrofonoaren baimena beharrezkoa da</string>
<string name="ok">Ados</string>
- <string name="show_exact_deviation">Erakutsi/ezkutatu desbideratze zehatza</string>
</resources>
diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml
@@ -12,7 +12,6 @@
A permissão do microfone é necessária. App será fechado.
</string>
<string name="ok">OK</string>
- <string name="show_exact_deviation">Mostrar/esconder o desvio exato</string>
<string-array name="tunings">
<item>Cromático</item>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
@@ -1,4 +1,4 @@
-<resources>
+<resources xmlns:tools="http://schemas.android.com/tools">
<string name="app_name">Cythara</string>
<string name="privacy_policy">Show privacy policy</string>
@@ -14,7 +14,7 @@
<string name="permission_required">Permission required</string>
<string name="microphone_permission_required">Microphone permission is required. App will close.</string>
<string name="ok">OK</string>
- <string name="show_exact_deviation">Show/hide exact deviation</string>
+ <string name="choose_tuning_mode" tools:ignore="MissingTranslation">Choose tuning mode</string>
<string-array name="tunings">
<item>Chromatic</item>