OsmAnd/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt

137 lines
4.2 KiB
Kotlin
Raw Normal View History

package net.osmand.telegram.helpers
2018-06-14 20:01:10 +02:00
import android.content.Intent
import android.graphics.Color
2018-06-14 20:01:10 +02:00
import android.net.Uri
import android.text.TextUtils
import net.osmand.aidl.map.ALatLon
2018-06-14 20:01:10 +02:00
import net.osmand.aidl.maplayer.point.AMapPoint
import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.helpers.TelegramHelper.MessageOsmAndBotLocation
2018-06-14 20:01:10 +02:00
import net.osmand.telegram.utils.AndroidUtils
import org.drinkless.td.libcore.telegram.TdApi
2018-06-14 20:01:10 +02:00
import java.io.File
private const val MAP_LAYER_ID = "telegram_layer"
class ShowLocationHelper(private val app: TelegramApplication) {
2018-06-11 18:57:33 +02:00
private val telegramHelper = app.telegramHelper
private val osmandAidlHelper = app.osmandAidlHelper
2018-06-11 18:57:33 +02:00
var showingLocation: Boolean = false
private set
fun setupMapLayer() {
execOsmandApi {
osmandAidlHelper.addMapLayer(MAP_LAYER_ID, "Telegram", 5.5f, null)
}
2018-06-11 18:57:33 +02:00
}
fun updateLocationsOnMap() {
execOsmandApi {
val messages = telegramHelper.getMessages()
for (message in messages) {
val chatTitle = telegramHelper.getChat(message.chatId)?.title
val date = Math.max(message.date, message.editDate) * 1000L
val expired = System.currentTimeMillis() - date > app.settings.userLocationExpireTime
if (chatTitle != null && expired) {
removeMapPoint(chatTitle, message)
}
}
}
}
fun showLocationOnMap(message: TdApi.Message) {
execOsmandApi {
val chatTitle = telegramHelper.getChat(message.chatId)?.title
2018-06-11 18:57:33 +02:00
val content = message.content
if (chatTitle != null && content is TdApi.MessageLocation) {
2018-06-11 18:57:33 +02:00
var userName = ""
2018-06-14 20:01:10 +02:00
var photoUri: Uri? = null
2018-06-11 18:57:33 +02:00
val user = telegramHelper.getUser(message.senderUserId)
if (user != null) {
userName = "${user.firstName} ${user.lastName}".trim()
if (userName.isEmpty()) {
userName = user.username
}
if (userName.isEmpty()) {
userName = user.phoneNumber
}
2018-06-14 20:01:10 +02:00
val photoPath = telegramHelper.getUserPhotoPath(user)
if (!TextUtils.isEmpty(photoPath)) {
photoUri = AndroidUtils.getUriForFile(app, File(photoPath))
app.grantUriPermission(OsmandAidlHelper.OSMAND_PACKAGE_NAME, photoUri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
2018-06-14 20:01:10 +02:00
}
2018-06-11 18:57:33 +02:00
}
if (userName.isEmpty()) {
userName = message.senderUserId.toString()
}
setupMapLayer()
2018-06-14 20:01:10 +02:00
val params = mutableMapOf<String, String>()
if (photoUri != null) {
params[AMapPoint.POINT_IMAGE_URI_PARAM] = photoUri.toString()
}
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatTitle}_${message.senderUserId}", userName, userName,
2018-06-14 20:01:10 +02:00
chatTitle, Color.RED, ALatLon(content.location.latitude, content.location.longitude), null, params)
} else if (chatTitle != null && content is MessageOsmAndBotLocation && content.isValid()) {
val name = content.name
setupMapLayer()
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatTitle}_$name", name, name,
chatTitle, Color.RED, ALatLon(content.lat, content.lon), null, null)
2018-06-11 18:57:33 +02:00
}
}
}
2018-06-11 18:57:33 +02:00
fun showChatMessages(chatTitle: String) {
execOsmandApi {
2018-06-11 18:57:33 +02:00
val messages = telegramHelper.getChatMessages(chatTitle)
for (message in messages) {
showLocationOnMap(message)
2018-06-11 18:57:33 +02:00
}
}
}
2018-06-11 18:57:33 +02:00
fun hideChatMessages(chatTitle: String) {
execOsmandApi {
2018-06-11 18:57:33 +02:00
val messages = telegramHelper.getChatMessages(chatTitle)
for (message in messages) {
val user = telegramHelper.getUser(message.senderUserId)
if (user != null) {
removeMapPoint(chatTitle, message)
2018-06-11 18:57:33 +02:00
}
}
}
}
2018-06-11 18:57:33 +02:00
fun startShowingLocation() {
if (!showingLocation) {
showingLocation = true
app.startUserLocationService()
}
}
fun stopShowingLocation() {
if (showingLocation) {
showingLocation = false
app.stopUserLocationService()
}
}
private fun removeMapPoint(chatTitle: String, message: TdApi.Message) {
val content = message.content
if (content is TdApi.MessageLocation) {
osmandAidlHelper.removeMapPoint(MAP_LAYER_ID, "${chatTitle}_${message.senderUserId}")
} else if (content is MessageOsmAndBotLocation) {
osmandAidlHelper.removeMapPoint(MAP_LAYER_ID, "${chatTitle}_${content.name}")
}
}
private fun execOsmandApi(action: (() -> Unit)) {
if (osmandAidlHelper.isOsmandConnected()) {
action.invoke()
} else if (osmandAidlHelper.isOsmandBound()) {
osmandAidlHelper.connectOsmand()
}
}
}