2018-06-11 12:41:43 +02:00
|
|
|
package net.osmand.telegram.helpers
|
|
|
|
|
2018-06-14 20:01:10 +02:00
|
|
|
import android.content.Intent
|
2018-06-11 12:41:43 +02:00
|
|
|
import android.graphics.Color
|
2018-06-14 20:01:10 +02:00
|
|
|
import android.net.Uri
|
|
|
|
import android.text.TextUtils
|
2018-06-11 12:41:43 +02:00
|
|
|
import net.osmand.aidl.map.ALatLon
|
2018-06-14 20:01:10 +02:00
|
|
|
import net.osmand.aidl.maplayer.point.AMapPoint
|
2018-06-11 12:41:43 +02:00
|
|
|
import net.osmand.telegram.TelegramApplication
|
2018-06-14 20:01:10 +02:00
|
|
|
import net.osmand.telegram.utils.AndroidUtils
|
2018-06-11 12:41:43 +02:00
|
|
|
import org.drinkless.td.libcore.telegram.TdApi
|
2018-06-14 20:01:10 +02:00
|
|
|
import java.io.File
|
2018-06-11 12:41:43 +02:00
|
|
|
|
|
|
|
class ShowLocationHelper(private val app: TelegramApplication) {
|
|
|
|
|
2018-06-11 18:57:33 +02:00
|
|
|
companion object {
|
|
|
|
private const val MAP_LAYER_ID = "telegram_layer"
|
|
|
|
}
|
2018-06-11 12:41:43 +02:00
|
|
|
|
2018-06-11 18:57:33 +02:00
|
|
|
private val telegramHelper = app.telegramHelper
|
|
|
|
private val osmandHelper = app.osmandHelper
|
2018-06-11 12:41:43 +02:00
|
|
|
|
2018-06-11 18:57:33 +02:00
|
|
|
var showingLocation: Boolean = false
|
|
|
|
private set
|
2018-06-11 12:41:43 +02:00
|
|
|
|
2018-06-11 20:22:07 +02:00
|
|
|
fun setupMapLayer() {
|
2018-06-16 13:55:14 +02:00
|
|
|
execOsmandApi {
|
|
|
|
osmandHelper.addMapLayer(MAP_LAYER_ID, "Telegram", 5.5f, null)
|
|
|
|
}
|
2018-06-11 18:57:33 +02:00
|
|
|
}
|
2018-06-11 12:41:43 +02:00
|
|
|
|
2018-06-16 13:55:14 +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 && message.content is TdApi.MessageLocation && expired) {
|
|
|
|
osmandHelper.removeMapPoint(MAP_LAYER_ID, "${chatTitle}_${message.senderUserId}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-06-16 13:55:14 +02:00
|
|
|
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))
|
2018-06-16 13:55:14 +02:00
|
|
|
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()
|
|
|
|
}
|
2018-06-11 20:22:07 +02:00
|
|
|
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()
|
|
|
|
}
|
2018-06-11 18:57:33 +02:00
|
|
|
osmandHelper.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)
|
2018-06-11 18:57:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 12:41:43 +02:00
|
|
|
|
2018-06-11 18:57:33 +02:00
|
|
|
fun showChatMessages(chatTitle: String) {
|
2018-06-16 13:55:14 +02:00
|
|
|
execOsmandApi {
|
2018-06-11 18:57:33 +02:00
|
|
|
val messages = telegramHelper.getChatMessages(chatTitle)
|
|
|
|
for (message in messages) {
|
2018-06-16 13:55:14 +02:00
|
|
|
showLocationOnMap(message)
|
2018-06-11 18:57:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 12:41:43 +02:00
|
|
|
|
2018-06-11 18:57:33 +02:00
|
|
|
fun hideChatMessages(chatTitle: String) {
|
2018-06-16 13:55:14 +02:00
|
|
|
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) {
|
|
|
|
osmandHelper.removeMapPoint(MAP_LAYER_ID, "${chatTitle}_${message.senderUserId}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 12:41:43 +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()
|
|
|
|
}
|
|
|
|
}
|
2018-06-16 13:55:14 +02:00
|
|
|
|
|
|
|
private fun execOsmandApi(action: (() -> Unit)) {
|
|
|
|
if (osmandHelper.isOsmandConnected()) {
|
|
|
|
action.invoke()
|
|
|
|
} else if (osmandHelper.isOsmandBound()) {
|
|
|
|
osmandHelper.connectOsmand()
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 12:41:43 +02:00
|
|
|
}
|