Message message = e.getMessage();
Event event = message.getExtension(Event.class);
if (event != null) {
for (Item item : event.getItems()) {
if (item.getPayload() instanceof AvatarMetadata) {
AvatarMetadata avatarMetadata = (AvatarMetadata) item.getPayload();
// Empty avatar
if (avatarMetadata.getInfoList().isEmpty()) {
notifyListeners(message.getFrom().asBareJid(), null);
} else {
try {
// Check if we have a cached avatar.
byte[] cachedImage = loadFromCache(item.getId());
if (cachedImage != null) {
notifyListeners(message.getFrom().asBareJid(), cachedImage);
} else {
// We don't have a cached copy, let's retrieve it.
// Determine the best info
AvatarMetadata.Info chosenInfo = null;
// Check if there's an avatar, which is stored in PubSub node (and therefore must be in PNG format).
for (AvatarMetadata.Info info : avatarMetadata.getInfoList()) {
if (info.getUrl() == null) {
chosenInfo = info;
}
}
// If only URLs are available, choose the first URL.
if (chosenInfo == null) {
for (AvatarMetadata.Info info : avatarMetadata.getInfoList()) {
if (info.getUrl() != null) {
chosenInfo = info;
break;
}
}