Merge pull request #5666 from osmandapp/MoveFromChatTitleToChatId

Change chatTitle to chatId
This commit is contained in:
Alexey 2018-07-13 15:02:40 +03:00 committed by GitHub
commit aff56654b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 103 additions and 104 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

@ -18,10 +18,12 @@ 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 USER_LOCATION_EXPIRE_TIME_DEFAULT = 15L * 60 * 1000 // 15 minutes
private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id"
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
@ -30,33 +32,34 @@ class TelegramSettings(private val app: TelegramApplication) {
var userLocationExpireTime = USER_LOCATION_EXPIRE_TIME_DEFAULT
init {
updatePrefs()
read()
}
fun hasAnyChatToShareLocation() = shareLocationChats.isNotEmpty()
fun isSharingLocationToChat(chatTitle: String) = shareLocationChats.contains(chatTitle)
fun isSharingLocationToChat(chatId: Long) = shareLocationChats.contains(chatId)
fun hasAnyChatToShowOnMap() = showOnMapChats.isNotEmpty()
fun isShowingChatOnMap(chatTitle: String) = showOnMapChats.contains(chatTitle)
fun isShowingChatOnMap(chatId: Long) = showOnMapChats.contains(chatId)
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(chatId: Long, share: Boolean) {
val shareLocationChats = shareLocationChats.toMutableList()
if (share) {
shareLocationChats.add(chatTitle)
shareLocationChats.add(chatId)
} else {
shareLocationChats.remove(chatTitle)
shareLocationChats.remove(chatId)
}
this.shareLocationChats = shareLocationChats.toHashSet()
}
@ -65,12 +68,12 @@ class TelegramSettings(private val app: TelegramApplication) {
this.shareLocationChats = emptySet()
}
fun showChatOnMap(chatTitle: String, show: Boolean) {
fun showChatOnMap(chatId: Long, show: Boolean) {
val showOnMapChats = showOnMapChats.toMutableList()
if (show) {
showOnMapChats.add(chatTitle)
showOnMapChats.add(chatId)
} else {
showOnMapChats.remove(chatTitle)
showOnMapChats.remove(chatId)
}
this.showOnMapChats = showOnMapChats.toHashSet()
}
@ -87,15 +90,15 @@ 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)
@ -110,17 +113,17 @@ class TelegramSettings(private val app: TelegramApplication) {
fun read() {
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
val shareLocationChats = mutableSetOf<String>()
val shareLocationChats = mutableSetOf<Long>()
val shareLocationChatsSet = prefs.getStringSet(SHARE_LOCATION_CHATS_KEY, mutableSetOf())
for (chatTitle in shareLocationChatsSet) {
shareLocationChats.add(chatTitle)
for (chatId in shareLocationChatsSet) {
shareLocationChats.add(chatId.toLong())
}
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)
for (chatId in showOnMapChatsSet) {
showOnMapChats.add(chatId.toLong())
}
this.showOnMapChats = showOnMapChats
@ -136,4 +139,18 @@ class TelegramSettings(private val app: TelegramApplication) {
userLocationExpireTime =
prefs.getLong(USER_LOCATION_EXPIRE_TIME_KEY, USER_LOCATION_EXPIRE_TIME_DEFAULT)
}
private fun updatePrefs() {
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
val idsInUse = prefs.getBoolean(TITLES_REPLACED_WITH_IDS, false)
if (!idsInUse) {
val edit = prefs.edit()
edit.putStringSet(SHARE_LOCATION_CHATS_KEY, emptySet())
edit.putStringSet(SHOW_ON_MAP_CHATS_KEY, emptySet())
edit.putBoolean(TITLES_REPLACED_WITH_IDS, true)
edit.apply()
}
}
}

View file

@ -53,11 +53,10 @@ class ShowLocationHelper(private val app: TelegramApplication) {
execOsmandApi {
val messages = telegramHelper.getMessages()
for (message in messages) {
val chatTitle = telegramHelper.getChat(message.chatId)?.title
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(message.chatId, message)
}
}
}
@ -65,6 +64,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 +86,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 +145,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,6 @@ 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 chatList = TreeSet<OrderedChat>()
private val chatLiveMessages = ConcurrentHashMap<Long, Long>()
@ -98,7 +97,7 @@ class TelegramHelper private constructor() {
}
}
fun getChatTitles() = chatTitles.keys().toList()
fun getChatIds() = chats.keys().toList()
fun getChat(id: Long) = chats[id]
@ -107,8 +106,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 { it.chatId == chatId }
fun getMessages() = usersLocationMessages.values.toList()
@ -130,13 +129,6 @@ class TelegramHelper private constructor() {
fun getSupergroupFullInfo(id: Int) = supergroupsFullInfo[id]
private fun updateChatTitles() {
chatTitles.clear()
for (chatEntry in chats.entries) {
chatTitles[chatEntry.value.title] = chatEntry.key
}
}
private fun isChannel(chat: TdApi.Chat): Boolean {
return chat.type is TdApi.ChatTypeSupergroup && (chat.type as TdApi.ChatTypeSupergroup).isChannel
}
@ -172,7 +164,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()
}
@ -346,7 +338,6 @@ class TelegramHelper private constructor() {
return
}
}
updateChatTitles()
listener?.onTelegramChatsRead()
}
@ -372,11 +363,8 @@ class TelegramHelper private constructor() {
}
removeOldMessages(message.senderUserId, message.chatId)
usersLocationMessages[message.id] = message
val chatTitle = chats[message.chatId]?.title
if (chatTitle != null) {
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(chatTitle, message)
}
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(message.chatId, message)
}
}
}
@ -397,15 +385,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 +428,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 +437,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)
}
}
}
@ -761,7 +746,6 @@ class TelegramHelper private constructor() {
chat.order = 0
setChatOrder(chat, order)
}
updateChatTitles()
listener?.onTelegramChatsChanged()
}
TdApi.UpdateChatTitle.CONSTRUCTOR -> {
@ -771,7 +755,6 @@ class TelegramHelper private constructor() {
synchronized(chat) {
chat.title = updateChat.title
}
updateChatTitles()
listener?.onTelegramChatChanged(chat)
}
}
@ -856,11 +839,8 @@ 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)
}
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(message.chatId, message)
}
}
}
@ -880,11 +860,8 @@ class TelegramHelper private constructor() {
newContent
}
}
val chatTitle = chats[message.chatId]?.title
if (chatTitle != null) {
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(chatTitle, message)
}
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(message.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

@ -120,7 +120,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() }
}
@ -274,12 +274,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) {
@ -301,7 +301,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()
@ -320,7 +320,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) {
@ -328,16 +328,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

@ -258,7 +258,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
}
private fun removeNonexistingChatsFromSettings() {
val presentChatTitles = telegramHelper.getChatTitles()
val presentChatTitles = telegramHelper.getChatIds()
settings.removeNonexistingChats(presentChatTitles)
}
@ -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)
}
}
}