add stale location icons

This commit is contained in:
Chumva 2018-08-21 19:15:01 +03:00
parent 117f04eb35
commit ea1c74a97c
7 changed files with 127 additions and 23 deletions

View file

@ -273,7 +273,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
override fun doInBackground(vararg messages: TdApi.Message): Void? {
for (message in messages) {
app.showLocationHelper.addLocationToMap(message)
app.showLocationHelper.addOrUpdateLocationOnMap(message)
}
return null
}

View file

@ -41,7 +41,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
item.getVisibleName(),
item.getVisibleName(),
item.chatTitle,
Color.RED,
Color.WHITE,
ALatLon(item.latLon!!.latitude, item.latLon!!.longitude),
null,
generatePhotoParams(item.photoPath)
@ -53,16 +53,18 @@ class ShowLocationHelper(private val app: TelegramApplication) {
execOsmandApi {
val messages = telegramHelper.getMessages()
for (message in messages) {
val date = Math.max(message.date, message.editDate) * 1000L
val expired = System.currentTimeMillis() - date > app.settings.locHistoryTime * 1000L
if (expired) {
val date = telegramHelper.getLastUpdatedTime(message) * 1000L
val messageShowingTime = System.currentTimeMillis() - date
if (messageShowingTime > app.settings.locHistoryTime * 1000L) {
removeMapPoint(message.chatId, message)
} else {
addOrUpdateLocationOnMap(message, true)
}
}
}
}
fun addLocationToMap(message: TdApi.Message) {
fun addOrUpdateLocationOnMap(message: TdApi.Message, update: Boolean = false) {
execOsmandApi {
val chatId = message.chatId
val chatTitle = telegramHelper.getChat(message.chatId)?.title
@ -79,20 +81,36 @@ class ShowLocationHelper(private val app: TelegramApplication) {
if (userName.isEmpty()) {
userName = user.phoneNumber
}
photoPath = telegramHelper.getUserPhotoPath(user)
val date = telegramHelper.getLastUpdatedTime(message) * 1000L
val expired = System.currentTimeMillis() - date > app.settings.staleLocTime * 1000L
photoPath = if (expired) {
telegramHelper.getUserGreyPhotoPath(user)
} else {
telegramHelper.getUserPhotoPath(user)
}
}
if (userName.isEmpty()) {
userName = message.senderUserId.toString()
}
setupMapLayer()
val params = generatePhotoParams(photoPath)
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatId}_${message.senderUserId}", userName, userName,
chatTitle, Color.RED, ALatLon(content.location.latitude, content.location.longitude), null, params)
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()
osmandAidlHelper.addMapPoint(MAP_LAYER_ID, "${chatId}_$name", name, name,
chatTitle, Color.RED, ALatLon(content.lat, content.lon), null, null)
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)
}
}
}
}
@ -101,7 +119,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
execOsmandApi {
val messages = telegramHelper.getChatMessages(chatId)
for (message in messages) {
addLocationToMap(message)
addOrUpdateLocationOnMap(message)
}
}
}

View file

@ -3,6 +3,7 @@ package net.osmand.telegram.helpers
import android.text.TextUtils
import net.osmand.PlatformUtil
import net.osmand.telegram.helpers.TelegramHelper.TelegramAuthenticationParameterType.*
import net.osmand.telegram.utils.PROFILE_GREY_PHOTOS_DIR
import org.drinkless.td.libcore.telegram.Client
import org.drinkless.td.libcore.telegram.Client.ResultHandler
import org.drinkless.td.libcore.telegram.TdApi
@ -309,6 +310,17 @@ class TelegramHelper private constructor() {
}
}
fun getUserGreyPhotoPath(user: TdApi.User): String? {
return if (hasLocalGreyUserPhoto(user)) {
"$appDir/$PROFILE_GREY_PHOTOS_DIR${user.id}.png"
} else {
if (!hasLocalUserPhoto(user) && hasRemoteUserPhoto(user)) {
requestUserPhoto(user)
}
null
}
}
fun getOsmAndBotDeviceName(message: TdApi.Message): String {
var deviceName = ""
if (message.replyMarkup is TdApi.ReplyMarkupInlineKeyboard) {
@ -322,6 +334,12 @@ class TelegramHelper private constructor() {
return deviceName
}
fun getUserIdFromChatType(type: TdApi.ChatType) = when (type) {
is TdApi.ChatTypePrivate -> type.userId
is TdApi.ChatTypeSecret -> type.userId
else -> 0
}
fun isOsmAndBot(userId: Int) = users[userId]?.username == OSMAND_BOT_USERNAME
fun isBot(userId: Int) = users[userId]?.type is TdApi.UserTypeBot
@ -341,6 +359,10 @@ class TelegramHelper private constructor() {
updateLiveMessagesExecutor?.awaitTermination(1, TimeUnit.MINUTES)
}
private fun hasLocalGreyUserPhoto(user: TdApi.User): Boolean {
return File("$appDir/$PROFILE_GREY_PHOTOS_DIR${user.id}.png").exists()
}
private fun hasLocalUserPhoto(user: TdApi.User): Boolean {
val localPhoto = user.profilePhoto?.small?.local
return if (localPhoto != null) {
@ -929,6 +951,7 @@ class TelegramHelper private constructor() {
TdApi.UpdateUser.CONSTRUCTOR -> {
val updateUser = obj as TdApi.UpdateUser
users[updateUser.user.id] = updateUser.user
listener?.onTelegramUserChanged(updateUser.user)
}
TdApi.UpdateUserStatus.CONSTRUCTOR -> {
val updateUserStatus = obj as TdApi.UpdateUserStatus

View file

@ -53,12 +53,13 @@ object TelegramUiHelper {
}
val type = chat.type
if (type is TdApi.ChatTypePrivate || type is TdApi.ChatTypeSecret) {
val userId = getUserIdFromChatType(type)
val userId = helper.getUserIdFromChatType(type)
val chatWithBot = helper.isBot(userId)
res.privateChat = true
res.chatWithBot = chatWithBot
if (!chatWithBot) {
res.userId = userId
val user = helper.getUser(userId)
val message = messages.firstOrNull { it.viaBotUserId == 0 }
if (message != null) {
res.lastUpdated = helper.getLastUpdatedTime(message)
@ -67,6 +68,9 @@ object TelegramUiHelper {
res.latLon = LatLon(content.location.latitude, content.location.longitude)
}
}
if (user != null) {
res.greyScaledPhotoPath = helper.getUserGreyPhotoPath(user)
}
}
} else if (type is TdApi.ChatTypeBasicGroup) {
res.placeholderId = R.drawable.img_group_picture
@ -82,12 +86,6 @@ object TelegramUiHelper {
return res
}
private fun getUserIdFromChatType(type: TdApi.ChatType) = when (type) {
is TdApi.ChatTypePrivate -> type.userId
is TdApi.ChatTypeSecret -> type.userId
else -> 0
}
fun getUserName(user: TdApi.User): String {
var name = "${user.firstName} ${user.lastName}".trim()
if (name.isEmpty()) {
@ -143,6 +141,7 @@ object TelegramUiHelper {
name = TelegramUiHelper.getUserName(user)
latLon = LatLon(content.location.latitude, content.location.longitude)
photoPath = helper.getUserPhotoPath(user)
greyScaledPhotoPath = helper.getUserGreyPhotoPath(user)
placeholderId = R.drawable.img_user_picture
userId = message.senderUserId
lastUpdated = helper.getLastUpdatedTime(message)
@ -159,6 +158,8 @@ object TelegramUiHelper {
internal set
var photoPath: String? = null
internal set
var greyScaledPhotoPath: String? = null
internal set
var placeholderId: Int = 0
internal set
var userId: Int = 0

View file

@ -311,7 +311,9 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
val canBeOpenedOnMap = item.canBeOpenedOnMap()
val openOnMapView = holder.getOpenOnMapClickView()
TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, item.placeholderId, false)
val staleLocation = System.currentTimeMillis() / 1000 - item.lastUpdated > settings.staleLocTime
TelegramUiHelper.setupPhoto(app, holder.icon, if (staleLocation) item.greyScaledPhotoPath else item.photoPath,
item.placeholderId, false)
holder.title?.text = item.getVisibleName()
openOnMapView?.isEnabled = canBeOpenedOnMap
if (canBeOpenedOnMap) {
@ -327,7 +329,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
}
if (location != null && item.latLon != null) {
holder.locationViewContainer?.visibility = if (item.lastUpdated > 0) View.VISIBLE else View.GONE
locationViewCache.outdatedLocation = System.currentTimeMillis() / 1000 - item.lastUpdated > settings.staleLocTime
locationViewCache.outdatedLocation = staleLocation
app.uiUtils.updateLocationView(
holder.directionIcon,
holder.distanceText,

View file

@ -34,7 +34,7 @@ private const val LIVE_NOW_TAB_POS = 1
class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListener {
private val log = PlatformUtil.getLog(TelegramHelper::class.java)
private val log = PlatformUtil.getLog(MainActivity::class.java)
private var telegramAuthorizationRequestHandler: TelegramAuthorizationRequestHandler? = null
private var paused: Boolean = false
@ -213,15 +213,20 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
}
override fun onTelegramChatChanged(chat: TdApi.Chat) {
val user = telegramHelper.getUser(telegramHelper.getUserIdFromChatType(chat.type))
if (user != null) {
app.uiUtils.checkUserGreyscaleImage(user)
}
runOnUi {
listeners.forEach { it.get()?.onTelegramChatChanged(chat) }
}
}
override fun onTelegramUserChanged(user: TdApi.User) {
app.uiUtils.checkUserGreyscaleImage(user)
val message = telegramHelper.getUserMessage(user)
if (message != null) {
app.showLocationHelper.addLocationToMap(message)
app.showLocationHelper.addOrUpdateLocationOnMap(message)
}
runOnUi {
listeners.forEach { it.get()?.onTelegramUserChanged(user) }

View file

@ -16,13 +16,22 @@ import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import net.osmand.Location
import net.osmand.PlatformUtil
import net.osmand.data.LatLon
import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.ui.views.DirectionDrawable
import org.drinkless.td.libcore.telegram.TdApi
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.util.*
const val PROFILE_GREY_PHOTOS_DIR = "profile_grey_photos/"
class UiUtils(private val app: TelegramApplication) {
private val log = PlatformUtil.getLog(UiUtils::class.java)
private val drawableCache = LinkedHashMap<Long, Drawable>()
private val circleBitmapCache = LinkedHashMap<String, Bitmap>()
@ -107,6 +116,52 @@ class UiUtils(private val app: TelegramApplication) {
return getDrawable(id, if (light) R.color.icon_light else 0)
}
fun checkUserGreyscaleImage(user: TdApi.User) {
val path = user.profilePhoto?.small?.local?.path
if (path != null && app.telegramHelper.getUserGreyPhotoPath(user) == null) {
app.uiUtils.convertToGrayscaleAndSave(path,
"${app.filesDir.absolutePath}/$PROFILE_GREY_PHOTOS_DIR${user.id}.png")
}
}
fun convertToGrayscaleAndSave(coloredImagePath: String, newFilePath: String) {
val currentImage = BitmapFactory.decodeFile(coloredImagePath)
val greyedImage = toGrayscale(currentImage)
saveBitmap(greyedImage, newFilePath)
}
private fun toGrayscale(bmpOriginal: Bitmap): Bitmap {
val bmpGrayscale = Bitmap.createBitmap(bmpOriginal.width, bmpOriginal.height, Bitmap.Config.ARGB_8888)
val c = Canvas(bmpGrayscale)
val paint = Paint()
val cm = ColorMatrix()
cm.setSaturation(0f)
val f = ColorMatrixColorFilter(cm)
paint.colorFilter = f
c.drawBitmap(bmpOriginal, 0f, 0f, paint)
return bmpGrayscale
}
private fun saveBitmap(bitmap: Bitmap, newFilePath: String) {
var fout: FileOutputStream? = null
try {
val file = File(newFilePath)
if (file.parentFile != null) {
file.parentFile.mkdirs()
}
fout = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout)
} catch (e: Exception) {
log.error(e)
} finally {
try {
fout?.close()
} catch (e: IOException) {
log.error(e)
}
}
}
private fun createCircleBitmap(source: Bitmap, recycleSource: Boolean = false): Bitmap {
val size = Math.min(source.width, source.height)