Merge pull request #6552 from osmandapp/TelegramImprovements
Fix sharing status and TdLib buffer
This commit is contained in:
commit
f727b7ad6c
5 changed files with 91 additions and 71 deletions
|
@ -204,13 +204,27 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
|
|
||||||
fun prepareForSharingNewMessages() {
|
fun prepareForSharingNewMessages() {
|
||||||
shareChatsInfo.forEach { (_, shareInfo) ->
|
shareChatsInfo.forEach { (_, shareInfo) ->
|
||||||
shareInfo.pendingTdLibText = 0
|
prepareForSharingNewMessages(shareInfo)
|
||||||
shareInfo.pendingTdLibMap = 0
|
|
||||||
shareInfo.pendingTextMessage = false
|
|
||||||
shareInfo.pendingMapMessage = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun prepareForSharingNewMessages(chatsIds: List<Long>) {
|
||||||
|
chatsIds.forEach {
|
||||||
|
shareChatsInfo[it]?.also { shareInfo ->
|
||||||
|
prepareForSharingNewMessages(shareInfo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun prepareForSharingNewMessages(shareInfo: ShareChatInfo) {
|
||||||
|
shareInfo.pendingTdLibText = 0
|
||||||
|
shareInfo.pendingTdLibMap = 0
|
||||||
|
shareInfo.currentTextMessageId = -1L
|
||||||
|
shareInfo.currentMapMessageId = -1L
|
||||||
|
shareInfo.pendingTextMessage = false
|
||||||
|
shareInfo.pendingMapMessage = false
|
||||||
|
}
|
||||||
|
|
||||||
fun getChatLivePeriod(chatId: Long) = shareChatsInfo[chatId]?.livePeriod
|
fun getChatLivePeriod(chatId: Long) = shareChatsInfo[chatId]?.livePeriod
|
||||||
|
|
||||||
fun getChatsShareInfo() = shareChatsInfo
|
fun getChatsShareInfo() = shareChatsInfo
|
||||||
|
@ -228,7 +242,11 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
|
|
||||||
fun getCurrentSharingDevice() = shareDevices.singleOrNull { it.externalId == currentSharingMode }
|
fun getCurrentSharingDevice() = shareDevices.singleOrNull { it.externalId == currentSharingMode }
|
||||||
|
|
||||||
fun getLastSuccessfulSendTime() = shareChatsInfo.values.maxBy { it.lastSuccessfulSendTime }?.lastSuccessfulSendTime ?: -1
|
fun getLastSuccessfulSendTime(): Long {
|
||||||
|
val lastSuccessTextSend = shareChatsInfo.values.maxBy { it.lastTextSuccessfulSendTime }?.lastTextSuccessfulSendTime ?: -1
|
||||||
|
val lastSuccessMapSend = shareChatsInfo.values.maxBy { it.lastMapSuccessfulSendTime }?.lastMapSuccessfulSendTime ?: -1
|
||||||
|
return Math.max(lastSuccessTextSend, lastSuccessMapSend)
|
||||||
|
}
|
||||||
|
|
||||||
fun stopSharingLocationToChats() {
|
fun stopSharingLocationToChats() {
|
||||||
shareChatsInfo.clear()
|
shareChatsInfo.clear()
|
||||||
|
@ -285,7 +303,7 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
} else {
|
} else {
|
||||||
shareInfo.currentMapMessageId = message.id
|
shareInfo.currentMapMessageId = message.id
|
||||||
shareInfo.pendingMapMessage = false
|
shareInfo.pendingMapMessage = false
|
||||||
shareInfo.lastSuccessfulSendTime = Math.max(message.editDate, message.date).toLong()
|
shareInfo.lastMapSuccessfulSendTime = System.currentTimeMillis() / 1000
|
||||||
if (!isOsmAndBot) {
|
if (!isOsmAndBot) {
|
||||||
shareInfo.pendingTdLibMap--
|
shareInfo.pendingTdLibMap--
|
||||||
if (shareTypeValue == SHARE_TYPE_MAP) {
|
if (shareTypeValue == SHARE_TYPE_MAP) {
|
||||||
|
@ -316,7 +334,7 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
shareInfo.currentTextMessageId = message.id
|
shareInfo.currentTextMessageId = message.id
|
||||||
shareInfo.updateTextMessageId++
|
shareInfo.updateTextMessageId++
|
||||||
shareInfo.pendingTextMessage = false
|
shareInfo.pendingTextMessage = false
|
||||||
shareInfo.lastSuccessfulSendTime = Math.max(message.editDate, message.date).toLong()
|
shareInfo.lastTextSuccessfulSendTime = System.currentTimeMillis() / 1000
|
||||||
if (!isOsmAndBot) {
|
if (!isOsmAndBot) {
|
||||||
shareInfo.pendingTdLibText--
|
shareInfo.pendingTdLibText--
|
||||||
shareInfo.sentMessages++
|
shareInfo.sentMessages++
|
||||||
|
@ -341,7 +359,7 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
lastSharingStatus.apply {
|
lastSharingStatus.apply {
|
||||||
statusChangeTime = newSharingStatus.statusChangeTime
|
statusChangeTime = newSharingStatus.statusChangeTime
|
||||||
locationTime = newSharingStatus.locationTime
|
locationTime = newSharingStatus.locationTime
|
||||||
chatsTitles = newSharingStatus.chatsTitles
|
chatsIds = newSharingStatus.chatsIds
|
||||||
title = newSharingStatus.title
|
title = newSharingStatus.title
|
||||||
|
|
||||||
if (statusType == SharingStatusType.INITIALIZING
|
if (statusType == SharingStatusType.INITIALIZING
|
||||||
|
@ -406,18 +424,16 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
var sendChatsErrors = false
|
var sendChatsErrors = false
|
||||||
|
|
||||||
shareChatsInfo.forEach { (_, shareInfo) ->
|
shareChatsInfo.forEach { (_, shareInfo) ->
|
||||||
if (shareInfo.lastSuccessfulSendTime == -1L && ((statusChangeTime / 1000 - shareInfo.start) < SHARING_INITIALIZATION_TIME)) {
|
if (shareInfo.lastTextSuccessfulSendTime == -1L && shareInfo.lastMapSuccessfulSendTime == -1L
|
||||||
|
&& ((statusChangeTime / 1000 - shareInfo.start) < SHARING_INITIALIZATION_TIME)) {
|
||||||
initializing = true
|
initializing = true
|
||||||
} else if (shareInfo.hasSharingError
|
} else if (shareInfo.hasSharingError
|
||||||
|| (shareInfo.lastSuccessfulSendTime - shareInfo.lastSendTextMessageTime > WAITING_TDLIB_TIME)
|
|| (shareInfo.lastSendTextMessageTime - shareInfo.lastTextSuccessfulSendTime > WAITING_TDLIB_TIME)
|
||||||
|| (shareInfo.lastSuccessfulSendTime - shareInfo.lastSendMapMessageTime > WAITING_TDLIB_TIME)
|
|| (shareInfo.lastSendMapMessageTime - shareInfo.lastMapSuccessfulSendTime > WAITING_TDLIB_TIME)
|
||||||
) {
|
) {
|
||||||
sendChatsErrors = true
|
sendChatsErrors = true
|
||||||
locationTime = shareInfo.lastSuccessfulSendTime
|
locationTime = Math.max(shareInfo.lastTextSuccessfulSendTime, shareInfo.lastMapSuccessfulSendTime)
|
||||||
val title = app.telegramHelper.getChat(shareInfo.chatId)?.title
|
chatsIds.add(shareInfo.chatId)
|
||||||
if (title != null) {
|
|
||||||
chatsTitles.add(title)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -567,7 +583,8 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
val shareTypeDef = SHARE_TYPE_VALUES[SHARE_TYPE_DEFAULT_INDEX]
|
val shareTypeDef = SHARE_TYPE_VALUES[SHARE_TYPE_DEFAULT_INDEX]
|
||||||
shareTypeValue = prefs.getString(SHARE_TYPE_KEY, shareTypeDef)
|
shareTypeValue = prefs.getString(SHARE_TYPE_KEY, shareTypeDef)
|
||||||
|
|
||||||
currentSharingMode = prefs.getString(SHARING_MODE_KEY, "")
|
val currentUserId = app.telegramHelper.getCurrentUserId()
|
||||||
|
currentSharingMode = prefs.getString(SHARING_MODE_KEY, if (currentUserId != -1) currentUserId.toString() else "")
|
||||||
|
|
||||||
val defPackage = if (AppConnect.getInstalledApps(app).size == 1) AppConnect.getInstalledApps(app).first().appPackage else ""
|
val defPackage = if (AppConnect.getInstalledApps(app).size == 1) AppConnect.getInstalledApps(app).first().appPackage else ""
|
||||||
appToConnectPackage = prefs.getString(APP_TO_CONNECT_PACKAGE_KEY, defPackage)
|
appToConnectPackage = prefs.getString(APP_TO_CONNECT_PACKAGE_KEY, defPackage)
|
||||||
|
@ -619,7 +636,8 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
obj.put(ShareChatInfo.CURRENT_TEXT_MESSAGE_ID_KEY, chatInfo.currentTextMessageId)
|
obj.put(ShareChatInfo.CURRENT_TEXT_MESSAGE_ID_KEY, chatInfo.currentTextMessageId)
|
||||||
obj.put(ShareChatInfo.USER_SET_LIVE_PERIOD_KEY, chatInfo.userSetLivePeriod)
|
obj.put(ShareChatInfo.USER_SET_LIVE_PERIOD_KEY, chatInfo.userSetLivePeriod)
|
||||||
obj.put(ShareChatInfo.USER_SET_LIVE_PERIOD_START_KEY, chatInfo.userSetLivePeriodStart)
|
obj.put(ShareChatInfo.USER_SET_LIVE_PERIOD_START_KEY, chatInfo.userSetLivePeriodStart)
|
||||||
obj.put(ShareChatInfo.LAST_SUCCESSFUL_SEND_TIME_KEY, chatInfo.lastSuccessfulSendTime)
|
obj.put(ShareChatInfo.LAST_TEXT_SUCCESSFUL_SEND_TIME_KEY, chatInfo.lastTextSuccessfulSendTime)
|
||||||
|
obj.put(ShareChatInfo.LAST_MAP_SUCCESSFUL_SEND_TIME_KEY, chatInfo.lastMapSuccessfulSendTime)
|
||||||
obj.put(ShareChatInfo.LAST_SEND_MAP_TIME_KEY, chatInfo.lastSendMapMessageTime)
|
obj.put(ShareChatInfo.LAST_SEND_MAP_TIME_KEY, chatInfo.lastSendMapMessageTime)
|
||||||
obj.put(ShareChatInfo.LAST_SEND_TEXT_TIME_KEY, chatInfo.lastSendTextMessageTime)
|
obj.put(ShareChatInfo.LAST_SEND_TEXT_TIME_KEY, chatInfo.lastSendTextMessageTime)
|
||||||
obj.put(ShareChatInfo.PENDING_TEXT_MESSAGE_KEY, chatInfo.pendingTextMessage)
|
obj.put(ShareChatInfo.PENDING_TEXT_MESSAGE_KEY, chatInfo.pendingTextMessage)
|
||||||
|
@ -648,7 +666,8 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
currentTextMessageId = obj.optLong(ShareChatInfo.CURRENT_TEXT_MESSAGE_ID_KEY)
|
currentTextMessageId = obj.optLong(ShareChatInfo.CURRENT_TEXT_MESSAGE_ID_KEY)
|
||||||
userSetLivePeriod = obj.optLong(ShareChatInfo.USER_SET_LIVE_PERIOD_KEY)
|
userSetLivePeriod = obj.optLong(ShareChatInfo.USER_SET_LIVE_PERIOD_KEY)
|
||||||
userSetLivePeriodStart = obj.optLong(ShareChatInfo.USER_SET_LIVE_PERIOD_START_KEY)
|
userSetLivePeriodStart = obj.optLong(ShareChatInfo.USER_SET_LIVE_PERIOD_START_KEY)
|
||||||
lastSuccessfulSendTime = obj.optLong(ShareChatInfo.LAST_SUCCESSFUL_SEND_TIME_KEY)
|
lastTextSuccessfulSendTime = obj.optLong(ShareChatInfo.LAST_TEXT_SUCCESSFUL_SEND_TIME_KEY)
|
||||||
|
lastMapSuccessfulSendTime = obj.optLong(ShareChatInfo.LAST_MAP_SUCCESSFUL_SEND_TIME_KEY)
|
||||||
lastSendMapMessageTime = obj.optInt(ShareChatInfo.LAST_SEND_MAP_TIME_KEY)
|
lastSendMapMessageTime = obj.optInt(ShareChatInfo.LAST_SEND_MAP_TIME_KEY)
|
||||||
lastSendTextMessageTime = obj.optInt(ShareChatInfo.LAST_SEND_TEXT_TIME_KEY)
|
lastSendTextMessageTime = obj.optInt(ShareChatInfo.LAST_SEND_TEXT_TIME_KEY)
|
||||||
pendingTextMessage = obj.optBoolean(ShareChatInfo.PENDING_TEXT_MESSAGE_KEY)
|
pendingTextMessage = obj.optBoolean(ShareChatInfo.PENDING_TEXT_MESSAGE_KEY)
|
||||||
|
@ -913,22 +932,25 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
var description: String = ""
|
var description: String = ""
|
||||||
var locationTime: Long = -1
|
var locationTime: Long = -1
|
||||||
var statusChangeTime: Long = -1
|
var statusChangeTime: Long = -1
|
||||||
var chatsTitles: MutableList<String> = mutableListOf()
|
var chatsIds: MutableList<Long> = mutableListOf()
|
||||||
|
|
||||||
lateinit var statusType: SharingStatusType
|
lateinit var statusType: SharingStatusType
|
||||||
|
|
||||||
fun getTitle(app: TelegramApplication): CharSequence {
|
fun getTitle(app: TelegramApplication): CharSequence {
|
||||||
return if (statusType != SharingStatusType.NOT_POSSIBLE_TO_SENT_TO_CHATS || chatsTitles.isEmpty()) {
|
return if (statusType != SharingStatusType.NOT_POSSIBLE_TO_SENT_TO_CHATS || chatsIds.isEmpty()) {
|
||||||
title
|
title
|
||||||
} else {
|
} else {
|
||||||
val spannableString = SpannableStringBuilder(title)
|
val spannableString = SpannableStringBuilder(title)
|
||||||
val iterator = chatsTitles.iterator()
|
val iterator = chatsIds.iterator()
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
val chatTitle = iterator.next()
|
val chatId = iterator.next()
|
||||||
val start = spannableString.length
|
val chatTitle = app.telegramHelper.getChat(chatId)?.title
|
||||||
val newSpannable = if (iterator.hasNext()) " @$chatTitle," else " @$chatTitle."
|
if (chatTitle != null) {
|
||||||
spannableString.append(newSpannable)
|
val start = spannableString.length
|
||||||
spannableString.setSpan(ForegroundColorSpan(app.uiUtils.getActiveColor()), start, spannableString.length - 1, 0)
|
val newSpannable = if (iterator.hasNext()) " @$chatTitle," else " @$chatTitle."
|
||||||
|
spannableString.append(newSpannable)
|
||||||
|
spannableString.setSpan(ForegroundColorSpan(app.uiUtils.getActiveColor()), start, spannableString.length - 1, 0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
spannableString
|
spannableString
|
||||||
}
|
}
|
||||||
|
@ -949,7 +971,8 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
var oldTextMessageId = -1L
|
var oldTextMessageId = -1L
|
||||||
var userSetLivePeriod = -1L
|
var userSetLivePeriod = -1L
|
||||||
var userSetLivePeriodStart = -1L
|
var userSetLivePeriodStart = -1L
|
||||||
var lastSuccessfulSendTime = -1L
|
var lastTextSuccessfulSendTime = -1L
|
||||||
|
var lastMapSuccessfulSendTime = -1L
|
||||||
var lastSendTextMessageTime = -1
|
var lastSendTextMessageTime = -1
|
||||||
var lastSendMapMessageTime = -1
|
var lastSendMapMessageTime = -1
|
||||||
var sentMessages = 0
|
var sentMessages = 0
|
||||||
|
@ -960,12 +983,8 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
var shouldSendViaBotTextMessage = false
|
var shouldSendViaBotTextMessage = false
|
||||||
var shouldSendViaBotMapMessage = false
|
var shouldSendViaBotMapMessage = false
|
||||||
var hasSharingError = false
|
var hasSharingError = false
|
||||||
var shouldDeletePreviousMapMessage = false
|
|
||||||
var shouldDeletePreviousTextMessage = false
|
|
||||||
var additionalActiveTime = ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0]
|
var additionalActiveTime = ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0]
|
||||||
|
|
||||||
fun getPendingTdLib() = pendingTdLibText + pendingTdLibMap
|
|
||||||
|
|
||||||
fun getNextAdditionalActiveTime(): Long {
|
fun getNextAdditionalActiveTime(): Long {
|
||||||
var index = ADDITIONAL_ACTIVE_TIME_VALUES_SEC.indexOf(additionalActiveTime)
|
var index = ADDITIONAL_ACTIVE_TIME_VALUES_SEC.indexOf(additionalActiveTime)
|
||||||
return if (ADDITIONAL_ACTIVE_TIME_VALUES_SEC.lastIndex > index) {
|
return if (ADDITIONAL_ACTIVE_TIME_VALUES_SEC.lastIndex > index) {
|
||||||
|
@ -991,7 +1010,8 @@ class TelegramSettings(private val app: TelegramApplication) {
|
||||||
internal const val CURRENT_TEXT_MESSAGE_ID_KEY = "currentTextMessageId"
|
internal const val CURRENT_TEXT_MESSAGE_ID_KEY = "currentTextMessageId"
|
||||||
internal const val USER_SET_LIVE_PERIOD_KEY = "userSetLivePeriod"
|
internal const val USER_SET_LIVE_PERIOD_KEY = "userSetLivePeriod"
|
||||||
internal const val USER_SET_LIVE_PERIOD_START_KEY = "userSetLivePeriodStart"
|
internal const val USER_SET_LIVE_PERIOD_START_KEY = "userSetLivePeriodStart"
|
||||||
internal const val LAST_SUCCESSFUL_SEND_TIME_KEY = "lastSuccessfulSendTime"
|
internal const val LAST_MAP_SUCCESSFUL_SEND_TIME_KEY = "lastMapSuccessfulSendTime"
|
||||||
|
internal const val LAST_TEXT_SUCCESSFUL_SEND_TIME_KEY = "lastTextSuccessfulSendTime"
|
||||||
internal const val LAST_SEND_MAP_TIME_KEY = "lastSendMapMessageTime"
|
internal const val LAST_SEND_MAP_TIME_KEY = "lastSendMapMessageTime"
|
||||||
internal const val LAST_SEND_TEXT_TIME_KEY = "lastSendTextMessageTime"
|
internal const val LAST_SEND_TEXT_TIME_KEY = "lastSendTextMessageTime"
|
||||||
internal const val PENDING_TEXT_MESSAGE_KEY = "pendingTextMessage"
|
internal const val PENDING_TEXT_MESSAGE_KEY = "pendingTextMessage"
|
||||||
|
|
|
@ -48,6 +48,14 @@ class LocationMessages(val app: TelegramApplication) {
|
||||||
return bufferedMessages.filter { it.chatId == chatId }.sortedBy { it.time }
|
return bufferedMessages.filter { it.chatId == chatId }.sortedBy { it.time }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getBufferedTextMessagesForChat(chatId: Long): List<BufferMessage> {
|
||||||
|
return bufferedMessages.filter { it.chatId == chatId && it.type == TYPE_TEXT }.sortedBy { it.time }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getBufferedMapMessagesForChat(chatId: Long): List<BufferMessage> {
|
||||||
|
return bufferedMessages.filter { it.chatId == chatId && it.type == TYPE_MAP }.sortedBy { it.time }
|
||||||
|
}
|
||||||
|
|
||||||
fun getIngoingMessages(currentUserId: Int, start: Long, end: Long): List<LocationMessage> {
|
fun getIngoingMessages(currentUserId: Int, start: Long, end: Long): List<LocationMessage> {
|
||||||
return dbHelper.getIngoingMessages(currentUserId, start, end)
|
return dbHelper.getIngoingMessages(currentUserId, start, end)
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,16 +76,12 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
||||||
livePeriod
|
livePeriod
|
||||||
}
|
}
|
||||||
livePeriod = newLivePeriod
|
livePeriod = newLivePeriod
|
||||||
shouldDeletePreviousMapMessage = true
|
|
||||||
shouldDeletePreviousTextMessage = true
|
|
||||||
currentMessageLimit = currentTime + Math.min(newLivePeriod, TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong())
|
currentMessageLimit = currentTime + Math.min(newLivePeriod, TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
shareInfo.userSetLivePeriod != shareInfo.livePeriod
|
shareInfo.userSetLivePeriod != shareInfo.livePeriod
|
||||||
&& (shareInfo.userSetLivePeriodStart + USER_SET_LIVE_PERIOD_DELAY_MS) > currentTime -> {
|
&& (shareInfo.userSetLivePeriodStart + USER_SET_LIVE_PERIOD_DELAY_MS) > currentTime -> {
|
||||||
shareInfo.apply {
|
shareInfo.apply {
|
||||||
shouldDeletePreviousMapMessage = true
|
|
||||||
shouldDeletePreviousTextMessage = true
|
|
||||||
livePeriod = shareInfo.userSetLivePeriod
|
livePeriod = shareInfo.userSetLivePeriod
|
||||||
currentMessageLimit = currentTime + Math.min(livePeriod, TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong())
|
currentMessageLimit = currentTime + Math.min(livePeriod, TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong())
|
||||||
}
|
}
|
||||||
|
@ -102,7 +98,7 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
||||||
var bufferedMessagesFull = false
|
var bufferedMessagesFull = false
|
||||||
app.settings.getChatsShareInfo().forEach { (chatId, shareInfo) ->
|
app.settings.getChatsShareInfo().forEach { (chatId, shareInfo) ->
|
||||||
checkAndSendBufferMessagesToChat(chatId)
|
checkAndSendBufferMessagesToChat(chatId)
|
||||||
if (shareInfo.getPendingTdLib() > 10) {
|
if (shareInfo.pendingTdLibText >= MAX_MESSAGES_IN_TDLIB_PER_CHAT || shareInfo.pendingTdLibMap >= MAX_MESSAGES_IN_TDLIB_PER_CHAT) {
|
||||||
bufferedMessagesFull = true
|
bufferedMessagesFull = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,9 +109,9 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
||||||
|
|
||||||
fun checkAndSendBufferMessagesToChat(chatId: Long) {
|
fun checkAndSendBufferMessagesToChat(chatId: Long) {
|
||||||
val shareInfo = app.settings.getChatsShareInfo()[chatId]
|
val shareInfo = app.settings.getChatsShareInfo()[chatId]
|
||||||
if (shareInfo != null && shareInfo.getPendingTdLib() < 10) {
|
if (shareInfo != null) {
|
||||||
app.locationMessages.getBufferedMessagesForChat(chatId).forEach {
|
app.locationMessages.getBufferedTextMessagesForChat(chatId).take(MAX_MESSAGES_IN_TDLIB_PER_CHAT).forEach {
|
||||||
if (it.type == LocationMessages.TYPE_TEXT) {
|
if (shareInfo.pendingTdLibText < MAX_MESSAGES_IN_TDLIB_PER_CHAT) {
|
||||||
if (it.deviceName.isEmpty()) {
|
if (it.deviceName.isEmpty()) {
|
||||||
if (!shareInfo.pendingTextMessage && shareInfo.currentTextMessageId != -1L) {
|
if (!shareInfo.pendingTextMessage && shareInfo.currentTextMessageId != -1L) {
|
||||||
app.telegramHelper.editTextLocation(shareInfo, it)
|
app.telegramHelper.editTextLocation(shareInfo, it)
|
||||||
|
@ -124,7 +120,10 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
||||||
} else {
|
} else {
|
||||||
sendLocationToBot(it, shareInfo, SHARE_TYPE_TEXT)
|
sendLocationToBot(it, shareInfo, SHARE_TYPE_TEXT)
|
||||||
}
|
}
|
||||||
} else if (it.type == LocationMessages.TYPE_MAP) {
|
}
|
||||||
|
}
|
||||||
|
app.locationMessages.getBufferedMapMessagesForChat(chatId).take(MAX_MESSAGES_IN_TDLIB_PER_CHAT).forEach {
|
||||||
|
if (shareInfo.pendingTdLibMap < MAX_MESSAGES_IN_TDLIB_PER_CHAT) {
|
||||||
if (it.deviceName.isEmpty()) {
|
if (it.deviceName.isEmpty()) {
|
||||||
if (!shareInfo.pendingMapMessage && shareInfo.currentMapMessageId != -1L) {
|
if (!shareInfo.pendingMapMessage && shareInfo.currentMapMessageId != -1L) {
|
||||||
app.telegramHelper.editMapLocation(shareInfo, it)
|
app.telegramHelper.editMapLocation(shareInfo, it)
|
||||||
|
@ -134,9 +133,6 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
||||||
sendLocationToBot(it, shareInfo, SHARE_TYPE_MAP)
|
sendLocationToBot(it, shareInfo, SHARE_TYPE_MAP)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (shareInfo.getPendingTdLib() >= 10) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -194,7 +190,7 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
||||||
var bufferedMessagesFull = false
|
var bufferedMessagesFull = false
|
||||||
|
|
||||||
chatsShareInfo.values.forEach { shareInfo ->
|
chatsShareInfo.values.forEach { shareInfo ->
|
||||||
if (shareInfo.getPendingTdLib() >= 10 || app.locationMessages.getBufferedMessagesCountForChat(shareInfo.chatId) >= 10) {
|
if (shareInfo.pendingTdLibText >= MAX_MESSAGES_IN_TDLIB_PER_CHAT || shareInfo.pendingTdLibMap >= MAX_MESSAGES_IN_TDLIB_PER_CHAT) {
|
||||||
bufferedMessagesFull = true
|
bufferedMessagesFull = true
|
||||||
}
|
}
|
||||||
when (app.settings.shareTypeValue) {
|
when (app.settings.shareTypeValue) {
|
||||||
|
@ -240,7 +236,7 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
||||||
app.locationMessages.addBufferedMessage(message)
|
app.locationMessages.addBufferedMessage(message)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (shareInfo.getPendingTdLib() < 10) {
|
if (shareInfo.pendingTdLibText < MAX_MESSAGES_IN_TDLIB_PER_CHAT) {
|
||||||
app.telegramHelper.editTextLocation(shareInfo, message)
|
app.telegramHelper.editTextLocation(shareInfo, message)
|
||||||
} else {
|
} else {
|
||||||
app.locationMessages.addBufferedMessage(message)
|
app.locationMessages.addBufferedMessage(message)
|
||||||
|
@ -273,7 +269,7 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
||||||
app.locationMessages.addBufferedMessage(message)
|
app.locationMessages.addBufferedMessage(message)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (shareInfo.getPendingTdLib() < 10) {
|
if (shareInfo.pendingTdLibMap < MAX_MESSAGES_IN_TDLIB_PER_CHAT) {
|
||||||
app.telegramHelper.editMapLocation(shareInfo, message)
|
app.telegramHelper.editMapLocation(shareInfo, message)
|
||||||
} else {
|
} else {
|
||||||
app.locationMessages.addBufferedMessage(message)
|
app.locationMessages.addBufferedMessage(message)
|
||||||
|
@ -297,18 +293,24 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
||||||
if (app.isInternetConnectionAvailable) {
|
if (app.isInternetConnectionAvailable) {
|
||||||
log.debug("sendLocationToBot ${locationMessage.deviceName}")
|
log.debug("sendLocationToBot ${locationMessage.deviceName}")
|
||||||
val url = getDeviceSharingUrl(locationMessage, locationMessage.deviceName)
|
val url = getDeviceSharingUrl(locationMessage, locationMessage.deviceName)
|
||||||
shareInfo.lastSendTextMessageTime = (System.currentTimeMillis() / 1000).toInt()
|
if (shareType == SHARE_TYPE_TEXT) {
|
||||||
shareInfo.lastSendTextMessageTime = (System.currentTimeMillis() / 1000).toInt()
|
shareInfo.lastSendTextMessageTime = (System.currentTimeMillis() / 1000).toInt()
|
||||||
|
} else if (shareType == SHARE_TYPE_MAP) {
|
||||||
|
shareInfo.lastSendMapMessageTime = (System.currentTimeMillis() / 1000).toInt()
|
||||||
|
}
|
||||||
AndroidNetworkUtils.sendRequestAsync(app, url, null, "Send Location", false, false,
|
AndroidNetworkUtils.sendRequestAsync(app, url, null, "Send Location", false, false,
|
||||||
object : AndroidNetworkUtils.OnRequestResultListener {
|
object : AndroidNetworkUtils.OnRequestResultListener {
|
||||||
override fun onResult(result: String?) {
|
override fun onResult(result: String?) {
|
||||||
val chatsShareInfo = app.settings.getChatsShareInfo()
|
val success = checkResult(result)
|
||||||
val success = checkResultAndUpdateShareInfoSuccessfulSendTime(result, chatsShareInfo)
|
|
||||||
val osmandBotId = app.telegramHelper.getOsmandBot()?.id ?: -1
|
val osmandBotId = app.telegramHelper.getOsmandBot()?.id ?: -1
|
||||||
val device = app.settings.getCurrentSharingDevice()
|
val device = app.settings.getCurrentSharingDevice()
|
||||||
if (success) {
|
if (success) {
|
||||||
shareInfo.sentMessages++
|
shareInfo.sentMessages++
|
||||||
shareInfo.lastSuccessfulSendTime = System.currentTimeMillis() / 1000
|
if (shareType == SHARE_TYPE_TEXT) {
|
||||||
|
shareInfo.lastTextSuccessfulSendTime = System.currentTimeMillis() / 1000
|
||||||
|
} else if (shareType == SHARE_TYPE_MAP) {
|
||||||
|
shareInfo.lastMapSuccessfulSendTime = System.currentTimeMillis() / 1000
|
||||||
|
}
|
||||||
app.locationMessages.removeBufferedMessage(locationMessage)
|
app.locationMessages.removeBufferedMessage(locationMessage)
|
||||||
if ((shareInfo.shouldSendViaBotTextMessage || shareInfo.shouldSendViaBotMapMessage) && osmandBotId != -1 && device != null) {
|
if ((shareInfo.shouldSendViaBotTextMessage || shareInfo.shouldSendViaBotMapMessage) && osmandBotId != -1 && device != null) {
|
||||||
app.telegramHelper.sendViaBotLocationMessage(osmandBotId, shareInfo, TdApi.Location(locationMessage.lat, locationMessage.lon), device, shareType)
|
app.telegramHelper.sendViaBotLocationMessage(osmandBotId, shareInfo, TdApi.Location(locationMessage.lat, locationMessage.lon), device, shareType)
|
||||||
|
@ -339,18 +341,12 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
||||||
return builder.toString()
|
return builder.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkResultAndUpdateShareInfoSuccessfulSendTime(result: String?, chatsShareInfo: Map<Long, TelegramSettings.ShareChatInfo>):Boolean {
|
private fun checkResult(result: String?): Boolean {
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
try {
|
try {
|
||||||
val jsonResult = JSONObject(result)
|
val jsonResult = JSONObject(result)
|
||||||
val status = jsonResult.getString("status")
|
val status = jsonResult.getString("status")
|
||||||
val currentTime = System.currentTimeMillis() / 1000
|
return status == "OK"
|
||||||
if (status == "OK") {
|
|
||||||
chatsShareInfo.forEach { (_, shareInfo) ->
|
|
||||||
shareInfo.lastSuccessfulSendTime = currentTime
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
} catch (e: JSONException) {
|
} catch (e: JSONException) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -365,6 +361,8 @@ class ShareLocationHelper(private val app: TelegramApplication) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
|
const val MAX_MESSAGES_IN_TDLIB_PER_CHAT = 10
|
||||||
|
|
||||||
// min and max values for the UI
|
// min and max values for the UI
|
||||||
const val MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC = TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC - 1
|
const val MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC = TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC - 1
|
||||||
const val MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC = TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC + 1
|
const val MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC = TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC + 1
|
||||||
|
|
|
@ -137,7 +137,7 @@ class TelegramHelper private constructor() {
|
||||||
|
|
||||||
fun getChat(id: Long) = chats[id]
|
fun getChat(id: Long) = chats[id]
|
||||||
|
|
||||||
fun getUser(id: Int) = users[id]
|
fun getUser(id: Int) = if (id == getCurrentUserId()) currentUser else users[id]
|
||||||
|
|
||||||
fun getOsmandBot() = osmandBot
|
fun getOsmandBot() = osmandBot
|
||||||
|
|
||||||
|
@ -689,8 +689,6 @@ class TelegramHelper private constructor() {
|
||||||
outgoingMessagesListeners.forEach {
|
outgoingMessagesListeners.forEach {
|
||||||
it.onUpdateMessages(listOf(message))
|
it.onUpdateMessages(listOf(message))
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
incomingMessagesListeners.forEach {
|
incomingMessagesListeners.forEach {
|
||||||
|
@ -882,9 +880,7 @@ class TelegramHelper private constructor() {
|
||||||
log.debug("handleMapLocationMessageUpdate - ERROR $obj")
|
log.debug("handleMapLocationMessageUpdate - ERROR $obj")
|
||||||
val error = obj as TdApi.Error
|
val error = obj as TdApi.Error
|
||||||
needRefreshActiveLiveLocationMessages = true
|
needRefreshActiveLiveLocationMessages = true
|
||||||
if (error.code == MESSAGE_CANNOT_BE_EDITED_ERROR_CODE) {
|
if (error.code != IGNORED_ERROR_CODE) {
|
||||||
shareInfo.shouldDeletePreviousMapMessage = true
|
|
||||||
} else if (error.code != IGNORED_ERROR_CODE) {
|
|
||||||
shareInfo.hasSharingError = true
|
shareInfo.hasSharingError = true
|
||||||
outgoingMessagesListeners.forEach {
|
outgoingMessagesListeners.forEach {
|
||||||
it.onSendLiveLocationError(error.code, error.message)
|
it.onSendLiveLocationError(error.code, error.message)
|
||||||
|
@ -929,9 +925,7 @@ class TelegramHelper private constructor() {
|
||||||
TdApi.Error.CONSTRUCTOR -> {
|
TdApi.Error.CONSTRUCTOR -> {
|
||||||
log.debug("handleTextLocationMessageUpdate - ERROR")
|
log.debug("handleTextLocationMessageUpdate - ERROR")
|
||||||
val error = obj as TdApi.Error
|
val error = obj as TdApi.Error
|
||||||
if (error.code == MESSAGE_CANNOT_BE_EDITED_ERROR_CODE) {
|
if (error.code != IGNORED_ERROR_CODE) {
|
||||||
shareInfo.shouldDeletePreviousTextMessage = true
|
|
||||||
} else if (error.code != IGNORED_ERROR_CODE) {
|
|
||||||
shareInfo.hasSharingError = true
|
shareInfo.hasSharingError = true
|
||||||
outgoingMessagesListeners.forEach {
|
outgoingMessagesListeners.forEach {
|
||||||
it.onSendLiveLocationError(error.code, error.message)
|
it.onSendLiveLocationError(error.code, error.message)
|
||||||
|
|
|
@ -75,7 +75,7 @@ class SharingStatusBottomSheet : DialogFragment() {
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
setOnClickListener {
|
setOnClickListener {
|
||||||
app.shareLocationHelper.checkNetworkType()
|
app.shareLocationHelper.checkNetworkType()
|
||||||
app.settings.prepareForSharingNewMessages()
|
app.settings.prepareForSharingNewMessages(sharingStatus.chatsIds)
|
||||||
app.shareLocationHelper.checkAndSendBufferMessages()
|
app.shareLocationHelper.checkAndSendBufferMessages()
|
||||||
app.forceUpdateMyLocation()
|
app.forceUpdateMyLocation()
|
||||||
dismiss()
|
dismiss()
|
||||||
|
|
Loading…
Reference in a new issue