1package eu.siacs.conversations.parser;
2
3import java.text.ParseException;
4import java.text.SimpleDateFormat;
5import java.util.Date;
6import java.util.Locale;
7
8import eu.siacs.conversations.entities.Account;
9import eu.siacs.conversations.entities.Contact;
10import eu.siacs.conversations.services.XmppConnectionService;
11import eu.siacs.conversations.xml.Element;
12
13public abstract class AbstractParser {
14
15 protected XmppConnectionService mXmppConnectionService;
16
17 protected AbstractParser(XmppConnectionService service) {
18 this.mXmppConnectionService = service;
19 }
20
21 protected long getTimestamp(Element packet) {
22 long now = System.currentTimeMillis();
23 if (packet.hasChild("delay")) {
24 try {
25 String stamp = packet.findChild("delay").getAttribute(
26 "stamp");
27 stamp = stamp.replace("Z", "+0000");
28 if (stamp.contains(".")) {
29 Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ",Locale.US)
30 .parse(stamp);
31 if (now<date.getTime()) {
32 return now;
33 } else {
34 return date.getTime();
35 }
36 } else {
37 Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",Locale.US)
38 .parse(stamp);
39 if (now<date.getTime()) {
40 return now;
41 } else {
42 return date.getTime();
43 }
44 }
45 } catch (ParseException e) {
46 return now;
47 }
48 } else {
49 return now;
50 }
51 }
52
53 protected void updateLastseen(Element packet, Account account, boolean presenceOverwrite) {
54 String[] fromParts = packet.getAttribute("from").split("/");
55 String from = fromParts[0];
56 String presence = null;
57 if (fromParts.length >= 2) {
58 presence = fromParts[1];
59 }
60 Contact contact = account.getRoster().getContact(from);
61 long timestamp = getTimestamp(packet);
62 if (timestamp >= contact.lastseen.time) {
63 contact.lastseen.time = timestamp;
64 if ((presence!=null)&&(presenceOverwrite)) {
65 contact.lastseen.presence = presence;
66 }
67 }
68 }
69}