fix preselecting multiple encryption recipients

This commit is contained in:
Vincent Breitmoser
2018-07-04 16:35:11 +02:00
parent 5c13b21577
commit 69fb637a70
4 changed files with 50 additions and 79 deletions

View File

@@ -51,15 +51,14 @@ import timber.log.Timber;
public class EncryptModeAsymmetricFragment extends EncryptModeFragment { public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
public static final String ARG_SINGATURE_KEY_ID = "signature_key_id";
public static final String ARG_ENCRYPTION_KEY_IDS = "encryption_key_ids";
KeyRepository keyRepository; KeyRepository keyRepository;
private KeySpinner mSignKeySpinner; private KeySpinner mSignKeySpinner;
private EncryptRecipientChipsInput mEncryptKeyView; private EncryptRecipientChipsInput mEncryptKeyView;
public static final String ARG_SINGATURE_KEY_ID = "signature_key_id";
public static final String ARG_ENCRYPTION_KEY_IDS = "encryption_key_ids";
public static EncryptModeAsymmetricFragment newInstance(long signatureKey, long[] encryptionKeyIds) { public static EncryptModeAsymmetricFragment newInstance(long signatureKey, long[] encryptionKeyIds) {
EncryptModeAsymmetricFragment frag = new EncryptModeAsymmetricFragment(); EncryptModeAsymmetricFragment frag = new EncryptModeAsymmetricFragment();
@@ -131,12 +130,19 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
// preselect keys given, from state or arguments // preselect keys given, from state or arguments
if (savedInstanceState == null) { if (savedInstanceState == null) {
Long signatureKeyId = getArguments().getLong(ARG_SINGATURE_KEY_ID); Bundle arguments = getArguments();
if (signatureKeyId == Constants.key.none) { preselectKeysFromArguments(arguments);
signatureKeyId = null; }
} }
long[] encryptionKeyIds = getArguments().getLongArray(ARG_ENCRYPTION_KEY_IDS);
preselectKeys(signatureKeyId, encryptionKeyIds); private void preselectKeysFromArguments(Bundle arguments) {
long preselectedSignatureKeyId = arguments.getLong(ARG_SINGATURE_KEY_ID);
if (preselectedSignatureKeyId != Constants.key.none) {
mSignKeySpinner.setPreSelectedKeyId(preselectedSignatureKeyId);
}
long[] preselectedEncryptionKeyIds = arguments.getLongArray(ARG_ENCRYPTION_KEY_IDS);
if (preselectedEncryptionKeyIds != null) {
mEncryptKeyView.setPreSelectedKeyIds(preselectedEncryptionKeyIds);
} }
} }
@@ -171,38 +177,6 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
} }
} }
/**
* If an Intent gives a signatureMasterKeyId and/or encryptionMasterKeyIds, preselect those!
*/
private void preselectKeys(Long signatureKeyId, long[] encryptionKeyIds) {
if (signatureKeyId != null) {
UnifiedKeyInfo unifiedKeyInfo = keyRepository.getUnifiedKeyInfo(signatureKeyId);
if (unifiedKeyInfo == null) {
String beautifyKeyId = KeyFormattingUtils.beautifyKeyId(signatureKeyId);
Notify.create(getActivity(), getString(R.string.error_preselect_sign_key, beautifyKeyId), Style.ERROR).show();
} else if (unifiedKeyInfo.has_any_secret()) {
mSignKeySpinner.setPreSelectedKeyId(signatureKeyId);
}
}
if (encryptionKeyIds != null) {
for (long preselectedId : encryptionKeyIds) {
UnifiedKeyInfo keyInfo = keyRepository.getUnifiedKeyInfo(preselectedId);
if (keyInfo == null) {
Timber.e("key not found for encryption!");
Notify.create(getActivity(), getString(R.string.error_preselect_encrypt_key,
KeyFormattingUtils.beautifyKeyId(preselectedId)),
Style.ERROR).show();
} else {
mEncryptKeyView.addChip(EncryptRecipientChipsInput.chipFromUnifiedKeyInfo(keyInfo));
}
}
// This is to work-around a rendering bug in TokenCompleteTextView
mEncryptKeyView.requestFocus();
}
}
@Override @Override
public boolean isAsymmetric() { public boolean isAsymmetric() {
return true; return true;

View File

@@ -1,6 +1,8 @@
package org.sufficientlysecure.keychain.ui.chips; package org.sufficientlysecure.keychain.ui.chips;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import android.content.Context; import android.content.Context;
@@ -13,6 +15,8 @@ import org.sufficientlysecure.keychain.ui.chips.EncryptRecipientChipsInput.Encry
public class EncryptRecipientChipsInput extends ChipsInput<EncryptRecipientChip> { public class EncryptRecipientChipsInput extends ChipsInput<EncryptRecipientChip> {
private long[] preselectedKeyIds;
public EncryptRecipientChipsInput(Context context) { public EncryptRecipientChipsInput(Context context) {
super(context); super(context);
init(); init();
@@ -31,6 +35,22 @@ public class EncryptRecipientChipsInput extends ChipsInput<EncryptRecipientChip>
public void setData(List<EncryptRecipientChip> keyInfoChips) { public void setData(List<EncryptRecipientChip> keyInfoChips) {
EncryptRecipientDropdownAdapter chipDropdownAdapter = new EncryptRecipientDropdownAdapter(getContext(), keyInfoChips); EncryptRecipientDropdownAdapter chipDropdownAdapter = new EncryptRecipientDropdownAdapter(getContext(), keyInfoChips);
setChipDropdownAdapter(chipDropdownAdapter); setChipDropdownAdapter(chipDropdownAdapter);
if (preselectedKeyIds != null) {
Arrays.sort(preselectedKeyIds);
ArrayList<EncryptRecipientChip> preselectedChips = new ArrayList<>();
for (EncryptRecipientChip keyInfoChip : keyInfoChips) {
if (Arrays.binarySearch(preselectedKeyIds, keyInfoChip.keyInfo.master_key_id()) >= 0) {
preselectedChips.add(keyInfoChip);
}
}
addChips(preselectedChips);
preselectedKeyIds = null;
}
}
public void setPreSelectedKeyIds(long[] preselectedEncryptionKeyIds) {
this.preselectedKeyIds = preselectedEncryptionKeyIds;
} }
public static class EncryptRecipientChip implements FilterableItem { public static class EncryptRecipientChip implements FilterableItem {

View File

@@ -45,7 +45,6 @@ public abstract class ChipsInput<T extends FilterableItem> extends ScrollViewMax
private boolean mShowChipDetailed = true; private boolean mShowChipDetailed = true;
private List<ChipsListener<T>> mChipsListenerList = new ArrayList<>(); private List<ChipsListener<T>> mChipsListenerList = new ArrayList<>();
private ChipValidator<T> mChipValidator;
private ChipsAdapter<T, ?> chipsAdapter; private ChipsAdapter<T, ?> chipsAdapter;
private RecyclerView chipsRecyclerView; private RecyclerView chipsRecyclerView;
@@ -187,8 +186,8 @@ public abstract class ChipsInput<T extends FilterableItem> extends ScrollViewMax
}); });
} }
public void addChip(T chip) { public void addChips(List<T> chips) {
chipsAdapter.addChip(chip); chipsAdapter.addChipsProgrammatically(chips);
} }
public ChipsInputEditText getEditText() { public ChipsInputEditText getEditText() {
@@ -317,14 +316,6 @@ public abstract class ChipsInput<T extends FilterableItem> extends ScrollViewMax
})); }));
} }
public ChipValidator<T> getChipValidator() {
return mChipValidator;
}
public void setChipValidator(ChipValidator<T> mChipValidator) {
this.mChipValidator = mChipValidator;
}
public interface ChipsListener<T extends FilterableItem> { public interface ChipsListener<T extends FilterableItem> {
void onChipAdded(T chip, int newSize); void onChipAdded(T chip, int newSize);
void onChipRemoved(T chip, int newSize); void onChipRemoved(T chip, int newSize);
@@ -338,8 +329,4 @@ public abstract class ChipsInput<T extends FilterableItem> extends ScrollViewMax
public void onTextChanged(CharSequence text) { } public void onTextChanged(CharSequence text) { }
public void onActionDone(CharSequence text) { } public void onActionDone(CharSequence text) { }
} }
public interface ChipValidator<T extends FilterableItem> {
boolean areEquals(T chip1, T chip2);
}
} }

View File

@@ -202,17 +202,19 @@ public abstract class ChipsAdapter<T extends FilterableItem, VH extends Recycler
} }
public void addChip(T chip) { public void addChip(T chip) {
if (!listContains(chipList, chip)) { if (chipList.contains(chip)) {
chipList.add(chip); return;
// notify listener
chipsInput.onChipAdded(chip, chipList.size());
// hide hint
editText.setHint(null);
// reset text
editText.setText(null);
// refresh data
notifyItemInserted(chipList.size());
} }
chipList.add(chip);
// notify listener
chipsInput.onChipAdded(chip, chipList.size());
// hide hint
editText.setHint(null);
// reset text
editText.setText(null);
// refresh data
notifyItemInserted(chipList.size() -1);
} }
public void removeChip(T chip) { public void removeChip(T chip) {
@@ -289,16 +291,4 @@ public abstract class ChipsAdapter<T extends FilterableItem, VH extends Recycler
editText = (EditText) view; editText = (EditText) view;
} }
} }
private boolean listContains(List<T> contactList, T chip) {
if (chipsInput.getChipValidator() != null) {
for (T item : contactList) {
if (chipsInput.getChipValidator().areEquals(item, chip)) {
return true;
}
}
}
return false;
}
} }