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.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-07-04 11:24:50 +02:00
|
|
|
import net.osmand.telegram.helpers.TelegramHelper.MessageOsmAndBotLocation
|
2018-07-11 13:32:02 +02:00
|
|
|
import net.osmand.telegram.helpers.TelegramUiHelper.ListItem
|
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
|
|
|
|
2018-06-27 10:06:24 +02:00
|
|
|
class ShowLocationHelper(private val app: TelegramApplication) {
|
2018-06-11 12:41:43 +02:00
|
|
|
|
2018-07-10 16:26:55 +02:00
|
|
|
companion object {
|
|
|
|
const val MAP_LAYER_ID = "telegram_layer"
|
|
|
|
}
|
|
|
|
|
2018-06-11 18:57:33 +02:00
|
|
|
private val telegramHelper = app.telegramHelper
|
2018-07-09 12:59:30 +02:00
|
|
|
private val osmandAidlHelper = app.osmandAidlHelper
|
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 {
|
2018-07-09 12:59:30 +02:00
|
|
|
osmandAidlHelper.addMapLayer(MAP_LAYER_ID, "Telegram", 5.5f, null)
|
2018-06-16 13:55:14 +02:00
|
|
|
}
|
2018-06-11 18:57:33 +02:00
|
|
|
}
|
2018-06-11 12:41:43 +02:00
|
|
|
|
2018-07-11 13:32:02 +02:00
|
|
|
fun showLocationOnMap(item: ListItem) {
|
|
|
|
if (item.latLon == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
execOsmandApi {
|
|
|
|
osmandAidlHelper.showMapPoint(
|
|
|
|
MAP_LAYER_ID,
|
|
|
|
item.getMapPointId(),
|
|
|
|
item.getVisibleName(),
|
|
|
|
item.getVisibleName(),
|
|
|
|
item.chatTitle,
|
|
|
|
Color.RED,
|
|
|
|
ALatLon(item.latLon!!.latitude, item.latLon!!.longitude),
|
|
|
|
null,
|
|
|
|
generatePhotoParams(item.photoPath)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 13:55:14 +02:00
|
|
|
fun updateLocationsOnMap() {
|
|
|
|
execOsmandApi {
|
|
|
|
val messages = telegramHelper.getMessages()
|
|
|
|
for (message in messages) {
|
|
|
|
val date = Math.max(message.date, message.editDate) * 1000L
|
2018-08-10 12:16:46 +02:00
|
|
|
val expired = System.currentTimeMillis() - date > app.settings.locHistoryTime
|
2018-07-13 13:02:59 +02:00
|
|
|
if (expired) {
|
2018-07-13 13:42:57 +02:00
|
|
|
removeMapPoint(message.chatId, message)
|
2018-06-16 13:55:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 12:04:45 +02:00
|
|
|
fun addLocationToMap(message: TdApi.Message) {
|
2018-06-16 13:55:14 +02:00
|
|
|
execOsmandApi {
|
2018-07-13 13:02:59 +02:00
|
|
|
val chatId = message.chatId
|
2018-06-16 13:55:14 +02:00
|
|
|
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-07-11 13:32:02 +02:00
|
|
|
var photoPath: String? = 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-07-11 13:32:02 +02:00
|
|
|
photoPath = telegramHelper.getUserPhotoPath(user)
|
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-07-11 13:32:02 +02:00
|
|
|
val params = generatePhotoParams(photoPath)
|
2018-07-13 13:02:59 +02:00
|
|
|
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatId}_${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-07-04 11:24:50 +02:00
|
|
|
} else if (chatTitle != null && content is MessageOsmAndBotLocation && content.isValid()) {
|
|
|
|
val name = content.name
|
|
|
|
setupMapLayer()
|
2018-07-13 13:02:59 +02:00
|
|
|
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatId}_$name", name, name,
|
2018-07-04 11:24:50 +02:00
|
|
|
chatTitle, Color.RED, ALatLon(content.lat, content.lon), null, null)
|
2018-06-11 18:57:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 12:41:43 +02:00
|
|
|
|
2018-07-13 13:02:59 +02:00
|
|
|
fun showChatMessages(chatId: Long) {
|
2018-06-16 13:55:14 +02:00
|
|
|
execOsmandApi {
|
2018-07-13 13:02:59 +02:00
|
|
|
val messages = telegramHelper.getChatMessages(chatId)
|
2018-06-11 18:57:33 +02:00
|
|
|
for (message in messages) {
|
2018-07-11 12:04:45 +02:00
|
|
|
addLocationToMap(message)
|
2018-06-11 18:57:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 12:41:43 +02:00
|
|
|
|
2018-07-13 13:02:59 +02:00
|
|
|
fun hideChatMessages(chatId: Long) {
|
2018-08-02 16:30:59 +02:00
|
|
|
hideMessages(telegramHelper.getChatMessages(chatId))
|
|
|
|
}
|
|
|
|
|
|
|
|
fun hideMessages(messages: List<TdApi.Message>) {
|
2018-06-16 13:55:14 +02:00
|
|
|
execOsmandApi {
|
2018-06-11 18:57:33 +02:00
|
|
|
for (message in messages) {
|
|
|
|
val user = telegramHelper.getUser(message.senderUserId)
|
|
|
|
if (user != null) {
|
2018-08-02 16:30:59 +02:00
|
|
|
removeMapPoint(message.chatId, 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 startShowingLocation() {
|
|
|
|
if (!showingLocation) {
|
|
|
|
showingLocation = true
|
|
|
|
app.startUserLocationService()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun stopShowingLocation() {
|
|
|
|
if (showingLocation) {
|
|
|
|
showingLocation = false
|
|
|
|
app.stopUserLocationService()
|
|
|
|
}
|
|
|
|
}
|
2018-06-16 13:55:14 +02:00
|
|
|
|
2018-07-11 13:32:02 +02:00
|
|
|
private fun generatePhotoParams(photoPath: String?): Map<String, String>? {
|
|
|
|
if (TextUtils.isEmpty(photoPath)) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
val photoUri = AndroidUtils.getUriForFile(app, File(photoPath))
|
|
|
|
app.grantUriPermission(
|
2018-08-10 14:31:34 +02:00
|
|
|
app.settings.appToConnectPackage,
|
2018-07-11 13:32:02 +02:00
|
|
|
photoUri,
|
|
|
|
Intent.FLAG_GRANT_READ_URI_PERMISSION
|
|
|
|
)
|
|
|
|
return mapOf(AMapPoint.POINT_IMAGE_URI_PARAM to photoUri.toString())
|
|
|
|
}
|
|
|
|
|
2018-07-13 13:02:59 +02:00
|
|
|
private fun removeMapPoint(chatId: Long, message: TdApi.Message) {
|
2018-07-04 11:24:50 +02:00
|
|
|
val content = message.content
|
|
|
|
if (content is TdApi.MessageLocation) {
|
2018-07-13 13:02:59 +02:00
|
|
|
osmandAidlHelper.removeMapPoint(MAP_LAYER_ID, "${chatId}_${message.senderUserId}")
|
2018-07-04 11:24:50 +02:00
|
|
|
} else if (content is MessageOsmAndBotLocation) {
|
2018-07-13 13:02:59 +02:00
|
|
|
osmandAidlHelper.removeMapPoint(MAP_LAYER_ID, "${chatId}_${content.name}")
|
2018-07-04 11:24:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 13:55:14 +02:00
|
|
|
private fun execOsmandApi(action: (() -> Unit)) {
|
2018-07-09 12:59:30 +02:00
|
|
|
if (osmandAidlHelper.isOsmandConnected()) {
|
2018-06-16 13:55:14 +02:00
|
|
|
action.invoke()
|
2018-07-09 12:59:30 +02:00
|
|
|
} else if (osmandAidlHelper.isOsmandBound()) {
|
|
|
|
osmandAidlHelper.connectOsmand()
|
2018-06-16 13:55:14 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-11 12:41:43 +02:00
|
|
|
}
|