mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 12:19:54 +01:00
Separate the sync layer from its clients. #112
This commit is contained in:
@@ -18,7 +18,6 @@ import org.briarproject.android.BriarActivity;
|
||||
import org.briarproject.android.util.LayoutUtils;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.forum.Forum;
|
||||
import org.briarproject.api.forum.ForumFactory;
|
||||
import org.briarproject.api.forum.ForumManager;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
@@ -38,7 +37,7 @@ import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.android.util.CommonLayoutParams.MATCH_MATCH;
|
||||
import static org.briarproject.android.util.CommonLayoutParams.WRAP_WRAP;
|
||||
import static org.briarproject.api.sync.MessagingConstants.MAX_GROUP_NAME_LENGTH;
|
||||
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH;
|
||||
|
||||
public class CreateForumActivity extends BriarActivity
|
||||
implements OnEditorActionListener, OnClickListener {
|
||||
@@ -52,7 +51,6 @@ implements OnEditorActionListener, OnClickListener {
|
||||
private TextView feedback = null;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject private volatile ForumFactory forumFactory;
|
||||
@Inject private volatile ForumManager forumManager;
|
||||
|
||||
@Override
|
||||
@@ -115,8 +113,9 @@ implements OnEditorActionListener, OnClickListener {
|
||||
}
|
||||
|
||||
private boolean validateName() {
|
||||
int length = StringUtils.toUtf8(nameEntry.getText().toString()).length;
|
||||
if (length > MAX_GROUP_NAME_LENGTH) {
|
||||
String name = nameEntry.getText().toString();
|
||||
int length = StringUtils.toUtf8(name).length;
|
||||
if (length > MAX_FORUM_NAME_LENGTH) {
|
||||
feedback.setText(R.string.name_too_long);
|
||||
return false;
|
||||
}
|
||||
@@ -138,8 +137,8 @@ implements OnEditorActionListener, OnClickListener {
|
||||
runOnDbThread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Forum f = forumFactory.createForum(name);
|
||||
long now = System.currentTimeMillis();
|
||||
Forum f = forumManager.createForum(name);
|
||||
forumManager.addForum(f);
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.briarproject.api.db.NoSuchSubscriptionException;
|
||||
import org.briarproject.api.event.Event;
|
||||
import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.event.EventListener;
|
||||
import org.briarproject.api.event.MessageAddedEvent;
|
||||
import org.briarproject.api.event.MessageValidatedEvent;
|
||||
import org.briarproject.api.event.SubscriptionRemovedEvent;
|
||||
import org.briarproject.api.forum.Forum;
|
||||
import org.briarproject.api.forum.ForumManager;
|
||||
@@ -56,7 +56,7 @@ import static org.briarproject.android.util.CommonLayoutParams.MATCH_WRAP;
|
||||
import static org.briarproject.android.util.CommonLayoutParams.MATCH_WRAP_1;
|
||||
|
||||
public class ForumActivity extends BriarActivity implements EventListener,
|
||||
OnClickListener, OnItemClickListener {
|
||||
OnClickListener, OnItemClickListener {
|
||||
|
||||
private static final int REQUEST_READ = 2;
|
||||
private static final Logger LOG =
|
||||
@@ -143,6 +143,8 @@ OnClickListener, OnItemClickListener {
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
eventBus.addListener(this);
|
||||
notificationManager.blockNotification(groupId);
|
||||
notificationManager.clearForumPostNotification(groupId);
|
||||
loadForum();
|
||||
loadHeaders();
|
||||
}
|
||||
@@ -276,11 +278,11 @@ OnClickListener, OnItemClickListener {
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
eventBus.removeListener(this);
|
||||
notificationManager.unblockNotification(groupId);
|
||||
if (isFinishing()) markPostsRead();
|
||||
}
|
||||
|
||||
private void markPostsRead() {
|
||||
notificationManager.clearForumPostNotification(groupId);
|
||||
List<MessageId> unread = new ArrayList<MessageId>();
|
||||
int count = adapter.getCount();
|
||||
for (int i = 0; i < count; i++) {
|
||||
@@ -312,8 +314,9 @@ OnClickListener, OnItemClickListener {
|
||||
}
|
||||
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof MessageAddedEvent) {
|
||||
if (((MessageAddedEvent) e).getGroupId().equals(groupId)) {
|
||||
if (e instanceof MessageValidatedEvent) {
|
||||
MessageValidatedEvent m = (MessageValidatedEvent) e;
|
||||
if (m.isValid() && m.getMessage().getGroupId().equals(groupId)) {
|
||||
LOG.info("Message added, reloading");
|
||||
loadHeaders();
|
||||
}
|
||||
|
||||
@@ -28,19 +28,18 @@ import org.briarproject.api.db.NoSuchSubscriptionException;
|
||||
import org.briarproject.api.event.Event;
|
||||
import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.event.EventListener;
|
||||
import org.briarproject.api.event.MessageAddedEvent;
|
||||
import org.briarproject.api.event.MessageValidatedEvent;
|
||||
import org.briarproject.api.event.RemoteSubscriptionsUpdatedEvent;
|
||||
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.ClientId;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -60,16 +59,13 @@ import static org.briarproject.android.util.CommonLayoutParams.MATCH_WRAP;
|
||||
import static org.briarproject.android.util.CommonLayoutParams.MATCH_WRAP_1;
|
||||
|
||||
public class ForumListActivity extends BriarActivity
|
||||
implements EventListener, OnClickListener, OnItemClickListener,
|
||||
OnCreateContextMenuListener {
|
||||
implements EventListener, OnClickListener, OnItemClickListener,
|
||||
OnCreateContextMenuListener {
|
||||
|
||||
private static final int MENU_ITEM_UNSUBSCRIBE = 1;
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ForumListActivity.class.getName());
|
||||
|
||||
private final Map<GroupId, GroupId> groupIds =
|
||||
new ConcurrentHashMap<GroupId, GroupId>();
|
||||
|
||||
private TextView empty = null;
|
||||
private ForumListAdapter adapter = null;
|
||||
private ListView list = null;
|
||||
@@ -179,7 +175,6 @@ OnCreateContextMenuListener {
|
||||
private void clearHeaders() {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
groupIds.clear();
|
||||
empty.setVisibility(GONE);
|
||||
list.setVisibility(GONE);
|
||||
available.setVisibility(GONE);
|
||||
@@ -194,12 +189,10 @@ OnCreateContextMenuListener {
|
||||
final Collection<ForumPostHeader> headers) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
GroupId id = f.getId();
|
||||
groupIds.put(id, id);
|
||||
list.setVisibility(VISIBLE);
|
||||
loading.setVisibility(GONE);
|
||||
// Remove the old item, if any
|
||||
ForumListItem item = findForum(id);
|
||||
ForumListItem item = findForum(f.getId());
|
||||
if (item != null) adapter.remove(item);
|
||||
// Add a new item
|
||||
adapter.add(new ForumListItem(f, headers));
|
||||
@@ -255,11 +248,12 @@ OnCreateContextMenuListener {
|
||||
}
|
||||
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof MessageAddedEvent) {
|
||||
GroupId g = ((MessageAddedEvent) e).getGroupId();
|
||||
if (groupIds.containsKey(g)) {
|
||||
if (e instanceof MessageValidatedEvent) {
|
||||
MessageValidatedEvent m = (MessageValidatedEvent) e;
|
||||
ClientId c = m.getClientId();
|
||||
if (m.isValid() && c.equals(forumManager.getClientId())) {
|
||||
LOG.info("Message added, reloading");
|
||||
loadHeaders(g);
|
||||
loadHeaders(m.getMessage().getGroupId());
|
||||
}
|
||||
} else if (e instanceof RemoteSubscriptionsUpdatedEvent) {
|
||||
LOG.info("Remote subscriptions changed, reloading");
|
||||
@@ -269,7 +263,7 @@ OnCreateContextMenuListener {
|
||||
loadHeaders();
|
||||
} else if (e instanceof SubscriptionRemovedEvent) {
|
||||
Group g = ((SubscriptionRemovedEvent) e).getGroup();
|
||||
if (groupIds.containsKey(g.getId())) {
|
||||
if (g.getClientId().equals(forumManager.getClientId())) {
|
||||
LOG.info("Group removed, reloading");
|
||||
loadHeaders();
|
||||
}
|
||||
@@ -303,7 +297,6 @@ OnCreateContextMenuListener {
|
||||
public void run() {
|
||||
ForumListItem item = findForum(g);
|
||||
if (item != null) {
|
||||
groupIds.remove(g);
|
||||
adapter.remove(item);
|
||||
adapter.notifyDataSetChanged();
|
||||
if (adapter.isEmpty()) {
|
||||
|
||||
@@ -30,12 +30,12 @@ import org.briarproject.api.crypto.PrivateKey;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.forum.Forum;
|
||||
import org.briarproject.api.forum.ForumManager;
|
||||
import org.briarproject.api.forum.ForumPost;
|
||||
import org.briarproject.api.forum.ForumPostFactory;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.Message;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
@@ -267,35 +267,35 @@ implements OnItemSelectedListener, OnClickListener {
|
||||
// Don't use an earlier timestamp than the newest post
|
||||
long timestamp = System.currentTimeMillis();
|
||||
timestamp = Math.max(timestamp, minTimestamp);
|
||||
Message m;
|
||||
ForumPost p;
|
||||
try {
|
||||
if (localAuthor == null) {
|
||||
m = forumPostFactory.createAnonymousPost(parentId,
|
||||
forum, "text/plain", timestamp, body);
|
||||
p = forumPostFactory.createAnonymousPost(groupId,
|
||||
timestamp, parentId, "text/plain", body);
|
||||
} else {
|
||||
KeyParser keyParser = crypto.getSignatureKeyParser();
|
||||
byte[] b = localAuthor.getPrivateKey();
|
||||
PrivateKey authorKey = keyParser.parsePrivateKey(b);
|
||||
m = forumPostFactory.createPseudonymousPost(parentId,
|
||||
forum, localAuthor, authorKey, "text/plain",
|
||||
timestamp, body);
|
||||
p = forumPostFactory.createPseudonymousPost(groupId,
|
||||
timestamp, parentId, localAuthor, "text/plain",
|
||||
body, authorKey);
|
||||
}
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
storePost(m);
|
||||
storePost(p);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void storePost(final Message m) {
|
||||
private void storePost(final ForumPost p) {
|
||||
runOnDbThread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
forumManager.addLocalPost(m);
|
||||
forumManager.addLocalPost(p);
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Storing message took " + duration + " ms");
|
||||
|
||||
Reference in New Issue
Block a user