From 69fb637a70139718be551cd8b5073cb46be5d0dd Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Wed, 4 Jul 2018 16:35:11 +0200 Subject: [PATCH] fix preselecting multiple encryption recipients --- .../ui/EncryptModeAsymmetricFragment.java | 58 +++++-------------- .../ui/chips/EncryptRecipientChipsInput.java | 20 +++++++ .../com/pchmn/materialchips/ChipsInput.java | 17 +----- .../materialchips/adapter/ChipsAdapter.java | 34 ++++------- 4 files changed, 50 insertions(+), 79 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java index 4264575c9..aa5237dc2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java @@ -51,15 +51,14 @@ import timber.log.Timber; 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; private KeySpinner mSignKeySpinner; 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) { EncryptModeAsymmetricFragment frag = new EncryptModeAsymmetricFragment(); @@ -131,12 +130,19 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment { // preselect keys given, from state or arguments if (savedInstanceState == null) { - Long signatureKeyId = getArguments().getLong(ARG_SINGATURE_KEY_ID); - if (signatureKeyId == Constants.key.none) { - signatureKeyId = null; - } - long[] encryptionKeyIds = getArguments().getLongArray(ARG_ENCRYPTION_KEY_IDS); - preselectKeys(signatureKeyId, encryptionKeyIds); + Bundle arguments = getArguments(); + preselectKeysFromArguments(arguments); + } + } + + 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 public boolean isAsymmetric() { return true; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/chips/EncryptRecipientChipsInput.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/chips/EncryptRecipientChipsInput.java index 5bdfc4f03..9ee636e2a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/chips/EncryptRecipientChipsInput.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/chips/EncryptRecipientChipsInput.java @@ -1,6 +1,8 @@ package org.sufficientlysecure.keychain.ui.chips; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import android.content.Context; @@ -13,6 +15,8 @@ import org.sufficientlysecure.keychain.ui.chips.EncryptRecipientChipsInput.Encry public class EncryptRecipientChipsInput extends ChipsInput { + private long[] preselectedKeyIds; + public EncryptRecipientChipsInput(Context context) { super(context); init(); @@ -31,6 +35,22 @@ public class EncryptRecipientChipsInput extends ChipsInput public void setData(List keyInfoChips) { EncryptRecipientDropdownAdapter chipDropdownAdapter = new EncryptRecipientDropdownAdapter(getContext(), keyInfoChips); setChipDropdownAdapter(chipDropdownAdapter); + + if (preselectedKeyIds != null) { + Arrays.sort(preselectedKeyIds); + ArrayList 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 { diff --git a/extern/MaterialChipsInput/src/main/java/com/pchmn/materialchips/ChipsInput.java b/extern/MaterialChipsInput/src/main/java/com/pchmn/materialchips/ChipsInput.java index 961a1fda4..0abff6721 100644 --- a/extern/MaterialChipsInput/src/main/java/com/pchmn/materialchips/ChipsInput.java +++ b/extern/MaterialChipsInput/src/main/java/com/pchmn/materialchips/ChipsInput.java @@ -45,7 +45,6 @@ public abstract class ChipsInput extends ScrollViewMax private boolean mShowChipDetailed = true; private List> mChipsListenerList = new ArrayList<>(); - private ChipValidator mChipValidator; private ChipsAdapter chipsAdapter; private RecyclerView chipsRecyclerView; @@ -187,8 +186,8 @@ public abstract class ChipsInput extends ScrollViewMax }); } - public void addChip(T chip) { - chipsAdapter.addChip(chip); + public void addChips(List chips) { + chipsAdapter.addChipsProgrammatically(chips); } public ChipsInputEditText getEditText() { @@ -317,14 +316,6 @@ public abstract class ChipsInput extends ScrollViewMax })); } - public ChipValidator getChipValidator() { - return mChipValidator; - } - - public void setChipValidator(ChipValidator mChipValidator) { - this.mChipValidator = mChipValidator; - } - public interface ChipsListener { void onChipAdded(T chip, int newSize); void onChipRemoved(T chip, int newSize); @@ -338,8 +329,4 @@ public abstract class ChipsInput extends ScrollViewMax public void onTextChanged(CharSequence text) { } public void onActionDone(CharSequence text) { } } - - public interface ChipValidator { - boolean areEquals(T chip1, T chip2); - } } diff --git a/extern/MaterialChipsInput/src/main/java/com/pchmn/materialchips/adapter/ChipsAdapter.java b/extern/MaterialChipsInput/src/main/java/com/pchmn/materialchips/adapter/ChipsAdapter.java index 8a0ef8a25..38b717403 100644 --- a/extern/MaterialChipsInput/src/main/java/com/pchmn/materialchips/adapter/ChipsAdapter.java +++ b/extern/MaterialChipsInput/src/main/java/com/pchmn/materialchips/adapter/ChipsAdapter.java @@ -202,17 +202,19 @@ public abstract class ChipsAdapter contactList, T chip) { - if (chipsInput.getChipValidator() != null) { - for (T item : contactList) { - if (chipsInput.getChipValidator().areEquals(item, chip)) { - return true; - } - } - } - - return false; - } }