Make MailboxManager#checkConnection() blocking and let the UI manage the executor

This commit is contained in:
Torsten Grote
2022-04-07 10:28:11 -03:00
parent 5b648cbd35
commit 60a1a4d2d1
3 changed files with 27 additions and 35 deletions

View File

@@ -1,9 +1,7 @@
package org.briarproject.bramble.api.mailbox; package org.briarproject.bramble.api.mailbox;
import org.briarproject.bramble.api.Consumer;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.lifecycle.IoExecutor;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@@ -36,13 +34,11 @@ public interface MailboxManager {
/** /**
* Can be used by the UI to test the mailbox connection. * Can be used by the UI to test the mailbox connection.
* After the connection has been made, the given {@param connectionCallback} *
* will be called with true (success) or false (error). * @return true (success) or false (error).
* In addition, a {@link OwnMailboxConnectionStatusEvent} might be broadcast * A {@link OwnMailboxConnectionStatusEvent} might be broadcast with a new
* with a new {@link MailboxStatus}. * {@link MailboxStatus}.
* <p>
* Note that the callback will be made on the {@link IoExecutor}.
*/ */
void checkConnection(Consumer<Boolean> connectionCallback); boolean checkConnection();
} }

View File

@@ -1,6 +1,5 @@
package org.briarproject.bramble.mailbox; package org.briarproject.bramble.mailbox;
import org.briarproject.bramble.api.Consumer;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.db.TransactionManager; import org.briarproject.bramble.api.db.TransactionManager;
@@ -98,8 +97,7 @@ class MailboxManagerImpl implements MailboxManager {
} }
@Override @Override
public void checkConnection(Consumer<Boolean> connectionCallback) { public boolean checkConnection() {
ioExecutor.execute(() -> {
boolean success; boolean success;
try { try {
MailboxProperties props = db.transactionWithNullableResult(true, MailboxProperties props = db.transactionWithNullableResult(true,
@@ -109,8 +107,7 @@ class MailboxManagerImpl implements MailboxManager {
success = false; success = false;
logException(LOG, WARNING, e); logException(LOG, WARNING, e);
} }
connectionCallback.accept(success); if (success) {
if (!success) return;
try { try {
// we are only recording successful connections here // we are only recording successful connections here
// as those update the UI and failures might be false negatives // as those update the UI and failures might be false negatives
@@ -120,7 +117,8 @@ class MailboxManagerImpl implements MailboxManager {
} catch (DbException e) { } catch (DbException e) {
logException(LOG, WARNING, e); logException(LOG, WARNING, e);
} }
}); }
return success;
} }
} }

View File

@@ -52,6 +52,7 @@ class MailboxViewModel extends DbViewModel
getLogger(MailboxViewModel.class.getName()); getLogger(MailboxViewModel.class.getName());
private final EventBus eventBus; private final EventBus eventBus;
private final Executor ioExecutor;
private final QrCodeDecoder qrCodeDecoder; private final QrCodeDecoder qrCodeDecoder;
private final PluginManager pluginManager; private final PluginManager pluginManager;
private final MailboxManager mailboxManager; private final MailboxManager mailboxManager;
@@ -76,6 +77,7 @@ class MailboxViewModel extends DbViewModel
MailboxManager mailboxManager) { MailboxManager mailboxManager) {
super(app, dbExecutor, lifecycleManager, db, androidExecutor); super(app, dbExecutor, lifecycleManager, db, androidExecutor);
this.eventBus = eventBus; this.eventBus = eventBus;
this.ioExecutor = ioExecutor;
this.pluginManager = pluginManager; this.pluginManager = pluginManager;
this.mailboxManager = mailboxManager; this.mailboxManager = mailboxManager;
qrCodeDecoder = new QrCodeDecoder(androidExecutor, ioExecutor, this); qrCodeDecoder = new QrCodeDecoder(androidExecutor, ioExecutor, this);
@@ -183,18 +185,14 @@ class MailboxViewModel extends DbViewModel
LiveData<Boolean> checkConnection() { LiveData<Boolean> checkConnection() {
MutableLiveData<Boolean> liveData = new MutableLiveData<>(); MutableLiveData<Boolean> liveData = new MutableLiveData<>();
mailboxManager.checkConnection(result -> ioExecutor.execute(() -> {
onConnectionCheckFinished(liveData, result)); boolean success = mailboxManager.checkConnection();
return liveData;
}
@IoExecutor
private void onConnectionCheckFinished(MutableLiveData<Boolean> liveData,
boolean success) {
if (LOG.isLoggable(INFO)) { if (LOG.isLoggable(INFO)) {
LOG.info("Got result from connection check: " + success); LOG.info("Got result from connection check: " + success);
} }
liveData.postValue(success); liveData.postValue(success);
});
return liveData;
} }
@UiThread @UiThread