Resolve merge conflict with social-backup-poc

This commit is contained in:
ameba23
2022-03-04 10:13:20 +01:00
8 changed files with 175 additions and 51 deletions

View File

@@ -2,32 +2,37 @@ package org.briarproject.briar.android.socialbackup;
import android.content.Context;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.identity.Author;
import org.briarproject.briar.R;
import org.briarproject.briar.android.activity.ActivityComponent;
import org.briarproject.briar.android.contact.ContactListAdapter;
import org.briarproject.briar.android.contact.ContactListItem;
import org.briarproject.briar.android.contact.OnContactClickListener;
import org.briarproject.briar.android.fragment.BaseFragment;
import org.briarproject.briar.api.socialbackup.BackupMetadata;
import org.briarproject.briar.android.view.BriarRecyclerView;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import javax.inject.Inject;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
public class ExistingBackupFragment extends BaseFragment {
public class ExistingBackupFragment extends BaseFragment implements
OnContactClickListener<ContactListItem> {
private static final String THRESHOLD = "threshold";
private static final String CUSTODIANS = "custodians";
public static final String TAG = ExistingBackupFragment.class.getName();
private final ContactListAdapter adapter = new ContactListAdapter(this);
private BriarRecyclerView list;
@Inject
ViewModelProvider.Factory viewModelFactory;
@@ -52,32 +57,41 @@ public class ExistingBackupFragment extends BaseFragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
// change toolbar text (relevant when navigating back to this fragment)
// requireActivity().setTitle(R.string.social_backup_trusted_contacts);
View view = inflater.inflate(R.layout.fragment_existing_backup,
container, false);
BackupMetadata backupMetadata = viewModel.getBackupMetadata();
List<Author> custodians = backupMetadata.getCustodians();
viewModel.loadCustodianList();
list = view.findViewById(R.id.custodianList);
list.setLayoutManager(new LinearLayoutManager(getActivity()));
list.setAdapter(adapter);
list.setEmptyText(R.string.no_contacts);
StringBuilder custodianNamesString = new StringBuilder();
for (Author custodian : custodians) {
custodianNamesString
.append("")
.append(custodian.getName())
.append("\n");
}
viewModel.getContactListItems().observe(getViewLifecycleOwner(),
result -> result.onError(this::handleException)
.onSuccess(adapter::submitList)
);
TextView textViewThreshold = view.findViewById(R.id.textViewThreshold);
textViewThreshold.setText(getString(R.string.existing_backup_explain,
backupMetadata.getThreshold()));
TextView textViewCustodians =
view.findViewById(R.id.textViewCustodians);
textViewCustodians.setText(custodianNamesString);
int threshold = viewModel.getThresholdFromExistingBackup();
int numberOfCustodians =
viewModel.getNumberOfCustodiansFromExistingBackup();
TextView mOfn = view.findViewById(R.id.textViewThreshold);
mOfn.setText(String.format(
getString(R.string.existing_backup_explain), threshold));
TextView thresholdRepresentation =
view.findViewById(R.id.textViewThresholdRepresentation);
thresholdRepresentation.setText(
buildThresholdRepresentationString(threshold,
numberOfCustodians));
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
// listener = (ShardsSentDismissedListener) context;
}
@Override
@@ -85,4 +99,27 @@ public class ExistingBackupFragment extends BaseFragment {
return TAG;
}
@Override
public void onItemClick(View view, ContactListItem item) {
}
private SpannableStringBuilder buildThresholdRepresentationString(
int threshold, int numberOfCustodians) {
char[] charArray = new char[numberOfCustodians];
Arrays.fill(charArray, ' ');
SpannableStringBuilder string =
new SpannableStringBuilder(new String(charArray));
for (int i = 0; i < numberOfCustodians; i++) {
int drawable = i < threshold
? R.drawable.ic_custodian_required
: R.drawable.ic_custodian_optional;
string.setSpan(new ImageSpan(getContext(), drawable), i,
i + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
// If we have more than 6, split it on two lines
if (numberOfCustodians > 6) string.insert(4, "\n");
return string;
}
}

View File

@@ -12,6 +12,7 @@ import org.briarproject.briar.android.contactselection.ContactSelectorListener;
import org.briarproject.briar.android.fragment.BaseFragment;
import java.util.Collection;
import java.util.List;
import javax.inject.Inject;
@@ -84,7 +85,7 @@ public class SocialBackupSetupActivity extends BriarActivity implements
Toast.makeText(this,
String.format("Selected %d contacts", contacts.size()),
Toast.LENGTH_SHORT).show();
viewModel.setCustodians(contacts);
viewModel.setCustodians((List<ContactId>) contacts);
ThresholdSelectorFragment fragment =
ThresholdSelectorFragment.newInstance(contacts.size());
showNextFragment(fragment);

View File

@@ -2,29 +2,37 @@ package org.briarproject.briar.android.socialbackup;
import android.app.Application;
import org.briarproject.bramble.api.connection.ConnectionRegistry;
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.DatabaseExecutor;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.system.AndroidExecutor;
import org.briarproject.briar.android.contact.ContactsViewModel;
import org.briarproject.briar.api.conversation.ConversationManager;
import org.briarproject.briar.api.identity.AuthorManager;
import org.briarproject.briar.api.socialbackup.BackupMetadata;
import org.briarproject.briar.api.socialbackup.SocialBackupManager;
import java.util.Collection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
public class SocialBackupSetupViewModel extends AndroidViewModel {
public class SocialBackupSetupViewModel extends ContactsViewModel {
private final SocialBackupManager socialBackupManager;
private final DatabaseComponent db;
private final ContactManager contactManager;
private BackupMetadata backupMetadata;
private Collection<ContactId> custodians;
private List<ContactId> custodians;
private int threshold;
@@ -38,20 +46,39 @@ public class SocialBackupSetupViewModel extends AndroidViewModel {
private final MutableLiveData<State> state =
new MutableLiveData<>();
@Inject
public SocialBackupSetupViewModel(
@NonNull Application app,
DatabaseComponent db,
@DatabaseExecutor Executor dbExecutor,
LifecycleManager lifecycleManager,
AuthorManager authorManager,
ConversationManager conversationManager,
ConnectionRegistry connectionRegistry,
EventBus eventBus,
AndroidExecutor androidExecutor,
SocialBackupManager socialBackupManager,
ContactManager contactManager
) {
super(app);
super(app, dbExecutor, lifecycleManager, db, androidExecutor,
contactManager, authorManager, conversationManager,
connectionRegistry, eventBus);
this.socialBackupManager = socialBackupManager;
this.db = db;
this.contactManager = contactManager;
}
public void loadCustodianList() {
try {
custodians = db.transactionWithResult(true,
socialBackupManager::getCustodianContactIds);
} catch (DbException e) {
custodians = new ArrayList<>();
}
loadContacts();
}
public boolean haveExistingBackup() {
try {
backupMetadata = db.transactionWithNullableResult(true,
@@ -62,15 +89,15 @@ public class SocialBackupSetupViewModel extends AndroidViewModel {
return (backupMetadata != null);
}
public BackupMetadata getBackupMetadata() {
return backupMetadata;
}
// public BackupMetadata getBackupMetadata() {
// return backupMetadata;
// }
public boolean haveEnoughContacts() throws DbException {
return (contactManager.getContacts().size() > 1);
}
public void setCustodians(Collection<ContactId> contacts) {
public void setCustodians(List<ContactId> contacts) {
custodians = contacts;
}
@@ -102,5 +129,19 @@ public class SocialBackupSetupViewModel extends AndroidViewModel {
public void onExplainerDismissed() {
state.postValue(State.CHOOSING_CUSTODIANS);
}
@Override
protected boolean displayContact(ContactId contactId) {
// Check if contact holds a backup piece
return custodians.contains(contactId);
}
public int getThresholdFromExistingBackup() {
return backupMetadata.getThreshold();
}
public int getNumberOfCustodiansFromExistingBackup() {
return backupMetadata.getCustodians().size();
}
}

View File

@@ -123,7 +123,6 @@ public class ThresholdSelectorFragment extends BaseFragment {
super.onAttach(context);
}
@Override
public String getUniqueTag() {
return TAG;

View File

@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
@@ -11,25 +10,35 @@
android:paddingBottom="@dimen/margin_medium"
android:layout_height="match_parent">
<TextView
android:id="@+id/textViewThreshold"
android:id="@+id/textViewThresholdRepresentation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="placeholder threshold"
android:textSize="16sp"
android:textSize="64sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewCustodians"
android:id="@+id/textViewThreshold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/backup_done_info"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewThreshold" />
app:layout_constraintTop_toBottomOf="@+id/textViewThresholdRepresentation" />
<org.briarproject.briar.android.view.BriarRecyclerView
android:id="@+id/custodianList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textViewThreshold"
app:scrollToEnd="false" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -650,18 +650,19 @@
<string name="pref_distributed_old_backup_summary">Information about your most recent social backup</string>
<!-- Social backup setup -->
<string name="select_custodians">Select Trusted Contacts:</string>
<string name="select_custodians">Select Trusted Contacts</string>
<string name="select_at_least_n_contacts">Please select at least %d contacts</string>
<string name="select_at_least_n_more_contacts">Please select at least %d more contacts</string>
<string name="select_no_more_than_n_contacts">Too many! Please select no more than %d contacts</string>
<string name="threshold">Choose the minimum number of trusted contacts needed to restore your account</string>
<string name="threshold">Set the minimum number of trusted contacts needed</string>
<string name="threshold_disabled">Two trusted contacts will be needed to restore your account</string>
<string name="threshold_secure">Secure</string>
<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_defined">Choose Threshold</string>
<string name="threshold_defined">Choose Minimum Needed</string>
<string name="threshold_m_of_n">%d of %d contacts needed to recover your account</string>
<!-- Recovery from the secret owner's POV -->
@@ -702,7 +703,7 @@
<!-- Titles for the app bar -->
<string name="title_distributed_backup">Social Backup</string>
<string name="title_select_custodians">Select Trusted Contacts</string>
<string name="title_define_threshold">Threshold</string>
<string name="title_define_threshold">Minimum Needed</string>
<string name="title_recovery_mode">Recovery Mode</string>
<string name="title_help_recover">Help recover account</string>
@@ -713,11 +714,9 @@
<string name="setup_new_account">Create new account</string>
<string name="setup_restore_account">Restore account from backup</string>
<!-- Symbols for visualising threshold values for social backup -->
<string name="filled_bullet">\u25CF</string>
<string name="linear_bullet">\u25CB</string>
<!-- Activity names -->
<string name="activity_name_distributed_backup">Social Backup</string>
<string name="activity_name_restore_account">Restore Account</string>
@@ -728,11 +727,15 @@
<string name="activity_name_new_or_recover_account">Create new account or recover existing account</string>
<string name="activity_name_recovery">Recover Account</string>
<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="existing_backup_explain">Any %d of the following contacts are needed to restore your Briar account, should you lose access to it.</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>
<!-- Display existing backup -->
<string name="social_backup_trusted_contacts">Trusted Contacts</string>
<!-- Remote Wipe Feature -->
<!-- Setup -->
@@ -780,4 +783,5 @@
<string name="remote_wipe_confirm_received">Briar data has been wiped from %1$s\'s device. They will be unreachable until they recover their account.</string>
<string name="button_confirm">Ok</string>
</resources>

View File

@@ -78,4 +78,11 @@ public interface SocialBackupManager extends
*/
byte[] getReturnShardPayloadBytes(Transaction txn, ContactId contactId)
throws DbException;
/**
* Get a list of the contact ids of your custodians, or an empty
* list if no backup exists.
*/
List<ContactId> getCustodianContactIds(Transaction txn);
}

View File

@@ -613,4 +613,30 @@ class SocialBackupManagerImpl extends ConversationClientImpl
results.entrySet().iterator().next();
return new Pair<>(e.getKey(), e.getValue());
}
public List<ContactId> getCustodianContactIds(Transaction txn) {
ArrayList<ContactId> contactIds = new ArrayList<>();
try {
BackupMetadata b = getBackupMetadata(txn);
if (b == null) throw new DbException();
List<Author> custodians = b.getCustodians();
for (Author custodian : custodians) {
contactIds.add(authorToContactId(txn, custodian));
}
} catch (DbException ignored) {
// Will return an empty list
}
return contactIds;
}
private ContactId authorToContactId(Transaction txn, Author author)
throws DbException {
ArrayList<Contact> contacts =
(ArrayList<Contact>) contactManager.getContacts(txn);
for (Contact c : contacts) {
if (c.getAuthor().equals(author)) return c.getId();
}
throw new DbException();
}
}