Skip to content

Commit

Permalink
Merge pull request #306 from nicolas-f/addattributecalibration
Browse files Browse the repository at this point in the history
Add calibration method property
  • Loading branch information
nicolas-f authored Jan 30, 2020
2 parents da38d4b + 4c872b1 commit 8dce7b4
Show file tree
Hide file tree
Showing 19 changed files with 94 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ android {
applicationId "org.noise_planet.noisecapture"
minSdkVersion 15
targetSdkVersion 28
versionCode 48
versionName "1.2.12"
versionCode 49
versionName "1.2.13"
// Store build date in apk
buildConfigField "long", "TIMESTAMP", System.currentTimeMillis() + "L"
buildConfigField "String", "GITHASH", "\"${getCheckedOutGitCommitHash().toString()}\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ private void doApply() {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("settings_recording_gain", String.valueOf(gain));
editor.putString("settings_calibration_method", String.valueOf(CALIBRATION_MODE_CALIBRATOR.equals(calibration_mode) ? Storage.Record.CALIBRATION_METHODS.Calibrator.ordinal() : Storage.Record.CALIBRATION_METHODS.Reference.ordinal()));
editor.apply();
Toast.makeText(getApplicationContext(),
getString(R.string.calibrate_done, gain), Toast.LENGTH_LONG).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public void onApply(View v) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("settings_recording_gain", String.valueOf(averageGain));
editor.putString("settings_calibration_method", String.valueOf(Storage.Record.CALIBRATION_METHODS.Traffic.ordinal()));
editor.apply();
Toast.makeText(getApplicationContext(),
getString(R.string.calibrate_done, averageGain), Toast.LENGTH_LONG).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ private void onNewMessage(byte... data) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("settings_recording_gain", String.valueOf(gain));
editor.putString("settings_calibration_method", String.valueOf(Storage.Record.CALIBRATION_METHODS.CalibratedSmartPhone.ordinal()));
editor.apply();
setState(CALIBRATION_STATE.AWAITING_START);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
} else if(MAXIMAL_MEASURE_TIME_SETTING.equals(key)) {
maximalMeasurementTime = getInteger(sharedPreferences,MAXIMAL_MEASURE_TIME_SETTING, DEFAULT_MAXIMAL_MEASURE_TIME_SETTING);
} else if("settings_recording_gain".equals(key) && measurementService != null) {
measurementService.setdBGain(getDouble(sharedPreferences, key, 0));
measurementService.setdBGain(getDouble(sharedPreferences, key, 0), getInteger(sharedPreferences, "settings_calibration_method", 0));
} else if(SETTINGS_MEASUREMENT_DISPLAY_WINDOW.equals(key) && measurementService != null) {
measurementService.getAudioProcess().setHannWindowFast(sharedPreferences.getString(SETTINGS_MEASUREMENT_DISPLAY_WINDOW, "RECTANGULAR").equals("HANN"));
if(BuildConfig.DEBUG) {
Expand Down Expand Up @@ -806,7 +806,7 @@ public void onServiceConnected(ComponentName className, IBinder service) {
measurementService.setDeletedLeqOnPause(getInteger(sharedPref,MeasurementActivity.DELETE_LEQ_ON_PAUSE_SETTING,
MeasurementActivity.DEFAULT_DELETE_LEQ_ON_PAUSE));
measurementService.setdBGain(
getDouble(sharedPref,"settings_recording_gain", 0));
getDouble(sharedPref,"settings_recording_gain", 0), getInteger(sharedPref, "settings_calibration_method", 0));
// Init gui if recording is ongoing
measurementService.addPropertyChangeListener(doProcessing);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

package org.noise_planet.noisecapture;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
Expand Down Expand Up @@ -70,6 +69,7 @@ public class MeasurementExport {
public static final String PROP_MODEL = "device_model";
public static final String PROP_TAGS = "tags";
public static final String PROP_GAIN_CALIBRATION = "gain_calibration";
public static final String PROP_METHOD_CALIBRATION = "method_calibration";
public static final String PROP_UUID = "uuid"; // Random anonymous ID that link non identified user's measure.
public static final String PROP_VERSION_NAME = "version_name";
public static final String PROP_BUILD_TIME = "build_date";
Expand Down Expand Up @@ -301,6 +301,7 @@ public void exportRecord(int recordId, OutputStream outputStream,boolean exportR
properties.setProperty(Storage.Record.COLUMN_UTC, String.valueOf(record.getUtc()));
properties.setProperty(Storage.Record.COLUMN_LEQ_MEAN, String.format(Locale.US, "%.02f", record.getLeqMean()));
properties.setProperty(PROP_GAIN_CALIBRATION, String.format(Locale.US, "%.02f", record.getCalibrationGain()));
properties.setProperty(PROP_METHOD_CALIBRATION, String.valueOf(record.getCalibrationMethod().name()));
properties.setProperty(Storage.Record.COLUMN_TIME_LENGTH, String.valueOf(record.getTimeLength()));
if(record.getPleasantness() != null) {
properties.setProperty(Storage.Record.COLUMN_PLEASANTNESS, String.valueOf(record.getPleasantness()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,13 @@ public void deleteRecords(Collection<Integer> recordIds) {
/**
* @return Record
*/
public int addRecord() {
public int addRecord(Storage.Record.CALIBRATION_METHODS calibrationMethod) {
SQLiteDatabase database = storage.getWritableDatabase();
try {
ContentValues contentValues = new ContentValues();
contentValues.put(Storage.Record.COLUMN_UTC, System.currentTimeMillis());
contentValues.put(Storage.Record.COLUMN_UPLOAD_ID, "");
contentValues.put(Storage.Record.COLUMN_CALIBRATION_METHOD, calibrationMethod.ordinal());
try {
return (int) database.insertOrThrow(Storage.Record.TABLE_NAME, null, contentValues);
} catch (SQLException sqlException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private enum LISTENER {GPS, NETWORK, PASSIVE}
// Seconds to delete when pause is activated
private int deletedLeqOnPause = 0;
private double dBGain = 0;
private int calibrationMethod = 0;
private PropertyChangeSupport listeners = new PropertyChangeSupport(this);
private static final Logger LOGGER = LoggerFactory.getLogger(MeasurementService.class);

Expand Down Expand Up @@ -128,8 +129,9 @@ MeasurementService getService() {
/**
* @param dBGain Gain in dB
*/
public void setdBGain(double dBGain) {
public void setdBGain(double dBGain, int calibrationMethod) {
this.dBGain = dBGain;
this.calibrationMethod = calibrationMethod;
if(audioProcess != null && Double.compare(0, dBGain) != 0) {
audioProcess.setGain((float)Math.pow(10, dBGain / 20));
}
Expand Down Expand Up @@ -681,7 +683,7 @@ public void startStorage() {
if(!isRecording()) {
startRecording();
}
recordId = measurementManager.addRecord();
recordId = measurementManager.addRecord(Storage.Record.CALIBRATION_METHODS.values()[calibrationMethod]);
leqAdded.set(0);
isStorageActivated.set(true);
showNotification();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,34 @@
package org.noise_planet.noisecapture;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class SettingsActivity extends MainActivity {
public class SettingsActivity extends MainActivity implements SharedPreferences.OnSharedPreferenceChangeListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Intent intent = getIntent();
initDrawer(intent != null ? intent.getIntExtra(RESULTS_RECORD_ID, -1) : null);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
sharedPref.registerOnSharedPreferenceChangeListener(this);

}


@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if("settings_recording_gain".equals(key)) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("settings_calibration_method", String.valueOf(Storage.Record.CALIBRATION_METHODS.ManualSetting.ordinal()));
editor.apply();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Expand Down
30 changes: 26 additions & 4 deletions app/src/main/java/org/noise_planet/noisecapture/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public TagInfo(int id, String name, int location, int color) {
}
}
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 10;
public static final int DATABASE_VERSION = 11;
public static final String DATABASE_NAME = "Storage.db";
private static final String ACTIVATE_FOREIGN_KEY = "PRAGMA foreign_keys=ON;";

Expand Down Expand Up @@ -189,6 +189,12 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
oldVersion = 10;
}
if(oldVersion == 10) {
if(!db.isReadOnly()) {
db.execSQL("ALTER TABLE RECORD ADD COLUMN calibration_method INTEGER DEFAULT 0");
}
oldVersion = 11;
}
}


Expand Down Expand Up @@ -282,6 +288,8 @@ private static Bitmap getBitmap(Cursor cursor, String field) {
}

public static class Record implements BaseColumns {
enum CALIBRATION_METHODS {None, ManualSetting, Calibrator, Reference, CalibratedSmartPhone, Traffic}

public static final String TABLE_NAME = "record";
public static final String COLUMN_ID = "record_id";
public static final String COLUMN_UTC = "record_utc";
Expand All @@ -293,6 +301,7 @@ public static class Record implements BaseColumns {
public static final String COLUMN_PHOTO_URI = "photo_uri";
public static final String COLUMN_CALIBRATION_GAIN = "calibration_gain";
public static final String COLUMN_NOISEPARTY_TAG = "noiseparty_tag";
public static final String COLUMN_CALIBRATION_METHOD = "calibration_method";

private int id;
private long utc;
Expand All @@ -304,14 +313,17 @@ public static class Record implements BaseColumns {
private Uri photoUri;
private float calibrationGain;
private String noisePartyTag;
private CALIBRATION_METHODS calibrationMethod;


public Record(Cursor cursor) {
this(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)),
cursor.getLong(cursor.getColumnIndex(COLUMN_UTC)),
cursor.getString(cursor.getColumnIndex(COLUMN_UPLOAD_ID)),
cursor.getFloat(cursor.getColumnIndex(COLUMN_LEQ_MEAN)),
cursor.getInt(cursor.getColumnIndex(COLUMN_TIME_LENGTH)),
cursor.getFloat(cursor.getColumnIndex(COLUMN_CALIBRATION_GAIN)));
cursor.getFloat(cursor.getColumnIndex(COLUMN_CALIBRATION_GAIN)),
cursor.getColumnIndex(COLUMN_CALIBRATION_METHOD) != -1 ? cursor.getInt(cursor.getColumnIndex(COLUMN_CALIBRATION_METHOD)) : 0);
noisePartyTag = getString(cursor, COLUMN_NOISEPARTY_TAG);
description = getString(cursor, COLUMN_DESCRIPTION);
String uriString = getString(cursor, COLUMN_PHOTO_URI);
Expand All @@ -322,13 +334,14 @@ public Record(Cursor cursor) {
}

public Record(int id, long utc, String uploadId, float leqMean, int timeLength,
float calibrationGain) {
float calibrationGain, int calibrationMethod) {
this.id = id;
this.utc = utc;
this.uploadId = uploadId;
this.leqMean = leqMean;
this.timeLength = timeLength;
this.calibrationGain = calibrationGain;
this.calibrationMethod = CALIBRATION_METHODS.values()[calibrationMethod];
}

/**
Expand All @@ -354,6 +367,14 @@ public Uri getPhotoUri() {
return photoUri;
}

public CALIBRATION_METHODS getCalibrationMethod() {
return calibrationMethod;
}

public void setCalibrationMethod(CALIBRATION_METHODS calibrationMethod) {
this.calibrationMethod = calibrationMethod;
}

/**
* @return Calibration gain in dB
*/
Expand Down Expand Up @@ -408,7 +429,8 @@ public float getLeqMean() {
Record.COLUMN_PHOTO_URI + " TEXT, " +
Record.COLUMN_PLEASANTNESS + " SMALLINT," +
Record.COLUMN_CALIBRATION_GAIN + " FLOAT DEFAULT 0," +
Record.COLUMN_NOISEPARTY_TAG + " TEXT" +
Record.COLUMN_NOISEPARTY_TAG + " TEXT," +
Record.COLUMN_CALIBRATION_METHOD + " INTEGER DEFAULT 0" +
")";


Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@
android:entries="@array/output_mode_labels"
/>

<EditTextPreference
android:defaultValue="0"
android:key="settings_calibration_method"
android:visibility="gone"
android:inputType="number"/>

</PreferenceCategory>

<PreferenceCategory android:title="@string/title_activity_map">
Expand Down
15 changes: 8 additions & 7 deletions app/src/test/java/org/noise_planet/noisecapture/TestDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testCreate() throws URISyntaxException {
MeasurementManager measurementManager =
new MeasurementManager(RuntimeEnvironment.application);

int recordId = measurementManager.addRecord();
int recordId = measurementManager.addRecord(Storage.Record.CALIBRATION_METHODS.None);
Storage.Leq leq = new Storage.Leq(recordId, -1, System.currentTimeMillis(), 12, 15, 50.d,
15.f, 4.f, 4.5f,System.currentTimeMillis());
List<Storage.LeqValue> leqValues = new ArrayList<Storage.LeqValue>();
Expand Down Expand Up @@ -152,7 +152,7 @@ public void testResults() throws URISyntaxException {
MeasurementManager measurementManager =
new MeasurementManager(RuntimeEnvironment.application);

int recordId = measurementManager.addRecord();
int recordId = measurementManager.addRecord(Storage.Record.CALIBRATION_METHODS.None);
Storage.Leq leq = new Storage.Leq(recordId, -1, System.currentTimeMillis(), 12, 15, 50.d,
15.f, 4.f, 4.5f,System.currentTimeMillis());
List<Storage.LeqValue> leqValues = new ArrayList<Storage.LeqValue>();
Expand Down Expand Up @@ -207,7 +207,7 @@ public void testExport() throws URISyntaxException, IOException {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application);
assertTrue(sharedPref.edit().putString("settings_user_noise_knowledge", "NOVICE").commit());

int recordId = measurementManager.addRecord();
int recordId = measurementManager.addRecord(Storage.Record.CALIBRATION_METHODS.Traffic);
Storage.Leq leq = new Storage.Leq(recordId, -1, System.currentTimeMillis(), 12, 15, 50.d,
15.f, 4.f, 4.5f,System.currentTimeMillis());
List<Storage.LeqValue> leqValues = new ArrayList<Storage.LeqValue>();
Expand Down Expand Up @@ -254,11 +254,11 @@ public void testExport() throws URISyntaxException, IOException {
assertEquals(JsonToken.BEGIN_OBJECT, jsonReader.peek());
jsonReader.beginObject();
assertEquals(JsonToken.NAME, jsonReader.peek());
Assert.assertEquals("type", jsonReader.nextName());
assertEquals("type", jsonReader.nextName());
assertEquals(JsonToken.STRING, jsonReader.peek());
Assert.assertEquals("FeatureCollection", jsonReader.nextString());
assertEquals("FeatureCollection", jsonReader.nextString());
assertEquals(JsonToken.NAME, jsonReader.peek());
Assert.assertEquals("features", jsonReader.nextName());
assertEquals("features", jsonReader.nextName());
assertEquals(JsonToken.BEGIN_ARRAY, jsonReader.peek());
foundJson = true;
}
Expand All @@ -268,6 +268,7 @@ public void testExport() throws URISyntaxException, IOException {
assertNotNull(meta);
assertNotNull(meta.getProperty(MeasurementExport.PROP_GAIN_CALIBRATION));
assertEquals("NOVICE", meta.getProperty(MeasurementExport.PROP_USER_PROFILE));
assertEquals(Storage.Record.CALIBRATION_METHODS.Traffic.name(), meta.getProperty(MeasurementExport.PROP_METHOD_CALIBRATION));
assertEquals("OGRS2018", meta.getProperty(Storage.Record.COLUMN_NOISEPARTY_TAG));
assertEquals(-4.76f, Float.valueOf(meta.getProperty(MeasurementExport.PROP_GAIN_CALIBRATION)), 0.01f);
assertEquals((float)leqBatch.computeGlobalLeq(),
Expand All @@ -283,7 +284,7 @@ public void testExportInvalidValues() throws URISyntaxException, IOException {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application);
assertTrue(sharedPref.edit().putString("settings_user_noise_knowledge", "NOVICE").commit());

int recordId = measurementManager.addRecord();
int recordId = measurementManager.addRecord(Storage.Record.CALIBRATION_METHODS.None);
Storage.Leq leq = new Storage.Leq(recordId, -1, System.currentTimeMillis(), 12, 15, 50.d,
15.f, 4.f, 4.5f,System.currentTimeMillis());
List<Storage.LeqValue> leqValues = new ArrayList<Storage.LeqValue>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
INSERT INTO "record" VALUES(29,1474192998234,'3d08cbac-acb7-4492-95d8-9f489e3ae45c',5.90635719299316406258e+01,62,'','',NULL,-7.0,NULL);
INSERT INTO "record" VALUES(29,1474192998234,'3d08cbac-acb7-4492-95d8-9f489e3ae45c',5.90635719299316406258e+01,62,'','',NULL,-7.0,NULL, 0);
COMMIT;
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Jun 08 14:25:40 CEST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-bin.zip
#Wed Jan 29 12:37:59 CET 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
Empty file.
Loading

0 comments on commit 8dce7b4

Please sign in to comment.