buffer time

This commit is contained in:
Dmitriy Ruban 2019-12-20 16:51:38 +02:00
parent 704d63ed1b
commit 14aa10ad02
3 changed files with 57 additions and 8 deletions

View file

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="buffer_time_descr">Time after which buffered messages will be deleted</string>
<string name="buffer_time">Buffer time</string>
<string name="time_zone_descr">Select time zone to show in your location messages.</string> <string name="time_zone_descr">Select time zone to show in your location messages.</string>
<string name="time_zone">Time zone</string> <string name="time_zone">Time zone</string>
<string name="units_and_formats">Units &amp; formats</string> <string name="units_and_formats">Units &amp; formats</string>

View file

@ -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_ACCURACY_INDEX = 0
private const val MIN_LOCATION_SPEED_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 BUFFER_TIME_KEY = "buffer_time"
private const val SETTINGS_NAME = "osmand_telegram_settings" private const val SETTINGS_NAME = "osmand_telegram_settings"
@ -143,7 +145,8 @@ class TelegramSettings(private val app: TelegramApplication) {
var liveNowSortType = LiveNowSortType.SORT_BY_DISTANCE 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 gpxLoggingPrefs = listOf(MinLocationDistance(), MinLocationAccuracy(), MinLocationSpeed())
val unitsAndFormatsPrefs = listOf(UnitsOfSpeed(), UnitsOfLength(), UtcOffset()) val unitsAndFormatsPrefs = listOf(UnitsOfSpeed(), UnitsOfLength(), UtcOffset())
@ -155,7 +158,7 @@ class TelegramSettings(private val app: TelegramApplication) {
var proxyEnabled = false var proxyEnabled = false
var bufferTime = MIN_BUFFER_TIME var bufferTime = BUFFER_TIME[BUFFER_TIME_INDEX]
init { init {
updatePrefs() updatePrefs()
@ -636,6 +639,8 @@ class TelegramSettings(private val app: TelegramApplication) {
edit.putBoolean(PROXY_ENABLED, proxyEnabled) edit.putBoolean(PROXY_ENABLED, proxyEnabled)
edit.putLong(BUFFER_TIME_KEY, bufferTime)
val jArray = convertShareChatsInfoToJson() val jArray = convertShareChatsInfoToJson()
if (jArray != null) { if (jArray != null) {
edit.putString(SHARE_CHATS_INFO_KEY, jArray.toString()) 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) showGpsPoints = prefs.getBoolean(SHOW_GPS_POINTS, false)
proxyEnabled = prefs.getBoolean(PROXY_ENABLED, false) proxyEnabled = prefs.getBoolean(PROXY_ENABLED, false)
bufferTime = prefs.getLong(BUFFER_TIME_KEY, BUFFER_TIME[BUFFER_TIME_INDEX])
try { try {
parseProxyPreferences(JSONObject(prefs.getString(PROXY_PREFERENCES_KEY, ""))) parseProxyPreferences(JSONObject(prefs.getString(PROXY_PREFERENCES_KEY, "")))
} catch (e: JSONException) { } catch (e: JSONException) {
@ -1090,6 +1098,19 @@ class TelegramSettings(private val app: TelegramApplication) {
override fun getMenuItems() = formattedUtcOffsets 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<String> {
return BUFFER_TIME.map { OsmandFormatter.getFormattedDuration(app, it) }
}
}
abstract inner class ListPreference( abstract inner class ListPreference(
@DrawableRes val iconId: Int, @DrawableRes val iconId: Int,
@StringRes val titleId: Int, @StringRes val titleId: Int,

View file

@ -25,35 +25,42 @@ class LocationMessages(val app: TelegramApplication) {
init { init {
dbHelper = SQLiteHelper(app) dbHelper = SQLiteHelper(app)
readBufferedMessages(app.settings.bufferTime) readBufferedMessages()
readLastMessages() readLastMessages()
} }
fun getBufferedMessages(): List<BufferMessage> { fun getBufferedMessages(): List<BufferMessage> {
removeOldBufferedMessages()
return bufferedMessages.sortedBy { it.time } return bufferedMessages.sortedBy { it.time }
} }
fun getBufferedMessagesCount(): Int { fun getBufferedMessagesCount(): Int {
removeOldBufferedMessages()
return bufferedMessages.size return bufferedMessages.size
} }
fun getBufferedMessagesCountForChat(chatId: Long, type: Int): Int { fun getBufferedMessagesCountForChat(chatId: Long, type: Int): Int {
removeOldBufferedMessages()
return bufferedMessages.count { it.chatId == chatId && it.type == type } return bufferedMessages.count { it.chatId == chatId && it.type == type }
} }
fun getBufferedMessagesCountForChat(chatId: Long): Int { fun getBufferedMessagesCountForChat(chatId: Long): Int {
removeOldBufferedMessages()
return bufferedMessages.count { it.chatId == chatId} return bufferedMessages.count { it.chatId == chatId}
} }
fun getBufferedMessagesForChat(chatId: Long): List<BufferMessage> { fun getBufferedMessagesForChat(chatId: Long): List<BufferMessage> {
removeOldBufferedMessages()
return bufferedMessages.filter { it.chatId == chatId }.sortedBy { it.time } return bufferedMessages.filter { it.chatId == chatId }.sortedBy { it.time }
} }
fun getBufferedTextMessagesForChat(chatId: Long): List<BufferMessage> { fun getBufferedTextMessagesForChat(chatId: Long): List<BufferMessage> {
removeOldBufferedMessages()
return bufferedMessages.filter { it.chatId == chatId && it.type == TYPE_TEXT }.sortedBy { it.time } return bufferedMessages.filter { it.chatId == chatId && it.type == TYPE_TEXT }.sortedBy { it.time }
} }
fun getBufferedMapMessagesForChat(chatId: Long): List<BufferMessage> { fun getBufferedMapMessagesForChat(chatId: Long): List<BufferMessage> {
removeOldBufferedMessages()
return bufferedMessages.filter { it.chatId == chatId && it.type == TYPE_MAP }.sortedBy { it.time } 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) dbHelper.removeBufferedMessage(message)
} }
private fun readBufferedMessages(bufferTime: Long) { private fun removeOldBufferedMessages() {
this.bufferedMessages = dbHelper.getBufferedMessages(bufferTime) if (this.bufferedMessages.isNotEmpty()) {
val bufferTime = app.settings.bufferTime * 1000
val currentTime = System.currentTimeMillis()
val cleanedList = arrayListOf<BufferMessage>()
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() { private fun readLastMessages() {
@ -293,7 +319,7 @@ class LocationMessages(val app: TelegramApplication) {
return res return res
} }
internal fun getBufferedMessages(bufferTime: Long): List<BufferMessage> { internal fun getBufferedMessages(): List<BufferMessage> {
val res = arrayListOf<BufferMessage>() val res = arrayListOf<BufferMessage>()
readableDatabase?.rawQuery(BUFFER_TABLE_SELECT, null)?.apply { readableDatabase?.rawQuery(BUFFER_TABLE_SELECT, null)?.apply {
if (moveToFirst()) { if (moveToFirst()) {
@ -303,7 +329,7 @@ class LocationMessages(val app: TelegramApplication) {
} }
close() close()
} }
return removeOldBufferedMessages(res, bufferTime) return res
} }
internal fun removeOldBufferedMessages(list: List<BufferMessage>, bufferTime: Long): List<BufferMessage> { internal fun removeOldBufferedMessages(list: List<BufferMessage>, bufferTime: Long): List<BufferMessage> {