diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessageFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessageFragment.kt index 19fad980..c0416da7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessageFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessageFragment.kt @@ -4,12 +4,9 @@ package pl.szczodrzynski.edziennik.ui.modules.messages -import android.graphics.Color -import android.os.Build import android.os.Bundle import android.os.Environment import android.text.Html -import android.text.Html.FROM_HTML_MODE_COMPACT import android.text.TextUtils import android.view.Gravity.CENTER_VERTICAL import android.view.Gravity.END @@ -18,7 +15,6 @@ import android.view.View import android.view.ViewGroup import android.widget.FrameLayout import android.widget.ProgressBar -import androidx.core.graphics.ColorUtils import androidx.fragment.app.Fragment import com.google.android.material.chip.Chip import com.mikepenz.iconics.IconicsColor @@ -55,7 +51,6 @@ import pl.szczodrzynski.navlib.colorAttr import java.io.File import kotlin.coroutines.CoroutineContext import kotlin.math.min -import kotlin.text.RegexOption.IGNORE_CASE class MessageFragment : Fragment(), CoroutineScope { companion object { @@ -158,33 +153,7 @@ class MessageFragment : Fragment(), CoroutineScope { } private fun showMessage() { - val hexPattern = "(#[a-fA-F0-9]{6})" - val colorRegex = "(?:color=\"$hexPattern\")|(?:style=\"color: ?${hexPattern})" - .toRegex(IGNORE_CASE) - - var text = (message.body ?: "") - .replace("\\[META:[A-z0-9]+;[0-9-]+]".toRegex(), "") - .replace("background-color: ?$hexPattern;".toRegex(), "") - - colorRegex.findAll(text).forEach { result -> - val group = result.groups.drop(1).firstOrNull { it != null } ?: return@forEach - - val color = Color.parseColor(group.value) - val luminance = ColorUtils.calculateLuminance(color) - - if (Themes.isDark && luminance <= 0.5) { - text = text.replaceRange(group.range, "#FFFFFF") - } else if (!Themes.isDark && luminance > 0.5) { - text = text.replaceRange(group.range, "#000000") - } - } - - b.body.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - Html.fromHtml(text, FROM_HTML_MODE_COMPACT) - } else { - Html.fromHtml(text) - } - + b.body.text = MessagesUtils.htmlToSpannable(message.body ?: "") b.date.text = getString(R.string.messages_date_time_format, Date.fromMillis(message.addedDate).formattedStringShort, Time.fromMillis(message.addedDate).stringHM) val messageInfo = MessagesUtils.getMessageInfo(app, message, 40, 20, 14, 10) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesAdapter.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesAdapter.kt index db01eb29..8264ccd7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesAdapter.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesAdapter.kt @@ -1,7 +1,6 @@ package pl.szczodrzynski.edziennik.ui.modules.messages import android.graphics.Typeface -import android.text.Html import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -40,15 +39,9 @@ class MessagesAdapter(private val app: App, private val onItemClickListener: OnI b.messageSubject.text = message.subject b.messageDate.text = Date.fromMillis(message.addedDate).formattedStringShort b.messageAttachmentImage.visibility = if (message.hasAttachments()) View.VISIBLE else View.GONE - try { - b.messageBody.text = Html.fromHtml( - if (message.body == null) "" else message.body!! - .substring(0, message.body!!.length.coerceAtMost(200)) - .replace("\\[META:[A-z0-9]+;[0-9-]+]".toRegex(), "") - ) - } catch (e: Exception) { - // TODO ??? - } + + val text = message.body?.substring(0, message.body!!.length.coerceAtMost(200)) ?: "" + b.messageBody.text = MessagesUtils.htmlToSpannable(text) if (message.type == Message.TYPE_SENT || message.type == Message.TYPE_DRAFT || message.seen) { b.messageSender.setTextAppearance(b.messageSender.context, R.style.NavView_TextView_Small) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesUtils.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesUtils.java deleted file mode 100644 index 91ed9a56..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesUtils.java +++ /dev/null @@ -1,220 +0,0 @@ -package pl.szczodrzynski.edziennik.ui.modules.messages; - -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.RectF; - -import androidx.core.graphics.ColorUtils; -import pl.szczodrzynski.edziennik.App; -import pl.szczodrzynski.edziennik.R; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipientFull; -import pl.szczodrzynski.edziennik.utils.Colors; -import pl.szczodrzynski.edziennik.utils.Utils; - -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_DELETED; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_DRAFT; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_RECEIVED; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_SENT; - -public class MessagesUtils { - public static class MessageInfo { - public Bitmap profileImage; - public String profileName; - - public MessageInfo(Bitmap profileImage, String profileName) { - this.profileImage = profileImage; - this.profileName = profileName; - } - } - - private static String getInitials(String name) { - if (name == null || name.isEmpty()) - return ""; - name = name.toUpperCase(); - String[] nameParts = name.split(" "); - return nameParts.length <= 1 ? - (name.length() == 0 ? "?" : name.charAt(0))+"" : - (nameParts[0].length() == 0 ? "?" : nameParts[0].charAt(0)) - + "" + - (nameParts[1].length() == 0 ? "?" : nameParts[1].charAt(0)); - } - - private static int getPaintCenter(Paint textPaint) { - return Math.round((textPaint.descent() + textPaint.ascent()) / 2); - } - - public static Bitmap getProfileImage(int diameterDp, int textSizeBigDp, int textSizeMediumDp, int textSizeSmallDp, int count, String ... names) { - float diameter = Utils.dpToPx(diameterDp); - float textSizeBig = Utils.dpToPx(textSizeBigDp); - float textSizeMedium = Utils.dpToPx(textSizeMediumDp); - float textSizeSmall = Utils.dpToPx(textSizeSmallDp); - - Bitmap bitmap = Bitmap.createBitmap((int) diameter, (int) diameter, Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(bitmap); - - Paint circlePaint = new Paint(); - circlePaint.setFlags(Paint.ANTI_ALIAS_FLAG); - - Paint textPaint = new Paint(); - textPaint.setTextAlign(Paint.Align.CENTER); - textPaint.setFlags(Paint.ANTI_ALIAS_FLAG); - - RectF rectF = new RectF(); - rectF.set(0, 0, diameter, diameter); - - String name; - int color; - - if (count == 1) { - name = names[0]; - circlePaint.setColor(color = Colors.stringToMaterialColor(name)); - textPaint.setColor(ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f)); - textPaint.setTextSize(textSizeBig); - canvas.drawArc(rectF, 0, 360, true, circlePaint); - canvas.drawText(getInitials(name), diameter/2, diameter/2 - getPaintCenter(textPaint), textPaint); - } - else if (count == 2) { - // top - name = names[0]; - circlePaint.setColor(color = Colors.stringToMaterialColor(name)); - textPaint.setColor(ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f)); - textPaint.setTextSize(textSizeMedium); - canvas.drawArc(rectF, 180, 180, true, circlePaint); - canvas.drawText(getInitials(name), diameter/2, diameter/4 - getPaintCenter(textPaint), textPaint); - - // bottom - name = names[1]; - circlePaint.setColor(color = Colors.stringToMaterialColor(name)); - textPaint.setColor(ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f)); - textPaint.setTextSize(textSizeMedium); - canvas.drawArc(rectF, 0, 180, true, circlePaint); - canvas.drawText(getInitials(name), diameter/2, diameter/4*3 - getPaintCenter(textPaint), textPaint); - } - else if (count == 3) { - // upper left - name = names[0]; - circlePaint.setColor(color = Colors.stringToMaterialColor(name)); - textPaint.setColor(ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f)); - textPaint.setTextSize(textSizeSmall); - canvas.drawArc(rectF, 180, 90, true, circlePaint); - canvas.drawText(getInitials(name), diameter/4, diameter/4 - getPaintCenter(textPaint) + diameter/32, textPaint); - - // upper right - name = names[1]; - circlePaint.setColor(color = Colors.stringToMaterialColor(name)); - textPaint.setColor(ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f)); - textPaint.setTextSize(textSizeSmall); - canvas.drawArc(rectF, 270, 90, true, circlePaint); - canvas.drawText(getInitials(name), diameter/4*3, diameter/4 - getPaintCenter(textPaint) + diameter/32, textPaint); - - // bottom - name = names[2]; - circlePaint.setColor(color = Colors.stringToMaterialColor(name)); - textPaint.setColor(ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f)); - textPaint.setTextSize(textSizeMedium); - canvas.drawArc(rectF, 0, 180, true, circlePaint); - canvas.drawText(getInitials(name), diameter/2, diameter/4*3 - getPaintCenter(textPaint), textPaint); - } - else if (count >= 4) { - // upper left - name = names[0]; - circlePaint.setColor(color = Colors.stringToMaterialColor(name)); - textPaint.setColor(ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f)); - textPaint.setTextSize(textSizeSmall); - canvas.drawArc(rectF, 180, 90, true, circlePaint); - canvas.drawText(getInitials(name), diameter/4, diameter/4 - getPaintCenter(textPaint) + diameter/32, textPaint); - - // upper right - name = names[1]; - circlePaint.setColor(color = Colors.stringToMaterialColor(name)); - textPaint.setColor(ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f)); - textPaint.setTextSize(textSizeSmall); - canvas.drawArc(rectF, 270, 90, true, circlePaint); - canvas.drawText(getInitials(name), diameter/4*3, diameter/4 - getPaintCenter(textPaint) + diameter/32, textPaint); - - // bottom left - name = names[2]; - circlePaint.setColor(color = Colors.stringToMaterialColor(name)); - textPaint.setColor(ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f)); - textPaint.setTextSize(textSizeSmall); - canvas.drawArc(rectF, 90, 90, true, circlePaint); - canvas.drawText(getInitials(name), diameter/4, diameter/4*3 - getPaintCenter(textPaint) - diameter/32, textPaint); - - // bottom right - if (count == 4) - name = names[3]; - if (count > 4) - name = "..."; - circlePaint.setColor(color = Colors.stringToMaterialColor(name)); - textPaint.setColor(ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f)); - textPaint.setTextSize(textSizeSmall); - canvas.drawArc(rectF, 0, 90, true, circlePaint); - canvas.drawText(count > 4 ? "+"+(count-3) : getInitials(name), diameter/4*3, diameter/4*3 - getPaintCenter(textPaint) - diameter/32, textPaint); - } - - return bitmap; - } - - public static MessageInfo getMessageInfo(App app, MessageFull message, int diameterDp, int textSizeBigDp, int textSizeMediumDp, int textSizeSmallDp) { - Bitmap profileImage = null; - String profileName = null; - if (message.type == TYPE_RECEIVED || message.type == TYPE_DELETED) { - profileName = message.senderFullName; - profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, 1, message.senderFullName); - } - else if (message.type == TYPE_SENT || message.type == TYPE_DRAFT && message.recipients != null) { - int count = message.recipients == null ? 0 : message.recipients.size(); - if (count == 0) { - profileName = app.getString(R.string.messages_draft_title); - profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, 1, "?"); - } - else if (count == 1) { - MessageRecipientFull recipient = message.recipients.get(0); - profileName = recipient.fullName; - profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, 1, recipient.fullName); - } - else if (count == 2) { - MessageRecipientFull recipient1 = message.recipients.get(0); - MessageRecipientFull recipient2 = message.recipients.get(1); - profileName = recipient1.fullName+", "+recipient2.fullName; - profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, 2, recipient1.fullName, recipient2.fullName); - } - else if (count == 3) { - MessageRecipientFull recipient1 = message.recipients.get(0); - MessageRecipientFull recipient2 = message.recipients.get(1); - MessageRecipientFull recipient3 = message.recipients.get(2); - profileName = recipient1.fullName+", "+recipient2.fullName+", "+recipient3.fullName; - profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, 3, recipient1.fullName, recipient2.fullName, recipient3.fullName); - } - else if (count == 4) { - MessageRecipientFull recipient1 = message.recipients.get(0); - MessageRecipientFull recipient2 = message.recipients.get(1); - MessageRecipientFull recipient3 = message.recipients.get(2); - MessageRecipientFull recipient4 = message.recipients.get(3); - profileName = recipient1.fullName+", "+recipient2.fullName+", "+recipient3.fullName+", "+recipient4.fullName; - profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, 4, recipient1.fullName, recipient2.fullName, recipient3.fullName, recipient4.fullName); - } - else { - MessageRecipientFull recipient1 = message.recipients.get(0); - MessageRecipientFull recipient2 = message.recipients.get(1); - MessageRecipientFull recipient3 = message.recipients.get(2); - - StringBuilder senderText = new StringBuilder(); - boolean first = true; - for (MessageRecipientFull recipient: message.recipients) { - if (!first) { - senderText.append(", "); - } - first = false; - senderText.append(recipient.fullName); - } - profileName = senderText.toString(); - - profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, count, recipient1.fullName, recipient2.fullName, recipient3.fullName); - } - } - return new MessageInfo(profileImage, profileName); - } -} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesUtils.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesUtils.kt new file mode 100644 index 00000000..bd79a5b0 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/messages/MessagesUtils.kt @@ -0,0 +1,218 @@ +package pl.szczodrzynski.edziennik.ui.modules.messages + +import android.graphics.* +import android.os.Build +import android.text.Html +import android.text.Spanned +import androidx.core.graphics.ColorUtils +import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.data.db.modules.messages.Message +import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull +import pl.szczodrzynski.edziennik.utils.Colors +import pl.szczodrzynski.edziennik.utils.Themes +import pl.szczodrzynski.edziennik.utils.Utils +import kotlin.math.roundToInt + +object MessagesUtils { + private fun getInitials(name: String?): String { + if (name == null || name.isEmpty()) return "" + val nameUppercase = name.toUpperCase() + val nameParts = nameUppercase.split(" ").toTypedArray() + return if (nameParts.size <= 1) (if (nameUppercase.isEmpty()) '?' else nameUppercase[0]).toString() + else (if (nameParts[0].isEmpty()) '?' else nameParts[0][0]).toString() + + if (nameParts[1].isEmpty()) "?" else nameParts[1][0] + } + + private fun getPaintCenter(textPaint: Paint): Int { + return ((textPaint.descent() + textPaint.ascent()) / 2).roundToInt() + } + + @JvmStatic + fun getProfileImage(diameterDp: Int, textSizeBigDp: Int, textSizeMediumDp: Int, textSizeSmallDp: Int, count: Int, vararg names: String?): Bitmap { + val diameter = Utils.dpToPx(diameterDp).toFloat() + val textSizeBig = Utils.dpToPx(textSizeBigDp).toFloat() + val textSizeMedium = Utils.dpToPx(textSizeMediumDp).toFloat() + val textSizeSmall = Utils.dpToPx(textSizeSmallDp).toFloat() + val bitmap = Bitmap.createBitmap(diameter.toInt(), diameter.toInt(), Bitmap.Config.ARGB_8888) + val canvas = Canvas(bitmap) + val circlePaint = Paint() + circlePaint.flags = Paint.ANTI_ALIAS_FLAG + val textPaint = Paint() + textPaint.textAlign = Paint.Align.CENTER + textPaint.flags = Paint.ANTI_ALIAS_FLAG + val rectF = RectF() + rectF[0f, 0f, diameter] = diameter + var name: String? + var color: Int + when { + count == 1 -> { + name = names[0] + circlePaint.color = Colors.stringToMaterialColor(name).also { color = it } + textPaint.color = ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f) + textPaint.textSize = textSizeBig + canvas.drawArc(rectF, 0f, 360f, true, circlePaint) + canvas.drawText(getInitials(name), diameter / 2, diameter / 2 - getPaintCenter(textPaint), textPaint) + } + count == 2 -> { // top + name = names[0] + circlePaint.color = Colors.stringToMaterialColor(name).also { color = it } + textPaint.color = ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f) + textPaint.textSize = textSizeMedium + canvas.drawArc(rectF, 180f, 180f, true, circlePaint) + canvas.drawText(getInitials(name), diameter / 2, diameter / 4 - getPaintCenter(textPaint), textPaint) + // bottom + name = names[1] + circlePaint.color = Colors.stringToMaterialColor(name).also { color = it } + textPaint.color = ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f) + textPaint.textSize = textSizeMedium + canvas.drawArc(rectF, 0f, 180f, true, circlePaint) + canvas.drawText(getInitials(name), diameter / 2, diameter / 4 * 3 - getPaintCenter(textPaint), textPaint) + } + count == 3 -> { // upper left + name = names[0] + circlePaint.color = Colors.stringToMaterialColor(name).also { color = it } + textPaint.color = ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f) + textPaint.textSize = textSizeSmall + canvas.drawArc(rectF, 180f, 90f, true, circlePaint) + canvas.drawText(getInitials(name), diameter / 4, diameter / 4 - getPaintCenter(textPaint) + diameter / 32, textPaint) + // upper right + name = names[1] + circlePaint.color = Colors.stringToMaterialColor(name).also { color = it } + textPaint.color = ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f) + textPaint.textSize = textSizeSmall + canvas.drawArc(rectF, 270f, 90f, true, circlePaint) + canvas.drawText(getInitials(name), diameter / 4 * 3, diameter / 4 - getPaintCenter(textPaint) + diameter / 32, textPaint) + // bottom + name = names[2] + circlePaint.color = Colors.stringToMaterialColor(name).also { color = it } + textPaint.color = ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f) + textPaint.textSize = textSizeMedium + canvas.drawArc(rectF, 0f, 180f, true, circlePaint) + canvas.drawText(getInitials(name), diameter / 2, diameter / 4 * 3 - getPaintCenter(textPaint), textPaint) + } + count >= 4 -> { // upper left + name = names[0] + circlePaint.color = Colors.stringToMaterialColor(name).also { color = it } + textPaint.color = ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f) + textPaint.textSize = textSizeSmall + canvas.drawArc(rectF, 180f, 90f, true, circlePaint) + canvas.drawText(getInitials(name), diameter / 4, diameter / 4 - getPaintCenter(textPaint) + diameter / 32, textPaint) + // upper right + name = names[1] + circlePaint.color = Colors.stringToMaterialColor(name).also { color = it } + textPaint.color = ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f) + textPaint.textSize = textSizeSmall + canvas.drawArc(rectF, 270f, 90f, true, circlePaint) + canvas.drawText(getInitials(name), diameter / 4 * 3, diameter / 4 - getPaintCenter(textPaint) + diameter / 32, textPaint) + // bottom left + name = names[2] + circlePaint.color = Colors.stringToMaterialColor(name).also { color = it } + textPaint.color = ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f) + textPaint.textSize = textSizeSmall + canvas.drawArc(rectF, 90f, 90f, true, circlePaint) + canvas.drawText(getInitials(name), diameter / 4, diameter / 4 * 3 - getPaintCenter(textPaint) - diameter / 32, textPaint) + // bottom right + if (count == 4) name = names[3] + if (count > 4) name = "..." + circlePaint.color = Colors.stringToMaterialColor(name).also { color = it } + textPaint.color = ColorUtils.blendARGB(Colors.legibleTextColor(color), color, 0.30f) + textPaint.textSize = textSizeSmall + canvas.drawArc(rectF, 0f, 90f, true, circlePaint) + canvas.drawText(if (count > 4) "+" + (count - 3) else getInitials(name), diameter / 4 * 3, diameter / 4 * 3 - getPaintCenter(textPaint) - diameter / 32, textPaint) + } + } + return bitmap + } + + fun getMessageInfo(app: App, message: MessageFull, diameterDp: Int, textSizeBigDp: Int, textSizeMediumDp: Int, textSizeSmallDp: Int): MessageInfo { + var profileImage: Bitmap? = null + var profileName: String? = null + if (message.type == Message.TYPE_RECEIVED || message.type == Message.TYPE_DELETED) { + profileName = message.senderFullName + profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, 1, message.senderFullName) + } else if (message.type == Message.TYPE_SENT || message.type == Message.TYPE_DRAFT && message.recipients != null) { + when (val count = message.recipients?.size ?: 0) { + 0 -> { + profileName = app.getString(R.string.messages_draft_title) + profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, 1, "?") + } + 1 -> { + val recipient = message.recipients!![0] + profileName = recipient.fullName + profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, 1, recipient.fullName) + } + 2 -> { + val recipient1 = message.recipients!![0] + val recipient2 = message.recipients!![1] + profileName = recipient1.fullName + ", " + recipient2.fullName + profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, 2, recipient1.fullName, recipient2.fullName) + } + 3 -> { + val recipient1 = message.recipients!![0] + val recipient2 = message.recipients!![1] + val recipient3 = message.recipients!![2] + profileName = recipient1.fullName + ", " + recipient2.fullName + ", " + recipient3.fullName + profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, 3, recipient1.fullName, recipient2.fullName, recipient3.fullName) + } + 4 -> { + val recipient1 = message.recipients!![0] + val recipient2 = message.recipients!![1] + val recipient3 = message.recipients!![2] + val recipient4 = message.recipients!![3] + profileName = recipient1.fullName + ", " + recipient2.fullName + ", " + recipient3.fullName + ", " + recipient4.fullName + profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, 4, recipient1.fullName, recipient2.fullName, recipient3.fullName, recipient4.fullName) + } + else -> { + val recipient1 = message.recipients!![0] + val recipient2 = message.recipients!![1] + val recipient3 = message.recipients!![2] + val senderText = StringBuilder() + var first = true + for (recipient in message.recipients!!) { + if (!first) { + senderText.append(", ") + } + first = false + senderText.append(recipient.fullName) + } + profileName = senderText.toString() + profileImage = getProfileImage(diameterDp, textSizeBigDp, textSizeMediumDp, textSizeSmallDp, count, recipient1.fullName, recipient2.fullName, recipient3.fullName) + } + } + } + return MessageInfo(profileImage, profileName) + } + + class MessageInfo(var profileImage: Bitmap?, var profileName: String?) + + @JvmStatic + fun htmlToSpannable(html: String): Spanned { + val hexPattern = "(#[a-fA-F0-9]{6})" + val colorRegex = "(?:color=\"$hexPattern\")|(?:style=\"color: ?${hexPattern})" + .toRegex(RegexOption.IGNORE_CASE) + + var text = html + .replace("\\[META:[A-z0-9]+;[0-9-]+]".toRegex(), "") + .replace("background-color: ?$hexPattern;".toRegex(), "") + + colorRegex.findAll(text).forEach { result -> + val group = result.groups.drop(1).firstOrNull { it != null } ?: return@forEach + + val color = Color.parseColor(group.value) + val luminance = ColorUtils.calculateLuminance(color) + + if (Themes.isDark && luminance <= 0.5) { + text = text.replaceRange(group.range, "#FFFFFF") + } else if (!Themes.isDark && luminance > 0.5) { + text = text.replaceRange(group.range, "#000000") + } + } + + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT) + } else { + Html.fromHtml(text) + } + } +}