get rid of lastMessageTransmitted in favor of db query

This commit is contained in:
Daniel Gultsch 2015-12-11 13:52:04 +01:00
parent 61b0681109
commit 293e820a58
5 changed files with 29 additions and 55 deletions

View file

@ -46,7 +46,6 @@ public class Conversation extends AbstractEntity implements Blockable {
public static final String ATTRIBUTE_NEXT_ENCRYPTION = "next_encryption";
public static final String ATTRIBUTE_MUC_PASSWORD = "muc_password";
public static final String ATTRIBUTE_MUTED_TILL = "muted_till";
public static final String ATTRIBUTE_LAST_MESSAGE_TRANSMITTED = "last_message_transmitted";
private String name;
private String contactUuid;
@ -693,33 +692,16 @@ public class Conversation extends AbstractEntity implements Blockable {
}
}
public void resetLastMessageTransmitted() {
this.setAttribute(ATTRIBUTE_LAST_MESSAGE_TRANSMITTED,String.valueOf(-1));
}
public boolean setLastMessageTransmitted(long value) {
long before = getLastMessageTransmitted();
if (value - before > 1000) {
this.setAttribute(ATTRIBUTE_LAST_MESSAGE_TRANSMITTED, String.valueOf(value));
return true;
} else {
return false;
}
}
public long getLastMessageTransmitted() {
long timestamp = getLongAttribute(ATTRIBUTE_LAST_MESSAGE_TRANSMITTED,0);
if (timestamp == 0) {
synchronized (this.messages) {
for(int i = this.messages.size() - 1; i >= 0; --i) {
Message message = this.messages.get(i);
if (message.getStatus() == Message.STATUS_RECEIVED) {
if (message.getStatus() == Message.STATUS_RECEIVED || message.isCarbon()) {
return message.getTimeSent();
}
}
}
}
return timestamp;
return 0;
}
public void setMutedTill(long value) {

View file

@ -329,7 +329,7 @@ public class MessageParser extends AbstractParser implements
}
if ((body != null || pgpEncrypted != null || axolotlEncrypted != null) && !isMucStatusMessage) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.toBareJid(), isTypeGroupChat);
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.toBareJid(), isTypeGroupChat, query);
if (isTypeGroupChat) {
if (counterpart.getResourcepart().equals(conversation.getMucOptions().getActualNick())) {
status = Message.STATUS_SEND_RECEIVED;
@ -431,11 +431,6 @@ public class MessageParser extends AbstractParser implements
mXmppConnectionService.sendMessagePacket(account, receipt);
}
}
if (account.isOnlineAndConnected() && query == null) {
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
mXmppConnectionService.updateConversation(conversation);
}
}
if (message.getStatus() == Message.STATUS_RECEIVED
&& conversation.getOtrSession() != null

View file

@ -9,6 +9,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Base64;
import android.util.Log;
import android.util.Pair;
import org.whispersystems.libaxolotl.AxolotlAddress;
import org.whispersystems.libaxolotl.IdentityKey;
@ -591,6 +592,19 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args);
}
public Pair<Long,String> getLastMessageReceived(Account account) {
SQLiteDatabase db = this.getReadableDatabase();
String sql = "select messages.timeSent,messages.serverMsgId from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and (messages.status=0 or messages.carbon=1 or messages.serverMsgId not null) order by messages.timesent desc limit 1";
String[] args = {account.getUuid()};
Cursor cursor = db.rawQuery(sql,args);
if (cursor.getCount() ==0) {
return null;
} else {
cursor.moveToFirst();
return new Pair<>(cursor.getLong(0),cursor.getString(1));
}
}
public Conversation findConversationByUuid(String conversationUuid) {
SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = {conversationUuid};

View file

@ -1,6 +1,7 @@
package eu.siacs.conversations.services;
import android.util.Log;
import android.util.Pair;
import java.math.BigInteger;
import java.util.ArrayList;
@ -75,16 +76,8 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
}
private long getLastMessageTransmitted(final Account account) {
long timestamp = 0;
for(final Conversation conversation : mXmppConnectionService.getConversations()) {
if (conversation.getAccount() == account) {
long tmp = conversation.getLastMessageTransmitted();
if (tmp > timestamp) {
timestamp = tmp;
}
}
}
return timestamp;
Pair<Long,String> pair = mXmppConnectionService.databaseBackend.getLastMessageReceived(account);
return pair == null ? 0 : pair.first;
}
public Query query(final Conversation conversation) {
@ -166,24 +159,18 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
final Conversation conversation = query.getConversation();
if (conversation != null) {
conversation.sort();
if (conversation.setLastMessageTransmitted(query.getEnd())) {
this.mXmppConnectionService.databaseBackend.updateConversation(conversation);
}
conversation.setHasMessagesLeftOnServer(query.getMessageCount() > 0);
if (query.hasCallback()) {
query.callback();
} else {
this.mXmppConnectionService.updateConversationUi();
}
} else {
for(Conversation tmp : this.mXmppConnectionService.getConversations()) {
if (tmp.getAccount() == query.getAccount()) {
tmp.sort();
if (tmp.setLastMessageTransmitted(query.getEnd())) {
this.mXmppConnectionService.databaseBackend.updateConversation(tmp);
}
}
}
if (query.hasCallback()) {
query.callback();
} else {
this.mXmppConnectionService.updateConversationUi();
}
}

View file

@ -221,9 +221,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
Message message = conversation.findUnsentMessageWithUuid(uuid);
if (message != null) {
markMessage(message, Message.STATUS_SEND);
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
databaseBackend.updateConversation(conversation);
}
}
}
}
@ -2883,7 +2880,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public void clearConversationHistory(final Conversation conversation) {
conversation.clearMessages();
conversation.setHasMessagesLeftOnServer(false); //avoid messages getting loaded through mam
conversation.resetLastMessageTransmitted();
Runnable runnable = new Runnable() {
@Override
public void run() {