Added shortcuts + improved bubble layouts

This commit is contained in:
Sylvain Berfini 2023-10-23 11:17:01 +02:00
parent 636a0b5c4d
commit 682aaafc85
11 changed files with 312 additions and 113 deletions

View file

@ -40,6 +40,10 @@ import androidx.core.app.NotificationManagerCompat
import androidx.core.app.Person
import androidx.core.app.RemoteInput
import androidx.core.content.LocusIdCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.R
@ -60,6 +64,7 @@ import org.linphone.core.tools.Log
import org.linphone.ui.call.CallActivity
import org.linphone.utils.AppUtils
import org.linphone.utils.LinphoneUtils
import org.linphone.utils.ShortcutUtils
class NotificationsManager @MainThread constructor(private val context: Context) {
companion object {
@ -81,6 +86,8 @@ class NotificationsManager @MainThread constructor(private val context: Context)
const val CHAT_NOTIFICATIONS_GROUP = "CHAT_NOTIF_GROUP"
}
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
private val notificationManager: NotificationManagerCompat by lazy {
NotificationManagerCompat.from(context)
}
@ -145,6 +152,17 @@ class NotificationsManager @MainThread constructor(private val context: Context)
return
}
if (ShortcutUtils.isShortcutToChatRoomAlreadyCreated(context, chatRoom)) {
Log.i("$TAG Chat room shortcut already exists")
} else {
Log.i(
"$TAG Ensure chat room shortcut exists for bubble notification"
)
scope.launch {
ShortcutUtils.createShortcutsToChatRooms(context)
}
}
showChatRoomNotification(chatRoom, messages)
}
@ -345,6 +363,10 @@ class NotificationsManager @MainThread constructor(private val context: Context)
fun onCoreStarted(core: Core) {
Log.i("$TAG Core has been started")
core.addListener(coreListener)
scope.launch {
ShortcutUtils.createShortcutsToChatRooms(context)
}
}
@WorkerThread
@ -801,8 +823,6 @@ class NotificationsManager @MainThread constructor(private val context: Context)
notificationBuilder.addPerson(person)
}
// TODO FIXME: shortcuts!
return notificationBuilder.build()
}

View file

@ -21,10 +21,7 @@ package org.linphone.utils
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.PorterDuff
import android.graphics.Rect
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
@ -41,7 +38,6 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.doOnLayout
@ -53,9 +49,7 @@ import androidx.emoji2.emojipicker.EmojiViewItem
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import coil.dispose
import coil.imageLoader
import coil.load
import coil.request.ImageRequest
import com.google.android.material.imageview.ShapeableImageView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
@ -356,61 +350,7 @@ private suspend fun loadContactPictureWithCoil(
} else {
AppUtils.getDimension(R.dimen.avatar_list_cell_size).toInt()
}
val bitmap = Bitmap.createBitmap(w, w, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val drawables = images.mapNotNull {
val request = ImageRequest.Builder(imageView.context)
.data(it)
.size(w / 2)
.allowHardware(false)
.build()
context.imageLoader.execute(request).drawable
}
val rectangles = if (drawables.size == 2) {
arrayListOf(
Rect(0, 0, w / 2, w),
Rect(w / 2, 0, w, w)
)
} else if (drawables.size == 3) {
arrayListOf(
Rect(0, 0, w / 2, w / 2),
Rect(w / 2, 0, w, w / 2),
Rect(0, w / 2, w, w)
)
} else if (drawables.size >= 4) {
arrayListOf(
Rect(0, 0, w / 2, w / 2),
Rect(w / 2, 0, w, w / 2),
Rect(0, w / 2, w / 2, w),
Rect(w / 2, w / 2, w, w)
)
} else {
arrayListOf()
}
for (i in 0 until rectangles.size) {
val src = if (drawables.size == 3 && i == 2) {
// To prevent deformation for the bottom image when merging 3 of them
val quarter = w / 4
Rect(0, quarter, w, 3 * quarter)
} else if (drawables.size == 2) {
// To prevent deformation when two images are next to each other
val quarter = w / 4
Rect(quarter, 0, 3 * quarter, w)
} else {
null
}
canvas.drawBitmap(
drawables[i].toBitmap(w, w, Bitmap.Config.ARGB_8888),
src,
rectangles[i],
null
)
}
val bitmap = ImageUtils.getBitmapFromMultipleAvatars(imageView.context, w, images)
imageView.load(bitmap)
}
} else {

View file

@ -21,9 +21,15 @@ package org.linphone.utils
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.ImageDecoder
import android.graphics.Rect
import android.net.Uri
import androidx.annotation.AnyThread
import androidx.annotation.WorkerThread
import androidx.core.graphics.drawable.toBitmap
import coil.imageLoader
import coil.request.ImageRequest
import java.io.FileNotFoundException
import org.linphone.core.tools.Log
@ -64,5 +70,65 @@ class ImageUtils {
Log.e("$TAG Can't get bitmap from null URI")
return null
}
@AnyThread
suspend fun getBitmapFromMultipleAvatars(context: Context, size: Int, images: List<String>): Bitmap {
val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val drawables = images.mapNotNull {
val request = ImageRequest.Builder(context)
.data(it)
.size(size / 2)
.allowHardware(false)
.build()
context.imageLoader.execute(request).drawable
}
val rectangles = if (drawables.size == 2) {
arrayListOf(
Rect(0, 0, size / 2, size),
Rect(size / 2, 0, size, size)
)
} else if (drawables.size == 3) {
arrayListOf(
Rect(0, 0, size / 2, size / 2),
Rect(size / 2, 0, size, size / 2),
Rect(0, size / 2, size, size)
)
} else if (drawables.size >= 4) {
arrayListOf(
Rect(0, 0, size / 2, size / 2),
Rect(size / 2, 0, size, size / 2),
Rect(0, size / 2, size / 2, size),
Rect(size / 2, size / 2, size, size)
)
} else {
arrayListOf()
}
for (i in 0 until rectangles.size) {
val src = if (drawables.size == 3 && i == 2) {
// To prevent deformation for the bottom image when merging 3 of them
val quarter = size / 4
Rect(0, quarter, size, 3 * quarter)
} else if (drawables.size == 2) {
// To prevent deformation when two images are next to each other
val quarter = size / 4
Rect(quarter, 0, 3 * quarter, size)
} else {
null
}
canvas.drawBitmap(
drawables[i].toBitmap(size, size, Bitmap.Config.ARGB_8888),
src,
rectangles[i],
null
)
}
return bitmap
}
}
}

View file

@ -0,0 +1,170 @@
/*
* Copyright (c) 2010-2023 Belledonne Communications SARL.
*
* This file is part of linphone-android
* (see https://www.linphone.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.linphone.utils
import android.content.Context
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.os.Bundle
import androidx.annotation.WorkerThread
import androidx.collection.ArraySet
import androidx.core.app.Person
import androidx.core.content.LocusIdCompat
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import kotlin.math.min
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.R
import org.linphone.contacts.getPerson
import org.linphone.core.ChatRoom
import org.linphone.core.tools.Log
import org.linphone.mediastream.Version
import org.linphone.ui.main.MainActivity
class ShortcutUtils {
companion object {
private const val TAG = "[Shortcut Utils]"
@WorkerThread
suspend fun createShortcutsToChatRooms(context: Context) {
val shortcuts = ArrayList<ShortcutInfoCompat>()
if (ShortcutManagerCompat.isRateLimitingActive(context)) {
Log.e("$TAG Rate limiting is active, aborting")
return
}
Log.i("$TAG Creating launcher shortcuts for chat rooms")
val maxShortcuts = min(ShortcutManagerCompat.getMaxShortcutCountPerActivity(context), 5)
var count = 0
for (room in coreContext.core.chatRooms) {
// Android can usually only have around 4-5 shortcuts at a time
if (count >= maxShortcuts) {
Log.w("$TAG Max amount of shortcuts reached ($count)")
break
}
val shortcut: ShortcutInfoCompat? = createChatRoomShortcut(context, room)
if (shortcut != null) {
Log.i("$TAG Created launcher shortcut for ${shortcut.shortLabel}")
shortcuts.add(shortcut)
count += 1
}
}
ShortcutManagerCompat.setDynamicShortcuts(context, shortcuts)
Log.i("$TAG Created $count launcher shortcuts")
}
@WorkerThread
private suspend fun createChatRoomShortcut(context: Context, chatRoom: ChatRoom): ShortcutInfoCompat? {
val localAddress = chatRoom.localAddress
val peerAddress = chatRoom.peerAddress
val id = LinphoneUtils.getChatRoomId(localAddress, peerAddress)
try {
val categories: ArraySet<String> = ArraySet()
categories.add(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION)
val personsList = arrayListOf<Person>()
val subject: String
val icon: IconCompat
if (chatRoom.hasCapability(ChatRoom.Capabilities.Basic.toInt())) {
val contact =
coreContext.contactsManager.findContactByAddress(peerAddress)
val person = contact?.getPerson()
if (person != null) {
personsList.add(person)
}
icon = person?.icon ?: coreContext.contactsManager.contactAvatar
subject = contact?.name ?: LinphoneUtils.getDisplayName(peerAddress)
} else if (chatRoom.hasCapability(ChatRoom.Capabilities.OneToOne.toInt()) && chatRoom.participants.isNotEmpty()) {
val address = chatRoom.participants.first().address
val contact =
coreContext.contactsManager.findContactByAddress(address)
val person = contact?.getPerson()
if (person != null) {
personsList.add(person)
}
subject = contact?.name ?: LinphoneUtils.getDisplayName(address)
icon = person?.icon ?: coreContext.contactsManager.contactAvatar
} else {
val list = arrayListOf<String>()
for (participant in chatRoom.participants) {
val contact =
coreContext.contactsManager.findContactByAddress(participant.address)
if (contact != null) {
personsList.add(contact.getPerson())
val picture = contact.photo
if (picture != null) {
list.add(picture)
}
}
}
subject = chatRoom.subject.orEmpty()
val iconSize = AppUtils.getDimension(R.dimen.avatar_list_cell_size).toInt()
icon = IconCompat.createWithAdaptiveBitmap(
ImageUtils.getBitmapFromMultipleAvatars(context, iconSize, list)
)
}
val persons = arrayOfNulls<Person>(personsList.size)
personsList.toArray(persons)
val localSipUri = localAddress.asStringUriOnly()
val peerSipUri = peerAddress.asStringUriOnly()
val args = Bundle()
args.putString("RemoteSipUri", peerSipUri)
args.putString("LocalSipUri", localSipUri)
val intent = Intent(Intent.ACTION_MAIN)
intent.setClass(context, MainActivity::class.java)
intent.putExtra("Chat", true)
intent.putExtra("RemoteSipUri", peerSipUri)
intent.putExtra("LocalSipUri", localSipUri)
return ShortcutInfoCompat.Builder(context, id)
.setShortLabel(subject)
.setIcon(icon)
.setPersons(persons)
.setCategories(categories)
.setIntent(intent)
.setLongLived(Version.sdkAboveOrEqual(Version.API30_ANDROID_11))
.setLocusId(LocusIdCompat(id))
.build()
} catch (e: Exception) {
Log.e("$TAG createChatRoomShortcut for id [$id] exception: $e")
}
return null
}
@WorkerThread
fun isShortcutToChatRoomAlreadyCreated(context: Context, chatRoom: ChatRoom): Boolean {
val id = LinphoneUtils.getChatRoomId(chatRoom)
val found = ShortcutManagerCompat.getDynamicShortcuts(context).find {
it.id == id
}
return found != null
}
}
}

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:topRightRadius="16dp" android:bottomRightRadius="16dp" android:bottomLeftRadius="16dp" />
<solid android:color="@color/gray_100"/>
<solid android:color="@color/gray_main2_100"/>
</shape>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="16dp" />
<solid android:color="@color/gray_100"/>
<solid android:color="@color/gray_main2_100"/>
</shape>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:topLeftRadius="16dp" android:topRightRadius="16dp" android:bottomLeftRadius="16dp" />
<solid android:color="@color/gray_main2_100"/>
<solid android:color="@color/orange_main_100"/>
</shape>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="16dp" />
<solid android:color="@color/gray_main2_100"/>
<solid android:color="@color/orange_main_100"/>
</shape>

View file

@ -50,8 +50,8 @@
<androidx.constraintlayout.widget.Barrier
android:id="@+id/background_end_barrier"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="end"
app:barrierMargin="10dp"
app:constraint_referenced_ids="delivery_status, text_message" />
@ -72,8 +72,8 @@
android:id="@+id/text_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:layout_marginTop="12dp"
android:layout_marginStart="20dp"
android:layout_marginTop="@dimen/chat_bubble_text_padding_with_bubble"
android:layout_marginEnd="16dp"
android:paddingBottom="@{model.groupedWithNextOne ? @dimen/chat_bubble_text_padding_with_status : @dimen/chat_bubble_text_padding_with_bubble, default=@dimen/chat_bubble_text_padding_with_status}"
android:text="@{model.text, default=`Lorem ipsum dolor sit amet`}"

View file

@ -5,6 +5,7 @@
<data>
<import type="android.view.View" />
<import type="org.linphone.core.ConsolidatedPresence" />
<import type="org.linphone.core.ChatMessage.State" />
<variable
name="onLongClickListener"
@ -21,59 +22,46 @@
android:onLongClick="@{onLongClickListener}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@{model.isGroupedWithPreviousOne ? @dimen/chat_bubble_grouped_top_margin : @dimen/chat_bubble_top_margin, default=@dimen/chat_bubble_top_margin}"
android:layout_marginEnd="16dp">
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="@{model.isGroupedWithPreviousOne ? @dimen/chat_bubble_grouped_top_margin : @dimen/chat_bubble_top_margin, default=@dimen/chat_bubble_top_margin}">
<androidx.constraintlayout.widget.Barrier
android:id="@+id/background_start_barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="start"
app:barrierMargin="-10dp"
app:constraint_referenced_ids="date_time, text_message" />
<ImageView
android:id="@+id/background"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@{model.isGroupedWithPreviousOne ? @drawable/shape_chat_bubble_outgoing_full : @drawable/shape_chat_bubble_outgoing_first, default=@drawable/shape_chat_bubble_outgoing_first}"
app:layout_constraintStart_toStartOf="@id/background_start_barrier"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="@id/bubble_bottom_barrier"
app:layout_constraintStart_toStartOf="@id/bubble_start_barrier"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.constraintlayout.widget.Barrier
android:id="@+id/bubble_start_barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="start"
app:barrierMargin="-18dp"
app:constraint_referenced_ids="text_message, date_time"/>
<androidx.constraintlayout.widget.Barrier
android:id="@+id/bubble_bottom_barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="text_bottom_anchor, delivery_status"/>
app:layout_constraintBottom_toBottomOf="@id/date_time"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style"
android:id="@+id/text_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="18dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="18dp"
android:text="@{model.text, default=`Lorem ipsum`}"
android:layout_marginStart="16dp"
android:layout_marginTop="@dimen/chat_bubble_text_padding_with_bubble"
android:layout_marginEnd="10dp"
android:paddingBottom="@{model.groupedWithNextOne ? @dimen/chat_bubble_text_padding_with_status : @dimen/chat_bubble_text_padding_with_bubble, default=@dimen/chat_bubble_text_padding_with_status}"
android:text="@{model.text, default=`Hi!`}"
android:textSize="14sp"
android:textColor="@color/gray_main2_700"
android:gravity="center_vertical|start"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintTop_toTopOf="@id/background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="@id/background"/>
<View
android:id="@+id/text_bottom_anchor"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf="@id/text_message"
app:layout_constraintStart_toStartOf="@id/text_message"/>
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style_300"
@ -81,15 +69,14 @@
android:onClick="@{showDeliveryInfoClickListener}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginEnd="5dp"
android:paddingBottom="5dp"
android:text="@{model.time, default=`13:40`}"
android:textSize="12sp"
android:visibility="@{model.isGroupedWithNextOne ? View.VISIBLE : View.GONE}"
app:layout_constraintTop_toBottomOf="@id/text_message"
app:layout_constraintEnd_toStartOf="@id/delivery_status"
app:layout_constraintBottom_toBottomOf="@id/background"/>
app:layout_constraintEnd_toStartOf="@id/delivery_status"/>
<ImageView
style="@style/default_text_style_300"
@ -97,15 +84,31 @@
android:onClick="@{showDeliveryInfoClickListener}"
android:layout_width="@dimen/small_icon_size"
android:layout_height="@dimen/small_icon_size"
android:layout_marginEnd="18dp"
android:layout_marginTop="2dp"
android:src="@{model.statusIcon, default=@drawable/checks}"
android:visibility="@{model.isGroupedWithNextOne ? View.VISIBLE : View.GONE}"
app:layout_constraintTop_toTopOf="@id/date_time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toEndOf="@id/text_message"
app:layout_constraintBottom_toBottomOf="@id/date_time"
app:tint="@color/orange_main_500" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/reactions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:layout_marginTop="-10dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal"
android:text="@{model.reactions, default=@string/emoji_love}"
android:textSize="20sp"
android:textColor="@color/gray_main2_600"
android:visibility="@{model.reactions.length() > 0 ? View.VISIBLE : View.GONE, default=gone}"
app:layout_constraintStart_toStartOf="@id/background"
app:layout_constraintTop_toBottomOf="@id/background"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

View file

@ -14,13 +14,13 @@
<dimen name="icon_size">24dp</dimen>
<dimen name="welcome_icon_size">100dp</dimen>
<dimen name="avatar_bubble_size">24dp</dimen>
<dimen name="avatar_bubble_size">30dp</dimen>
<dimen name="avatar_list_cell_size">45dp</dimen>
<dimen name="avatar_favorite_list_cell_size">50dp</dimen>
<dimen name="avatar_big_size">100dp</dimen>
<dimen name="avatar_in_call_size">120dp</dimen>
<dimen name="avatar_bubble_presence_badge_size">5dp</dimen>
<dimen name="avatar_bubble_presence_badge_size">8dp</dimen>
<dimen name="avatar_presence_badge_size">12dp</dimen>
<dimen name="avatar_presence_badge_in_call_size">26dp</dimen>
<dimen name="avatar_presence_badge_padding">2dp</dimen>