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

198 lines
5.9 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.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
import net.osmand.telegram.helpers.TelegramUiHelper.ListItem
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
2018-08-22 17:53:21 +02:00
import android.net.Uri
import net.osmand.telegram.R
class ShowLocationHelper(private val app: TelegramApplication) {
companion object {
const val MAP_LAYER_ID = "telegram_layer"
}
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
}
2018-08-22 17:53:21 +02:00
fun showLocationOnMap(item: ListItem, stale: Boolean = false) {
if (item.latLon == null) {
return
}
execOsmandApi {
osmandAidlHelper.showMapPoint(
MAP_LAYER_ID,
item.getMapPointId(),
item.getVisibleName(),
item.getVisibleName(),
item.chatTitle,
2018-08-21 18:15:01 +02:00
Color.WHITE,
ALatLon(item.latLon!!.latitude, item.latLon!!.longitude),
null,
2018-08-23 09:41:14 +02:00
generatePhotoParams(if (stale) item.grayscalePhotoPath else item.photoPath, stale)
)
}
}
fun updateLocationsOnMap() {
execOsmandApi {
val messages = telegramHelper.getMessages()
for (message in messages) {
2018-08-22 17:53:21 +02:00
val date = telegramHelper.getLastUpdatedTime(message)
val messageShowingTime = System.currentTimeMillis() / 1000 - date
if (messageShowingTime > app.settings.locHistoryTime) {
2018-07-13 13:42:57 +02:00
removeMapPoint(message.chatId, message)
2018-08-22 17:53:21 +02:00
} else if (app.settings.isShowingChatOnMap(message.chatId)) {
2018-08-21 18:15:01 +02:00
addOrUpdateLocationOnMap(message, true)
}
}
}
}
2018-08-21 18:15:01 +02:00
fun addOrUpdateLocationOnMap(message: TdApi.Message, update: Boolean = false) {
execOsmandApi {
2018-07-13 13:02:59 +02:00
val chatId = message.chatId
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 = ""
var photoPath: String? = null
2018-08-22 18:04:11 +02:00
val date = telegramHelper.getLastUpdatedTime(message)
val stale = System.currentTimeMillis() / 1000 - date > app.settings.staleLocTime
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-08-22 17:53:21 +02:00
photoPath = if (stale) {
2018-08-21 18:15:01 +02:00
telegramHelper.getUserGreyPhotoPath(user)
} else {
telegramHelper.getUserPhotoPath(user)
}
2018-06-11 18:57:33 +02:00
}
if (userName.isEmpty()) {
userName = message.senderUserId.toString()
}
setupMapLayer()
2018-08-22 17:53:21 +02:00
val params = generatePhotoParams(photoPath, stale)
2018-08-21 18:15:01 +02:00
if (update) {
osmandAidlHelper.updateMapPoint(MAP_LAYER_ID, "${chatId}_${message.senderUserId}", userName, userName,
chatTitle, Color.WHITE, ALatLon(content.location.latitude, content.location.longitude), null, params)
} else {
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatId}_${message.senderUserId}", userName, userName,
chatTitle, Color.WHITE, ALatLon(content.location.latitude, content.location.longitude), null, params)
}
} else if (chatTitle != null && content is MessageOsmAndBotLocation && content.isValid()) {
val name = content.name
setupMapLayer()
2018-08-21 18:15:01 +02:00
if (update) {
osmandAidlHelper.updateMapPoint(MAP_LAYER_ID, "${chatId}_$name", name, name,
chatTitle, Color.WHITE, ALatLon(content.lat, content.lon), null, null)
} else {
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatId}_$name", name, name,
chatTitle, Color.WHITE, ALatLon(content.lat, content.lon), null, null)
}
2018-06-11 18:57:33 +02:00
}
}
}
2018-07-13 13:02:59 +02:00
fun showChatMessages(chatId: Long) {
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-08-21 18:15:01 +02:00
addOrUpdateLocationOnMap(message)
2018-06-11 18:57:33 +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>) {
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 18:57:33 +02:00
fun startShowingLocation() {
if (!showingLocation) {
showingLocation = true
app.startUserLocationService()
}
}
fun stopShowingLocation() {
if (showingLocation) {
showingLocation = false
app.stopUserLocationService()
}
}
2018-08-22 17:53:21 +02:00
private fun generatePhotoParams(photoPath: String?, stale: Boolean = false): Map<String, String>? {
val photoUri = if (TextUtils.isEmpty(photoPath)) {
2018-08-28 13:58:44 +02:00
val resId = if (stale) {
R.drawable.img_user_picture
2018-08-22 17:53:21 +02:00
} else {
2018-08-28 13:58:44 +02:00
R.drawable.img_user_picture_active
2018-08-22 17:53:21 +02:00
}
2018-08-28 13:58:44 +02:00
AndroidUtils.resourceToUri(app, resId)
2018-08-22 17:53:21 +02:00
} else {
AndroidUtils.getUriForFile(app, File(photoPath))
}
2018-08-22 17:53:21 +02:00
return generatePhotoParamsFromUri(photoUri)
}
private fun generatePhotoParamsFromUri(photoUri: Uri): Map<String, String>? {
app.grantUriPermission(
app.settings.appToConnectPackage,
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) {
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}")
} else if (content is MessageOsmAndBotLocation) {
2018-07-13 13:02:59 +02:00
osmandAidlHelper.removeMapPoint(MAP_LAYER_ID, "${chatId}_${content.name}")
}
}
private fun execOsmandApi(action: (() -> Unit)) {
2018-08-17 12:47:03 +02:00
if (!osmandAidlHelper.isOsmandConnected() && osmandAidlHelper.isOsmandBound()) {
osmandAidlHelper.connectOsmand()
}
if (osmandAidlHelper.isOsmandConnected()) {
action.invoke()
}
}
}