use bitmap instead of URI for avatars in notification. fixes #3310

This commit is contained in:
Daniel Gultsch 2019-01-09 09:03:33 +01:00
parent a963ef8453
commit c4539b8583
3 changed files with 17 additions and 27 deletions

View File

@ -1,5 +1,6 @@
package eu.siacs.conversations.services; package eu.siacs.conversations.services;
import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
@ -48,6 +49,8 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
private static final int TRANSPARENT = 0x00000000; private static final int TRANSPARENT = 0x00000000;
private static final int PLACEHOLDER_COLOR = 0xFF202020; 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_CONTACT = "contact";
private static final String PREFIX_CONVERSATION = "conversation"; private static final String PREFIX_CONVERSATION = "conversation";
private static final String PREFIX_ACCOUNT = "account"; private static final String PREFIX_ACCOUNT = "account";
@ -62,6 +65,10 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
this.mXmppConnectionService = service; 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) { private Bitmap get(final Contact contact, final int size, boolean cachedOnly) {
if (contact.isSelf()) { if (contact.isSelf()) {
return get(contact.getAccount(), size, cachedOnly); return get(contact.getAccount(), size, cachedOnly);

View File

@ -44,7 +44,7 @@ public class ContactChooserTargetService extends ChooserTargetService implements
} }
mXmppConnectionService.populateWithOrderedConversations(conversations, false); mXmppConnectionService.populateWithOrderedConversations(conversations, false);
final ComponentName componentName = new ComponentName(this, ConversationsActivity.class); 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) { for(Conversation conversation : conversations) {
if (conversation.sentMessagesCount() == 0) { if (conversation.sentMessagesCount() == 0) {
continue; continue;

View File

@ -11,6 +11,7 @@ import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Icon;
import android.media.AudioAttributes; import android.media.AudioAttributes;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.Uri; import android.net.Uri;
@ -510,13 +511,14 @@ public class NotificationService {
final Conversation conversation = (Conversation) messages.get(0).getConversation(); final Conversation conversation = (Conversation) messages.get(0).getConversation();
final UnreadConversation.Builder mUnreadBuilder = new UnreadConversation.Builder(conversation.getName().toString()); final UnreadConversation.Builder mUnreadBuilder = new UnreadConversation.Builder(conversation.getName().toString());
mBuilder.setLargeIcon(mXmppConnectionService.getAvatarService() mBuilder.setLargeIcon(mXmppConnectionService.getAvatarService()
.get(conversation, getPixel(64))); .get(conversation, AvatarService.getSystemUiAvatarSize(mXmppConnectionService)));
mBuilder.setContentTitle(conversation.getName()); mBuilder.setContentTitle(conversation.getName());
if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) { if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) {
int count = messages.size(); int count = messages.size();
mBuilder.setContentText(mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count)); mBuilder.setContentText(mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count));
} else { } else {
Message message; Message message;
//TODO starting with Android 9 we might want to put images in MessageStyle
if ((message = getImage(messages)) != null) { if ((message = getImage(messages)) != null) {
modifyForImage(mBuilder, mUnreadBuilder, message, messages); modifyForImage(mBuilder, mUnreadBuilder, message, messages);
} else { } else {
@ -602,8 +604,7 @@ public class NotificationService {
private void modifyForImage(final Builder builder, final UnreadConversation.Builder uBuilder, private void modifyForImage(final Builder builder, final UnreadConversation.Builder uBuilder,
final Message message, final ArrayList<Message> messages) { final Message message, final ArrayList<Message> messages) {
try { try {
final Bitmap bitmap = mXmppConnectionService.getFileBackend() final Bitmap bitmap = mXmppConnectionService.getFileBackend().getThumbnail(message, getPixel(288), false);
.getThumbnail(message, getPixel(288), false);
final ArrayList<Message> tmp = new ArrayList<>(); final ArrayList<Message> tmp = new ArrayList<>();
for (final Message msg : messages) { for (final Message msg : messages) {
if (msg.getType() == Message.TYPE_TEXT if (msg.getType() == Message.TYPE_TEXT
@ -638,44 +639,26 @@ public class NotificationService {
} else { } else {
builder.setName(UIHelper.getMessageDisplayName(message)); builder.setName(UIHelper.getMessageDisplayName(message));
} }
IconCompat icon = getIcon(message); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (icon != null) { builder.setIcon(IconCompat.createWithBitmap(mXmppConnectionService.getAvatarService().get(message, AvatarService.getSystemUiAvatarSize(mXmppConnectionService), false)));
builder.setIcon(icon);
} }
return builder.build(); 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<Message> messages) { private void modifyForTextOnly(final Builder builder, final UnreadConversation.Builder uBuilder, final ArrayList<Message> messages) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
final Person me = new Person.Builder().setName(mXmppConnectionService.getString(R.string.me)).build(); final Person me = new Person.Builder().setName(mXmppConnectionService.getString(R.string.me)).build();
NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(me); NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(me);
final Conversation conversation = (Conversation) messages.get(0).getConversation(); 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()); messagingStyle.setConversationTitle(conversation.getName());
} }
for (Message message : messages) { for (Message message : messages) {
final Person sender = message.getStatus() == Message.STATUS_RECEIVED ? getPerson(message) : null; final Person sender = message.getStatus() == Message.STATUS_RECEIVED ? getPerson(message) : null;
messagingStyle.addMessage(UIHelper.getMessagePreview(mXmppConnectionService, message).first, message.getTimeSent(), sender); messagingStyle.addMessage(UIHelper.getMessagePreview(mXmppConnectionService, message).first, message.getTimeSent(), sender);
} }
messagingStyle.setGroupConversation(multiple);
builder.setStyle(messagingStyle); builder.setStyle(messagingStyle);
} else { } else {
if (messages.get(0).getConversation().getMode() == Conversation.MODE_SINGLE) { if (messages.get(0).getConversation().getMode() == Conversation.MODE_SINGLE) {