getDurationForWidget fix / refactor

This commit is contained in:
Dmitriy Ruban 2020-01-06 14:08:21 +02:00
parent 90a8ef9492
commit a67edaf6d0
4 changed files with 34 additions and 31 deletions

View file

@ -225,9 +225,23 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
updateWidgetHandler?.postDelayed({
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.firstWriteTime, isSending)
log.info("difference in time: ${System.currentTimeMillis() - app().locationMessages.firstWriteTime}")
val isSending = sharingStatus.statusType == TelegramSettings.SharingStatusType.SENDING
val sharingChats = app().settings.getShareLocationChats()
var oldestTime = 0L
if (sharingChats.isNotEmpty()) {
sharingChats.forEach { id ->
val bufferMessages = app().locationMessages.getBufferedMessagesForChat(id)
if (bufferMessages.isNotEmpty()) {
val newTime = bufferMessages[0].time
if (oldestTime == 0L || newTime < oldestTime) {
oldestTime = newTime
}
} else {
oldestTime = 0L
}
}
}
app().showLocationHelper.addOrUpdateStatusWidget(oldestTime, isSending)
}
startWidgetUpdates()
}, UPDATE_WIDGET_INTERVAL_MS)

View file

@ -25,13 +25,10 @@ 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> {
@ -100,7 +97,6 @@ class LocationMessages(val app: TelegramApplication) {
messages.add(message)
this.bufferedMessages = messages
dbHelper.addBufferedMessage(message)
updateFirstWriteTime()
}
fun addNewLocationMessage(message: TdApi.Message) {
@ -143,7 +139,6 @@ class LocationMessages(val app: TelegramApplication) {
log.debug("clearBufferedMessages")
dbHelper.clearBufferedMessages()
bufferedMessages = emptyList()
updateFirstWriteTime()
}
fun removeBufferedMessage(message: BufferMessage) {
@ -152,15 +147,6 @@ 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() {
@ -177,14 +163,9 @@ 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

@ -213,7 +213,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
iconDay = STATUS_WIDGET_ANIM_ICON_DAY
iconNight = STATUS_WIDGET_ANIM_ICON_NIGHT
val diffTime = (System.currentTimeMillis() - time) / 1000
OsmandFormatter.getFormattedDurationShort(diffTime)
OsmandFormatter.getFormattedDurationForWidget(diffTime)
}
time == 0L && isSending -> {
iconDay = STATUS_WIDGET_ANIM_ICON_DAY
@ -223,11 +223,17 @@ class ShowLocationHelper(private val app: TelegramApplication) {
else -> {
iconDay = STATUS_WIDGET_OFF_ICON_DAY
iconNight = STATUS_WIDGET_OFF_ICON_NIGHT
app.getString(R.string.shared_string_off)
app.getString(R.string.shared_string_start)
}
}
val subText = when {
time > 0 -> app.getString(R.string.shared_string_minute_short)
time > 0 -> {
if (text.length > 2) {
app.getString(R.string.shared_string_hour_short)
} else {
app.getString(R.string.shared_string_minute_short)
}
}
else -> ""
}
osmandAidlHelper.addMapWidget(

View file

@ -45,14 +45,16 @@ object OsmandFormatter {
fixed2.minimumIntegerDigits = 1
}
fun getFormattedDurationShort(seconds: Long): String {
fun getFormattedDurationForWidget(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)
}
return when {
hours > 9 -> String.format("%10d:%01d", hours, minutes)
hours > 0 -> String.format("%1d:%01d", hours, minutes)
minutes > 9 -> String.format("%11d", minutes)
minutes > 0 -> String.format("%1d", minutes)
else -> "1"
}.trim()
}
fun getFormattedDuration(ctx: Context, seconds: Long, short: Boolean = false): String {