mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 21:59:54 +01:00
Merge branch '309-client-layer-events-for-messaging' into 'master'
Client layer events for messaging Adds and uses a new `PrivateMessageReceivedEvent` and eliminate the need for an event for adding local messages. Both done in separate commits. This addresses part of #309 See merge request !208
This commit is contained in:
@@ -12,6 +12,7 @@ import android.widget.TextView;
|
|||||||
import org.briarproject.R;
|
import org.briarproject.R;
|
||||||
import org.briarproject.api.contact.ContactId;
|
import org.briarproject.api.contact.ContactId;
|
||||||
import org.briarproject.api.identity.Author;
|
import org.briarproject.api.identity.Author;
|
||||||
|
import org.briarproject.api.sync.GroupId;
|
||||||
import org.briarproject.util.StringUtils;
|
import org.briarproject.util.StringUtils;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -67,7 +68,7 @@ public abstract class BaseContactListAdapter<VH extends BaseContactListAdapter.B
|
|||||||
return contacts.get(position);
|
return contacts.get(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateItem(int position, ContactListItem item) {
|
void updateItem(int position, ContactListItem item) {
|
||||||
contacts.updateItemAt(position, item);
|
contacts.updateItemAt(position, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +76,7 @@ public abstract class BaseContactListAdapter<VH extends BaseContactListAdapter.B
|
|||||||
return contacts.indexOf(c);
|
return contacts.indexOf(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int findItemPosition(ContactId c) {
|
int findItemPosition(ContactId c) {
|
||||||
int count = getItemCount();
|
int count = getItemCount();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
ContactListItem item = getItem(i);
|
ContactListItem item = getItem(i);
|
||||||
@@ -84,6 +85,15 @@ public abstract class BaseContactListAdapter<VH extends BaseContactListAdapter.B
|
|||||||
return INVALID_POSITION; // Not found
|
return INVALID_POSITION; // Not found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int findItemPosition(GroupId g) {
|
||||||
|
int count = getItemCount();
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
ContactListItem item = getItem(i);
|
||||||
|
if (item.getGroupId().equals(g)) return i;
|
||||||
|
}
|
||||||
|
return INVALID_POSITION; // Not found
|
||||||
|
}
|
||||||
|
|
||||||
public void addAll(List<ContactListItem> contacts) {
|
public void addAll(List<ContactListItem> contacts) {
|
||||||
this.contacts.addAll(contacts);
|
this.contacts.addAll(contacts);
|
||||||
}
|
}
|
||||||
@@ -130,7 +140,7 @@ public abstract class BaseContactListAdapter<VH extends BaseContactListAdapter.B
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int compareByTime(ContactListItem c1, ContactListItem c2) {
|
int compareByTime(ContactListItem c1, ContactListItem c2) {
|
||||||
long time1 = c1.getTimestamp();
|
long time1 = c1.getTimestamp();
|
||||||
long time2 = c2.getTimestamp();
|
long time2 = c2.getTimestamp();
|
||||||
if (time1 < time2) return 1;
|
if (time1 < time2) return 1;
|
||||||
@@ -138,7 +148,7 @@ public abstract class BaseContactListAdapter<VH extends BaseContactListAdapter.B
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected class SortedListCallBacks
|
private class SortedListCallBacks
|
||||||
extends SortedList.Callback<ContactListItem> {
|
extends SortedList.Callback<ContactListItem> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -68,11 +68,11 @@ public class ContactListAdapter
|
|||||||
extends BaseContactListAdapter.BaseContactHolder {
|
extends BaseContactListAdapter.BaseContactHolder {
|
||||||
|
|
||||||
public final ImageView bulb;
|
public final ImageView bulb;
|
||||||
public final TextView unread;
|
final TextView unread;
|
||||||
public final TextView date;
|
public final TextView date;
|
||||||
public final TextView identity;
|
public final TextView identity;
|
||||||
|
|
||||||
public ContactHolder(View v) {
|
ContactHolder(View v) {
|
||||||
super(v);
|
super(v);
|
||||||
|
|
||||||
bulb = (ImageView) v.findViewById(R.id.bulbView);
|
bulb = (ImageView) v.findViewById(R.id.bulbView);
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import org.briarproject.api.event.Event;
|
|||||||
import org.briarproject.api.event.EventBus;
|
import org.briarproject.api.event.EventBus;
|
||||||
import org.briarproject.api.event.EventListener;
|
import org.briarproject.api.event.EventListener;
|
||||||
import org.briarproject.api.event.MessageStateChangedEvent;
|
import org.briarproject.api.event.MessageStateChangedEvent;
|
||||||
|
import org.briarproject.api.event.PrivateMessageReceivedEvent;
|
||||||
import org.briarproject.api.forum.ForumInvitationMessage;
|
import org.briarproject.api.forum.ForumInvitationMessage;
|
||||||
import org.briarproject.api.forum.ForumSharingManager;
|
import org.briarproject.api.forum.ForumSharingManager;
|
||||||
import org.briarproject.api.identity.IdentityManager;
|
import org.briarproject.api.identity.IdentityManager;
|
||||||
@@ -231,12 +232,16 @@ public class ContactListFragment extends BaseFragment implements EventListener {
|
|||||||
} else if (e instanceof ContactRemovedEvent) {
|
} else if (e instanceof ContactRemovedEvent) {
|
||||||
LOG.info("Contact removed");
|
LOG.info("Contact removed");
|
||||||
removeItem(((ContactRemovedEvent) e).getContactId());
|
removeItem(((ContactRemovedEvent) e).getContactId());
|
||||||
|
} else if (e instanceof PrivateMessageReceivedEvent) {
|
||||||
|
LOG.info("Message received, update contact");
|
||||||
|
PrivateMessageReceivedEvent p = (PrivateMessageReceivedEvent) e;
|
||||||
|
PrivateMessageHeader h = p.getMessageHeader();
|
||||||
|
updateItem(p.getGroupId(), ConversationItem.from(h));
|
||||||
} else if (e instanceof MessageStateChangedEvent) {
|
} else if (e instanceof MessageStateChangedEvent) {
|
||||||
MessageStateChangedEvent m = (MessageStateChangedEvent) e;
|
MessageStateChangedEvent m = (MessageStateChangedEvent) e;
|
||||||
ClientId c = m.getClientId();
|
ClientId c = m.getClientId();
|
||||||
if (m.getState() == DELIVERED &&
|
if (m.getState() == DELIVERED &&
|
||||||
(c.equals(messagingManager.getClientId()) ||
|
(c.equals(introductionManager.getClientId()) ||
|
||||||
c.equals(introductionManager.getClientId()) ||
|
|
||||||
c.equals(forumSharingManager.getClientId()))) {
|
c.equals(forumSharingManager.getClientId()))) {
|
||||||
LOG.info("Message added, reloading");
|
LOG.info("Message added, reloading");
|
||||||
reloadConversation(m.getMessage().getGroupId());
|
reloadConversation(m.getMessage().getGroupId());
|
||||||
@@ -278,6 +283,20 @@ public class ContactListFragment extends BaseFragment implements EventListener {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateItem(final GroupId g, final ConversationItem m) {
|
||||||
|
listener.runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
int position = adapter.findItemPosition(g);
|
||||||
|
ContactListItem item = adapter.getItem(position);
|
||||||
|
if (item != null) {
|
||||||
|
item.addMessage(m);
|
||||||
|
adapter.updateItem(position, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void removeItem(final ContactId c) {
|
private void removeItem(final ContactId c) {
|
||||||
listener.runOnUiThread(new Runnable() {
|
listener.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -35,13 +35,21 @@ public class ContactListItem {
|
|||||||
unread = 0;
|
unread = 0;
|
||||||
if (!empty) {
|
if (!empty) {
|
||||||
for (ConversationItem i : messages) {
|
for (ConversationItem i : messages) {
|
||||||
if (i.getTime() > timestamp) timestamp = i.getTime();
|
addMessage(i);
|
||||||
if (i instanceof IncomingItem && !((IncomingItem) i).isRead())
|
|
||||||
unread++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addMessage(ConversationItem message) {
|
||||||
|
empty = empty && message == null;
|
||||||
|
if (message != null) {
|
||||||
|
if (message.getTime() > timestamp) timestamp = message.getTime();
|
||||||
|
if (message instanceof IncomingItem &&
|
||||||
|
!((IncomingItem) message).isRead())
|
||||||
|
unread++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Contact getContact() {
|
public Contact getContact() {
|
||||||
return contact;
|
return contact;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,9 +47,9 @@ import org.briarproject.api.event.EventListener;
|
|||||||
import org.briarproject.api.event.ForumInvitationReceivedEvent;
|
import org.briarproject.api.event.ForumInvitationReceivedEvent;
|
||||||
import org.briarproject.api.event.IntroductionRequestReceivedEvent;
|
import org.briarproject.api.event.IntroductionRequestReceivedEvent;
|
||||||
import org.briarproject.api.event.IntroductionResponseReceivedEvent;
|
import org.briarproject.api.event.IntroductionResponseReceivedEvent;
|
||||||
import org.briarproject.api.event.MessageStateChangedEvent;
|
|
||||||
import org.briarproject.api.event.MessagesAckedEvent;
|
import org.briarproject.api.event.MessagesAckedEvent;
|
||||||
import org.briarproject.api.event.MessagesSentEvent;
|
import org.briarproject.api.event.MessagesSentEvent;
|
||||||
|
import org.briarproject.api.event.PrivateMessageReceivedEvent;
|
||||||
import org.briarproject.api.forum.ForumInvitationMessage;
|
import org.briarproject.api.forum.ForumInvitationMessage;
|
||||||
import org.briarproject.api.forum.ForumSharingManager;
|
import org.briarproject.api.forum.ForumSharingManager;
|
||||||
import org.briarproject.api.introduction.IntroductionManager;
|
import org.briarproject.api.introduction.IntroductionManager;
|
||||||
@@ -62,7 +62,6 @@ import org.briarproject.api.messaging.PrivateMessageFactory;
|
|||||||
import org.briarproject.api.messaging.PrivateMessageHeader;
|
import org.briarproject.api.messaging.PrivateMessageHeader;
|
||||||
import org.briarproject.api.plugins.ConnectionRegistry;
|
import org.briarproject.api.plugins.ConnectionRegistry;
|
||||||
import org.briarproject.api.sync.GroupId;
|
import org.briarproject.api.sync.GroupId;
|
||||||
import org.briarproject.api.sync.Message;
|
|
||||||
import org.briarproject.api.sync.MessageId;
|
import org.briarproject.api.sync.MessageId;
|
||||||
import org.briarproject.util.StringUtils;
|
import org.briarproject.util.StringUtils;
|
||||||
|
|
||||||
@@ -87,7 +86,6 @@ import static java.util.logging.Level.INFO;
|
|||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.android.contact.ConversationItem.IncomingItem;
|
import static org.briarproject.android.contact.ConversationItem.IncomingItem;
|
||||||
import static org.briarproject.android.contact.ConversationItem.OutgoingItem;
|
import static org.briarproject.android.contact.ConversationItem.OutgoingItem;
|
||||||
import static org.briarproject.api.sync.ValidationManager.State.DELIVERED;
|
|
||||||
|
|
||||||
public class ConversationActivity extends BriarActivity
|
public class ConversationActivity extends BriarActivity
|
||||||
implements EventListener, OnClickListener,
|
implements EventListener, OnClickListener,
|
||||||
@@ -97,14 +95,13 @@ public class ConversationActivity extends BriarActivity
|
|||||||
Logger.getLogger(ConversationActivity.class.getName());
|
Logger.getLogger(ConversationActivity.class.getName());
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
protected AndroidNotificationManager notificationManager;
|
AndroidNotificationManager notificationManager;
|
||||||
@Inject
|
@Inject
|
||||||
protected ConnectionRegistry connectionRegistry;
|
protected ConnectionRegistry connectionRegistry;
|
||||||
@Inject
|
@Inject
|
||||||
@CryptoExecutor
|
@CryptoExecutor
|
||||||
protected Executor cryptoExecutor;
|
protected Executor cryptoExecutor;
|
||||||
|
|
||||||
private Map<MessageId, byte[]> bodyCache = new HashMap<>();
|
|
||||||
private ConversationAdapter adapter;
|
private ConversationAdapter adapter;
|
||||||
private CircleImageView toolbarAvatar;
|
private CircleImageView toolbarAvatar;
|
||||||
private ImageView toolbarStatus;
|
private ImageView toolbarStatus;
|
||||||
@@ -121,7 +118,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
@Inject
|
@Inject
|
||||||
protected volatile EventBus eventBus;
|
protected volatile EventBus eventBus;
|
||||||
@Inject
|
@Inject
|
||||||
protected volatile PrivateMessageFactory privateMessageFactory;
|
volatile PrivateMessageFactory privateMessageFactory;
|
||||||
@Inject
|
@Inject
|
||||||
protected volatile IntroductionManager introductionManager;
|
protected volatile IntroductionManager introductionManager;
|
||||||
@Inject
|
@Inject
|
||||||
@@ -132,6 +129,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
private volatile String contactName = null;
|
private volatile String contactName = null;
|
||||||
private volatile byte[] contactIdenticonKey = null;
|
private volatile byte[] contactIdenticonKey = null;
|
||||||
private volatile boolean connected = false;
|
private volatile boolean connected = false;
|
||||||
|
private volatile Map<MessageId, byte[]> bodyCache = new HashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle state) {
|
public void onCreate(Bundle state) {
|
||||||
@@ -146,10 +144,13 @@ public class ConversationActivity extends BriarActivity
|
|||||||
|
|
||||||
// Custom Toolbar
|
// Custom Toolbar
|
||||||
Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
|
Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
|
||||||
toolbarAvatar = (CircleImageView) tb.findViewById(R.id.contactAvatar);
|
if (tb != null) {
|
||||||
toolbarStatus = (ImageView) tb.findViewById(R.id.contactStatus);
|
toolbarAvatar =
|
||||||
toolbarTitle = (TextView) tb.findViewById(R.id.contactName);
|
(CircleImageView) tb.findViewById(R.id.contactAvatar);
|
||||||
setSupportActionBar(tb);
|
toolbarStatus = (ImageView) tb.findViewById(R.id.contactStatus);
|
||||||
|
toolbarTitle = (TextView) tb.findViewById(R.id.contactName);
|
||||||
|
setSupportActionBar(tb);
|
||||||
|
}
|
||||||
ActionBar ab = getSupportActionBar();
|
ActionBar ab = getSupportActionBar();
|
||||||
if (ab != null) {
|
if (ab != null) {
|
||||||
ab.setDisplayShowHomeEnabled(true);
|
ab.setDisplayShowHomeEnabled(true);
|
||||||
@@ -170,8 +171,11 @@ public class ConversationActivity extends BriarActivity
|
|||||||
|
|
||||||
content = (EditText) findViewById(R.id.contentView);
|
content = (EditText) findViewById(R.id.contentView);
|
||||||
sendButton = (ImageButton) findViewById(R.id.sendButton);
|
sendButton = (ImageButton) findViewById(R.id.sendButton);
|
||||||
sendButton.setEnabled(false); // Enabled after loading the conversation
|
if (sendButton != null) {
|
||||||
sendButton.setOnClickListener(this);
|
// Enabled after loading the conversation
|
||||||
|
sendButton.setEnabled(false);
|
||||||
|
sendButton.setOnClickListener(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -235,7 +239,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBackPressed() {
|
public void onBackPressed() {
|
||||||
// FIXME disabled exit transition, because it doesn't work for some reason
|
// FIXME disabled exit transition, because it doesn't work for some reason #318
|
||||||
//supportFinishAfterTransition();
|
//supportFinishAfterTransition();
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
@@ -416,7 +420,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addIntroduction(final ConversationItem item) {
|
private void addConversationItem(final ConversationItem item) {
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -469,14 +473,14 @@ public class ConversationActivity extends BriarActivity
|
|||||||
LOG.info("Contact removed");
|
LOG.info("Contact removed");
|
||||||
finishOnUiThread();
|
finishOnUiThread();
|
||||||
}
|
}
|
||||||
} else if (e instanceof MessageStateChangedEvent) {
|
} else if (e instanceof PrivateMessageReceivedEvent) {
|
||||||
MessageStateChangedEvent m = (MessageStateChangedEvent) e;
|
PrivateMessageReceivedEvent p = (PrivateMessageReceivedEvent) e;
|
||||||
if (m.getState() == DELIVERED &&
|
if (p.getGroupId().equals(groupId)) {
|
||||||
m.getMessage().getGroupId().equals(groupId)) {
|
LOG.info("Message received, adding");
|
||||||
LOG.info("Message added, reloading");
|
PrivateMessageHeader h = p.getMessageHeader();
|
||||||
// Mark new incoming messages as read directly
|
addConversationItem(ConversationItem.from(h));
|
||||||
if (m.isLocal()) loadMessages();
|
loadMessageBody(h);
|
||||||
else markMessageReadIfNew(m.getMessage());
|
markMessageReadIfNew(h);
|
||||||
}
|
}
|
||||||
} else if (e instanceof MessagesSentEvent) {
|
} else if (e instanceof MessagesSentEvent) {
|
||||||
MessagesSentEvent m = (MessagesSentEvent) e;
|
MessagesSentEvent m = (MessagesSentEvent) e;
|
||||||
@@ -510,7 +514,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
if (event.getContactId().equals(contactId)) {
|
if (event.getContactId().equals(contactId)) {
|
||||||
IntroductionRequest ir = event.getIntroductionRequest();
|
IntroductionRequest ir = event.getIntroductionRequest();
|
||||||
ConversationItem item = new ConversationIntroductionInItem(ir);
|
ConversationItem item = new ConversationIntroductionInItem(ir);
|
||||||
addIntroduction(item);
|
addConversationItem(item);
|
||||||
}
|
}
|
||||||
} else if (e instanceof IntroductionResponseReceivedEvent) {
|
} else if (e instanceof IntroductionResponseReceivedEvent) {
|
||||||
IntroductionResponseReceivedEvent event =
|
IntroductionResponseReceivedEvent event =
|
||||||
@@ -519,7 +523,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
IntroductionResponse ir = event.getIntroductionResponse();
|
IntroductionResponse ir = event.getIntroductionResponse();
|
||||||
ConversationItem item =
|
ConversationItem item =
|
||||||
ConversationItem.from(this, contactName, ir);
|
ConversationItem.from(this, contactName, ir);
|
||||||
addIntroduction(item);
|
addConversationItem(item);
|
||||||
}
|
}
|
||||||
} else if (e instanceof ForumInvitationReceivedEvent) {
|
} else if (e instanceof ForumInvitationReceivedEvent) {
|
||||||
ForumInvitationReceivedEvent event =
|
ForumInvitationReceivedEvent event =
|
||||||
@@ -530,7 +534,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void markMessageReadIfNew(final Message m) {
|
private void markMessageReadIfNew(final PrivateMessageHeader h) {
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -538,23 +542,23 @@ public class ConversationActivity extends BriarActivity
|
|||||||
if (item != null) {
|
if (item != null) {
|
||||||
// Mark the message read if it's the newest message
|
// Mark the message read if it's the newest message
|
||||||
long lastMsgTime = item.getTime();
|
long lastMsgTime = item.getTime();
|
||||||
long newMsgTime = m.getTimestamp();
|
long newMsgTime = h.getTimestamp();
|
||||||
if (newMsgTime > lastMsgTime) markNewMessageRead(m);
|
if (newMsgTime > lastMsgTime) markNewMessageRead(h.getId());
|
||||||
else loadMessages();
|
else loadMessages();
|
||||||
} else {
|
} else {
|
||||||
// mark the message as read as well if it is the first one
|
// mark the message as read as well if it is the first one
|
||||||
markNewMessageRead(m);
|
markNewMessageRead(h.getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void markNewMessageRead(final Message m) {
|
private void markNewMessageRead(final MessageId m) {
|
||||||
runOnDbThread(new Runnable() {
|
runOnDbThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
messagingManager.setReadFlag(m.getId(), true);
|
messagingManager.setReadFlag(m, true);
|
||||||
loadMessages();
|
loadMessages();
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
if (LOG.isLoggable(WARNING))
|
if (LOG.isLoggable(WARNING))
|
||||||
@@ -605,8 +609,9 @@ public class ConversationActivity extends BriarActivity
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
storeMessage(privateMessageFactory.createPrivateMessage(
|
storeMessage(privateMessageFactory
|
||||||
groupId, timestamp, null, "text/plain", body));
|
.createPrivateMessage(groupId, timestamp, null,
|
||||||
|
"text/plain", body), body);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -614,7 +619,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void storeMessage(final PrivateMessage m) {
|
private void storeMessage(final PrivateMessage m, final byte[] body) {
|
||||||
runOnDbThread(new Runnable() {
|
runOnDbThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -624,6 +629,16 @@ public class ConversationActivity extends BriarActivity
|
|||||||
long duration = System.currentTimeMillis() - now;
|
long duration = System.currentTimeMillis() - now;
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Storing message took " + duration + " ms");
|
LOG.info("Storing message took " + duration + " ms");
|
||||||
|
|
||||||
|
PrivateMessageHeader h = new PrivateMessageHeader(
|
||||||
|
m.getMessage().getId(),
|
||||||
|
m.getMessage().getTimestamp(), m.getContentType(),
|
||||||
|
true, false, false, false);
|
||||||
|
ConversationMessageItem item =
|
||||||
|
(ConversationMessageItem) ConversationItem.from(h);
|
||||||
|
item.setBody(body);
|
||||||
|
bodyCache.put(m.getMessage().getId(), body);
|
||||||
|
addConversationItem(item);
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
if (LOG.isLoggable(WARNING))
|
if (LOG.isLoggable(WARNING))
|
||||||
LOG.log(WARNING, e.toString(), e);
|
LOG.log(WARNING, e.toString(), e);
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package org.briarproject.api.event;
|
||||||
|
|
||||||
|
import org.briarproject.api.messaging.PrivateMessageHeader;
|
||||||
|
import org.briarproject.api.sync.GroupId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event that is broadcast when a new private message was received.
|
||||||
|
*/
|
||||||
|
public class PrivateMessageReceivedEvent extends Event {
|
||||||
|
|
||||||
|
private final PrivateMessageHeader messageHeader;
|
||||||
|
private final GroupId groupId;
|
||||||
|
|
||||||
|
public PrivateMessageReceivedEvent(PrivateMessageHeader messageHeader,
|
||||||
|
GroupId groupId) {
|
||||||
|
this.messageHeader = messageHeader;
|
||||||
|
this.groupId = groupId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PrivateMessageHeader getMessageHeader() {
|
||||||
|
return messageHeader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GroupId getGroupId() {
|
||||||
|
return groupId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,17 +10,21 @@ import org.briarproject.api.contact.ContactManager.AddContactHook;
|
|||||||
import org.briarproject.api.contact.ContactManager.RemoveContactHook;
|
import org.briarproject.api.contact.ContactManager.RemoveContactHook;
|
||||||
import org.briarproject.api.data.BdfDictionary;
|
import org.briarproject.api.data.BdfDictionary;
|
||||||
import org.briarproject.api.data.BdfList;
|
import org.briarproject.api.data.BdfList;
|
||||||
|
import org.briarproject.api.data.MetadataParser;
|
||||||
import org.briarproject.api.db.DatabaseComponent;
|
import org.briarproject.api.db.DatabaseComponent;
|
||||||
import org.briarproject.api.db.DbException;
|
import org.briarproject.api.db.DbException;
|
||||||
import org.briarproject.api.db.Transaction;
|
import org.briarproject.api.db.Transaction;
|
||||||
|
import org.briarproject.api.event.PrivateMessageReceivedEvent;
|
||||||
import org.briarproject.api.messaging.MessagingManager;
|
import org.briarproject.api.messaging.MessagingManager;
|
||||||
import org.briarproject.api.messaging.PrivateMessage;
|
import org.briarproject.api.messaging.PrivateMessage;
|
||||||
import org.briarproject.api.messaging.PrivateMessageHeader;
|
import org.briarproject.api.messaging.PrivateMessageHeader;
|
||||||
import org.briarproject.api.sync.ClientId;
|
import org.briarproject.api.sync.ClientId;
|
||||||
import org.briarproject.api.sync.Group;
|
import org.briarproject.api.sync.Group;
|
||||||
import org.briarproject.api.sync.GroupId;
|
import org.briarproject.api.sync.GroupId;
|
||||||
|
import org.briarproject.api.sync.Message;
|
||||||
import org.briarproject.api.sync.MessageId;
|
import org.briarproject.api.sync.MessageId;
|
||||||
import org.briarproject.api.sync.MessageStatus;
|
import org.briarproject.api.sync.MessageStatus;
|
||||||
|
import org.briarproject.clients.BdfIncomingMessageHook;
|
||||||
import org.briarproject.util.StringUtils;
|
import org.briarproject.util.StringUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -29,22 +33,23 @@ import java.util.Map;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
class MessagingManagerImpl implements MessagingManager, Client, AddContactHook,
|
class MessagingManagerImpl extends BdfIncomingMessageHook
|
||||||
RemoveContactHook {
|
implements MessagingManager, Client, AddContactHook, RemoveContactHook {
|
||||||
|
|
||||||
static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString(
|
static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString(
|
||||||
"6bcdc006c0910b0f44e40644c3b31f1a"
|
"6bcdc006c0910b0f44e40644c3b31f1a"
|
||||||
+ "8bf9a6d6021d40d219c86b731b903070"));
|
+ "8bf9a6d6021d40d219c86b731b903070"));
|
||||||
|
|
||||||
private final DatabaseComponent db;
|
private final DatabaseComponent db;
|
||||||
private final ClientHelper clientHelper;
|
|
||||||
private final PrivateGroupFactory privateGroupFactory;
|
private final PrivateGroupFactory privateGroupFactory;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
MessagingManagerImpl(DatabaseComponent db, ClientHelper clientHelper,
|
MessagingManagerImpl(DatabaseComponent db, ClientHelper clientHelper,
|
||||||
|
MetadataParser metadataParser,
|
||||||
PrivateGroupFactory privateGroupFactory) {
|
PrivateGroupFactory privateGroupFactory) {
|
||||||
|
super(clientHelper, metadataParser);
|
||||||
|
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.clientHelper = clientHelper;
|
|
||||||
this.privateGroupFactory = privateGroupFactory;
|
this.privateGroupFactory = privateGroupFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,6 +92,22 @@ class MessagingManagerImpl implements MessagingManager, Client, AddContactHook,
|
|||||||
return CLIENT_ID;
|
return CLIENT_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void incomingMessage(Transaction txn, Message m, BdfList body,
|
||||||
|
BdfDictionary meta) throws DbException, FormatException {
|
||||||
|
|
||||||
|
GroupId groupId = m.getGroupId();
|
||||||
|
long timestamp = meta.getLong("timestamp");
|
||||||
|
String contentType = meta.getString("contentType");
|
||||||
|
boolean local = meta.getBoolean("local");
|
||||||
|
boolean read = meta.getBoolean("read");
|
||||||
|
PrivateMessageHeader header = new PrivateMessageHeader(
|
||||||
|
m.getId(), timestamp, contentType, local, read, false, false);
|
||||||
|
PrivateMessageReceivedEvent event = new PrivateMessageReceivedEvent(
|
||||||
|
header, groupId);
|
||||||
|
txn.attach(event);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addLocalMessage(PrivateMessage m) throws DbException {
|
public void addLocalMessage(PrivateMessage m) throws DbException {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -45,11 +45,13 @@ public class MessagingModule {
|
|||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
MessagingManager getMessagingManager(LifecycleManager lifecycleManager,
|
MessagingManager getMessagingManager(LifecycleManager lifecycleManager,
|
||||||
ContactManager contactManager,
|
ContactManager contactManager, ValidationManager validationManager,
|
||||||
MessagingManagerImpl messagingManager) {
|
MessagingManagerImpl messagingManager) {
|
||||||
lifecycleManager.registerClient(messagingManager);
|
lifecycleManager.registerClient(messagingManager);
|
||||||
contactManager.registerAddContactHook(messagingManager);
|
contactManager.registerAddContactHook(messagingManager);
|
||||||
contactManager.registerRemoveContactHook(messagingManager);
|
contactManager.registerRemoveContactHook(messagingManager);
|
||||||
|
validationManager
|
||||||
|
.registerIncomingMessageHook(CLIENT_ID, messagingManager);
|
||||||
return messagingManager;
|
return messagingManager;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user