From b15463488eff798ae074d6a62590eb5bdb7d3ad2 Mon Sep 17 00:00:00 2001 From: Chumva Date: Mon, 9 Dec 2019 17:33:20 +0200 Subject: [PATCH] Format utc time in text locations --- .../net/osmand/telegram/TelegramSettings.kt | 5 +- .../osmand/telegram/utils/DataConstants.kt | 80 +++++++++---------- .../telegram/utils/OsmandLocationUtils.kt | 59 +++----------- 3 files changed, 55 insertions(+), 89 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index f22650f63b..211abe0bf4 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -1077,14 +1077,15 @@ class TelegramSettings(private val app: TelegramApplication) { R.drawable.ic_world_globe_dark, R.string.time_zone, R.string.time_zone_descr ) { + private val formattedUtcOffsets = DataConstants.utcOffsets.keys.toList() override fun getCurrentValue() = utcOffset override fun setCurrentValue(index: Int) { - utcOffset = DataConstants.utcOffsets[index] + utcOffset = formattedUtcOffsets[index] } - override fun getMenuItems() = DataConstants.utcOffsets + override fun getMenuItems() = formattedUtcOffsets } abstract inner class ListPreference( diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/DataConstants.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/DataConstants.kt index 2b94978e9a..759dc9e2f1 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/DataConstants.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/DataConstants.kt @@ -47,45 +47,45 @@ object DataConstants { "UK" to "+44", "US" to "+1","UY" to "+598", "VI" to "+1340", "UZ" to "+998", "VU" to "+678", "VE" to "+58", "VA" to "+3906698,+379", "VN" to "+84", "WF" to "+681","YE" to "+967", "ZM" to "+260", "ZW" to "+263") - val utcOffsets = listOf( - "$UTC_FORMAT−12", - "$UTC_FORMAT−11", - "$UTC_FORMAT−10", - "$UTC_FORMAT−09:30", - "$UTC_FORMAT−9", - "$UTC_FORMAT−8", - "$UTC_FORMAT−7", - "$UTC_FORMAT−6", - "$UTC_FORMAT−5", - "$UTC_FORMAT−4", - "$UTC_FORMAT−03:30", - "$UTC_FORMAT−3", - "$UTC_FORMAT−2", - "$UTC_FORMAT−1", - UTC_FORMAT, - "$UTC_FORMAT+1", - "$UTC_FORMAT+2", - "$UTC_FORMAT+3", - "$UTC_FORMAT+03:30", - "$UTC_FORMAT+4", - "$UTC_FORMAT+04:30", - "$UTC_FORMAT+5", - "$UTC_FORMAT+05:30", - "$UTC_FORMAT+05:45", - "$UTC_FORMAT+6", - "$UTC_FORMAT+06:30", - "$UTC_FORMAT+7", - "$UTC_FORMAT+8", - "$UTC_FORMAT+08:45", - "$UTC_FORMAT+9", - "$UTC_FORMAT+09:30", - "$UTC_FORMAT+10", - "$UTC_FORMAT+10:30", - "$UTC_FORMAT+11", - "$UTC_FORMAT+12", - "$UTC_FORMAT+12:45", - "$UTC_FORMAT+13", - "$UTC_FORMAT+13:45", - "$UTC_FORMAT+14" + val utcOffsets = mapOf( + "$UTC_FORMAT-12" to -12f, + "$UTC_FORMAT-11" to -11f, + "$UTC_FORMAT-10" to -10f, + "$UTC_FORMAT-09:30" to -9.5f, + "$UTC_FORMAT-9" to -9f, + "$UTC_FORMAT-8" to -8f, + "$UTC_FORMAT-7" to -7f, + "$UTC_FORMAT-6" to -6f, + "$UTC_FORMAT-5" to -5f, + "$UTC_FORMAT-4" to -4f, + "$UTC_FORMAT-03:30" to -3.5f, + "$UTC_FORMAT-3" to -3f, + "$UTC_FORMAT-2" to -2f, + "$UTC_FORMAT-1" to -1f, + UTC_FORMAT to 0f, + "$UTC_FORMAT+1" to 1f, + "$UTC_FORMAT+2" to 2f, + "$UTC_FORMAT+3" to 3f, + "$UTC_FORMAT+03:30" to 3.5f, + "$UTC_FORMAT+4" to 4f, + "$UTC_FORMAT+04:30" to 4.5f, + "$UTC_FORMAT+5" to 5f, + "$UTC_FORMAT+05:30" to 5.5f, + "$UTC_FORMAT+05:45" to 5.75f, + "$UTC_FORMAT+6" to 6f, + "$UTC_FORMAT+06:30" to 6.5f, + "$UTC_FORMAT+7" to 7f, + "$UTC_FORMAT+8" to 8f, + "$UTC_FORMAT+08:45" to 8.75f, + "$UTC_FORMAT+9" to 9f, + "$UTC_FORMAT+09:30" to 9.5f, + "$UTC_FORMAT+10" to 10f, + "$UTC_FORMAT+10:30" to 10.5f, + "$UTC_FORMAT+11" to 11f, + "$UTC_FORMAT+12" to 12f, + "$UTC_FORMAT+12:45" to 12.75f, + "$UTC_FORMAT+13" to 13f, + "$UTC_FORMAT+13:45" to 13.75f, + "$UTC_FORMAT+14" to 14f ) } \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt index e2609eac1f..40e49caf4f 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt @@ -163,9 +163,17 @@ object OsmandLocationUtils { return String.format(Locale.US, "%.5f, %.5f", sig.lat, sig.lon) } - fun formatFullTime(ti: Long): String { + fun formatFullTime(ti: Long, app: TelegramApplication): String { val dt = Date(ti) - return UTC_DATE_FORMAT.format(dt) + " " + UTC_TIME_FORMAT.format(dt) + UTC_FORMAT_SUFFIX + val offsetKey = app.settings.utcOffset + val utcOffset = DataConstants.utcOffsets[offsetKey] ?: 0f + val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US) + + simpleDateFormat.timeZone = TimeZone.getTimeZone(DataConstants.UTC_FORMAT).apply { + rawOffset = (utcOffset * 60 * 60 * 1000).toInt() + } + + return "${simpleDateFormat.format(dt)} $offsetKey" } fun parseOsmAndBotLocationContent(oldContent: MessageOsmAndBotLocation, content: TdApi.MessageContent): MessageOsmAndBotLocation { @@ -329,49 +337,6 @@ object OsmandLocationUtils { return 0 } - fun getTextMessageContent(updateId: Int, location: LocationMessage): TdApi.InputMessageText { - val entities = mutableListOf() - val builder = StringBuilder() - val locationMessage = formatLocation(location) - - val firstSpace = USER_TEXT_LOCATION_TITLE.indexOf(' ') - val secondSpace = USER_TEXT_LOCATION_TITLE.indexOf(' ', firstSpace + 1) - entities.add(TdApi.TextEntity(builder.length + firstSpace + 1, secondSpace - firstSpace, TdApi.TextEntityTypeTextUrl(SHARING_LINK))) - builder.append("$USER_TEXT_LOCATION_TITLE\n") - - entities.add(TdApi.TextEntity(builder.lastIndex, LOCATION_PREFIX.length, TdApi.TextEntityTypeBold())) - builder.append(LOCATION_PREFIX) - - entities.add(TdApi.TextEntity(builder.length, locationMessage.length, - TdApi.TextEntityTypeTextUrl("$BASE_SHARING_URL?lat=${location.lat}&lon=${location.lon}"))) - builder.append("$locationMessage\n") - - if (location.altitude != 0.0) { - entities.add(TdApi.TextEntity(builder.lastIndex, ALTITUDE_PREFIX.length, TdApi.TextEntityTypeBold())) - builder.append(String.format(Locale.US, "$ALTITUDE_PREFIX%.1f m\n", location.altitude)) - } - if (location.speed > 0) { - entities.add(TdApi.TextEntity(builder.lastIndex, SPEED_PREFIX.length, TdApi.TextEntityTypeBold())) - builder.append(String.format(Locale.US, "$SPEED_PREFIX%.1f m/s\n", location.speed)) - } - if (location.bearing > 0) { - entities.add(TdApi.TextEntity(builder.lastIndex, BEARING_PREFIX.length, TdApi.TextEntityTypeBold())) - builder.append(String.format(Locale.US, "$BEARING_PREFIX%.1f$BEARING_SUFFIX\n", location.bearing)) - } - if (location.hdop != 0.0 && location.speed == 0.0) { - entities.add(TdApi.TextEntity(builder.lastIndex, HDOP_PREFIX.length, TdApi.TextEntityTypeBold())) - builder.append(String.format(Locale.US, "$HDOP_PREFIX%d m\n", location.hdop.toInt())) - } - if (updateId == 0) { - builder.append(String.format("$UPDATED_PREFIX%s\n", formatFullTime(location.time))) - } else { - builder.append(String.format("$UPDATED_PREFIX%s (%d)\n", formatFullTime(location.time), updateId)) - } - val textMessage = builder.toString().trim() - - return TdApi.InputMessageText(TdApi.FormattedText(textMessage, entities.toTypedArray()), true, true) - } - fun getTextMessageContent(updateId: Int, location: BufferMessage, app: TelegramApplication): TdApi.InputMessageText { val entities = mutableListOf() val builder = StringBuilder() @@ -409,9 +374,9 @@ object OsmandLocationUtils { builder.append(String.format(Locale.US, "$HDOP_PREFIX%s\n", formattedHdop)) } if (updateId == 0) { - builder.append(String.format("$UPDATED_PREFIX%s\n", formatFullTime(location.time))) + builder.append(String.format("$UPDATED_PREFIX%s\n", formatFullTime(location.time, app))) } else { - builder.append(String.format("$UPDATED_PREFIX%s (%d)\n", formatFullTime(location.time), updateId)) + builder.append(String.format("$UPDATED_PREFIX%s (%d)\n", formatFullTime(location.time, app), updateId)) } val textMessage = builder.toString().trim()