store last message received date in conversation

This commit is contained in:
iNPUTmice 2014-12-04 16:20:28 +01:00
parent 2b958e51df
commit 34558cc277
5 changed files with 49 additions and 55 deletions

View File

@ -43,6 +43,7 @@ public class Conversation extends AbstractEntity {
public static final String ATTRIBUTE_NEXT_ENCRYPTION = "next_encryption"; public static final String ATTRIBUTE_NEXT_ENCRYPTION = "next_encryption";
public static final String ATTRIBUTE_MUC_PASSWORD = "muc_password"; public static final String ATTRIBUTE_MUC_PASSWORD = "muc_password";
public static final String ATTRIBUTE_MUTED_TILL = "muted_till"; public static final String ATTRIBUTE_MUTED_TILL = "muted_till";
public static final String ATTRIBUTE_LAST_MESSAGE_RECEIVED = "last_message_received";
private String name; private String name;
private String contactUuid; private String contactUuid;
@ -470,6 +471,14 @@ public class Conversation extends AbstractEntity {
} }
} }
public void setLastMessageReceived(long value) {
this.setAttribute(ATTRIBUTE_LAST_MESSAGE_RECEIVED, String.valueOf(value));
}
public long getLastMessageReceived() {
return getLongAttribute(ATTRIBUTE_LAST_MESSAGE_RECEIVED,0);
}
public void setMutedTill(long value) { public void setMutedTill(long value) {
this.setAttribute(ATTRIBUTE_MUTED_TILL, String.valueOf(value)); this.setAttribute(ATTRIBUTE_MUTED_TILL, String.valueOf(value));
} }

View File

@ -4,9 +4,12 @@ import android.util.Base64;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
@ -23,6 +26,8 @@ public abstract class AbstractGenerator {
public final String IDENTITY_NAME = "Conversations 0.9.3"; public final String IDENTITY_NAME = "Conversations 0.9.3";
public final String IDENTITY_TYPE = "phone"; public final String IDENTITY_TYPE = "phone";
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
protected XmppConnectionService mXmppConnectionService; protected XmppConnectionService mXmppConnectionService;
protected AbstractGenerator(XmppConnectionService service) { protected AbstractGenerator(XmppConnectionService service) {
@ -46,4 +51,9 @@ public abstract class AbstractGenerator {
byte[] sha1 = md.digest(s.toString().getBytes()); byte[] sha1 = md.digest(s.toString().getBytes());
return new String(Base64.encode(sha1, Base64.DEFAULT)).trim(); return new String(Base64.encode(sha1, Base64.DEFAULT)).trim();
} }
public static String getTimestamp(long time) {
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
return DATE_FORMAT.format(time);
}
} }

View File

@ -24,40 +24,31 @@ public abstract class AbstractParser {
protected long getTimestamp(Element packet) { protected long getTimestamp(Element packet) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
ArrayList<String> stamps = new ArrayList<>(); Element delay = packet.findChild("delay");
for (Element child : packet.getChildren()) { if (delay == null) {
if (child.getName().equals("delay")) {
stamps.add(child.getAttribute("stamp").replace("Z", "+0000"));
}
}
Collections.sort(stamps);
if (stamps.size() >= 1) {
try {
String stamp = stamps.get(stamps.size() - 1);
if (stamp.contains(".")) {
Date date = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US)
.parse(stamp);
if (now < date.getTime()) {
return now;
} else {
return date.getTime();
}
} else {
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",
Locale.US).parse(stamp);
if (now < date.getTime()) {
return now;
} else {
return date.getTime();
}
}
} catch (ParseException e) {
return now;
}
} else {
return now; return now;
} }
String stamp = delay.getAttribute("stamp");
if (stamp == null) {
return now;
}
try {
long time = parseTimestamp(stamp).getTime();
return now < time ? now : time;
} catch (ParseException e) {
return now;
}
}
public static Date parseTimestamp(String timestamp) throws ParseException {
timestamp = timestamp.replace("Z", "+0000");
SimpleDateFormat dateFormat;
if (timestamp.contains(".")) {
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
} else {
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",Locale.US);
}
return dateFormat.parse(timestamp);
} }
protected void updateLastseen(final Element packet, final Account account, protected void updateLastseen(final Element packet, final Account account,
@ -66,8 +57,7 @@ public abstract class AbstractParser {
try { try {
from = Jid.fromString(packet.getAttribute("from")).toBareJid(); from = Jid.fromString(packet.getAttribute("from")).toBareJid();
} catch (final InvalidJidException e) { } catch (final InvalidJidException e) {
// TODO: Handle this? return;
from = null;
} }
String presence = from == null || from.isBareJid() ? "" : from.getResourcepart(); String presence = from == null || from.isBareJid() ? "" : from.getResourcepart();
Contact contact = account.getRoster().getContact(from); Contact contact = account.getRoster().getContact(from);

View File

@ -14,7 +14,6 @@ import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnMessagePacketReceived; import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.pep.Avatar; import eu.siacs.conversations.xmpp.pep.Avatar;
import eu.siacs.conversations.xmpp.stanzas.MessagePacket; import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
@ -487,6 +486,8 @@ public class MessageParser extends AbstractParser implements
} }
Conversation conversation = message.getConversation(); Conversation conversation = message.getConversation();
conversation.add(message); conversation.add(message);
conversation.setLastMessageReceived(System.currentTimeMillis());
mXmppConnectionService.updateConversation(conversation);
if (message.getStatus() == Message.STATUS_RECEIVED if (message.getStatus() == Message.STATUS_RECEIVED
&& conversation.getOtrSession() != null && conversation.getOtrSession() != null

View File

@ -32,20 +32,14 @@ import net.java.otr4j.session.SessionStatus;
import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection; import org.openintents.openpgp.util.OpenPgpServiceConnection;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import de.duenndns.ssl.MemorizingTrustManager; import de.duenndns.ssl.MemorizingTrustManager;
@ -1251,27 +1245,17 @@ public class XmppConnectionService extends Service {
PresencePacket packet = new PresencePacket(); PresencePacket packet = new PresencePacket();
packet.setFrom(conversation.getAccount().getJid()); packet.setFrom(conversation.getAccount().getJid());
packet.setTo(joinJid); packet.setTo(joinJid);
Element x = new Element("x"); Element x = packet.addChild("x","http://jabber.org/protocol/muc");
x.setAttribute("xmlns", "http://jabber.org/protocol/muc");
if (conversation.getMucOptions().getPassword() != null) { if (conversation.getMucOptions().getPassword() != null) {
Element password = x.addChild("password"); x.addChild("password").setContent(conversation.getMucOptions().getPassword());
password.setContent(conversation.getMucOptions().getPassword());
} }
x.addChild("history").setAttribute("since",PresenceGenerator.getTimestamp(conversation.getLastMessageReceived()));
String sig = account.getPgpSignature(); String sig = account.getPgpSignature();
if (sig != null) { if (sig != null) {
packet.addChild("status").setContent("online"); packet.addChild("status").setContent("online");
packet.addChild("x", "jabber:x:signed").setContent(sig); packet.addChild("x", "jabber:x:signed").setContent(sig);
} }
if (conversation.getMessages().size() != 0) { Log.d(Config.LOGTAG,packet.toString());
final SimpleDateFormat mDateFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
mDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = new Date(conversation.getLatestMessage()
.getTimeSent() + 1000);
x.addChild("history").setAttribute("since",
mDateFormat.format(date));
}
packet.addChild(x);
sendPresencePacket(account, packet); sendPresencePacket(account, packet);
if (!joinJid.equals(conversation.getContactJid())) { if (!joinJid.equals(conversation.getContactJid())) {
conversation.setContactJid(joinJid); conversation.setContactJid(joinJid);