buffer time
This commit is contained in:
parent
704d63ed1b
commit
14aa10ad02
3 changed files with 57 additions and 8 deletions
|
@ -1,5 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<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">Time zone</string>
|
||||
<string name="units_and_formats">Units & formats</string>
|
||||
|
|
|
@ -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<String> {
|
||||
return BUFFER_TIME.map { OsmandFormatter.getFormattedDuration(app, it) }
|
||||
}
|
||||
}
|
||||
|
||||
abstract inner class ListPreference(
|
||||
@DrawableRes val iconId: Int,
|
||||
@StringRes val titleId: Int,
|
||||
|
|
|
@ -25,35 +25,42 @@ class LocationMessages(val app: TelegramApplication) {
|
|||
|
||||
init {
|
||||
dbHelper = SQLiteHelper(app)
|
||||
readBufferedMessages(app.settings.bufferTime)
|
||||
readBufferedMessages()
|
||||
readLastMessages()
|
||||
}
|
||||
|
||||
fun getBufferedMessages(): List<BufferMessage> {
|
||||
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<BufferMessage> {
|
||||
removeOldBufferedMessages()
|
||||
return bufferedMessages.filter { it.chatId == chatId }.sortedBy { it.time }
|
||||
}
|
||||
|
||||
fun getBufferedTextMessagesForChat(chatId: Long): List<BufferMessage> {
|
||||
removeOldBufferedMessages()
|
||||
return bufferedMessages.filter { it.chatId == chatId && it.type == TYPE_TEXT }.sortedBy { it.time }
|
||||
}
|
||||
|
||||
fun getBufferedMapMessagesForChat(chatId: Long): List<BufferMessage> {
|
||||
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<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() {
|
||||
|
@ -293,7 +319,7 @@ class LocationMessages(val app: TelegramApplication) {
|
|||
return res
|
||||
}
|
||||
|
||||
internal fun getBufferedMessages(bufferTime: Long): List<BufferMessage> {
|
||||
internal fun getBufferedMessages(): List<BufferMessage> {
|
||||
val res = arrayListOf<BufferMessage>()
|
||||
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<BufferMessage>, bufferTime: Long): List<BufferMessage> {
|
||||
|
|
Loading…
Reference in a new issue