2014-10-22 12:38:44 -04:00
|
|
|
package eu.siacs.conversations.xmpp.pep;
|
|
|
|
|
2015-07-20 08:26:29 -04:00
|
|
|
import android.util.Base64;
|
|
|
|
|
2014-10-22 12:38:44 -04:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2014-11-05 15:55:47 -05:00
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
|
|
|
|
2014-10-22 12:38:44 -04:00
|
|
|
public class Avatar {
|
2015-05-05 00:17:34 -04:00
|
|
|
|
|
|
|
public enum Origin { PEP, VCARD };
|
|
|
|
|
2014-10-22 12:38:44 -04:00
|
|
|
public String type;
|
|
|
|
public String sha1sum;
|
|
|
|
public String image;
|
|
|
|
public int height;
|
|
|
|
public int width;
|
|
|
|
public long size;
|
2014-11-05 15:55:47 -05:00
|
|
|
public Jid owner;
|
2015-05-05 00:17:34 -04:00
|
|
|
public Origin origin = Origin.PEP; //default to maintain compat
|
2014-10-22 12:38:44 -04:00
|
|
|
|
|
|
|
public byte[] getImageAsBytes() {
|
|
|
|
return Base64.decode(image, Base64.DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFilename() {
|
2015-05-05 00:17:34 -04:00
|
|
|
return sha1sum;
|
2014-10-22 12:38:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Avatar parseMetadata(Element items) {
|
|
|
|
Element item = items.findChild("item");
|
|
|
|
if (item == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Element metadata = item.findChild("metadata");
|
|
|
|
if (metadata == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String primaryId = item.getAttribute("id");
|
|
|
|
if (primaryId == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
for (Element child : metadata.getChildren()) {
|
|
|
|
if (child.getName().equals("info")
|
|
|
|
&& primaryId.equals(child.getAttribute("id"))) {
|
|
|
|
Avatar avatar = new Avatar();
|
|
|
|
String height = child.getAttribute("height");
|
|
|
|
String width = child.getAttribute("width");
|
|
|
|
String size = child.getAttribute("bytes");
|
|
|
|
try {
|
|
|
|
if (height != null) {
|
|
|
|
avatar.height = Integer.parseInt(height);
|
|
|
|
}
|
|
|
|
if (width != null) {
|
|
|
|
avatar.width = Integer.parseInt(width);
|
|
|
|
}
|
|
|
|
if (size != null) {
|
|
|
|
avatar.size = Long.parseLong(size);
|
|
|
|
}
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
avatar.type = child.getAttribute("type");
|
2015-05-05 00:17:34 -04:00
|
|
|
String hash = child.getAttribute("id");
|
|
|
|
if (!isValidSHA1(hash)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
avatar.sha1sum = hash;
|
|
|
|
avatar.origin = Origin.PEP;
|
2014-10-22 12:38:44 -04:00
|
|
|
return avatar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-05-05 00:17:34 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object object) {
|
|
|
|
if (object != null && object instanceof Avatar) {
|
|
|
|
Avatar other = (Avatar) object;
|
|
|
|
return other.getFilename().equals(this.getFilename());
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Avatar parsePresence(Element x) {
|
2015-05-14 08:42:21 -04:00
|
|
|
String hash = x == null ? null : x.findChildContent("photo");
|
2015-05-05 00:17:34 -04:00
|
|
|
if (hash == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!isValidSHA1(hash)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Avatar avatar = new Avatar();
|
|
|
|
avatar.sha1sum = hash;
|
|
|
|
avatar.origin = Origin.VCARD;
|
|
|
|
return avatar;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isValidSHA1(String s) {
|
|
|
|
return s != null && s.matches("[a-fA-F0-9]{40}");
|
|
|
|
}
|
2014-10-22 12:38:44 -04:00
|
|
|
}
|