Facade for forum post headers. #172

This commit is contained in:
akwizgran
2015-12-17 16:27:55 +00:00
parent f899bc0c38
commit 351e1bbbe6
11 changed files with 148 additions and 79 deletions

View File

@@ -28,9 +28,9 @@ import org.briarproject.api.event.MessageAddedEvent;
import org.briarproject.api.event.SubscriptionRemovedEvent;
import org.briarproject.api.forum.Forum;
import org.briarproject.api.forum.ForumManager;
import org.briarproject.api.forum.ForumPostHeader;
import org.briarproject.api.identity.Author;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageHeader;
import org.briarproject.api.sync.MessageId;
import java.util.ArrayList;
@@ -180,8 +180,8 @@ OnClickListener, OnItemClickListener {
public void run() {
try {
long now = System.currentTimeMillis();
Collection<MessageHeader> headers =
forumManager.getMessageHeaders(groupId);
Collection<ForumPostHeader> headers =
forumManager.getPostHeaders(groupId);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
@@ -196,7 +196,7 @@ OnClickListener, OnItemClickListener {
});
}
private void displayHeaders(final Collection<MessageHeader> headers) {
private void displayHeaders(final Collection<ForumPostHeader> headers) {
runOnUiThread(new Runnable() {
public void run() {
loading.setVisibility(GONE);
@@ -207,10 +207,10 @@ OnClickListener, OnItemClickListener {
} else {
empty.setVisibility(GONE);
list.setVisibility(VISIBLE);
for (MessageHeader h : headers) {
for (ForumPostHeader h : headers) {
ForumItem item = new ForumItem(h);
byte[] body = bodyCache.get(h.getId());
if (body == null) loadMessageBody(h);
if (body == null) loadPostBody(h);
else item.setBody(body);
adapter.add(item);
}
@@ -223,16 +223,16 @@ OnClickListener, OnItemClickListener {
});
}
private void loadMessageBody(final MessageHeader h) {
private void loadPostBody(final ForumPostHeader h) {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
byte[] body = forumManager.getMessageBody(h.getId());
byte[] body = forumManager.getPostBody(h.getId());
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Loading message took " + duration + " ms");
displayMessage(h.getId(), body);
displayPost(h.getId(), body);
} catch (NoSuchMessageException e) {
// The item will be removed when we get the event
} catch (DbException e) {
@@ -243,7 +243,7 @@ OnClickListener, OnItemClickListener {
});
}
private void displayMessage(final MessageId m, final byte[] body) {
private void displayPost(final MessageId m, final byte[] body) {
runOnUiThread(new Runnable() {
public void run() {
bodyCache.put(m, body);
@@ -268,7 +268,7 @@ OnClickListener, OnItemClickListener {
if (request == REQUEST_READ && result == RESULT_PREV_NEXT) {
int position = data.getIntExtra("briar.POSITION", -1);
if (position >= 0 && position < adapter.getCount())
displayMessage(position);
displayPost(position);
}
}
@@ -276,24 +276,24 @@ OnClickListener, OnItemClickListener {
public void onPause() {
super.onPause();
eventBus.removeListener(this);
if (isFinishing()) markMessagesRead();
if (isFinishing()) markPostsRead();
}
private void markMessagesRead() {
private void markPostsRead() {
notificationManager.clearForumPostNotification(groupId);
List<MessageId> unread = new ArrayList<MessageId>();
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
MessageHeader h = adapter.getItem(i).getHeader();
ForumPostHeader h = adapter.getItem(i).getHeader();
if (!h.isRead()) unread.add(h.getId());
}
if (unread.isEmpty()) return;
if (LOG.isLoggable(INFO))
LOG.info("Marking " + unread.size() + " messages read");
markMessagesRead(Collections.unmodifiableList(unread));
LOG.info("Marking " + unread.size() + " posts read");
markPostsRead(Collections.unmodifiableList(unread));
}
private void markMessagesRead(final Collection<MessageId> unread) {
private void markPostsRead(final Collection<MessageId> unread) {
runOnDbThread(new Runnable() {
public void run() {
try {
@@ -331,7 +331,7 @@ OnClickListener, OnItemClickListener {
Intent i = new Intent(this, WriteForumPostActivity.class);
i.putExtra("briar.GROUP_ID", groupId.getBytes());
i.putExtra("briar.FORUM_NAME", forum.getName());
i.putExtra("briar.MIN_TIMESTAMP", getMinTimestampForNewMessage());
i.putExtra("briar.MIN_TIMESTAMP", getMinTimestampForNewPost());
startActivity(i);
} else if (view == shareButton) {
Intent i = new Intent(this, ShareForumActivity.class);
@@ -341,8 +341,8 @@ OnClickListener, OnItemClickListener {
}
}
private long getMinTimestampForNewMessage() {
// Don't use an earlier timestamp than the newest message
private long getMinTimestampForNewPost() {
// Don't use an earlier timestamp than the newest post
long timestamp = 0;
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
@@ -354,21 +354,21 @@ OnClickListener, OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
displayMessage(position);
displayPost(position);
}
private void displayMessage(int position) {
MessageHeader item = adapter.getItem(position).getHeader();
private void displayPost(int position) {
ForumPostHeader header = adapter.getItem(position).getHeader();
Intent i = new Intent(this, ReadForumPostActivity.class);
i.putExtra("briar.GROUP_ID", groupId.getBytes());
i.putExtra("briar.FORUM_NAME", forum.getName());
i.putExtra("briar.MESSAGE_ID", item.getId().getBytes());
Author author = item.getAuthor();
i.putExtra("briar.MESSAGE_ID", header.getId().getBytes());
Author author = header.getAuthor();
if (author != null) i.putExtra("briar.AUTHOR_NAME", author.getName());
i.putExtra("briar.AUTHOR_STATUS", item.getAuthorStatus().name());
i.putExtra("briar.CONTENT_TYPE", item.getContentType());
i.putExtra("briar.TIMESTAMP", item.getTimestamp());
i.putExtra("briar.MIN_TIMESTAMP", getMinTimestampForNewMessage());
i.putExtra("briar.AUTHOR_STATUS", header.getAuthorStatus().name());
i.putExtra("briar.CONTENT_TYPE", header.getContentType());
i.putExtra("briar.TIMESTAMP", header.getTimestamp());
i.putExtra("briar.MIN_TIMESTAMP", getMinTimestampForNewPost());
i.putExtra("briar.POSITION", position);
startActivityForResult(i, REQUEST_READ);
}

View File

@@ -13,8 +13,8 @@ import android.widget.TextView;
import org.briarproject.R;
import org.briarproject.android.util.AuthorView;
import org.briarproject.android.util.LayoutUtils;
import org.briarproject.api.forum.ForumPostHeader;
import org.briarproject.api.identity.Author;
import org.briarproject.api.sync.MessageHeader;
import org.briarproject.util.StringUtils;
import java.util.ArrayList;
@@ -38,7 +38,7 @@ class ForumAdapter extends ArrayAdapter<ForumItem> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ForumItem item = getItem(position);
MessageHeader header = item.getHeader();
ForumPostHeader header = item.getHeader();
Context ctx = getContext();
Resources res = ctx.getResources();

View File

@@ -1,19 +1,19 @@
package org.briarproject.android.forum;
import org.briarproject.api.sync.MessageHeader;
import org.briarproject.api.forum.ForumPostHeader;
// This class is not thread-safe
class ForumItem {
private final MessageHeader header;
private final ForumPostHeader header;
private byte[] body;
ForumItem(MessageHeader header) {
ForumItem(ForumPostHeader header) {
this.header = header;
body = null;
}
MessageHeader getHeader() {
ForumPostHeader getHeader() {
return header;
}

View File

@@ -34,9 +34,9 @@ import org.briarproject.api.event.SubscriptionAddedEvent;
import org.briarproject.api.event.SubscriptionRemovedEvent;
import org.briarproject.api.forum.Forum;
import org.briarproject.api.forum.ForumManager;
import org.briarproject.api.forum.ForumPostHeader;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageHeader;
import java.util.Collection;
import java.util.Map;
@@ -156,8 +156,8 @@ OnCreateContextMenuListener {
long now = System.currentTimeMillis();
for (Forum f : forumManager.getForums()) {
try {
Collection<MessageHeader> headers =
forumManager.getMessageHeaders(f.getId());
Collection<ForumPostHeader> headers =
forumManager.getPostHeaders(f.getId());
displayHeaders(f, headers);
} catch (NoSuchSubscriptionException e) {
// Continue
@@ -191,7 +191,7 @@ OnCreateContextMenuListener {
}
private void displayHeaders(final Forum f,
final Collection<MessageHeader> headers) {
final Collection<ForumPostHeader> headers) {
runOnUiThread(new Runnable() {
public void run() {
GroupId id = f.getId();
@@ -282,8 +282,8 @@ OnCreateContextMenuListener {
try {
long now = System.currentTimeMillis();
Forum f = forumManager.getForum(g);
Collection<MessageHeader> headers =
forumManager.getMessageHeaders(g);
Collection<ForumPostHeader> headers =
forumManager.getPostHeaders(g);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Partial load took " + duration + " ms");

View File

@@ -1,7 +1,7 @@
package org.briarproject.android.forum;
import org.briarproject.api.forum.Forum;
import org.briarproject.api.sync.MessageHeader;
import org.briarproject.api.forum.ForumPostHeader;
import java.util.Collection;
@@ -12,17 +12,17 @@ class ForumListItem {
private final long timestamp;
private final int unread;
ForumListItem(Forum forum, Collection<MessageHeader> headers) {
ForumListItem(Forum forum, Collection<ForumPostHeader> headers) {
this.forum = forum;
empty = headers.isEmpty();
if (empty) {
timestamp = 0;
unread = 0;
} else {
MessageHeader newest = null;
ForumPostHeader newest = null;
long timestamp = -1;
int unread = 0;
for (MessageHeader h : headers) {
for (ForumPostHeader h : headers) {
if (h.getTimestamp() > timestamp) {
timestamp = h.getTimestamp();
newest = h;

View File

@@ -120,7 +120,7 @@ implements OnClickListener {
content = new TextView(this);
content.setPadding(pad, 0, pad, pad);
message.addView(content);
loadMessageBody();
loadPostBody();
}
scrollView.addView(message);
layout.addView(scrollView);
@@ -161,10 +161,10 @@ implements OnClickListener {
@Override
public void onPause() {
super.onPause();
if (isFinishing()) markMessageRead();
if (isFinishing()) markPostRead();
}
private void markMessageRead() {
private void markPostRead() {
runOnDbThread(new Runnable() {
public void run() {
try {
@@ -181,16 +181,16 @@ implements OnClickListener {
});
}
private void loadMessageBody() {
private void loadPostBody() {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
byte[] body = forumManager.getMessageBody(messageId);
byte[] body = forumManager.getPostBody(messageId);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Loading message took " + duration + " ms");
displayMessageBody(StringUtils.fromUtf8(body));
LOG.info("Loading post took " + duration + " ms");
displayPostBody(StringUtils.fromUtf8(body));
} catch (NoSuchMessageException e) {
finishOnUiThread();
} catch (DbException e) {
@@ -201,7 +201,7 @@ implements OnClickListener {
});
}
private void displayMessageBody(final String body) {
private void displayPostBody(final String body) {
runOnUiThread(new Runnable() {
public void run() {
content.setText(body);

View File

@@ -78,8 +78,8 @@ implements OnItemSelectedListener, OnClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile IdentityManager identityManager;
@Inject private volatile ForumManager forumManager;
@Inject private volatile CryptoComponent crypto;
@Inject private volatile ForumPostFactory forumPostFactory;
@Inject private volatile CryptoComponent crypto;
private volatile MessageId parentId = null;
private volatile long minTimestamp = -1;
private volatile LocalAuthor localAuthor = null;
@@ -160,10 +160,10 @@ implements OnItemSelectedListener, OnClickListener {
@Override
public void onResume() {
super.onResume();
loadAuthorsAndGroup();
loadAuthorsAndForum();
}
private void loadAuthorsAndGroup() {
private void loadAuthorsAndForum() {
runOnDbThread(new Runnable() {
public void run() {
try {
@@ -174,7 +174,7 @@ implements OnItemSelectedListener, OnClickListener {
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
displayAuthorsAndGroup(localAuthors);
displayAuthorsAndForum(localAuthors);
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
@@ -183,7 +183,7 @@ implements OnItemSelectedListener, OnClickListener {
});
}
private void displayAuthorsAndGroup(
private void displayAuthorsAndForum(
final Collection<LocalAuthor> localAuthors) {
runOnUiThread(new Runnable() {
public void run() {
@@ -226,7 +226,7 @@ implements OnItemSelectedListener, OnClickListener {
byte[] b = data.getByteArrayExtra("briar.LOCAL_AUTHOR_ID");
if (b == null) throw new IllegalStateException();
localAuthorId = new AuthorId(b);
loadAuthorsAndGroup();
loadAuthorsAndForum();
}
}
@@ -254,14 +254,14 @@ implements OnItemSelectedListener, OnClickListener {
public void onClick(View view) {
if (forum == null) throw new IllegalStateException();
String message = content.getText().toString();
if (message.equals("")) return;
createMessage(StringUtils.toUtf8(message));
String body = content.getText().toString();
if (body.equals("")) return;
createPost(StringUtils.toUtf8(body));
Toast.makeText(this, R.string.post_sent_toast, LENGTH_LONG).show();
finish();
}
private void createMessage(final byte[] body) {
private void createPost(final byte[] body) {
cryptoExecutor.execute(new Runnable() {
public void run() {
// Don't use an earlier timestamp than the newest post
@@ -285,17 +285,17 @@ implements OnItemSelectedListener, OnClickListener {
} catch (IOException e) {
throw new RuntimeException(e);
}
storeMessage(m);
storePost(m);
}
});
}
private void storeMessage(final Message m) {
private void storePost(final Message m) {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
forumManager.addLocalMessage(m);
forumManager.addLocalPost(m);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Storing message took " + duration + " ms");

View File

@@ -5,7 +5,6 @@ import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DbException;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageHeader;
import org.briarproject.api.sync.MessageId;
import java.util.Collection;
@@ -18,8 +17,8 @@ public interface ForumManager {
*/
boolean addForum(Forum f) throws DbException;
/** Stores a local message. */
void addLocalMessage(Message m) throws DbException;
/** Stores a local forum post. */
void addLocalPost(Message m) throws DbException;
/** Returns all forums to which the user could subscribe. */
Collection<Forum> getAvailableForums() throws DbException;
@@ -30,12 +29,11 @@ public interface ForumManager {
/** Returns all forums to which the user subscribes. */
Collection<Forum> getForums() throws DbException;
/** Returns the body of the message with the given ID. */
byte[] getMessageBody(MessageId m) throws DbException;
/** Returns the body of the forum post with the given ID. */
byte[] getPostBody(MessageId m) throws DbException;
/** Returns the headers of all messages in the given forum. */
Collection<MessageHeader> getMessageHeaders(GroupId g)
throws DbException;
/** Returns the headers of all posts in the given forum. */
Collection<ForumPostHeader> getPostHeaders(GroupId g) throws DbException;
/** Returns all contacts who subscribe to the given forum. */
Collection<Contact> getSubscribers(GroupId g) throws DbException;
@@ -49,7 +47,7 @@ public interface ForumManager {
*/
void removeForum(Forum f) throws DbException;
/** Marks a message as read or unread. */
/** Marks a forum post as read or unread. */
void setReadFlag(MessageId m, boolean read) throws DbException;
/**

View File

@@ -0,0 +1,19 @@
package org.briarproject.api.forum;
import org.briarproject.api.identity.Author;
import org.briarproject.api.sync.MessageId;
public interface ForumPostHeader {
MessageId getId();
Author getAuthor();
Author.Status getAuthorStatus();
String getContentType();
long getTimestamp();
boolean isRead();
}

View File

@@ -8,6 +8,7 @@ import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.forum.Forum;
import org.briarproject.api.forum.ForumManager;
import org.briarproject.api.forum.ForumPostHeader;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
@@ -35,7 +36,7 @@ class ForumManagerImpl implements ForumManager {
}
@Override
public void addLocalMessage(Message m) throws DbException {
public void addLocalPost(Message m) throws DbException {
db.addLocalMessage(m);
}
@@ -61,14 +62,19 @@ class ForumManagerImpl implements ForumManager {
}
@Override
public byte[] getMessageBody(MessageId m) throws DbException {
public byte[] getPostBody(MessageId m) throws DbException {
return db.getMessageBody(m);
}
@Override
public Collection<MessageHeader> getMessageHeaders(GroupId g)
public Collection<ForumPostHeader> getPostHeaders(GroupId g)
throws DbException {
return db.getMessageHeaders(g);
Collection<MessageHeader> headers = db.getMessageHeaders(g);
List<ForumPostHeader> postHeaders =
new ArrayList<ForumPostHeader>(headers.size());
for (MessageHeader m : headers)
postHeaders.add(new ForumPostHeaderImpl(m));
return Collections.unmodifiableList(postHeaders);
}
@Override

View File

@@ -0,0 +1,46 @@
package org.briarproject.forum;
import org.briarproject.api.forum.ForumPostHeader;
import org.briarproject.api.identity.Author;
import org.briarproject.api.sync.MessageHeader;
import org.briarproject.api.sync.MessageId;
// Temporary facade during sync protocol refactoring
class ForumPostHeaderImpl implements ForumPostHeader {
private final MessageHeader messageHeader;
ForumPostHeaderImpl(MessageHeader messageHeader) {
this.messageHeader = messageHeader;
}
@Override
public MessageId getId() {
return messageHeader.getId();
}
@Override
public Author getAuthor() {
return messageHeader.getAuthor();
}
@Override
public Author.Status getAuthorStatus() {
return messageHeader.getAuthorStatus();
}
@Override
public String getContentType() {
return messageHeader.getContentType();
}
@Override
public long getTimestamp() {
return messageHeader.getTimestamp();
}
@Override
public boolean isRead() {
return messageHeader.isRead();
}
}