diff --git a/OsmAnd/res/values-ru/strings.xml b/OsmAnd/res/values-ru/strings.xml index daea9de17f..0c0b131f5b 100644 --- a/OsmAnd/res/values-ru/strings.xml +++ b/OsmAnd/res/values-ru/strings.xml @@ -1,5 +1,14 @@ + Изменить единицы измерения длины и скорости + Единицы измерения + Мили/футы + Мили/ярды + Километры/метры + ярд + фт + мл/ч + мл Поделиться используя Tut : {0}\n{1} Чтобы увидить местоположение следуйте ссылке {0} или android ссылке {1} diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index d47b538bb4..03ca086c3b 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -1,5 +1,14 @@ + Change unit of length and speed + Unit of length + Miles/foots + Miles/yards + Kilometers/meters + yd + ft + mph + mi Share location using I\'m here : {0}\n{1} To see location follow the web browser link {0} or android intent link {1} diff --git a/OsmAnd/res/xml/settings_pref.xml b/OsmAnd/res/xml/settings_pref.xml index bdc5b2ba06..fa4033d222 100644 --- a/OsmAnd/res/xml/settings_pref.xml +++ b/OsmAnd/res/xml/settings_pref.xml @@ -50,8 +50,9 @@ - + + diff --git a/OsmAnd/src/net/osmand/OsmAndFormatter.java b/OsmAnd/src/net/osmand/OsmAndFormatter.java index 13981938ab..896862f0d2 100644 --- a/OsmAnd/src/net/osmand/OsmAndFormatter.java +++ b/OsmAnd/src/net/osmand/OsmAndFormatter.java @@ -4,23 +4,57 @@ import java.text.MessageFormat; import net.osmand.data.Amenity; import net.osmand.data.AmenityType; +import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; +import net.osmand.plus.OsmandSettings.MetricsConstants; import android.content.Context; public class OsmAndFormatter { + private final static float METERS_IN_KILOMETER = 1000f; + private final static float METERS_IN_MILE = 1609.344f; // 1609.344 + private final static float YARDS_IN_METER = 1.0936f; + private final static float FOOTS_IN_METER = YARDS_IN_METER * 3f; public static String getFormattedDistance(int meters, Context ctx) { - if (meters >= 100000) { - return meters / 1000 + " " + ctx.getString(R.string.km); //$NON-NLS-1$ - } else if (meters >= 10000) { - return MessageFormat.format("{0,number,#.#} " + ctx.getString(R.string.km), ((float) meters) / 1000); //$NON-NLS-1$ - } else if (meters > 1500) { - return MessageFormat.format("{0,number,#.#} " + ctx.getString(R.string.km), ((float) meters) / 1000); //$NON-NLS-1$ - } else if (meters > 900) { - return MessageFormat.format("{0,number,#.##} " + ctx.getString(R.string.km), ((float) meters) / 1000); //$NON-NLS-1$ + MetricsConstants mc = OsmandSettings.getDefaultMetricConstants(ctx); + int mainUnitStr; + float mainUnitInMeters; + if (mc == MetricsConstants.KILOMETERS_AND_METERS) { + mainUnitStr = R.string.km; + mainUnitInMeters = METERS_IN_KILOMETER; } else { - return meters + " " + ctx.getString(R.string.m); //$NON-NLS-1$ + mainUnitStr = R.string.mile; + mainUnitInMeters = METERS_IN_MILE; + } + + if (meters >= 100 * mainUnitInMeters) { + return meters / mainUnitInMeters + " " + ctx.getString(mainUnitStr); //$NON-NLS-1$ + } else if (meters > 1.5f * mainUnitInMeters) { + return MessageFormat.format("{0,number,#.#} " + ctx.getString(mainUnitStr), ((float) meters) / mainUnitInMeters); //$NON-NLS-1$ + } else if (meters > 0.9f * mainUnitInMeters) { + return MessageFormat.format("{0,number,#.##} " + ctx.getString(mainUnitStr), ((float) meters) / mainUnitInMeters); //$NON-NLS-1$ + } else { + if (mc == MetricsConstants.KILOMETERS_AND_METERS) { + return meters + " " + ctx.getString(R.string.m); //$NON-NLS-1$ + } else if (mc == MetricsConstants.MILES_AND_YARDS) { + int yards = (int) (meters * YARDS_IN_METER); + return yards + " " + ctx.getString(R.string.yard); //$NON-NLS-1$ + } else if(mc == MetricsConstants.MILES_AND_FOOTS) { + int foots = (int) (meters * FOOTS_IN_METER); + return foots + " " + ctx.getString(R.string.foot); //$NON-NLS-1$ + } + return meters + " " + ctx.getString(R.string.m); //$NON-NLS-1$ + } + } + + public static String getFormattedSpeed(float metersperseconds, Context ctx) { + MetricsConstants mc = OsmandSettings.getDefaultMetricConstants(ctx); + float kmh = metersperseconds * 3.6f; + if(mc == MetricsConstants.KILOMETERS_AND_METERS){ + return ((int) kmh) + ctx.getString(R.string.km_h); + } else { + return ((int) (kmh * METERS_IN_KILOMETER / METERS_IN_MILE)) + ctx.getString(R.string.mile_per_hour); } } diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 87981777da..b220710a3f 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -94,6 +94,22 @@ public class OsmandSettings { } } + + public enum MetricsConstants { + KILOMETERS_AND_METERS(R.string.si_km_m), + MILES_AND_YARDS(R.string.si_mi_yard), + MILES_AND_FOOTS(R.string.si_mi_foots); + + private final int key; + MetricsConstants(int key) { + this.key = key; + } + + public String toHumanString(Context ctx){ + return ctx.getResources().getString(key); + } + + } // These settings are stored in SharedPreferences public static final String SHARED_PREFERENCES_NAME = "net.osmand.settings"; //$NON-NLS-1$ @@ -147,7 +163,31 @@ public class OsmandSettings { } return internetConnectionAvailable; } + + // this value string is synchronized with settings_pref.xml preference name + public static final String DEFAULT_METRIC_SYSTEM = "default_metric_system"; //$NON-NLS-1$ + public static final int DEFAULT_METRIC_SYSTEM_DEF = 0; + // cache of metrics constants as they are used very often + private static MetricsConstants metricConstants = null; + + public static MetricsConstants getDefaultMetricConstants(Context ctx) { + if (metricConstants == null) { + int value = getSharedPreferences(ctx).getInt(DEFAULT_METRIC_SYSTEM, DEFAULT_METRIC_SYSTEM_DEF); + if (value >= MetricsConstants.values().length) { + metricConstants = MetricsConstants.KILOMETERS_AND_METERS; + } else { + metricConstants = MetricsConstants.values()[value]; + } + } + return metricConstants; + } + + public static void setDefaultMetricConstants(Editor editor, MetricsConstants constants){ + editor.putInt(DEFAULT_METRIC_SYSTEM, constants.ordinal()).commit(); + metricConstants = constants; + } + // this value string is synchronized with settings_pref.xml preference name diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java index 6d16dd1070..de9c2fef2f 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java @@ -21,6 +21,7 @@ import net.osmand.plus.ResourceManager; import net.osmand.plus.SQLiteTileSource; import net.osmand.plus.OsmandSettings.ApplicationMode; import net.osmand.plus.OsmandSettings.DayNightMode; +import net.osmand.plus.OsmandSettings.MetricsConstants; import net.osmand.plus.activities.RouteProvider.RouteService; import net.osmand.plus.render.BaseOsmandRender; import net.osmand.plus.render.RendererRegistry; @@ -97,6 +98,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference private ListPreference maxLevelToDownload; private ListPreference mapScreenOrientation; private ListPreference voicePreference; + private ListPreference metricPreference; private ListPreference rendererPreference; private ListPreference routeServiceInterval; private ListPreference routeServiceWaitInterval; @@ -162,16 +164,18 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference saveTrackInterval.setOnPreferenceChangeListener(this); positionOnMap =(ListPreference) screen.findPreference(OsmandSettings.POSITION_ON_MAP); positionOnMap.setOnPreferenceChangeListener(this); - mapScreenOrientation =(ListPreference) screen.findPreference(OsmandSettings.MAP_SCREEN_ORIENTATION); + mapScreenOrientation = (ListPreference) screen.findPreference(OsmandSettings.MAP_SCREEN_ORIENTATION); mapScreenOrientation.setOnPreferenceChangeListener(this); - maxLevelToDownload =(ListPreference) screen.findPreference(OsmandSettings.MAX_LEVEL_TO_DOWNLOAD_TILE); + maxLevelToDownload = (ListPreference) screen.findPreference(OsmandSettings.MAX_LEVEL_TO_DOWNLOAD_TILE); maxLevelToDownload.setOnPreferenceChangeListener(this); - tileSourcePreference =(ListPreference) screen.findPreference(OsmandSettings.MAP_TILE_SOURCES); + tileSourcePreference = (ListPreference) screen.findPreference(OsmandSettings.MAP_TILE_SOURCES); tileSourcePreference.setOnPreferenceChangeListener(this); - routerPreference =(ListPreference) screen.findPreference(OsmandSettings.ROUTER_SERVICE); + routerPreference = (ListPreference) screen.findPreference(OsmandSettings.ROUTER_SERVICE); routerPreference.setOnPreferenceChangeListener(this); - voicePreference =(ListPreference) screen.findPreference(OsmandSettings.VOICE_PROVIDER); + voicePreference = (ListPreference) screen.findPreference(OsmandSettings.VOICE_PROVIDER); voicePreference.setOnPreferenceChangeListener(this); + metricPreference = (ListPreference) screen.findPreference(OsmandSettings.DEFAULT_METRIC_SYSTEM); + metricPreference.setOnPreferenceChangeListener(this); rendererPreference =(ListPreference) screen.findPreference(OsmandSettings.RENDERER); rendererPreference.setOnPreferenceChangeListener(this); routeServiceInterval =(ListPreference) screen.findPreference(OsmandSettings.SERVICE_OFF_INTERVAL); @@ -301,6 +305,15 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference entries[i] = RouteService.values()[i].getName(); } fill(routerPreference, entries, entries, entry); + + names = new String[MetricsConstants.values().length]; + values = new String[MetricsConstants.values().length]; + entry = OsmandSettings.getDefaultMetricConstants(this).name(); + for (int i = 0; i < MetricsConstants.values().length; i++) { + values[i] = MetricsConstants.values()[i].name(); + names[i] = MetricsConstants.values()[i].toHumanString(this); + } + fill(metricPreference, names, values, entry); // read available voice data File extStorage = OsmandSettings.extendOsmandPath(getApplicationContext(), ResourceManager.VOICE_PATH); @@ -523,6 +536,9 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference } edit.commit(); getMyApplication().initCommandPlayer(); + } else if (preference == metricPreference) { + MetricsConstants mc = MetricsConstants.valueOf((String) newValue); + OsmandSettings.setDefaultMetricConstants(edit, mc); } else if (preference == tileSourcePreference) { if(VECTOR_MAP.equals((String) newValue)){ edit.putBoolean(OsmandSettings.MAP_VECTOR_DATA, true); diff --git a/OsmAnd/src/net/osmand/plus/views/MapInfoLayer.java b/OsmAnd/src/net/osmand/plus/views/MapInfoLayer.java index f450c9787b..fedd371e0f 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapInfoLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapInfoLayer.java @@ -235,7 +235,7 @@ public class MapInfoLayer implements OsmandMapLayer { if(map.getLastKnownLocation() != null && map.getLastKnownLocation().hasSpeed()){ if(Math.abs(map.getLastKnownLocation().getSpeed() - cachedSpeed) > .3f){ cachedSpeed = map.getLastKnownLocation().getSpeed(); - cachedSpeedString = ((int) (cachedSpeed * 3.6f)) + map.getString(R.string.km_h); + cachedSpeedString = OsmAndFormatter.getFormattedSpeed(cachedSpeed, map); float right = paintBlack.measureText(cachedSpeedString) + 8 * scaleCoefficient + boundsForSpeed.left; boundsForSpeed.right = boundsForDist.right = Math.max(right, boundsForDist.right); }