Trackbook now displays kilometer (or miles) if a distance is longer than 1 km (or 1 mi). Plus: added a unit-switch for Great Britain (see #12)

This commit is contained in:
y20k 2018-05-14 17:43:34 +02:00
parent 6af7859937
commit eda489e428
3 changed files with 38 additions and 11 deletions

View file

@ -253,7 +253,7 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
attachTapListenerToStatisticHeaderViews(); attachTapListenerToStatisticHeaderViews();
// attach listener for taps on statistics - for US and other states plagued by Imperial units // attach listener for taps on statistics - for US and other states plagued by Imperial units
if (LengthUnitHelper.getUnitSystem() == IMPERIAL) { if (LengthUnitHelper.getUnitSystem() == IMPERIAL || Locale.getDefault().getCountry().equals("GB")) {
attachTapListenerToStatisticsSheet(); attachTapListenerToStatisticsSheet();
} }

View file

@ -36,20 +36,47 @@ public final class LengthUnitHelper implements TrackbookKeys {
/* Converts for the given uni System a distance value to a readable string */ /* Converts for the given uni System a distance value to a readable string */
public static String convertDistanceToString(double distance, int unitSystem) { public static String convertDistanceToString(double distance, int unitSystem) {
// check for locale and set unit system accordingly
String unit; String unit;
NumberFormat numberFormat = NumberFormat.getNumberInstance();
// check for locale and set unit system accordingly
if (unitSystem == IMPERIAL) { if (unitSystem == IMPERIAL) {
// miles and feet
if (distance > 1610) {
// convert distance to miles
distance = distance * 0.000621371192;
// set measurement unit
unit = "mi";
// set number precision
numberFormat.setMaximumFractionDigits(2);
} else {
// convert distance to feet // convert distance to feet
distance = distance * 3.28084f; distance = distance * 3.28084f;
// set measurement unit // set measurement unit
unit = "ft"; unit = "ft";
// set number precision
numberFormat.setMaximumFractionDigits(0);
}
} else {
// kilometer and meter
if (distance >= 1000) {
// convert distance to kilometer
distance = distance * 0.001f;
// set measurement unit
unit = "km";
// set number precision
numberFormat.setMaximumFractionDigits(2);
} else { } else {
// set measurement unit // set measurement unit
unit = "m"; unit = "m";
// set number precision
numberFormat.setMaximumFractionDigits(0);
}
} }
// format distance according to current locale // format distance according to current locale
NumberFormat numberFormat = NumberFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(0);
return numberFormat.format(distance) + " " + unit; return numberFormat.format(distance) + " " + unit;
} }

View file

@ -30,7 +30,7 @@ allprojects {
constraintLayoutVersion = '1.1.0-beta5' constraintLayoutVersion = '1.1.0-beta5'
osmdroidVersion = '6.0.1' osmdroidVersion = '6.0.1'
gsonVersion = '2.8.2' gsonVersion = '2.8.4'
} }
} }