OsmAnd/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt
2018-09-05 12:58:32 +03:00

264 lines
No EOL
8 KiB
Kotlin

package net.osmand.telegram.helpers
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.AsyncTask
import android.text.TextUtils
import net.osmand.aidl.map.ALatLon
import net.osmand.aidl.maplayer.point.AMapPoint
import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.helpers.TelegramHelper.MessageOsmAndBotLocation
import net.osmand.telegram.helpers.TelegramUiHelper.ListItem
import net.osmand.telegram.utils.AndroidUtils
import org.drinkless.td.libcore.telegram.TdApi
import java.io.File
import java.util.concurrent.Executors
class ShowLocationHelper(private val app: TelegramApplication) {
companion object {
const val MAP_LAYER_ID = "telegram_layer"
const val MIN_OSMAND_CALLBACK_VERSION_CODE = 320
}
private val telegramHelper = app.telegramHelper
private val osmandAidlHelper = app.osmandAidlHelper
private val executor = Executors.newSingleThreadExecutor()
var showingLocation: Boolean = false
private set
private var forcedStop: Boolean = false
fun setupMapLayer() {
execOsmandApi {
osmandAidlHelper.addMapLayer(MAP_LAYER_ID, "Telegram", 5.5f, null)
}
}
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,
Color.WHITE,
ALatLon(item.latLon!!.latitude, item.latLon!!.longitude),
null,
generatePhotoParams(if (stale) item.grayscalePhotoPath else item.photoPath, stale)
)
}
}
fun updateLocationsOnMap() {
execOsmandApi {
val messages = telegramHelper.getMessages()
for (message in messages) {
val date = telegramHelper.getLastUpdatedTime(message)
val messageShowingTime = System.currentTimeMillis() / 1000 - date
if (messageShowingTime > app.settings.locHistoryTime) {
removeMapPoint(message.chatId, message)
} else if (app.settings.isShowingChatOnMap(message.chatId)) {
addOrUpdateLocationOnMap(message, true)
}
}
}
}
fun addOrUpdateLocationOnMap(message: TdApi.Message, update: Boolean = false) {
execOsmandApi {
val chatId = message.chatId
val chatTitle = telegramHelper.getChat(message.chatId)?.title
val content = message.content
val date = telegramHelper.getLastUpdatedTime(message)
val stale = System.currentTimeMillis() / 1000 - date > app.settings.staleLocTime
if (chatTitle != null && content is TdApi.MessageLocation) {
var userName = ""
var photoPath: String? = null
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
}
photoPath = if (stale) {
telegramHelper.getUserGreyPhotoPath(user)
} else {
telegramHelper.getUserPhotoPath(user)
}
}
if (userName.isEmpty()) {
userName = message.senderUserId.toString()
}
setupMapLayer()
val params = generatePhotoParams(photoPath, stale)
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()
if (update) {
osmandAidlHelper.updateMapPoint(MAP_LAYER_ID, "${chatId}_$name", name, name,
chatTitle, Color.WHITE, ALatLon(content.lat, content.lon), null, generatePhotoParams(null, stale))
} else {
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatId}_$name", name, name,
chatTitle, Color.WHITE, ALatLon(content.lat, content.lon), null, generatePhotoParams(null, stale))
}
}
}
}
fun showChatMessages(chatId: Long) {
execOsmandApi {
val messages = telegramHelper.getChatMessages(chatId)
for (message in messages) {
addOrUpdateLocationOnMap(message)
}
}
}
fun hideChatMessages(chatId: Long) {
hideMessages(telegramHelper.getChatMessages(chatId))
}
fun hideMessages(messages: List<TdApi.Message>) {
execOsmandApi {
for (message in messages) {
val user = telegramHelper.getUser(message.senderUserId)
if (user != null) {
removeMapPoint(message.chatId, message)
}
}
}
}
fun startShowingLocation() {
if (!showingLocation && !forcedStop) {
showingLocation = if (isUseOsmandCallback()) {
osmandAidlHelper.registerForUpdates()
} else {
app.startUserLocationService()
true
}
}
}
fun stopShowingLocation(force: Boolean = false) {
forcedStop = force
if (showingLocation) {
showingLocation = false
if (isUseOsmandCallback()) {
osmandAidlHelper.unregisterFromUpdates()
} else {
app.stopUserLocationService()
}
}
}
fun isUseOsmandCallback(): Boolean {
val packageOsmAndInfo = app.packageManager.getPackageInfo(app.settings.appToConnectPackage, 0)
return packageOsmAndInfo.versionCode >= MIN_OSMAND_CALLBACK_VERSION_CODE
}
fun startShowMessagesTask(chatId: Long, vararg messages: TdApi.Message) {
if (app.settings.isShowingChatOnMap(chatId)) {
ShowMessagesTask(app).executeOnExecutor(executor, *messages)
}
}
fun startDeleteMessagesTask(chatId: Long, messages: List<TdApi.Message>) {
if (app.settings.isShowingChatOnMap(chatId)) {
DeleteMessagesTask(app).executeOnExecutor(executor, messages)
}
}
fun startUpdateMessagesTask() {
UpdateMessagesTask(app).executeOnExecutor(executor)
}
private class ShowMessagesTask(private val app: TelegramApplication) : AsyncTask<TdApi.Message, Void, Void?>() {
override fun doInBackground(vararg messages: TdApi.Message): Void? {
for (message in messages) {
app.showLocationHelper.addOrUpdateLocationOnMap(message)
}
return null
}
}
private class DeleteMessagesTask(private val app: TelegramApplication) : AsyncTask<List<TdApi.Message>, Void, Void?>() {
override fun doInBackground(vararg messages: List<TdApi.Message>): Void? {
for (list in messages) {
app.showLocationHelper.hideMessages(list)
}
return null
}
}
private class UpdateMessagesTask(private val app: TelegramApplication) : AsyncTask<Void, Void, Void?>() {
override fun doInBackground(vararg params: Void?): Void? {
app.showLocationHelper.updateLocationsOnMap()
return null
}
}
private fun generatePhotoParams(photoPath: String?, stale: Boolean = false): Map<String, String>? {
val photoUri = if (TextUtils.isEmpty(photoPath)) {
val resId = if (stale) {
R.drawable.img_user_picture
} else {
R.drawable.img_user_picture_active
}
AndroidUtils.resourceToUri(app, resId)
} else {
AndroidUtils.getUriForFile(app, File(photoPath))
}
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())
}
private fun removeMapPoint(chatId: Long, message: TdApi.Message) {
val content = message.content
if (content is TdApi.MessageLocation) {
osmandAidlHelper.removeMapPoint(MAP_LAYER_ID, "${chatId}_${message.senderUserId}")
} else if (content is MessageOsmAndBotLocation) {
osmandAidlHelper.removeMapPoint(MAP_LAYER_ID, "${chatId}_${content.name}")
}
}
private fun execOsmandApi(action: (() -> Unit)) {
if (!osmandAidlHelper.isOsmandConnected() && osmandAidlHelper.isOsmandBound()) {
osmandAidlHelper.connectOsmand()
}
if (osmandAidlHelper.isOsmandConnected()) {
action.invoke()
}
}
}