2014-12-05 00:54:16 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.util.HashSet;
|
2014-12-08 20:59:14 +00:00
|
|
|
import java.util.List;
|
2014-12-05 00:54:16 +00:00
|
|
|
|
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.xml.Element;
|
2014-12-08 20:59:14 +00:00
|
|
|
import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
|
2014-12-05 00:54:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
|
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
|
|
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
|
|
|
|
2014-12-08 20:59:14 +00:00
|
|
|
public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
|
2014-12-05 00:54:16 +00:00
|
|
|
|
|
|
|
private final XmppConnectionService mXmppConnectionService;
|
|
|
|
|
|
|
|
private final HashSet<Query> queries = new HashSet<Query>();
|
|
|
|
|
|
|
|
public MessageArchiveService(final XmppConnectionService service) {
|
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void query(final Conversation conversation) {
|
|
|
|
synchronized (this.queries) {
|
|
|
|
final Account account = conversation.getAccount();
|
|
|
|
long start = conversation.getLastMessageReceived();
|
|
|
|
long end = account.getXmppConnection().getLastSessionEstablished();
|
2014-12-08 20:59:14 +00:00
|
|
|
if (end - start >= Config.MAX_HISTORY_AGE) {
|
|
|
|
start = end - Config.MAX_HISTORY_AGE;
|
|
|
|
}
|
2014-12-05 00:54:16 +00:00
|
|
|
final Query query = new Query(conversation, start, end);
|
|
|
|
this.queries.add(query);
|
|
|
|
IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
|
|
|
|
this.mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
|
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
2014-12-08 20:59:14 +00:00
|
|
|
if (packet.getType() == IqPacket.TYPE_ERROR) {
|
|
|
|
finalizeQuery(query);
|
|
|
|
}
|
2014-12-05 00:54:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 20:59:14 +00:00
|
|
|
private void finalizeQuery(Query query) {
|
|
|
|
synchronized (this.queries) {
|
|
|
|
this.queries.remove(query);
|
|
|
|
}
|
|
|
|
query.getConversation().sort();
|
|
|
|
this.mXmppConnectionService.updateConversationUi();
|
|
|
|
}
|
|
|
|
|
2014-12-05 00:54:16 +00:00
|
|
|
public void processFin(Element fin) {
|
|
|
|
if (fin == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Query query = findQuery(fin.getAttribute("queryid"));
|
|
|
|
if (query == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
boolean complete = fin.getAttributeAsBoolean("complete");
|
|
|
|
Element set = fin.findChild("set","http://jabber.org/protocol/rsm");
|
|
|
|
Element last = set == null ? null : set.findChild("last");
|
|
|
|
if (complete || last == null) {
|
2014-12-08 20:59:14 +00:00
|
|
|
final Account account = query.getConversation().getAccount();
|
|
|
|
Log.d(Config.LOGTAG,account.getJid().toBareJid().toString()+": completed mam query for "+query.getWith().toString());
|
|
|
|
this.finalizeQuery(query);
|
2014-12-05 00:54:16 +00:00
|
|
|
} else {
|
2014-12-08 20:59:14 +00:00
|
|
|
final Query nextQuery = query.next(last == null ? null : last.getContent());
|
2014-12-05 00:54:16 +00:00
|
|
|
IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(nextQuery);
|
|
|
|
synchronized (this.queries) {
|
|
|
|
this.queries.remove(query);
|
|
|
|
this.queries.add(nextQuery);
|
|
|
|
}
|
|
|
|
this.mXmppConnectionService.sendIqPacket(query.getConversation().getAccount(),packet,new OnIqPacketReceived() {
|
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
2014-12-08 20:59:14 +00:00
|
|
|
if (packet.getType() == IqPacket.TYPE_ERROR) {
|
|
|
|
finalizeQuery(nextQuery);
|
|
|
|
}
|
2014-12-05 00:54:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Query findQuery(String id) {
|
|
|
|
if (id == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
synchronized (this.queries) {
|
|
|
|
for(Query query : this.queries) {
|
|
|
|
if (query.getQueryId().equals(id)) {
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 20:59:14 +00:00
|
|
|
@Override
|
|
|
|
public void onAdvancedStreamFeaturesAvailable(Account account) {
|
|
|
|
if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
|
|
|
|
List<Conversation> conversations = mXmppConnectionService.getConversations();
|
|
|
|
for (Conversation conversation : conversations) {
|
|
|
|
if (conversation.getMode() == Conversation.MODE_SINGLE && conversation.getAccount() == account) {
|
|
|
|
this.query(conversation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG,"no mam available");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 00:54:16 +00:00
|
|
|
public class Query {
|
|
|
|
private long start;
|
|
|
|
private long end;
|
|
|
|
private Jid with;
|
|
|
|
private String queryId;
|
|
|
|
private String after = null;
|
|
|
|
private Conversation conversation;
|
|
|
|
|
|
|
|
public Query(Conversation conversation, long start, long end) {
|
|
|
|
this.conversation = conversation;
|
|
|
|
this.with = conversation.getContactJid().toBareJid();
|
|
|
|
this.start = start;
|
|
|
|
this.end = end;
|
|
|
|
this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Query next(String after) {
|
|
|
|
Query query = new Query(this.conversation,this.start,this.end);
|
|
|
|
query.after = after;
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getAfter() {
|
|
|
|
return after;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getQueryId() {
|
|
|
|
return queryId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Jid getWith() {
|
|
|
|
return with;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getStart() {
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getEnd() {
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Conversation getConversation() {
|
|
|
|
return conversation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|