Resolve merge conflict with social-backup-poc

This commit is contained in:
ameba23
2021-10-06 11:32:11 +02:00
33 changed files with 768 additions and 83 deletions

View File

@@ -387,6 +387,13 @@ public class ConversationActivity extends BriarActivity
}
});
// enable help recover account action if available
observeOnce(viewModel.amCustodian(), this, enable -> {
if (enable) {
menu.findItem(R.id.action_help_recover_account).setEnabled(true);
}
});
return super.onCreateOptionsMenu(menu);
}

View File

@@ -41,6 +41,7 @@ import org.briarproject.briar.api.messaging.PrivateMessageFactory;
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
import org.briarproject.briar.api.messaging.event.AttachmentReceivedEvent;
import org.briarproject.briar.api.remotewipe.RemoteWipeManager;
import org.briarproject.briar.api.socialbackup.SocialBackupManager;
import java.util.Collection;
import java.util.List;
@@ -86,6 +87,7 @@ public class ConversationViewModel extends DbViewModel
private final AttachmentRetriever attachmentRetriever;
private final AttachmentCreator attachmentCreator;
private final RemoteWipeManager remoteWipeManager;
private final SocialBackupManager socialBackupManager;
@Nullable
private ContactId contactId = null;
@@ -108,6 +110,7 @@ public class ConversationViewModel extends DbViewModel
new MutableLiveEvent<>();
private final MutableLiveData<Boolean> amRemoteWiper = new MutableLiveData<>();
private final MutableLiveData<Boolean> isRemoteWiper = new MutableLiveData<>();
private final MutableLiveData<Boolean> amCustodian = new MutableLiveData<>();
@Inject
ConversationViewModel(Application application,
@@ -123,7 +126,8 @@ public class ConversationViewModel extends DbViewModel
PrivateMessageFactory privateMessageFactory,
AttachmentRetriever attachmentRetriever,
RemoteWipeManager remoteWipeManager,
AttachmentCreator attachmentCreator) {
AttachmentCreator attachmentCreator,
SocialBackupManager socialBackupManager) {
super(application, dbExecutor, lifecycleManager, db, androidExecutor);
this.db = db;
this.eventBus = eventBus;
@@ -135,6 +139,7 @@ public class ConversationViewModel extends DbViewModel
this.attachmentRetriever = attachmentRetriever;
this.attachmentCreator = attachmentCreator;
this.remoteWipeManager = remoteWipeManager;
this.socialBackupManager = socialBackupManager;
messagingGroupId = map(contactItem, c ->
messagingManager.getContactGroup(c.getContact()).getId());
contactDeleted.setValue(false);
@@ -311,6 +316,10 @@ public class ConversationViewModel extends DbViewModel
boolean isWiper = db.transactionWithResult(true,
txn -> remoteWipeManager.isWiper(txn, c));
isRemoteWiper.postValue(isWiper);
// Check if we are a social backup custodian for this contact
boolean amCustodianBool = db.transactionWithResult(true,
txn -> socialBackupManager.amCustodian(txn, c));
amCustodian.postValue(amCustodianBool);
}
@DatabaseExecutor
@@ -404,6 +413,10 @@ public class ConversationViewModel extends DbViewModel
return isRemoteWiper;
}
LiveData<Boolean> amCustodian() {
return amCustodian;
}
@UiThread
void recheckFeaturesAndOnboarding(ContactId contactId) {
runOnDbThread(() -> {

View File

@@ -4,6 +4,7 @@ import android.os.Bundle;
import android.widget.Toast;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.contact.ContactManager;
import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.briar.R;
@@ -29,6 +30,9 @@ public class DistributedBackupActivity extends BriarActivity implements
@Inject
public SocialBackupManager socialBackupManager;
@Inject
public ContactManager contactManager;
@Inject
public DatabaseComponent db;
@@ -52,6 +56,20 @@ public class DistributedBackupActivity extends BriarActivity implements
showInitialFragment(fragment);
});
} catch (DbException e) {
// Check the number of contacts in the contacts list > 1
try {
if (contactManager.getContacts().size() < 2) {
Toast.makeText(this,
R.string.social_backup_not_enough_contacts,
Toast.LENGTH_LONG).show();
finish();
}
} catch (DbException dbException) {
Toast.makeText(this,
R.string.reading_contacts_error,
Toast.LENGTH_LONG).show();
finish();
}
CustodianSelectorFragment fragment =
CustodianSelectorFragment.newInstance();
showInitialFragment(fragment);

View File

@@ -64,17 +64,13 @@ public class ThresholdSelectorFragment extends BaseFragment {
message = view.findViewById(R.id.textViewMessage);
mOfn = view.findViewById(R.id.textViewmOfn);
if (numberOfCustodians == 2) {
message.setText(R.string.threshold_too_few_custodians);
}
if (numberOfCustodians > 3) {
seekBar.setMax(numberOfCustodians -3);
seekBar.setProgress(threshold - 2);
seekBar.setOnSeekBarChangeListener(new SeekBarListener());
recommendedThreshold =
SecretSharingWrapper.defaultThreshold(numberOfCustodians);
threshold = recommendedThreshold;
seekBar.setProgress(threshold - 2);
} else {
seekBar.setEnabled(false);
threshold = 2;

View File

@@ -1,6 +1,8 @@
package org.briarproject.briar.android.socialbackup.recover;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
@@ -14,6 +16,7 @@ import org.briarproject.bramble.api.system.AndroidExecutor;
import org.briarproject.briar.android.contact.add.nearby.QrCodeUtils;
import org.briarproject.briar.android.viewmodel.LiveEvent;
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
import org.briarproject.briar.api.socialbackup.MessageEncoder;
import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
import org.briarproject.briar.api.socialbackup.recovery.RestoreAccount;
import org.briarproject.briar.api.socialbackup.recovery.SecretOwnerTask;
@@ -22,6 +25,7 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.util.HashSet;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
@@ -50,6 +54,7 @@ class OwnerReturnShardViewModel extends AndroidViewModel
private final Executor ioExecutor;
private final SecretOwnerTask task;
private final RestoreAccount restoreAccount;
private final SharedPreferences prefs;
private final MutableLiveEvent<Boolean> errorTryAgain =
new MutableLiveEvent<>();
@@ -65,18 +70,26 @@ class OwnerReturnShardViewModel extends AndroidViewModel
private Bitmap qrCodeBitmap;
private WifiManager wifiManager;
private SecretKey secretKey;
private final MessageEncoder messageEncoder;
@Inject
OwnerReturnShardViewModel(Application app,
AndroidExecutor androidExecutor,
SecretOwnerTask task,
RestoreAccount restoreAccount,
@IoExecutor Executor ioExecutor) {
@IoExecutor Executor ioExecutor,
MessageEncoder messageEncoder) {
super(app);
this.androidExecutor = androidExecutor;
this.ioExecutor = ioExecutor;
this.restoreAccount = restoreAccount;
this.messageEncoder = messageEncoder;
this.task = task;
this.prefs = app.getSharedPreferences("account-recovery",
Context.MODE_PRIVATE);
restoreAccount.restoreFromPrevious(prefs.getStringSet("recovered", new HashSet<>()));
wifiManager = (WifiManager) app.getSystemService(WIFI_SERVICE);
// IntentFilter filter = new IntentFilter(ACTION_SCAN_MODE_CHANGED);
@@ -144,13 +157,6 @@ class OwnerReturnShardViewModel extends AndroidViewModel
ioExecutor.execute(() -> {
task.start(this, getWifiIpv4Address());
});
// KeyAgreementTask oldTask = task;
// KeyAgreementTask newTask = keyAgreementTaskProvider.get();
// task = newTask;
// ioExecutor.execute(() -> {
// if (oldTask != null) oldTask.stopListening();
// newTask.listen();
// });
}
@UiThread
@@ -218,16 +224,18 @@ class OwnerReturnShardViewModel extends AndroidViewModel
this.state.postValue(state);
});
} else if (state instanceof SecretOwnerTask.State.Success) {
// startClicked.setEvent(true);
this.state.postValue(state);
// TODO do same for failure
} else {
this.state.postValue(state);
}
}
public RestoreAccount.AddReturnShardPayloadResult addToShardSet(ReturnShardPayload toAdd) {
return restoreAccount.addReturnShardPayload(toAdd);
RestoreAccount.AddReturnShardPayloadResult result = restoreAccount.addReturnShardPayload(toAdd);
if (result == RestoreAccount.AddReturnShardPayloadResult.OK) {
prefs.edit().putStringSet("recovered", restoreAccount.getEncodedShards()).apply();
}
return result;
}
public boolean canRecover() {

View File

@@ -52,9 +52,11 @@ public class RestoreAccountActivity extends BaseActivity
showInitialFragment(RestoreAccountSetPasswordFragment.newInstance());
} else if (state == State.DOZE) {
showDozeFragment();
} else if (state == State.CREATED || state == State.FAILED) {
// TODO: Show an error if failed
} else if (state == State.CREATED) {
showApp();
} else { // FAILED
// TODO: Show an error if failed
finish();
}
}

View File

@@ -1,6 +1,8 @@
package org.briarproject.briar.android.socialbackup.recover;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.contact.ContactManager;
@@ -45,27 +47,27 @@ class RestoreAccountViewModel extends AndroidViewModel {
new MutableLiveData<>(false);
private final AccountManager accountManager;
private final ContactManager contactManager;
private final Executor ioExecutor;
private final PasswordStrengthEstimator strengthEstimator;
private final DozeHelper dozeHelper;
private final RestoreAccount restoreAccount;
private final SharedPreferences prefs;
@Inject
RestoreAccountViewModel(Application app,
AccountManager accountManager,
ContactManager contactManager,
RestoreAccount restoreAccount,
@IoExecutor Executor ioExecutor,
PasswordStrengthEstimator strengthEstimator,
DozeHelper dozeHelper) {
super(app);
this.accountManager = accountManager;
this.contactManager = contactManager;
this.ioExecutor = ioExecutor;
this.strengthEstimator = strengthEstimator;
this.dozeHelper = dozeHelper;
this.restoreAccount = restoreAccount;
this.prefs = app.getSharedPreferences("account-recovery",
Context.MODE_PRIVATE);
ioExecutor.execute(() -> {
if (accountManager.accountExists()) {
@@ -112,17 +114,24 @@ class RestoreAccountViewModel extends AndroidViewModel {
if (socialBackup == null) {
LOG.warning("Cannot retrieve social backup");
state.postEvent(State.FAILED);
return;
}
Identity identity = socialBackup.getIdentity();
ioExecutor.execute(() -> {
if (accountManager.restoreAccount(identity, password)) {
LOG.info("Restored account");
try {
restoreAccount.addContactsToDb();
restoreAccount.restoreAccountWhenDatabaseReady();
} catch (DbException e) {
LOG.warning("Cannot retrieve social backup");
LOG.warning("Failure processing social backup");
e.printStackTrace();
state.postEvent(State.FAILED);
return;
}
// Remove partial recovery from shared preferences
prefs.edit().clear().apply();
state.postEvent(State.CREATED);
} else {
LOG.warning("Failed to create account");

View File

@@ -31,6 +31,7 @@
android:id="@+id/action_help_recover_account"
android:icon="@drawable/introduction_white"
android:title="@string/help_recover_account"
android:enabled="false"
app:showAsAction="never"/>
<item

View File

@@ -9,7 +9,7 @@
<string name="setup_name_explanation">Your nickname will be shown next to any content you post. You can\'t change it after creating your account.</string>
<string name="setup_next">Next</string>
<string name="setup_password_intro">Choose a Password</string>
<string name="setup_password_explanation">Your Briar account is stored encrypted on your device, not in the cloud. If you forget your password or uninstall Briar, there\'s no way to recover your account.\n\nChoose a long password that\'s hard to guess, such as four random words, or ten random letters, numbers and symbols.</string>
<string name="setup_password_explanation">Your Briar account is stored encrypted on your device, not in the cloud. If you forget your password or uninstall Briar, you can only recover your account if you have made a social backup.\n\nChoose a long password that\'s hard to guess, such as four random words, or ten random letters, numbers and symbols.</string>
<string name="setup_doze_title">Background Connections</string>
<string name="setup_doze_intro">To receive messages, Briar needs to stay connected in the background.</string>
<string name="setup_doze_explanation">To receive messages, Briar needs to stay connected in the background. Please disable battery optimizations so Briar can stay connected.</string>
@@ -37,7 +37,7 @@
<string name="sign_in_button">Sign In</string>
<string name="forgotten_password">I have forgotten my password</string>
<string name="dialog_title_lost_password">Lost Password</string>
<string name="dialog_message_lost_password">Your Briar account is stored encrypted on your device, not in the cloud, so we can\'t reset your password. Would you like to delete your account and start again?\n\nCaution: Your identities, contacts and messages will be permanently lost.</string>
<string name="dialog_message_lost_password">Your Briar account is stored encrypted on your device, not in the cloud, so we can\'t reset your password. If you have made a social backup, you can delete your account and set a new password when you restore it. Would you like to delete your account and start again?\n\nCaution: Your identities, contacts and messages will be permanently lost if you do not have a backup.</string>
<string name="startup_failed_notification_title">Briar could not start</string>
<string name="startup_failed_notification_text">Tap for more information.</string>
<string name="startup_failed_activity_title">Briar Startup Failure</string>
@@ -664,7 +664,6 @@
<string name="threshold_recommended">Secure - recommended threshold</string>
<string name="threshold_low_insecure">Insecure higher threshold recommended</string>
<string name="threshold_high_insecure">Danger of loss lower threshold recommended</string>
<string name="threshold_too_few_custodians">Danger of loss - more contacts recommended</string>
<string name="threshold_defined">Choose Threshold</string>
<string name="threshold_m_of_n">%d of %d contacts needed to recover your account</string>
@@ -730,6 +729,9 @@
<string name="activity_name_custodian_help_recovery">Help recover account</string>
<string name="existing_backup_explain">%d of the following contacts are needed to restore your account:</string>
<string name="social_backup_not_enough_contacts">To make a social backup, you need at least 2 contacts in your contacts list</string>
<string name="reading_contacts_error">There was an error reading your contacts list</string>
<!-- Remote Wipe -->
<!-- setup -->