Merge pull request #271 from Aga-C/add-contact-photo-notification

Added contact photo to notification
This commit is contained in:
Tibor Kaputa 2022-01-19 15:43:33 +01:00 committed by GitHub
commit 1edc254b71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,13 +3,19 @@ package com.simplemobiletools.smsmessenger.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Handler
import android.os.Looper
import android.provider.Telephony
import com.simplemobiletools.commons.extensions.getMyContactsCursor
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.simplemobiletools.commons.extensions.isNumberBlocked
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.extensions.*
import com.simplemobiletools.smsmessenger.helpers.refreshMessages
import com.simplemobiletools.smsmessenger.models.Message
@ -37,6 +43,8 @@ class SmsReceiver : BroadcastReceiver() {
threadId = context.getThreadId(address)
}
val bitmap = getPhotoForNotification(address, context)
Handler(Looper.getMainLooper()).post {
val privateCursor = context.getMyContactsCursor(false, true)?.loadInBackground()
if (!context.isNumberBlocked(address)) {
@ -65,9 +73,33 @@ class SmsReceiver : BroadcastReceiver() {
refreshMessages()
}
context.showReceivedMessageNotification(address, body, threadId, null)
context.showReceivedMessageNotification(address, body, threadId, bitmap)
}
}
}
}
private fun getPhotoForNotification(address: String, context: Context): Bitmap? {
val photo = SimpleContactsHelper(context).getPhotoUriFromPhoneNumber(address)
val size = context.resources.getDimension(R.dimen.notification_large_icon_size).toInt()
if (photo.isEmpty()) {
return null
}
val options = RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.centerCrop()
return try {
Glide.with(context)
.asBitmap()
.load(photo)
.apply(options)
.apply(RequestOptions.circleCropTransform())
.into(size, size)
.get()
} catch (e: Exception) {
null
}
}
}