From 1eb37fd3ae55c55e2050006e607affe21517c0d4 Mon Sep 17 00:00:00 2001 From: Chumva Date: Thu, 4 Oct 2018 16:50:49 +0300 Subject: [PATCH] Fix max sharing time and add check for internet connection --- .../src/net/osmand/telegram/TelegramSettings.kt | 1 - .../telegram/helpers/ShareLocationHelper.kt | 16 +++++++++++++++- .../osmand/telegram/ui/MyLocationTabFragment.kt | 12 +++++++----- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 94454f4764..62feb72eae 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -126,7 +126,6 @@ class TelegramSettings(private val app: TelegramApplication) { if (share) { val lp: Long = when { livePeriod < TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong() - livePeriod > TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong() else -> livePeriod } chatLivePeriods[chatId] = lp diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index 991ec25de8..2521260fba 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -40,11 +40,25 @@ class ShareLocationHelper(private val app: TelegramApplication) { if (location != null && app.isInternetConnectionAvailable) { val chatLivePeriods = app.settings.getChatLivePeriods() + val updatedLivePeriods = mutableMapOf() if (chatLivePeriods.isNotEmpty()) { + chatLivePeriods.forEach { (chatId, livePeriod) -> + if (livePeriod > TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC) { + val startTime = app.settings.getChatShareLocStartSec(chatId) + val currTime = (System.currentTimeMillis() / 1000) + if (startTime != null && startTime + TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC < currTime) { + app.settings.shareLocationToChat(chatId, true, livePeriod - TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC) + } else if (startTime != null) { + updatedLivePeriods[chatId] = TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong() + } + } else { + updatedLivePeriods[chatId] = livePeriod + } + } val user = app.telegramHelper.getCurrentUser() val sharingMode = app.settings.currentSharingMode if (user != null && sharingMode == user.id.toString()) { - app.telegramHelper.sendLiveLocationMessage(chatLivePeriods, location.latitude, location.longitude) + app.telegramHelper.sendLiveLocationMessage(updatedLivePeriods, location.latitude, location.longitude) } else if (sharingMode.isNotEmpty()) { val url = "https://live.osmand.net/device/$sharingMode/send?lat=${location.latitude}&lon=${location.longitude}" AndroidNetworkUtils.sendRequestAsync(url, null) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt index 39de2eb67b..789a8759ff 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt @@ -440,7 +440,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener { if (sharingMode) { val message = telegramHelper.getChatLiveMessages()[chat.id] if (message != null) { - settings.updateChatShareLocStartSec(chatId, message.date.toLong()) +// settings.updateChatShareLocStartSec(chatId, message.date.toLong()) } } else { continue @@ -465,7 +465,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener { if (settings.isSharingLocationToChat(it.chatId) && (settings.getChatShareLocStartSec(it.chatId) == null || settings.getChatLivePeriod(it.chatId) == null)) { settings.shareLocationToChat(it.chatId, true, (it.content as TdApi.MessageLocation).livePeriod.toLong()) - settings.updateChatShareLocStartSec(it.chatId, it.date.toLong()) +// settings.updateChatShareLocStartSec(it.chatId, it.date.toLong()) } } sharingMode = settings.hasAnyChatToShareLocation() @@ -582,9 +582,11 @@ class MyLocationTabFragment : Fragment(), TelegramListener { val chatNextAddTime = settings.getChatNextAddActiveTime(chat.id) val newLivePeriod = settings.getChatLiveMessageExpireTime(chat.id) + settings.getChatAddActiveTime(chat.id) settings.shareLocationToChat(chat.id, true, newLivePeriod, chatNextAddTime) - telegramHelper.pauseSendingLiveLocationToChat(chat.id) - telegramHelper.deleteLiveLocationMessage(chat.id) - telegramHelper.resumeSendingLiveLocationToChat(chat.id) + if (app.isInternetConnectionAvailable) { + telegramHelper.pauseSendingLiveLocationToChat(chat.id) + telegramHelper.deleteLiveLocationMessage(chat.id) + telegramHelper.resumeSendingLiveLocationToChat(chat.id) + } notifyItemChanged(position) } }