From 1c7a2b1db1d079f5bd5fb0aad084bbfdd2bc2833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Tue, 3 Jul 2018 20:08:19 +0200 Subject: [PATCH] show status icon etc in chip dropdown --- .../ui/EncryptModeAsymmetricFragment.java | 42 ++++++------- .../EncryptRecipientDropdownAdapter.java | 63 +++++++++++++++++++ .../ui/chips/EncryptRecipientChipsInput.java | 5 +- .../layout/encrypt_asymmetric_fragment.xml | 2 +- 4 files changed, 86 insertions(+), 26 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/EncryptRecipientDropdownAdapter.java 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 d1523e9d8..4264575c9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java @@ -35,9 +35,6 @@ import android.view.ViewGroup; import android.widget.ViewAnimator; import com.pchmn.materialchips.ChipsInput.SimpleChipsListener; -import com.pchmn.materialchips.model.ChipInterface; -import com.pchmn.materialchips.simple.SimpleChip; -import com.pchmn.materialchips.simple.SimpleChipsInput; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.daos.KeyRepository; @@ -50,13 +47,14 @@ import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.util.Notify.Style; import org.sufficientlysecure.keychain.ui.widget.KeySpinner; import org.sufficientlysecure.keychain.util.Passphrase; +import timber.log.Timber; public class EncryptModeAsymmetricFragment extends EncryptModeFragment { KeyRepository keyRepository; private KeySpinner mSignKeySpinner; - private SimpleChipsInput 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"; @@ -103,16 +101,16 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment { mSignKeySpinner.setShowNone(R.string.cert_none); final ViewAnimator vEncryptionIcon = view.findViewById(R.id.result_encryption_icon); - mEncryptKeyView.addChipsListener(new SimpleChipsListener() { + mEncryptKeyView.addChipsListener(new SimpleChipsListener() { @Override - public void onChipAdded(SimpleChip chipInterface, int newSize) { + public void onChipAdded(EncryptRecipientChip chipInterface, int newSize) { if (vEncryptionIcon.getDisplayedChild() != 1) { vEncryptionIcon.setDisplayedChild(1); } } @Override - public void onChipRemoved(SimpleChip chipInterface, int newSize) { + public void onChipRemoved(EncryptRecipientChip chipInterface, int newSize) { int child = newSize == 0 ? 0 : 1; if (vEncryptionIcon.getDisplayedChild() != child) { vEncryptionIcon.setDisplayedChild(child); @@ -129,13 +127,7 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment { EncryptModeViewModel viewModel = ViewModelProviders.of(this).get(EncryptModeViewModel.class); viewModel.getSignKeyLiveData(requireContext()).observe(this, mSignKeySpinner::setData); - viewModel.getEncryptRecipientLiveData(requireContext()).observe(this, (keyUnifiedData) -> { - ArrayList simpleChips = new ArrayList<>(); - for (EncryptRecipientChip chip : keyUnifiedData) { - simpleChips.add(new SimpleChip(chip.keyInfo.master_key_id(), chip.keyInfo.name(), chip.keyInfo.email(), chip.keyInfo.user_id_list())); - } - mEncryptKeyView.setData(simpleChips); - }); + viewModel.getEncryptRecipientLiveData(requireContext()).observe(this, mEncryptKeyView::setData); // preselect keys given, from state or arguments if (savedInstanceState == null) { @@ -196,12 +188,16 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment { if (encryptionKeyIds != null) { for (long preselectedId : encryptionKeyIds) { UnifiedKeyInfo keyInfo = keyRepository.getUnifiedKeyInfo(preselectedId); - EncryptRecipientChip recipientChip = EncryptRecipientChipsInput.chipFromUnifiedKeyInfo(keyInfo); - // mEncryptKeyView.addChip(recipientChip); - // EncryptRecipientChip infooo = - // new EncryptRecipientChip(ring.getMasterKeyId(), ring.getPrimaryUserIdWithFallback(), "infooo", null); - // mEncryptKeyView.addChip(infooo); + 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(); } @@ -220,8 +216,8 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment { @Override public long[] getAsymmetricEncryptionKeyIds() { List keyIds = new ArrayList<>(); - for (ChipInterface chip : mEncryptKeyView.getSelectedChipList()) { - keyIds.add((long) chip.getId()); + for (EncryptRecipientChip chip : mEncryptKeyView.getSelectedChipList()) { + keyIds.add(chip.keyInfo.master_key_id()); } long[] keyIdsArr = new long[keyIds.size()]; @@ -236,8 +232,8 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment { @Override public String[] getAsymmetricEncryptionUserIds() { List userIds = new ArrayList<>(); - for (ChipInterface chip : mEncryptKeyView.getSelectedChipList()) { - userIds.add(chip.getInfo()); + for (EncryptRecipientChip chip : mEncryptKeyView.getSelectedChipList()) { + userIds.add(chip.keyInfo.user_id()); } return userIds.toArray(new String[userIds.size()]); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/EncryptRecipientDropdownAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/EncryptRecipientDropdownAdapter.java new file mode 100644 index 000000000..811cd85e0 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/EncryptRecipientDropdownAdapter.java @@ -0,0 +1,63 @@ +package org.sufficientlysecure.keychain.ui.adapter; + + +import java.util.List; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.pchmn.materialchips.ChipsInput; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.ui.adapter.EncryptRecipientDropdownAdapter.ItemViewHolder; +import org.sufficientlysecure.keychain.ui.chips.EncryptRecipientChipsInput.EncryptRecipientChip; +import org.sufficientlysecure.keychain.ui.util.KeyInfoFormatter; + + +public class EncryptRecipientDropdownAdapter extends ChipsInput.ChipDropdownAdapter { + private final LayoutInflater layoutInflater; + + public EncryptRecipientDropdownAdapter(Context context, List keyInfoChips) { + super(keyInfoChips); + + layoutInflater = LayoutInflater.from(context); + } + + class ItemViewHolder extends RecyclerView.ViewHolder { + private final TextView vMainUserId; + private final TextView vMainUserIdRest; + private final TextView vCreationDate; + private final ImageView vStatusIcon; + + ItemViewHolder(View view) { + super(view); + + vMainUserId = itemView.findViewById(R.id.key_list_item_name); + vMainUserIdRest = itemView.findViewById(R.id.key_list_item_email); + vStatusIcon = itemView.findViewById(R.id.key_list_item_status_icon); + vCreationDate = itemView.findViewById(R.id.key_list_item_creation); + } + } + + @NonNull + @Override + public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = layoutInflater.inflate(R.layout.key_list_item, parent, false); + return new ItemViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) { + EncryptRecipientChip chip = getItem(position); + + KeyInfoFormatter keyInfoFormatter = new KeyInfoFormatter(layoutInflater.getContext(), chip.keyInfo, null); + keyInfoFormatter.formatUserId(holder.vMainUserId, holder.vMainUserIdRest); + keyInfoFormatter.formatCreationDate(holder.vCreationDate); + keyInfoFormatter.formatStatusIcon(holder.vStatusIcon); + } +} 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 ea4c1bd47..2a291791f 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 @@ -9,6 +9,7 @@ import android.util.AttributeSet; import com.pchmn.materialchips.ChipsInput; import com.pchmn.materialchips.adapter.FilterableAdapter.FilterableItem; import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo; +import org.sufficientlysecure.keychain.ui.adapter.EncryptRecipientDropdownAdapter; import org.sufficientlysecure.keychain.ui.chips.EncryptRecipientChipsInput.EncryptRecipientChip; @@ -29,8 +30,8 @@ public class EncryptRecipientChipsInput extends ChipsInput } public void setData(List keyInfoChips) { -// SimpleChipDropdownAdapter chipDropdownAdapter = new SimpleChipDropdownAdapter(getContext(), simpleChips); -// setChipDropdownAdapter(chipDropdownAdapter); + EncryptRecipientDropdownAdapter chipDropdownAdapter = new EncryptRecipientDropdownAdapter(getContext(), keyInfoChips); + setChipDropdownAdapter(chipDropdownAdapter); } public static class EncryptRecipientChip implements FilterableItem { diff --git a/OpenKeychain/src/main/res/layout/encrypt_asymmetric_fragment.xml b/OpenKeychain/src/main/res/layout/encrypt_asymmetric_fragment.xml index 7f4b505b7..08bc865cb 100644 --- a/OpenKeychain/src/main/res/layout/encrypt_asymmetric_fragment.xml +++ b/OpenKeychain/src/main/res/layout/encrypt_asymmetric_fragment.xml @@ -38,7 +38,7 @@ -