Separated event infrastructure from DB.

This commit is contained in:
akwizgran
2014-10-03 09:44:54 +01:00
parent 6a4ea49786
commit 8b8df435a5
29 changed files with 368 additions and 244 deletions

View File

@@ -27,6 +27,7 @@ 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.SettingsUpdatedEvent;
import org.briarproject.api.lifecycle.Service;
@@ -52,6 +53,7 @@ Service, EventListener {
private final DatabaseComponent db;
private final Executor dbExecutor;
private final EventBus eventBus;
private final Context appContext;
private final Map<ContactId, Integer> contactCounts =
new HashMap<ContactId, Integer>(); // Locking: this
@@ -65,14 +67,16 @@ Service, EventListener {
@Inject
public AndroidNotificationManagerImpl(DatabaseComponent db,
@DatabaseExecutor Executor dbExecutor, Application app) {
@DatabaseExecutor Executor dbExecutor, EventBus eventBus,
Application app) {
this.db = db;
this.dbExecutor = dbExecutor;
this.eventBus = eventBus;
appContext = app.getApplicationContext();
}
public boolean start() {
db.addListener(this);
eventBus.addListener(this);
loadSettings();
return true;
}
@@ -91,7 +95,7 @@ Service, EventListener {
}
public boolean stop() {
db.removeListener(this);
eventBus.removeListener(this);
return true;
}

View File

@@ -23,6 +23,7 @@ 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;
@@ -58,6 +59,7 @@ public class BriarService extends RoboService implements EventListener {
@Inject private volatile AndroidExecutor androidExecutor;
@Inject @DatabaseExecutor private volatile Executor dbExecutor;
@Inject private volatile DatabaseComponent db;
@Inject private volatile EventBus eventBus;
private volatile boolean started = false;
@Override
@@ -92,7 +94,7 @@ public class BriarService extends RoboService implements EventListener {
public void run() {
StartResult result = lifecycleManager.startServices();
if(result == SUCCESS) {
db.addListener(BriarService.this);
eventBus.addListener(BriarService.this);
started = true;
} else if(result == ALREADY_RUNNING) {
LOG.info("Already running");
@@ -146,7 +148,7 @@ public class BriarService extends RoboService implements EventListener {
@Override
public void run() {
if(started) {
db.removeListener(BriarService.this);
eventBus.removeListener(BriarService.this);
lifecycleManager.stopServices();
}
androidExecutor.shutdown();

View File

@@ -35,6 +35,7 @@ import org.briarproject.api.TransportId;
import org.briarproject.api.db.DatabaseComponent;
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.SettingsUpdatedEvent;
import org.briarproject.util.StringUtils;
@@ -72,6 +73,7 @@ OnClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject private volatile EventBus eventBus;
private volatile Settings settings;
private volatile boolean bluetoothSetting = true;
@@ -201,7 +203,7 @@ OnClickListener {
@Override
public void onResume() {
super.onResume();
db.addListener(this);
eventBus.addListener(this);
loadSettings();
}
@@ -262,7 +264,7 @@ OnClickListener {
@Override
public void onPause() {
super.onPause();
db.removeListener(this);
eventBus.removeListener(this);
}
public void onClick(View view) {

View File

@@ -33,6 +33,7 @@ import org.briarproject.api.db.NoSuchContactException;
import org.briarproject.api.event.ContactAddedEvent;
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.MessageExpiredEvent;
@@ -75,6 +76,7 @@ EventListener, ConnectionListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject private volatile EventBus eventBus;
@Override
public void onCreate(Bundle state) {
@@ -125,7 +127,7 @@ EventListener, ConnectionListener {
@Override
public void onResume() {
super.onResume();
db.addListener(this);
eventBus.addListener(this);
connectionRegistry.addListener(this);
loadContacts();
}
@@ -210,7 +212,7 @@ EventListener, ConnectionListener {
@Override
public void onPause() {
super.onPause();
db.removeListener(this);
eventBus.removeListener(this);
connectionRegistry.removeListener(this);
}

View File

@@ -50,6 +50,7 @@ import org.briarproject.api.db.NoSuchMessageException;
import org.briarproject.api.db.NoSuchSubscriptionException;
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.MessageExpiredEvent;
@@ -96,6 +97,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject private volatile EventBus eventBus;
@Inject private volatile MessageFactory messageFactory;
private volatile ContactId contactId = null;
private volatile String contactName = null;
@@ -188,7 +190,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
@Override
public void onResume() {
super.onResume();
db.addListener(this);
eventBus.addListener(this);
loadContactAndGroup();
loadHeaders();
}
@@ -331,7 +333,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
@Override
public void onPause() {
super.onPause();
db.removeListener(this);
eventBus.removeListener(this);
if(isFinishing()) markMessagesRead();
}

View File

@@ -30,6 +30,7 @@ import org.briarproject.api.ContactId;
import org.briarproject.api.db.DatabaseComponent;
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.LocalSubscriptionsUpdatedEvent;
import org.briarproject.api.event.RemoteSubscriptionsUpdatedEvent;
@@ -69,6 +70,7 @@ SelectContactsDialog.Listener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject private volatile EventBus eventBus;
private volatile GroupId groupId = null;
private volatile Group group = null;
private volatile boolean subscribed = false;
@@ -171,7 +173,7 @@ SelectContactsDialog.Listener {
@Override
public void onResume() {
super.onResume();
db.addListener(this);
eventBus.addListener(this);
loadSubscribers();
}
@@ -224,7 +226,7 @@ SelectContactsDialog.Listener {
@Override
public void onPause() {
super.onPause();
db.removeListener(this);
eventBus.removeListener(this);
}
public void onClick(View view) {

View File

@@ -35,6 +35,7 @@ import org.briarproject.api.db.MessageHeader;
import org.briarproject.api.db.NoSuchMessageException;
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.MessageExpiredEvent;
@@ -72,6 +73,7 @@ OnClickListener, OnItemClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject private volatile EventBus eventBus;
private volatile GroupId groupId = null;
private volatile Group group = null;
@@ -139,7 +141,7 @@ OnClickListener, OnItemClickListener {
@Override
public void onResume() {
super.onResume();
db.addListener(this);
eventBus.addListener(this);
loadGroup();
loadHeaders();
}
@@ -272,7 +274,7 @@ OnClickListener, OnItemClickListener {
@Override
public void onPause() {
super.onPause();
db.removeListener(this);
eventBus.removeListener(this);
if(isFinishing()) markMessagesRead();
}

View File

@@ -30,6 +30,7 @@ import org.briarproject.api.db.DbException;
import org.briarproject.api.db.MessageHeader;
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.MessageExpiredEvent;
@@ -70,6 +71,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject private volatile EventBus eventBus;
@Override
public void onCreate(Bundle state) {
@@ -143,7 +145,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
@Override
public void onResume() {
super.onResume();
db.addListener(this);
eventBus.addListener(this);
loadHeaders();
}
@@ -210,7 +212,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
adapter.sort(GroupListItemComparator.INSTANCE);
adapter.notifyDataSetChanged();
selectFirstUnread();
}
}
});
}
@@ -255,7 +257,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
@Override
public void onPause() {
super.onPause();
db.removeListener(this);
eventBus.removeListener(this);
}
public void eventOccurred(Event e) {

View File

@@ -16,6 +16,7 @@ import org.briarproject.android.util.ListLoadingProgressBar;
import org.briarproject.api.db.DatabaseComponent;
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.RemoteSubscriptionsUpdatedEvent;
import org.briarproject.api.event.SubscriptionAddedEvent;
@@ -44,6 +45,7 @@ implements EventListener, OnItemClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject private volatile EventBus eventBus;
@Override
public void onCreate(Bundle state) {
@@ -69,7 +71,7 @@ implements EventListener, OnItemClickListener {
@Override
public void onResume() {
super.onResume();
db.addListener(this);
eventBus.addListener(this);
loadGroups();
}
@@ -111,7 +113,7 @@ implements EventListener, OnItemClickListener {
@Override
public void onPause() {
super.onPause();
db.removeListener(this);
eventBus.removeListener(this);
}
public void eventOccurred(Event e) {