buffer time initial commit

This commit is contained in:
Dmitriy Ruban 2019-12-19 17:17:34 +02:00
parent 22be792a53
commit 704d63ed1b
2 changed files with 25 additions and 5 deletions

View file

@ -61,6 +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 const val BUFFER_TIME_KEY = "buffer_time"
private const val SETTINGS_NAME = "osmand_telegram_settings"
private const val SHARE_LOCATION_CHATS_KEY = "share_location_chats"
@ -152,6 +155,8 @@ class TelegramSettings(private val app: TelegramApplication) {
var proxyEnabled = false
var bufferTime = MIN_BUFFER_TIME
init {
updatePrefs()
read()

View file

@ -25,7 +25,7 @@ class LocationMessages(val app: TelegramApplication) {
init {
dbHelper = SQLiteHelper(app)
readBufferedMessages()
readBufferedMessages(app.settings.bufferTime)
readLastMessages()
}
@ -140,8 +140,8 @@ class LocationMessages(val app: TelegramApplication) {
dbHelper.removeBufferedMessage(message)
}
private fun readBufferedMessages() {
this.bufferedMessages = dbHelper.getBufferedMessages()
private fun readBufferedMessages(bufferTime: Long) {
this.bufferedMessages = dbHelper.getBufferedMessages(bufferTime)
}
private fun readLastMessages() {
@ -293,7 +293,7 @@ class LocationMessages(val app: TelegramApplication) {
return res
}
internal fun getBufferedMessages(): List<BufferMessage> {
internal fun getBufferedMessages(bufferTime: Long): List<BufferMessage> {
val res = arrayListOf<BufferMessage>()
readableDatabase?.rawQuery(BUFFER_TABLE_SELECT, null)?.apply {
if (moveToFirst()) {
@ -303,7 +303,22 @@ class LocationMessages(val app: TelegramApplication) {
}
close()
}
return res
return removeOldBufferedMessages(res, bufferTime)
}
internal fun removeOldBufferedMessages(list: List<BufferMessage>, bufferTime: Long): List<BufferMessage> {
log.info("removeOldBufferedMessages" )
val currentTime = System.currentTimeMillis()
val cleanedList = arrayListOf<BufferMessage>()
list.forEach { message ->
if (currentTime - message.time > bufferTime) {
removeBufferedMessage(message)
log.debug("remove old buffered message: $message" )
} else {
cleanedList.add(message)
}
}
return cleanedList
}
internal fun getLastMessages(): MutableList<LocationMessage> {