mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Use ConversationManager for timestamps and unread counts
This commit is contained in:
@@ -23,11 +23,7 @@ import org.briarproject.android.util.BriarRecyclerView;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.contact.ContactManager;
|
||||
import org.briarproject.api.conversation.ConversationForumInvitationItem;
|
||||
import org.briarproject.api.conversation.ConversationIntroductionRequestItem;
|
||||
import org.briarproject.api.conversation.ConversationIntroductionResponseItem;
|
||||
import org.briarproject.api.conversation.ConversationItem;
|
||||
import org.briarproject.api.conversation.ConversationMessageItem;
|
||||
import org.briarproject.api.conversation.ConversationManager;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.NoSuchContactException;
|
||||
import org.briarproject.api.event.ContactAddedEvent;
|
||||
@@ -40,22 +36,14 @@ import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.event.EventListener;
|
||||
import org.briarproject.api.event.MessageStateChangedEvent;
|
||||
import org.briarproject.api.event.PrivateMessageReceivedEvent;
|
||||
import org.briarproject.api.forum.ForumInvitationMessage;
|
||||
import org.briarproject.api.forum.ForumSharingManager;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.introduction.IntroductionManager;
|
||||
import org.briarproject.api.introduction.IntroductionMessage;
|
||||
import org.briarproject.api.introduction.IntroductionRequest;
|
||||
import org.briarproject.api.introduction.IntroductionResponse;
|
||||
import org.briarproject.api.messaging.MessagingManager;
|
||||
import org.briarproject.api.messaging.PrivateMessageHeader;
|
||||
import org.briarproject.api.plugins.ConnectionRegistry;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -88,11 +76,7 @@ public class ContactListFragment extends BaseFragment implements EventListener {
|
||||
@Inject
|
||||
protected volatile IdentityManager identityManager;
|
||||
@Inject
|
||||
protected volatile MessagingManager messagingManager;
|
||||
@Inject
|
||||
protected volatile IntroductionManager introductionManager;
|
||||
@Inject
|
||||
protected volatile ForumSharingManager forumSharingManager;
|
||||
protected volatile ConversationManager conversationManager;
|
||||
|
||||
public static ContactListFragment newInstance() {
|
||||
|
||||
@@ -208,15 +192,19 @@ public class ContactListFragment extends BaseFragment implements EventListener {
|
||||
try {
|
||||
ContactId id = c.getId();
|
||||
GroupId groupId =
|
||||
messagingManager.getConversationId(id);
|
||||
Collection<ConversationItem> messages =
|
||||
getMessages(id);
|
||||
conversationManager.getConversationId(id);
|
||||
boolean connected =
|
||||
connectionRegistry.isConnected(c.getId());
|
||||
LocalAuthor localAuthor = identityManager
|
||||
.getLocalAuthor(c.getLocalAuthorId());
|
||||
long timestamp = conversationManager.getTimestamp(id);
|
||||
long now1 = System.currentTimeMillis();
|
||||
int unread = conversationManager.getUnreadCount(id);
|
||||
long duration = System.currentTimeMillis() - now1;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Loading unread messages took " + duration + " ms");
|
||||
contacts.add(new ContactListItem(c, localAuthor,
|
||||
connected, groupId, messages));
|
||||
connected, groupId, timestamp, unread));
|
||||
} catch (NoSuchContactException e) {
|
||||
// Continue
|
||||
}
|
||||
@@ -264,13 +252,11 @@ public class ContactListFragment extends BaseFragment implements EventListener {
|
||||
LOG.info("Message received, update contact");
|
||||
PrivateMessageReceivedEvent p = (PrivateMessageReceivedEvent) e;
|
||||
PrivateMessageHeader h = p.getMessageHeader();
|
||||
updateItem(p.getGroupId(), ConversationMessageItem.from(h));
|
||||
updateItem(p.getGroupId(), h.getTimestamp(), h.isRead());
|
||||
} else if (e instanceof MessageStateChangedEvent) {
|
||||
MessageStateChangedEvent m = (MessageStateChangedEvent) e;
|
||||
ClientId c = m.getClientId();
|
||||
if (m.getState() == DELIVERED &&
|
||||
(c.equals(introductionManager.getClientId()) ||
|
||||
c.equals(forumSharingManager.getClientId()))) {
|
||||
if (m.getState() == DELIVERED && conversationManager.isWrappedClient(c)) {
|
||||
LOG.info("Message added, reloading");
|
||||
reloadConversation(m.getMessage().getGroupId());
|
||||
}
|
||||
@@ -282,10 +268,10 @@ public class ContactListFragment extends BaseFragment implements EventListener {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
ContactId c = messagingManager.getContactId(g);
|
||||
Collection<ConversationItem> messages =
|
||||
getMessages(c);
|
||||
updateItem(c, messages);
|
||||
ContactId c = conversationManager.getContactId(g);
|
||||
long timestamp = conversationManager.getTimestamp(c);
|
||||
int unread = conversationManager.getUnreadCount(c);
|
||||
updateItem(c, timestamp, unread);
|
||||
} catch (NoSuchContactException e) {
|
||||
LOG.info("Contact removed");
|
||||
} catch (DbException e) {
|
||||
@@ -296,29 +282,31 @@ public class ContactListFragment extends BaseFragment implements EventListener {
|
||||
});
|
||||
}
|
||||
|
||||
private void updateItem(final ContactId c,
|
||||
final Collection<ConversationItem> messages) {
|
||||
private void updateItem(final ContactId c, final long timestamp, final int unread) {
|
||||
listener.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int position = adapter.findItemPosition(c);
|
||||
ContactListItem item = adapter.getItem(position);
|
||||
if (item != null) {
|
||||
item.setMessages(messages);
|
||||
item.setTimestamp(timestamp);
|
||||
item.setUnreadCount(unread);
|
||||
adapter.updateItem(position, item);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void updateItem(final GroupId g, final ConversationItem m) {
|
||||
private void updateItem(final GroupId g, final long timestamp, final boolean unread) {
|
||||
listener.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int position = adapter.findItemPosition(g);
|
||||
ContactListItem item = adapter.getItem(position);
|
||||
if (item != null) {
|
||||
item.addMessage(m);
|
||||
item.setTimestamp(timestamp);
|
||||
if (unread)
|
||||
item.setUnreadCount(item.getUnreadCount() + 1);
|
||||
adapter.updateItem(position, item);
|
||||
}
|
||||
}
|
||||
@@ -349,50 +337,4 @@ public class ContactListFragment extends BaseFragment implements EventListener {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// This needs to be called from the DB thread
|
||||
private Collection<ConversationItem> getMessages(ContactId id)
|
||||
throws DbException {
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
Collection<ConversationItem> messages = new ArrayList<>();
|
||||
|
||||
Collection<PrivateMessageHeader> headers =
|
||||
messagingManager.getMessageHeaders(id);
|
||||
for (PrivateMessageHeader h : headers) {
|
||||
messages.add(ConversationMessageItem.from(h));
|
||||
}
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Loading message headers took " + duration + " ms");
|
||||
|
||||
now = System.currentTimeMillis();
|
||||
Collection<IntroductionMessage> introductions =
|
||||
introductionManager
|
||||
.getIntroductionMessages(id);
|
||||
for (IntroductionMessage m : introductions) {
|
||||
if (m instanceof IntroductionRequest)
|
||||
messages.add(ConversationIntroductionRequestItem
|
||||
.from((IntroductionRequest) m));
|
||||
else
|
||||
messages.add(ConversationIntroductionResponseItem
|
||||
.from((IntroductionResponse) m));
|
||||
}
|
||||
duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Loading introduction messages took " + duration + " ms");
|
||||
|
||||
now = System.currentTimeMillis();
|
||||
Collection<ForumInvitationMessage> invitations =
|
||||
forumSharingManager.getInvitationMessages(id);
|
||||
for (ForumInvitationMessage i : invitations) {
|
||||
messages.add(ConversationForumInvitationItem.from(i));
|
||||
}
|
||||
duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Loading forum invitations took " + duration + " ms");
|
||||
|
||||
return messages;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package org.briarproject.android.contact;
|
||||
|
||||
import org.briarproject.api.contact.Contact;
|
||||
import org.briarproject.api.conversation.ConversationItem;
|
||||
import org.briarproject.api.conversation.ConversationManager;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
|
||||
@@ -13,40 +15,20 @@ public class ContactListItem {
|
||||
private final Contact contact;
|
||||
private final LocalAuthor localAuthor;
|
||||
private final GroupId groupId;
|
||||
private boolean connected, empty;
|
||||
private boolean connected;
|
||||
private long timestamp;
|
||||
private int unread;
|
||||
|
||||
public ContactListItem(Contact contact, LocalAuthor localAuthor,
|
||||
boolean connected,
|
||||
GroupId groupId,
|
||||
Collection<ConversationItem> messages) {
|
||||
long timestamp, int unread) {
|
||||
this.contact = contact;
|
||||
this.localAuthor = localAuthor;
|
||||
this.groupId = groupId;
|
||||
this.connected = connected;
|
||||
setMessages(messages);
|
||||
}
|
||||
|
||||
void setMessages(Collection<ConversationItem> messages) {
|
||||
empty = messages == null || messages.isEmpty();
|
||||
timestamp = 0;
|
||||
unread = 0;
|
||||
if (!empty) {
|
||||
for (ConversationItem i : messages) {
|
||||
addMessage(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void addMessage(ConversationItem message) {
|
||||
empty = empty && message == null;
|
||||
if (message != null) {
|
||||
if (message.getTime() > timestamp) timestamp = message.getTime();
|
||||
if (message instanceof ConversationItem.IncomingItem &&
|
||||
!((ConversationItem.IncomingItem) message).isRead())
|
||||
unread++;
|
||||
}
|
||||
this.timestamp = timestamp;
|
||||
this.unread = unread;
|
||||
}
|
||||
|
||||
public Contact getContact() {
|
||||
@@ -70,14 +52,22 @@ public class ContactListItem {
|
||||
}
|
||||
|
||||
boolean isEmpty() {
|
||||
return empty;
|
||||
return timestamp < 0;
|
||||
}
|
||||
|
||||
long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
void setTimestamp(long timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
int getUnreadCount() {
|
||||
return unread;
|
||||
}
|
||||
|
||||
void setUnreadCount(int unread) {
|
||||
this.unread = unread;
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,6 @@ import org.briarproject.api.conversation.ConversationIntroductionResponseItem;
|
||||
import org.briarproject.api.conversation.ConversationItem;
|
||||
import org.briarproject.api.conversation.ConversationItem.IncomingItem;
|
||||
import org.briarproject.api.conversation.ConversationManager;
|
||||
import org.briarproject.api.conversation.ConversationMessageItem;
|
||||
import org.briarproject.api.crypto.CryptoExecutor;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.NoSuchContactException;
|
||||
@@ -58,7 +57,6 @@ import org.briarproject.api.introduction.IntroductionRequest;
|
||||
import org.briarproject.api.introduction.IntroductionResponse;
|
||||
import org.briarproject.api.messaging.PrivateMessage;
|
||||
import org.briarproject.api.messaging.PrivateMessageFactory;
|
||||
import org.briarproject.api.messaging.PrivateMessageHeader;
|
||||
import org.briarproject.api.plugins.ConnectionRegistry;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
@@ -366,7 +364,7 @@ public class ConversationActivity extends BriarActivity
|
||||
SparseArray<IncomingItem> list = adapter.getIncomingMessages();
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
IncomingItem item = list.valueAt(i);
|
||||
if (!item.isRead()) unread.add((ConversationItem) item);
|
||||
if (!item.isRead()) unread.add(item);
|
||||
}
|
||||
if (unread.isEmpty()) return;
|
||||
if (LOG.isLoggable(INFO))
|
||||
@@ -381,7 +379,7 @@ public class ConversationActivity extends BriarActivity
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
for (ConversationItem item : unread)
|
||||
conversationManager.setReadFlag(item, true);
|
||||
conversationManager.setReadFlag(contactId, item, true);
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Marking read took " + duration + " ms");
|
||||
@@ -488,7 +486,7 @@ public class ConversationActivity extends BriarActivity
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
conversationManager.setReadFlag(item, true);
|
||||
conversationManager.setReadFlag(contactId, item, true);
|
||||
loadMessages();
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
|
||||
@@ -103,7 +103,7 @@ public class ForumSharingStatusActivity extends BriarActivity {
|
||||
.getLocalAuthor(c.getLocalAuthorId());
|
||||
ContactListItem item =
|
||||
new ContactListItem(c, localAuthor, false, null,
|
||||
null);
|
||||
-1, 0);
|
||||
contactItems.add(item);
|
||||
}
|
||||
} catch (DbException e) {
|
||||
@@ -141,7 +141,7 @@ public class ForumSharingStatusActivity extends BriarActivity {
|
||||
.getLocalAuthor(c.getLocalAuthorId());
|
||||
ContactListItem item =
|
||||
new ContactListItem(c, localAuthor, false, null,
|
||||
null);
|
||||
-1, 0);
|
||||
contactItems.add(item);
|
||||
}
|
||||
} catch (DbException e) {
|
||||
|
||||
@@ -16,8 +16,7 @@ public class SelectableContactListItem extends ContactListItem {
|
||||
public SelectableContactListItem(Contact contact, LocalAuthor localAuthor,
|
||||
GroupId groupId, boolean selected, boolean disabled) {
|
||||
|
||||
super(contact, localAuthor, false, groupId,
|
||||
Collections.<ConversationItem>emptyList());
|
||||
super(contact, localAuthor, false, groupId, -1, 0);
|
||||
|
||||
this.selected = selected;
|
||||
this.disabled = disabled;
|
||||
|
||||
@@ -162,14 +162,18 @@ public class ContactChooserFragment extends BaseFragment {
|
||||
ContactId id = c.getId();
|
||||
GroupId groupId =
|
||||
conversationManager.getConversationId(id);
|
||||
Collection<ConversationItem> messages =
|
||||
getMessages(id);
|
||||
boolean connected =
|
||||
connectionRegistry.isConnected(c.getId());
|
||||
LocalAuthor localAuthor = identityManager
|
||||
.getLocalAuthor(c.getLocalAuthorId());
|
||||
long timestamp = conversationManager.getTimestamp(id);
|
||||
long now1 = System.currentTimeMillis();
|
||||
int unread = conversationManager.getUnreadCount(id);
|
||||
long duration = System.currentTimeMillis() - now1;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Loading unread messages took " + duration + " ms");
|
||||
contacts.add(new ContactListItem(c, localAuthor,
|
||||
connected, groupId, messages));
|
||||
connected, groupId, timestamp, unread));
|
||||
}
|
||||
}
|
||||
displayContacts(localAuthorId, contacts);
|
||||
@@ -213,21 +217,4 @@ public class ContactChooserFragment extends BaseFragment {
|
||||
builder.setNegativeButton(android.R.string.cancel, null);
|
||||
builder.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* This needs to be called from the DbThread
|
||||
*/
|
||||
private Collection<ConversationItem> getMessages(ContactId id)
|
||||
throws DbException {
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
Collection<ConversationItem> messages =
|
||||
conversationManager.getMessages(id, false);
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Loading message headers took " + duration + " ms");
|
||||
|
||||
return messages;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user