Merge branch '535-formatexception-when-loading-contact-list-after-receiving-invitation' into 'master'

Use Client Layer Events in ContactListFragment

This prevents trying to access the same group metadata in different groups.
Also, the conversation does not need to be reloaded once introduction messages arrive.

Closes #535

See merge request !254
This commit is contained in:
akwizgran
2016-08-01 10:21:31 +00:00
5 changed files with 42 additions and 22 deletions

View File

@@ -33,7 +33,9 @@ import org.briarproject.api.event.ContactStatusChangedEvent;
import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.MessageStateChangedEvent;
import org.briarproject.api.event.ForumInvitationReceivedEvent;
import org.briarproject.api.event.IntroductionRequestReceivedEvent;
import org.briarproject.api.event.IntroductionResponseReceivedEvent;
import org.briarproject.api.event.PrivateMessageReceivedEvent;
import org.briarproject.api.forum.ForumInvitationMessage;
import org.briarproject.api.forum.ForumSharingManager;
@@ -41,10 +43,11 @@ 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;
@@ -58,7 +61,6 @@ import static android.support.v4.app.ActivityOptionsCompat.makeSceneTransitionAn
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.android.BriarActivity.GROUP_ID;
import static org.briarproject.api.sync.ValidationManager.State.DELIVERED;
public class ContactListFragment extends BaseFragment implements EventListener {
@@ -68,7 +70,7 @@ public class ContactListFragment extends BaseFragment implements EventListener {
Logger.getLogger(ContactListFragment.class.getName());
@Inject
protected ConnectionRegistry connectionRegistry;
ConnectionRegistry connectionRegistry;
@Inject
protected EventBus eventBus;
@@ -245,6 +247,7 @@ public class ContactListFragment extends BaseFragment implements EventListener {
}
} else if (e instanceof ContactStatusChangedEvent) {
LOG.info("Contact Status changed, reloading");
// TODO We can update the contact state without needing to reload
loadContacts();
} else if (e instanceof ContactConnectedEvent) {
setConnected(((ContactConnectedEvent) e).getContactId(), true);
@@ -258,26 +261,31 @@ public class ContactListFragment extends BaseFragment implements EventListener {
PrivateMessageReceivedEvent p = (PrivateMessageReceivedEvent) e;
PrivateMessageHeader h = p.getMessageHeader();
updateItem(p.getGroupId(), ConversationItem.from(h));
} 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()))) {
LOG.info("Message added, reloading");
reloadConversation(m.getMessage().getGroupId());
}
} else if (e instanceof IntroductionRequestReceivedEvent) {
LOG.info("Introduction Request received, update contact");
IntroductionRequestReceivedEvent m =
(IntroductionRequestReceivedEvent) e;
IntroductionRequest ir = m.getIntroductionRequest();
updateItem(m.getContactId(), ConversationItem.from(ir));
} else if (e instanceof IntroductionResponseReceivedEvent) {
LOG.info("Introduction Response received, update contact");
IntroductionResponseReceivedEvent m =
(IntroductionResponseReceivedEvent) e;
IntroductionResponse ir = m.getIntroductionResponse();
updateItem(m.getContactId(), ConversationItem.from(ir));
} else if (e instanceof ForumInvitationReceivedEvent) {
LOG.info("Forum Invitation received, reloading conversation...");
ForumInvitationReceivedEvent m = (ForumInvitationReceivedEvent) e;
reloadConversation(m.getContactId());
}
}
private void reloadConversation(final GroupId g) {
private void reloadConversation(final ContactId c) {
listener.runOnDbThread(new Runnable() {
@Override
public void run() {
try {
ContactId c = messagingManager.getContactId(g);
Collection<ConversationItem> messages =
getMessages(c);
Collection<ConversationItem> messages = getMessages(c);
updateItem(c, messages);
} catch (NoSuchContactException e) {
LOG.info("Contact removed");
@@ -304,6 +312,20 @@ public class ContactListFragment extends BaseFragment implements EventListener {
});
}
private void updateItem(final ContactId c, final ConversationItem m) {
listener.runOnUiThread(new Runnable() {
@Override
public void run() {
int position = adapter.findItemPosition(c);
ContactListItem item = adapter.getItem(position);
if (item != null) {
item.addMessage(m);
adapter.updateItem(position, item);
}
}
});
}
private void updateItem(final GroupId g, final ConversationItem m) {
listener.runOnUiThread(new Runnable() {
@Override

View File

@@ -2,7 +2,6 @@ package org.briarproject.api.event;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.forum.Forum;
import org.briarproject.api.introduction.IntroductionRequest;
public class ForumInvitationReceivedEvent extends InvitationReceivedEvent {

View File

@@ -1,13 +1,12 @@
package org.briarproject.api.event;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.forum.Forum;
public abstract class InvitationReceivedEvent extends Event {
private final ContactId contactId;
public InvitationReceivedEvent(ContactId contactId) {
InvitationReceivedEvent(ContactId contactId) {
this.contactId = contactId;
}

View File

@@ -22,7 +22,7 @@ public interface SharingMessage {
private final GroupId groupId;
private final SessionId sessionId;
public BaseMessage(GroupId groupId, SessionId sessionId) {
BaseMessage(GroupId groupId, SessionId sessionId) {
this.groupId = groupId;
this.sessionId = sessionId;

View File

@@ -23,7 +23,7 @@ import static org.briarproject.api.sharing.SharingConstants.TASK_UNSHARE_SHAREAB
import static org.briarproject.api.sharing.SharingMessage.BaseMessage;
import static org.briarproject.api.sharing.SharingMessage.SimpleMessage;
public class InviteeEngine<IS extends InviteeSessionState, IR extends InvitationReceivedEvent>
class InviteeEngine<IS extends InviteeSessionState, IR extends InvitationReceivedEvent>
implements ProtocolEngine<InviteeSessionState.Action, IS, BaseMessage> {
private static final Logger LOG =