From be1e7ee9a9ecaa55d9a72671dab2196abd156fde Mon Sep 17 00:00:00 2001
From: ganfra <francoisg@matrix.org>
Date: Tue, 8 Dec 2020 12:44:08 +0100
Subject: [PATCH] VoIP: add call back action on rejected tile

---
 .../im/vector/app/core/extensions/TextView.kt | 27 ++++++++++++++++---
 .../timeline/item/CallTileTimelineItem.kt     |  7 +++--
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/vector/src/main/java/im/vector/app/core/extensions/TextView.kt b/vector/src/main/java/im/vector/app/core/extensions/TextView.kt
index 4294358ae0..17dccc7eef 100644
--- a/vector/src/main/java/im/vector/app/core/extensions/TextView.kt
+++ b/vector/src/main/java/im/vector/app/core/extensions/TextView.kt
@@ -18,8 +18,12 @@ package im.vector.app.core.extensions
 
 import android.text.Spannable
 import android.text.SpannableString
+import android.text.TextPaint
+import android.text.method.LinkMovementMethod
+import android.text.style.ClickableSpan
 import android.text.style.ForegroundColorSpan
 import android.text.style.UnderlineSpan
+import android.view.View
 import android.widget.TextView
 import androidx.annotation.AttrRes
 import androidx.annotation.ColorRes
@@ -52,11 +56,13 @@ fun TextView.setTextOrHide(newText: CharSequence?, hideWhenBlank: Boolean = true
  * @param coloredTextRes the resource id of the colored part of the text
  * @param colorAttribute attribute of the color. Default to colorAccent
  * @param underline true to also underline the text. Default to false
+ * @param onClick attributes to handle click on the colored part if needed required
  */
 fun TextView.setTextWithColoredPart(@StringRes fullTextRes: Int,
                                     @StringRes coloredTextRes: Int,
                                     @AttrRes colorAttribute: Int = R.attr.colorAccent,
-                                    underline: Boolean = false) {
+                                    underline: Boolean = false,
+                                    onClick: (() -> Unit)?) {
     val coloredPart = resources.getString(coloredTextRes)
     // Insert colored part into the full text
     val fullText = resources.getString(fullTextRes, coloredPart)
@@ -69,6 +75,21 @@ fun TextView.setTextWithColoredPart(@StringRes fullTextRes: Int,
     text = SpannableString(fullText)
             .apply {
                 setSpan(foregroundSpan, index, index + coloredPart.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
+                if (onClick != null) {
+                    val clickableSpan = object : ClickableSpan() {
+                        override fun onClick(widget: View) {
+                            onClick()
+                        }
+
+                        override fun updateDrawState(ds: TextPaint) {
+                            ds.color = color
+                            // underline will be handled separately if needed, see below
+                            ds.isUnderlineText = false
+                        }
+                    }
+                    setSpan(clickableSpan, index, index + coloredPart.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
+                    movementMethod = LinkMovementMethod.getInstance()
+                }
                 if (underline) {
                     setSpan(UnderlineSpan(), index, index + coloredPart.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                 }
@@ -76,12 +97,12 @@ fun TextView.setTextWithColoredPart(@StringRes fullTextRes: Int,
 }
 
 fun TextView.setLeftDrawable(@DrawableRes iconRes: Int, @ColorRes tintColor: Int? = null) {
-    val icon = if(tintColor != null){
+    val icon = if (tintColor != null) {
         val tint = ContextCompat.getColor(context, tintColor)
         ContextCompat.getDrawable(context, iconRes)?.also {
             DrawableCompat.setTint(it.mutate(), tint)
         }
-    }else {
+    } else {
         ContextCompat.getDrawable(context, iconRes)
     }
     setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null)
diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/CallTileTimelineItem.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/CallTileTimelineItem.kt
index 06fbb37c55..d6748270b4 100644
--- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/CallTileTimelineItem.kt
+++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/CallTileTimelineItem.kt
@@ -96,12 +96,15 @@ abstract class CallTileTimelineItem : AbsBaseMessageItem<CallTileTimelineItem.Ho
         when (attributes.callStatus) {
             CallStatus.INVITED -> if (attributes.informationData.sentByMe) {
                 setText(R.string.call_tile_you_started_call)
-            }else {
+            } else {
                 text = context.getString(R.string.call_tile_other_started_call, attributes.userOfInterest.getBestName())
             }
             CallStatus.IN_CALL -> setText(R.string.call_tile_in_call)
             CallStatus.REJECTED -> if (attributes.informationData.sentByMe) {
-                setTextWithColoredPart(R.string.call_tile_you_declined, R.string.call_tile_call_back)
+                setTextWithColoredPart(R.string.call_tile_you_declined, R.string.call_tile_call_back) {
+                    val callbackAction = RoomDetailAction.StartCall(attributes.callKind == CallKind.VIDEO)
+                    attributes.callback?.onTimelineItemAction(callbackAction)
+                }
             } else {
                 text = context.getString(R.string.call_tile_other_declined, attributes.userOfInterest.getBestName())
             }