Separate the sync layer from its clients. #112

This commit is contained in:
akwizgran
2015-12-21 14:36:24 +00:00
parent f5f572139a
commit 5355951466
117 changed files with 3160 additions and 3465 deletions

View File

@@ -1,87 +1,48 @@
package org.briarproject.android;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.briarproject.api.android.AndroidExecutor;
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import org.briarproject.api.android.AndroidExecutor;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import javax.inject.Inject;
class AndroidExecutorImpl implements AndroidExecutor {
private static final int SHUTDOWN = 0, RUN = 1;
private static final Logger LOG =
Logger.getLogger(AndroidExecutorImpl.class.getName());
private final Runnable loop;
private final AtomicBoolean started = new AtomicBoolean(false);
private final CountDownLatch startLatch = new CountDownLatch(1);
private volatile Handler handler = null;
private final Handler handler;
@Inject
AndroidExecutorImpl() {
loop = new Runnable() {
public void run() {
Looper.prepare();
handler = new FutureTaskHandler();
startLatch.countDown();
Looper.loop();
}
};
AndroidExecutorImpl(Application app) {
Context ctx = app.getApplicationContext();
handler = new FutureTaskHandler(ctx.getMainLooper());
}
private void startIfNecessary() {
if (started.getAndSet(true)) return;
new Thread(loop, "AndroidExecutor").start();
try {
startLatch.await();
} catch (InterruptedException e) {
LOG.warning("Interrupted while starting executor thread");
Thread.currentThread().interrupt();
}
}
public <V> V call(Callable<V> c) throws InterruptedException,
ExecutionException {
startIfNecessary();
public <V> Future<V> submit(Callable<V> c) {
Future<V> f = new FutureTask<V>(c);
Message m = Message.obtain(handler, RUN, f);
handler.sendMessage(m);
return f.get();
handler.sendMessage(Message.obtain(handler, 0, f));
return f;
}
public void shutdown() {
if (handler != null) {
Message m = Message.obtain(handler, SHUTDOWN);
handler.sendMessage(m);
}
public void execute(Runnable r) {
handler.post(r);
}
private static class FutureTaskHandler extends Handler {
private FutureTaskHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message m) {
switch(m.what) {
case SHUTDOWN:
Looper.myLooper().quit();
break;
case RUN:
((FutureTask<?>) m.obj).run();
break;
default:
throw new IllegalArgumentException();
}
((FutureTask<?>) m.obj).run();
}
}
}

View File

@@ -14,23 +14,25 @@ import org.briarproject.android.contact.ConversationActivity;
import org.briarproject.android.forum.ForumActivity;
import org.briarproject.android.forum.ForumListActivity;
import org.briarproject.api.Settings;
import org.briarproject.api.android.AndroidExecutor;
import org.briarproject.api.android.AndroidNotificationManager;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DatabaseExecutor;
import org.briarproject.api.db.DbException;
import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.MessageValidatedEvent;
import org.briarproject.api.event.SettingsUpdatedEvent;
import org.briarproject.api.forum.ForumManager;
import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.GroupId;
import org.briarproject.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import javax.inject.Inject;
@@ -44,7 +46,7 @@ import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;
import static java.util.logging.Level.WARNING;
class AndroidNotificationManagerImpl implements AndroidNotificationManager,
EventListener {
EventListener {
private static final int PRIVATE_MESSAGE_NOTIFICATION_ID = 3;
private static final int FORUM_POST_NOTIFICATION_ID = 4;
@@ -59,27 +61,33 @@ EventListener {
private final DatabaseComponent db;
private final Executor dbExecutor;
private final EventBus eventBus;
private final MessagingManager messagingManager;
private final ForumManager forumManager;
private final AndroidExecutor androidExecutor;
private final Context appContext;
private final Lock lock = new ReentrantLock();
// The following are locking: lock
private final Map<ContactId, Integer> contactCounts =
new HashMap<ContactId, Integer>();
// The following must only be accessed on the main UI thread
private final Map<GroupId, Integer> contactCounts =
new HashMap<GroupId, Integer>();
private final Map<GroupId, Integer> forumCounts =
new HashMap<GroupId, Integer>();
private int contactTotal = 0, forumTotal = 0;
private int nextRequestId = 0;
private ContactId activeContact;
private GroupId visibleGroup = null;
private volatile Settings settings = new Settings();
@Inject
public AndroidNotificationManagerImpl(DatabaseComponent db,
@DatabaseExecutor Executor dbExecutor, EventBus eventBus,
Application app) {
MessagingManager messagingManager, ForumManager forumManager,
AndroidExecutor androidExecutor, Application app) {
this.db = db;
this.dbExecutor = dbExecutor;
this.eventBus = eventBus;
this.messagingManager = messagingManager;
this.forumManager = forumManager;
this.androidExecutor = androidExecutor;
appContext = app.getApplicationContext();
}
@@ -104,62 +112,71 @@ EventListener {
public boolean stop() {
eventBus.removeListener(this);
clearNotifications();
return true;
}
public void eventOccurred(Event e) {
if (e instanceof SettingsUpdatedEvent) loadSettings();
private void clearNotifications() {
androidExecutor.execute(new Runnable() {
public void run() {
clearPrivateMessageNotification();
clearForumPostNotification();
}
});
}
public void showPrivateMessageNotification(ContactId c) {
lock.lock();
try {
// check first if user has this conversation open at the moment
if (activeContact == null || !activeContact.equals(c)) {
Integer count = contactCounts.get(c);
if (count == null) contactCounts.put(c, 1);
else contactCounts.put(c, count + 1);
private void clearPrivateMessageNotification() {
Object o = appContext.getSystemService(NOTIFICATION_SERVICE);
NotificationManager nm = (NotificationManager) o;
nm.cancel(PRIVATE_MESSAGE_NOTIFICATION_ID);
}
private void clearForumPostNotification() {
Object o = appContext.getSystemService(NOTIFICATION_SERVICE);
NotificationManager nm = (NotificationManager) o;
nm.cancel(FORUM_POST_NOTIFICATION_ID);
}
public void eventOccurred(Event e) {
if (e instanceof SettingsUpdatedEvent) {
loadSettings();
} else if (e instanceof MessageValidatedEvent) {
MessageValidatedEvent m = (MessageValidatedEvent) e;
if (m.isValid() && !m.isLocal()) {
ClientId c = m.getClientId();
if (c.equals(messagingManager.getClientId()))
showPrivateMessageNotification(m.getMessage().getGroupId());
else if (c.equals(forumManager.getClientId()))
showForumPostNotification(m.getMessage().getGroupId());
}
}
}
public void showPrivateMessageNotification(final GroupId g) {
androidExecutor.execute(new Runnable() {
public void run() {
Integer count = contactCounts.get(g);
if (count == null) contactCounts.put(g, 1);
else contactCounts.put(g, count + 1);
contactTotal++;
if (!g.equals(visibleGroup))
updatePrivateMessageNotification();
}
});
}
public void clearPrivateMessageNotification(final GroupId g) {
androidExecutor.execute(new Runnable() {
public void run() {
Integer count = contactCounts.remove(g);
if (count == null) return; // Already cleared
contactTotal -= count;
// FIXME: If the notification isn't showing, this may show it
updatePrivateMessageNotification();
}
} finally {
lock.unlock();
}
});
}
public void clearPrivateMessageNotification(ContactId c) {
lock.lock();
try {
Integer count = contactCounts.remove(c);
if (count == null) return; // Already cleared
contactTotal -= count;
updatePrivateMessageNotification();
} finally {
lock.unlock();
}
}
public void blockPrivateMessageNotification(ContactId c) {
lock.lock();
try {
activeContact = c;
} finally {
lock.unlock();
}
}
public void unblockPrivateMessageNotification(ContactId c) {
lock.lock();
try {
if (activeContact != null && activeContact.equals(c)) {
activeContact = null;
}
} finally {
lock.unlock();
}
}
// Locking: lock
private void updatePrivateMessageNotification() {
if (contactTotal == 0) {
clearPrivateMessageNotification();
@@ -180,9 +197,10 @@ EventListener {
b.setAutoCancel(true);
if (contactCounts.size() == 1) {
Intent i = new Intent(appContext, ConversationActivity.class);
ContactId c = contactCounts.keySet().iterator().next();
i.putExtra("briar.CONTACT_ID", c.getInt());
i.setData(Uri.parse(CONTACT_URI + "/" + c.getInt()));
GroupId g = contactCounts.keySet().iterator().next();
i.putExtra("briar.GROUP_ID", g.getBytes());
String idHex = StringUtils.toHexString(g.getBytes());
i.setData(Uri.parse(CONTACT_URI + "/" + idHex));
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder t = TaskStackBuilder.create(appContext);
t.addParentStack(ConversationActivity.class);
@@ -202,13 +220,6 @@ EventListener {
}
}
// Locking: lock
private void clearPrivateMessageNotification() {
Object o = appContext.getSystemService(NOTIFICATION_SERVICE);
NotificationManager nm = (NotificationManager) o;
nm.cancel(PRIVATE_MESSAGE_NOTIFICATION_ID);
}
private int getDefaults() {
int defaults = DEFAULT_LIGHTS;
boolean sound = settings.getBoolean("notifySound", true);
@@ -220,32 +231,31 @@ EventListener {
return defaults;
}
public void showForumPostNotification(GroupId g) {
lock.lock();
try {
Integer count = forumCounts.get(g);
if (count == null) forumCounts.put(g, 1);
else forumCounts.put(g, count + 1);
forumTotal++;
updateForumPostNotification();
} finally {
lock.unlock();
}
public void showForumPostNotification(final GroupId g) {
androidExecutor.execute(new Runnable() {
public void run() {
Integer count = forumCounts.get(g);
if (count == null) forumCounts.put(g, 1);
else forumCounts.put(g, count + 1);
forumTotal++;
if (!g.equals(visibleGroup))
updateForumPostNotification();
}
});
}
public void clearForumPostNotification(GroupId g) {
lock.lock();
try {
Integer count = forumCounts.remove(g);
if (count == null) return; // Already cleared
forumTotal -= count;
updateForumPostNotification();
} finally {
lock.unlock();
}
public void clearForumPostNotification(final GroupId g) {
androidExecutor.execute(new Runnable() {
public void run() {
Integer count = forumCounts.remove(g);
if (count == null) return; // Already cleared
forumTotal -= count;
// FIXME: If the notification isn't showing, this may show it
updateForumPostNotification();
}
});
}
// Locking: lock
private void updateForumPostNotification() {
if (forumTotal == 0) {
clearForumPostNotification();
@@ -288,23 +298,19 @@ EventListener {
}
}
// Locking: lock
private void clearForumPostNotification() {
Object o = appContext.getSystemService(NOTIFICATION_SERVICE);
NotificationManager nm = (NotificationManager) o;
nm.cancel(FORUM_POST_NOTIFICATION_ID);
public void blockNotification(final GroupId g) {
androidExecutor.execute(new Runnable() {
public void run() {
visibleGroup = g;
}
});
}
public void clearNotifications() {
lock.lock();
try {
contactCounts.clear();
forumCounts.clear();
contactTotal = forumTotal = 0;
clearPrivateMessageNotification();
clearForumPostNotification();
} finally {
lock.unlock();
}
public void unblockNotification(final GroupId g) {
androidExecutor.execute(new Runnable() {
public void run() {
if (g.equals(visibleGroup)) visibleGroup = null;
}
});
}
}

View File

@@ -128,6 +128,7 @@ public abstract class BaseActivity extends AppCompatActivity
return scopedObjects;
}
// FIXME: Factor out prefs code so it can be used by SplashScreenActivity
private SharedPreferences getSharedPrefs() {
return getSharedPreferences(PREFS_DB, MODE_PRIVATE);
}

View File

@@ -11,22 +11,11 @@ import android.support.v4.app.NotificationCompat;
import org.briarproject.R;
import org.briarproject.api.android.AndroidExecutor;
import org.briarproject.api.android.AndroidNotificationManager;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.db.DatabaseConfig;
import org.briarproject.api.db.DatabaseExecutor;
import org.briarproject.api.db.DbException;
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.lifecycle.LifecycleManager;
import org.briarproject.api.lifecycle.LifecycleManager.StartResult;
import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.sync.GroupId;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
@@ -34,6 +23,7 @@ import javax.inject.Inject;
import roboguice.service.RoboService;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;
@@ -41,7 +31,7 @@ import static java.util.logging.Level.WARNING;
import static org.briarproject.api.lifecycle.LifecycleManager.StartResult.ALREADY_RUNNING;
import static org.briarproject.api.lifecycle.LifecycleManager.StartResult.SUCCESS;
public class BriarService extends RoboService implements EventListener {
public class BriarService extends RoboService {
private static final int ONGOING_NOTIFICATION_ID = 1;
private static final int FAILURE_NOTIFICATION_ID = 2;
@@ -53,14 +43,10 @@ public class BriarService extends RoboService implements EventListener {
private final Binder binder = new BriarBinder();
@Inject private DatabaseConfig databaseConfig;
@Inject private AndroidNotificationManager notificationManager;
// Fields that are accessed from background threads must be volatile
@Inject private volatile LifecycleManager lifecycleManager;
@Inject private volatile AndroidExecutor androidExecutor;
@Inject @DatabaseExecutor private volatile Executor dbExecutor;
@Inject private volatile MessagingManager messagingManager;
@Inject private volatile EventBus eventBus;
private volatile boolean started = false;
@Override
@@ -95,7 +81,6 @@ public class BriarService extends RoboService implements EventListener {
public void run() {
StartResult result = lifecycleManager.startServices();
if (result == SUCCESS) {
eventBus.addListener(BriarService.this);
started = true;
} else if (result == ALREADY_RUNNING) {
LOG.info("Already running");
@@ -110,25 +95,34 @@ public class BriarService extends RoboService implements EventListener {
}.start();
}
private void showStartupFailureNotification(StartResult result) {
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
b.setSmallIcon(android.R.drawable.stat_notify_error);
b.setContentTitle(getText(R.string.startup_failed_notification_title));
b.setContentText(getText(R.string.startup_failed_notification_text));
Intent i = new Intent(this, StartupFailureActivity.class);
i.setFlags(FLAG_ACTIVITY_NEW_TASK);
i.putExtra("briar.START_RESULT", result);
i.putExtra("briar.FAILURE_NOTIFICATION_ID", FAILURE_NOTIFICATION_ID);
b.setContentIntent(PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT));
Object o = getSystemService(NOTIFICATION_SERVICE);
NotificationManager nm = (NotificationManager) o;
nm.notify(FAILURE_NOTIFICATION_ID, b.build());
// Bring the dashboard to the front to clear all other activities
i = new Intent(this, DashboardActivity.class);
i.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("briar.STARTUP_FAILED", true);
startActivity(i);
private void showStartupFailureNotification(final StartResult result) {
androidExecutor.execute(new Runnable() {
public void run() {
NotificationCompat.Builder b =
new NotificationCompat.Builder(BriarService.this);
b.setSmallIcon(android.R.drawable.stat_notify_error);
b.setContentTitle(getText(
R.string.startup_failed_notification_title));
b.setContentText(getText(
R.string.startup_failed_notification_text));
Intent i = new Intent(BriarService.this,
StartupFailureActivity.class);
i.setFlags(FLAG_ACTIVITY_NEW_TASK);
i.putExtra("briar.START_RESULT", result);
i.putExtra("briar.FAILURE_NOTIFICATION_ID",
FAILURE_NOTIFICATION_ID);
b.setContentIntent(PendingIntent.getActivity(BriarService.this,
0, i, FLAG_UPDATE_CURRENT));
Object o = getSystemService(NOTIFICATION_SERVICE);
NotificationManager nm = (NotificationManager) o;
nm.notify(FAILURE_NOTIFICATION_ID, b.build());
// Bring the dashboard to the front to clear the back stack
i = new Intent(BriarService.this, DashboardActivity.class);
i.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("briar.STARTUP_FAILED", true);
startActivity(i);
}
});
}
@Override
@@ -146,16 +140,11 @@ public class BriarService extends RoboService implements EventListener {
super.onDestroy();
LOG.info("Destroyed");
stopForeground(true);
notificationManager.clearNotifications();
// Stop the services in a background thread
new Thread() {
@Override
public void run() {
if (started) {
eventBus.removeListener(BriarService.this);
lifecycleManager.stopServices();
}
androidExecutor.shutdown();
if (started) lifecycleManager.stopServices();
}
}.start();
}
@@ -167,39 +156,6 @@ public class BriarService extends RoboService implements EventListener {
// FIXME: Work out what to do about it
}
public void eventOccurred(Event e) {
if (e instanceof MessageAddedEvent) {
MessageAddedEvent m = (MessageAddedEvent) e;
GroupId g = m.getGroupId();
ContactId c = m.getContactId();
if (c != null) showMessageNotification(g, c);
}
}
private void showMessageNotification(final GroupId g, final ContactId c) {
dbExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
if (g.equals(messagingManager.getConversationId(c)))
notificationManager.showPrivateMessageNotification(c);
else notificationManager.showForumPostNotification(g);
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch (InterruptedException e) {
LOG.info("Interruped while waiting for database");
Thread.currentThread().interrupt();
}
}
});
}
/** Waits for the database to be opened before returning. */
public void waitForDatabase() throws InterruptedException {
lifecycleManager.waitForDatabase();
}
/** Waits for all services to start before returning. */
public void waitForStartup() throws InterruptedException {
lifecycleManager.waitForStartup();

View File

@@ -74,12 +74,12 @@ public class CrashReportActivity extends AppCompatActivity implements OnClickLis
private static final Logger LOG =
Logger.getLogger(CrashReportActivity.class.getName());
private final AndroidExecutor androidExecutor = new AndroidExecutorImpl();
private final AndroidExecutor androidExecutor =
new AndroidExecutorImpl(getApplication());
private ScrollView scroll = null;
private ListLoadingProgressBar progress = null;
private LinearLayout status = null;
private ImageButton share = null;
private File temp = null;
private volatile String stack = null;
@@ -120,7 +120,7 @@ public class CrashReportActivity extends AppCompatActivity implements OnClickLis
Resources res = getResources();
int background = res.getColor(R.color.button_bar_background);
footer.setBackgroundColor(background);
share = new ImageButton(this);
ImageButton share = new ImageButton(this);
share.setBackgroundResource(0);
share.setImageResource(R.drawable.social_share);
share.setOnClickListener(this);
@@ -323,11 +323,11 @@ public class CrashReportActivity extends AppCompatActivity implements OnClickLis
// Is Bluetooth available?
BluetoothAdapter bt = null;
try {
bt = androidExecutor.call(new Callable<BluetoothAdapter>() {
bt = androidExecutor.submit(new Callable<BluetoothAdapter>() {
public BluetoothAdapter call() throws Exception {
return BluetoothAdapter.getDefaultAdapter();
}
});
}).get();
} catch (InterruptedException e) {
LOG.warning("Interrupted while getting BluetoothAdapter");
Thread.currentThread().interrupt();

View File

@@ -16,8 +16,8 @@ import com.google.inject.Injector;
import org.briarproject.R;
import org.briarproject.android.util.LayoutUtils;
import org.briarproject.api.db.DatabaseConfig;
import org.briarproject.util.FileUtils;
import java.io.File;
import java.util.logging.Logger;
import roboguice.RoboGuice;
@@ -86,8 +86,9 @@ public class SplashScreenActivity extends RoboSplashActivity {
if (hex != null && databaseConfig.databaseExists()) {
startActivity(new Intent(this, DashboardActivity.class));
} else {
prefs.edit().clear().commit();
delete(databaseConfig.getDatabaseDirectory());
prefs.edit().clear().apply();
FileUtils.deleteFileOrDir(
databaseConfig.getDatabaseDirectory());
startActivity(new Intent(this, SetupActivity.class));
}
}
@@ -106,17 +107,11 @@ public class SplashScreenActivity extends RoboSplashActivity {
}
}
private void delete(File f) {
if (f.isFile()) f.delete();
else if (f.isDirectory()) for (File child : f.listFiles()) delete(child);
}
private void setPreferencesDefaults() {
new Thread() {
@Override
public void run() {
PreferenceManager
.setDefaultValues(SplashScreenActivity.this,
PreferenceManager.setDefaultValues(SplashScreenActivity.this,
R.xml.panic_preferences, false);
}
}.start();

View File

@@ -326,11 +326,11 @@ public class TestingActivity extends BriarActivity implements OnClickListener {
// Is Bluetooth available?
BluetoothAdapter bt = null;
try {
bt = androidExecutor.call(new Callable<BluetoothAdapter>() {
bt = androidExecutor.submit(new Callable<BluetoothAdapter>() {
public BluetoothAdapter call() throws Exception {
return BluetoothAdapter.getDefaultAdapter();
}
});
}).get();
} catch (InterruptedException e) {
LOG.warning("Interrupted while getting BluetoothAdapter");
Thread.currentThread().interrupt();

View File

@@ -3,7 +3,6 @@ package org.briarproject.android.contact;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.util.SortedList;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
@@ -23,10 +22,11 @@ import org.briarproject.api.event.ContactRemovedEvent;
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.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;
@@ -36,6 +36,7 @@ import java.util.logging.Logger;
import javax.inject.Inject;
import static android.support.v7.util.SortedList.INVALID_POSITION;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
@@ -105,18 +106,16 @@ public class ContactListActivity extends BriarActivity
for (Contact c : contactManager.getContacts()) {
try {
ContactId id = c.getId();
GroupId conversation =
GroupId groupId =
messagingManager.getConversationId(id);
Collection<PrivateMessageHeader> headers =
messagingManager.getMessageHeaders(id);
boolean connected =
connectionRegistry.isConnected(c.getId());
contacts.add(new ContactListItem(c, connected,
conversation,
headers));
groupId, headers));
} catch (NoSuchContactException e) {
// Continue
LOG.info("Contact removed");
}
}
displayContacts(contacts);
@@ -141,7 +140,7 @@ public class ContactListActivity extends BriarActivity
// sorting criteria and cause duplicates
for (ContactListItem contact : contacts) {
int position = adapter.findItemPosition(contact);
if (position == SortedList.INVALID_POSITION) {
if (position == INVALID_POSITION) {
adapter.add(contact);
} else {
adapter.updateItem(position, contact);
@@ -169,19 +168,22 @@ public class ContactListActivity extends BriarActivity
} else if (e instanceof ContactRemovedEvent) {
LOG.info("Contact removed");
removeItem(((ContactRemovedEvent) e).getContactId());
} else if (e instanceof MessageAddedEvent) {
LOG.info("Message added, reloading");
ContactId source = ((MessageAddedEvent) e).getContactId();
if (source == null) loadContacts();
else reloadContact(source);
} else if (e instanceof MessageValidatedEvent) {
MessageValidatedEvent m = (MessageValidatedEvent) e;
ClientId c = m.getClientId();
if (m.isValid() && c.equals(messagingManager.getClientId())) {
LOG.info("Message added, reloading");
reloadConversation(m.getMessage().getGroupId());
}
}
}
private void reloadContact(final ContactId c) {
private void reloadConversation(final GroupId g) {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
ContactId c = messagingManager.getContactId(g);
Collection<PrivateMessageHeader> headers =
messagingManager.getMessageHeaders(c);
long duration = System.currentTimeMillis() - now;
@@ -189,7 +191,7 @@ public class ContactListActivity extends BriarActivity
LOG.info("Partial load took " + duration + " ms");
updateItem(c, headers);
} catch (NoSuchContactException e) {
removeItem(c);
LOG.info("Contact removed");
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);

View File

@@ -14,11 +14,12 @@ import android.widget.TextView;
import org.briarproject.R;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.sync.GroupId;
import java.util.List;
import static android.support.v7.util.SortedList.INVALID_POSITION;
public class ContactListAdapter
extends RecyclerView.Adapter<ContactListAdapter.ContactHolder> {
@@ -130,17 +131,9 @@ public class ContactListAdapter
ui.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContactId contactId = item.getContact().getId();
String contactName = item.getContact().getAuthor().getName();
GroupId groupId = item.getConversationId();
AuthorId localAuthorId = item.getContact().getLocalAuthorId();
GroupId groupId = item.getGroupId();
Intent i = new Intent(ctx, ConversationActivity.class);
i.putExtra("briar.CONTACT_ID", contactId.getInt());
i.putExtra("briar.CONTACT_NAME", contactName);
i.putExtra("briar.GROUP_ID", groupId.getBytes());
i.putExtra("briar.LOCAL_AUTHOR_ID", localAuthorId.getBytes());
ctx.startActivity(i);
}
});
@@ -156,8 +149,7 @@ public class ContactListAdapter
}
public ContactListItem getItem(int position) {
if (position == SortedList.INVALID_POSITION ||
contacts.size() <= position) {
if (position == INVALID_POSITION || contacts.size() <= position) {
return null; // Not found
}
return contacts.get(position);
@@ -186,7 +178,7 @@ public class ContactListAdapter
ContactListItem item = getItem(i);
if (item.getContact().getId().equals(c)) return i;
}
return SortedList.INVALID_POSITION; // Not found
return INVALID_POSITION; // Not found
}
public void addAll(final List<ContactListItem> contacts) {

View File

@@ -10,15 +10,15 @@ import java.util.Collection;
class ContactListItem {
private final Contact contact;
private final GroupId conversation;
private final GroupId groupId;
private boolean connected, empty;
private long timestamp;
private int unread;
ContactListItem(Contact contact, boolean connected, GroupId conversation,
ContactListItem(Contact contact, boolean connected, GroupId groupId,
Collection<PrivateMessageHeader> headers) {
this.contact = contact;
this.conversation = conversation;
this.groupId = groupId;
this.connected = connected;
setHeaders(headers);
}
@@ -39,8 +39,8 @@ class ContactListItem {
return contact;
}
GroupId getConversationId() {
return conversation;
GroupId getGroupId() {
return groupId;
}
boolean isConnected() {

View File

@@ -27,21 +27,19 @@ import org.briarproject.api.crypto.CryptoExecutor;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.NoSuchContactException;
import org.briarproject.api.db.NoSuchMessageException;
import org.briarproject.api.db.NoSuchSubscriptionException;
import org.briarproject.api.event.ContactConnectedEvent;
import org.briarproject.api.event.ContactDisconnectedEvent;
import org.briarproject.api.event.ContactRemovedEvent;
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.MessagesAckedEvent;
import org.briarproject.api.event.MessagesSentEvent;
import org.briarproject.api.messaging.MessagingManager;
import org.briarproject.api.messaging.PrivateConversation;
import org.briarproject.api.messaging.PrivateMessage;
import org.briarproject.api.messaging.PrivateMessageFactory;
import org.briarproject.api.messaging.PrivateMessageHeader;
import org.briarproject.api.messaging.PrivateMessageHeader.Status;
import org.briarproject.api.plugins.ConnectionRegistry;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
@@ -66,8 +64,6 @@ import javax.inject.Inject;
import static android.widget.Toast.LENGTH_SHORT;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.api.messaging.PrivateMessageHeader.Status.DELIVERED;
import static org.briarproject.api.messaging.PrivateMessageHeader.Status.SENT;
public class ConversationActivity extends BriarActivity
implements EventListener, OnClickListener {
@@ -89,20 +85,19 @@ public class ConversationActivity extends BriarActivity
@Inject private volatile MessagingManager messagingManager;
@Inject private volatile EventBus eventBus;
@Inject private volatile PrivateMessageFactory privateMessageFactory;
private volatile GroupId groupId = null;
private volatile ContactId contactId = null;
private volatile String contactName = null;
private volatile GroupId groupId = null;
private volatile PrivateConversation conversation = null;
private volatile boolean connected;
private volatile boolean connected = false;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
Intent i = getIntent();
int id = i.getIntExtra("briar.CONTACT_ID", -1);
if (id == -1) throw new IllegalStateException();
contactId = new ContactId(id);
byte[] b = i.getByteArrayExtra("briar.GROUP_ID");
if (b == null) throw new IllegalStateException();
groupId = new GroupId(b);
setContentView(R.layout.activity_conversation);
@@ -122,19 +117,17 @@ public class ConversationActivity extends BriarActivity
public void onResume() {
super.onResume();
eventBus.addListener(this);
notificationManager.blockPrivateMessageNotification(contactId);
loadContactAndGroup();
notificationManager.blockNotification(groupId);
notificationManager.clearPrivateMessageNotification(groupId);
loadContactDetails();
loadHeaders();
// remove the notification for this conversation since we see it now
notificationManager.clearPrivateMessageNotification(contactId);
}
@Override
public void onPause() {
super.onPause();
eventBus.removeListener(this);
notificationManager.unblockPrivateMessageNotification(contactId);
notificationManager.unblockNotification(groupId);
if (isFinishing()) markMessagesRead();
}
@@ -164,26 +157,21 @@ public class ConversationActivity extends BriarActivity
}
}
private void loadContactAndGroup() {
private void loadContactDetails() {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
contactId = messagingManager.getContactId(groupId);
Contact contact = contactManager.getContact(contactId);
contactName = contact.getAuthor().getName();
groupId = messagingManager.getConversationId(contactId);
conversation = messagingManager.getConversation(groupId);
connected = connectionRegistry.isConnected(contactId);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO)) {
LOG.info("Loading contact and conversation took "
+ duration + " ms");
}
if (LOG.isLoggable(INFO))
LOG.info("Loading contact took " + duration + " ms");
displayContactDetails();
} catch (NoSuchContactException e) {
finishOnUiThread();
} catch (NoSuchSubscriptionException e) {
finishOnUiThread();
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
@@ -234,7 +222,11 @@ public class ConversationActivity extends BriarActivity
runOnUiThread(new Runnable() {
public void run() {
sendButton.setEnabled(true);
if (!headers.isEmpty()) {
if (headers.isEmpty()) {
// we have no messages,
// so let the list know to hide progress bar
list.showData();
} else {
for (PrivateMessageHeader h : headers) {
ConversationItem item = new ConversationItem(h);
byte[] body = bodyCache.get(h.getId());
@@ -244,10 +236,6 @@ public class ConversationActivity extends BriarActivity
}
// Scroll to the bottom
list.scrollToPosition(adapter.getItemCount() - 1);
} else {
// we have no messages,
// so let the list know to hide progress bar
list.showData();
}
}
});
@@ -278,17 +266,13 @@ public class ConversationActivity extends BriarActivity
public void run() {
bodyCache.put(m, body);
int count = adapter.getItemCount();
for (int i = 0; i < count; i++) {
ConversationItem item = adapter.getItem(i);
if (item.getHeader().getId().equals(m)) {
item.setBody(body);
adapter.notifyItemChanged(i);
// Scroll to the bottom
list.scrollToPosition(count - 1);
return;
}
}
@@ -297,7 +281,6 @@ public class ConversationActivity extends BriarActivity
}
private void markMessagesRead() {
notificationManager.clearPrivateMessageNotification(contactId);
List<MessageId> unread = new ArrayList<MessageId>();
int count = adapter.getItemCount();
for (int i = 0; i < count; i++) {
@@ -335,35 +318,25 @@ public class ConversationActivity extends BriarActivity
LOG.info("Contact removed");
finishOnUiThread();
}
} else if (e instanceof MessageAddedEvent) {
MessageAddedEvent mEvent = (MessageAddedEvent) e;
GroupId g = mEvent.getGroupId();
if (g.equals(groupId)) {
// mark new incoming messages as read directly
if (mEvent.getContactId() != null) {
ConversationItem item = adapter.getLastItem();
if (item != null) {
markIncomingMessageRead(mEvent.getMessage(),
item.getHeader().getTimestamp());
}
}
} else if (e instanceof MessageValidatedEvent) {
MessageValidatedEvent m = (MessageValidatedEvent) e;
if (m.isValid() && m.getMessage().getGroupId().equals(groupId)) {
LOG.info("Message added, reloading");
// TODO: get and add the ConversationItem here to prevent
// reloading the entire conversation
loadHeaders();
// Mark new incoming messages as read directly
if (m.isLocal()) loadHeaders();
else markMessageReadIfNew(m.getMessage());
}
} else if (e instanceof MessagesSentEvent) {
MessagesSentEvent m = (MessagesSentEvent) e;
if (m.getContactId().equals(contactId)) {
LOG.info("Messages sent");
markMessages(m.getMessageIds(), SENT);
markMessages(m.getMessageIds(), true, false);
}
} else if (e instanceof MessagesAckedEvent) {
MessagesAckedEvent m = (MessagesAckedEvent) e;
if (m.getContactId().equals(contactId)) {
LOG.info("Messages acked");
markMessages(m.getMessageIds(), DELIVERED);
markMessages(m.getMessageIds(), true, true);
}
} else if (e instanceof ContactConnectedEvent) {
ContactConnectedEvent c = (ContactConnectedEvent) e;
@@ -382,8 +355,37 @@ public class ConversationActivity extends BriarActivity
}
}
private void markMessageReadIfNew(final Message m) {
runOnUiThread(new Runnable() {
public void run() {
ConversationItem item = adapter.getLastItem();
if (item != null) {
// Mark the message read if it's the newest message
long lastMsgTime = item.getHeader().getTimestamp();
long newMsgTime = m.getTimestamp();
if (newMsgTime > lastMsgTime) markNewMessageRead(m);
else loadHeaders();
}
}
});
}
private void markNewMessageRead(final Message m) {
runOnDbThread(new Runnable() {
public void run() {
try {
messagingManager.setReadFlag(m.getId(), true);
loadHeaders();
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
});
}
private void markMessages(final Collection<MessageId> messageIds,
final Status status) {
final boolean sent, final boolean seen) {
runOnUiThread(new Runnable() {
public void run() {
Set<MessageId> messages = new HashSet<MessageId>(messageIds);
@@ -391,7 +393,8 @@ public class ConversationActivity extends BriarActivity
for (int i = 0; i < count; i++) {
ConversationItem item = adapter.getItem(i);
if (messages.contains(item.getHeader().getId())) {
item.setStatus(status);
item.setSent(sent);
item.setSeen(seen);
adapter.notifyItemChanged(i);
}
}
@@ -399,41 +402,8 @@ public class ConversationActivity extends BriarActivity
});
}
private void markIncomingMessageRead(final Message m,
final long lastMsgTime) {
// stop here if message is older than latest message we have
long newMsgTime = m.getTimestamp();
if (newMsgTime < lastMsgTime) return;
runOnDbThread(new Runnable() {
public void run() {
try {
// mark messages as read, because is latest
messagingManager.setReadFlag(m.getId(), true);
showIncomingMessageRead();
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
// TODO else: smooth-scroll up to unread messages if out of view
});
}
private void showIncomingMessageRead() {
runOnUiThread(new Runnable() {
public void run() {
// this is only called from markIncomingMessageRead()
// so we can assume that it was the last message that changed
adapter.notifyItemChanged(adapter.getItemCount() - 1);
}
});
}
public void onClick(View view) {
markMessagesRead();
String message = content.getText().toString();
if (message.equals("")) return;
long timestamp = System.currentTimeMillis();
@@ -445,21 +415,16 @@ public class ConversationActivity extends BriarActivity
private long getMinTimestampForNewMessage() {
// Don't use an earlier timestamp than the newest message
long timestamp = 0;
ConversationItem item = adapter.getLastItem();
if (item != null) {
timestamp = item.getHeader().getTimestamp();
}
return timestamp + 1;
return item == null ? 0 : item.getHeader().getTimestamp() + 1;
}
private void createMessage(final byte[] body, final long timestamp) {
cryptoExecutor.execute(new Runnable() {
public void run() {
try {
Message m = privateMessageFactory.createPrivateMessage(null,
conversation, "text/plain", timestamp, body);
storeMessage(m);
storeMessage(privateMessageFactory.createPrivateMessage(
groupId, timestamp, null, "text/plain", body));
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
} catch (IOException e) {
@@ -469,7 +434,7 @@ public class ConversationActivity extends BriarActivity
});
}
private void storeMessage(final Message m) {
private void storeMessage(final PrivateMessage m) {
runOnDbThread(new Runnable() {
public void run() {
try {
@@ -487,29 +452,20 @@ public class ConversationActivity extends BriarActivity
}
private void askToRemoveContact() {
runOnUiThread(new Runnable() {
@Override
public void run() {
DialogInterface.OnClickListener okListener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
removeContact();
}
};
AlertDialog.Builder builder =
new AlertDialog.Builder(ConversationActivity.this);
builder.setTitle(
getString(R.string.dialog_title_delete_contact));
builder.setMessage(
getString(R.string.dialog_message_delete_contact));
builder.setPositiveButton(android.R.string.ok, okListener);
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
}
});
DialogInterface.OnClickListener okListener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
removeContact();
}
};
AlertDialog.Builder builder =
new AlertDialog.Builder(ConversationActivity.this);
builder.setTitle(getString(R.string.dialog_title_delete_contact));
builder.setMessage(getString(R.string.dialog_message_delete_contact));
builder.setPositiveButton(android.R.string.ok, okListener);
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
}
private void removeContact() {

View File

@@ -14,9 +14,6 @@ import org.briarproject.R;
import org.briarproject.api.messaging.PrivateMessageHeader;
import org.briarproject.util.StringUtils;
import static org.briarproject.api.messaging.PrivateMessageHeader.Status.DELIVERED;
import static org.briarproject.api.messaging.PrivateMessageHeader.Status.SENT;
class ConversationAdapter extends
RecyclerView.Adapter<ConversationAdapter.MessageHolder> {
@@ -113,18 +110,18 @@ class ConversationAdapter extends
PrivateMessageHeader header = item.getHeader();
if (header.isLocal()) {
if (item.getStatus() == DELIVERED) {
if (item.isSeen()) {
ui.status.setImageResource(R.drawable.message_delivered);
} else if (item.getStatus() == SENT) {
} else if (item.isSent()) {
ui.status.setImageResource(R.drawable.message_sent);
} else {
ui.status.setImageResource(R.drawable.message_stored);
}
} else if (!header.isRead()) {
int bottom = ui.layout.getPaddingBottom();
int left = ui.layout.getPaddingLeft();
int top = ui.layout.getPaddingTop();
int right = ui.layout.getPaddingRight();
int left = ui.layout.getPaddingLeft();
int bottom = ui.layout.getPaddingBottom();
// show unread messages in different color to not miss them
ui.layout.setBackgroundResource(R.drawable.msg_in_unread);
@@ -185,7 +182,9 @@ class ConversationAdapter extends
this.messages.endBatchedUpdates();
}
// TODO: Does this class need to be public?
public static class MessageHolder extends RecyclerView.ViewHolder {
public ViewGroup layout;
public TextView body;
public TextView date;

View File

@@ -1,19 +1,19 @@
package org.briarproject.android.contact;
import org.briarproject.api.messaging.PrivateMessageHeader;
import org.briarproject.api.messaging.PrivateMessageHeader.Status;
// This class is not thread-safe
class ConversationItem {
private final PrivateMessageHeader header;
private byte[] body;
private Status status;
private boolean sent, seen;
ConversationItem(PrivateMessageHeader header) {
this.header = header;
body = null;
status = header.getStatus();
sent = header.isSent();
seen = header.isSeen();
}
PrivateMessageHeader getHeader() {
@@ -28,11 +28,19 @@ class ConversationItem {
this.body = body;
}
Status getStatus() {
return status;
boolean isSent() {
return sent;
}
void setStatus(Status status) {
this.status = status;
void setSent(boolean sent) {
this.sent = sent;
}
boolean isSeen() {
return seen;
}
void setSeen(boolean seen) {
this.seen = seen;
}
}

View File

@@ -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))

View File

@@ -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();
}

View File

@@ -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()) {

View File

@@ -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");