mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
Introduce ContactExchangeResult
to include all result information in LiveData
This commit is contained in:
@@ -8,13 +8,10 @@ import org.briarproject.bramble.api.keyagreement.KeyAgreementResult;
|
|||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
|
||||||
|
|
||||||
import static android.widget.Toast.LENGTH_LONG;
|
import static android.widget.Toast.LENGTH_LONG;
|
||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
@@ -23,40 +20,29 @@ import static java.util.Objects.requireNonNull;
|
|||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
public class ContactExchangeActivity extends KeyAgreementActivity {
|
public class ContactExchangeActivity extends KeyAgreementActivity {
|
||||||
|
|
||||||
@Inject
|
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
|
||||||
|
|
||||||
private ContactExchangeViewModel viewModel;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void injectActivity(ActivityComponent component) {
|
|
||||||
component.inject(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(@Nullable Bundle state) {
|
public void onCreate(@Nullable Bundle state) {
|
||||||
super.onCreate(state);
|
super.onCreate(state);
|
||||||
requireNonNull(getSupportActionBar())
|
requireNonNull(getSupportActionBar())
|
||||||
.setTitle(R.string.add_contact_title);
|
.setTitle(R.string.add_contact_title);
|
||||||
viewModel = new ViewModelProvider(this, viewModelFactory)
|
|
||||||
.get(ContactExchangeViewModel.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startContactExchange(KeyAgreementResult result) {
|
private void startContactExchange(KeyAgreementResult agreementResult) {
|
||||||
viewModel.getSucceeded().observe(this, succeeded -> {
|
viewModel.getContactExchangeResult().observe(this, result -> {
|
||||||
if (succeeded == null) return;
|
if (result instanceof ContactExchangeResult.Success) {
|
||||||
if (succeeded) {
|
Author remoteAuthor =
|
||||||
Author remote = requireNonNull(viewModel.getRemoteAuthor());
|
((ContactExchangeResult.Success) result).remoteAuthor;
|
||||||
contactExchangeSucceeded(remote);
|
contactExchangeSucceeded(remoteAuthor);
|
||||||
} else {
|
} else if (result instanceof ContactExchangeResult.Error) {
|
||||||
Author duplicate = viewModel.getDuplicateAuthor();
|
Author duplicateAuthor =
|
||||||
if (duplicate == null) contactExchangeFailed();
|
((ContactExchangeResult.Error) result).duplicateAuthor;
|
||||||
else duplicateContact(duplicate);
|
if (duplicateAuthor == null) contactExchangeFailed();
|
||||||
}
|
else duplicateContact(duplicateAuthor);
|
||||||
|
} else throw new AssertionError();
|
||||||
});
|
});
|
||||||
viewModel.startContactExchange(result.getTransportId(),
|
viewModel.startContactExchange(agreementResult.getTransportId(),
|
||||||
result.getConnection(), result.getMasterKey(),
|
agreementResult.getConnection(), agreementResult.getMasterKey(),
|
||||||
result.wasAlice());
|
agreementResult.wasAlice());
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package org.briarproject.briar.android.contact.add.nearby;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
|
@Immutable
|
||||||
|
@NotNullByDefault
|
||||||
|
abstract class ContactExchangeResult {
|
||||||
|
|
||||||
|
static class Success extends ContactExchangeResult {
|
||||||
|
final Author remoteAuthor;
|
||||||
|
|
||||||
|
Success(Author remoteAuthor) {
|
||||||
|
this.remoteAuthor = remoteAuthor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Error extends ContactExchangeResult {
|
||||||
|
@Nullable
|
||||||
|
final Author duplicateAuthor;
|
||||||
|
|
||||||
|
Error(@Nullable Author duplicateAuthor) {
|
||||||
|
this.duplicateAuthor = duplicateAuthor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,17 +8,17 @@ import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
|||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
import org.briarproject.bramble.api.db.ContactExistsException;
|
import org.briarproject.bramble.api.db.ContactExistsException;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||||
|
import org.briarproject.briar.android.contact.add.nearby.ContactExchangeResult.Error;
|
||||||
|
import org.briarproject.briar.android.contact.add.nearby.ContactExchangeResult.Success;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
@@ -39,10 +39,8 @@ class ContactExchangeViewModel extends AndroidViewModel {
|
|||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
private final ContactExchangeManager contactExchangeManager;
|
private final ContactExchangeManager contactExchangeManager;
|
||||||
private final ConnectionManager connectionManager;
|
private final ConnectionManager connectionManager;
|
||||||
private final MutableLiveData<Boolean> succeeded = new MutableLiveData<>();
|
private final MutableLiveData<ContactExchangeResult> exchangeResult =
|
||||||
|
new MutableLiveData<>();
|
||||||
@Nullable
|
|
||||||
private volatile Author remoteAuthor, duplicateAuthor;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ContactExchangeViewModel(Application app, @IoExecutor Executor ioExecutor,
|
ContactExchangeViewModel(Application app, @IoExecutor Executor ioExecutor,
|
||||||
@@ -62,18 +60,16 @@ class ContactExchangeViewModel extends AndroidViewModel {
|
|||||||
Contact contact = contactExchangeManager.exchangeContacts(conn,
|
Contact contact = contactExchangeManager.exchangeContacts(conn,
|
||||||
masterKey, alice, true);
|
masterKey, alice, true);
|
||||||
// Reuse the connection as a transport connection
|
// Reuse the connection as a transport connection
|
||||||
connectionManager.manageOutgoingConnection(contact.getId(),
|
connectionManager
|
||||||
t, conn);
|
.manageOutgoingConnection(contact.getId(), t, conn);
|
||||||
remoteAuthor = contact.getAuthor();
|
exchangeResult.postValue(new Success(contact.getAuthor()));
|
||||||
succeeded.postValue(true);
|
|
||||||
} catch (ContactExistsException e) {
|
} catch (ContactExistsException e) {
|
||||||
tryToClose(conn);
|
tryToClose(conn);
|
||||||
duplicateAuthor = e.getRemoteAuthor();
|
exchangeResult.postValue(new Error(e.getRemoteAuthor()));
|
||||||
succeeded.postValue(false);
|
|
||||||
} catch (DbException | IOException e) {
|
} catch (DbException | IOException e) {
|
||||||
tryToClose(conn);
|
tryToClose(conn);
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
succeeded.postValue(false);
|
exchangeResult.postValue(new Error(null));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -87,19 +83,7 @@ class ContactExchangeViewModel extends AndroidViewModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
LiveData<ContactExchangeResult> getContactExchangeResult() {
|
||||||
@Nullable
|
return exchangeResult;
|
||||||
Author getRemoteAuthor() {
|
|
||||||
return remoteAuthor;
|
|
||||||
}
|
|
||||||
|
|
||||||
@UiThread
|
|
||||||
@Nullable
|
|
||||||
Author getDuplicateAuthor() {
|
|
||||||
return duplicateAuthor;
|
|
||||||
}
|
|
||||||
|
|
||||||
LiveData<Boolean> getSucceeded() {
|
|
||||||
return succeeded;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,10 +22,10 @@ import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
|
|||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
import org.briarproject.briar.android.activity.BriarActivity;
|
import org.briarproject.briar.android.activity.BriarActivity;
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
|
|
||||||
import org.briarproject.briar.android.contact.add.nearby.IntroFragment.IntroScreenSeenListener;
|
import org.briarproject.briar.android.contact.add.nearby.IntroFragment.IntroScreenSeenListener;
|
||||||
import org.briarproject.briar.android.contact.add.nearby.KeyAgreementFragment.KeyAgreementEventListener;
|
import org.briarproject.briar.android.contact.add.nearby.KeyAgreementFragment.KeyAgreementEventListener;
|
||||||
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
|
import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -38,6 +38,7 @@ import androidx.appcompat.app.AlertDialog.Builder;
|
|||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
import androidx.fragment.app.FragmentManager;
|
import androidx.fragment.app.FragmentManager;
|
||||||
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
|
||||||
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
|
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
|
||||||
import static android.Manifest.permission.CAMERA;
|
import static android.Manifest.permission.CAMERA;
|
||||||
@@ -97,12 +98,18 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(KeyAgreementActivity.class.getName());
|
getLogger(KeyAgreementActivity.class.getName());
|
||||||
|
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ViewModelProvider.Factory viewModelFactory;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
EventBus eventBus;
|
EventBus eventBus;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
PluginManager pluginManager;
|
PluginManager pluginManager;
|
||||||
|
|
||||||
|
protected ContactExchangeViewModel viewModel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set to true in onPostResume() and false in onPause(). This prevents the
|
* Set to true in onPostResume() and false in onPause(). This prevents the
|
||||||
* QR code fragment from being shown if onRequestPermissionsResult() is
|
* QR code fragment from being shown if onRequestPermissionsResult() is
|
||||||
@@ -140,6 +147,8 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
@Override
|
@Override
|
||||||
public void injectActivity(ActivityComponent component) {
|
public void injectActivity(ActivityComponent component) {
|
||||||
component.inject(this);
|
component.inject(this);
|
||||||
|
viewModel = new ViewModelProvider(this, viewModelFactory)
|
||||||
|
.get(ContactExchangeViewModel.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user