display avatar in groups

This commit is contained in:
Daniel Gultsch 2018-04-14 17:54:04 +02:00
parent 32a105fa1c
commit afb7c0592b
5 changed files with 67 additions and 64 deletions

View File

@ -390,6 +390,10 @@ public class MucOptions {
this.features.addAll(features); this.features.addAll(features);
} }
public String getAvatar() {
return account.getRoster().getContact(conversation.getJid()).getAvatar();
}
private void updateFormData(Data form) { private void updateFormData(Data form) {
this.form = form; this.form = form;
} }

View File

@ -48,6 +48,7 @@ import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.UUID;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
@ -649,9 +650,7 @@ public class FileBackend {
avatar.width = options.outWidth; avatar.width = options.outWidth;
avatar.type = options.outMimeType; avatar.type = options.outMimeType;
return avatar; return avatar;
} catch (IOException e) { } catch (NoSuchAlgorithmException | IOException e) {
return null;
} catch (NoSuchAlgorithmException e) {
return null; return null;
} finally { } finally {
close(is); close(is);
@ -663,14 +662,13 @@ public class FileBackend {
return file.exists(); return file.exists();
} }
public boolean save(Avatar avatar) { public boolean save(final Avatar avatar) {
File file; File file;
if (isAvatarCached(avatar)) { if (isAvatarCached(avatar)) {
file = new File(getAvatarPath(avatar.getFilename())); file = new File(getAvatarPath(avatar.getFilename()));
avatar.size = file.length(); avatar.size = file.length();
} else { } else {
String filename = getAvatarPath(avatar.getFilename()); file = new File(mXmppConnectionService.getCacheDir().getAbsolutePath()+"/"+ UUID.randomUUID().toString());
file = new File(filename + ".tmp");
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
OutputStream os = null; OutputStream os = null;
try { try {
@ -685,6 +683,7 @@ public class FileBackend {
mDigestOutputStream.close(); mDigestOutputStream.close();
String sha1sum = CryptoHelper.bytesToHex(digest.digest()); String sha1sum = CryptoHelper.bytesToHex(digest.digest());
if (sha1sum.equals(avatar.sha1sum)) { if (sha1sum.equals(avatar.sha1sum)) {
String filename = getAvatarPath(avatar.getFilename());
file.renameTo(new File(filename)); file.renameTo(new File(filename));
} else { } else {
Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner); Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
@ -701,7 +700,7 @@ public class FileBackend {
return true; return true;
} }
public String getAvatarPath(String avatar) { private String getAvatarPath(String avatar) {
return mXmppConnectionService.getFilesDir().getAbsolutePath() + "/avatars/" + avatar; return mXmppConnectionService.getFilesDir().getAbsolutePath() + "/avatars/" + avatar;
} }

View File

@ -258,14 +258,21 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
if (bitmap != null || cachedOnly) { if (bitmap != null || cachedOnly) {
return bitmap; return bitmap;
} }
final List<MucOptions.User> users = mucOptions.getUsersRelevantForNameAndAvatar();
if (users.size() == 0) { bitmap = mXmppConnectionService.getFileBackend().getAvatar(mucOptions.getAvatar(), size);
Conversation c = mucOptions.getConversation();
bitmap = getImpl(c.getName().toString(), c.getJid().asBareJid().toString(), size); if (bitmap == null) {
} else { final List<MucOptions.User> users = mucOptions.getUsersRelevantForNameAndAvatar();
bitmap = getImpl(users, size); 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); this.mXmppConnectionService.getBitmapCache().put(KEY, bitmap);
return bitmap; return bitmap;
} }

View File

@ -1574,10 +1574,10 @@ public class XmppConnectionService extends Service {
public List<Conversation> findAllConferencesWith(Contact contact) { public List<Conversation> findAllConferencesWith(Contact contact) {
ArrayList<Conversation> results = new ArrayList<>(); ArrayList<Conversation> results = new ArrayList<>();
for (Conversation conversation : conversations) { for (final Conversation c : conversations) {
if (conversation.getMode() == Conversation.MODE_MULTI if (c.getMode() == Conversation.MODE_MULTI
&& conversation.getMucOptions().isContactInRoom(contact)) { && (c.getJid().asBareJid().equals(c.getJid().asBareJid()) || c.getMucOptions().isContactInRoom(contact))) {
results.add(conversation); results.add(c);
} }
} }
return results; return results;
@ -2851,58 +2851,52 @@ public class XmppConnectionService extends Service {
private void fetchAvatarPep(Account account, final Avatar avatar, final UiCallback<Avatar> callback) { private void fetchAvatarPep(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
IqPacket packet = this.mIqGenerator.retrievePepAvatar(avatar); IqPacket packet = this.mIqGenerator.retrievePepAvatar(avatar);
sendIqPacket(account, packet, new OnIqPacketReceived() { sendIqPacket(account, packet, (a, result) -> {
synchronized (mInProgressAvatarFetches) {
@Override mInProgressAvatarFetches.remove(generateFetchKey(a, avatar));
public void onIqPacketReceived(Account account, IqPacket result) { }
synchronized (mInProgressAvatarFetches) { final String ERROR = a.getJid().asBareJid() + ": fetching avatar for " + avatar.owner + " failed ";
mInProgressAvatarFetches.remove(generateFetchKey(account, avatar)); if (result.getType() == IqPacket.TYPE.RESULT) {
} avatar.image = mIqParser.avatarData(result);
final String ERROR = account.getJid().asBareJid() if (avatar.image != null) {
+ ": fetching avatar for " + avatar.owner + " failed "; if (getFileBackend().save(avatar)) {
if (result.getType() == IqPacket.TYPE.RESULT) { if (a.getJid().asBareJid().equals(avatar.owner)) {
avatar.image = mIqParser.avatarData(result); if (a.setAvatar(avatar.getFilename())) {
if (avatar.image != null) { databaseBackend.updateAccount(a);
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();
} }
if (callback != null) { getAvatarService().clear(a);
callback.success(avatar); updateConversationUi();
} updateAccountUi();
Log.d(Config.LOGTAG, account.getJid().asBareJid() } else {
+ ": successfully fetched pep avatar for " + avatar.owner); Contact contact = a.getRoster().getContact(avatar.owner);
return; contact.setAvatar(avatar);
getAvatarService().clear(contact);
updateConversationUi();
updateRosterUi();
} }
} else { if (callback != null) {
callback.success(avatar);
Log.d(Config.LOGTAG, ERROR + "(parsing error)"); }
Log.d(Config.LOGTAG, a.getJid().asBareJid()
+ ": successfully fetched pep avatar for " + avatar.owner);
return;
} }
} else { } 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);
}
}); });
} }

View File

@ -35,7 +35,6 @@ import eu.siacs.conversations.http.AesGcmURLStreamHandler;
import rocks.xmpp.addr.Jid; import rocks.xmpp.addr.Jid;
public final class CryptoHelper { public final class CryptoHelper {
public static final String FILETRANSFER = "?FILETRANSFERv1:";
private final static char[] hexArray = "0123456789abcdef".toCharArray(); 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}"); 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}");