From 8e92a8559be89786556b548f26f6a5826fc7f89b Mon Sep 17 00:00:00 2001 From: woheller69 Date: Tue, 24 May 2022 15:53:18 +0200 Subject: [PATCH] Add option to switch gain between normal and low --- .../woheller69/audiometry/FileOperations.java | 28 +++++++++++++++++++ .../woheller69/audiometry/MainActivity.java | 28 ++++++++++++++++++- .../audiometry/PerformSingleTest.java | 1 + .../woheller69/audiometry/PerformTest.java | 6 +++- .../org/woheller69/audiometry/TestLookup.java | 1 + app/src/main/res/menu/main.xml | 4 +++ app/src/main/res/values-de/strings.xml | 5 +++- app/src/main/res/values/strings.xml | 5 +++- 8 files changed, 74 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/woheller69/audiometry/FileOperations.java b/app/src/main/java/org/woheller69/audiometry/FileOperations.java index 58566d3..aebadc4 100644 --- a/app/src/main/java/org/woheller69/audiometry/FileOperations.java +++ b/app/src/main/java/org/woheller69/audiometry/FileOperations.java @@ -31,6 +31,34 @@ public double read0dBSPL(Context context){ //0dB SPL equals hearing threshold a return 0; } + public static void writeGain(Context context){ + try{ + FileOutputStream fos = context.openFileOutput("Gain", Context.MODE_PRIVATE); + try{ + fos.write(PerformTest.gain); + fos.close(); + } catch (IOException q) {System.out.println (q.toString());} + } catch (FileNotFoundException e) {System.out.println (e.toString()); + } + } + + public static int readGain(Context context){ + try{ + FileInputStream fis = context.openFileInput("Gain"); + int gain=fis.read(); + fis.close(); + return gain; + } catch (IOException e) {} + return PerformTest.defaultGain; // return default gain + } + + public static void deleteAllFiles(Context context){ + File file = new File(context.getFilesDir()+"/"); + for(File tempFile : file.listFiles()) { + tempFile.delete(); + } + } + public void deleteCalibration(Context context){ File file = new File(context.getFilesDir()+"/" + "CalibrationPreferences"); file.delete(); diff --git a/app/src/main/java/org/woheller69/audiometry/MainActivity.java b/app/src/main/java/org/woheller69/audiometry/MainActivity.java index eaa018b..31f6691 100644 --- a/app/src/main/java/org/woheller69/audiometry/MainActivity.java +++ b/app/src/main/java/org/woheller69/audiometry/MainActivity.java @@ -6,7 +6,6 @@ import android.os.Build; import android.os.Bundle; import android.os.Environment; -import android.provider.DocumentsContract; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; @@ -47,6 +46,8 @@ protected void onCreate(Bundle savedInstanceState) { result -> { File intData = new File(Environment.getDataDirectory() + "//data//" + this.getPackageName()); if (result.getData()!=null && result.getData().getData()!=null) Backup.zipExtract(this, intData, result.getData().getData()); + PerformTest.gain=FileOperations.readGain(this); + //Toast.makeText(this,"Gain: "+PerformTest.gain,Toast.LENGTH_LONG).show(); checkShowInvisibleButtons(); }); } @@ -60,6 +61,10 @@ private void checkShowInvisibleButtons(){ testResults.setVisibility(View.VISIBLE); startSingleTest.setVisibility(View.VISIBLE); if (GithubStar.shouldShowStarDialog(this)) GithubStar.starDialog(this,"https://github.com/woheller69/audiometer"); + } else { + startTest.setVisibility(View.GONE); + testResults.setVisibility(View.GONE); + startSingleTest.setVisibility(View.GONE); } } /** @@ -115,6 +120,7 @@ public boolean onCreateOptionsMenu(Menu menu) { } else { menu.findItem(R.id.user).setIcon(R.drawable.ic_user2_36dp); } + menu.findItem(R.id.lowGain).setChecked(FileOperations.readGain(this) != PerformTest.highGain); return true; } @@ -128,6 +134,7 @@ public boolean onOptionsItemSelected(MenuItem item) { File intData; int id = item.getItemId(); if (id==R.id.backup) { + FileOperations.writeGain(this); intData = new File(Environment.getDataDirectory()+"//data//" + this.getPackageName() + "//files//"); extStorage = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS); String filesBackup = getResources().getString(R.string.app_name)+".zip"; @@ -171,6 +178,8 @@ public boolean onOptionsItemSelected(MenuItem item) { mRestore.launch(intent); } else { Backup.zipExtract(this, intData, Uri.fromFile(zipFileBackup)); + PerformTest.gain=FileOperations.readGain(this); + //Toast.makeText(this,"Gain: "+PerformTest.gain,Toast.LENGTH_LONG).show(); checkShowInvisibleButtons(); } } @@ -179,6 +188,23 @@ public boolean onOptionsItemSelected(MenuItem item) { AlertDialog dialog = builder.create(); dialog.show(); Objects.requireNonNull(dialog.getWindow()).setGravity(Gravity.BOTTOM); + }else if (id==R.id.lowGain){ + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(getResources().getString(R.string.changeGain)); + builder.setMessage(getResources().getString(R.string.changeGainDescription)); + builder.setPositiveButton(R.string.dialog_OK_button, (dialog, whichButton) -> { + if (item.isChecked()) PerformTest.gain=PerformTest.highGain; // item.isChecked always previous value until invalidated, so value has to be inverted + else PerformTest.gain=PerformTest.lowGain; + FileOperations.deleteAllFiles(this); + FileOperations.writeGain(this); + //Toast.makeText(this,"Gain: "+FileOperations.readGain(this),Toast.LENGTH_LONG).show(); + checkShowInvisibleButtons(); + invalidateOptionsMenu(); + }); + builder.setNegativeButton(R.string.dialog_NO_button, (dialog, whichButton) -> dialog.cancel()); + AlertDialog dialog = builder.create(); + dialog.show(); + } else if (id==R.id.user){ SharedPreferences prefManager = PreferenceManager.getDefaultSharedPreferences(this); if (prefManager.getInt("user",1)==1){ diff --git a/app/src/main/java/org/woheller69/audiometry/PerformSingleTest.java b/app/src/main/java/org/woheller69/audiometry/PerformSingleTest.java index 4c30ee1..60d209c 100644 --- a/app/src/main/java/org/woheller69/audiometry/PerformSingleTest.java +++ b/app/src/main/java/org/woheller69/audiometry/PerformSingleTest.java @@ -159,6 +159,7 @@ protected void onCreate(Bundle savedInstanceState) { @Override public void onResume() { + gain=FileOperations.readGain(this); AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE); am.setStreamVolume(AudioManager.STREAM_MUSIC, gain, 0); testThread = new testThread(); diff --git a/app/src/main/java/org/woheller69/audiometry/PerformTest.java b/app/src/main/java/org/woheller69/audiometry/PerformTest.java index 2c2f8a5..03a3c3e 100644 --- a/app/src/main/java/org/woheller69/audiometry/PerformTest.java +++ b/app/src/main/java/org/woheller69/audiometry/PerformTest.java @@ -28,7 +28,10 @@ public class PerformTest extends AppCompatActivity { private final int sampleRate = 44100; private final int numSamples = duration * sampleRate; private final int volume = 32767; - static public int gain = 9; + static public final int lowGain = 4; + static public final int highGain = 9; + static public final int defaultGain = highGain; + static public int gain = defaultGain; static public final int[] testFrequencies = {125, 250, 500, 1000, 2000, 3000, 4000, 6000, 8000}; static final float[] correctiondBSPLtodBHL ={19.7f,9.0f,2.0f,0f,-3.7f,-8.1f,-7.8f, 2.1f,10.2f}; //estimated from ISO226:2003 hearing threshold. Taken from https://github.com/IoSR-Surrey/MatlabToolbox/blob/master/%2Biosr/%2Bauditory/iso226.m Corrected to value=0 @1000Hz private boolean heard = false; @@ -278,6 +281,7 @@ public boolean onDoubleTap(MotionEvent e) { @Override public void onResume() { + gain=FileOperations.readGain(this); AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE); am.setStreamVolume(AudioManager.STREAM_MUSIC, gain, 0); testThread = new testThread(); diff --git a/app/src/main/java/org/woheller69/audiometry/TestLookup.java b/app/src/main/java/org/woheller69/audiometry/TestLookup.java index 6011860..74098fe 100644 --- a/app/src/main/java/org/woheller69/audiometry/TestLookup.java +++ b/app/src/main/java/org/woheller69/audiometry/TestLookup.java @@ -120,6 +120,7 @@ public static String[] getAllSavedTests(Context context) { for (Iterator iterator = list.iterator(); iterator.hasNext();) { String string = iterator.next(); if (string.equals("CalibrationPreferences")) iterator.remove(); + else if (string.equals("Gain")) iterator.remove(); else if (user == 1 && string.contains("U2")) iterator.remove(); else if (user == 2 && !string.contains("U2")) iterator.remove(); } diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml index 9e4b337..fe4de0d 100644 --- a/app/src/main/res/menu/main.xml +++ b/app/src/main/res/menu/main.xml @@ -11,6 +11,10 @@ android:id="@+id/restore" android:title="@string/main_restore" app:showAsAction="never" /> + Berechtigung erforderlich %s benötigt Zugriff auf den externen Speicher. Bitte die Berechtigung erteilen und erneut versuchen. Bitte die Datei zuerst löschen und erneut versuchen. - Fehler: Leiser nicht möglich! + Fehler: Leiser nicht möglich! Probieren Sie die Option: Geringere Verstärkung nutzen Amplitude: %d Debug Löschen @@ -46,4 +46,7 @@ LINKS RECHTS Test angehalten + Geringere Verstärkung nutzen + Verstärkung ändern + Alle Kalibrierungs- und Testdaten werden gelöscht. Fortsetzen? diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 797a470..9c42e8e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -38,7 +38,7 @@ Permission required %s needs access to external storage. Please accept permission and try again. Please delete file and try again - Error: cannot get lower! + Error: cannot get lower! Try setting: Use low gain Amplitude: %d Debug Delete @@ -46,4 +46,7 @@ LEFT RIGHT Test paused + Use low gain + Change gain + This will delete all calibration and test data. Continue?