mirror of
https://gitlab.linphone.org/BC/public/linphone-android.git
synced 2026-01-17 11:28:06 +00:00
Improved avatar geneator to be able to create Icons for notifications & shortcuts
This commit is contained in:
parent
682aaafc85
commit
27a408c9f1
4 changed files with 33 additions and 33 deletions
|
|
@ -23,9 +23,9 @@ import android.content.Context
|
|||
import android.graphics.*
|
||||
import android.graphics.drawable.BitmapDrawable
|
||||
import android.text.TextPaint
|
||||
import android.util.TypedValue
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
import androidx.core.graphics.drawable.IconCompat
|
||||
import org.linphone.R
|
||||
import org.linphone.utils.AppUtils
|
||||
|
||||
|
|
@ -50,10 +50,6 @@ class AvatarGenerator(private val context: Context) {
|
|||
textSize = size
|
||||
}
|
||||
|
||||
fun setTextColorResource(resource: Int) = apply {
|
||||
textColor = ContextCompat.getColor(context, resource)
|
||||
}
|
||||
|
||||
fun setAvatarSize(size: Int) = apply {
|
||||
avatarSize = size
|
||||
}
|
||||
|
|
@ -62,14 +58,7 @@ class AvatarGenerator(private val context: Context) {
|
|||
initials = label
|
||||
}
|
||||
|
||||
fun setBackgroundColorAttribute(attribute: Int) = apply {
|
||||
val theme = context.theme
|
||||
val backgroundColorTypedValue = TypedValue()
|
||||
theme.resolveAttribute(attribute, backgroundColorTypedValue, true)
|
||||
backgroundColor = ContextCompat.getColor(context, backgroundColorTypedValue.resourceId)
|
||||
}
|
||||
|
||||
fun build(): BitmapDrawable {
|
||||
private fun preBuild(): Bitmap {
|
||||
val textPainter = getTextPainter()
|
||||
val painter = getPainter()
|
||||
|
||||
|
|
@ -86,7 +75,15 @@ class AvatarGenerator(private val context: Context) {
|
|||
canvas.drawCircle(halfSize, halfSize, halfSize, painter)
|
||||
canvas.drawText(initials, bounds.left, bounds.top - textPainter.ascent(), textPainter)
|
||||
|
||||
return BitmapDrawable(context.resources, bitmap)
|
||||
return bitmap
|
||||
}
|
||||
|
||||
fun build(): BitmapDrawable {
|
||||
return BitmapDrawable(context.resources, preBuild())
|
||||
}
|
||||
|
||||
fun buildIcon(): IconCompat {
|
||||
return IconCompat.createWithAdaptiveBitmap(preBuild())
|
||||
}
|
||||
|
||||
private fun getTextPainter(): TextPaint {
|
||||
|
|
|
|||
|
|
@ -34,7 +34,6 @@ import androidx.core.app.Person
|
|||
import androidx.core.graphics.drawable.IconCompat
|
||||
import androidx.loader.app.LoaderManager
|
||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||
import org.linphone.R
|
||||
import org.linphone.core.Address
|
||||
import org.linphone.core.Core
|
||||
import org.linphone.core.CoreListenerStub
|
||||
|
|
@ -47,6 +46,7 @@ import org.linphone.ui.main.contacts.model.ContactAvatarModel
|
|||
import org.linphone.ui.main.contacts.model.ContactNumberOrAddressClickListener
|
||||
import org.linphone.ui.main.contacts.model.ContactNumberOrAddressModel
|
||||
import org.linphone.ui.main.model.isInSecureMode
|
||||
import org.linphone.utils.AppUtils
|
||||
import org.linphone.utils.ImageUtils
|
||||
import org.linphone.utils.LinphoneUtils
|
||||
import org.linphone.utils.PhoneNumberUtils
|
||||
|
|
@ -56,8 +56,6 @@ class ContactsManager @UiThread constructor(context: Context) {
|
|||
private const val TAG = "[Contacts Manager]"
|
||||
}
|
||||
|
||||
val contactAvatar: IconCompat
|
||||
|
||||
private var nativeContactsLoaded = false
|
||||
|
||||
private val listeners = arrayListOf<ContactsListener>()
|
||||
|
|
@ -94,13 +92,6 @@ class ContactsManager @UiThread constructor(context: Context) {
|
|||
}
|
||||
}
|
||||
|
||||
init {
|
||||
contactAvatar = IconCompat.createWithResource(
|
||||
context,
|
||||
R.drawable.user_circle
|
||||
)
|
||||
}
|
||||
|
||||
@UiThread
|
||||
fun loadContacts(activity: MainActivity) {
|
||||
val manager = LoaderManager.getInstance(activity)
|
||||
|
|
@ -346,7 +337,7 @@ class ContactsManager @UiThread constructor(context: Context) {
|
|||
val bm = ImageUtils.getBitmap(coreContext.context, photo)
|
||||
personBuilder.setIcon(
|
||||
if (bm == null) {
|
||||
coreContext.contactsManager.contactAvatar
|
||||
AvatarGenerator(coreContext.context).setInitials(AppUtils.getInitials(name)).buildIcon()
|
||||
} else {
|
||||
IconCompat.createWithAdaptiveBitmap(bm)
|
||||
}
|
||||
|
|
@ -375,7 +366,7 @@ fun Friend.getPerson(): Person {
|
|||
val bm: Bitmap? = getAvatarBitmap()
|
||||
personBuilder.setIcon(
|
||||
if (bm == null) {
|
||||
coreContext.contactsManager.contactAvatar
|
||||
AvatarGenerator(coreContext.context).setInitials(AppUtils.getInitials(name.orEmpty())).buildIcon()
|
||||
} else {
|
||||
IconCompat.createWithAdaptiveBitmap(bm)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ import kotlinx.coroutines.launch
|
|||
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||
import org.linphone.LinphoneApplication.Companion.corePreferences
|
||||
import org.linphone.R
|
||||
import org.linphone.contacts.AvatarGenerator
|
||||
import org.linphone.contacts.getAvatarBitmap
|
||||
import org.linphone.contacts.getPerson
|
||||
import org.linphone.core.Address
|
||||
|
|
@ -987,7 +988,9 @@ class NotificationsManager @MainThread constructor(private val context: Context)
|
|||
return friend?.getPerson()
|
||||
?: Person.Builder()
|
||||
.setName(displayName)
|
||||
.setIcon(coreContext.contactsManager.contactAvatar)
|
||||
.setIcon(
|
||||
AvatarGenerator(context).setInitials(AppUtils.getInitials(displayName)).buildIcon()
|
||||
)
|
||||
.setKey(displayName)
|
||||
.build()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ import androidx.core.graphics.drawable.IconCompat
|
|||
import kotlin.math.min
|
||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||
import org.linphone.R
|
||||
import org.linphone.contacts.AvatarGenerator
|
||||
import org.linphone.contacts.getPerson
|
||||
import org.linphone.core.ChatRoom
|
||||
import org.linphone.core.tools.Log
|
||||
|
|
@ -92,8 +93,10 @@ class ShortcutUtils {
|
|||
personsList.add(person)
|
||||
}
|
||||
|
||||
icon = person?.icon ?: coreContext.contactsManager.contactAvatar
|
||||
subject = contact?.name ?: LinphoneUtils.getDisplayName(peerAddress)
|
||||
icon = person?.icon ?: AvatarGenerator(context).setInitials(
|
||||
AppUtils.getInitials(subject)
|
||||
).buildIcon()
|
||||
} else if (chatRoom.hasCapability(ChatRoom.Capabilities.OneToOne.toInt()) && chatRoom.participants.isNotEmpty()) {
|
||||
val address = chatRoom.participants.first().address
|
||||
val contact =
|
||||
|
|
@ -104,7 +107,9 @@ class ShortcutUtils {
|
|||
}
|
||||
|
||||
subject = contact?.name ?: LinphoneUtils.getDisplayName(address)
|
||||
icon = person?.icon ?: coreContext.contactsManager.contactAvatar
|
||||
icon = person?.icon ?: AvatarGenerator(context).setInitials(
|
||||
AppUtils.getInitials(subject)
|
||||
).buildIcon()
|
||||
} else {
|
||||
val list = arrayListOf<String>()
|
||||
for (participant in chatRoom.participants) {
|
||||
|
|
@ -120,10 +125,14 @@ class ShortcutUtils {
|
|||
}
|
||||
}
|
||||
subject = chatRoom.subject.orEmpty()
|
||||
val iconSize = AppUtils.getDimension(R.dimen.avatar_list_cell_size).toInt()
|
||||
icon = IconCompat.createWithAdaptiveBitmap(
|
||||
ImageUtils.getBitmapFromMultipleAvatars(context, iconSize, list)
|
||||
)
|
||||
icon = if (list.isNotEmpty()) {
|
||||
val iconSize = AppUtils.getDimension(R.dimen.avatar_list_cell_size).toInt()
|
||||
IconCompat.createWithAdaptiveBitmap(
|
||||
ImageUtils.getBitmapFromMultipleAvatars(context, iconSize, list)
|
||||
)
|
||||
} else {
|
||||
AvatarGenerator(context).setInitials(subject).buildIcon()
|
||||
}
|
||||
}
|
||||
|
||||
val persons = arrayOfNulls<Person>(personsList.size)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue