Moved some boilerplate into the BriarActivity superclass.

This commit is contained in:
akwizgran
2014-03-01 11:20:04 +00:00
parent 90ad3caedd
commit 86f8d97d81
16 changed files with 97 additions and 270 deletions

View File

@@ -13,7 +13,6 @@ import static org.briarproject.android.util.CommonLayoutParams.MATCH_WRAP_1;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
@@ -26,7 +25,6 @@ import org.briarproject.android.util.ListLoadingProgressBar;
import org.briarproject.api.AuthorId;
import org.briarproject.api.Contact;
import org.briarproject.api.ContactId;
import org.briarproject.api.android.DatabaseUiExecutor;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.MessageHeader;
@@ -37,7 +35,6 @@ import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.MessageAddedEvent;
import org.briarproject.api.event.MessageExpiredEvent;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.transport.ConnectionListener;
import org.briarproject.api.transport.ConnectionRegistry;
@@ -70,8 +67,6 @@ ConnectionListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Override
public void onCreate(Bundle state) {
@@ -128,10 +123,9 @@ ConnectionListener {
private void loadContacts() {
clearContacts();
dbUiExecutor.execute(new Runnable() {
runOnDbThread(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Map<ContactId, Long> times = db.getLastConnected();
for(Contact c : db.getContacts()) {
@@ -153,10 +147,6 @@ ConnectionListener {
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
@@ -257,10 +247,9 @@ ConnectionListener {
}
private void reloadContact(final ContactId c) {
dbUiExecutor.execute(new Runnable() {
runOnDbThread(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<MessageHeader> headers =
db.getInboxMessageHeaders(c);
@@ -273,10 +262,6 @@ ConnectionListener {
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});

View File

@@ -38,7 +38,6 @@ import org.briarproject.android.util.LayoutUtils;
import org.briarproject.android.util.ListLoadingProgressBar;
import org.briarproject.api.AuthorId;
import org.briarproject.api.ContactId;
import org.briarproject.api.android.DatabaseUiExecutor;
import org.briarproject.api.crypto.CryptoExecutor;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
@@ -51,7 +50,6 @@ import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.MessageAddedEvent;
import org.briarproject.api.event.MessageExpiredEvent;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.Message;
@@ -95,8 +93,6 @@ implements EventListener, OnClickListener, OnItemClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Inject private volatile MessageFactory messageFactory;
private volatile ContactId contactId = null;
private volatile GroupId groupId = null;
@@ -202,10 +198,9 @@ implements EventListener, OnClickListener, OnItemClickListener {
}
private void loadHeadersAndGroup() {
dbUiExecutor.execute(new Runnable() {
runOnDbThread(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<MessageHeader> headers =
db.getInboxMessageHeaders(contactId);
@@ -221,10 +216,6 @@ implements EventListener, OnClickListener, OnItemClickListener {
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
@@ -259,10 +250,9 @@ implements EventListener, OnClickListener, OnItemClickListener {
}
private void loadMessageBody(final MessageHeader h) {
dbUiExecutor.execute(new Runnable() {
runOnDbThread(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
byte[] body = db.getMessageBody(h.getId());
long duration = System.currentTimeMillis() - now;
@@ -274,10 +264,6 @@ implements EventListener, OnClickListener, OnItemClickListener {
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
@@ -333,10 +319,9 @@ implements EventListener, OnClickListener, OnItemClickListener {
}
private void markMessagesRead(final Collection<MessageId> unread) {
dbUiExecutor.execute(new Runnable() {
runOnDbThread(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
for(MessageId m : unread) db.setReadFlag(m, true);
long duration = System.currentTimeMillis() - now;
@@ -345,10 +330,6 @@ implements EventListener, OnClickListener, OnItemClickListener {
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
@@ -408,10 +389,9 @@ implements EventListener, OnClickListener, OnItemClickListener {
}
private void storeMessage(final Message m) {
dbUiExecutor.execute(new Runnable() {
runOnDbThread(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.addLocalMessage(m);
long duration = System.currentTimeMillis() - now;
@@ -420,10 +400,6 @@ implements EventListener, OnClickListener, OnItemClickListener {
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});

View File

@@ -11,7 +11,6 @@ import static org.briarproject.android.util.CommonLayoutParams.MATCH_WRAP_1;
import static org.briarproject.android.util.CommonLayoutParams.WRAP_WRAP_1;
import static org.briarproject.api.Author.Status.VERIFIED;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
@@ -23,11 +22,9 @@ import org.briarproject.android.util.ElasticHorizontalSpace;
import org.briarproject.android.util.HorizontalBorder;
import org.briarproject.android.util.LayoutUtils;
import org.briarproject.api.AuthorId;
import org.briarproject.api.android.DatabaseUiExecutor;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.NoSuchMessageException;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.MessageId;
import org.briarproject.util.StringUtils;
@@ -62,8 +59,6 @@ implements OnClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile MessageId messageId = null;
private volatile GroupId groupId = null;
private volatile long timestamp = -1;
@@ -182,10 +177,9 @@ implements OnClickListener {
}
private void setReadInDatabase(final boolean read) {
dbUiExecutor.execute(new Runnable() {
runOnDbThread(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.setReadFlag(messageId, read);
long duration = System.currentTimeMillis() - now;
@@ -195,10 +189,6 @@ implements OnClickListener {
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
@@ -215,35 +205,33 @@ implements OnClickListener {
}
private void loadMessageBody() {
dbUiExecutor.execute(new Runnable() {
runOnDbThread(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
byte[] body = db.getMessageBody(messageId);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Loading message took " + duration + " ms");
final String text = StringUtils.fromUtf8(body);
runOnUiThread(new Runnable() {
public void run() {
content.setText(text);
}
});
displayMessageBody(StringUtils.fromUtf8(body));
} catch(NoSuchMessageException e) {
finishOnUiThread();
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
}
private void displayMessageBody(final String body) {
runOnUiThread(new Runnable() {
public void run() {
content.setText(body);
}
});
}
@Override
public void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);

View File

@@ -26,13 +26,11 @@ import org.briarproject.android.util.CommonLayoutParams;
import org.briarproject.android.util.LayoutUtils;
import org.briarproject.api.AuthorId;
import org.briarproject.api.LocalAuthor;
import org.briarproject.api.android.DatabaseUiExecutor;
import org.briarproject.api.crypto.CryptoExecutor;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.NoSuchContactException;
import org.briarproject.api.db.NoSuchSubscriptionException;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.Message;
@@ -66,8 +64,6 @@ implements OnClickListener {
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Inject private volatile MessageFactory messageFactory;
private volatile String contactName = null;
private volatile GroupId groupId = null;
@@ -146,10 +142,9 @@ implements OnClickListener {
}
private void loadAuthorAndGroup() {
dbUiExecutor.execute(new Runnable() {
runOnDbThread(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
localAuthor = db.getLocalAuthor(localAuthorId);
group = db.getGroup(groupId);
@@ -164,9 +159,6 @@ implements OnClickListener {
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
@@ -212,10 +204,9 @@ implements OnClickListener {
}
private void storeMessage(final Message m) {
dbUiExecutor.execute(new Runnable() {
runOnDbThread(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.addLocalMessage(m);
long duration = System.currentTimeMillis() - now;
@@ -224,10 +215,6 @@ implements OnClickListener {
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});