From 519b2d2073c14ec030ed6333bbd814c8da7ae3cf Mon Sep 17 00:00:00 2001 From: Dmitriy Ruban Date: Tue, 24 Dec 2019 17:06:46 +0200 Subject: [PATCH] tracker status widget --- .../osmand/telegram/TelegramApplication.kt | 1 + .../net/osmand/telegram/TelegramService.kt | 22 +++++++++++-- .../telegram/helpers/ShowLocationHelper.kt | 31 +++++++++++++------ .../anim_widget_location_sharing_day.xml | 10 ++++++ .../anim_widget_location_sharing_night.xml | 10 ++++++ .../plus/views/mapwidgets/TextInfoWidget.java | 5 +++ 6 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 OsmAnd/res/drawable/anim_widget_location_sharing_day.xml create mode 100644 OsmAnd/res/drawable/anim_widget_location_sharing_night.xml diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt index 7cd50a96c0..4dc883bed3 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt @@ -56,6 +56,7 @@ class TelegramApplication : Application(), OsmandHelperListener { ) showLocationHelper.addDirectionContextMenuButton() showLocationHelper.startShowingLocation() + showLocationHelper.addOrUpdateStatusWidget(-1, false) } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt index ba38c0f10f..cc1e7819ea 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt @@ -20,6 +20,7 @@ import net.osmand.telegram.utils.AndroidUtils import org.drinkless.td.libcore.telegram.TdApi import java.util.* +private const val UPDATE_WIDGET_INTERVAL_MS = 1000L // 1 sec private const val UPDATE_LIVE_MESSAGES_INTERVAL_MS = 10000L // 10 sec private const val UPDATE_LIVE_TRACKS_INTERVAL_MS = 30000L // 30 sec @@ -36,6 +37,9 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis private var updateTracksHandler: Handler? = null private var tracksHandlerThread = HandlerThread("TracksUpdateServiceThread") + private var updateWidgetHandler: Handler? = null + private var updateWidgetThread = HandlerThread("WidgetUpdateServiceThread") + var handler: Handler? = null private set var usedBy = 0 @@ -58,8 +62,10 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis super.onCreate() mHandlerThread.start() tracksHandlerThread.start() + updateWidgetThread.start() updateShareInfoHandler = Handler(mHandlerThread.looper) updateTracksHandler = Handler(tracksHandlerThread.looper) + updateWidgetHandler = Handler(updateWidgetThread.looper) } override fun onBind(intent: Intent): IBinder? { @@ -106,6 +112,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis if (isUsedByMyLocation(usedBy)) { initLocationUpdates() startShareInfoUpdates() + startWidgetUpdates() } if (isUsedByUsersLocations(usedBy)) { app.telegramHelper.startLiveMessagesUpdates(app.settings.sendMyLocInterval) @@ -139,6 +146,8 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis app.telegramService = null tracksHandlerThread.quit() mHandlerThread.quit() + updateWidgetThread.quit() + app().showLocationHelper.addOrUpdateStatusWidget(-1, false) usedBy = 0 @@ -154,7 +163,6 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis if (shouldCleanupResources) { app.cleanupResources() } - app().showLocationHelper.updateStatusWidget(-1) // remove notification stopForeground(java.lang.Boolean.TRUE) @@ -195,7 +203,6 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis updateShareInfoHandler?.postDelayed({ if (isUsedByMyLocation(usedBy)) { app().shareLocationHelper.updateSendLiveMessages() - app().showLocationHelper.updateStatusWidget(app().locationMessages.getBufferedMessagesCount()) startShareInfoUpdates() } }, UPDATE_LIVE_MESSAGES_INTERVAL_MS) @@ -211,6 +218,17 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis } }, UPDATE_LIVE_TRACKS_INTERVAL_MS) } + + private fun startWidgetUpdates() { + 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.getBufferedMessagesCount(), isSending) + } + startWidgetUpdates() + }, UPDATE_WIDGET_INTERVAL_MS) + } @SuppressLint("MissingPermission") private fun getFirstTimeRunDefaultLocation(): net.osmand.Location? { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index c56545b72e..9ffd426714 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -44,9 +44,11 @@ class ShowLocationHelper(private val app: TelegramApplication) { const val GPX_COLORS_COUNT = 10 private const val STATUS_WIDGET_ID = "status_widget" - private const val STATUS_WIDGET_MENU_ICON = "ic_action_speed" - private const val STATUS_WIDGET_DAY_ICON = "widget_speed_day" - private const val STATUS_WIDGET_NIGHT_ICON = "widget_speed_night" + private const val STATUS_WIDGET_MENU_ICON = "widget_location_sharing_night" + private const val STATUS_WIDGET_ANIM_ICON_DAY = "anim_widget_location_sharing_day" + private const val STATUS_WIDGET_ANIM_ICON_NIGHT = "anim_widget_location_sharing_night" + private const val STATUS_WIDGET_OFF_ICON_DAY = "widget_location_sharing_off_day" + private const val STATUS_WIDGET_OFF_ICON_NIGHT = "widget_location_sharing_off_night" val GPX_COLORS = arrayOf( "red", "orange", "lightblue", "blue", "purple", @@ -203,22 +205,31 @@ class ShowLocationHelper(private val app: TelegramApplication) { } } - fun updateStatusWidget(count: Int) { + fun addOrUpdateStatusWidget(count: Int, isSending: Boolean) { + val iconDay: String + val iconNight: String val text = when { - count > 0 -> count.toString() - count == 0 -> "ON" - else -> "OFF" + count >= 0 && isSending -> { + iconDay = STATUS_WIDGET_ANIM_ICON_DAY + iconNight = STATUS_WIDGET_ANIM_ICON_NIGHT + app.getString(R.string.shared_string_ok) + } + else -> { + iconDay = STATUS_WIDGET_OFF_ICON_DAY + iconNight = STATUS_WIDGET_OFF_ICON_NIGHT + app.getString(R.string.shared_string_off) + } } val bufferText = when { - count > 0 -> count.toString() + count > 0 -> "($count)" else -> "" } osmandAidlHelper.addMapWidget( STATUS_WIDGET_ID, STATUS_WIDGET_MENU_ICON, app.getString(R.string.status_widget_title), - STATUS_WIDGET_DAY_ICON, - STATUS_WIDGET_NIGHT_ICON, + iconDay, + iconNight, text, bufferText, 50, getStatusWidgetIntent()) } diff --git a/OsmAnd/res/drawable/anim_widget_location_sharing_day.xml b/OsmAnd/res/drawable/anim_widget_location_sharing_day.xml new file mode 100644 index 0000000000..d7f847dc50 --- /dev/null +++ b/OsmAnd/res/drawable/anim_widget_location_sharing_day.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/OsmAnd/res/drawable/anim_widget_location_sharing_night.xml b/OsmAnd/res/drawable/anim_widget_location_sharing_night.xml new file mode 100644 index 0000000000..8d5ade60f0 --- /dev/null +++ b/OsmAnd/res/drawable/anim_widget_location_sharing_night.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/TextInfoWidget.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/TextInfoWidget.java index ec29bca7ea..d32166e041 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/TextInfoWidget.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/TextInfoWidget.java @@ -3,6 +3,7 @@ package net.osmand.plus.views.mapwidgets; import android.app.Activity; import android.graphics.Paint.Style; import android.graphics.Typeface; +import android.graphics.drawable.AnimationDrawable; import android.graphics.drawable.Drawable; import android.text.TextUtils; import android.view.Gravity; @@ -71,6 +72,10 @@ public class TextInfoWidget { public void setImageDrawable(Drawable imageDrawable, boolean gone) { if(imageDrawable != null) { imageView.setImageDrawable(imageDrawable); + Object anim = imageView.getDrawable(); + if (anim instanceof AnimationDrawable) { + ((AnimationDrawable) anim).start(); + } imageView.setVisibility(View.VISIBLE); } else { imageView.setVisibility(gone ? View.GONE : View.INVISIBLE);