Moved some functions from LinphoneUtils to AppUtils

This commit is contained in:
Sylvain Berfini 2023-09-28 16:38:31 +02:00
parent 78eec3f6c8
commit 1fe8bad37b
6 changed files with 88 additions and 87 deletions

View file

@ -40,8 +40,8 @@ import org.linphone.notifications.NotificationsManager
import org.linphone.telecom.TelecomManager
import org.linphone.ui.call.CallActivity
import org.linphone.utils.ActivityMonitor
import org.linphone.utils.AppUtils
import org.linphone.utils.Event
import org.linphone.utils.LinphoneUtils
class CoreContext @UiThread constructor(val context: Context) : HandlerThread("Core Thread") {
companion object {
@ -393,7 +393,7 @@ class CoreContext @UiThread constructor(val context: Context) : HandlerThread("C
@WorkerThread
private fun computeUserAgent() {
val deviceName = LinphoneUtils.getDeviceName(context)
val deviceName = AppUtils.getDeviceName(context)
val appName = context.getString(org.linphone.R.string.app_name)
val androidVersion = BuildConfig.VERSION_NAME
val userAgent = "$appName/$androidVersion ($deviceName) LinphoneSDK"

View file

@ -30,7 +30,6 @@ import org.linphone.core.Friend
import org.linphone.core.FriendListenerStub
import org.linphone.core.tools.Log
import org.linphone.utils.AppUtils
import org.linphone.utils.LinphoneUtils
import org.linphone.utils.TimestampUtils
class ContactAvatarModel @WorkerThread constructor(val friend: Friend) {
@ -44,7 +43,7 @@ class ContactAvatarModel @WorkerThread constructor(val friend: Friend) {
val avatar = MutableLiveData<Uri>()
val initials = LinphoneUtils.getInitials(friend.name.orEmpty())
val initials = AppUtils.getInitials(friend.name.orEmpty())
val lastPresenceInfo = MutableLiveData<String>()
@ -52,7 +51,7 @@ class ContactAvatarModel @WorkerThread constructor(val friend: Friend) {
val name = MutableLiveData<String>()
val firstLetter: String = LinphoneUtils.getFirstLetter(friend.name.orEmpty())
val firstLetter: String = AppUtils.getFirstLetter(friend.name.orEmpty())
val firstContactStartingByThatLetter = MutableLiveData<Boolean>()

View file

@ -24,6 +24,7 @@ import androidx.annotation.WorkerThread
import org.linphone.core.Address
import org.linphone.core.Friend
import org.linphone.ui.main.contacts.model.ContactAvatarModel
import org.linphone.utils.AppUtils
import org.linphone.utils.LinphoneUtils
class ContactOrSuggestionModel @WorkerThread constructor(
@ -39,7 +40,7 @@ class ContactOrSuggestionModel @WorkerThread constructor(
val name = LinphoneUtils.getDisplayName(address)
val initials = LinphoneUtils.getInitials(name)
val initials = AppUtils.getInitials(name)
lateinit var contactAvatarModel: ContactAvatarModel

View file

@ -130,7 +130,7 @@ class AccountModel @WorkerThread constructor(
val name = LinphoneUtils.getDisplayName(account.params.identityAddress)
displayName.postValue(name)
initials.postValue(LinphoneUtils.getInitials(name))
initials.postValue(AppUtils.getInitials(name))
val pictureUri = account.params.pictureUri.orEmpty()
if (pictureUri != avatar.value) {

View file

@ -19,10 +19,15 @@
*/
package org.linphone.utils
import android.Manifest
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import android.util.DisplayMetrics
import android.util.Rational
import android.view.LayoutInflater
@ -33,7 +38,10 @@ import androidx.annotation.DimenRes
import androidx.annotation.DrawableRes
import androidx.annotation.MainThread
import androidx.annotation.StringRes
import androidx.core.app.ActivityCompat
import androidx.databinding.DataBindingUtil
import androidx.emoji2.text.EmojiCompat
import java.util.Locale
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.R
import org.linphone.core.tools.Log
@ -41,6 +49,8 @@ import org.linphone.databinding.ToastBinding
class AppUtils {
companion object {
const val TAG = "[App Utils]"
@AnyThread
fun getDimension(@DimenRes id: Int): Float {
return coreContext.context.resources.getDimension(id)
@ -92,6 +102,77 @@ class AppUtils {
return ratio
}
@AnyThread
fun getFirstLetter(displayName: String): String {
return getInitials(displayName, 1)
}
@AnyThread
fun getInitials(displayName: String, limit: Int = 2): String {
if (displayName.isEmpty()) return ""
val split = displayName.uppercase(Locale.getDefault()).split(" ")
var initials = ""
var characters = 0
val emoji = coreContext.emojiCompat
for (i in split.indices) {
if (split[i].isNotEmpty()) {
try {
if (emoji.loadState == EmojiCompat.LOAD_STATE_SUCCEEDED && emoji.hasEmojiGlyph(
split[i]
)
) {
val glyph = emoji.process(split[i])
if (characters > 0) { // Limit initial to 1 emoji only
Log.d("$TAG We limit initials to one emoji only")
initials = ""
}
initials += glyph
break // Limit initial to 1 emoji only
} else {
initials += split[i][0]
}
} catch (ise: IllegalStateException) {
Log.e("$TAG Can't call hasEmojiGlyph: $ise")
initials += split[i][0]
}
characters += 1
if (characters >= limit) break
}
}
return initials
}
@AnyThread
fun getDeviceName(context: Context): String {
var name = Settings.Global.getString(
context.contentResolver,
Settings.Global.DEVICE_NAME
)
if (name == null) {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.BLUETOOTH_CONNECT
) == PackageManager.PERMISSION_GRANTED
) {
val adapter = BluetoothAdapter.getDefaultAdapter()
name = adapter?.name
}
}
if (name == null) {
name = Settings.Secure.getString(
context.contentResolver,
"bluetooth_name"
)
}
if (name == null) {
name = Build.MANUFACTURER + " " + Build.MODEL
}
return name
}
@MainThread
fun getRedToast(
context: Context,

View file

@ -19,18 +19,9 @@
*/
package org.linphone.utils
import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import androidx.annotation.AnyThread
import androidx.annotation.IntegerRes
import androidx.annotation.WorkerThread
import androidx.core.app.ActivityCompat
import androidx.emoji2.text.EmojiCompat
import java.util.Locale
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.R
import org.linphone.core.Account
@ -51,49 +42,6 @@ class LinphoneUtils {
return coreContext.core.defaultAccount ?: coreContext.core.accountList.firstOrNull()
}
@AnyThread
fun getFirstLetter(displayName: String): String {
return getInitials(displayName, 1)
}
@AnyThread
fun getInitials(displayName: String, limit: Int = 2): String {
if (displayName.isEmpty()) return ""
val split = displayName.uppercase(Locale.getDefault()).split(" ")
var initials = ""
var characters = 0
val emoji = coreContext.emojiCompat
for (i in split.indices) {
if (split[i].isNotEmpty()) {
try {
if (emoji.loadState == EmojiCompat.LOAD_STATE_SUCCEEDED && emoji.hasEmojiGlyph(
split[i]
)
) {
val glyph = emoji.process(split[i])
if (characters > 0) { // Limit initial to 1 emoji only
Log.d("$TAG We limit initials to one emoji only")
initials = ""
}
initials += glyph
break // Limit initial to 1 emoji only
} else {
initials += split[i][0]
}
} catch (ise: IllegalStateException) {
Log.e("$TAG Can't call hasEmojiGlyph: $ise")
initials += split[i][0]
}
characters += 1
if (characters >= limit) break
}
}
return initials
}
@WorkerThread
fun getDisplayName(address: Address?): String {
if (address == null) return "[null]"
@ -230,34 +178,6 @@ class LinphoneUtils {
}
}
@AnyThread
fun getDeviceName(context: Context): String {
var name = Settings.Global.getString(
context.contentResolver,
Settings.Global.DEVICE_NAME
)
if (name == null) {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.BLUETOOTH_CONNECT
) == PackageManager.PERMISSION_GRANTED
) {
val adapter = BluetoothAdapter.getDefaultAdapter()
name = adapter?.name
}
}
if (name == null) {
name = Settings.Secure.getString(
context.contentResolver,
"bluetooth_name"
)
}
if (name == null) {
name = Build.MANUFACTURER + " " + Build.MODEL
}
return name
}
@WorkerThread
fun getChatRoomId(room: ChatRoom): String {
return getChatRoomId(room.localAddress, room.peerAddress)