Speedup buffer sending. Fix buffer hanging on app restart.

This commit is contained in:
max-klaus 2020-01-27 18:18:42 +03:00
parent e737223c5c
commit 28a9ffb058
2 changed files with 29 additions and 6 deletions

View file

@ -109,10 +109,11 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
sendLocationInterval = intent.getLongExtra(SEND_LOCATION_INTERVAL, 0)
setupServiceErrorInterval()
app.telegramService = this
app.telegramHelper.addIncomingMessagesListener(this)
app.telegramHelper.addOutgoingMessagesListener(this)
app.telegramService = this
if (isUsedByMyLocation(usedBy)) {
initLocationUpdates()
startShareInfoUpdates()
@ -122,6 +123,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
app.telegramHelper.startLiveMessagesUpdates(app.settings.sendMyLocInterval)
startTracksUpdates()
}
app.shareLocationHelper.checkAndSendBufferMessages()
val locationNotification = app.notificationHelper.locationNotification
val notification = app.notificationHelper.buildNotification(locationNotification)

View file

@ -1,5 +1,7 @@
package net.osmand.telegram.helpers
import android.os.Handler
import android.os.Message
import net.osmand.Location
import net.osmand.PlatformUtil
import net.osmand.telegram.*
@ -16,7 +18,9 @@ import org.json.JSONObject
private const val USER_SET_LIVE_PERIOD_DELAY_MS = 5000 // 5 sec
private const val MIN_MESSAGE_SENDING_INTERVAL_MS = 1000 // 1 sec
private const val MIN_MESSAGES_BUFFER_CHECK_INTERVAL_MS = 800 // 0.8 sec
private const val MIN_MESSAGES_BUFFER_CHECK_INTERVAL_MS = 300 // 0.3 sec
private const val SEND_MESSAGES_BUFFER_MS_MSG_ID = 5000
class ShareLocationHelper(private val app: TelegramApplication) {
@ -52,6 +56,9 @@ class ShareLocationHelper(private val app: TelegramApplication) {
private var lastTimeInMillis: Long = 0L
private var sendBufferMessagesHandler = Handler()
private var sendBufferMessagesRunnable = Runnable { app.shareLocationHelper.checkAndSendBufferMessages(false) }
fun updateLocation(location: Location?) {
val lastPoint = lastLocation
var record = true
@ -120,7 +127,7 @@ class ShareLocationHelper(private val app: TelegramApplication) {
}
}
fun checkAndSendBufferMessages() {
fun checkAndSendBufferMessages(checkNetworkTypeAllowed: Boolean = true) {
log.debug("checkAndSendBufferMessages")
var pendingMessagesLimitReached = false
app.settings.getChatsShareInfo().forEach { (chatId, shareInfo) ->
@ -129,18 +136,30 @@ class ShareLocationHelper(private val app: TelegramApplication) {
pendingMessagesLimitReached = true
}
}
if (pendingMessagesLimitReached) {
if (pendingMessagesLimitReached && checkNetworkTypeAllowed) {
checkNetworkType()
}
}
fun checkAndSendBufferMessagesDelayed() {
if (!sendBufferMessagesHandler.hasMessages(SEND_MESSAGES_BUFFER_MS_MSG_ID)) {
val msg = Message.obtain(sendBufferMessagesHandler, sendBufferMessagesRunnable)
msg.what = SEND_MESSAGES_BUFFER_MS_MSG_ID
sendBufferMessagesHandler.sendMessageDelayed(msg, MIN_MESSAGES_BUFFER_CHECK_INTERVAL_MS.toLong() + 1L)
}
}
fun checkAndSendBufferMessagesToChat(chatId: Long, forced: Boolean = false): Int {
val shareChatsInfo = app.settings.getChatsShareInfo()
val shareInfo = shareChatsInfo[chatId]
if (shareInfo != null) {
val bufferedMessagesCount = app.locationMessages.getBufferedMessagesCountForChat(chatId)
if (bufferedMessagesCount > 0) {
checkAndSendBufferMessagesDelayed()
}
val currentTime = System.currentTimeMillis()
if (currentTime - shareInfo.lastBufferCheckTime < MIN_MESSAGES_BUFFER_CHECK_INTERVAL_MS && !forced) {
return app.locationMessages.getBufferedMessagesCountForChat(chatId)
return bufferedMessagesCount
}
shareInfo.lastBufferCheckTime = currentTime
@ -182,7 +201,9 @@ class ShareLocationHelper(private val app: TelegramApplication) {
refreshNotification()
checkAndSendBufferMessages()
if (app.telegramService != null) {
checkAndSendBufferMessages()
}
} else {
app.forceUpdateMyLocation()
}