properly clear muc user avatar caches

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/entities/MucOptions.java            |  9 
src/main/java/eu/siacs/conversations/parser/PresenceParser.java          |  6 
src/main/java/eu/siacs/conversations/services/AvatarService.java         | 22 
src/main/java/eu/siacs/conversations/services/XmppConnectionService.java |  6 
4 files changed, 30 insertions(+), 13 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/entities/MucOptions.java 🔗

@@ -213,8 +213,13 @@ public class MucOptions {
 			return getAccount().getRoster().getContactFromRoster(getJid());
 		}
 
-		public void setAvatar(Avatar avatar) {
-			this.avatar = avatar;
+		public boolean setAvatar(Avatar avatar) {
+			if (this.avatar != null && this.avatar.equals(avatar)) {
+				return false;
+			} else {
+				this.avatar = avatar;
+				return true;
+			}
 		}
 
 		public String getAvatar() {

src/main/java/eu/siacs/conversations/parser/PresenceParser.java 🔗

@@ -35,7 +35,7 @@ public class PresenceParser extends AbstractParser implements
 			processConferencePresence(packet, mucOptions);
 			final List<MucOptions.User> tileUserAfter = mucOptions.getUsers(5);
 			if (!tileUserAfter.equals(tileUserBefore)) {
-				mXmppConnectionService.getAvatarService().clear(conversation);
+				mXmppConnectionService.getAvatarService().clear(mucOptions);
 			}
 			if (before != mucOptions.online() || (mucOptions.online() && count != mucOptions.getUserCount())) {
 				mXmppConnectionService.updateConversationUi();
@@ -86,7 +86,9 @@ public class PresenceParser extends AbstractParser implements
 						if (avatar != null) {
 							avatar.owner = from;
 							if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
-								user.setAvatar(avatar);
+								if (user.setAvatar(avatar)) {
+									mXmppConnectionService.getAvatarService().clear(user);
+								}
 							} else {
 								mXmppConnectionService.fetchAvatar(mucOptions.getAccount(), avatar);
 							}

src/main/java/eu/siacs/conversations/services/AvatarService.java 🔗

@@ -197,8 +197,7 @@ public class AvatarService {
 	public void clear(MucOptions options) {
 		synchronized (this.sizes) {
 			for (Integer size : sizes) {
-				this.mXmppConnectionService.getBitmapCache().remove(
-						key(options, size));
+				this.mXmppConnectionService.getBitmapCache().remove(key(options, size));
 			}
 		}
 	}
@@ -253,8 +252,15 @@ public class AvatarService {
 	public void clear(Account account) {
 		synchronized (this.sizes) {
 			for (Integer size : sizes) {
-				this.mXmppConnectionService.getBitmapCache().remove(
-						key(account, size));
+				this.mXmppConnectionService.getBitmapCache().remove(key(account, size));
+			}
+		}
+	}
+
+	public void clear(MucOptions.User user) {
+		synchronized (this.sizes) {
+			for (Integer size : sizes) {
+				this.mXmppConnectionService.getBitmapCache().remove(key(user, size));
 			}
 		}
 	}
@@ -346,12 +352,12 @@ public class AvatarService {
 		if (avatar != null) {
 			Uri uri = mXmppConnectionService.getFileBackend().getAvatarUri(avatar);
 			if (uri != null) {
-				drawTile(canvas, uri, left, top, right, bottom);
+				if (drawTile(canvas, uri, left, top, right, bottom)) {
+					return true;
+				}
 			}
-		} else {
-			drawTile(canvas, account.getJid().toBareJid().toString(), left, top, right, bottom);
 		}
-		return true;
+		return drawTile(canvas, account.getJid().toBareJid().toString(), left, top, right, bottom);
 	}
 
 	private boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) {

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java 🔗

@@ -2367,7 +2367,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
 								if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
 									MucOptions.User user = conversation.getMucOptions().findUser(avatar.owner.getResourcepart());
 									if (user != null) {
-										user.setAvatar(avatar);
+										if (user.setAvatar(avatar)) {
+											getAvatarService().clear(user);
+											updateConversationUi();
+											updateMucRosterUi();
+										}
 									}
 								}
 							}