From 50e736f5a2e381cd5f6855f4a04b1bda285b2b84 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 3 Aug 2018 12:21:23 +0300 Subject: [PATCH 1/6] parse new map locations --- .../src/net/osmand/telegram/helpers/ShowLocationHelper.kt | 3 +++ .../src/net/osmand/telegram/helpers/TelegramHelper.kt | 7 +++++++ .../src/net/osmand/telegram/helpers/TelegramUiHelper.kt | 3 +++ 3 files changed, 13 insertions(+) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 8c82b8665f..043edbc6fc 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -79,6 +79,9 @@ class ShowLocationHelper(private val app: TelegramApplication) { if (userName.isEmpty()) { userName = user.phoneNumber } + if (telegramHelper.isOsmAndBot(user.id)) { + userName = telegramHelper.getOsmAndBotDeviceName(message) + } photoPath = telegramHelper.getUserPhotoPath(user) } if (userName.isEmpty()) { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 3cd053c849..f2b53239e2 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -253,6 +253,13 @@ class TelegramHelper private constructor() { } } + fun getOsmAndBotDeviceName(message: TdApi.Message): String { + val replyMarkup = + message.replyMarkup as TdApi.ReplyMarkupInlineKeyboard + val deviceName = replyMarkup.rows[0][1].text + return deviceName.split("\\s".toRegex())[1] + } + fun isOsmAndBot(userId: Int) = users[userId]?.username == OSMAND_BOT_USERNAME fun isBot(userId: Int) = users[userId]?.type is TdApi.UserTypeBot diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt index 7837d145b3..96e9d28f30 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt @@ -131,6 +131,9 @@ object TelegramUiHelper { if (name.isEmpty()) { name = user.phoneNumber } + if (helper.isOsmAndBot(user.id)) { + name = helper.getOsmAndBotDeviceName(message) + } latLon = LatLon(content.location.latitude, content.location.longitude) photoPath = helper.getUserPhotoPath(user) placeholderId = R.drawable.img_user_picture From 6e473384c149ecbaabdfa7940566cd1b94eeb5dc Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 3 Aug 2018 13:39:03 +0300 Subject: [PATCH 2/6] convert OsmAnd bot location messages to MessageOsmAndBotLocation --- .../telegram/helpers/ShowLocationHelper.kt | 3 --- .../osmand/telegram/helpers/TelegramHelper.kt | 20 +++++++++++++++++++ .../telegram/helpers/TelegramUiHelper.kt | 3 --- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 043edbc6fc..8c82b8665f 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -79,9 +79,6 @@ class ShowLocationHelper(private val app: TelegramApplication) { if (userName.isEmpty()) { userName = user.phoneNumber } - if (telegramHelper.isOsmAndBot(user.id)) { - userName = telegramHelper.getOsmAndBotDeviceName(message) - } photoPath = telegramHelper.getUserPhotoPath(user) } if (userName.isEmpty()) { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index f2b53239e2..9786ef65b0 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -391,6 +391,8 @@ class TelegramHelper private constructor() { val oldContent = message.content if (oldContent is TdApi.MessageText) { message.content = parseOsmAndBotLocation(oldContent.text.text) + } else if (oldContent is TdApi.MessageLocation && isOsmAndBot(message.senderUserId)) { + message.content = parseOsmAndBotLocation(message) } removeOldMessages(message.senderUserId, message.chatId) usersLocationMessages[message.id] = message @@ -663,6 +665,22 @@ class TelegramHelper private constructor() { ) } + private fun parseOsmAndBotLocation(message: TdApi.Message): MessageOsmAndBotLocation { + val messageLocation = message.content as TdApi.MessageLocation + return MessageOsmAndBotLocation( + getOsmAndBotDeviceName(message), + messageLocation.location.latitude, + messageLocation.location.longitude, + -1.0, + -1.0, + -1.0, + -1, + -1, + -1, + -1 + ) + } + class MessageOsmAndBotLocation internal constructor( val name: String, val lat: Double, @@ -888,6 +906,8 @@ class TelegramHelper private constructor() { val newContent = updateMessageContent.newContent message.content = if (newContent is TdApi.MessageText) { parseOsmAndBotLocation(newContent.text.text) + } else if (newContent is TdApi.MessageLocation && isOsmAndBot(message.senderUserId)) { + parseOsmAndBotLocation(message) } else { newContent } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt index 96e9d28f30..7837d145b3 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt @@ -131,9 +131,6 @@ object TelegramUiHelper { if (name.isEmpty()) { name = user.phoneNumber } - if (helper.isOsmAndBot(user.id)) { - name = helper.getOsmAndBotDeviceName(message) - } latLon = LatLon(content.location.latitude, content.location.longitude) photoPath = helper.getUserPhotoPath(user) placeholderId = R.drawable.img_user_picture From c65868f522a1e69eeefdf0779da2c0db7f90c033 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 3 Aug 2018 13:50:57 +0300 Subject: [PATCH 3/6] Merge branch 'master' of ssh://github.com/osmandapp/Osmand into TelegramUiImprovements # Conflicts: # OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt --- .../src/net/osmand/telegram/helpers/TelegramHelper.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index b96052321d..8044085dcc 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -643,6 +643,15 @@ class TelegramHelper private constructor() { } } + private fun parseOsmAndBotLocation(message: TdApi.Message): MessageOsmAndBotLocation { + val messageLocation = message.content as TdApi.MessageLocation + val res = MessageOsmAndBotLocation() + res.name = getOsmAndBotDeviceName(message) + res.lat = messageLocation.location.latitude + res.lon = messageLocation.location.longitude + return res + } + private fun parseOsmAndBotLocation(text: String): MessageOsmAndBotLocation { val res = MessageOsmAndBotLocation() for (s in text.lines()) { From a12f2eca5acdff96e4286c3d1ae4e9d46dceb717 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 3 Aug 2018 14:35:49 +0300 Subject: [PATCH 4/6] remove unnecessary check for OsmAnd bot --- .../src/net/osmand/telegram/helpers/TelegramHelper.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 8044085dcc..b39b64b29b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -256,8 +256,7 @@ class TelegramHelper private constructor() { } fun getOsmAndBotDeviceName(message: TdApi.Message): String { - val replyMarkup = - message.replyMarkup as TdApi.ReplyMarkupInlineKeyboard + val replyMarkup = message.replyMarkup as TdApi.ReplyMarkupInlineKeyboard val deviceName = replyMarkup.rows[0][1].text return deviceName.split("\\s".toRegex())[1] } @@ -393,7 +392,7 @@ class TelegramHelper private constructor() { val oldContent = message.content if (oldContent is TdApi.MessageText) { message.content = parseOsmAndBotLocation(oldContent.text.text) - } else if (oldContent is TdApi.MessageLocation && isOsmAndBot(message.senderUserId)) { + } else if (oldContent is TdApi.MessageLocation) { message.content = parseOsmAndBotLocation(message) } removeOldMessages(message.senderUserId, message.chatId) From 87879559bb4055e4a2074c5ed455f015bea570e6 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 3 Aug 2018 14:42:01 +0300 Subject: [PATCH 5/6] add check for Osmand bot --- .../src/net/osmand/telegram/helpers/TelegramHelper.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index b39b64b29b..fdeb6c7461 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -392,7 +392,8 @@ class TelegramHelper private constructor() { val oldContent = message.content if (oldContent is TdApi.MessageText) { message.content = parseOsmAndBotLocation(oldContent.text.text) - } else if (oldContent is TdApi.MessageLocation) { + } else if (oldContent is TdApi.MessageLocation && + (isOsmAndBot(message.senderUserId) || isOsmAndBot(message.viaBotUserId))) { message.content = parseOsmAndBotLocation(message) } removeOldMessages(message.senderUserId, message.chatId) From 37c8d2b9a01e3f58dde4543fdf6592c05c3724da Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 3 Aug 2018 14:55:07 +0300 Subject: [PATCH 6/6] add checks in getOsmAndBotDeviceName --- .../osmand/telegram/helpers/TelegramHelper.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index fdeb6c7461..a9a99d5def 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -256,9 +256,16 @@ class TelegramHelper private constructor() { } fun getOsmAndBotDeviceName(message: TdApi.Message): String { - val replyMarkup = message.replyMarkup as TdApi.ReplyMarkupInlineKeyboard - val deviceName = replyMarkup.rows[0][1].text - return deviceName.split("\\s".toRegex())[1] + var deviceName = "" + if (message.replyMarkup is TdApi.ReplyMarkupInlineKeyboard) { + val replyMarkup = message.replyMarkup as TdApi.ReplyMarkupInlineKeyboard + try { + deviceName = replyMarkup.rows[0][1].text.split("\\s".toRegex())[1] + } catch (e: Exception) { + + } + } + return deviceName } fun isOsmAndBot(userId: Int) = users[userId]?.username == OSMAND_BOT_USERNAME @@ -895,7 +902,8 @@ class TelegramHelper private constructor() { val newContent = updateMessageContent.newContent message.content = if (newContent is TdApi.MessageText) { parseOsmAndBotLocation(newContent.text.text) - } else if (newContent is TdApi.MessageLocation && isOsmAndBot(message.senderUserId)) { + } else if (newContent is TdApi.MessageLocation && + (isOsmAndBot(message.senderUserId) || isOsmAndBot(message.viaBotUserId))) { parseOsmAndBotLocation(message) } else { newContent