Notification colour from account colour

Stephen Paul Weber created

Change summary

src/main/java/eu/siacs/conversations/services/NotificationService.java | 48 
1 file changed, 27 insertions(+), 21 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/services/NotificationService.java 🔗

@@ -672,7 +672,7 @@ public class NotificationService {
         builder.setFullScreenIntent(pendingIntent, true);
         builder.setContentIntent(pendingIntent); // old androids need this?
         builder.setOngoing(true);
-        modifyIncomingCall(builder);
+        modifyIncomingCall(builder, contact.getAccount());
         final Notification notification = builder.build();
         notification.flags = notification.flags | Notification.FLAG_INSISTENT;
         notify(INCOMING_CALL_NOTIFICATION_ID, notification);
@@ -879,10 +879,16 @@ public class NotificationService {
         }
     }
 
-    private void setNotificationColor(final Builder mBuilder) {
-        TypedValue typedValue = new TypedValue();
-        mXmppConnectionService.getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
-        mBuilder.setColor(typedValue.data);
+    private void setNotificationColor(final Builder mBuilder, Account account) {
+        int color;
+        if (account != null && mXmppConnectionService.getAccounts().size() > 1) {
+		      color = UIHelper.getColorForName(account.getJid().asBareJid().toString());
+        } else {
+            TypedValue typedValue = new TypedValue();
+            mXmppConnectionService.getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
+            color = typedValue.data;
+        }
+        mBuilder.setColor(color);
     }
 
     public void updateNotification() {
@@ -921,17 +927,16 @@ public class NotificationService {
             }
             final Builder mBuilder;
             if (notifications.size() == 1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
-                mBuilder =
-                        buildSingleConversations(
-                                notifications.values().iterator().next(), notify, quiteHours);
-                modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences);
+                ArrayList<Message> messages = notifications.values().iterator().next();
+                mBuilder = buildSingleConversations(messages, notify, quiteHours);
+                modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences, messages.isEmpty() ? null : messages.get(0).getConversation().getAccount());
                 notify(NOTIFICATION_ID, mBuilder.build());
             } else {
                 mBuilder = buildMultipleConversation(notify, quiteHours);
                 if (notifyOnlyOneChild) {
                     mBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
                 }
-                modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences);
+                modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences, null);
                 if (!summaryOnly) {
                     for (Map.Entry<String, ArrayList<Message>> entry : notifications.entrySet()) {
                         String uuid = entry.getKey();
@@ -943,10 +948,8 @@ public class NotificationService {
                             singleBuilder.setGroupAlertBehavior(
                                     NotificationCompat.GROUP_ALERT_SUMMARY);
                         }
-                        modifyForSoundVibrationAndLight(
-                                singleBuilder, notifyThis, quiteHours, preferences);
+                        modifyForSoundVibrationAndLight(singleBuilder, notifyThis, quiteHours, preferences, entry.getValue().isEmpty() ? null : entry.getValue().get(0).getConversation().getAccount());
                         singleBuilder.setGroup(MESSAGES_GROUP);
-                        setNotificationColor(singleBuilder);
                         notify(entry.getKey(), NOTIFICATION_ID, singleBuilder.build());
                     }
                 }
@@ -981,7 +984,7 @@ public class NotificationService {
     }
 
     private void modifyForSoundVibrationAndLight(
-            Builder mBuilder, boolean notify, boolean quietHours, SharedPreferences preferences) {
+            Builder mBuilder, boolean notify, boolean quietHours, SharedPreferences preferences, Account account) {
         final Resources resources = mXmppConnectionService.getResources();
         final String ringtone =
                 preferences.getString(
@@ -1019,16 +1022,19 @@ public class NotificationService {
                                 ? NotificationCompat.PRIORITY_HIGH
                                 : NotificationCompat.PRIORITY_DEFAULT)
                         : NotificationCompat.PRIORITY_LOW);
-        setNotificationColor(mBuilder);
+        setNotificationColor(mBuilder, account);
         mBuilder.setDefaults(0);
         if (led) {
             mBuilder.setLights(LED_COLOR, 2000, 3000);
         }
     }
 
-    private void modifyIncomingCall(final Builder mBuilder) {
+    private void modifyIncomingCall(final Builder mBuilder, Account account) {
         mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
-        setNotificationColor(mBuilder);
+        setNotificationColor(mBuilder, account);
+        if (Build.VERSION.SDK_INT >= 26 && account != null && mXmppConnectionService.getAccounts().size() > 1) {
+            mBuilder.setColorized(true);
+        }
         mBuilder.setLights(LED_COLOR, 2000, 3000);
     }
 
@@ -1091,7 +1097,7 @@ public class NotificationService {
             builder.setContentIntent(createContentIntent(firstConversation));
         }
         builder.setDeleteIntent(createMissedCallsDeleteIntent(null));
-        modifyMissedCall(builder);
+        modifyMissedCall(builder, null);
         return builder;
     }
 
@@ -1147,11 +1153,11 @@ public class NotificationService {
                                     (Conversation) conversation,
                                     AvatarService.getSystemUiAvatarSize(mXmppConnectionService)));
         }
-        modifyMissedCall(builder);
+        modifyMissedCall(builder, conversation.getAccount());
         return builder;
     }
 
-    private void modifyMissedCall(final Builder builder) {
+    private void modifyMissedCall(final Builder builder, Account account) {
         final SharedPreferences preferences =
                 PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
         final Resources resources = mXmppConnectionService.getResources();
@@ -1161,7 +1167,7 @@ public class NotificationService {
         }
         builder.setPriority(NotificationCompat.PRIORITY_HIGH);
         builder.setSound(null);
-        setNotificationColor(builder);
+        setNotificationColor(builder, account);
     }
 
     private Builder buildMultipleConversation(final boolean notify, final boolean quietHours) {