moved metric/imperial conversion to its own helper class

This commit is contained in:
y20k 2018-04-23 18:27:58 +02:00
parent 72a2131e45
commit 31afd893e4
4 changed files with 120 additions and 74 deletions

View file

@ -55,6 +55,8 @@ import org.y20k.trackbook.core.Track;
import org.y20k.trackbook.helpers.DialogHelper;
import org.y20k.trackbook.helpers.DropdownAdapter;
import org.y20k.trackbook.helpers.ExportHelper;
import org.y20k.trackbook.helpers.LocaleUnitHelper;
import org.y20k.trackbook.helpers.LocationHelper;
import org.y20k.trackbook.helpers.LogHelper;
import org.y20k.trackbook.helpers.MapHelper;
import org.y20k.trackbook.helpers.StorageHelper;
@ -370,16 +372,16 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
}
// populate views
mDistanceView.setText(mTrack.getTrackDistanceString());
mDistanceView.setText(LocaleUnitHelper.convertDistanceToString(mTrack.getTrackDistance()));
mStepsView.setText(stepsTaken);
mWaypointsView.setText(String.valueOf(mTrack.getWayPoints().size()));
mDurationView.setText(mTrack.getTrackDurationString());
mDurationView.setText(LocationHelper.convertToReadableTime(mTrack.getTrackDuration(), true));
mRecordingStartView.setText(recordingStart);
mRecordingStopView.setText(recordingStop);
mPositiveElevationView.setText(mTrack.getPositiveElevationString());
mNegativeElevationView.setText(mTrack.getNegativeElevationString());
mMaxAltitudeView.setText(mTrack.getMaxAltitudeString());
mMinAltitudeView.setText(mTrack.getMinAltitudeString());
mPositiveElevationView.setText(LocaleUnitHelper.convertDistanceToString(mTrack.getPositiveElevation()));
mNegativeElevationView.setText(LocaleUnitHelper.convertDistanceToString(mTrack.getNegativeElevation()));
mMaxAltitudeView.setText(LocaleUnitHelper.convertDistanceToString(mTrack.getMaxAltitude()));
mMinAltitudeView.setText(LocaleUnitHelper.convertDistanceToString(mTrack.getMinAltitude()));
// show/hide elevation views depending on file format version
if (mTrack.getTrackFormatVersion() > 1 && mTrack.getMinAltitude() > 0) {
@ -511,7 +513,7 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
int dialogNegativeButton = R.string.dialog_default_action_cancel;
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
String recordingStartDate = df.format(mTrack.getRecordingStart());
String dialogMessage = getString(R.string.dialog_delete_content) + " " + recordingStartDate + " | " + mTrack.getTrackDistanceString();
String dialogMessage = getString(R.string.dialog_delete_content) + " " + recordingStartDate + " | " + LocaleUnitHelper.convertDistanceToString(mTrack.getTrackDistance());
// show delete dialog - results are handles by onActivityResult
DialogFragment dialogFragment = DialogHelper.newInstance(dialogTitle, dialogMessage, dialogPositiveButton, dialogNegativeButton);
@ -539,13 +541,13 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
if (ExportHelper.gpxFileExists(mTrack)) {
// CASE: OVERWRITE - GPX file exists
dialogTitle = R.string.dialog_export_title_overwrite;
dialogMessage = getString(R.string.dialog_export_content_overwrite) + " (" + recordingStartDate + " | " + mTrack.getTrackDistanceString() + ")";
dialogMessage = getString(R.string.dialog_export_content_overwrite) + " (" + recordingStartDate + " | " + LocaleUnitHelper.convertDistanceToString(mTrack.getTrackDistance()) + ")";
dialogPositiveButton = R.string.dialog_export_action_overwrite;
dialogNegativeButton = R.string.dialog_default_action_cancel;
} else {
// CASE: EXPORT - GPX file does NOT yet exits
dialogTitle = R.string.dialog_export_title_export;
dialogMessage = getString(R.string.dialog_export_content_export) + " (" + recordingStartDate + " | " + mTrack.getTrackDistanceString() + ")";
dialogMessage = getString(R.string.dialog_export_content_export) + " (" + recordingStartDate + " | " + LocaleUnitHelper.convertDistanceToString(mTrack.getTrackDistance()) + ")";
dialogPositiveButton = R.string.dialog_export_action_export;
dialogNegativeButton = R.string.dialog_default_action_cancel;
}

View file

@ -25,11 +25,9 @@ import org.y20k.trackbook.helpers.LocationHelper;
import org.y20k.trackbook.helpers.TrackbookKeys;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
/**
@ -275,40 +273,9 @@ public class Track implements TrackbookKeys, Parcelable {
}
/* Getter for string representation of maximum altitude of recording */
public String getMaxAltitudeString() {
return convertDistanceToString(mMaxAltitude);
}
/* Getter for string representation of lowest altitude of recording */
public String getMinAltitudeString() {
return convertDistanceToString(mMinAltitude);
}
/* Getter for string representation of positive elevation of recording (cumulative altitude difference) */
public String getPositiveElevationString() {
return convertDistanceToString(mPositiveElevation);
}
/* Getter for string representation of negative elevation of recording (cumulative altitude difference) */
public String getNegativeElevationString() {
return convertDistanceToString(mNegativeElevation);
}
/* Getter for string representation of track duration */
public String getTrackDurationString() {
return LocationHelper.convertToReadableTime(mDuration, true);
}
/* Getter for string representation of track distance */
public String getTrackDistanceString() {
double trackDistance = (double) mWayPoints.get(mWayPoints.size()-1).getDistanceToStartingPoint();
return convertDistanceToString(trackDistance);
/* Getter track distance */
public double getTrackDistance() {
return (double) mWayPoints.get(mWayPoints.size()-1).getDistanceToStartingPoint();
}
@ -334,35 +301,35 @@ public class Track implements TrackbookKeys, Parcelable {
// }
/* Converts a given distance value to a readable string */
private String convertDistanceToString(double distance) {
// check for locale and set unit system accordingly
String unit;
if (getUnitSystem(Locale.getDefault()) == IMPERIAL) {
// convert distance to feet
distance = distance * 3.28084f;
// set measurement unit
unit = "ft";
} else {
// set measurement unit
unit = "m";
}
return String.format (Locale.ENGLISH, "%.0f", distance) + unit;
}
// /* Converts a given distance value to a readable string */
// private String convertDistanceToString(double distance) {
// // check for locale and set unit system accordingly
// String unit;
// if (getUnitSystem(Locale.getDefault()) == IMPERIAL) {
// // convert distance to feet
// distance = distance * 3.28084f;
// // set measurement unit
// unit = "ft";
// } else {
// // set measurement unit
// unit = "m";
// }
// return String.format (Locale.ENGLISH, "%.0f", distance) + unit;
// }
/* Determines which unit system the device is using (metric or imperial) */
private int getUnitSystem(Locale locale) {
// America (US), Liberia (LR), Myanmar(MM) use the imperial system
List<String> imperialSystemCountries = Arrays.asList("US", "LR", "MM");
String countryCode = locale.getCountry();
if (imperialSystemCountries.contains(countryCode)){
return IMPERIAL;
} else {
return METRIC;
}
}
// /* Determines which unit system the device is using (metric or imperial) */
// private int getUnitSystem(Locale locale) {
// // America (US), Liberia (LR), Myanmar(MM) use the imperial system
// List<String> imperialSystemCountries = Arrays.asList("US", "LR", "MM");
// String countryCode = locale.getCountry();
//
// if (imperialSystemCountries.contains(countryCode)){
// return IMPERIAL;
// } else {
// return METRIC;
// }
// }
@Override

View file

@ -0,0 +1,77 @@
/**
* LocaleUnitHelper.java
* Implements the LocaleUnitHelper class
* A LocaleUnitHelper offers helper methods for dealing with unit systems and locales
*
* This file is part of
* TRACKBOOK - Movement Recorder for Android
*
* Copyright (c) 2016-18 - Y20K.org
* Licensed under the MIT-License
* http://opensource.org/licenses/MIT
*
* Trackbook uses osmdroid - OpenStreetMap-Tools for Android
* https://github.com/osmdroid/osmdroid
*/
package org.y20k.trackbook.helpers;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
/**
* LocaleUnitHelper class
*/
public final class LocaleUnitHelper implements TrackbookKeys {
/* Converts for the default locale a distance value to a readable string */
public static String convertDistanceToString(double distance) {
return convertDistanceToString(distance, getUnitSystem(Locale.getDefault()));
}
/* Converts for the given uni System a distance value to a readable string */
public static String convertDistanceToString(double distance, int unitSystem) {
// check for locale and set unit system accordingly
String unit;
if (unitSystem == IMPERIAL) {
// convert distance to feet
distance = distance * 3.28084f;
// set measurement unit
unit = "ft";
} else {
// set measurement unit
unit = "m";
}
return String.format (Locale.ENGLISH, "%.0f", distance) + unit;
}
/* Determines which unit system the device is using (metric or imperial) */
private static int getUnitSystem(Locale locale) {
// America (US), Liberia (LR), Myanmar(MM) use the imperial system
List<String> imperialSystemCountries = Arrays.asList("US", "LR", "MM");
String countryCode = locale.getCountry();
if (imperialSystemCountries.contains(countryCode)){
return IMPERIAL;
} else {
return METRIC;
}
}
/* Returns the opposite uni system based on the current locale */
public static int getOppositeUnitSystem() {
int unitSystem = getUnitSystem(Locale.getDefault());
if (unitSystem == METRIC){
return IMPERIAL;
} else {
return METRIC;
}
}
}

View file

@ -163,8 +163,8 @@ public final class NotificationHelper implements TrackbookKeys {
/* Build context text for notification builder */
private static String getContextString(Context context, Track track) {
return context.getString(R.string.notification_content_distance) + ": " + track.getTrackDistanceString() + " | " +
context.getString(R.string.notification_content_duration) + ": " + track.getTrackDurationString();
return context.getString(R.string.notification_content_distance) + ": " + LocaleUnitHelper.convertDistanceToString(track.getTrackDistance()) + " | " +
context.getString(R.string.notification_content_duration) + ": " + LocationHelper.convertToReadableTime(track.getTrackDuration(), true);
}
}