improve thresholdSelectorFragment

This commit is contained in:
ameba23
2021-03-08 12:31:32 +01:00
parent 033c9f4d59
commit 085e25cc14

View File

@@ -27,6 +27,7 @@ import static java.util.Objects.requireNonNull;
public class ThresholdSelectorFragment extends BaseFragment { public class ThresholdSelectorFragment extends BaseFragment {
public static final String TAG = ThresholdSelectorFragment.class.getName(); public static final String TAG = ThresholdSelectorFragment.class.getName();
private static final String NUMBER_CUSTODIANS = "numberCustodians";
protected ThresholdDefinedListener listener; protected ThresholdDefinedListener listener;
@@ -38,10 +39,10 @@ public class ThresholdSelectorFragment extends BaseFragment {
public static ThresholdSelectorFragment newInstance(int numberCustodians) { public static ThresholdSelectorFragment newInstance(int numberCustodians) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putInt("numberCustodians", numberCustodians); bundle.putInt(NUMBER_CUSTODIANS, numberCustodians);
ThresholdSelectorFragment fragment = new ThresholdSelectorFragment(); ThresholdSelectorFragment fragment = new ThresholdSelectorFragment();
fragment.setArguments(bundle); fragment.setArguments(bundle);
fragment.setNumberCustodians(numberCustodians); // fragment.setNumberCustodians(numberCustodians);
return fragment; return fragment;
} }
@@ -60,19 +61,24 @@ public class ThresholdSelectorFragment extends BaseFragment {
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_select_threshold, View view = inflater.inflate(R.layout.fragment_select_threshold,
container, false); container, false);
if (savedInstanceState != null) { // if (savedInstanceState != null) {
numberOfCustodians = savedInstanceState.getInt("numberCustodians"); // numberOfCustodians = savedInstanceState.getInt("numberCustodians");
} // }
Bundle args = requireArguments();
numberOfCustodians = args.getInt(NUMBER_CUSTODIANS);
seekBar = view.findViewById(R.id.seekBar); seekBar = view.findViewById(R.id.seekBar);
thresholdRepresentation = view.findViewById(R.id.textViewThresholdRepresentation); thresholdRepresentation = view.findViewById(R.id.textViewThresholdRepresentation);
message = view.findViewById(R.id.textViewMessage); message = view.findViewById(R.id.textViewMessage);
int max = numberOfCustodians - 3;
seekBar.setMax(2); seekBar.setMax(max);
seekBar.setOnSeekBarChangeListener(new SeekBarListener()); seekBar.setOnSeekBarChangeListener(new SeekBarListener());
seekBar.setProgress(1); int defaultThreshold = SecretSharingWrapper.defaultThreshold(numberOfCustodians);
seekBar.setProgress(defaultThreshold - 2);
thresholdRepresentation.setText(buildThresholdRepresentationString(3)); thresholdRepresentation.setText(buildThresholdRepresentationString(defaultThreshold));
return view; return view;
// return super.onCreateView(inflater, container, savedInstanceState);
} }
@Override @Override
@@ -112,12 +118,12 @@ public class ThresholdSelectorFragment extends BaseFragment {
private String buildThresholdRepresentationString (int threshold) { private String buildThresholdRepresentationString (int threshold) {
String thresholdRepresentationText = ""; String thresholdRepresentationText = "";
for (int i = 0; i < threshold; i++) { for (int i = 0; i < threshold; i++) {
// thresholdRepresentationText += R.string.filled_bullet; thresholdRepresentationText += getString(R.string.filled_bullet);
thresholdRepresentationText += "1"; // thresholdRepresentationText += "1 ";
} }
for (int i = 0; i < (numberOfCustodians - threshold); i++) { for (int i = 0; i < (numberOfCustodians - threshold); i++) {
// thresholdRepresentationText += R.string.linear_bullet; thresholdRepresentationText += getString(R.string.linear_bullet);
thresholdRepresentationText += "0"; // thresholdRepresentationText += "0 ";
} }
return thresholdRepresentationText; return thresholdRepresentationText;
} }
@@ -127,8 +133,6 @@ public class ThresholdSelectorFragment extends BaseFragment {
@Override @Override
public void onProgressChanged(SeekBar seekBar, int progress, public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) { boolean fromUser) {
// progress can be 0, 1, 2 - translate allowed slider value to actual
// threshold
int threshold = progress + 2; int threshold = progress + 2;
thresholdRepresentation.setText( thresholdRepresentation.setText(
@@ -137,8 +141,9 @@ public class ThresholdSelectorFragment extends BaseFragment {
int sanityLevel = SecretSharingWrapper.thresholdSanity(threshold, numberOfCustodians); int sanityLevel = SecretSharingWrapper.thresholdSanity(threshold, numberOfCustodians);
int text = R.string.threshold_secure; int text = R.string.threshold_secure;
if (sanityLevel < -1) text = R.string.threshold_low_insecure; // TODO use 1 / -1 instead of 0
if (sanityLevel > 1) text = R.string.threshold_high_insecure; if (sanityLevel < 0) text = R.string.threshold_low_insecure;
if (sanityLevel > 0) text = R.string.threshold_high_insecure;
message.setText(text); message.setText(text);
// TODO change colour of thresholdRepresentation to green/red based on sanityLevel // TODO change colour of thresholdRepresentation to green/red based on sanityLevel
} }