Fixed emoji display in contacts' name

This commit is contained in:
Sylvain Berfini 2020-12-01 11:03:10 +01:00
parent 4495fe5273
commit 66bd8f26b5
4 changed files with 21 additions and 4 deletions

View file

@ -178,6 +178,9 @@ dependencies {
implementation 'com.google.android.material:material:1.2.1'
implementation 'com.google.android:flexbox:2.0.0'
implementation 'androidx.emoji:emoji:1.1.0'
implementation 'androidx.emoji:emoji-bundled:1.1.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'
kapt 'com.github.bumptech.glide:compiler:4.11.0'

View file

@ -34,6 +34,7 @@ import org.linphone.activities.main.viewmodels.ListTopBarViewModel
import org.linphone.contact.Contact
import org.linphone.databinding.ContactListCellBinding
import org.linphone.databinding.GenericListHeaderBinding
import org.linphone.utils.AppUtils
import org.linphone.utils.Event
import org.linphone.utils.HeaderAdapter
import org.linphone.utils.SelectionListAdapter
@ -109,7 +110,7 @@ class ContactsListAdapter(
override fun getHeaderViewForPosition(context: Context, position: Int): View {
val contact = getItem(position)
val firstLetter = contact.fullName?.first().toString()
val firstLetter = AppUtils.getInitials(contact.fullName ?: "", 1)
val binding: GenericListHeaderBinding = DataBindingUtil.inflate(
LayoutInflater.from(context),
R.layout.generic_list_header, null, false

View file

@ -29,6 +29,8 @@ import android.os.Vibrator
import android.telephony.PhoneStateListener
import android.telephony.TelephonyManager
import android.view.*
import androidx.emoji.bundled.BundledEmojiCompatConfig
import androidx.emoji.text.EmojiCompat
import androidx.lifecycle.MutableLiveData
import java.io.File
import kotlin.math.abs
@ -233,6 +235,8 @@ class CoreContext(val context: Context, coreConfig: Config) {
val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
Log.i("[Context] Registering phone state listener")
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE)
EmojiCompat.init(BundledEmojiCompatConfig(context))
}
fun stop() {

View file

@ -24,6 +24,7 @@ import android.content.*
import android.text.Spanned
import android.util.TypedValue
import androidx.core.text.HtmlCompat
import androidx.emoji.text.EmojiCompat
import java.util.*
import java.util.regex.Pattern
import org.linphone.LinphoneApplication.Companion.coreContext
@ -93,15 +94,23 @@ class AppUtils {
return HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY)
}
fun getInitials(displayName: String): String {
fun getInitials(displayName: String, limit: Int = 2): String {
if (displayName.isEmpty()) return ""
val emoji = EmojiCompat.get()
val split = displayName.toUpperCase(Locale.getDefault()).split(" ")
var initials = ""
var characters = 0
for (i in split.indices) {
if (split[i].isNotEmpty()) {
initials += split[i][0]
if (initials.length >= 2) break
if (emoji.hasEmojiGlyph(split[i])) {
initials += emoji.process(split[i])
} else {
initials += split[i][0]
}
characters += 1
if (characters >= limit) break
}
}
return initials