Avatar.java

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