From afb7c0592b62075b72cb614d8c876a76c1ca3ca5 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 14 Apr 2018 17:54:04 +0200 Subject: [PATCH] display avatar in groups --- .../conversations/entities/MucOptions.java | 4 + .../persistance/FileBackend.java | 13 ++- .../conversations/services/AvatarService.java | 19 ++-- .../services/XmppConnectionService.java | 94 +++++++++---------- .../conversations/utils/CryptoHelper.java | 1 - 5 files changed, 67 insertions(+), 64 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/MucOptions.java b/src/main/java/eu/siacs/conversations/entities/MucOptions.java index 3dd0b183a..8a9ed9e41 100644 --- a/src/main/java/eu/siacs/conversations/entities/MucOptions.java +++ b/src/main/java/eu/siacs/conversations/entities/MucOptions.java @@ -390,6 +390,10 @@ public class MucOptions { this.features.addAll(features); } + public String getAvatar() { + return account.getRoster().getContact(conversation.getJid()).getAvatar(); + } + private void updateFormData(Data form) { this.form = form; } diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index df64e0600..dc1ca8dcc 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -48,6 +48,7 @@ import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Locale; +import java.util.UUID; import eu.siacs.conversations.Config; import eu.siacs.conversations.R; @@ -649,9 +650,7 @@ public class FileBackend { avatar.width = options.outWidth; avatar.type = options.outMimeType; return avatar; - } catch (IOException e) { - return null; - } catch (NoSuchAlgorithmException e) { + } catch (NoSuchAlgorithmException | IOException e) { return null; } finally { close(is); @@ -663,14 +662,13 @@ public class FileBackend { return file.exists(); } - public boolean save(Avatar avatar) { + public boolean save(final Avatar avatar) { File file; if (isAvatarCached(avatar)) { file = new File(getAvatarPath(avatar.getFilename())); avatar.size = file.length(); } else { - String filename = getAvatarPath(avatar.getFilename()); - file = new File(filename + ".tmp"); + file = new File(mXmppConnectionService.getCacheDir().getAbsolutePath()+"/"+ UUID.randomUUID().toString()); file.getParentFile().mkdirs(); OutputStream os = null; try { @@ -685,6 +683,7 @@ public class FileBackend { mDigestOutputStream.close(); String sha1sum = CryptoHelper.bytesToHex(digest.digest()); if (sha1sum.equals(avatar.sha1sum)) { + String filename = getAvatarPath(avatar.getFilename()); file.renameTo(new File(filename)); } else { Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner); @@ -701,7 +700,7 @@ public class FileBackend { return true; } - public String getAvatarPath(String avatar) { + private String getAvatarPath(String avatar) { return mXmppConnectionService.getFilesDir().getAbsolutePath() + "/avatars/" + avatar; } diff --git a/src/main/java/eu/siacs/conversations/services/AvatarService.java b/src/main/java/eu/siacs/conversations/services/AvatarService.java index e1a0f0ad2..1332811a6 100644 --- a/src/main/java/eu/siacs/conversations/services/AvatarService.java +++ b/src/main/java/eu/siacs/conversations/services/AvatarService.java @@ -258,14 +258,21 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded { if (bitmap != null || cachedOnly) { return bitmap; } - final List users = mucOptions.getUsersRelevantForNameAndAvatar(); - if (users.size() == 0) { - Conversation c = mucOptions.getConversation(); - bitmap = getImpl(c.getName().toString(), c.getJid().asBareJid().toString(), size); - } else { - bitmap = getImpl(users, size); + + bitmap = mXmppConnectionService.getFileBackend().getAvatar(mucOptions.getAvatar(), size); + + if (bitmap == null) { + final List users = mucOptions.getUsersRelevantForNameAndAvatar(); + if (users.size() == 0) { + Conversation c = mucOptions.getConversation(); + bitmap = getImpl(c.getName().toString(), c.getJid().asBareJid().toString(), size); + } else { + bitmap = getImpl(users, size); + } } + this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap); + return bitmap; } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 9822da360..ef1323805 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -1574,10 +1574,10 @@ public class XmppConnectionService extends Service { public List findAllConferencesWith(Contact contact) { ArrayList results = new ArrayList<>(); - for (Conversation conversation : conversations) { - if (conversation.getMode() == Conversation.MODE_MULTI - && conversation.getMucOptions().isContactInRoom(contact)) { - results.add(conversation); + for (final Conversation c : conversations) { + if (c.getMode() == Conversation.MODE_MULTI + && (c.getJid().asBareJid().equals(c.getJid().asBareJid()) || c.getMucOptions().isContactInRoom(contact))) { + results.add(c); } } return results; @@ -2851,58 +2851,52 @@ public class XmppConnectionService extends Service { private void fetchAvatarPep(Account account, final Avatar avatar, final UiCallback callback) { IqPacket packet = this.mIqGenerator.retrievePepAvatar(avatar); - 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().asBareJid() - + ": fetching avatar for " + avatar.owner + " failed "; - if (result.getType() == IqPacket.TYPE.RESULT) { - avatar.image = mIqParser.avatarData(result); - if (avatar.image != null) { - if (getFileBackend().save(avatar)) { - if (account.getJid().asBareJid().equals(avatar.owner)) { - if (account.setAvatar(avatar.getFilename())) { - databaseBackend.updateAccount(account); - } - getAvatarService().clear(account); - updateConversationUi(); - updateAccountUi(); - } else { - Contact contact = account.getRoster() - .getContact(avatar.owner); - contact.setAvatar(avatar); - getAvatarService().clear(contact); - updateConversationUi(); - updateRosterUi(); + sendIqPacket(account, packet, (a, result) -> { + synchronized (mInProgressAvatarFetches) { + mInProgressAvatarFetches.remove(generateFetchKey(a, avatar)); + } + final String ERROR = a.getJid().asBareJid() + ": fetching avatar for " + avatar.owner + " failed "; + if (result.getType() == IqPacket.TYPE.RESULT) { + avatar.image = mIqParser.avatarData(result); + if (avatar.image != null) { + if (getFileBackend().save(avatar)) { + if (a.getJid().asBareJid().equals(avatar.owner)) { + if (a.setAvatar(avatar.getFilename())) { + databaseBackend.updateAccount(a); } - if (callback != null) { - callback.success(avatar); - } - Log.d(Config.LOGTAG, account.getJid().asBareJid() - + ": successfully fetched pep avatar for " + avatar.owner); - return; + getAvatarService().clear(a); + updateConversationUi(); + updateAccountUi(); + } else { + Contact contact = a.getRoster().getContact(avatar.owner); + contact.setAvatar(avatar); + getAvatarService().clear(contact); + updateConversationUi(); + updateRosterUi(); } - } else { - - Log.d(Config.LOGTAG, ERROR + "(parsing error)"); + if (callback != null) { + callback.success(avatar); + } + Log.d(Config.LOGTAG, a.getJid().asBareJid() + + ": successfully fetched pep avatar for " + avatar.owner); + return; } } else { - Element error = result.findChild("error"); - if (error == null) { - Log.d(Config.LOGTAG, ERROR + "(server error)"); - } else { - Log.d(Config.LOGTAG, ERROR + error.toString()); - } - } - if (callback != null) { - callback.error(0, null); - } + Log.d(Config.LOGTAG, ERROR + "(parsing error)"); + } + } else { + Element error = result.findChild("error"); + if (error == null) { + Log.d(Config.LOGTAG, ERROR + "(server error)"); + } else { + Log.d(Config.LOGTAG, ERROR + error.toString()); + } } + if (callback != null) { + callback.error(0, null); + } + }); } diff --git a/src/main/java/eu/siacs/conversations/utils/CryptoHelper.java b/src/main/java/eu/siacs/conversations/utils/CryptoHelper.java index caecff78b..540c0b9ca 100644 --- a/src/main/java/eu/siacs/conversations/utils/CryptoHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/CryptoHelper.java @@ -35,7 +35,6 @@ import eu.siacs.conversations.http.AesGcmURLStreamHandler; import rocks.xmpp.addr.Jid; public final class CryptoHelper { - public static final String FILETRANSFER = "?FILETRANSFERv1:"; private final static char[] hexArray = "0123456789abcdef".toCharArray(); public static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}");