add asyncTask for converting images
This commit is contained in:
parent
67f9b52b66
commit
00c160a015
6 changed files with 126 additions and 70 deletions
|
@ -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<String, String>? {
|
||||
if (TextUtils.isEmpty(photoPath)) {
|
||||
return null
|
||||
private fun generatePhotoParams(photoPath: String?, stale: Boolean = false): Map<String, String>? {
|
||||
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<String, String>? {
|
||||
app.grantUriPermission(
|
||||
app.settings.appToConnectPackage,
|
||||
photoUri,
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<TdApi.Message>) {}
|
||||
|
||||
override fun updateLocationMessages() {}
|
||||
|
||||
override fun switchButtonsVisibility(visible: Boolean) {
|
||||
val buttonsVisibility = if (visible) View.VISIBLE else View.GONE
|
||||
if (buttonsBar.visibility != buttonsVisibility) {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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<Long, Drawable>()
|
||||
private val circleBitmapCache = LinkedHashMap<String, Bitmap>()
|
||||
|
||||
|
@ -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<String, String, Void?>() {
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue