Add colors to live tracks and add group name to gpx file name

(cherry picked from commit 960a231201)
This commit is contained in:
Chumva 2019-07-19 13:04:57 +03:00
parent 0d770e4f8a
commit 550a2fb433
4 changed files with 48 additions and 23 deletions

View file

@ -9,6 +9,7 @@ import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import net.osmand.PlatformUtil
import net.osmand.telegram.helpers.OsmandAidlHelper
import net.osmand.telegram.helpers.ShowLocationHelper
import net.osmand.telegram.helpers.TelegramHelper
import net.osmand.telegram.utils.AndroidUtils
import net.osmand.telegram.utils.OsmandApiUtils
@ -112,7 +113,7 @@ class TelegramSettings(private val app: TelegramApplication) {
private var shareChatsInfo = ConcurrentHashMap<Long, ShareChatInfo>()
private var hiddenOnMapChats: Set<Long> = emptySet()
private var shareDevices: Set<DeviceBot> = emptySet()
private var liveTracksInfo: Set<LiveTrackInfo> = emptySet()
private var liveTracksInfo = emptyList<LiveTrackInfo>()
var sharingStatusChanges = ConcurrentLinkedQueue<SharingStatus>()
@ -177,6 +178,9 @@ class TelegramSettings(private val app: TelegramApplication) {
fun getLiveTracksInfo() = liveTracksInfo
fun getLiveTrackInfo(userId: Int, chatId: Long, deviceName: String) =
liveTracksInfo.firstOrNull { it.userId == userId && it.chatId == chatId && it.deviceName == deviceName }
fun isShowingChatOnMap(chatId: Long) = !hiddenOnMapChats.contains(chatId)
fun isLiveTrackEnabled(userId: Int, chatId: Long, deviceName: String) =
@ -185,11 +189,12 @@ class TelegramSettings(private val app: TelegramApplication) {
fun updateLiveTrack(userId: Int, chatId: Long, deviceName: String, enable: Boolean) {
val tracksInfo = liveTracksInfo.toMutableList()
if (enable) {
tracksInfo.add(LiveTrackInfo(userId, chatId, deviceName))
val colorIndex = if (tracksInfo.size > 0) (tracksInfo.last().colorIndex + 1) % ShowLocationHelper.GPX_COLORS_COUNT else 0
tracksInfo.add(LiveTrackInfo(userId, chatId, deviceName, colorIndex))
} else {
tracksInfo.remove(LiveTrackInfo(userId, chatId, deviceName))
tracksInfo.removeAll { it.userId == userId && it.chatId == chatId && it.deviceName == deviceName }
}
liveTracksInfo = tracksInfo.toHashSet()
liveTracksInfo = tracksInfo.toList()
}
fun removeNonexistingChats(presentChatIds: List<Long>) {
@ -747,6 +752,7 @@ class TelegramSettings(private val app: TelegramApplication) {
obj.put(LiveTrackInfo.USER_ID, liveTrackInfo.userId)
obj.put(LiveTrackInfo.CHAT_ID, liveTrackInfo.chatId)
obj.put(LiveTrackInfo.DEVICE_NAME, liveTrackInfo.deviceName)
obj.put(LiveTrackInfo.COLOR_INDEX, liveTrackInfo.colorIndex)
put(obj)
}
}
@ -863,10 +869,11 @@ class TelegramSettings(private val app: TelegramApplication) {
val userId = obj.optInt(LiveTrackInfo.USER_ID)
val chatId = obj.optLong(LiveTrackInfo.CHAT_ID)
val deviceName = obj.optString(LiveTrackInfo.DEVICE_NAME)
val colorIndex = obj.optInt(LiveTrackInfo.COLOR_INDEX)
list.add(LiveTrackInfo(userId, chatId, deviceName))
list.add(LiveTrackInfo(userId, chatId, deviceName, colorIndex))
}
liveTracksInfo = list.toHashSet()
liveTracksInfo = list.toList()
}
private fun parseShareDevices(json: String) {
@ -1192,12 +1199,13 @@ class TelegramSettings(private val app: TelegramApplication) {
}
}
data class LiveTrackInfo(val userId: Int, val chatId: Long, val deviceName: String) {
data class LiveTrackInfo(val userId: Int, val chatId: Long, val deviceName: String, val colorIndex: Int) {
companion object {
internal const val USER_ID = "userId"
internal const val CHAT_ID = "chatId"
internal const val DEVICE_NAME = "deviceName"
internal const val COLOR_INDEX = "colorIndex"
}
}

View file

@ -69,7 +69,7 @@ class LocationMessages(val app: TelegramApplication) {
}
fun getMessagesForUserInChat(userId: Int, chatId: Long, deviceName: String, start: Long, end: Long): List<LocationMessage> {
return dbHelper.getMessagesForUserInChat(userId, chatId,deviceName, start, end)
return dbHelper.getMessagesForUserInChat(userId, chatId, deviceName, start, end)
}
fun getMessagesForUser(userId: Int, start: Long, end: Long): List<LocationMessage> {

View file

@ -39,6 +39,14 @@ class ShowLocationHelper(private val app: TelegramApplication) {
const val DIRECTION_ICON_ID = "ic_action_start_navigation"
const val LIVE_TRACKS_DIR = "livetracks"
const val GPX_COLORS_COUNT = 10
val GPX_COLORS = arrayOf(
"red", "orange", "lightblue", "blue", "purple",
"translucent_red", "translucent_orange", "translucent_lightblue",
"translucent_blue", "translucent_purple"
)
}
private val telegramHelper = app.telegramHelper
@ -271,7 +279,8 @@ class ShowLocationHelper(private val app: TelegramApplication) {
log.debug("LiveTracks onSavingGpxFinish $path time ${startTime - System.currentTimeMillis()}")
val uri = AndroidUtils.getUriForFile(app, File(path))
val destinationPath = "$LIVE_TRACKS_DIR/${it.metadata.name}.gpx"
osmandAidlHelper.importGpxFromUri(uri, destinationPath, GPXUtilities.GPXColor.AQUA.name, true)
val color = it.extensionsToRead["color"] ?: ""
osmandAidlHelper.importGpxFromUri(uri, destinationPath, color, true)
log.debug("LiveTracks importGpxFromUri finish time ${startTime - System.currentTimeMillis()}")
}

View file

@ -8,6 +8,7 @@ import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.helpers.LocationMessages
import net.osmand.telegram.helpers.LocationMessages.BufferMessage
import net.osmand.telegram.helpers.LocationMessages.LocationMessage
import net.osmand.telegram.helpers.ShowLocationHelper
import net.osmand.telegram.helpers.TelegramHelper
import net.osmand.telegram.helpers.TelegramUiHelper
import net.osmand.util.GeoPointParserUtil
@ -420,6 +421,7 @@ object OsmandLocationUtils {
var previousTime: Long = -1
var previousChatId: Long = -1
var previousUserId = -1
var previousDeviceName = ""
var segment: GPXUtilities.TrkSegment? = null
var track: GPXUtilities.Track? = null
var gpx: GPXUtilities.GPXFile? = null
@ -428,15 +430,16 @@ object OsmandLocationUtils {
items.forEach {
val userId = it.userId
val chatId = it.chatId
val deviceName = it.deviceName
val time = it.time
if (previousTime >= time) {
return@forEach
}
countedLocations++
if (previousUserId != userId || (newGpxPerChat && previousChatId != chatId)) {
if (previousUserId != userId || previousDeviceName != deviceName || (newGpxPerChat && previousChatId != chatId)) {
gpx = GPXUtilities.GPXFile(app.packageName).apply {
metadata = GPXUtilities.Metadata().apply {
name = getGpxFileNameForUserId(app, userId, time)
name = getGpxFileNameForUserId(app, userId, chatId, time)
}
val colorIndex = app.settings.getLiveTrackInfo(userId, chatId, deviceName)?.colorIndex ?: -1
if (colorIndex != -1) {
extensionsToWrite["color"] = ShowLocationHelper.GPX_COLORS[colorIndex]
}
}
previousTime = 0
@ -444,6 +447,10 @@ object OsmandLocationUtils {
segment = null
dataTracks.add(gpx!!)
}
if (previousTime >= time) {
return@forEach
}
countedLocations++
val pt = GPXUtilities.WptPt()
pt.lat = it.lat
pt.lon = it.lon
@ -472,6 +479,7 @@ object OsmandLocationUtils {
previousTime = time
previousUserId = userId
previousChatId = chatId
previousDeviceName = deviceName
}
return dataTracks
@ -485,15 +493,15 @@ object OsmandLocationUtils {
}
}
fun getGpxFileNameForUserId(app: TelegramApplication, userId: Int, time: Long): String {
fun getGpxFileNameForUserId(app: TelegramApplication, userId: Int, chatId: Long, time: Long): String {
var userName = userId.toString()
try {
val user = app.telegramHelper.getUser(userId)
if (user != null) {
userName = TelegramUiHelper.getUserName(user)
}
} catch (e: NumberFormatException) {
//ignore
val user = app.telegramHelper.getUser(userId)
if (user != null) {
userName = TelegramUiHelper.getUserName(user)
}
val chat = app.telegramHelper.getChat(chatId)
if (chat != null && app.telegramHelper.isGroup(chat)) {
return "${userName}_${chat.title}_${UTC_DATE_FORMAT.format(Date(time))}"
}
return "${userName}_${UTC_DATE_FORMAT.format(Date(time))}"
}