mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 04:39:54 +01:00
Improve existing backup fragment
This commit is contained in:
@@ -2,32 +2,37 @@ package org.briarproject.briar.android.socialbackup;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.text.Spannable;
|
||||||
|
import android.text.SpannableStringBuilder;
|
||||||
|
import android.text.style.ImageSpan;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.TextView;
|
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.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
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.android.fragment.BaseFragment;
|
||||||
import org.briarproject.briar.api.socialbackup.BackupMetadata;
|
import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
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();
|
public static final String TAG = ExistingBackupFragment.class.getName();
|
||||||
|
private final ContactListAdapter adapter = new ContactListAdapter(this);
|
||||||
|
private BriarRecyclerView list;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ViewModelProvider.Factory viewModelFactory;
|
ViewModelProvider.Factory viewModelFactory;
|
||||||
@@ -52,32 +57,42 @@ public class ExistingBackupFragment extends BaseFragment {
|
|||||||
@Override
|
@Override
|
||||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
|
||||||
ViewGroup container, @Nullable Bundle savedInstanceState) {
|
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,
|
View view = inflater.inflate(R.layout.fragment_existing_backup,
|
||||||
container, false);
|
container, false);
|
||||||
BackupMetadata backupMetadata = viewModel.getBackupMetadata();
|
viewModel.loadCustodianList();
|
||||||
List<Author> custodians = backupMetadata.getCustodians();
|
list = view.findViewById(R.id.custodianList);
|
||||||
|
list.setLayoutManager(new LinearLayoutManager(getActivity()));
|
||||||
|
list.setAdapter(adapter);
|
||||||
|
list.setEmptyText(R.string.no_contacts);
|
||||||
|
|
||||||
StringBuilder custodianNamesString = new StringBuilder();
|
viewModel.getContactListItems().observe(getViewLifecycleOwner(),
|
||||||
for (Author custodian : custodians) {
|
result -> result.onError(this::handleException)
|
||||||
custodianNamesString
|
.onSuccess(adapter::submitList)
|
||||||
.append("• ")
|
);
|
||||||
.append(custodian.getName())
|
|
||||||
.append("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
TextView textViewThreshold = view.findViewById(R.id.textViewThreshold);
|
int threshold = viewModel.getThresholdFromExistingBackup();
|
||||||
textViewThreshold.setText(getString(R.string.existing_backup_explain,
|
int numberOfCustodians =
|
||||||
backupMetadata.getThreshold()));
|
viewModel.getNumberOfCustodiansFromExistingBackup();
|
||||||
TextView textViewCustodians =
|
|
||||||
view.findViewById(R.id.textViewCustodians);
|
TextView mOfn = view.findViewById(R.id.textViewThreshold);
|
||||||
textViewCustodians.setText(custodianNamesString);
|
mOfn.setText(String.format(
|
||||||
|
getString(R.string.threshold_m_of_n), threshold,
|
||||||
|
numberOfCustodians));
|
||||||
|
|
||||||
|
TextView thresholdRepresentation =
|
||||||
|
view.findViewById(R.id.textViewThresholdRepresentation);
|
||||||
|
thresholdRepresentation.setText(
|
||||||
|
buildThresholdRepresentationString(threshold,
|
||||||
|
numberOfCustodians));
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAttach(Context context) {
|
public void onAttach(Context context) {
|
||||||
super.onAttach(context);
|
super.onAttach(context);
|
||||||
// listener = (ShardsSentDismissedListener) context;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -85,4 +100,27 @@ public class ExistingBackupFragment extends BaseFragment {
|
|||||||
return TAG;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import org.briarproject.briar.android.contactselection.ContactSelectorListener;
|
|||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
@@ -84,7 +85,7 @@ public class SocialBackupSetupActivity extends BriarActivity implements
|
|||||||
Toast.makeText(this,
|
Toast.makeText(this,
|
||||||
String.format("Selected %d contacts", contacts.size()),
|
String.format("Selected %d contacts", contacts.size()),
|
||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
viewModel.setCustodians(contacts);
|
viewModel.setCustodians((List<ContactId>) contacts);
|
||||||
ThresholdSelectorFragment fragment =
|
ThresholdSelectorFragment fragment =
|
||||||
ThresholdSelectorFragment.newInstance(contacts.size());
|
ThresholdSelectorFragment.newInstance(contacts.size());
|
||||||
showNextFragment(fragment);
|
showNextFragment(fragment);
|
||||||
|
|||||||
@@ -2,29 +2,37 @@ package org.briarproject.briar.android.socialbackup;
|
|||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.connection.ConnectionRegistry;
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
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.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.BackupMetadata;
|
||||||
import org.briarproject.briar.api.socialbackup.SocialBackupManager;
|
import org.briarproject.briar.api.socialbackup.SocialBackupManager;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.lifecycle.AndroidViewModel;
|
|
||||||
import androidx.lifecycle.MutableLiveData;
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
|
||||||
public class SocialBackupSetupViewModel extends AndroidViewModel {
|
public class SocialBackupSetupViewModel extends ContactsViewModel {
|
||||||
|
|
||||||
private final SocialBackupManager socialBackupManager;
|
private final SocialBackupManager socialBackupManager;
|
||||||
private final DatabaseComponent db;
|
private final DatabaseComponent db;
|
||||||
private final ContactManager contactManager;
|
private final ContactManager contactManager;
|
||||||
private BackupMetadata backupMetadata;
|
private BackupMetadata backupMetadata;
|
||||||
private Collection<ContactId> custodians;
|
private List<ContactId> custodians;
|
||||||
private int threshold;
|
private int threshold;
|
||||||
|
|
||||||
|
|
||||||
@@ -38,20 +46,39 @@ public class SocialBackupSetupViewModel extends AndroidViewModel {
|
|||||||
|
|
||||||
private final MutableLiveData<State> state =
|
private final MutableLiveData<State> state =
|
||||||
new MutableLiveData<>();
|
new MutableLiveData<>();
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public SocialBackupSetupViewModel(
|
public SocialBackupSetupViewModel(
|
||||||
@NonNull Application app,
|
@NonNull Application app,
|
||||||
DatabaseComponent db,
|
DatabaseComponent db,
|
||||||
|
@DatabaseExecutor Executor dbExecutor,
|
||||||
|
LifecycleManager lifecycleManager,
|
||||||
|
AuthorManager authorManager,
|
||||||
|
ConversationManager conversationManager,
|
||||||
|
ConnectionRegistry connectionRegistry,
|
||||||
|
EventBus eventBus,
|
||||||
|
AndroidExecutor androidExecutor,
|
||||||
SocialBackupManager socialBackupManager,
|
SocialBackupManager socialBackupManager,
|
||||||
ContactManager contactManager
|
ContactManager contactManager
|
||||||
) {
|
) {
|
||||||
super(app);
|
super(app, dbExecutor, lifecycleManager, db, androidExecutor,
|
||||||
|
contactManager, authorManager, conversationManager,
|
||||||
|
connectionRegistry, eventBus);
|
||||||
|
|
||||||
this.socialBackupManager = socialBackupManager;
|
this.socialBackupManager = socialBackupManager;
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.contactManager = contactManager;
|
this.contactManager = contactManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void loadCustodianList() {
|
||||||
|
try {
|
||||||
|
custodians = db.transactionWithResult(true,
|
||||||
|
socialBackupManager::getCustodianContactIds);
|
||||||
|
} catch (DbException e) {
|
||||||
|
custodians = new ArrayList<>();
|
||||||
|
}
|
||||||
|
loadContacts();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean haveExistingBackup() {
|
public boolean haveExistingBackup() {
|
||||||
try {
|
try {
|
||||||
backupMetadata = db.transactionWithNullableResult(true,
|
backupMetadata = db.transactionWithNullableResult(true,
|
||||||
@@ -62,15 +89,15 @@ public class SocialBackupSetupViewModel extends AndroidViewModel {
|
|||||||
return (backupMetadata != null);
|
return (backupMetadata != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BackupMetadata getBackupMetadata() {
|
// public BackupMetadata getBackupMetadata() {
|
||||||
return backupMetadata;
|
// return backupMetadata;
|
||||||
}
|
// }
|
||||||
|
|
||||||
public boolean haveEnoughContacts() throws DbException {
|
public boolean haveEnoughContacts() throws DbException {
|
||||||
return (contactManager.getContacts().size() > 1);
|
return (contactManager.getContacts().size() > 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCustodians(Collection<ContactId> contacts) {
|
public void setCustodians(List<ContactId> contacts) {
|
||||||
custodians = contacts;
|
custodians = contacts;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,5 +129,19 @@ public class SocialBackupSetupViewModel extends AndroidViewModel {
|
|||||||
public void onExplainerDismissed() {
|
public void onExplainerDismissed() {
|
||||||
state.postValue(State.CHOOSING_CUSTODIANS);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -123,7 +123,6 @@ public class ThresholdSelectorFragment extends BaseFragment {
|
|||||||
super.onAttach(context);
|
super.onAttach(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUniqueTag() {
|
public String getUniqueTag() {
|
||||||
return TAG;
|
return TAG;
|
||||||
|
|||||||
@@ -11,25 +11,35 @@
|
|||||||
android:paddingBottom="@dimen/margin_medium"
|
android:paddingBottom="@dimen/margin_medium"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/textViewThreshold"
|
android:id="@+id/textViewThresholdRepresentation"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:text="placeholder threshold"
|
android:textSize="64sp"
|
||||||
android:textSize="16sp"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/textViewCustodians"
|
android:id="@+id/textViewThreshold"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:text="@string/backup_done_info"
|
|
||||||
android:textSize="16sp"
|
android:textSize="16sp"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="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>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
@@ -719,10 +719,6 @@
|
|||||||
<string name="setup_new_account">Create new account</string>
|
<string name="setup_new_account">Create new account</string>
|
||||||
<string name="setup_restore_account">Restore account from backup</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 -->
|
<!-- activity names -->
|
||||||
<string name="activity_name_distributed_backup">Social Backup</string>
|
<string name="activity_name_distributed_backup">Social Backup</string>
|
||||||
<string name="activity_name_restore_account">Restore Account</string>
|
<string name="activity_name_restore_account">Restore Account</string>
|
||||||
@@ -738,4 +734,7 @@
|
|||||||
|
|
||||||
<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="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>
|
<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>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user