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)

master
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();
// 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();
}

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 */
public static String convertDistanceToString(double distance, int unitSystem) {
// check for locale and set unit system accordingly
String unit;
NumberFormat numberFormat = NumberFormat.getNumberInstance();
// check for locale and set unit system accordingly
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
distance = distance * 3.28084f;
// set measurement unit
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 {
// set measurement unit
unit = "m";
// set number precision
numberFormat.setMaximumFractionDigits(0);
}
}
// format distance according to current locale
NumberFormat numberFormat = NumberFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(0);
return numberFormat.format(distance) + " " + unit;
}

View File

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