From 815970cde9e08682c79bc1a1246d3b7ecd501d33 Mon Sep 17 00:00:00 2001
From: Arne <arne@monocles.de>
Date: Fri, 3 Jan 2025 09:44:28 +0100
Subject: [PATCH] Option for colored usernames in group chats

---
 .../services/NotificationService.java             |  6 +++---
 .../services/XmppConnectionService.java           |  4 ++++
 .../ui/adapter/ConversationAdapter.java           |  3 +++
 .../conversations/ui/adapter/MessageAdapter.java  |  4 ++--
 .../eu/siacs/conversations/utils/UIHelper.java    | 15 +++++++++++++++
 .../res/drawable/rounded_format_color_text_24.xml |  5 +++++
 src/main/res/values/bools.xml                     |  1 +
 src/main/res/values/strings.xml                   |  2 ++
 src/main/res/xml/preferences_interface.xml        |  6 ++++++
 9 files changed, 41 insertions(+), 5 deletions(-)
 create mode 100644 src/main/res/drawable/rounded_format_color_text_24.xml

diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java
index ee00ea618..3b7330552 100644
--- a/src/main/java/eu/siacs/conversations/services/NotificationService.java
+++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java
@@ -1736,7 +1736,7 @@ public class NotificationService {
                 builder.setUri(uri.toString());
             }
         } else {
-            builder.setName(UIHelper.getMessageDisplayName(message));
+            builder.setName(UIHelper.getColoredUsername(mXmppConnectionService, message));
         }
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
             final Jid jid = contact == null ? message.getCounterpart() : contact.getJid();
@@ -1849,7 +1849,7 @@ public class NotificationService {
                 final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
                 SpannableString styledString;
                 for (Message message : messages) {
-                    final String name = UIHelper.getMessageDisplayName(message);
+                    final SpannableString name = UIHelper.getColoredUsername(mXmppConnectionService, message);
                     styledString = new SpannableString(name + ": " + message.getBody());
                     styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
                     style.addLine(styledString);
@@ -1857,7 +1857,7 @@ public class NotificationService {
                 builder.setStyle(style);
                 int count = messages.size();
                 if (count == 1) {
-                    final String name = UIHelper.getMessageDisplayName(messages.get(0));
+                    final SpannableString name = UIHelper.getColoredUsername(mXmppConnectionService, messages.get(0));
                     styledString = new SpannableString(name + ": " + messages.get(0).getBody());
                     styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
                     builder.setContentText(styledString);
diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
index 87c729ff6..32fa12e80 100644
--- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
+++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
@@ -6301,4 +6301,8 @@ public class XmppConnectionService extends Service {
         PUSH,
         PRESENCE
     }
+
+    public boolean colored_muc_names() {
+        return getBooleanPreference("colored_muc_names", R.bool.use_colored_muc_names);
+    }
 }
diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java
index cebb75e16..b81e429af 100644
--- a/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java
+++ b/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java
@@ -196,9 +196,12 @@ public class ConversationAdapter
             if (status == Message.STATUS_RECEIVED) {
                 if (conversation.getMode() == Conversation.MODE_MULTI) {
                     viewHolder.binding.senderName.setVisibility(View.VISIBLE);
+                    /*
                     final String dname = UIHelper.getMessageDisplayName(message);
                     final String[] words = dname.split("\\s+");
                     viewHolder.binding.senderName.setText((words.length > 0 ? words[0] : dname) + ':');
+                     */
+                    viewHolder.binding.senderName.setText(UIHelper.getColoredUsername(activity.xmppConnectionService, message) + ":");
                 } else {
                     viewHolder.binding.senderName.setVisibility(View.GONE);
                 }
diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java
index 86c9fcbb0..a6d7e899b 100644
--- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java
+++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java
@@ -375,7 +375,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
                 if (displayName != null) {
                     timeInfoBuilder.add("");
                     viewHolder.username().setVisibility(View.VISIBLE);
-                    viewHolder.username().setText(displayName);
+                    viewHolder.username().setText(UIHelper.getColoredUsername(activity.xmppConnectionService, message));
                 }
             } else {
                 viewHolder.username().setText(null);
@@ -613,7 +613,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
         if (message.getBody() != null && !message.getBody().equals("")) {
             viewHolder.messageBody().setTextIsSelectable(true);
             viewHolder.messageBody().setVisibility(View.VISIBLE);
-            final String nick = UIHelper.getMessageDisplayName(message);
+            final SpannableString nick = UIHelper.getColoredUsername(activity.xmppConnectionService, message);
             SpannableStringBuilder body = getSpannableBody(message);
             final var processMarkup = body.getSpans(0, body.length(), Message.PlainTextSpan.class).length > 0;
             if (body.length() > Config.MAX_DISPLAY_MESSAGE_CHARS) {
diff --git a/src/main/java/eu/siacs/conversations/utils/UIHelper.java b/src/main/java/eu/siacs/conversations/utils/UIHelper.java
index 29ad53807..52676d811 100644
--- a/src/main/java/eu/siacs/conversations/utils/UIHelper.java
+++ b/src/main/java/eu/siacs/conversations/utils/UIHelper.java
@@ -1,11 +1,16 @@
 package eu.siacs.conversations.utils;
 
 import android.content.Context;
+import android.graphics.Typeface;
 import android.graphics.drawable.Drawable;
 import android.content.res.ColorStateList;
+import android.text.Spannable;
+import android.text.SpannableString;
 import android.text.SpannableStringBuilder;
 import android.text.format.DateFormat;
 import android.text.format.DateUtils;
+import android.text.style.ForegroundColorSpan;
+import android.text.style.StyleSpan;
 import android.util.Pair;
 import android.widget.TextView;
 
@@ -610,4 +615,14 @@ public class UIHelper {
             return size + " B";
         }
     }
+
+    public static SpannableString getColoredUsername(final XmppConnectionService service, final Message message) {
+        final SpannableString user;
+        user = SpannableString.valueOf(UIHelper.getMessageDisplayName(message));
+        user.setSpan(new StyleSpan(Typeface.BOLD), 0, user.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+        if (service.colored_muc_names()) {
+            user.setSpan(new ForegroundColorSpan(message.getAvatarBackgroundColor()), 0, user.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+        }
+        return user;
+    }
 }
diff --git a/src/main/res/drawable/rounded_format_color_text_24.xml b/src/main/res/drawable/rounded_format_color_text_24.xml
new file mode 100644
index 000000000..2a8f0858f
--- /dev/null
+++ b/src/main/res/drawable/rounded_format_color_text_24.xml
@@ -0,0 +1,5 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="?colorControlNormal" android:viewportHeight="960" android:viewportWidth="960" android:width="24dp">
+      
+    <path android:fillColor="@android:color/white" android:pathData="M120,960Q103,960 91.5,948.5Q80,937 80,920L80,840Q80,823 91.5,811.5Q103,800 120,800L840,800Q857,800 868.5,811.5Q880,823 880,840L880,920Q880,937 868.5,948.5Q857,960 840,960L120,960ZM285,680Q262,680 248.5,660.5Q235,641 243,619L419,150Q424,136 436,128Q448,120 462,120L498,120Q513,120 524.5,128Q536,136 541,150L718,620Q726,642 712.5,661Q699,680 676,680Q662,680 650,672Q638,664 633,650L594,536L368,536L327,651Q322,665 310.5,672.5Q299,680 285,680ZM396,456L564,456L482,224L478,224L396,456Z"/>
+    
+</vector>
diff --git a/src/main/res/values/bools.xml b/src/main/res/values/bools.xml
index 4e0b4207d..38f2372a3 100644
--- a/src/main/res/values/bools.xml
+++ b/src/main/res/values/bools.xml
@@ -13,4 +13,5 @@
     <bool name="app_lock_enabled">false</bool>
     <bool name="secure_tls">false</bool>
     <bool name="prefer_ipv6">false</bool>
+    <bool name="use_colored_muc_names">false</bool>
 </resources>
diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml
index 12f3f7662..9c6052596 100644
--- a/src/main/res/values/strings.xml
+++ b/src/main/res/values/strings.xml
@@ -1406,4 +1406,6 @@
     <string name="pref_secure_tls">Use secure TLS Ciphers only</string>
     <string name="pref_prefer_ipv6_summary">Prefers IPv6 when connecting</string>
     <string name="pref_prefer_ipv6">Prefer IPv6</string>
+    <string name="pref_use_colored_muc_names">Use colored user names</string>
+    <string name="pref_use_colored_muc_names_summary">Use colored user names in group chats</string>
 </resources>
\ No newline at end of file
diff --git a/src/main/res/xml/preferences_interface.xml b/src/main/res/xml/preferences_interface.xml
index e24881b7a..18e873ce1 100644
--- a/src/main/res/xml/preferences_interface.xml
+++ b/src/main/res/xml/preferences_interface.xml
@@ -103,6 +103,12 @@
             android:key="show_own_accounts"
             android:summary="@string/pref_show_own_accounts_summary"
             android:title="@string/pref_show_own_accounts" />
+        <SwitchPreferenceCompat
+            android:defaultValue="@bool/use_colored_muc_names"
+            android:icon="@drawable/rounded_format_color_text_24"
+            android:key="colored_muc_names"
+            android:summary="@string/pref_use_colored_muc_names_summary"
+            android:title="@string/pref_use_colored_muc_names" />
         <SwitchPreferenceCompat
             android:defaultValue="@bool/show_nav_bar"
             android:key="show_nav_bar"