diff --git a/OsmAnd-telegram/res/values/strings.xml b/OsmAnd-telegram/res/values/strings.xml index 05b27552e7..5ea053ad64 100644 --- a/OsmAnd-telegram/res/values/strings.xml +++ b/OsmAnd-telegram/res/values/strings.xml @@ -1,5 +1,7 @@ + Time after which buffered messages will be deleted + Buffer time Select time zone to show in your location messages. Time zone Units & formats diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 37b9b86b2f..11ecf04ede 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -61,7 +61,9 @@ private const val MIN_LOCATION_DISTANCE_INDEX = 0 private const val MIN_LOCATION_ACCURACY_INDEX = 0 private const val MIN_LOCATION_SPEED_INDEX = 0 -private const val MIN_BUFFER_TIME = 60 * 1L // 1 minute for test +private val BUFFER_TIME = listOf(60 * 60L, 2 * 60 * 60L, 4 * 60 * 60L, 8 * 60 * 60L, + 12 * 60 * 60L, 24 * 60 * 60L) +private const val BUFFER_TIME_INDEX = 0 private const val BUFFER_TIME_KEY = "buffer_time" private const val SETTINGS_NAME = "osmand_telegram_settings" @@ -143,7 +145,8 @@ class TelegramSettings(private val app: TelegramApplication) { var liveNowSortType = LiveNowSortType.SORT_BY_DISTANCE - val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref(), ShareTypePref()) + val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref(), ShareTypePref(), + BufferTimePref()) val gpxLoggingPrefs = listOf(MinLocationDistance(), MinLocationAccuracy(), MinLocationSpeed()) val unitsAndFormatsPrefs = listOf(UnitsOfSpeed(), UnitsOfLength(), UtcOffset()) @@ -155,7 +158,7 @@ class TelegramSettings(private val app: TelegramApplication) { var proxyEnabled = false - var bufferTime = MIN_BUFFER_TIME + var bufferTime = BUFFER_TIME[BUFFER_TIME_INDEX] init { updatePrefs() @@ -636,6 +639,8 @@ class TelegramSettings(private val app: TelegramApplication) { edit.putBoolean(PROXY_ENABLED, proxyEnabled) + edit.putLong(BUFFER_TIME_KEY, bufferTime) + val jArray = convertShareChatsInfoToJson() if (jArray != null) { edit.putString(SHARE_CHATS_INFO_KEY, jArray.toString()) @@ -718,6 +723,9 @@ class TelegramSettings(private val app: TelegramApplication) { showGpsPoints = prefs.getBoolean(SHOW_GPS_POINTS, false) proxyEnabled = prefs.getBoolean(PROXY_ENABLED, false) + + bufferTime = prefs.getLong(BUFFER_TIME_KEY, BUFFER_TIME[BUFFER_TIME_INDEX]) + try { parseProxyPreferences(JSONObject(prefs.getString(PROXY_PREFERENCES_KEY, ""))) } catch (e: JSONException) { @@ -1090,6 +1098,19 @@ class TelegramSettings(private val app: TelegramApplication) { override fun getMenuItems() = formattedUtcOffsets } + inner class BufferTimePref : ListPreference(R.drawable.ic_action_time_span, R.string.buffer_time, + R.string.buffer_time_descr) { + override fun getCurrentValue() = OsmandFormatter.getFormattedDuration(app, bufferTime) + + override fun setCurrentValue(index: Int) { + bufferTime = BUFFER_TIME[index] + } + + override fun getMenuItems(): List { + return BUFFER_TIME.map { OsmandFormatter.getFormattedDuration(app, it) } + } + } + abstract inner class ListPreference( @DrawableRes val iconId: Int, @StringRes val titleId: Int, diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt index f418379cc3..4a1f855c27 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt @@ -25,35 +25,42 @@ class LocationMessages(val app: TelegramApplication) { init { dbHelper = SQLiteHelper(app) - readBufferedMessages(app.settings.bufferTime) + readBufferedMessages() readLastMessages() } fun getBufferedMessages(): List { + removeOldBufferedMessages() return bufferedMessages.sortedBy { it.time } } fun getBufferedMessagesCount(): Int { + removeOldBufferedMessages() return bufferedMessages.size } fun getBufferedMessagesCountForChat(chatId: Long, type: Int): Int { + removeOldBufferedMessages() return bufferedMessages.count { it.chatId == chatId && it.type == type } } fun getBufferedMessagesCountForChat(chatId: Long): Int { + removeOldBufferedMessages() return bufferedMessages.count { it.chatId == chatId} } fun getBufferedMessagesForChat(chatId: Long): List { + removeOldBufferedMessages() return bufferedMessages.filter { it.chatId == chatId }.sortedBy { it.time } } fun getBufferedTextMessagesForChat(chatId: Long): List { + removeOldBufferedMessages() return bufferedMessages.filter { it.chatId == chatId && it.type == TYPE_TEXT }.sortedBy { it.time } } fun getBufferedMapMessagesForChat(chatId: Long): List { + removeOldBufferedMessages() return bufferedMessages.filter { it.chatId == chatId && it.type == TYPE_MAP }.sortedBy { it.time } } @@ -140,8 +147,27 @@ class LocationMessages(val app: TelegramApplication) { dbHelper.removeBufferedMessage(message) } - private fun readBufferedMessages(bufferTime: Long) { - this.bufferedMessages = dbHelper.getBufferedMessages(bufferTime) + private fun removeOldBufferedMessages() { + if (this.bufferedMessages.isNotEmpty()) { + val bufferTime = app.settings.bufferTime * 1000 + val currentTime = System.currentTimeMillis() + val cleanedList = arrayListOf() + this.bufferedMessages.forEach { message -> + val diffTime = currentTime - message.time + if (diffTime > bufferTime) { + log.debug("remove old buffered message with diff in time: $diffTime") + removeBufferedMessage(message) + } else { + cleanedList.add(message) + } + } + this.bufferedMessages = cleanedList + } + } + + private fun readBufferedMessages() { + this.bufferedMessages = dbHelper.getBufferedMessages() + removeOldBufferedMessages() } private fun readLastMessages() { @@ -293,7 +319,7 @@ class LocationMessages(val app: TelegramApplication) { return res } - internal fun getBufferedMessages(bufferTime: Long): List { + internal fun getBufferedMessages(): List { val res = arrayListOf() readableDatabase?.rawQuery(BUFFER_TABLE_SELECT, null)?.apply { if (moveToFirst()) { @@ -303,7 +329,7 @@ class LocationMessages(val app: TelegramApplication) { } close() } - return removeOldBufferedMessages(res, bufferTime) + return res } internal fun removeOldBufferedMessages(list: List, bufferTime: Long): List {