Skip to content

Commit 7f4d5f7

Browse files
committed
Add field separator and decimal comma preferences
Issue #921
1 parent 04343ec commit 7f4d5f7

File tree

4 files changed

+72
-35
lines changed

4 files changed

+72
-35
lines changed

gpslogger/src/main/java/com/mendhak/gpslogger/common/PreferenceHelper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,15 @@ public void setCSVDelimiter(String delimiter) {
538538
prefs.edit().putString(PreferenceNames.LOG_TO_CSV_DELIMITER, delimiter).apply();
539539
}
540540

541+
@ProfilePreference(name=PreferenceNames.LOG_TO_CSV_DECIMAL_COMMA)
542+
public boolean shouldCSVUseCommaInsteadOfPoint(){
543+
return prefs.getBoolean(PreferenceNames.LOG_TO_CSV_DECIMAL_COMMA, false);
544+
}
545+
546+
public void setShouldCSVUseCommaInsteadOfDecimal(boolean useComma){
547+
prefs.edit().putBoolean(PreferenceNames.LOG_TO_CSV_DECIMAL_COMMA, useComma).apply();
548+
}
549+
541550
/**
542551
* Whether to log to a GeoJSON file
543552
*/

gpslogger/src/main/java/com/mendhak/gpslogger/loggers/csv/CSVFileLogger.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,26 @@ public void annotate(String description, Location loc) throws Exception {
131131
try (CSVPrinter printer = new CSVPrinter(out, header)) {
132132
printer.printRecord(
133133
Strings.getIsoDateTime(new Date(loc.getTime())),
134-
loc.getLatitude(),
135-
loc.getLongitude(),
136-
loc.hasAltitude() ? loc.getAltitude() : "",
137-
loc.hasAccuracy() ? loc.getAccuracy() : "",
138-
loc.hasBearing() ? loc.getBearing() : "",
139-
loc.hasSpeed() ? loc.getSpeed() : "",
134+
applyDecimalComma(loc.getLatitude()),
135+
applyDecimalComma(loc.getLongitude()),
136+
loc.hasAltitude() ? applyDecimalComma(loc.getAltitude()) : "",
137+
loc.hasAccuracy() ? applyDecimalComma(loc.getAccuracy()) : "",
138+
loc.hasBearing() ? applyDecimalComma(loc.getBearing()) : "",
139+
loc.hasSpeed() ? applyDecimalComma(loc.getSpeed()) : "",
140140
Maths.getBundledSatelliteCount(loc),
141141
loc.getProvider(),
142-
(loc.getExtras() != null && !Strings.isNullOrEmpty(loc.getExtras().getString(BundleConstants.HDOP))) ? loc.getExtras().getString(BundleConstants.HDOP) : "",
143-
(loc.getExtras() != null && !Strings.isNullOrEmpty(loc.getExtras().getString(BundleConstants.VDOP))) ? loc.getExtras().getString(BundleConstants.VDOP) : "",
144-
(loc.getExtras() != null && !Strings.isNullOrEmpty(loc.getExtras().getString(BundleConstants.PDOP))) ? loc.getExtras().getString(BundleConstants.PDOP) : "",
145-
(loc.getExtras() != null && !Strings.isNullOrEmpty(loc.getExtras().getString(BundleConstants.GEOIDHEIGHT))) ? loc.getExtras().getString(BundleConstants.GEOIDHEIGHT) : "",
142+
(loc.getExtras() != null && !Strings.isNullOrEmpty(loc.getExtras().getString(BundleConstants.HDOP))) ? applyDecimalComma(loc.getExtras().getString(BundleConstants.HDOP)) : "",
143+
(loc.getExtras() != null && !Strings.isNullOrEmpty(loc.getExtras().getString(BundleConstants.VDOP))) ? applyDecimalComma(loc.getExtras().getString(BundleConstants.VDOP)) : "",
144+
(loc.getExtras() != null && !Strings.isNullOrEmpty(loc.getExtras().getString(BundleConstants.PDOP))) ? applyDecimalComma(loc.getExtras().getString(BundleConstants.PDOP)) : "",
145+
(loc.getExtras() != null && !Strings.isNullOrEmpty(loc.getExtras().getString(BundleConstants.GEOIDHEIGHT))) ? applyDecimalComma(loc.getExtras().getString(BundleConstants.GEOIDHEIGHT)) : "",
146146
(loc.getExtras() != null && !Strings.isNullOrEmpty(loc.getExtras().getString(BundleConstants.AGEOFDGPSDATA))) ? loc.getExtras().getString(BundleConstants.AGEOFDGPSDATA) : "",
147147
(loc.getExtras() != null && !Strings.isNullOrEmpty(loc.getExtras().getString(BundleConstants.DGPSID))) ? loc.getExtras().getString(BundleConstants.DGPSID) : "",
148148
"", //Activity detection was removed, but keeping this here for backward compatibility.
149149
(batteryLevel != null) ? batteryLevel : "",
150150
description,
151151
loc.getTime(),
152152
Strings.getIsoDateTimeWithOffset(new Date(loc.getTime())),
153-
Session.getInstance().getTotalTravelled(),
153+
applyDecimalComma(Session.getInstance().getTotalTravelled()),
154154
Session.getInstance().getStartTimeStamp(),
155155
PreferenceHelper.getInstance().getCurrentProfileName()
156156
);
@@ -160,6 +160,17 @@ public void annotate(String description, Location loc) throws Exception {
160160
Files.addToMediaDatabase(file, "text/csv");
161161
}
162162

163+
/**
164+
* Apply user selected decimal comma, if that option was enabled.
165+
*/
166+
private String applyDecimalComma(Object value) {
167+
String returnValue = String.valueOf(value);
168+
if(PreferenceHelper.getInstance().shouldCSVUseCommaInsteadOfPoint()){
169+
returnValue = returnValue.replace(".",",");
170+
}
171+
return returnValue;
172+
}
173+
163174
@Override
164175
public String getName() {
165176
return name;

gpslogger/src/main/java/com/mendhak/gpslogger/senders/customurl/CustomUrlManager.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,24 @@ private List<SerializableLocation> getLocationsFromCSV(File f) {
6363
for(CSVRecord record : records){
6464
Location csvLoc = new Location(record.get(CSVFileLogger.FIELDS.PROVIDER));
6565
csvLoc.setTime(Long.parseLong(record.get(CSVFileLogger.FIELDS.TIMESTAMP_MILLIS)));
66-
csvLoc.setLatitude(Double.parseDouble(record.get(CSVFileLogger.FIELDS.LAT)));
67-
csvLoc.setLongitude(Double.parseDouble(record.get(CSVFileLogger.FIELDS.LON)));
66+
csvLoc.setLatitude(Double.parseDouble( unApplyDecimalComma(record.get(CSVFileLogger.FIELDS.LAT)) ));
67+
csvLoc.setLongitude(Double.parseDouble( unApplyDecimalComma(record.get(CSVFileLogger.FIELDS.LON)) ));
6868

6969
if(!Strings.isNullOrEmpty(record.get(CSVFileLogger.FIELDS.ELEVATION))){
70-
csvLoc.setAltitude(Double.parseDouble(record.get(CSVFileLogger.FIELDS.ELEVATION)));
70+
csvLoc.setAltitude(Double.parseDouble( unApplyDecimalComma(record.get(CSVFileLogger.FIELDS.ELEVATION))));
7171
}
7272

7373
if(!Strings.isNullOrEmpty(record.get(CSVFileLogger.FIELDS.ACCURACY))){
74-
csvLoc.setAccuracy(Float.parseFloat(record.get(CSVFileLogger.FIELDS.ACCURACY)));
74+
csvLoc.setAccuracy(Float.parseFloat( unApplyDecimalComma(record.get(CSVFileLogger.FIELDS.ACCURACY))));
7575
}
7676

7777

7878
if(!Strings.isNullOrEmpty(record.get(CSVFileLogger.FIELDS.BEARING))){
79-
csvLoc.setBearing(Float.parseFloat(record.get(CSVFileLogger.FIELDS.BEARING)));
79+
csvLoc.setBearing(Float.parseFloat( unApplyDecimalComma(record.get(CSVFileLogger.FIELDS.BEARING))));
8080
}
8181

8282
if(!Strings.isNullOrEmpty(record.get(CSVFileLogger.FIELDS.SPEED))){
83-
csvLoc.setSpeed(Float.parseFloat(record.get(CSVFileLogger.FIELDS.SPEED)));
83+
csvLoc.setSpeed(Float.parseFloat(unApplyDecimalComma(record.get(CSVFileLogger.FIELDS.SPEED))));
8484
}
8585

8686
Bundle b = new Bundle();
@@ -89,11 +89,11 @@ private List<SerializableLocation> getLocationsFromCSV(File f) {
8989
b.putInt(BundleConstants.SATELLITES_FIX, Integer.parseInt(record.get(CSVFileLogger.FIELDS.SATELLITES)));
9090
}
9191

92-
b.putString(BundleConstants.HDOP, record.get(CSVFileLogger.FIELDS.HDOP));
93-
b.putString(BundleConstants.VDOP, record.get(CSVFileLogger.FIELDS.VDOP));
94-
b.putString(BundleConstants.PDOP, record.get(CSVFileLogger.FIELDS.PDOP));
92+
b.putString(BundleConstants.HDOP, unApplyDecimalComma(record.get(CSVFileLogger.FIELDS.HDOP)));
93+
b.putString(BundleConstants.VDOP, unApplyDecimalComma(record.get(CSVFileLogger.FIELDS.VDOP)));
94+
b.putString(BundleConstants.PDOP, unApplyDecimalComma(record.get(CSVFileLogger.FIELDS.PDOP)));
9595

96-
b.putString(BundleConstants.GEOIDHEIGHT, record.get(CSVFileLogger.FIELDS.GEOID_HEIGHT));
96+
b.putString(BundleConstants.GEOIDHEIGHT, unApplyDecimalComma(record.get(CSVFileLogger.FIELDS.GEOID_HEIGHT)));
9797
b.putString(BundleConstants.AGEOFDGPSDATA, record.get(CSVFileLogger.FIELDS.AGE_OF_DGPS_DATA));
9898
b.putString(BundleConstants.DGPSID, record.get(CSVFileLogger.FIELDS.DGPS_ID));
9999

@@ -105,7 +105,7 @@ private List<SerializableLocation> getLocationsFromCSV(File f) {
105105
b.putString(BundleConstants.TIME_WITH_OFFSET, record.get(CSVFileLogger.FIELDS.TIME_WITH_OFFSET));
106106

107107
if(!Strings.isNullOrEmpty(record.get(CSVFileLogger.FIELDS.DISTANCE))){
108-
b.putDouble(BundleConstants.DISTANCE, Double.parseDouble(record.get(CSVFileLogger.FIELDS.DISTANCE)));
108+
b.putDouble(BundleConstants.DISTANCE, Double.parseDouble(unApplyDecimalComma(record.get(CSVFileLogger.FIELDS.DISTANCE))));
109109
}
110110

111111
if(!Strings.isNullOrEmpty(record.get(CSVFileLogger.FIELDS.START_TIMESTAMP_MILLIS))){
@@ -127,6 +127,14 @@ private List<SerializableLocation> getLocationsFromCSV(File f) {
127127
return locations;
128128
}
129129

130+
/**
131+
* Replace commas with points, in case the CSV contained decimal commas.
132+
* This is necessary as all the subsequent processing expects decimals
133+
*/
134+
private String unApplyDecimalComma(String recordValue) {
135+
return recordValue.replace(",",".");
136+
}
137+
130138
private void sendLocations(SerializableLocation[] locations){
131139
if(locations.length > 0){
132140

gpslogger/src/main/java/com/mendhak/gpslogger/ui/fragments/settings/LoggingSettingsFragment.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@
2626
import android.os.Environment;
2727
import android.provider.Settings;
2828
import android.text.Html;
29-
import android.text.InputType;
3029

3130
import androidx.annotation.NonNull;
32-
import androidx.preference.ListPreference;
3331
import androidx.preference.Preference;
3432
import androidx.preference.PreferenceFragmentCompat;
3533
import androidx.preference.SwitchPreferenceCompat;
@@ -50,6 +48,7 @@
5048
import java.util.Date;
5149

5250
import eltos.simpledialogfragment.SimpleDialog;
51+
import eltos.simpledialogfragment.form.Check;
5352
import eltos.simpledialogfragment.form.Input;
5453
import eltos.simpledialogfragment.form.SimpleFormDialog;
5554
import eltos.simpledialogfragment.list.SimpleListDialog;
@@ -115,15 +114,19 @@ public void onCreate(Bundle savedInstanceState) {
115114
findPreference(PreferenceNames.LOGGING_WRITE_TIME_WITH_OFFSET).setOnPreferenceChangeListener(this);
116115
setPreferenceTimeZoneOffsetSummary(preferenceHelper.shouldWriteTimeWithOffset());
117116

118-
findPreference(PreferenceNames.LOG_TO_CSV_DELIMITER).setOnPreferenceClickListener(this);
119-
setPreferenceCsvSummary(preferenceHelper.getCSVDelimiter());
117+
findPreference("log_plain_text_csv_advanced").setOnPreferenceClickListener(this);
118+
setPreferenceCsvSummary(preferenceHelper.getCSVDelimiter(), preferenceHelper.shouldCSVUseCommaInsteadOfPoint());
120119

121120
}
122121

123-
private void setPreferenceCsvSummary(String delimiter){
124-
String sample = "aaa,bbb,25.189";
122+
private void setPreferenceCsvSummary(String delimiter, Boolean useComma){
123+
String sample = "lorem,ipsum,";
124+
String number = "12.345";
125125
sample = sample.replaceAll(",", delimiter);
126-
findPreference(PreferenceNames.LOG_TO_CSV_DELIMITER).setSummary(sample);
126+
if(useComma){
127+
number = number.replace(".", ",");
128+
}
129+
findPreference("log_plain_text_csv_advanced").setSummary(sample+number);
127130

128131
}
129132

@@ -172,19 +175,23 @@ public boolean onPreferenceClick(Preference preference) {
172175
return true;
173176
}
174177

175-
if(preference.getKey().equalsIgnoreCase(PreferenceNames.LOG_TO_CSV_DELIMITER)){
178+
if(preference.getKey().equalsIgnoreCase("log_plain_text_csv_advanced")){
176179
SimpleFormDialog.build()
177-
.title(R.string.log_plain_text_title_csv_delimiter)
180+
.title(R.string.log_plain_text_csv_advanced_title)
178181
.pos(R.string.ok)
179182
.neg(R.string.cancel)
180183
.fields(
181184
Input.plain(PreferenceNames.LOG_TO_CSV_DELIMITER)
185+
.hint(R.string.log_plain_text_csv_field_delimiter)
182186
.text(preferenceHelper.getCSVDelimiter())
183187
.max(1)
184188
.min(1)
185-
.required()
189+
.required(),
190+
Check.box(PreferenceNames.LOG_TO_CSV_DECIMAL_COMMA)
191+
.label(R.string.log_plain_text_decimal_comma)
192+
.check(preferenceHelper.shouldCSVUseCommaInsteadOfPoint())
186193
)
187-
.show(this,PreferenceNames.LOG_TO_CSV_DELIMITER);
194+
.show(this,"log_plain_text_csv_advanced");
188195
}
189196

190197
if(preference.getKey().equalsIgnoreCase(PreferenceNames.GPSLOGGER_FOLDER)){
@@ -328,10 +335,12 @@ public boolean onResult(@NonNull String dialogTag, int which, @NonNull Bundle ex
328335
return true;
329336
}
330337

331-
if(dialogTag.equalsIgnoreCase(PreferenceNames.LOG_TO_CSV_DELIMITER)){
338+
if(dialogTag.equalsIgnoreCase("log_plain_text_csv_advanced")){
332339
String delimiter = extras.getString(PreferenceNames.LOG_TO_CSV_DELIMITER);
340+
boolean useComma = extras.getBoolean(PreferenceNames.LOG_TO_CSV_DECIMAL_COMMA);
333341
preferenceHelper.setCSVDelimiter(delimiter);
334-
setPreferenceCsvSummary(preferenceHelper.getCSVDelimiter());
342+
preferenceHelper.setShouldCSVUseCommaInsteadOfDecimal(useComma);
343+
setPreferenceCsvSummary(preferenceHelper.getCSVDelimiter(), preferenceHelper.shouldCSVUseCommaInsteadOfPoint());
335344
return true;
336345
}
337346

0 commit comments

Comments
 (0)