Avatar.java

 1package eu.siacs.conversations.xmpp.pep;
 2
 3import eu.siacs.conversations.xml.Element;
 4import android.util.Base64;
 5
 6public class Avatar {
 7	public String type;
 8	public String sha1sum;
 9	public String image;
10	public int height;
11	public int width;
12	public long size;
13	public String owner;
14	public byte[] getImageAsBytes() {
15		return Base64.decode(image, Base64.DEFAULT);
16	}
17	public String getFilename() {
18		if (type==null) {
19			return sha1sum;
20		} else if (type.equalsIgnoreCase("image/webp")) {
21			return sha1sum+".webp";
22		} else if (type.equalsIgnoreCase("image/png")) {
23			return sha1sum+".png";
24		} else {
25			return sha1sum;
26		}
27	}
28	public static Avatar parseMetadata(Element items) {
29		Element item = items.findChild("item");
30		if (item==null) {
31			return null;
32		}
33		Element metadata = item.findChild("metadata");
34		if (metadata==null) {
35			return null;
36		}
37		String primaryId = item.getAttribute("id");
38		if (primaryId==null) {
39			return null;
40		}
41		for(Element child : metadata.getChildren()) {
42			if (child.getName().equals("info") && primaryId.equals(child.getAttribute("id"))) {
43				Avatar avatar = new Avatar();
44				String height = child.getAttribute("height");
45				String width = child.getAttribute("width");
46				String size = child.getAttribute("bytes");
47				try {
48					if (height!=null) {
49						avatar.height = Integer.parseInt(height);
50					}
51					if (width!=null) {
52						avatar.width = Integer.parseInt(width);
53					}
54					if (size!=null) {
55						avatar.size = Long.parseLong(size);
56					}
57				} catch (NumberFormatException e) {
58					return null;
59				}
60				avatar.type = child.getAttribute("type");
61				avatar.sha1sum = child.getAttribute("id");
62				return avatar;
63			}
64		}
65		return null;
66	}
67}