change buffered points count in widget to first write time to buffer

This commit is contained in:
Dmitriy Ruban 2020-01-03 18:58:49 +02:00
parent 06ac9d7baf
commit 90a8ef9492
5 changed files with 44 additions and 7 deletions

View file

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="status_widget_title">OsmAnd Tracker status</string>
<string name="buffer_time_descr">Time after which buffered location messages will be deleted</string>
<string name="buffer_time_descr">Maximum time to store points in the buffer</string>
<string name="buffer_time">Buffer expiration time</string>
<string name="time_zone_descr">Select time zone to show in your location messages.</string>

View file

@ -27,6 +27,8 @@ private const val UPDATE_LIVE_TRACKS_INTERVAL_MS = 30000L // 30 sec
class TelegramService : Service(), LocationListener, TelegramIncomingMessagesListener,
TelegramOutgoingMessagesListener {
private val log = PlatformUtil.getLog(TelegramService::class.java)
private fun app() = application as TelegramApplication
private val binder = LocationServiceBinder()
private var shouldCleanupResources: Boolean = false
@ -224,7 +226,8 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
if (isUsedByMyLocation(usedBy)) {
val sharingStatus = app().settings.sharingStatusChanges.last()
val isSending = !((sharingStatus.statusType == TelegramSettings.SharingStatusType.NO_GPS) || (sharingStatus.statusType == TelegramSettings.SharingStatusType.INITIALIZING))
app().showLocationHelper.addOrUpdateStatusWidget(app().locationMessages.getBufferedMessagesCount(), isSending)
app().showLocationHelper.addOrUpdateStatusWidget(app().locationMessages.firstWriteTime, isSending)
log.info("difference in time: ${System.currentTimeMillis() - app().locationMessages.firstWriteTime}")
}
startWidgetUpdates()
}, UPDATE_WIDGET_INTERVAL_MS)

View file

@ -25,10 +25,13 @@ class LocationMessages(val app: TelegramApplication) {
private var lastRemoveTime: Long? = null
var firstWriteTime: Long = 0L
init {
dbHelper = SQLiteHelper(app)
readBufferedMessages()
readLastMessages()
firstWriteTime = getFirstWriteTime() ?: 0L
}
fun getBufferedMessages(): List<BufferMessage> {
@ -97,6 +100,7 @@ class LocationMessages(val app: TelegramApplication) {
messages.add(message)
this.bufferedMessages = messages
dbHelper.addBufferedMessage(message)
updateFirstWriteTime()
}
fun addNewLocationMessage(message: TdApi.Message) {
@ -139,6 +143,7 @@ class LocationMessages(val app: TelegramApplication) {
log.debug("clearBufferedMessages")
dbHelper.clearBufferedMessages()
bufferedMessages = emptyList()
updateFirstWriteTime()
}
fun removeBufferedMessage(message: BufferMessage) {
@ -147,6 +152,15 @@ class LocationMessages(val app: TelegramApplication) {
messages.remove(message)
this.bufferedMessages = messages
dbHelper.removeBufferedMessage(message)
updateFirstWriteTime()
}
private fun updateFirstWriteTime() {
if (firstWriteTime == 0L && bufferedMessages.isNotEmpty()) {
firstWriteTime = System.currentTimeMillis()
} else if (bufferedMessages.isEmpty()) {
firstWriteTime = 0L
}
}
private fun removeOldBufferedMessages() {
@ -163,9 +177,14 @@ class LocationMessages(val app: TelegramApplication) {
messages.removeAll(expiredList)
this.bufferedMessages = messages.toList()
lastRemoveTime = currentTime
updateFirstWriteTime()
}
}
private fun getFirstWriteTime(): Long? {
return bufferedMessages.minBy { it.time }?.time
}
private fun isTimeToDelete(currentTime: Long) = if (lastRemoveTime != null) {
currentTime - lastRemoveTime!! > 60000L
} else {

View file

@ -205,11 +205,17 @@ class ShowLocationHelper(private val app: TelegramApplication) {
}
}
fun addOrUpdateStatusWidget(count: Int, isSending: Boolean) {
fun addOrUpdateStatusWidget(time: Long, isSending: Boolean) {
val iconDay: String
val iconNight: String
val text = when {
count >= 0 && isSending -> {
time > 0L -> {
iconDay = STATUS_WIDGET_ANIM_ICON_DAY
iconNight = STATUS_WIDGET_ANIM_ICON_NIGHT
val diffTime = (System.currentTimeMillis() - time) / 1000
OsmandFormatter.getFormattedDurationShort(diffTime)
}
time == 0L && isSending -> {
iconDay = STATUS_WIDGET_ANIM_ICON_DAY
iconNight = STATUS_WIDGET_ANIM_ICON_NIGHT
app.getString(R.string.shared_string_ok)
@ -220,8 +226,8 @@ class ShowLocationHelper(private val app: TelegramApplication) {
app.getString(R.string.shared_string_off)
}
}
val bufferText = when {
count > 0 -> "($count)"
val subText = when {
time > 0 -> app.getString(R.string.shared_string_minute_short)
else -> ""
}
osmandAidlHelper.addMapWidget(
@ -230,7 +236,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
app.getString(R.string.status_widget_title),
iconDay,
iconNight,
text, bufferText, 50, getStatusWidgetIntent())
text, subText, 50, getStatusWidgetIntent())
}
private fun getStatusWidgetIntent(): Intent {

View file

@ -45,6 +45,16 @@ object OsmandFormatter {
fixed2.minimumIntegerDigits = 1
}
fun getFormattedDurationShort(seconds: Long): String {
val hours = seconds / (60 * 60)
val minutes = seconds / 60 % 60
return if (hours >= 1) {
String.format("%1d:%02d", hours, minutes)
} else {
String.format("%02d", minutes)
}
}
fun getFormattedDuration(ctx: Context, seconds: Long, short: Boolean = false): String {
val hours = seconds / (60 * 60)
val minutes = seconds / 60 % 60