From ea1c74a97c95f548bc34a3560d2d6064621398ef Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 21 Aug 2018 19:15:01 +0300 Subject: [PATCH 1/7] add stale location icons --- .../net/osmand/telegram/TelegramService.kt | 2 +- .../telegram/helpers/ShowLocationHelper.kt | 40 ++++++++++---- .../osmand/telegram/helpers/TelegramHelper.kt | 23 ++++++++ .../telegram/helpers/TelegramUiHelper.kt | 15 ++--- .../osmand/telegram/ui/LiveNowTabFragment.kt | 6 +- .../net/osmand/telegram/ui/MainActivity.kt | 9 ++- .../src/net/osmand/telegram/utils/UiUtils.kt | 55 +++++++++++++++++++ 7 files changed, 127 insertions(+), 23 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt index 56f80b662f..dfbc240693 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt @@ -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 } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 0c4cc50d9c..c9bec9e805 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -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) } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index e787c6036c..cafbfb35c8 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -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 diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt index 2146140780..5682c74da7 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt @@ -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 diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 184fc5896e..a1fcbcc2d8 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -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, diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index e1b41b3c0f..5e11c0d1b2 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -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) } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt index aef147a999..03c791a4e5 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt @@ -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() private val circleBitmapCache = LinkedHashMap() @@ -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) From 8b07bbfb5d5da0c6ffb2acb13666493611df63b3 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 22 Aug 2018 14:12:40 +0300 Subject: [PATCH 2/7] add check for chat icon --- .../osmand/telegram/helpers/TelegramHelper.kt | 16 ++++++---------- .../osmand/telegram/ui/LiveNowTabFragment.kt | 1 + .../net/osmand/telegram/ui/MainActivity.kt | 5 ----- .../src/net/osmand/telegram/utils/UiUtils.kt | 19 +++++++++++++------ 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index cafbfb35c8..9734a4ac79 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -3,7 +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 net.osmand.telegram.utils.* import org.drinkless.td.libcore.telegram.Client import org.drinkless.td.libcore.telegram.Client.ResultHandler import org.drinkless.td.libcore.telegram.TdApi @@ -311,13 +311,10 @@ class TelegramHelper private constructor() { } fun getUserGreyPhotoPath(user: TdApi.User): String? { - return if (hasLocalGreyUserPhoto(user)) { - "$appDir/$PROFILE_GREY_PHOTOS_DIR${user.id}.png" + return if (hasLocalGreyUserPhoto(user.id)) { + "$appDir/$PROFILE_GREY_PHOTOS_DIR${user.id}$SAVED_GREY_PHOTOS_EXT" } else { - if (!hasLocalUserPhoto(user) && hasRemoteUserPhoto(user)) { - requestUserPhoto(user) - } - null + getUserPhotoPath(user) } } @@ -359,8 +356,8 @@ 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() + fun hasLocalGreyUserPhoto(userId: Int): Boolean { + return File("$appDir/$PROFILE_GREY_PHOTOS_DIR$userId$SAVED_GREY_PHOTOS_EXT").exists() } private fun hasLocalUserPhoto(user: TdApi.User): Boolean { @@ -951,7 +948,6 @@ 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 diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index a1fcbcc2d8..c393ee536e 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -144,6 +144,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage override fun onSendLiveLocationError(code: Int, message: String) {} override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) { + app.uiUtils.checkUserPhotoFromChat(chatId) app.runInUIThread { updateList() } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index 5e11c0d1b2..e8eaad6202 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -213,17 +213,12 @@ 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.addOrUpdateLocationOnMap(message) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt index 03c791a4e5..00fc12ae4d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt @@ -21,7 +21,6 @@ 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 @@ -29,6 +28,8 @@ import java.util.* const val PROFILE_GREY_PHOTOS_DIR = "profile_grey_photos/" +const val SAVED_GREY_PHOTOS_EXT = ".png" + class UiUtils(private val app: TelegramApplication) { private val log = PlatformUtil.getLog(UiUtils::class.java) @@ -116,11 +117,17 @@ 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 checkUserPhotoFromChat(chatId: Long) { + val chat = app.telegramHelper.getChat(chatId) + val chatIconPath = chat?.photo?.small?.local?.path + if (chat != null && chatIconPath != null) { + val userId = app.telegramHelper.getUserIdFromChatType(chat.type) + if (userId != 0 && !app.telegramHelper.hasLocalGreyUserPhoto(userId)) { + convertToGrayscaleAndSave( + chatIconPath, + "${app.filesDir.absolutePath}/$PROFILE_GREY_PHOTOS_DIR$userId$SAVED_GREY_PHOTOS_EXT" + ) + } } } From 00c160a015206c1c50d0b7f1cc8fc34007e63a08 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 22 Aug 2018 18:53:21 +0300 Subject: [PATCH 3/7] add asyncTask for converting images --- .../telegram/helpers/ShowLocationHelper.kt | 39 ++++--- .../osmand/telegram/helpers/TelegramHelper.kt | 10 +- .../osmand/telegram/ui/LiveNowTabFragment.kt | 11 +- .../net/osmand/telegram/ui/MainActivity.kt | 17 ++- .../net/osmand/telegram/utils/AndroidUtils.kt | 9 ++ .../src/net/osmand/telegram/utils/UiUtils.kt | 110 ++++++++++-------- 6 files changed, 126 insertions(+), 70 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index c9bec9e805..f00f03c052 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -11,6 +11,8 @@ 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 android.net.Uri +import net.osmand.telegram.R class ShowLocationHelper(private val app: TelegramApplication) { @@ -30,7 +32,7 @@ class ShowLocationHelper(private val app: TelegramApplication) { } } - fun showLocationOnMap(item: ListItem) { + fun showLocationOnMap(item: ListItem, stale: Boolean = false) { if (item.latLon == null) { return } @@ -44,7 +46,7 @@ class ShowLocationHelper(private val app: TelegramApplication) { Color.WHITE, ALatLon(item.latLon!!.latitude, item.latLon!!.longitude), null, - generatePhotoParams(item.photoPath) + generatePhotoParams(if (stale) item.greyScaledPhotoPath else item.photoPath, stale) ) } } @@ -53,11 +55,11 @@ class ShowLocationHelper(private val app: TelegramApplication) { execOsmandApi { val messages = telegramHelper.getMessages() for (message in messages) { - val date = telegramHelper.getLastUpdatedTime(message) * 1000L - val messageShowingTime = System.currentTimeMillis() - date - if (messageShowingTime > app.settings.locHistoryTime * 1000L) { + val date = telegramHelper.getLastUpdatedTime(message) + val messageShowingTime = System.currentTimeMillis() / 1000 - date + if (messageShowingTime > app.settings.locHistoryTime) { removeMapPoint(message.chatId, message) - } else { + } else if (app.settings.isShowingChatOnMap(message.chatId)) { addOrUpdateLocationOnMap(message, true) } } @@ -72,6 +74,8 @@ class ShowLocationHelper(private val app: TelegramApplication) { if (chatTitle != null && content is TdApi.MessageLocation) { var userName = "" var photoPath: String? = null + val date = telegramHelper.getLastUpdatedTime(message) * 1000L + val stale = System.currentTimeMillis() - date > app.settings.staleLocTime * 1000L val user = telegramHelper.getUser(message.senderUserId) if (user != null) { userName = "${user.firstName} ${user.lastName}".trim() @@ -81,9 +85,7 @@ class ShowLocationHelper(private val app: TelegramApplication) { if (userName.isEmpty()) { userName = user.phoneNumber } - val date = telegramHelper.getLastUpdatedTime(message) * 1000L - val expired = System.currentTimeMillis() - date > app.settings.staleLocTime * 1000L - photoPath = if (expired) { + photoPath = if (stale) { telegramHelper.getUserGreyPhotoPath(user) } else { telegramHelper.getUserPhotoPath(user) @@ -93,7 +95,7 @@ class ShowLocationHelper(private val app: TelegramApplication) { userName = message.senderUserId.toString() } setupMapLayer() - val params = generatePhotoParams(photoPath) + 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) @@ -153,11 +155,20 @@ class ShowLocationHelper(private val app: TelegramApplication) { } } - private fun generatePhotoParams(photoPath: String?): Map? { - if (TextUtils.isEmpty(photoPath)) { - return null + private fun generatePhotoParams(photoPath: String?, stale: Boolean = false): Map? { + val photoUri = if (TextUtils.isEmpty(photoPath)) { + if (stale) { + AndroidUtils.resourceToUri(app, R.drawable.img_user_picture) + } else { + AndroidUtils.resourceToUri(app, R.drawable.img_user_picture_active) + } + } else { + AndroidUtils.getUriForFile(app, File(photoPath)) } - val photoUri = AndroidUtils.getUriForFile(app, File(photoPath)) + return generatePhotoParamsFromUri(photoUri) + } + + private fun generatePhotoParamsFromUri(photoUri: Uri): Map? { app.grantUriPermission( app.settings.appToConnectPackage, photoUri, diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 9734a4ac79..2170a92a2d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -311,10 +311,10 @@ class TelegramHelper private constructor() { } fun getUserGreyPhotoPath(user: TdApi.User): String? { - return if (hasLocalGreyUserPhoto(user.id)) { - "$appDir/$PROFILE_GREY_PHOTOS_DIR${user.id}$SAVED_GREY_PHOTOS_EXT" + return if (hasGrayscaleUserPhoto(user.id)) { + "$appDir/$PROFILE_GRAYSCALE_PHOTOS_DIR${user.id}$SAVED_GRAYSCALE_PHOTOS_EXT" } else { - getUserPhotoPath(user) + null } } @@ -356,8 +356,8 @@ class TelegramHelper private constructor() { updateLiveMessagesExecutor?.awaitTermination(1, TimeUnit.MINUTES) } - fun hasLocalGreyUserPhoto(userId: Int): Boolean { - return File("$appDir/$PROFILE_GREY_PHOTOS_DIR$userId$SAVED_GREY_PHOTOS_EXT").exists() + fun hasGrayscaleUserPhoto(userId: Int): Boolean { + return File("$appDir/$PROFILE_GRAYSCALE_PHOTOS_DIR$userId$SAVED_GRAYSCALE_PHOTOS_EXT").exists() } private fun hasLocalUserPhoto(user: TdApi.User): Boolean { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index c393ee536e..b4406044e8 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -144,7 +144,6 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage override fun onSendLiveLocationError(code: Int, message: String) {} override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) { - app.uiUtils.checkUserPhotoFromChat(chatId) app.runInUIThread { updateList() } } @@ -313,8 +312,12 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val openOnMapView = holder.getOpenOnMapClickView() val staleLocation = System.currentTimeMillis() / 1000 - item.lastUpdated > settings.staleLocTime - TelegramUiHelper.setupPhoto(app, holder.icon, if (staleLocation) item.greyScaledPhotoPath else item.photoPath, - item.placeholderId, false) + if (staleLocation) { + TelegramUiHelper.setupPhoto(app, holder.icon, item.greyScaledPhotoPath, item.placeholderId, false) + } else { + TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, R.drawable.img_user_picture_active, false) + } + holder.title?.text = item.getVisibleName() openOnMapView?.isEnabled = canBeOpenedOnMap if (canBeOpenedOnMap) { @@ -322,7 +325,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage if (!isOsmAndInstalled()) { showOsmAndMissingDialog() } else { - app.showLocationHelper.showLocationOnMap(item) + app.showLocationHelper.showLocationOnMap(item, staleLocation) } } } else { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index e8eaad6202..b727ba5460 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -32,7 +32,7 @@ private const val PERMISSION_REQUEST_LOCATION = 1 private const val MY_LOCATION_TAB_POS = 0 private const val LIVE_NOW_TAB_POS = 1 -class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListener { +class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListener, TelegramIncomingMessagesListener { private val log = PlatformUtil.getLog(MainActivity::class.java) @@ -143,6 +143,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene if (telegramHelper.listener != this) { telegramHelper.listener = this } + telegramHelper.addIncomingMessagesListener(this) app.locationProvider.checkIfLastKnownLocationIsValid() @@ -159,6 +160,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene override fun onPause() { super.onPause() telegramHelper.listener = null + telegramHelper.removeIncomingMessagesListener(this) app.locationProvider.pauseAllUpdates() @@ -207,18 +209,23 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } override fun onTelegramChatsChanged() { + telegramHelper.getMessagesByChatIds().forEach { + app.uiUtils.checkUserPhotoFromChat(it.key) + } runOnUi { listeners.forEach { it.get()?.onTelegramChatsChanged() } } } override fun onTelegramChatChanged(chat: TdApi.Chat) { + app.uiUtils.checkUserPhotoFromChat(chat.id) runOnUi { listeners.forEach { it.get()?.onTelegramChatChanged(chat) } } } override fun onTelegramUserChanged(user: TdApi.User) { + app.uiUtils.checkUserGreyPhoto(user.id, telegramHelper.getUserPhotoPath(user)) val message = telegramHelper.getUserMessage(user) if (message != null) { app.showLocationHelper.addOrUpdateLocationOnMap(message) @@ -243,6 +250,14 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } } + override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) { + app.uiUtils.checkUserPhotoFromChat(chatId) + } + + override fun onDeleteChatLocationMessages(chatId: Long, messages: List) {} + + override fun updateLocationMessages() {} + override fun switchButtonsVisibility(visible: Boolean) { val buttonsVisibility = if (visible) View.VISIBLE else View.GONE if (buttonsBar.visibility != buttonsVisibility) { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt index df73fd121a..6133e0fdc3 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt @@ -2,6 +2,7 @@ package net.osmand.telegram.utils import android.Manifest import android.app.Activity +import android.content.ContentResolver import android.content.Context import android.content.Intent import android.content.pm.PackageManager @@ -113,6 +114,14 @@ object AndroidUtils { } } + fun resourceToUri(ctx: Context, resID: Int): Uri { + return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + + "://${ctx.resources.getResourcePackageName(resID)}" + + "/${ctx.resources.getResourceTypeName(resID)}" + + "/${ctx.resources.getResourceEntryName(resID)}" + ) + } + fun isAppInstalled(ctx: Context, appPackage: String): Boolean { try { ctx.packageManager.getPackageInfo(appPackage, 0) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt index 00fc12ae4d..1652d33a2a 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt @@ -6,6 +6,7 @@ import android.graphics.drawable.Drawable import android.graphics.drawable.LayerDrawable import android.hardware.Sensor import android.hardware.SensorManager +import android.os.AsyncTask import android.support.annotation.ColorInt import android.support.annotation.ColorRes import android.support.annotation.DrawableRes @@ -26,14 +27,12 @@ import java.io.FileOutputStream import java.io.IOException import java.util.* -const val PROFILE_GREY_PHOTOS_DIR = "profile_grey_photos/" +const val PROFILE_GRAYSCALE_PHOTOS_DIR = "profile_grayscale_photos/" -const val SAVED_GREY_PHOTOS_EXT = ".png" +const val SAVED_GRAYSCALE_PHOTOS_EXT = ".jpeg" class UiUtils(private val app: TelegramApplication) { - private val log = PlatformUtil.getLog(UiUtils::class.java) - private val drawableCache = LinkedHashMap() private val circleBitmapCache = LinkedHashMap() @@ -121,51 +120,17 @@ class UiUtils(private val app: TelegramApplication) { val chat = app.telegramHelper.getChat(chatId) val chatIconPath = chat?.photo?.small?.local?.path if (chat != null && chatIconPath != null) { - val userId = app.telegramHelper.getUserIdFromChatType(chat.type) - if (userId != 0 && !app.telegramHelper.hasLocalGreyUserPhoto(userId)) { - convertToGrayscaleAndSave( - chatIconPath, - "${app.filesDir.absolutePath}/$PROFILE_GREY_PHOTOS_DIR$userId$SAVED_GREY_PHOTOS_EXT" - ) - } + checkUserGreyPhoto(app.telegramHelper.getUserIdFromChatType(chat.type), chatIconPath) } } - - 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) - } + fun checkUserGreyPhoto(userId: Int, userOriginalPhotoPath: String?) { + if (userId != 0 && !app.telegramHelper.hasGrayscaleUserPhoto(userId)) { + ConvertPhotoToGrayscale().executeOnExecutor( + AsyncTask.THREAD_POOL_EXECUTOR, + userOriginalPhotoPath, + "${app.filesDir.absolutePath}/$PROFILE_GRAYSCALE_PHOTOS_DIR$userId$SAVED_GRAYSCALE_PHOTOS_EXT" + ) } } @@ -270,4 +235,57 @@ class UiUtils(private val app: TelegramApplication) { var screenOrientation: Int = 0 var outdatedLocation: Boolean = false } + + private class ConvertPhotoToGrayscale : AsyncTask() { + + private val log = PlatformUtil.getLog(ConvertPhotoToGrayscale::class.java) + + override fun doInBackground(vararg params: String?): Void? { + val userOriginalPhotoPath = params[0] + val userGrayScalePhotoPath = params[1] + if (userOriginalPhotoPath != null && userGrayScalePhotoPath != null) { + convertToGrayscaleAndSave(userOriginalPhotoPath, userGrayScalePhotoPath) + } + return null + } + + 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.JPEG, 100, fout) + } catch (e: Exception) { + log.error(e) + } finally { + try { + fout?.close() + } catch (e: IOException) { + log.error(e) + } + } + } + } } From 76d238b2406fcce52d8fdedf52d1b83750171f5f Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 22 Aug 2018 19:04:11 +0300 Subject: [PATCH 4/7] remove unnecessary changes --- .../src/net/osmand/telegram/helpers/ShowLocationHelper.kt | 4 ++-- OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt | 2 +- OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index f00f03c052..32ff1e8b8c 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -74,8 +74,8 @@ class ShowLocationHelper(private val app: TelegramApplication) { if (chatTitle != null && content is TdApi.MessageLocation) { var userName = "" var photoPath: String? = null - val date = telegramHelper.getLastUpdatedTime(message) * 1000L - val stale = System.currentTimeMillis() - date > app.settings.staleLocTime * 1000L + val date = telegramHelper.getLastUpdatedTime(message) + val stale = System.currentTimeMillis() / 1000 - date > app.settings.staleLocTime val user = telegramHelper.getUser(message.senderUserId) if (user != null) { userName = "${user.firstName} ${user.lastName}".trim() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index b727ba5460..225879cdff 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -34,7 +34,7 @@ private const val LIVE_NOW_TAB_POS = 1 class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListener, TelegramIncomingMessagesListener { - private val log = PlatformUtil.getLog(MainActivity::class.java) + private val log = PlatformUtil.getLog(TelegramHelper::class.java) private var telegramAuthorizationRequestHandler: TelegramAuthorizationRequestHandler? = null private var paused: Boolean = false diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt index 1652d33a2a..5a9cb81b00 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt @@ -32,7 +32,6 @@ const val PROFILE_GRAYSCALE_PHOTOS_DIR = "profile_grayscale_photos/" const val SAVED_GRAYSCALE_PHOTOS_EXT = ".jpeg" class UiUtils(private val app: TelegramApplication) { - private val drawableCache = LinkedHashMap() private val circleBitmapCache = LinkedHashMap() From ef915383e58a6ed31057e9f1f593f951bf07bf32 Mon Sep 17 00:00:00 2001 From: Chumva Date: Thu, 23 Aug 2018 10:41:14 +0300 Subject: [PATCH 5/7] rename checkUserGrayPhoto --- .../net/osmand/telegram/helpers/ShowLocationHelper.kt | 2 +- .../net/osmand/telegram/helpers/TelegramUiHelper.kt | 6 +++--- .../src/net/osmand/telegram/ui/LiveNowTabFragment.kt | 2 +- .../src/net/osmand/telegram/ui/MainActivity.kt | 2 +- .../src/net/osmand/telegram/utils/UiUtils.kt | 11 +++++------ 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 32ff1e8b8c..771dcfd459 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -46,7 +46,7 @@ class ShowLocationHelper(private val app: TelegramApplication) { Color.WHITE, ALatLon(item.latLon!!.latitude, item.latLon!!.longitude), null, - generatePhotoParams(if (stale) item.greyScaledPhotoPath else item.photoPath, stale) + generatePhotoParams(if (stale) item.grayscalePhotoPath else item.photoPath, stale) ) } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt index 5682c74da7..267077a777 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramUiHelper.kt @@ -69,7 +69,7 @@ object TelegramUiHelper { } } if (user != null) { - res.greyScaledPhotoPath = helper.getUserGreyPhotoPath(user) + res.grayscalePhotoPath = helper.getUserGreyPhotoPath(user) } } } else if (type is TdApi.ChatTypeBasicGroup) { @@ -141,7 +141,7 @@ object TelegramUiHelper { name = TelegramUiHelper.getUserName(user) latLon = LatLon(content.location.latitude, content.location.longitude) photoPath = helper.getUserPhotoPath(user) - greyScaledPhotoPath = helper.getUserGreyPhotoPath(user) + grayscalePhotoPath = helper.getUserGreyPhotoPath(user) placeholderId = R.drawable.img_user_picture userId = message.senderUserId lastUpdated = helper.getLastUpdatedTime(message) @@ -158,7 +158,7 @@ object TelegramUiHelper { internal set var photoPath: String? = null internal set - var greyScaledPhotoPath: String? = null + var grayscalePhotoPath: String? = null internal set var placeholderId: Int = 0 internal set diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index b4406044e8..f96ce8139d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -313,7 +313,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage val staleLocation = System.currentTimeMillis() / 1000 - item.lastUpdated > settings.staleLocTime if (staleLocation) { - TelegramUiHelper.setupPhoto(app, holder.icon, item.greyScaledPhotoPath, item.placeholderId, false) + TelegramUiHelper.setupPhoto(app, holder.icon, item.grayscalePhotoPath, item.placeholderId, false) } else { TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, R.drawable.img_user_picture_active, false) } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index 225879cdff..a8aeb9acf6 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -225,7 +225,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } override fun onTelegramUserChanged(user: TdApi.User) { - app.uiUtils.checkUserGreyPhoto(user.id, telegramHelper.getUserPhotoPath(user)) + app.uiUtils.checkUserGrayPhoto(user.id, telegramHelper.getUserPhotoPath(user)) val message = telegramHelper.getUserMessage(user) if (message != null) { app.showLocationHelper.addOrUpdateLocationOnMap(message) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt index 5a9cb81b00..b854121747 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt @@ -119,11 +119,11 @@ class UiUtils(private val app: TelegramApplication) { val chat = app.telegramHelper.getChat(chatId) val chatIconPath = chat?.photo?.small?.local?.path if (chat != null && chatIconPath != null) { - checkUserGreyPhoto(app.telegramHelper.getUserIdFromChatType(chat.type), chatIconPath) + checkUserGrayPhoto(app.telegramHelper.getUserIdFromChatType(chat.type), chatIconPath) } } - fun checkUserGreyPhoto(userId: Int, userOriginalPhotoPath: String?) { + fun checkUserGrayPhoto(userId: Int, userOriginalPhotoPath: String?) { if (userId != 0 && !app.telegramHelper.hasGrayscaleUserPhoto(userId)) { ConvertPhotoToGrayscale().executeOnExecutor( AsyncTask.THREAD_POOL_EXECUTOR, @@ -250,13 +250,12 @@ class UiUtils(private val app: TelegramApplication) { fun convertToGrayscaleAndSave(coloredImagePath: String, newFilePath: String) { val currentImage = BitmapFactory.decodeFile(coloredImagePath) - val greyedImage = toGrayscale(currentImage) - saveBitmap(greyedImage, newFilePath) + val grayscaleImage = toGrayscale(currentImage) + saveBitmap(grayscaleImage, newFilePath) } private fun toGrayscale(bmpOriginal: Bitmap): Bitmap { - val bmpGrayscale = - Bitmap.createBitmap(bmpOriginal.width, bmpOriginal.height, Bitmap.Config.ARGB_8888) + val bmpGrayscale = Bitmap.createBitmap(bmpOriginal.width, bmpOriginal.height, Bitmap.Config.ARGB_8888) val c = Canvas(bmpGrayscale) val paint = Paint() val cm = ColorMatrix() From 6bfece0744de3a32b1404527dc6e664a42f3a23f Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 28 Aug 2018 14:58:44 +0300 Subject: [PATCH 6/7] remove telegram logic from uiUtils --- .../telegram/helpers/ShowLocationHelper.kt | 7 ++-- .../net/osmand/telegram/ui/MainActivity.kt | 32 ++++++++++++++++--- .../src/net/osmand/telegram/utils/UiUtils.kt | 14 ++------ 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 771dcfd459..8c32b78684 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -157,11 +157,12 @@ class ShowLocationHelper(private val app: TelegramApplication) { private fun generatePhotoParams(photoPath: String?, stale: Boolean = false): Map? { val photoUri = if (TextUtils.isEmpty(photoPath)) { - if (stale) { - AndroidUtils.resourceToUri(app, R.drawable.img_user_picture) + val resId = if (stale) { + R.drawable.img_user_picture } else { - AndroidUtils.resourceToUri(app, R.drawable.img_user_picture_active) + R.drawable.img_user_picture_active } + AndroidUtils.resourceToUri(app, resId) } else { AndroidUtils.getUriForFile(app, File(photoPath)) } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index 96de59aa3d..c2664faf11 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -25,6 +25,8 @@ import net.osmand.telegram.ui.LoginDialogFragment.LoginDialogType import net.osmand.telegram.ui.MyLocationTabFragment.ActionButtonsListener import net.osmand.telegram.ui.views.LockableViewPager import net.osmand.telegram.utils.AndroidUtils +import net.osmand.telegram.utils.PROFILE_GRAYSCALE_PHOTOS_DIR +import net.osmand.telegram.utils.SAVED_GRAYSCALE_PHOTOS_EXT import org.drinkless.td.libcore.telegram.TdApi import java.lang.ref.WeakReference @@ -221,8 +223,8 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } override fun onTelegramChatsChanged() { - telegramHelper.getMessagesByChatIds().forEach { - app.uiUtils.checkUserPhotoFromChat(it.key) + telegramHelper.getMessagesByChatIds(settings.locHistoryTime).forEach { + checkUserPhotoFromChat(it.key) } runOnUi { listeners.forEach { it.get()?.onTelegramChatsChanged() } @@ -230,14 +232,17 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } override fun onTelegramChatChanged(chat: TdApi.Chat) { - app.uiUtils.checkUserPhotoFromChat(chat.id) + checkUserPhotoFromChat(chat.id) runOnUi { listeners.forEach { it.get()?.onTelegramChatChanged(chat) } } } override fun onTelegramUserChanged(user: TdApi.User) { - app.uiUtils.checkUserGrayPhoto(user.id, telegramHelper.getUserPhotoPath(user)) + val photoPath = telegramHelper.getUserPhotoPath(user) + if (photoPath != null) { + checkUserGrayPhoto(user.id, photoPath) + } val message = telegramHelper.getUserMessage(user) if (message != null) { app.showLocationHelper.addOrUpdateLocationOnMap(message) @@ -263,7 +268,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) { - app.uiUtils.checkUserPhotoFromChat(chatId) + checkUserPhotoFromChat(chatId) } override fun onDeleteChatLocationMessages(chatId: Long, messages: List) {} @@ -346,6 +351,23 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } } + private fun checkUserPhotoFromChat(chatId: Long) { + val chat = app.telegramHelper.getChat(chatId) + val chatIconPath = chat?.photo?.small?.local?.path + if (chat != null && chatIconPath != null) { + checkUserGrayPhoto(app.telegramHelper.getUserIdFromChatType(chat.type), chatIconPath) + } + } + + private fun checkUserGrayPhoto(userId: Int, userOriginalPhotoPath: String) { + if (userId != 0 && !app.telegramHelper.hasGrayscaleUserPhoto(userId)) { + app.uiUtils.convertAndSaveUserGrayPhoto( + userOriginalPhotoPath, + "${app.filesDir.absolutePath}/$PROFILE_GRAYSCALE_PHOTOS_DIR$userId$SAVED_GRAYSCALE_PHOTOS_EXT" + ) + } + } + private fun isOsmAndInstalled() = AndroidUtils.isAppInstalled(this, settings.appToConnectPackage) private fun runOnUi(action: (() -> Unit)) { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt index b854121747..0e2c6812ac 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt @@ -115,20 +115,12 @@ class UiUtils(private val app: TelegramApplication) { return getDrawable(id, if (light) R.color.icon_light else 0) } - fun checkUserPhotoFromChat(chatId: Long) { - val chat = app.telegramHelper.getChat(chatId) - val chatIconPath = chat?.photo?.small?.local?.path - if (chat != null && chatIconPath != null) { - checkUserGrayPhoto(app.telegramHelper.getUserIdFromChatType(chat.type), chatIconPath) - } - } - - fun checkUserGrayPhoto(userId: Int, userOriginalPhotoPath: String?) { - if (userId != 0 && !app.telegramHelper.hasGrayscaleUserPhoto(userId)) { + fun convertAndSaveUserGrayPhoto(userOriginalPhotoPath: String, greyPhotoPath: String) { + if (File(userOriginalPhotoPath).exists()) { ConvertPhotoToGrayscale().executeOnExecutor( AsyncTask.THREAD_POOL_EXECUTOR, userOriginalPhotoPath, - "${app.filesDir.absolutePath}/$PROFILE_GRAYSCALE_PHOTOS_DIR$userId$SAVED_GRAYSCALE_PHOTOS_EXT" + greyPhotoPath ) } } From 8815e8968b47d1ffc1de71486be14975174a5b81 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 28 Aug 2018 15:26:48 +0300 Subject: [PATCH 7/7] rename constants and addGrayPhoto --- .../osmand/telegram/helpers/TelegramHelper.kt | 4 ++-- .../net/osmand/telegram/ui/MainActivity.kt | 24 +++++++++---------- .../src/net/osmand/telegram/utils/UiUtils.kt | 10 ++++---- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 278ff546b5..ef91f3b758 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -323,7 +323,7 @@ class TelegramHelper private constructor() { fun getUserGreyPhotoPath(user: TdApi.User): String? { return if (hasGrayscaleUserPhoto(user.id)) { - "$appDir/$PROFILE_GRAYSCALE_PHOTOS_DIR${user.id}$SAVED_GRAYSCALE_PHOTOS_EXT" + "$appDir/$GRAYSCALE_PHOTOS_DIR${user.id}$GRAYSCALE_PHOTOS_EXT" } else { null } @@ -368,7 +368,7 @@ class TelegramHelper private constructor() { } fun hasGrayscaleUserPhoto(userId: Int): Boolean { - return File("$appDir/$PROFILE_GRAYSCALE_PHOTOS_DIR$userId$SAVED_GRAYSCALE_PHOTOS_EXT").exists() + return File("$appDir/$GRAYSCALE_PHOTOS_DIR$userId$GRAYSCALE_PHOTOS_EXT").exists() } private fun hasLocalUserPhoto(user: TdApi.User): Boolean { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index c2664faf11..9f66f3de4c 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -25,8 +25,8 @@ import net.osmand.telegram.ui.LoginDialogFragment.LoginDialogType import net.osmand.telegram.ui.MyLocationTabFragment.ActionButtonsListener import net.osmand.telegram.ui.views.LockableViewPager import net.osmand.telegram.utils.AndroidUtils -import net.osmand.telegram.utils.PROFILE_GRAYSCALE_PHOTOS_DIR -import net.osmand.telegram.utils.SAVED_GRAYSCALE_PHOTOS_EXT +import net.osmand.telegram.utils.GRAYSCALE_PHOTOS_DIR +import net.osmand.telegram.utils.GRAYSCALE_PHOTOS_EXT import org.drinkless.td.libcore.telegram.TdApi import java.lang.ref.WeakReference @@ -224,7 +224,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene override fun onTelegramChatsChanged() { telegramHelper.getMessagesByChatIds(settings.locHistoryTime).forEach { - checkUserPhotoFromChat(it.key) + addGrayPhoto(it.key) } runOnUi { listeners.forEach { it.get()?.onTelegramChatsChanged() } @@ -232,7 +232,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } override fun onTelegramChatChanged(chat: TdApi.Chat) { - checkUserPhotoFromChat(chat.id) + addGrayPhoto(chat.id) runOnUi { listeners.forEach { it.get()?.onTelegramChatChanged(chat) } } @@ -241,7 +241,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene override fun onTelegramUserChanged(user: TdApi.User) { val photoPath = telegramHelper.getUserPhotoPath(user) if (photoPath != null) { - checkUserGrayPhoto(user.id, photoPath) + addGrayPhoto(user.id, photoPath) } val message = telegramHelper.getUserMessage(user) if (message != null) { @@ -268,7 +268,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) { - checkUserPhotoFromChat(chatId) + addGrayPhoto(chatId) } override fun onDeleteChatLocationMessages(chatId: Long, messages: List) {} @@ -351,19 +351,19 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } } - private fun checkUserPhotoFromChat(chatId: Long) { + private fun addGrayPhoto(chatId: Long) { val chat = app.telegramHelper.getChat(chatId) val chatIconPath = chat?.photo?.small?.local?.path if (chat != null && chatIconPath != null) { - checkUserGrayPhoto(app.telegramHelper.getUserIdFromChatType(chat.type), chatIconPath) + addGrayPhoto(app.telegramHelper.getUserIdFromChatType(chat.type), chatIconPath) } } - private fun checkUserGrayPhoto(userId: Int, userOriginalPhotoPath: String) { + private fun addGrayPhoto(userId: Int, originalPhotoPath: String) { if (userId != 0 && !app.telegramHelper.hasGrayscaleUserPhoto(userId)) { - app.uiUtils.convertAndSaveUserGrayPhoto( - userOriginalPhotoPath, - "${app.filesDir.absolutePath}/$PROFILE_GRAYSCALE_PHOTOS_DIR$userId$SAVED_GRAYSCALE_PHOTOS_EXT" + app.uiUtils.convertAndSaveGrayPhoto( + originalPhotoPath, + "${app.filesDir.absolutePath}/$GRAYSCALE_PHOTOS_DIR$userId$GRAYSCALE_PHOTOS_EXT" ) } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt index 0e2c6812ac..23bf249b1b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/UiUtils.kt @@ -27,9 +27,9 @@ import java.io.FileOutputStream import java.io.IOException import java.util.* -const val PROFILE_GRAYSCALE_PHOTOS_DIR = "profile_grayscale_photos/" +const val GRAYSCALE_PHOTOS_DIR = "grayscale_photos/" -const val SAVED_GRAYSCALE_PHOTOS_EXT = ".jpeg" +const val GRAYSCALE_PHOTOS_EXT = ".jpeg" class UiUtils(private val app: TelegramApplication) { private val drawableCache = LinkedHashMap() @@ -115,11 +115,11 @@ class UiUtils(private val app: TelegramApplication) { return getDrawable(id, if (light) R.color.icon_light else 0) } - fun convertAndSaveUserGrayPhoto(userOriginalPhotoPath: String, greyPhotoPath: String) { - if (File(userOriginalPhotoPath).exists()) { + fun convertAndSaveGrayPhoto(originalPhotoPath: String, greyPhotoPath: String) { + if (File(originalPhotoPath).exists()) { ConvertPhotoToGrayscale().executeOnExecutor( AsyncTask.THREAD_POOL_EXECUTOR, - userOriginalPhotoPath, + originalPhotoPath, greyPhotoPath ) }