From 90a8ef9492964fd11a8d0358821a929f7b4d3eee Mon Sep 17 00:00:00 2001 From: Dmitriy Ruban Date: Fri, 3 Jan 2020 18:58:49 +0200 Subject: [PATCH] change buffered points count in widget to first write time to buffer --- OsmAnd-telegram/res/values/strings.xml | 1 - .../net/osmand/telegram/TelegramService.kt | 5 ++++- .../telegram/helpers/LocationMessages.kt | 19 +++++++++++++++++++ .../telegram/helpers/ShowLocationHelper.kt | 16 +++++++++++----- .../osmand/telegram/utils/OsmandFormatter.kt | 10 ++++++++++ 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/OsmAnd-telegram/res/values/strings.xml b/OsmAnd-telegram/res/values/strings.xml index c40efc6d42..3ce0099318 100644 --- a/OsmAnd-telegram/res/values/strings.xml +++ b/OsmAnd-telegram/res/values/strings.xml @@ -1,7 +1,6 @@ OsmAnd Tracker status - Time after which buffered location messages will be deleted Maximum time to store points in the buffer Buffer expiration time Select time zone to show in your location messages. diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt index cc1e7819ea..3cc2482c3b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt @@ -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) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt index 065c7489d2..2ab6a47eff 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt @@ -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 { @@ -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 { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 9ffd426714..c4b73689d9 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -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 { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandFormatter.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandFormatter.kt index b351a90efb..010419c937 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandFormatter.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandFormatter.kt @@ -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