From 2486a60feae4bb0714bf4b1accdf57b9eafc2898 Mon Sep 17 00:00:00 2001 From: ameba23 Date: Mon, 22 Mar 2021 17:03:22 +0100 Subject: [PATCH] only display custodian help recover explainer screen if you are a custodian --- .../CustodianHelpRecoverActivity.java | 45 +++- .../ThresholdSelectorFragment.java | 247 +++++++++--------- 2 files changed, 165 insertions(+), 127 deletions(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/socialbackup/CustodianHelpRecoverActivity.java b/briar-android/src/main/java/org/briarproject/briar/android/socialbackup/CustodianHelpRecoverActivity.java index cd6a7a2a6..9c48693c6 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/socialbackup/CustodianHelpRecoverActivity.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/socialbackup/CustodianHelpRecoverActivity.java @@ -1,11 +1,22 @@ package org.briarproject.briar.android.socialbackup; +import android.content.Intent; import android.os.Bundle; +import android.widget.Toast; +import org.briarproject.bramble.api.contact.ContactId; +import org.briarproject.bramble.api.db.DatabaseComponent; +import org.briarproject.bramble.api.db.DbException; import org.briarproject.briar.R; import org.briarproject.briar.android.activity.ActivityComponent; import org.briarproject.briar.android.activity.BriarActivity; import org.briarproject.briar.android.fragment.BaseFragment; +import org.briarproject.briar.api.socialbackup.BackupMetadata; +import org.briarproject.briar.api.socialbackup.SocialBackupManager; + +import javax.inject.Inject; + +import static org.briarproject.briar.android.conversation.ConversationActivity.CONTACT_ID; public class CustodianHelpRecoverActivity extends BriarActivity implements BaseFragment.BaseFragmentListener, CustodianScanQrButtonListener { @@ -14,15 +25,39 @@ public class CustodianHelpRecoverActivity extends BriarActivity implements component.inject(this); } + @Inject + public SocialBackupManager socialBackupManager; + + @Inject + public DatabaseComponent db; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recover); // TODO change this - // TODO check if we have a shard for this secret owner - // if not we should not even display the menu item - CustodianRecoveryModeExplainerFragment fragment = - new CustodianRecoveryModeExplainerFragment(); - showInitialFragment(fragment); + + Intent intent = getIntent(); + int id = intent.getIntExtra(CONTACT_ID, -1); + if (id == -1) throw new IllegalStateException("No ContactId"); + ContactId contactId = new ContactId(id); + + // check if we have a shard for this secret owner + try { + db.transaction(false, txn -> { + if (!socialBackupManager.amCustodian(txn, contactId)) { + throw new DbException(); + } + CustodianRecoveryModeExplainerFragment fragment = + new CustodianRecoveryModeExplainerFragment(); + showInitialFragment(fragment); + }); + } catch (DbException e) { + // TODO improve this + Toast.makeText(this, + "You do not hold a backup shard from this contact", + Toast.LENGTH_SHORT).show(); + finish(); + } } @Override diff --git a/briar-android/src/main/java/org/briarproject/briar/android/socialbackup/ThresholdSelectorFragment.java b/briar-android/src/main/java/org/briarproject/briar/android/socialbackup/ThresholdSelectorFragment.java index 51c229508..74d8f0569 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/socialbackup/ThresholdSelectorFragment.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/socialbackup/ThresholdSelectorFragment.java @@ -8,162 +8,165 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.ImageView; import android.widget.SeekBar; import android.widget.TextView; -import android.widget.Toast; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import org.briarproject.bramble.api.db.DbException; import org.briarproject.briar.R; import org.briarproject.briar.android.activity.ActivityComponent; -import org.briarproject.briar.android.contactselection.ContactSelectorListener; import org.briarproject.briar.android.fragment.BaseFragment; import org.magmacollective.darkcrystal.secretsharingwrapper.SecretSharingWrapper; -import static java.util.Objects.requireNonNull; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; public class ThresholdSelectorFragment extends BaseFragment { - public static final String TAG = ThresholdSelectorFragment.class.getName(); - private static final String NUMBER_CUSTODIANS = "numberCustodians"; + public static final String TAG = ThresholdSelectorFragment.class.getName(); + private static final String NUMBER_CUSTODIANS = "numberCustodians"; - protected ThresholdDefinedListener listener; + protected ThresholdDefinedListener listener; - // TODO this should be the actual number of custodians - private int numberOfCustodians; - private int threshold; - private int recommendedThreshold; - private SeekBar seekBar; - private TextView thresholdRepresentation; - private TextView message; - private TextView mOfn; + // TODO this should be the actual number of custodians + private int numberOfCustodians; + private int threshold; + private int recommendedThreshold; + private SeekBar seekBar; + private TextView thresholdRepresentation; + private TextView message; + private TextView mOfn; - public static ThresholdSelectorFragment newInstance(int numberCustodians) { - Bundle bundle = new Bundle(); - bundle.putInt(NUMBER_CUSTODIANS, numberCustodians); - ThresholdSelectorFragment fragment = new ThresholdSelectorFragment(); - fragment.setArguments(bundle); - return fragment; - } + public static ThresholdSelectorFragment newInstance(int numberCustodians) { + Bundle bundle = new Bundle(); + bundle.putInt(NUMBER_CUSTODIANS, numberCustodians); + ThresholdSelectorFragment fragment = new ThresholdSelectorFragment(); + fragment.setArguments(bundle); + return fragment; + } - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - requireActivity().setTitle(R.string.title_define_threshold); - } + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + requireActivity().setTitle(R.string.title_define_threshold); + } - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.fragment_select_threshold, - container, false); - Bundle args = requireArguments(); - numberOfCustodians = args.getInt(NUMBER_CUSTODIANS); + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, + @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_select_threshold, + container, false); + Bundle args = requireArguments(); + numberOfCustodians = args.getInt(NUMBER_CUSTODIANS); - seekBar = view.findViewById(R.id.seekBar); - thresholdRepresentation = view.findViewById(R.id.textViewThresholdRepresentation); - message = view.findViewById(R.id.textViewMessage); - mOfn = view.findViewById(R.id.textViewmOfn); - int max = numberOfCustodians - 3; - seekBar.setMax(max); - seekBar.setOnSeekBarChangeListener(new SeekBarListener()); - recommendedThreshold = SecretSharingWrapper.defaultThreshold(numberOfCustodians); - threshold = recommendedThreshold; - seekBar.setProgress(threshold - 2); + seekBar = view.findViewById(R.id.seekBar); + thresholdRepresentation = + view.findViewById(R.id.textViewThresholdRepresentation); + message = view.findViewById(R.id.textViewMessage); + mOfn = view.findViewById(R.id.textViewmOfn); + int max = numberOfCustodians - 3; + seekBar.setMax(max); + seekBar.setOnSeekBarChangeListener(new SeekBarListener()); + recommendedThreshold = + SecretSharingWrapper.defaultThreshold(numberOfCustodians); + threshold = recommendedThreshold; + seekBar.setProgress(threshold - 2); - thresholdRepresentation.setText(buildThresholdRepresentationString()); - setmOfnText(); - return view; + thresholdRepresentation.setText(buildThresholdRepresentationString()); + setmOfnText(); + return view; // return super.onCreateView(inflater, container, savedInstanceState); - } + } - private void setmOfnText() { - mOfn.setText(String.format("%d of %d contacts needed to recover your account", threshold, numberOfCustodians)); - } + private void setmOfnText() { + mOfn.setText(String.format( + "%d of %d contacts needed to recover your account", threshold, + numberOfCustodians)); + } - @Override - public void onAttach(Context context) { - super.onAttach(context); - listener = (ThresholdDefinedListener) context; - } + @Override + public void onAttach(Context context) { + super.onAttach(context); + listener = (ThresholdDefinedListener) context; + } - @Override - public String getUniqueTag() { - return TAG; - } + @Override + public String getUniqueTag() { + return TAG; + } - @Override - public void injectFragment(ActivityComponent component) { - component.inject(this); - } + @Override + public void injectFragment(ActivityComponent component) { + component.inject(this); + } - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - inflater.inflate(R.menu.define_threshold_actions, menu); - super.onCreateOptionsMenu(menu, inflater); - } + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + inflater.inflate(R.menu.define_threshold_actions, menu); + super.onCreateOptionsMenu(menu, inflater); + } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.action_threshold_defined: - try { - listener.thresholdDefined(threshold); - } catch (DbException e) { - e.printStackTrace(); - } - return true; - default: - return super.onOptionsItemSelected(item); - } - } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.action_threshold_defined: + try { + listener.thresholdDefined(threshold); + } catch (DbException e) { + e.printStackTrace(); + } + return true; + default: + return super.onOptionsItemSelected(item); + } + } - private String buildThresholdRepresentationString () { - String thresholdRepresentationText = ""; - for (int i = 0; i < threshold; i++) { - thresholdRepresentationText += getString(R.string.filled_bullet); - } - for (int i = 0; i < (numberOfCustodians - threshold); i++) { - thresholdRepresentationText += getString(R.string.linear_bullet); - } - return thresholdRepresentationText; - } + private String buildThresholdRepresentationString() { + String thresholdRepresentationText = ""; + for (int i = 0; i < threshold; i++) { + thresholdRepresentationText += getString(R.string.filled_bullet); + } + for (int i = 0; i < (numberOfCustodians - threshold); i++) { + thresholdRepresentationText += getString(R.string.linear_bullet); + } + return thresholdRepresentationText; + } - private class SeekBarListener implements SeekBar.OnSeekBarChangeListener { + private class SeekBarListener implements SeekBar.OnSeekBarChangeListener { - @Override - public void onProgressChanged(SeekBar seekBar, int progress, - boolean fromUser) { - threshold = progress + 2; + @Override + public void onProgressChanged(SeekBar seekBar, int progress, + boolean fromUser) { + threshold = progress + 2; - thresholdRepresentation.setText( - buildThresholdRepresentationString() - ); - setmOfnText(); + thresholdRepresentation.setText( + buildThresholdRepresentationString() + ); + setmOfnText(); - int sanityLevel = SecretSharingWrapper.thresholdSanity(threshold, numberOfCustodians); - int text = R.string.threshold_secure; - if (threshold == recommendedThreshold) text = R.string.threshold_recommended; - if (sanityLevel < -1) text = R.string.threshold_low_insecure; - if (sanityLevel > 0) text = R.string.threshold_high_insecure; - message.setText(text); - // TODO change colour of thresholdRepresentation to green/red based on sanityLevel - } + int sanityLevel = SecretSharingWrapper + .thresholdSanity(threshold, numberOfCustodians); + int text = R.string.threshold_secure; + if (threshold == recommendedThreshold) + text = R.string.threshold_recommended; + if (sanityLevel < -1) text = R.string.threshold_low_insecure; + if (sanityLevel > 0) text = R.string.threshold_high_insecure; + message.setText(text); + // TODO change colour of thresholdRepresentation to green/red based on sanityLevel + } - @Override - public void onStartTrackingTouch(SeekBar seekBar) { - // do nothing - } + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + // do nothing + } - @Override - public void onStopTrackingTouch(SeekBar seekBar) { - // do nothing - } + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + // do nothing + } - } + } }