Fix the ability to open locations from OsmAnd-Telegram on map in OsmAnd

This commit is contained in:
Alex Sytnyk 2018-07-11 14:32:02 +03:00
parent f208ac3af6
commit a821a872df
3 changed files with 44 additions and 17 deletions

View file

@ -2,12 +2,12 @@ package net.osmand.telegram.helpers
import android.content.Intent import android.content.Intent
import android.graphics.Color import android.graphics.Color
import android.net.Uri
import android.text.TextUtils import android.text.TextUtils
import net.osmand.aidl.map.ALatLon import net.osmand.aidl.map.ALatLon
import net.osmand.aidl.maplayer.point.AMapPoint import net.osmand.aidl.maplayer.point.AMapPoint
import net.osmand.telegram.TelegramApplication import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.helpers.TelegramHelper.MessageOsmAndBotLocation import net.osmand.telegram.helpers.TelegramHelper.MessageOsmAndBotLocation
import net.osmand.telegram.helpers.TelegramUiHelper.ListItem
import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.AndroidUtils
import org.drinkless.td.libcore.telegram.TdApi import org.drinkless.td.libcore.telegram.TdApi
import java.io.File import java.io.File
@ -30,6 +30,25 @@ class ShowLocationHelper(private val app: TelegramApplication) {
} }
} }
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)
)
}
}
fun updateLocationsOnMap() { fun updateLocationsOnMap() {
execOsmandApi { execOsmandApi {
val messages = telegramHelper.getMessages() val messages = telegramHelper.getMessages()
@ -50,7 +69,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
val content = message.content val content = message.content
if (chatTitle != null && content is TdApi.MessageLocation) { if (chatTitle != null && content is TdApi.MessageLocation) {
var userName = "" var userName = ""
var photoUri: Uri? = null var photoPath: String? = null
val user = telegramHelper.getUser(message.senderUserId) val user = telegramHelper.getUser(message.senderUserId)
if (user != null) { if (user != null) {
userName = "${user.firstName} ${user.lastName}".trim() userName = "${user.firstName} ${user.lastName}".trim()
@ -60,20 +79,13 @@ class ShowLocationHelper(private val app: TelegramApplication) {
if (userName.isEmpty()) { if (userName.isEmpty()) {
userName = user.phoneNumber userName = user.phoneNumber
} }
val photoPath = telegramHelper.getUserPhotoPath(user) 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)
}
} }
if (userName.isEmpty()) { if (userName.isEmpty()) {
userName = message.senderUserId.toString() userName = message.senderUserId.toString()
} }
setupMapLayer() setupMapLayer()
val params = mutableMapOf<String, String>() val params = generatePhotoParams(photoPath)
if (photoUri != null) {
params[AMapPoint.POINT_IMAGE_URI_PARAM] = photoUri.toString()
}
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatTitle}_${message.senderUserId}", userName, userName, osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatTitle}_${message.senderUserId}", userName, userName,
chatTitle, Color.RED, ALatLon(content.location.latitude, content.location.longitude), null, params) chatTitle, Color.RED, ALatLon(content.location.latitude, content.location.longitude), null, params)
} else if (chatTitle != null && content is MessageOsmAndBotLocation && content.isValid()) { } else if (chatTitle != null && content is MessageOsmAndBotLocation && content.isValid()) {
@ -120,6 +132,19 @@ class ShowLocationHelper(private val app: TelegramApplication) {
} }
} }
private fun generatePhotoParams(photoPath: String?): Map<String, String>? {
if (TextUtils.isEmpty(photoPath)) {
return null
}
val photoUri = AndroidUtils.getUriForFile(app, File(photoPath))
app.grantUriPermission(
OsmandAidlHelper.OSMAND_PACKAGE_NAME,
photoUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
)
return mapOf(AMapPoint.POINT_IMAGE_URI_PARAM to photoUri.toString())
}
private fun removeMapPoint(chatTitle: String, message: TdApi.Message) { private fun removeMapPoint(chatTitle: String, message: TdApi.Message) {
val content = message.content val content = message.content
if (content is TdApi.MessageLocation) { if (content is TdApi.MessageLocation) {

View file

@ -144,6 +144,8 @@ object TelegramUiHelper {
abstract fun canBeOpenedOnMap(): Boolean abstract fun canBeOpenedOnMap(): Boolean
abstract fun getMapPointId(): String abstract fun getMapPointId(): String
abstract fun getVisibleName(): String
} }
class ChatItem : ListItem() { class ChatItem : ListItem() {
@ -160,6 +162,8 @@ object TelegramUiHelper {
override fun canBeOpenedOnMap() = latLon != null && !chatWithBot override fun canBeOpenedOnMap() = latLon != null && !chatWithBot
override fun getMapPointId() = "${chatTitle}_$userId" override fun getMapPointId() = "${chatTitle}_$userId"
override fun getVisibleName() = chatTitle
} }
class LocationItem : ListItem() { class LocationItem : ListItem() {
@ -173,5 +177,7 @@ object TelegramUiHelper {
val id = if (userId != 0) userId.toString() else name val id = if (userId != 0) userId.toString() else name
return "${chatTitle}_$id" return "${chatTitle}_$id"
} }
override fun getVisibleName() = name
} }
} }

View file

@ -244,16 +244,14 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
val openOnMapView = holder.getOpenOnMapClickView() val openOnMapView = holder.getOpenOnMapClickView()
TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, item.placeholderId) TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, item.placeholderId)
holder.title?.text = item.getVisibleName()
openOnMapView?.isEnabled = canBeOpenedOnMap openOnMapView?.isEnabled = canBeOpenedOnMap
if (canBeOpenedOnMap) { if (canBeOpenedOnMap) {
openOnMapView?.setOnClickListener { openOnMapView?.setOnClickListener {
if (osmandAidlHelper.isOsmandNotInstalled()) { if (osmandAidlHelper.isOsmandNotInstalled()) {
showOsmAndMissingDialog() showOsmAndMissingDialog()
} else { } else {
// osmandAidlHelper.showLayerPointOnMap( app.showLocationHelper.showLocationOnMap(item)
// ShowLocationHelper.MAP_LAYER_ID,
// item.getMapPointId()
// )
} }
} }
} else { } else {
@ -278,14 +276,12 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
val chatTitle = item.chatTitle val chatTitle = item.chatTitle
val stateTextInd = if (settings.isShowingChatOnMap(chatTitle)) 1 else 0 val stateTextInd = if (settings.isShowingChatOnMap(chatTitle)) 1 else 0
holder.title?.text = chatTitle
holder.description?.text = "Chat description" // FIXME holder.description?.text = "Chat description" // FIXME
holder.imageButton?.visibility = View.GONE holder.imageButton?.visibility = View.GONE
holder.showOnMapRow?.setOnClickListener { showPopupMenu(holder, chatTitle) } holder.showOnMapRow?.setOnClickListener { showPopupMenu(holder, chatTitle) }
holder.showOnMapState?.text = menuList[stateTextInd] holder.showOnMapState?.text = menuList[stateTextInd]
holder.bottomDivider?.visibility = if (nextIsLocation) View.VISIBLE else View.GONE holder.bottomDivider?.visibility = if (nextIsLocation) View.VISIBLE else View.GONE
} else if (item is LocationItem && holder is ContactViewHolder) { } else if (item is LocationItem && holder is ContactViewHolder) {
holder.title?.text = item.name
holder.description?.text = "Some description" holder.description?.text = "Some description"
} }
} }