Parse distance in location messages

This commit is contained in:
Chumva 2019-12-10 12:00:39 +02:00
parent 46700ed438
commit c8dc6c29ea
2 changed files with 38 additions and 21 deletions

View file

@ -17,6 +17,14 @@ object OsmandFormatter {
val YARDS_IN_ONE_METER = 1.0936f val YARDS_IN_ONE_METER = 1.0936f
val FEET_IN_ONE_METER = YARDS_IN_ONE_METER * 3f val FEET_IN_ONE_METER = YARDS_IN_ONE_METER * 3f
val FORMAT_METERS_KEY = "m"
val FORMAT_FEET_KEY = "ft"
val FORMAT_YARDS_KEY = "yd"
val FORMAT_KILOMETERS_KEY = "km"
val FORMAT_NAUTICALMILES_KEY = "nmi"
val FORMAT_MILES_KEY = "mi"
private val fixed2 = DecimalFormat("0.00") private val fixed2 = DecimalFormat("0.00")
private val fixed1 = DecimalFormat("0.0") private val fixed1 = DecimalFormat("0.0")
@ -175,15 +183,15 @@ object OsmandFormatter {
val mainUnitInMeters: Float val mainUnitInMeters: Float
when (mc) { when (mc) {
MetricsConstants.KILOMETERS_AND_METERS -> { MetricsConstants.KILOMETERS_AND_METERS -> {
mainUnitStr = if (useLocalizedString) ctx.getString(R.string.km) else "km" mainUnitStr = if (useLocalizedString) ctx.getString(R.string.km) else FORMAT_KILOMETERS_KEY
mainUnitInMeters = METERS_IN_KILOMETER mainUnitInMeters = METERS_IN_KILOMETER
} }
MetricsConstants.NAUTICAL_MILES -> { MetricsConstants.NAUTICAL_MILES -> {
mainUnitStr = if (useLocalizedString) ctx.getString(R.string.nm) else "nmi" mainUnitStr = if (useLocalizedString) ctx.getString(R.string.nm) else FORMAT_NAUTICALMILES_KEY
mainUnitInMeters = METERS_IN_ONE_NAUTICALMILE mainUnitInMeters = METERS_IN_ONE_NAUTICALMILE
} }
else -> { else -> {
mainUnitStr = if (useLocalizedString) ctx.getString(R.string.mile) else "mi" mainUnitStr = if (useLocalizedString) ctx.getString(R.string.mile) else FORMAT_MILES_KEY
mainUnitInMeters = METERS_IN_ONE_MILE mainUnitInMeters = METERS_IN_ONE_MILE
} }
} }
@ -204,15 +212,15 @@ object OsmandFormatter {
return MessageFormat.format(format2 + mainUnitStr, meters / mainUnitInMeters).replace('\n', ' ') //$NON-NLS-1$ return MessageFormat.format(format2 + mainUnitStr, meters / mainUnitInMeters).replace('\n', ' ') //$NON-NLS-1$
} else { } else {
if (mc == MetricsConstants.KILOMETERS_AND_METERS || mc == MetricsConstants.MILES_AND_METERS) { if (mc == MetricsConstants.KILOMETERS_AND_METERS || mc == MetricsConstants.MILES_AND_METERS) {
return (meters + 0.5).toInt().toString() + if (useLocalizedString) " " + ctx.getString(R.string.m) else " m" //$NON-NLS-1$ return (meters + 0.5).toInt().toString() + if (useLocalizedString) " " + ctx.getString(R.string.m) else " $FORMAT_METERS_KEY" //$NON-NLS-1$
} else if (mc == MetricsConstants.MILES_AND_FEET) { } else if (mc == MetricsConstants.MILES_AND_FEET) {
val feet = (meters * FEET_IN_ONE_METER + 0.5).toInt() val feet = (meters * FEET_IN_ONE_METER + 0.5).toInt()
return feet.toString() + if (useLocalizedString) " " + ctx.getString(R.string.foot) else " ft" //$NON-NLS-1$ return feet.toString() + if (useLocalizedString) " " + ctx.getString(R.string.foot) else " $FORMAT_FEET_KEY" //$NON-NLS-1$
} else if (mc == MetricsConstants.MILES_AND_YARDS) { } else if (mc == MetricsConstants.MILES_AND_YARDS) {
val yards = (meters * YARDS_IN_ONE_METER + 0.5).toInt() val yards = (meters * YARDS_IN_ONE_METER + 0.5).toInt()
return yards.toString() + if (useLocalizedString) " " + ctx.getString(R.string.yard) else " yd" //$NON-NLS-1$ return yards.toString() + if (useLocalizedString) " " + ctx.getString(R.string.yard) else " $FORMAT_YARDS_KEY" //$NON-NLS-1$
} }
return (meters + 0.5).toInt().toString() + if (useLocalizedString) " " + ctx.getString(R.string.m) else " m" //$NON-NLS-1$ return (meters + 0.5).toInt().toString() + if (useLocalizedString) " " + ctx.getString(R.string.m) else " $FORMAT_METERS_KEY" //$NON-NLS-1$
} }
} }
@ -220,9 +228,9 @@ object OsmandFormatter {
val mc = ctx.settings.metricsConstants val mc = ctx.settings.metricsConstants
val useFeet = mc == MetricsConstants.MILES_AND_FEET || mc == MetricsConstants.MILES_AND_YARDS val useFeet = mc == MetricsConstants.MILES_AND_FEET || mc == MetricsConstants.MILES_AND_YARDS
return if (!useFeet) { return if (!useFeet) {
(alt + 0.5).toInt().toString() + if (useLocalizedString) " " + ctx.getString(R.string.m) else " m" (alt + 0.5).toInt().toString() + if (useLocalizedString) " " + ctx.getString(R.string.m) else " $FORMAT_METERS_KEY"
} else { } else {
(alt * FEET_IN_ONE_METER + 0.5).toInt().toString() + if (useLocalizedString) " " + ctx.getString(R.string.foot) else " ft" (alt * FEET_IN_ONE_METER + 0.5).toInt().toString() + if (useLocalizedString) " " + ctx.getString(R.string.foot) else " $FORMAT_FEET_KEY"
} }
} }

View file

@ -252,12 +252,7 @@ object OsmandLocationUtils {
} }
s.startsWith(ALTITUDE_PREFIX) -> { s.startsWith(ALTITUDE_PREFIX) -> {
val altStr = s.removePrefix(ALTITUDE_PREFIX) val altStr = s.removePrefix(ALTITUDE_PREFIX)
try { res.altitude = parseDistance(altStr)
val alt = altStr.split(" ").first()
res.altitude = alt.toDouble()
} catch (e: Exception) {
e.printStackTrace()
}
} }
s.startsWith(SPEED_PREFIX) -> { s.startsWith(SPEED_PREFIX) -> {
val speedStr = s.removePrefix(SPEED_PREFIX) val speedStr = s.removePrefix(SPEED_PREFIX)
@ -279,12 +274,7 @@ object OsmandLocationUtils {
} }
s.startsWith(HDOP_PREFIX) -> { s.startsWith(HDOP_PREFIX) -> {
val hdopStr = s.removePrefix(HDOP_PREFIX) val hdopStr = s.removePrefix(HDOP_PREFIX)
try { res.hdop = parseDistance(hdopStr)
val hdop = hdopStr.split(" ").first()
res.hdop = hdop.toDouble()
} catch (e: Exception) {
e.printStackTrace()
}
} }
s.startsWith(UPDATED_PREFIX) -> { s.startsWith(UPDATED_PREFIX) -> {
if (res.lastUpdated == 0) { if (res.lastUpdated == 0) {
@ -345,6 +335,25 @@ object OsmandLocationUtils {
return 0 return 0
} }
fun parseDistance(distanceS: String): Double {
try {
val distanceSplit = distanceS.split(" ")
val distanceVal = distanceSplit.first().toDouble()
return when (distanceSplit.last()) {
OsmandFormatter.FORMAT_METERS_KEY -> return distanceVal
OsmandFormatter.FORMAT_FEET_KEY -> return distanceVal / OsmandFormatter.FEET_IN_ONE_METER
OsmandFormatter.FORMAT_YARDS_KEY -> return distanceVal / OsmandFormatter.YARDS_IN_ONE_METER
OsmandFormatter.FORMAT_KILOMETERS_KEY -> return distanceVal * OsmandFormatter.METERS_IN_KILOMETER
OsmandFormatter.FORMAT_NAUTICALMILES_KEY -> return distanceVal * OsmandFormatter.METERS_IN_ONE_NAUTICALMILE
OsmandFormatter.FORMAT_MILES_KEY -> return distanceVal * OsmandFormatter.METERS_IN_ONE_MILE
else -> distanceVal
}
} catch (e: Exception) {
e.printStackTrace()
}
return 0.0
}
fun getTextMessageContent(updateId: Int, location: BufferMessage, app: TelegramApplication): TdApi.InputMessageText { fun getTextMessageContent(updateId: Int, location: BufferMessage, app: TelegramApplication): TdApi.InputMessageText {
val entities = mutableListOf<TdApi.TextEntity>() val entities = mutableListOf<TdApi.TextEntity>()
val builder = StringBuilder() val builder = StringBuilder()