change chaiTitle to chatId

This commit is contained in:
Chumva 2018-07-13 14:02:59 +03:00
parent 5930856428
commit 01d8e3f33e
8 changed files with 112 additions and 97 deletions

View file

@ -246,9 +246,9 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
}
}
override fun onReceiveChatLocationMessages(chatTitle: String, vararg messages: TdApi.Message) {
override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) {
val app = app()
if (app.settings.isShowingChatOnMap(chatTitle)) {
if (app.settings.isShowingChatOnMap(chatId)) {
ShowMessagesTask(app).executeOnExecutor(executor, *messages)
}
}

View file

@ -16,18 +16,20 @@ private const val SEND_MY_LOCATION_INTERVAL_KEY = "send_my_location_interval"
private const val SEND_MY_LOCATION_INTERVAL_DEFAULT = 5L * 1000 // 5 seconds
private const val USER_LOCATION_EXPIRE_TIME_KEY = "user_location_expire_time"
private const val CHANGED_TO_CHAT_ID_KEY = "changed_to_chat_id"
private const val USER_LOCATION_EXPIRE_TIME_DEFAULT = 15L * 60 * 1000 // 15 minutes
class TelegramSettings(private val app: TelegramApplication) {
private var shareLocationChats: Set<String> = emptySet()
private var showOnMapChats: Set<String> = emptySet()
private var shareLocationChats: Set<Long> = emptySet()
private var showOnMapChats: Set<Long> = emptySet()
var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS
var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR
var sendMyLocationInterval = SEND_MY_LOCATION_INTERVAL_DEFAULT
var userLocationExpireTime = USER_LOCATION_EXPIRE_TIME_DEFAULT
var afterMovingChatTitleToChatId = false
init {
read()
@ -35,28 +37,28 @@ class TelegramSettings(private val app: TelegramApplication) {
fun hasAnyChatToShareLocation() = shareLocationChats.isNotEmpty()
fun isSharingLocationToChat(chatTitle: String) = shareLocationChats.contains(chatTitle)
fun isSharingLocationToChat(id: Long) = shareLocationChats.contains(id)
fun hasAnyChatToShowOnMap() = showOnMapChats.isNotEmpty()
fun isShowingChatOnMap(chatTitle: String) = showOnMapChats.contains(chatTitle)
fun isShowingChatOnMap(id: Long) = showOnMapChats.contains(id)
fun removeNonexistingChats(presentChatTitles: List<String>) {
fun removeNonexistingChats(presentChatIds: List<Long>) {
val shareLocationChats = shareLocationChats.toMutableList()
shareLocationChats.intersect(presentChatTitles)
shareLocationChats.intersect(presentChatIds)
this.shareLocationChats = shareLocationChats.toHashSet()
val showOnMapChats = showOnMapChats.toMutableList()
showOnMapChats.intersect(presentChatTitles)
showOnMapChats.intersect(presentChatIds)
this.showOnMapChats = showOnMapChats.toHashSet()
}
fun shareLocationToChat(chatTitle: String, share: Boolean) {
fun shareLocationToChat(id: Long, share: Boolean) {
val shareLocationChats = shareLocationChats.toMutableList()
if (share) {
shareLocationChats.add(chatTitle)
shareLocationChats.add(id)
} else {
shareLocationChats.remove(chatTitle)
shareLocationChats.remove(id)
}
this.shareLocationChats = shareLocationChats.toHashSet()
}
@ -65,12 +67,12 @@ class TelegramSettings(private val app: TelegramApplication) {
this.shareLocationChats = emptySet()
}
fun showChatOnMap(chatTitle: String, show: Boolean) {
fun showChatOnMap(id: Long, show: Boolean) {
val showOnMapChats = showOnMapChats.toMutableList()
if (show) {
showOnMapChats.add(chatTitle)
showOnMapChats.add(id)
} else {
showOnMapChats.remove(chatTitle)
showOnMapChats.remove(id)
}
this.showOnMapChats = showOnMapChats.toHashSet()
}
@ -87,20 +89,21 @@ class TelegramSettings(private val app: TelegramApplication) {
val shareLocationChatsSet = mutableSetOf<String>()
val shareLocationChats = ArrayList(shareLocationChats)
for (chatTitle in shareLocationChats) {
shareLocationChatsSet.add(chatTitle)
for (chatId in shareLocationChats) {
shareLocationChatsSet.add(chatId.toString())
}
edit.putStringSet(SHARE_LOCATION_CHATS_KEY, shareLocationChatsSet)
val showOnMapChatsSet = mutableSetOf<String>()
val showOnMapChats = ArrayList(showOnMapChats)
for (chatTitle in showOnMapChats) {
showOnMapChatsSet.add(chatTitle)
for (chatId in showOnMapChats) {
showOnMapChatsSet.add(chatId.toString())
}
edit.putStringSet(SHOW_ON_MAP_CHATS_KEY, showOnMapChatsSet)
edit.putString(METRICS_CONSTANTS_KEY, metricsConstants.name)
edit.putString(SPEED_CONSTANTS_KEY, speedConstants.name)
edit.putBoolean(CHANGED_TO_CHAT_ID_KEY, afterMovingChatTitleToChatId)
edit.putLong(SEND_MY_LOCATION_INTERVAL_KEY, sendMyLocationInterval)
@ -109,18 +112,28 @@ class TelegramSettings(private val app: TelegramApplication) {
fun read() {
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
afterMovingChatTitleToChatId =
prefs.getBoolean(CHANGED_TO_CHAT_ID_KEY, false)
val shareLocationChats = mutableSetOf<String>()
val shareLocationChats = mutableSetOf<Long>()
val shareLocationChatsSet = prefs.getStringSet(SHARE_LOCATION_CHATS_KEY, mutableSetOf())
for (chatTitle in shareLocationChatsSet) {
shareLocationChats.add(chatTitle)
}
this.shareLocationChats = shareLocationChats
val showOnMapChats = mutableSetOf<String>()
val showOnMapChats = mutableSetOf<Long>()
val showOnMapChatsSet = prefs.getStringSet(SHOW_ON_MAP_CHATS_KEY, mutableSetOf())
for (chatTitle in showOnMapChatsSet) {
showOnMapChats.add(chatTitle)
if (!afterMovingChatTitleToChatId) {
showOnMapChatsSet.clear()
shareLocationChatsSet.clear()
afterMovingChatTitleToChatId = true
}
for (chatId in shareLocationChatsSet) {
shareLocationChats.add(chatId.toLong())
}
this.shareLocationChats = shareLocationChats
for (chatId in showOnMapChatsSet) {
showOnMapChats.add(chatId.toLong())
}
this.showOnMapChats = showOnMapChats

View file

@ -53,11 +53,11 @@ class ShowLocationHelper(private val app: TelegramApplication) {
execOsmandApi {
val messages = telegramHelper.getMessages()
for (message in messages) {
val chatTitle = telegramHelper.getChat(message.chatId)?.title
val chatId = message.chatId
val date = Math.max(message.date, message.editDate) * 1000L
val expired = System.currentTimeMillis() - date > app.settings.userLocationExpireTime
if (chatTitle != null && expired) {
removeMapPoint(chatTitle, message)
if (expired) {
removeMapPoint(chatId, message)
}
}
}
@ -65,6 +65,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
fun addLocationToMap(message: TdApi.Message) {
execOsmandApi {
val chatId = message.chatId
val chatTitle = telegramHelper.getChat(message.chatId)?.title
val content = message.content
if (chatTitle != null && content is TdApi.MessageLocation) {
@ -86,33 +87,33 @@ class ShowLocationHelper(private val app: TelegramApplication) {
}
setupMapLayer()
val params = generatePhotoParams(photoPath)
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatTitle}_${message.senderUserId}", userName, userName,
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatId}_${message.senderUserId}", userName, userName,
chatTitle, Color.RED, ALatLon(content.location.latitude, content.location.longitude), null, params)
} else if (chatTitle != null && content is MessageOsmAndBotLocation && content.isValid()) {
val name = content.name
setupMapLayer()
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatTitle}_$name", name, name,
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatId}_$name", name, name,
chatTitle, Color.RED, ALatLon(content.lat, content.lon), null, null)
}
}
}
fun showChatMessages(chatTitle: String) {
fun showChatMessages(chatId: Long) {
execOsmandApi {
val messages = telegramHelper.getChatMessages(chatTitle)
val messages = telegramHelper.getChatMessages(chatId)
for (message in messages) {
addLocationToMap(message)
}
}
}
fun hideChatMessages(chatTitle: String) {
fun hideChatMessages(chatId: Long) {
execOsmandApi {
val messages = telegramHelper.getChatMessages(chatTitle)
val messages = telegramHelper.getChatMessages(chatId)
for (message in messages) {
val user = telegramHelper.getUser(message.senderUserId)
if (user != null) {
removeMapPoint(chatTitle, message)
removeMapPoint(chatId, message)
}
}
}
@ -145,12 +146,12 @@ class ShowLocationHelper(private val app: TelegramApplication) {
return mapOf(AMapPoint.POINT_IMAGE_URI_PARAM to photoUri.toString())
}
private fun removeMapPoint(chatTitle: String, message: TdApi.Message) {
private fun removeMapPoint(chatId: Long, message: TdApi.Message) {
val content = message.content
if (content is TdApi.MessageLocation) {
osmandAidlHelper.removeMapPoint(MAP_LAYER_ID, "${chatTitle}_${message.senderUserId}")
osmandAidlHelper.removeMapPoint(MAP_LAYER_ID, "${chatId}_${message.senderUserId}")
} else if (content is MessageOsmAndBotLocation) {
osmandAidlHelper.removeMapPoint(MAP_LAYER_ID, "${chatTitle}_${content.name}")
osmandAidlHelper.removeMapPoint(MAP_LAYER_ID, "${chatId}_${content.name}")
}
}

View file

@ -49,7 +49,7 @@ class TelegramHelper private constructor() {
private val secretChats = ConcurrentHashMap<Int, TdApi.SecretChat>()
private val chats = ConcurrentHashMap<Long, TdApi.Chat>()
private val chatTitles = ConcurrentHashMap<String, Long>()
private val chatIds = TreeSet<Long>()
private val chatList = TreeSet<OrderedChat>()
private val chatLiveMessages = ConcurrentHashMap<Long, Long>()
@ -98,7 +98,7 @@ class TelegramHelper private constructor() {
}
}
fun getChatTitles() = chatTitles.keys().toList()
fun getChatTitles() = chatIds.toList()
fun getChat(id: Long) = chats[id]
@ -107,8 +107,8 @@ class TelegramHelper private constructor() {
fun getUserMessage(user: TdApi.User) =
usersLocationMessages.values.firstOrNull { it.senderUserId == user.id }
fun getChatMessages(chatTitle: String) =
usersLocationMessages.values.filter { chats[it.chatId]?.title == chatTitle }
fun getChatMessages(chatId: Long) =
usersLocationMessages.values.filter { chats[it.chatId]?.id == chatId }
fun getMessages() = usersLocationMessages.values.toList()
@ -131,9 +131,9 @@ class TelegramHelper private constructor() {
fun getSupergroupFullInfo(id: Int) = supergroupsFullInfo[id]
private fun updateChatTitles() {
chatTitles.clear()
chatIds.clear()
for (chatEntry in chats.entries) {
chatTitles[chatEntry.value.title] = chatEntry.key
chatIds.add(chatEntry.key)
}
}
@ -172,7 +172,7 @@ class TelegramHelper private constructor() {
}
interface TelegramIncomingMessagesListener {
fun onReceiveChatLocationMessages(chatTitle: String, vararg messages: TdApi.Message)
fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message)
fun updateLocationMessages()
}
@ -372,11 +372,9 @@ class TelegramHelper private constructor() {
}
removeOldMessages(message.senderUserId, message.chatId)
usersLocationMessages[message.id] = message
val chatTitle = chats[message.chatId]?.title
if (chatTitle != null) {
val chatId = message.chatId
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(chatTitle, message)
}
it.onReceiveChatLocationMessages(chatId, message)
}
}
}
@ -397,15 +395,15 @@ class TelegramHelper private constructor() {
* @latitude Latitude of the location
* @longitude Longitude of the location
*/
fun sendLiveLocationMessage(chatTitles: List<String>, livePeriod: Int, latitude: Double, longitude: Double): Boolean {
fun sendLiveLocationMessage(chatIds: List<Long>, livePeriod: Int, latitude: Double, longitude: Double): Boolean {
if (!requestingActiveLiveLocationMessages && haveAuthorization) {
if (needRefreshActiveLiveLocationMessages) {
getActiveLiveLocationMessages {
sendLiveLocationImpl(chatTitles, livePeriod, latitude, longitude)
sendLiveLocationImpl(chatIds, livePeriod, latitude, longitude)
}
needRefreshActiveLiveLocationMessages = false
} else {
sendLiveLocationImpl(chatTitles, livePeriod, latitude, longitude)
sendLiveLocationImpl(chatIds, livePeriod, latitude, longitude)
}
return true
}
@ -440,7 +438,7 @@ class TelegramHelper private constructor() {
}
}
private fun sendLiveLocationImpl(chatTitles: List<String>, livePeriod: Int, latitude: Double, longitude: Double) {
private fun sendLiveLocationImpl(chatIds: List<Long>, livePeriod: Int, latitude: Double, longitude: Double) {
val lp = when {
livePeriod < MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC
livePeriod > MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC
@ -449,18 +447,15 @@ class TelegramHelper private constructor() {
val location = TdApi.Location(latitude, longitude)
val content = TdApi.InputMessageLocation(location, lp)
for (chatTitle in chatTitles) {
val chatId = this.chatTitles[chatTitle]
if (chatId != null) {
val msgId = chatLiveMessages[chatId]
if (msgId != null) {
if (msgId != 0L) {
client?.send(TdApi.EditMessageLiveLocation(chatId, msgId, null, location), liveLocationMessageUpdatesHandler)
}
} else {
chatLiveMessages[chatId] = 0L
client?.send(TdApi.SendMessage(chatId, 0, false, true, null, content), liveLocationMessageUpdatesHandler)
for (chatId in chatIds) {
val msgId = chatLiveMessages[chatId]
if (msgId != null) {
if (msgId != 0L) {
client?.send(TdApi.EditMessageLiveLocation(chatId, msgId, null, location), liveLocationMessageUpdatesHandler)
}
} else {
chatLiveMessages[chatId] = 0L
client?.send(TdApi.SendMessage(chatId, 0, false, true, null, content), liveLocationMessageUpdatesHandler)
}
}
}
@ -856,11 +851,9 @@ class TelegramHelper private constructor() {
synchronized(message) {
message.editDate = updateMessageEdited.editDate
}
val chatTitle = chats[message.chatId]?.title
if (chatTitle != null) {
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(chatTitle, message)
}
val chatId = message.chatId
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(chatId, message)
}
}
}
@ -880,11 +873,9 @@ class TelegramHelper private constructor() {
newContent
}
}
val chatTitle = chats[message.chatId]?.title
if (chatTitle != null) {
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(chatTitle, message)
}
val chatId = message.chatId
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(chatId, message)
}
}
}

View file

@ -41,6 +41,7 @@ object TelegramUiHelper {
messages: List<TdApi.Message>
): ChatItem {
val res = ChatItem().apply {
chatId = chat.id
chatTitle = chat.title
photoPath = chat.photo?.small?.local?.path
placeholderId = R.drawable.img_user_picture
@ -95,6 +96,7 @@ object TelegramUiHelper {
): LocationItem? {
return if (content.isValid()) {
LocationItem().apply {
chatId = chat.id
chatTitle = chat.title
name = content.name
latLon = LatLon(content.lat, content.lon)
@ -113,6 +115,7 @@ object TelegramUiHelper {
val user = helper.getUser(message.senderUserId) ?: return null
val content = message.content as TdApi.MessageLocation
return LocationItem().apply {
chatId = chat.id
chatTitle = chat.title
name = "${user.firstName} ${user.lastName}".trim()
if (name.isEmpty()) {
@ -130,6 +133,8 @@ object TelegramUiHelper {
abstract class ListItem {
var chatId: Long = 0
internal set
var chatTitle: String = ""
internal set
var latLon: LatLon? = null
@ -161,7 +166,7 @@ object TelegramUiHelper {
override fun canBeOpenedOnMap() = latLon != null && !chatWithBot
override fun getMapPointId() = "${chatTitle}_$userId"
override fun getMapPointId() = "${chatId}_$userId"
override fun getVisibleName() = chatTitle
}
@ -175,7 +180,7 @@ object TelegramUiHelper {
override fun getMapPointId(): String {
val id = if (userId != 0) userId.toString() else name
return "${chatTitle}_$id"
return "${chatId}_$id"
}
override fun getVisibleName() = name

View file

@ -119,7 +119,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
override fun onSendLiveLocationError(code: Int, message: String) {}
override fun onReceiveChatLocationMessages(chatTitle: String, vararg messages: TdApi.Message) {
override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) {
app.runInUIThread { updateList() }
}
@ -273,12 +273,12 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
if (item is ChatItem && holder is ChatViewHolder) {
val nextIsLocation = !lastItem && items[position + 1] is LocationItem
val chatTitle = item.chatTitle
val stateTextInd = if (settings.isShowingChatOnMap(chatTitle)) 1 else 0
val chatId = item.chatId
val stateTextInd = if (settings.isShowingChatOnMap(chatId)) 1 else 0
holder.description?.text = getChatItemDescription(item)
holder.imageButton?.visibility = View.GONE
holder.showOnMapRow?.setOnClickListener { showPopupMenu(holder, chatTitle) }
holder.showOnMapRow?.setOnClickListener { showPopupMenu(holder, chatId) }
holder.showOnMapState?.text = menuList[stateTextInd]
holder.bottomDivider?.visibility = if (nextIsLocation) View.VISIBLE else View.GONE
} else if (item is LocationItem && holder is ContactViewHolder) {
@ -300,7 +300,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
}
}
private fun showPopupMenu(holder: ChatViewHolder, chatTitle: String) {
private fun showPopupMenu(holder: ChatViewHolder, chatId: Long) {
val ctx = holder.itemView.context
val paint = Paint()
@ -319,7 +319,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
setOnItemClickListener { _, _, position, _ ->
val allSelected = position == 1
settings.showChatOnMap(chatTitle, allSelected)
settings.showChatOnMap(chatId, allSelected)
if (settings.hasAnyChatToShowOnMap()) {
if (osmandAidlHelper.isOsmandNotInstalled()) {
if (allSelected) {
@ -327,16 +327,16 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
}
} else {
if (allSelected) {
app.showLocationHelper.showChatMessages(chatTitle)
app.showLocationHelper.showChatMessages(chatId)
} else {
app.showLocationHelper.hideChatMessages(chatTitle)
app.showLocationHelper.hideChatMessages(chatId)
}
app.showLocationHelper.startShowingLocation()
}
} else {
app.showLocationHelper.stopShowingLocation()
if (!allSelected) {
app.showLocationHelper.hideChatMessages(chatTitle)
app.showLocationHelper.hideChatMessages(chatId)
}
}

View file

@ -451,8 +451,8 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val chat = chats[position]
val chatTitle = chat.title
holder.groupName?.text = chatTitle
val chatId = chat.id
holder.groupName?.text = chat.title
var drawable: Drawable? = null
var bitmap: Bitmap? = null
@ -469,9 +469,9 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
holder.icon?.setImageDrawable(drawable)
}
holder.shareLocationSwitch?.setOnCheckedChangeListener(null)
holder.shareLocationSwitch?.isChecked = settings.isSharingLocationToChat(chatTitle)
holder.shareLocationSwitch?.isChecked = settings.isSharingLocationToChat(chatId)
holder.shareLocationSwitch?.setOnCheckedChangeListener { view, isChecked ->
settings.shareLocationToChat(chatTitle, isChecked)
settings.shareLocationToChat(chatId, isChecked)
if (settings.hasAnyChatToShareLocation()) {
if (!AndroidUtils.isLocationPermissionAvailable(view.context)) {
if (isChecked) {
@ -486,9 +486,9 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
}
holder.showOnMapSwitch?.setOnCheckedChangeListener(null)
holder.showOnMapSwitch?.isChecked = settings.isShowingChatOnMap(chatTitle)
holder.showOnMapSwitch?.isChecked = settings.isShowingChatOnMap(chatId)
holder.showOnMapSwitch?.setOnCheckedChangeListener { _, isChecked ->
settings.showChatOnMap(chatTitle, isChecked)
settings.showChatOnMap(chatId, isChecked)
if (settings.hasAnyChatToShowOnMap()) {
if (osmandAidlHelper.isOsmandNotInstalled()) {
if (isChecked) {
@ -496,16 +496,16 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
}
} else {
if (isChecked) {
app.showLocationHelper.showChatMessages(chatTitle)
app.showLocationHelper.showChatMessages(chatId)
} else {
app.showLocationHelper.hideChatMessages(chatTitle)
app.showLocationHelper.hideChatMessages(chatId)
}
app.showLocationHelper.startShowingLocation()
}
} else {
app.showLocationHelper.stopShowingLocation()
if (!isChecked) {
app.showLocationHelper.hideChatMessages(chatTitle)
app.showLocationHelper.hideChatMessages(chatId)
}
}
}

View file

@ -17,6 +17,7 @@ import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.helpers.ShareLocationHelper
import net.osmand.telegram.helpers.TelegramUiHelper
import net.osmand.telegram.ui.SetTimeDialogFragment.SetTimeListAdapter.ChatViewHolder
import net.osmand.telegram.utils.AndroidUtils
import org.drinkless.td.libcore.telegram.TdApi
import java.util.concurrent.TimeUnit
@ -77,6 +78,7 @@ class SetTimeDialogFragment : DialogFragment() {
text = getString(R.string.shared_string_share)
setOnClickListener {
Toast.makeText(context, "Share", Toast.LENGTH_SHORT).show()
}
}
@ -146,6 +148,9 @@ class SetTimeDialogFragment : DialogFragment() {
if (seconds >= ShareLocationHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC) {
if (id != null) {
chatIdsToDuration[id] = seconds
app.settings.shareLocationToChat(id, true)
app.shareLocationHelper.startSharingLocation()
} else {
chatIdsToDuration.keys.forEach {
chatIdsToDuration[it] = seconds