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
29 public static Avatar parseMetadata(Element items) {
30 Element item = items.findChild("item");
31 if (item==null) {
32 return null;
33 }
34 Element metadata = item.findChild("metadata");
35 if (metadata==null) {
36 return null;
37 }
38 String primaryId = item.getAttribute("id");
39 if (primaryId==null) {
40 return null;
41 }
42 for(Element child : metadata.getChildren()) {
43 if (child.getName().equals("info") && primaryId.equals(child.getAttribute("id"))) {
44 Avatar avatar = new Avatar();
45 String height = child.getAttribute("height");
46 String width = child.getAttribute("width");
47 String size = child.getAttribute("bytes");
48 try {
49 if (height!=null) {
50 avatar.height = Integer.parseInt(height);
51 }
52 if (width!=null) {
53 avatar.width = Integer.parseInt(width);
54 }
55 if (size!=null) {
56 avatar.size = Long.parseLong(size);
57 }
58 } catch (NumberFormatException e) {
59 return null;
60 }
61 avatar.type = child.getAttribute("type");
62 avatar.sha1sum = child.getAttribute("id");
63 return avatar;
64 }
65 }
66 return null;
67 }
68}