From f6a6982bd0232fed847d4d32103a999aa6f94cd5 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 11 Feb 2014 15:34:24 +0100 Subject: [PATCH] conference histories are now working. only the actually needed history is queried --- .../gultsch/chat/entities/Conversation.java | 2 +- src/de/gultsch/chat/entities/Message.java | 4 +++ .../chat/persistance/DatabaseBackend.java | 4 +++ .../chat/services/XmppConnectionService.java | 28 +++++++++++++++---- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/de/gultsch/chat/entities/Conversation.java b/src/de/gultsch/chat/entities/Conversation.java index bddd1356a..e0e5623cb 100644 --- a/src/de/gultsch/chat/entities/Conversation.java +++ b/src/de/gultsch/chat/entities/Conversation.java @@ -73,7 +73,7 @@ public class Conversation extends AbstractEntity { } public boolean isRead() { - if (this.messages == null) return true; + if ((this.messages == null)||(this.messages.size() == 0)) return true; return this.messages.get(this.messages.size() - 1).isRead(); } diff --git a/src/de/gultsch/chat/entities/Message.java b/src/de/gultsch/chat/entities/Message.java index d53e93c18..a168c645a 100644 --- a/src/de/gultsch/chat/entities/Message.java +++ b/src/de/gultsch/chat/entities/Message.java @@ -127,5 +127,9 @@ public class Message extends AbstractEntity { public void markUnread() { this.read = false; } + + public void setTime(long time) { + this.timeSent = time; + } } diff --git a/src/de/gultsch/chat/persistance/DatabaseBackend.java b/src/de/gultsch/chat/persistance/DatabaseBackend.java index b3290ee0d..f746f4472 100644 --- a/src/de/gultsch/chat/persistance/DatabaseBackend.java +++ b/src/de/gultsch/chat/persistance/DatabaseBackend.java @@ -229,6 +229,10 @@ public class DatabaseBackend extends SQLiteOpenHelper { } } } + + public void mergeMessageIntoConversation(Message message) { + // select counterpart,body,(timeSent/1000)-180 as min,(timeSent/1000)+180 as max from messages where min<1392035670 and max>1392035670; + } public List getContacts(Account account) { List list = new ArrayList(); diff --git a/src/de/gultsch/chat/services/XmppConnectionService.java b/src/de/gultsch/chat/services/XmppConnectionService.java index 5a43f99f6..a71259239 100644 --- a/src/de/gultsch/chat/services/XmppConnectionService.java +++ b/src/de/gultsch/chat/services/XmppConnectionService.java @@ -1,6 +1,9 @@ package de.gultsch.chat.services; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.Hashtable; import java.util.List; @@ -28,12 +31,8 @@ import de.gultsch.chat.xmpp.XmppConnection; import android.app.NotificationManager; import android.app.Service; import android.content.Context; -import android.content.CursorLoader; import android.content.Intent; -import android.content.Loader; -import android.content.Loader.OnLoadCompleteListener; import android.database.ContentObserver; -import android.database.Cursor; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; @@ -115,8 +114,7 @@ public class XmppConnectionService extends Service { String counterPart = null; conversation = findOrCreateConversation(account, jid, muc); if (muc) { - if ((fromParts.length == 1) || (packet.hasChild("subject")) - || (packet.hasChild("delay"))) { + if ((fromParts.length == 1) || (packet.hasChild("subject"))) { return; } counterPart = fromParts[1]; @@ -129,6 +127,16 @@ public class XmppConnectionService extends Service { } Message message = new Message(conversation, counterPart, body, Message.ENCRYPTION_NONE, status); + if (packet.hasChild("delay")) { + try { + String stamp = packet.findChild("delay").getAttribute("stamp"); + stamp = stamp.replace("Z","+0000"); + Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(stamp); + message.setTime(date.getTime()); + } catch (ParseException e) { + Log.d(LOGTAG,"error trying to parse date"+e.getMessage()); + } + } if(notify) { message.markUnread(); } @@ -550,7 +558,15 @@ public class XmppConnectionService extends Service { packet.setAttribute("to", muc + "/" + account.getUsername()); Element x = new Element("x"); x.setAttribute("xmlns", "http://jabber.org/protocol/muc"); + if (conversation.getMessages().size()!=0) { + Element history = new Element("history"); + history.setAttribute("seconds",(System.currentTimeMillis() - conversation.getLatestMessageDate()) / 1000+""); + x.addChild(history); + } else { + Log.d(LOGTAG,"conversation had no prior messages"+conversation.getMessages().size()); + } packet.addChild(x); + Log.d(LOGTAG,packet.toString()); connections.get(conversation.getAccount()).sendPresencePacket(packet); }