mirror of
https://gitlab.linphone.org/BC/public/linphone-android.git
synced 2026-01-17 11:28:06 +00:00
Improved emoji compat use for contacts
This commit is contained in:
parent
8bb88f397e
commit
7c0f9585e7
3 changed files with 50 additions and 20 deletions
|
|
@ -24,6 +24,7 @@ import android.content.Context
|
|||
import android.os.Handler
|
||||
import android.os.HandlerThread
|
||||
import android.os.Looper
|
||||
import androidx.emoji2.text.EmojiCompat
|
||||
import java.util.*
|
||||
import org.linphone.BuildConfig
|
||||
import org.linphone.LinphoneApplication.Companion.corePreferences
|
||||
|
|
@ -33,8 +34,12 @@ import org.linphone.core.tools.Log
|
|||
class CoreContext(val context: Context) : HandlerThread("Core Thread") {
|
||||
lateinit var core: Core
|
||||
|
||||
lateinit var emojiCompat: EmojiCompat
|
||||
|
||||
val contactsManager = ContactsManager()
|
||||
|
||||
private val mainThread = Handler(Looper.getMainLooper())
|
||||
|
||||
@SuppressLint("HandlerLeak")
|
||||
private lateinit var coreThread: Handler
|
||||
|
||||
|
|
@ -44,6 +49,11 @@ class CoreContext(val context: Context) : HandlerThread("Core Thread") {
|
|||
}
|
||||
}
|
||||
|
||||
init {
|
||||
EmojiCompat.init(context)
|
||||
emojiCompat = EmojiCompat.get()
|
||||
}
|
||||
|
||||
override fun run() {
|
||||
Looper.prepare()
|
||||
|
||||
|
|
@ -93,6 +103,12 @@ class CoreContext(val context: Context) : HandlerThread("Core Thread") {
|
|||
}
|
||||
}
|
||||
|
||||
fun postOnMainThread(lambda: () -> Unit) {
|
||||
mainThread.post {
|
||||
lambda.invoke()
|
||||
}
|
||||
}
|
||||
|
||||
private fun computeUserAgent() {
|
||||
// TODO FIXME
|
||||
val deviceName: String = "Linphone6"
|
||||
|
|
|
|||
|
|
@ -27,8 +27,15 @@ import androidx.appcompat.app.AppCompatActivity
|
|||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.emoji2.text.EmojiCompat
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||
import org.linphone.R
|
||||
import org.linphone.core.tools.Log
|
||||
import org.linphone.databinding.MainActivityBinding
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
|
@ -47,14 +54,14 @@ class MainActivity : AppCompatActivity() {
|
|||
R.color.primary_color
|
||||
)
|
||||
|
||||
if (checkSelfPermission(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
|
||||
coreContext.contactsManager.loadContacts(this)
|
||||
}
|
||||
|
||||
while (!coreContext.isReady()) {
|
||||
Thread.sleep(20)
|
||||
}
|
||||
|
||||
if (checkSelfPermission(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
|
||||
loadContacts()
|
||||
}
|
||||
|
||||
binding = DataBindingUtil.setContentView(this, R.layout.main_activity)
|
||||
binding.lifecycleOwner = this
|
||||
}
|
||||
|
|
@ -76,7 +83,7 @@ class MainActivity : AppCompatActivity() {
|
|||
grantResults: IntArray
|
||||
) {
|
||||
if (requestCode == CONTACTS_PERMISSION_REQUEST && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
coreContext.contactsManager.loadContacts(this)
|
||||
loadContacts()
|
||||
}
|
||||
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||
|
|
@ -89,4 +96,25 @@ class MainActivity : AppCompatActivity() {
|
|||
binding.sideMenu.openDrawer(binding.sideMenuContent, true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadContacts() {
|
||||
val emojiCompat = coreContext.emojiCompat
|
||||
lifecycleScope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
// Wait for emoji compat library to have been loaded
|
||||
Log.i("[Main Activity] Waiting for emoji compat library to have been loaded")
|
||||
while (emojiCompat.loadState == EmojiCompat.LOAD_STATE_DEFAULT || emojiCompat.loadState == EmojiCompat.LOAD_STATE_LOADING) {
|
||||
delay(100)
|
||||
}
|
||||
|
||||
Log.i(
|
||||
"[Main Activity] Emoji compat library loading status is ${emojiCompat.loadState}, loading contacts"
|
||||
)
|
||||
coreContext.postOnMainThread {
|
||||
// Contacts loading must be started from UI thread
|
||||
coreContext.contactsManager.loadContacts(this@MainActivity)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,20 +33,6 @@ import org.linphone.core.tools.Log
|
|||
|
||||
class LinphoneUtils {
|
||||
companion object {
|
||||
private val emojiCompat: EmojiCompat?
|
||||
get() = initEmojiCompat()
|
||||
|
||||
private fun initEmojiCompat(): EmojiCompat? {
|
||||
return try {
|
||||
EmojiCompat.get()
|
||||
} catch (ise: IllegalStateException) {
|
||||
Log.w(
|
||||
"[App Utils] EmojiCompat.get() triggered IllegalStateException [$ise], trying manual init"
|
||||
)
|
||||
EmojiCompat.init(coreContext.context)
|
||||
}
|
||||
}
|
||||
|
||||
fun getFirstLetter(displayName: String): String {
|
||||
return getInitials(displayName, 1)
|
||||
}
|
||||
|
|
@ -57,7 +43,7 @@ class LinphoneUtils {
|
|||
val split = displayName.uppercase(Locale.getDefault()).split(" ")
|
||||
var initials = ""
|
||||
var characters = 0
|
||||
val emoji = emojiCompat
|
||||
val emoji = coreContext.emojiCompat
|
||||
|
||||
for (i in split.indices) {
|
||||
if (split[i].isNotEmpty()) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue