Shorcuts creation improvements

This commit is contained in:
Sylvain Berfini 2022-04-07 11:13:35 +02:00
parent f71811f65a
commit 898d3d0aa4
4 changed files with 33 additions and 19 deletions

View file

@ -25,7 +25,6 @@ import android.app.*
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.graphics.drawable.Icon
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences import org.linphone.LinphoneApplication.Companion.corePreferences
@ -118,7 +117,7 @@ class Api31Compatibility {
} else { } else {
Person.Builder() Person.Builder()
.setName(conferenceInfo.subject) .setName(conferenceInfo.subject)
.setIcon(Icon.createWithResource(context, R.drawable.voip_multiple_contacts_avatar_alt)) .setIcon(coreContext.contactsManager.groupAvatar.toIcon(context))
.setImportant(false) .setImportant(false)
.build() .build()
} }

View file

@ -68,6 +68,9 @@ class ContactsManager(private val context: Context) {
var contactIdToWatchFor: String = "" var contactIdToWatchFor: String = ""
val contactAvatar: IconCompat
val groupAvatar: IconCompat
private val localFriends = arrayListOf<Friend>() private val localFriends = arrayListOf<Friend>()
private val contactsUpdatedListeners = ArrayList<ContactsUpdatedListener>() private val contactsUpdatedListeners = ArrayList<ContactsUpdatedListener>()
@ -79,13 +82,18 @@ class ContactsManager(private val context: Context) {
for (friend in friends) { for (friend in friends) {
refreshContactOnPresenceReceived(friend) refreshContactOnPresenceReceived(friend)
} }
Log.i("[Contacts Manager] Contacts refreshed due to presence received")
notifyListeners() notifyListeners()
Log.i("[Contacts Manager] Presence notified to all listeners")
} }
} }
init { init {
initSyncAccount() initSyncAccount()
contactAvatar = IconCompat.createWithResource(context, R.drawable.voip_single_contact_avatar_alt)
groupAvatar = IconCompat.createWithResource(context, R.drawable.voip_multiple_contacts_avatar_alt)
val core = coreContext.core val core = coreContext.core
for (list in core.friendsLists) { for (list in core.friendsLists) {
list.addListener(friendListListener) list.addListener(friendListListener)
@ -260,6 +268,7 @@ class ContactsManager(private val context: Context) {
Log.d("[Contacts Manager] Received presence information for contact $friend") Log.d("[Contacts Manager] Received presence information for contact $friend")
if (corePreferences.storePresenceInNativeContact && PermissionHelper.get().hasWriteContactsPermission()) { if (corePreferences.storePresenceInNativeContact && PermissionHelper.get().hasWriteContactsPermission()) {
if (friend.refKey != null) { if (friend.refKey != null) {
Log.i("[Contacts Manager] Storing presence in native contact ${friend.refKey}")
storePresenceInNativeContact(friend) storePresenceInNativeContact(friend)
} }
} }
@ -383,17 +392,17 @@ fun Friend.getPerson(): Person {
val bm: Bitmap? = val bm: Bitmap? =
ImageUtils.getRoundBitmapFromUri( ImageUtils.getRoundBitmapFromUri(
coreContext.context, coreContext.context,
getPictureUri() getThumbnailUri()
) )
val icon = val icon =
if (bm == null) IconCompat.createWithResource( if (bm == null) {
coreContext.context, coreContext.contactsManager.contactAvatar
R.drawable.voip_single_contact_avatar_alt } else IconCompat.createWithAdaptiveBitmap(bm)
) else IconCompat.createWithAdaptiveBitmap(bm)
if (icon != null) { if (icon != null) {
personBuilder.setIcon(icon) personBuilder.setIcon(icon)
} }
personBuilder.setUri(nativeUri)
personBuilder.setImportant(starred) personBuilder.setImportant(starred)
return personBuilder.build() return personBuilder.build()
} }

View file

@ -431,7 +431,7 @@ class NotificationsManager(private val context: Context) {
if (picture != null) { if (picture != null) {
IconCompat.createWithAdaptiveBitmap(picture) IconCompat.createWithAdaptiveBitmap(picture)
} else { } else {
IconCompat.createWithResource(context, R.drawable.voip_single_contact_avatar_alt) coreContext.contactsManager.contactAvatar
} }
if (userIcon != null) builder.setIcon(userIcon) if (userIcon != null) builder.setIcon(userIcon)
builder.build() builder.build()
@ -791,7 +791,7 @@ class NotificationsManager(private val context: Context) {
} }
style.isGroupConversation = notifiable.isGroup style.isGroupConversation = notifiable.isGroup
val icon = lastPerson?.icon ?: IconCompat.createWithResource(context, R.drawable.voip_single_contact_avatar_alt) val icon = lastPerson?.icon ?: coreContext.contactsManager.contactAvatar
val bubble = NotificationCompat.BubbleMetadata.Builder(bubbleIntent, icon) val bubble = NotificationCompat.BubbleMetadata.Builder(bubbleIntent, icon)
.setDesiredHeightResId(R.dimen.chat_message_bubble_desired_height) .setDesiredHeightResId(R.dimen.chat_message_bubble_desired_height)
.build() .build()

View file

@ -30,8 +30,8 @@ import androidx.core.app.Person
import androidx.core.content.LocusIdCompat import androidx.core.content.LocusIdCompat
import androidx.core.content.pm.ShortcutInfoCompat import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.graphics.drawable.IconCompat import androidx.core.graphics.drawable.IconCompat
import kotlin.math.min
import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.R
import org.linphone.activities.main.MainActivity import org.linphone.activities.main.MainActivity
import org.linphone.contact.getPerson import org.linphone.contact.getPerson
import org.linphone.core.Address import org.linphone.core.Address
@ -132,7 +132,8 @@ class ShortcutsHelper(val context: Context) {
return return
} }
val maxShortcuts = shortcutManager.maxShortcutCountPerActivity Log.i("[Shortcut Helper] Creating launcher shortcuts for chat rooms")
val maxShortcuts = min(shortcutManager.maxShortcutCountPerActivity, 5)
var count = 0 var count = 0
for (room in coreContext.core.chatRooms) { for (room in coreContext.core.chatRooms) {
// Android can usually only have around 4-5 shortcuts at a time // Android can usually only have around 4-5 shortcuts at a time
@ -143,12 +144,13 @@ class ShortcutsHelper(val context: Context) {
val shortcut: ShortcutInfo? = createChatRoomShortcut(context, room) val shortcut: ShortcutInfo? = createChatRoomShortcut(context, room)
if (shortcut != null) { if (shortcut != null) {
Log.i("[Shortcut Helper] Creating launcher shortcut for ${shortcut.shortLabel}") Log.i("[Shortcut Helper] Created launcher shortcut for ${shortcut.shortLabel}")
shortcuts.add(shortcut) shortcuts.add(shortcut)
count += 1 count += 1
} }
} }
shortcutManager.dynamicShortcuts = shortcuts shortcutManager.dynamicShortcuts = shortcuts
Log.i("[Shortcut Helper] Created $count launcher shortcuts")
} }
private fun createChatRoomShortcut(context: Context, chatRoom: ChatRoom): ShortcutInfo? { private fun createChatRoomShortcut(context: Context, chatRoom: ChatRoom): ShortcutInfo? {
@ -166,20 +168,24 @@ class ShortcutsHelper(val context: Context) {
if (chatRoom.hasCapability(ChatRoomCapabilities.Basic.toInt())) { if (chatRoom.hasCapability(ChatRoomCapabilities.Basic.toInt())) {
val contact = val contact =
coreContext.contactsManager.findContactByAddress(chatRoom.peerAddress) coreContext.contactsManager.findContactByAddress(chatRoom.peerAddress)
if (contact != null) { val person = contact?.getPerson()
personsList.add(contact.getPerson()) if (person != null) {
personsList.add(person)
} }
icon = person?.icon ?: coreContext.contactsManager.contactAvatar
subject = contact?.name ?: LinphoneUtils.getDisplayName(chatRoom.peerAddress) subject = contact?.name ?: LinphoneUtils.getDisplayName(chatRoom.peerAddress)
icon = contact?.getPerson()?.icon ?: IconCompat.createWithResource(context, R.drawable.voip_single_contact_avatar_alt)
} else if (chatRoom.hasCapability(ChatRoomCapabilities.OneToOne.toInt()) && chatRoom.participants.isNotEmpty()) { } else if (chatRoom.hasCapability(ChatRoomCapabilities.OneToOne.toInt()) && chatRoom.participants.isNotEmpty()) {
val address = chatRoom.participants.first().address val address = chatRoom.participants.first().address
val contact = val contact =
coreContext.contactsManager.findContactByAddress(address) coreContext.contactsManager.findContactByAddress(address)
if (contact != null) { val person = contact?.getPerson()
personsList.add(contact.getPerson()) if (person != null) {
personsList.add(person)
} }
subject = contact?.name ?: LinphoneUtils.getDisplayName(address) subject = contact?.name ?: LinphoneUtils.getDisplayName(address)
icon = contact?.getPerson()?.icon ?: IconCompat.createWithResource(context, R.drawable.voip_single_contact_avatar_alt) icon = person?.icon ?: coreContext.contactsManager.contactAvatar
} else { } else {
for (participant in chatRoom.participants) { for (participant in chatRoom.participants) {
val contact = val contact =
@ -189,7 +195,7 @@ class ShortcutsHelper(val context: Context) {
} }
} }
subject = chatRoom.subject.orEmpty() subject = chatRoom.subject.orEmpty()
icon = IconCompat.createWithResource(context, R.drawable.voip_multiple_contacts_avatar_alt) icon = coreContext.contactsManager.groupAvatar
} }
val persons = arrayOfNulls<Person>(personsList.size) val persons = arrayOfNulls<Person>(personsList.size)