From 42d5cc759f8a0c3f4cff7d0eef4157f572214ae6 Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Wed, 12 Feb 2020 14:14:42 +0200 Subject: [PATCH] status widget blink --- .../telegram/helpers/ShareLocationHelper.kt | 1 + .../telegram/helpers/ShowLocationHelper.kt | 66 +++++++++++++++---- 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index b23deb2a4b..8aa7d29856 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -83,6 +83,7 @@ class ShareLocationHelper(private val app: TelegramApplication) { lastLocationUpdateTime = System.currentTimeMillis() lastLocation = location if (app.settings.getChatsShareInfo().isNotEmpty()) { + app.showLocationHelper.shouldBlinkWidget = true shareLocationMessages(location, app.telegramHelper.getCurrentUserId()) } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index b6fe245d0a..16469f069c 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -74,6 +74,8 @@ class ShowLocationHelper(private val app: TelegramApplication) { private var forcedStop: Boolean = false + var shouldBlinkWidget: Boolean = false + init { app.osmandAidlHelper.addConnectionListener(object : OsmandAidlHelper.OsmandHelperListener { override fun onOsmandConnectionStateChanged(connected: Boolean) { @@ -222,8 +224,10 @@ class ShowLocationHelper(private val app: TelegramApplication) { } fun addOrUpdateStatusWidget(time: Long, isSending: Boolean) { + var noSubText = false var iconDay: String var iconNight: String + val diffTime = (System.currentTimeMillis() - time) / 1000 val menuIcon = if (isOsmandHasStatusWidgetIcon()) { STATUS_WIDGET_MENU_ICON } else { @@ -233,14 +237,17 @@ class ShowLocationHelper(private val app: TelegramApplication) { time > 0L && isSending -> { iconDay = STATUS_WIDGET_ON_ICON_DAY iconNight = STATUS_WIDGET_ON_ICON_NIGHT - val diffTime = (System.currentTimeMillis() - time) / 1000 OsmandFormatter.getFormattedDurationForWidget(diffTime) } time > 0L && !isSending -> { iconDay = STATUS_WIDGET_ICON_DAY iconNight = STATUS_WIDGET_ICON_NIGHT - val diffTime = (System.currentTimeMillis() - time) / 1000 - OsmandFormatter.getFormattedDurationForWidget(diffTime) + if (diffTime >= 2 * 60) { + OsmandFormatter.getFormattedDurationForWidget(diffTime) + } else { + noSubText = true + app.getString(R.string.shared_string_error_short) + } } time == 0L && isSending -> { iconDay = STATUS_WIDGET_ON_ICON_DAY @@ -263,7 +270,7 @@ class ShowLocationHelper(private val app: TelegramApplication) { iconNight = STATUS_WIDGET_ICON_OLD } val subText = when { - time > 0 -> { + time > 0 && !noSubText -> { if (text.length > 2) { app.getString(R.string.shared_string_hour_short) } else { @@ -272,13 +279,18 @@ class ShowLocationHelper(private val app: TelegramApplication) { } else -> "" } - osmandAidlHelper.addMapWidget( - STATUS_WIDGET_ID, - menuIcon, - app.getString(R.string.status_widget_title), - iconDay, - iconNight, - text, subText, 50, getStatusWidgetIntent()) + if (shouldBlinkWidget && isSending && isOsmandHasStatusWidgetIcon()) { + BlinkWidgetTask(app, menuIcon, text, subText, getStatusWidgetIntent()).executeOnExecutor(executor) + shouldBlinkWidget = false + } else { + osmandAidlHelper.addMapWidget( + STATUS_WIDGET_ID, + menuIcon, + app.getString(R.string.status_widget_title), + iconDay, + iconNight, + text, subText, 50, getStatusWidgetIntent()) + } } private fun getStatusWidgetIntent(): Intent { @@ -545,6 +557,38 @@ class ShowLocationHelper(private val app: TelegramApplication) { } } + private class BlinkWidgetTask(private val app: TelegramApplication, private val menuIcon: String, + private val text: String, private val subText: String, + private val intent: Intent) : AsyncTask() { + + override fun onPreExecute() { + super.onPreExecute() + app.osmandAidlHelper.addMapWidget( + STATUS_WIDGET_ID, + menuIcon, + app.getString(R.string.status_widget_title), + STATUS_WIDGET_OFF_ICON_DAY, + STATUS_WIDGET_OFF_ICON_NIGHT, + text, subText, 50, intent) + } + + override fun doInBackground(vararg params: Void?): Void? { + Thread.sleep(300) + return null + } + + override fun onPostExecute(result: Void?) { + super.onPostExecute(result) + app.osmandAidlHelper.addMapWidget( + STATUS_WIDGET_ID, + menuIcon, + app.getString(R.string.status_widget_title), + STATUS_WIDGET_ON_ICON_DAY, + STATUS_WIDGET_ON_ICON_NIGHT, + text, subText, 50, intent) + } + } + private fun generatePointDetails(bearing: Float?, altitude: Float?, precision: Float?): List { val details = mutableListOf() if (bearing != null && bearing != 0.0f) {