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
15 public byte[] getImageAsBytes() {
16 return Base64.decode(image, Base64.DEFAULT);
17 }
18
19 public String getFilename() {
20 if (type == null) {
21 return sha1sum;
22 } else if (type.equalsIgnoreCase("image/webp")) {
23 return sha1sum + ".webp";
24 } else if (type.equalsIgnoreCase("image/png")) {
25 return sha1sum + ".png";
26 } else {
27 return sha1sum;
28 }
29 }
30
31 public static Avatar parseMetadata(Element items) {
32 Element item = items.findChild("item");
33 if (item == null) {
34 return null;
35 }
36 Element metadata = item.findChild("metadata");
37 if (metadata == null) {
38 return null;
39 }
40 String primaryId = item.getAttribute("id");
41 if (primaryId == null) {
42 return null;
43 }
44 for (Element child : metadata.getChildren()) {
45 if (child.getName().equals("info")
46 && primaryId.equals(child.getAttribute("id"))) {
47 Avatar avatar = new Avatar();
48 String height = child.getAttribute("height");
49 String width = child.getAttribute("width");
50 String size = child.getAttribute("bytes");
51 try {
52 if (height != null) {
53 avatar.height = Integer.parseInt(height);
54 }
55 if (width != null) {
56 avatar.width = Integer.parseInt(width);
57 }
58 if (size != null) {
59 avatar.size = Long.parseLong(size);
60 }
61 } catch (NumberFormatException e) {
62 return null;
63 }
64 avatar.type = child.getAttribute("type");
65 avatar.sha1sum = child.getAttribute("id");
66 return avatar;
67 }
68 }
69 return null;
70 }
71}