package de.thedevstack.conversationsplus.services; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Typeface; import android.net.Uri; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Locale; import de.thedevstack.android.logcat.Logging; import de.thedevstack.conversationsplus.Config; import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.thedevstack.conversationsplus.R; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.entities.Bookmark; import de.thedevstack.conversationsplus.entities.Contact; import de.thedevstack.conversationsplus.entities.Conversation; import de.thedevstack.conversationsplus.entities.ListItem; import de.thedevstack.conversationsplus.entities.MucOptions; import de.thedevstack.conversationsplus.generator.IqGenerator; import de.thedevstack.conversationsplus.parser.IqParser; import de.thedevstack.conversationsplus.persistance.DatabaseBackend; import de.thedevstack.conversationsplus.ui.UiCallback; import de.thedevstack.conversationsplus.utils.AvatarUtil; import de.thedevstack.conversationsplus.utils.ImageUtil; import de.thedevstack.conversationsplus.utils.UIHelper; import de.thedevstack.conversationsplus.utils.UiUpdateHelper; import de.thedevstack.conversationsplus.utils.XmppSendUtil; import de.thedevstack.conversationsplus.xml.Element; import de.thedevstack.conversationsplus.xmpp.OnIqPacketReceived; import de.thedevstack.conversationsplus.xmpp.pep.Avatar; import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket; public class AvatarService { private static final int FG_COLOR = 0xFFFAFAFA; private static final int TRANSPARENT = 0x00000000; private static final int PLACEHOLDER_COLOR = 0xFF202020; private static final String PREFIX_CONTACT = "contact"; private static final String PREFIX_CONVERSATION = "conversation"; private static final String PREFIX_ACCOUNT = "account"; private static final String PREFIX_GENERIC = "generic"; private static final AvatarService INSTANCE = new AvatarService(); final private ArrayList sizes = new ArrayList<>(); private final List mInProgressAvatarFetches = new ArrayList<>(); public static AvatarService getInstance() { return INSTANCE; } private Bitmap get(final Contact contact, final int size, boolean cachedOnly) { final String KEY = key(contact, size); Bitmap avatar = ImageUtil.getBitmapFromCache(KEY); if (avatar != null || cachedOnly) { return avatar; } if (contact.getProfilePhoto() != null) { avatar = ImageUtil.cropCenterSquare(Uri.parse(contact.getProfilePhoto()), size); } if (avatar == null && contact.getAvatar() != null) { avatar = AvatarUtil.getAvatar(contact.getAvatar(), size); } if (avatar == null) { avatar = get(contact.getDisplayName(), size, cachedOnly); } ImageUtil.addBitmapToCache(KEY, avatar); return avatar; } public void clear(Contact contact) { synchronized (this.sizes) { for (Integer size : sizes) { ImageUtil.removeBitmapFromCache(key(contact, size)); } } } private String key(Contact contact, int size) { synchronized (this.sizes) { if (!this.sizes.contains(size)) { this.sizes.add(size); } } return PREFIX_CONTACT + "_" + contact.getAccount().getJid().toBareJid() + "_" + contact.getJid() + "_" + String.valueOf(size); } public Bitmap get(ListItem item, int size) { return get(item,size,false); } public Bitmap get(ListItem item, int size, boolean cachedOnly) { if (item instanceof Contact) { return get((Contact) item, size,cachedOnly); } else if (item instanceof Bookmark) { Bookmark bookmark = (Bookmark) item; if (bookmark.getConversation() != null) { return get(bookmark.getConversation(), size, cachedOnly); } else { return get(bookmark.getDisplayName(), size, cachedOnly); } } else { return get(item.getDisplayName(), size, cachedOnly); } } public Bitmap get(Conversation conversation, int size) { return get(conversation,size,false); } public Bitmap get(Conversation conversation, int size, boolean cachedOnly) { if (conversation.getMode() == Conversation.MODE_SINGLE) { return get(conversation.getContact(), size, cachedOnly); } else { return get(conversation.getMucOptions(), size, cachedOnly); } } public void clear(Conversation conversation) { if (conversation.getMode() == Conversation.MODE_SINGLE) { clear(conversation.getContact()); } else { clear(conversation.getMucOptions()); } } private Bitmap get(MucOptions mucOptions, int size, boolean cachedOnly) { final String KEY = key(mucOptions, size); Bitmap bitmap = ImageUtil.getBitmapFromCache(KEY); if (bitmap != null || cachedOnly) { return bitmap; } final List users = new ArrayList<>(mucOptions.getUsers()); int count = users.size(); bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); bitmap.eraseColor(TRANSPARENT); if (count == 0) { String name = mucOptions.getConversation().getName(); final String letter = name.isEmpty() ? "X" : name.substring(0,1); final int color = UIHelper.getColorForName(name); drawTile(canvas, letter, color, 0, 0, size, size); } else if (count == 1) { drawTile(canvas, users.get(0), 0, 0, size, size); } else if (count == 2) { drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size); drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size); } else if (count == 3) { drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size); drawTile(canvas, users.get(1), size / 2 + 1, 0, size, size / 2 - 1); drawTile(canvas, users.get(2), size / 2 + 1, size / 2 + 1, size, size); } else if (count == 4) { drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1); drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size); drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1); drawTile(canvas, users.get(3), size / 2 + 1, size / 2 + 1, size, size); } else { drawTile(canvas, users.get(0), 0, 0, size / 2 - 1, size / 2 - 1); drawTile(canvas, users.get(1), 0, size / 2 + 1, size / 2 - 1, size); drawTile(canvas, users.get(2), size / 2 + 1, 0, size, size / 2 - 1); drawTile(canvas, "\u2026", PLACEHOLDER_COLOR, size / 2 + 1, size / 2 + 1, size, size); } ImageUtil.addBitmapToCache(KEY, bitmap); return bitmap; } public void clear(MucOptions options) { synchronized (this.sizes) { for (Integer size : sizes) { ImageUtil.removeBitmapFromCache(key(options, size)); } } } private String key(MucOptions options, int size) { synchronized (this.sizes) { if (!this.sizes.contains(size)) { this.sizes.add(size); } } return PREFIX_CONVERSATION + "_" + options.getConversation().getUuid() + "_" + String.valueOf(size); } public Bitmap get(Account account, int size) { final String KEY = key(account, size); Bitmap avatar = ImageUtil.getBitmapFromCache(KEY); if (avatar != null) { return avatar; } avatar = AvatarUtil.getAvatar(account.getAvatar(), size); if (avatar == null) { avatar = get(account.getJid().toBareJid().toString(), size,false); } ImageUtil.addBitmapToCache(KEY, avatar); return avatar; } public void clear(Account account) { synchronized (this.sizes) { for (Integer size : sizes) { ImageUtil.removeBitmapFromCache(key(account, size)); } } } private String key(Account account, int size) { synchronized (this.sizes) { if (!this.sizes.contains(size)) { this.sizes.add(size); } } return PREFIX_ACCOUNT + "_" + account.getUuid() + "_" + String.valueOf(size); } public Bitmap get(String name, int size) { return get(name,size,false); } public Bitmap get(final String name, final int size, boolean cachedOnly) { final String KEY = key(name, size); Bitmap bitmap = ImageUtil.getBitmapFromCache(KEY); if (bitmap != null || cachedOnly) { return bitmap; } bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); final String trimmedName = name.trim(); final String letter = trimmedName.isEmpty() ? "X" : trimmedName.substring(0,1); final int color = UIHelper.getColorForName(name); drawTile(canvas, letter, color, 0, 0, size, size); ImageUtil.addBitmapToCache(KEY, bitmap); return bitmap; } private String key(String name, int size) { synchronized (this.sizes) { if (!this.sizes.contains(size)) { this.sizes.add(size); } } return PREFIX_GENERIC + "_" + name + "_" + String.valueOf(size); } private void drawTile(Canvas canvas, String letter, int tileColor, int left, int top, int right, int bottom) { letter = letter.toUpperCase(Locale.getDefault()); Paint tilePaint = new Paint(), textPaint = new Paint(); tilePaint.setColor(tileColor); textPaint.setFlags(Paint.ANTI_ALIAS_FLAG); textPaint.setColor(FG_COLOR); textPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL)); textPaint.setTextSize((float) ((right - left) * 0.8)); Rect rect = new Rect(); canvas.drawRect(new Rect(left, top, right, bottom), tilePaint); textPaint.getTextBounds(letter, 0, 1, rect); float width = textPaint.measureText(letter); canvas.drawText(letter, (right + left) / 2 - width / 2, (top + bottom) / 2 + rect.height() / 2, textPaint); } private void drawTile(Canvas canvas, MucOptions.User user, int left, int top, int right, int bottom) { Contact contact = user.getContact(); if (contact != null) { Uri uri = null; if (contact.getProfilePhoto() != null) { uri = Uri.parse(contact.getProfilePhoto()); } else if (contact.getAvatar() != null) { uri = AvatarUtil.getAvatarUri(contact.getAvatar()); } if (uri != null) { Bitmap bitmap = ImageUtil.cropCenter(uri, bottom - top, right - left); if (bitmap != null) { drawTile(canvas, bitmap, left, top, right, bottom); return; } } } String name = contact != null ? contact.getDisplayName() : user.getName(); final String letter = name.isEmpty() ? "X" : name.substring(0,1); final int color = UIHelper.getColorForName(name); drawTile(canvas, letter, color, left, top, right, bottom); } private void drawTile(Canvas canvas, Bitmap bm, int dstleft, int dsttop, int dstright, int dstbottom) { Rect dst = new Rect(dstleft, dsttop, dstright, dstbottom); canvas.drawBitmap(bm, null, dst, null); } public void publishAvatar(final Account account, final Uri image, final UiCallback callback) { final Bitmap.CompressFormat format = Config.AVATAR_FORMAT; final int size = Config.AVATAR_SIZE; final Avatar avatar = AvatarUtil.getPepAvatar(image, size, format); if (avatar != null) { avatar.height = size; avatar.width = size; if (format.equals(Bitmap.CompressFormat.WEBP)) { avatar.type = "image/webp"; } else if (format.equals(Bitmap.CompressFormat.JPEG)) { avatar.type = "image/jpeg"; } else if (format.equals(Bitmap.CompressFormat.PNG)) { avatar.type = "image/png"; } if (!AvatarUtil.save(avatar)) { callback.error(R.string.error_saving_avatar, avatar); return; } final IqPacket packet = IqGenerator.publishAvatar(avatar); XmppSendUtil.sendIqPacket(account, packet, new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket result) { if (result.getType() == IqPacket.TYPE.RESULT) { final IqPacket packet = IqGenerator.publishAvatarMetadata(avatar); XmppSendUtil.sendIqPacket(account, packet, new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket result) { if (result.getType() == IqPacket.TYPE.RESULT) { if (account.setAvatar(avatar.getFilename())) { AvatarService.getInstance().clear(account); DatabaseBackend.getInstance(ConversationsPlusApplication.getAppContext()).updateAccount(account); } callback.success(avatar); } else { callback.error( R.string.error_publish_avatar_server_reject, avatar); } } }); } else { callback.error( R.string.error_publish_avatar_server_reject, avatar); } } }); } else { callback.error(R.string.error_publish_avatar_converting, null); } } private static String generateFetchKey(Account account, final Avatar avatar) { return account.getJid().toBareJid()+"_"+avatar.owner+"_"+avatar.sha1sum; } public void fetchAvatar(Account account, final Avatar avatar, final UiCallback callback) { final String KEY = generateFetchKey(account, avatar); synchronized(this.mInProgressAvatarFetches) { if (this.mInProgressAvatarFetches.contains(KEY)) { return; } else { switch (avatar.origin) { case PEP: this.mInProgressAvatarFetches.add(KEY); fetchAvatarPep(account, avatar, callback); break; case VCARD: this.mInProgressAvatarFetches.add(KEY); fetchAvatarVcard(account, avatar, callback); break; } } } } private void fetchAvatarPep(final Account account, final Avatar avatar, final UiCallback callback) { IqPacket packet = IqGenerator.retrievePepAvatar(avatar); XmppSendUtil.sendIqPacket(account, packet, new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket result) { synchronized (mInProgressAvatarFetches) { mInProgressAvatarFetches.remove(generateFetchKey(account, avatar)); } final String ERROR = account.getJid().toBareJid() + ": fetching avatar for " + avatar.owner + " failed "; if (result.getType() == IqPacket.TYPE.RESULT) { avatar.image = IqParser.avatarData(result); if (avatar.image != null) { if (AvatarUtil.save(avatar)) { if (account.getJid().toBareJid().equals(avatar.owner)) { if (account.setAvatar(avatar.getFilename())) { DatabaseBackend.getInstance(ConversationsPlusApplication.getAppContext()).updateAccount(account); } AvatarService.this.clear(account); UiUpdateHelper.updateConversationUi(); UiUpdateHelper.updateAccountUi(); } else { Contact contact = account.getRoster() .getContact(avatar.owner); contact.setAvatar(avatar); AvatarService.this.clear(contact); UiUpdateHelper.updateConversationUi(); UiUpdateHelper.updateRosterUi(); } if (callback != null) { callback.success(avatar); } Logging.d(Config.LOGTAG, account.getJid().toBareJid() + ": succesfuly fetched pep avatar for " + avatar.owner); return; } } else { Logging.d(Config.LOGTAG, ERROR + "(parsing error)"); } } else { Element error = result.findChild("error"); if (error == null) { Logging.d(Config.LOGTAG, ERROR + "(server error)"); } else { Logging.d(Config.LOGTAG, ERROR + error.toString()); } } if (callback != null) { callback.error(0, null); } } }); } private void fetchAvatarVcard(final Account account, final Avatar avatar, final UiCallback callback) { IqPacket packet = IqGenerator.retrieveVcardAvatar(avatar); XmppSendUtil.sendIqPacket(account, packet, new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket packet) { synchronized (mInProgressAvatarFetches) { mInProgressAvatarFetches.remove(generateFetchKey(account, avatar)); } if (packet.getType() == IqPacket.TYPE.RESULT) { Element vCard = packet.findChild("vCard", "vcard-temp"); Element photo = vCard != null ? vCard.findChild("PHOTO") : null; String image = photo != null ? photo.findChildContent("BINVAL") : null; if (image != null) { avatar.image = image; if (AvatarUtil.save(avatar)) { Logging.d(Config.LOGTAG, account.getJid().toBareJid() + ": successfully fetched vCard avatar for " + avatar.owner); Contact contact = account.getRoster() .getContact(avatar.owner); contact.setAvatar(avatar); AvatarService.this.clear(contact); UiUpdateHelper.updateConversationUi(); UiUpdateHelper.updateRosterUi(); } } } } }); } public void checkForAvatar(Account account, final UiCallback callback) { IqPacket packet = IqGenerator.retrieveAvatarMetaData(null); XmppSendUtil.sendIqPacket(account, packet, new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket packet) { if (packet.getType() == IqPacket.TYPE.RESULT) { Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub"); if (pubsub != null) { Element items = pubsub.findChild("items"); if (items != null) { Avatar avatar = Avatar.parseMetadata(items); if (avatar != null) { avatar.owner = account.getJid().toBareJid(); if (AvatarUtil.isAvatarCached(avatar)) { if (account.setAvatar(avatar.getFilename())) { DatabaseBackend.getInstance(ConversationsPlusApplication.getAppContext()).updateAccount(account); } AvatarService.this.clear(account); callback.success(avatar); } else { fetchAvatarPep(account, avatar, callback); } return; } } } } callback.error(0, null); } }); } public void fetchAvatar(Account account, Avatar avatar) { fetchAvatar(account, avatar, null); } public void clearFetchInProgress(Account account) { synchronized (this.mInProgressAvatarFetches) { for(Iterator iterator = this.mInProgressAvatarFetches.iterator(); iterator.hasNext();) { final String KEY = iterator.next(); if (KEY.startsWith(account.getJid().toBareJid()+"_")) { iterator.remove(); } } } } }