mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +01:00
return shard activity dependencies
This commit is contained in:
@@ -34,8 +34,8 @@ public class CustodianHelpRecoverActivity extends BriarActivity implements
|
|||||||
@Inject
|
@Inject
|
||||||
public SocialBackupManager socialBackupManager;
|
public SocialBackupManager socialBackupManager;
|
||||||
|
|
||||||
@Inject
|
// @Inject
|
||||||
private MessageEncoder messageEncoder;
|
// public MessageEncoder messageEncoder;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public DatabaseComponent db;
|
public DatabaseComponent db;
|
||||||
@@ -73,10 +73,8 @@ public class CustodianHelpRecoverActivity extends BriarActivity implements
|
|||||||
public void scanQrButtonClicked() {
|
public void scanQrButtonClicked() {
|
||||||
try {
|
try {
|
||||||
db.transaction(false, txn -> {
|
db.transaction(false, txn -> {
|
||||||
ReturnShardPayload returnShardPayload = socialBackupManager
|
byte[] returnShardPayloadBytes = socialBackupManager
|
||||||
.getReturnShardPayload(txn, contactId);
|
.getReturnShardPayloadBytes(txn, contactId);
|
||||||
byte[] returnShardPayloadBytes = messageEncoder
|
|
||||||
.encodeReturnShardPayload(returnShardPayload);
|
|
||||||
|
|
||||||
Intent i = new Intent(this, ReturnShardActivity.class);
|
Intent i = new Intent(this, ReturnShardActivity.class);
|
||||||
i.putExtra(RETURN_SHARD_PAYLOAD, returnShardPayloadBytes);
|
i.putExtra(RETURN_SHARD_PAYLOAD, returnShardPayloadBytes);
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import android.widget.Toast;
|
|||||||
import org.briarproject.bramble.api.FormatException;
|
import org.briarproject.bramble.api.FormatException;
|
||||||
import org.briarproject.bramble.api.client.ClientHelper;
|
import org.briarproject.bramble.api.client.ClientHelper;
|
||||||
import org.briarproject.bramble.api.data.BdfList;
|
import org.briarproject.bramble.api.data.BdfList;
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
|
||||||
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;
|
||||||
@@ -19,9 +18,10 @@ import org.briarproject.briar.android.contact.add.nearby.AddNearbyContactFragmen
|
|||||||
import org.briarproject.briar.android.contact.add.nearby.AddNearbyContactPermissionManager;
|
import org.briarproject.briar.android.contact.add.nearby.AddNearbyContactPermissionManager;
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
import org.briarproject.briar.android.util.RequestBluetoothDiscoverable;
|
import org.briarproject.briar.android.util.RequestBluetoothDiscoverable;
|
||||||
import org.briarproject.briar.api.socialbackup.MessageEncoder;
|
import org.briarproject.briar.api.socialbackup.BackupPayload;
|
||||||
import org.briarproject.briar.api.socialbackup.MessageParser;
|
import org.briarproject.briar.api.socialbackup.MessageParser;
|
||||||
import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
|
import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
|
||||||
|
import org.briarproject.briar.api.socialbackup.Shard;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -37,6 +37,7 @@ import static android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
|
|||||||
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
|
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
|
||||||
import static android.widget.Toast.LENGTH_LONG;
|
import static android.widget.Toast.LENGTH_LONG;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
|
import static org.briarproject.bramble.util.ValidationUtils.checkSize;
|
||||||
import static org.briarproject.briar.android.socialbackup.CustodianHelpRecoverActivity.RETURN_SHARD_PAYLOAD;
|
import static org.briarproject.briar.android.socialbackup.CustodianHelpRecoverActivity.RETURN_SHARD_PAYLOAD;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@@ -50,8 +51,8 @@ public class ReturnShardActivity extends BaseActivity
|
|||||||
@Inject
|
@Inject
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
ViewModelProvider.Factory viewModelFactory;
|
||||||
|
|
||||||
@Inject
|
// @Inject
|
||||||
MessageParser messageParser;
|
// MessageParser messageParser;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ClientHelper clientHelper;
|
ClientHelper clientHelper;
|
||||||
@@ -78,15 +79,33 @@ public class ReturnShardActivity extends BaseActivity
|
|||||||
permissionLauncher::launch, viewModel.isBluetoothSupported());
|
permissionLauncher::launch, viewModel.isBluetoothSupported());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO the following two methods should be injected from messageParser
|
||||||
|
private Shard parseShardMessage(BdfList body) throws FormatException {
|
||||||
|
// Message type, secret ID, shard
|
||||||
|
byte[] secretId = body.getRaw(1);
|
||||||
|
byte[] shard = body.getRaw(2);
|
||||||
|
return new Shard(secretId, shard);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ReturnShardPayload parseReturnShardPayload(BdfList body)
|
||||||
|
throws FormatException {
|
||||||
|
checkSize(body, 2);
|
||||||
|
Shard shard = parseShardMessage(body.getList(0));
|
||||||
|
org.briarproject.briar.api.socialbackup.BackupPayload backupPayload =
|
||||||
|
new BackupPayload(body.getRaw(1));
|
||||||
|
return new ReturnShardPayload(shard, backupPayload);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(@Nullable Bundle state) {
|
public void onCreate(@Nullable Bundle state) {
|
||||||
super.onCreate(state);
|
super.onCreate(state);
|
||||||
|
|
||||||
byte[] returnShardPayloadBytes = getIntent().getByteArrayExtra(RETURN_SHARD_PAYLOAD);
|
byte[] returnShardPayloadBytes =
|
||||||
|
getIntent().getByteArrayExtra(RETURN_SHARD_PAYLOAD);
|
||||||
if (returnShardPayloadBytes != null) {
|
if (returnShardPayloadBytes != null) {
|
||||||
try {
|
try {
|
||||||
ReturnShardPayload returnShardPayload = messageParser
|
ReturnShardPayload returnShardPayload = parseReturnShardPayload(
|
||||||
.parseReturnShardPayload(clientHelper.toList(returnShardPayloadBytes));
|
clientHelper.toList(returnShardPayloadBytes));
|
||||||
viewModel.setSending(true);
|
viewModel.setSending(true);
|
||||||
viewModel.setReturnShardPayload(returnShardPayload);
|
viewModel.setReturnShardPayload(returnShardPayload);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
@@ -199,10 +218,10 @@ public class ReturnShardActivity extends BaseActivity
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onReturnShardStateChanged(ReturnShardState state) {
|
private void onReturnShardStateChanged(ReturnShardState state) {
|
||||||
if (state instanceof ReturnShardState.ContactExchangeFinished) {
|
if (state instanceof ReturnShardState.SocialBackupExchangeFinished) {
|
||||||
ReturnShardState.ContactExchangeResult result =
|
ReturnShardState.SocialBackupExchangeResult result =
|
||||||
((ReturnShardState.ContactExchangeFinished) state).result;
|
((ReturnShardState.SocialBackupExchangeFinished) state).result;
|
||||||
onContactExchangeResult(result);
|
onSocialBackupExchangeResult(result);
|
||||||
} else if (state instanceof ReturnShardState.Failed) {
|
} else if (state instanceof ReturnShardState.Failed) {
|
||||||
Boolean qrCodeTooOld =
|
Boolean qrCodeTooOld =
|
||||||
((ReturnShardState.Failed) state).qrCodeTooOld;
|
((ReturnShardState.Failed) state).qrCodeTooOld;
|
||||||
@@ -210,27 +229,14 @@ public class ReturnShardActivity extends BaseActivity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onContactExchangeResult(
|
private void onSocialBackupExchangeResult(
|
||||||
ReturnShardState.ContactExchangeResult result) {
|
ReturnShardState.SocialBackupExchangeResult result) {
|
||||||
if (result instanceof ReturnShardState.ContactExchangeResult.Success) {
|
if (result instanceof ReturnShardState.SocialBackupExchangeResult.Success) {
|
||||||
Author remoteAuthor =
|
// String text = getString(R.string.contact_added_toast, contactName);
|
||||||
((ReturnShardState.ContactExchangeResult.Success) result).remoteAuthor;
|
Toast.makeText(this, "Shard return successful", LENGTH_LONG).show();
|
||||||
String contactName = remoteAuthor.getName();
|
|
||||||
String text = getString(R.string.contact_added_toast, contactName);
|
|
||||||
Toast.makeText(this, text, LENGTH_LONG).show();
|
|
||||||
supportFinishAfterTransition();
|
supportFinishAfterTransition();
|
||||||
} else if (result instanceof ReturnShardState.ContactExchangeResult.Error) {
|
} else if (result instanceof ReturnShardState.SocialBackupExchangeResult.Error) {
|
||||||
Author duplicateAuthor =
|
showErrorFragment();
|
||||||
((ReturnShardState.ContactExchangeResult.Error) result).duplicateAuthor;
|
|
||||||
if (duplicateAuthor == null) {
|
|
||||||
showErrorFragment();
|
|
||||||
} else {
|
|
||||||
String contactName = duplicateAuthor.getName();
|
|
||||||
String text =
|
|
||||||
getString(R.string.contact_already_exists, contactName);
|
|
||||||
Toast.makeText(this, text, LENGTH_LONG).show();
|
|
||||||
supportFinishAfterTransition();
|
|
||||||
}
|
|
||||||
} else throw new AssertionError();
|
} else throw new AssertionError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,15 +27,15 @@ abstract class ReturnShardState {
|
|||||||
static class KeyAgreementStarted extends ReturnShardState {
|
static class KeyAgreementStarted extends ReturnShardState {
|
||||||
}
|
}
|
||||||
|
|
||||||
static class ContactExchangeStarted extends ReturnShardState {
|
static class SocialBackupExchangeStarted extends ReturnShardState {
|
||||||
}
|
}
|
||||||
|
|
||||||
static class ContactExchangeFinished extends ReturnShardState {
|
static class SocialBackupExchangeFinished extends ReturnShardState {
|
||||||
final ContactExchangeResult
|
final SocialBackupExchangeResult
|
||||||
result;
|
result;
|
||||||
|
|
||||||
ContactExchangeFinished(
|
SocialBackupExchangeFinished(
|
||||||
ContactExchangeResult result) {
|
SocialBackupExchangeResult result) {
|
||||||
this.result = result;
|
this.result = result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,16 +58,12 @@ abstract class ReturnShardState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract static class ContactExchangeResult {
|
abstract static class SocialBackupExchangeResult {
|
||||||
static class Success extends ContactExchangeResult {
|
static class Success extends SocialBackupExchangeResult {
|
||||||
final Author remoteAuthor;
|
Success() {}
|
||||||
|
|
||||||
Success(Author remoteAuthor) {
|
|
||||||
this.remoteAuthor = remoteAuthor;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static class Error extends ContactExchangeResult {
|
static class Error extends SocialBackupExchangeResult {
|
||||||
@Nullable
|
@Nullable
|
||||||
final Author duplicateAuthor;
|
final Author duplicateAuthor;
|
||||||
|
|
||||||
|
|||||||
@@ -353,7 +353,7 @@ class ReturnShardViewModel extends AndroidViewModel
|
|||||||
KeyAgreementResult result =
|
KeyAgreementResult result =
|
||||||
((KeyAgreementFinishedEvent) e).getResult();
|
((KeyAgreementFinishedEvent) e).getResult();
|
||||||
startContactExchange(result);
|
startContactExchange(result);
|
||||||
state.setValue(new ReturnShardState.ContactExchangeStarted());
|
state.setValue(new ReturnShardState.SocialBackupExchangeStarted());
|
||||||
} else if (e instanceof KeyAgreementAbortedEvent) {
|
} else if (e instanceof KeyAgreementAbortedEvent) {
|
||||||
LOG.info("KeyAgreementAbortedEvent received");
|
LOG.info("KeyAgreementAbortedEvent received");
|
||||||
resetPayloadFlags();
|
resetPayloadFlags();
|
||||||
@@ -449,32 +449,28 @@ class ReturnShardViewModel extends AndroidViewModel
|
|||||||
if (sending) {
|
if (sending) {
|
||||||
socialBackupExchangeManager.sendReturnShard(conn, masterKey, alice, returnShardPayload);
|
socialBackupExchangeManager.sendReturnShard(conn, masterKey, alice, returnShardPayload);
|
||||||
} else {
|
} else {
|
||||||
ReturnShardPayload returnShardPayload = socialBackupExchangeManager.receiveReturnShard(conn, masterKey, alice);
|
returnShardPayload = socialBackupExchangeManager.receiveReturnShard(conn, masterKey, alice);
|
||||||
}
|
}
|
||||||
// Reuse the connection as a transport connection
|
ReturnShardState.SocialBackupExchangeResult.Success
|
||||||
connectionManager
|
|
||||||
.manageOutgoingConnection(contact.getId(), t, conn);
|
|
||||||
ReturnShardState.ContactExchangeResult.Success
|
|
||||||
success =
|
success =
|
||||||
new ReturnShardState.ContactExchangeResult.Success(
|
new ReturnShardState.SocialBackupExchangeResult.Success();
|
||||||
contact.getAuthor());
|
|
||||||
state.postValue(
|
state.postValue(
|
||||||
new ReturnShardState.ContactExchangeFinished(success));
|
new ReturnShardState.SocialBackupExchangeFinished(success));
|
||||||
} catch (ContactExistsException e) {
|
} catch (ContactExistsException e) {
|
||||||
tryToClose(conn);
|
tryToClose(conn);
|
||||||
ReturnShardState.ContactExchangeResult.Error
|
ReturnShardState.SocialBackupExchangeResult.Error
|
||||||
error = new ReturnShardState.ContactExchangeResult.Error(
|
error = new ReturnShardState.SocialBackupExchangeResult.Error(
|
||||||
e.getRemoteAuthor());
|
e.getRemoteAuthor());
|
||||||
state.postValue(
|
state.postValue(
|
||||||
new ReturnShardState.ContactExchangeFinished(error));
|
new ReturnShardState.SocialBackupExchangeFinished(error));
|
||||||
} catch (DbException | IOException e) {
|
} catch (DbException | IOException e) {
|
||||||
tryToClose(conn);
|
tryToClose(conn);
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
ReturnShardState.ContactExchangeResult.Error
|
ReturnShardState.SocialBackupExchangeResult.Error
|
||||||
error =
|
error =
|
||||||
new ReturnShardState.ContactExchangeResult.Error(null);
|
new ReturnShardState.SocialBackupExchangeResult.Error(null);
|
||||||
state.postValue(
|
state.postValue(
|
||||||
new ReturnShardState.ContactExchangeFinished(error));
|
new ReturnShardState.SocialBackupExchangeFinished(error));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user