From c4539b85836a8a3233ddba2eaddc5215ad8946f1 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 9 Jan 2019 09:03:33 +0100 Subject: [PATCH] use bitmap instead of URI for avatars in notification. fixes #3310 --- .../conversations/services/AvatarService.java | 7 ++++ .../services/ContactChooserTargetService.java | 2 +- .../services/NotificationService.java | 35 +++++-------------- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/AvatarService.java b/src/main/java/eu/siacs/conversations/services/AvatarService.java index 923646f47..bf37dca4f 100644 --- a/src/main/java/eu/siacs/conversations/services/AvatarService.java +++ b/src/main/java/eu/siacs/conversations/services/AvatarService.java @@ -1,5 +1,6 @@ package eu.siacs.conversations.services; +import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -48,6 +49,8 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded { private static final int TRANSPARENT = 0x00000000; private static final int PLACEHOLDER_COLOR = 0xFF202020; + public static final int SYSTEM_UI_AVATAR_SIZE = 48; + private static final String PREFIX_CONTACT = "contact"; private static final String PREFIX_CONVERSATION = "conversation"; private static final String PREFIX_ACCOUNT = "account"; @@ -62,6 +65,10 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded { this.mXmppConnectionService = service; } + public static int getSystemUiAvatarSize(final Context context) { + return (int) (SYSTEM_UI_AVATAR_SIZE * context.getResources().getDisplayMetrics().density); + } + private Bitmap get(final Contact contact, final int size, boolean cachedOnly) { if (contact.isSelf()) { return get(contact.getAccount(), size, cachedOnly); diff --git a/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java b/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java index 907a3dc86..d207a8566 100644 --- a/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java +++ b/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java @@ -44,7 +44,7 @@ public class ContactChooserTargetService extends ChooserTargetService implements } mXmppConnectionService.populateWithOrderedConversations(conversations, false); final ComponentName componentName = new ComponentName(this, ConversationsActivity.class); - final int pixel = (int) (48 * getResources().getDisplayMetrics().density); + final int pixel = AvatarService.getSystemUiAvatarSize(this); for(Conversation conversation : conversations) { if (conversation.sentMessagesCount() == 0) { continue; diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index faaf8c7c8..6af7037fd 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -11,6 +11,7 @@ import android.content.SharedPreferences; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Typeface; +import android.graphics.drawable.Icon; import android.media.AudioAttributes; import android.media.RingtoneManager; import android.net.Uri; @@ -510,13 +511,14 @@ public class NotificationService { final Conversation conversation = (Conversation) messages.get(0).getConversation(); final UnreadConversation.Builder mUnreadBuilder = new UnreadConversation.Builder(conversation.getName().toString()); mBuilder.setLargeIcon(mXmppConnectionService.getAvatarService() - .get(conversation, getPixel(64))); + .get(conversation, AvatarService.getSystemUiAvatarSize(mXmppConnectionService))); mBuilder.setContentTitle(conversation.getName()); if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) { int count = messages.size(); mBuilder.setContentText(mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count)); } else { Message message; + //TODO starting with Android 9 we might want to put images in MessageStyle if ((message = getImage(messages)) != null) { modifyForImage(mBuilder, mUnreadBuilder, message, messages); } else { @@ -602,8 +604,7 @@ public class NotificationService { private void modifyForImage(final Builder builder, final UnreadConversation.Builder uBuilder, final Message message, final ArrayList messages) { try { - final Bitmap bitmap = mXmppConnectionService.getFileBackend() - .getThumbnail(message, getPixel(288), false); + final Bitmap bitmap = mXmppConnectionService.getFileBackend().getThumbnail(message, getPixel(288), false); final ArrayList tmp = new ArrayList<>(); for (final Message msg : messages) { if (msg.getType() == Message.TYPE_TEXT @@ -638,44 +639,26 @@ public class NotificationService { } else { builder.setName(UIHelper.getMessageDisplayName(message)); } - IconCompat icon = getIcon(message); - if (icon != null) { - builder.setIcon(icon); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + builder.setIcon(IconCompat.createWithBitmap(mXmppConnectionService.getAvatarService().get(message, AvatarService.getSystemUiAvatarSize(mXmppConnectionService), false))); } return builder.build(); } - private IconCompat getIcon(Message message) { - final Contact contact; - if (message.getConversation().getMode() == Conversation.MODE_SINGLE) { - contact = message.getContact(); - } else { - Jid jid = message.getTrueCounterpart(); - contact = jid == null ? null : message.getConversation().getAccount().getRoster().getContact(jid); - } - if (contact != null) { - if (contact.getProfilePhoto() != null && QuickConversationsService.isConversations()) { - return IconCompat.createWithContentUri(contact.getProfilePhoto()); - } - if (contact.getAvatarFilename() != null) { - return IconCompat.createWithContentUri(mXmppConnectionService.getFileBackend().getAvatarUri(contact.getAvatarFilename())); - } - } - return null; - } - private void modifyForTextOnly(final Builder builder, final UnreadConversation.Builder uBuilder, final ArrayList messages) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { final Person me = new Person.Builder().setName(mXmppConnectionService.getString(R.string.me)).build(); NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(me); final Conversation conversation = (Conversation) messages.get(0).getConversation(); - if (conversation.getMode() == Conversation.MODE_MULTI) { + final boolean multiple = conversation.getMode() == Conversation.MODE_MULTI; + if (multiple) { messagingStyle.setConversationTitle(conversation.getName()); } for (Message message : messages) { final Person sender = message.getStatus() == Message.STATUS_RECEIVED ? getPerson(message) : null; messagingStyle.addMessage(UIHelper.getMessagePreview(mXmppConnectionService, message).first, message.getTimeSent(), sender); } + messagingStyle.setGroupConversation(multiple); builder.setStyle(messagingStyle); } else { if (messages.get(0).getConversation().getMode() == Conversation.MODE_SINGLE) {