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 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;

View File

@@ -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<EncryptRecipientChip> {
private long[] preselectedKeyIds;
public EncryptRecipientChipsInput(Context context) {
super(context);
init();
@@ -31,6 +35,22 @@ public class EncryptRecipientChipsInput extends ChipsInput<EncryptRecipientChip>
public void setData(List<EncryptRecipientChip> keyInfoChips) {
EncryptRecipientDropdownAdapter chipDropdownAdapter = new EncryptRecipientDropdownAdapter(getContext(), keyInfoChips);
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 {