tracker status widget

This commit is contained in:
Dmitriy Ruban 2019-12-24 17:06:46 +02:00
parent 4beb3de098
commit 519b2d2073
6 changed files with 67 additions and 12 deletions

View file

@ -56,6 +56,7 @@ class TelegramApplication : Application(), OsmandHelperListener {
)
showLocationHelper.addDirectionContextMenuButton()
showLocationHelper.startShowingLocation()
showLocationHelper.addOrUpdateStatusWidget(-1, false)
}
}
}

View file

@ -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)
@ -212,6 +219,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? {
val app = app()

View file

@ -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())
}

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/widget_location_sharing_day"
android:duration="500" />
<item
android:drawable="@drawable/widget_location_sharing_off_day"
android:duration="500" />
</animation-list>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/widget_location_sharing_night"
android:duration="500" />
<item
android:drawable="@drawable/widget_location_sharing_off_night"
android:duration="500" />
</animation-list>

View file

@ -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);