From 2bb284bd367a06e8b4c4fdaa7cfea11ca3e78c31 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 1 Dec 2015 17:01:37 +0100 Subject: [PATCH] compose: load recipient data for addresses before adding tokens --- .../com/fsck/k9/activity/MessageCompose.java | 2 +- .../fsck/k9/activity/RecipientAdapter.java | 1 + .../com/fsck/k9/activity/RecipientLoader.java | 42 +++++++++++--- .../fsck/k9/activity/RecipientPresenter.java | 57 +++++++++++++++---- .../fsck/k9/activity/RecipientSelectView.java | 8 +-- .../com/fsck/k9/activity/RecipientView.java | 12 ++-- 6 files changed, 92 insertions(+), 30 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java index 258d53985..d76cbd0bb 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java @@ -2112,7 +2112,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, * If a reply-to was included with the message use that, otherwise use the from * or sender address. */ - recipientPresenter.initFromReplyToMessage(message, mAccount); + recipientPresenter.initFromReplyToMessage(message); if (message.getMessageId() != null && message.getMessageId().length() > 0) { mInReplyTo = message.getMessageId(); diff --git a/k9mail/src/main/java/com/fsck/k9/activity/RecipientAdapter.java b/k9mail/src/main/java/com/fsck/k9/activity/RecipientAdapter.java index 5432641a4..ade763bd1 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/RecipientAdapter.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/RecipientAdapter.java @@ -107,6 +107,7 @@ public class RecipientAdapter extends BaseAdapter implements Filterable { } public static void setContactPhotoOrPlaceholder(Context context, ImageView imageView, Recipient recipient) { + imageView.setImageDrawable(null); if (recipient.photoThumbnailUri != null) { Glide.with(context).load(recipient.photoThumbnailUri).into(imageView); } else { diff --git a/k9mail/src/main/java/com/fsck/k9/activity/RecipientLoader.java b/k9mail/src/main/java/com/fsck/k9/activity/RecipientLoader.java index 795a087f3..be1559cea 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/RecipientLoader.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/RecipientLoader.java @@ -47,17 +47,26 @@ public class RecipientLoader extends AsyncTaskLoader> { ContactsContract.Contacts.DISPLAY_NAME + ", " + ContactsContract.CommonDataKinds.Email._ID; - private String query; - private String cryptoProvider; + private final String query; + private final Address[] addresses; + private final String cryptoProvider; private List cachedRecipients; private ForceLoadContentObserver observerContact, observerKey; private CancellationSignal mCancellationSignal; - public RecipientLoader(Context context, String query, String cryptoProvider) { + public RecipientLoader(Context context, String cryptoProvider, String query) { super(context); this.query = query; + this.addresses = null; + this.cryptoProvider = cryptoProvider; + } + + public RecipientLoader(Context context, String cryptoProvider, Address... addresses) { + super(context); + this.query = null; + this.addresses = addresses; this.cryptoProvider = cryptoProvider; } @@ -75,14 +84,20 @@ public class RecipientLoader extends AsyncTaskLoader> { ArrayList recipients = new ArrayList(); HashMap recipientMap = new HashMap(); - loadContactData(recipients, recipientMap); + if (addresses != null) { + fillContactDataFromAddresses(addresses, recipients, recipientMap); + } else if (query != null) { + fillContactDataFromQuery(query, recipients, recipientMap); + } else { + throw new IllegalStateException("loader must be initialized with query or list of addresses!"); + } if (recipients.isEmpty()) { return recipients; } if (cryptoProvider != null) { - loadCryptoStatusData(recipientMap); + fillCryptoStatusData(recipientMap); } return recipients; @@ -94,7 +109,20 @@ public class RecipientLoader extends AsyncTaskLoader> { } } - private void loadContactData(ArrayList recipients, HashMap recipientMap) { + private void fillContactDataFromAddresses(Address[] addresses, ArrayList recipients, + HashMap recipientMap) { + + for (Address address : addresses) { + // TODO actually query photos + Recipient recipient = new Recipient(address); + recipients.add(recipient); + recipientMap.put(address.getAddress(), recipient); + } + + } + + private void fillContactDataFromQuery( + String query, ArrayList recipients, HashMap recipientMap) { Uri queryUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_FILTER_URI, Uri.encode(query)); @@ -138,7 +166,7 @@ public class RecipientLoader extends AsyncTaskLoader> { } - private void loadCryptoStatusData(HashMap recipientMap) { + private void fillCryptoStatusData(HashMap recipientMap) { ArrayList recipientArrayList = new ArrayList(recipientMap.keySet()); String[] recipientAddresses = recipientArrayList.toArray(new String[ recipientArrayList.size() ]); diff --git a/k9mail/src/main/java/com/fsck/k9/activity/RecipientPresenter.java b/k9mail/src/main/java/com/fsck/k9/activity/RecipientPresenter.java index d575c3fbd..a1cd65c18 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/RecipientPresenter.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/RecipientPresenter.java @@ -6,6 +6,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -31,11 +32,14 @@ public class RecipientPresenter { private static final String STATE_KEY_BCC_SHOWN = "com.fsck.k9.activity.MessageCompose.bccShown"; + private Context context; private RecipientView recipientView; private Account account; + private String cryptoProvider; - public RecipientPresenter(RecipientView recipientView, Account account) { + public RecipientPresenter(Context context, RecipientView recipientView, Account account) { this.recipientView = recipientView; + this.context = context; recipientView.setPresenter(this); onSwitchAccount(account); } @@ -80,7 +84,7 @@ public class RecipientPresenter { return false; } - public void initFromReplyToMessage(Message message, Account mAccount) { + public void initFromReplyToMessage(Message message) { Address[] replyToAddresses; if (message.getReplyTo().length > 0) { replyToAddresses = message.getReplyTo(); @@ -91,28 +95,28 @@ public class RecipientPresenter { try { // if we're replying to a message we sent, we probably meant // to reply to the recipient of that message - if (mAccount.isAnIdentity(replyToAddresses)) { + if (account.isAnIdentity(replyToAddresses)) { replyToAddresses = message.getRecipients(RecipientType.TO); } - recipientView.addToAddresses(replyToAddresses); + addRecipientsFromAddresses(RecipientType.TO, replyToAddresses); if (message.getReplyTo().length > 0) { for (Address address : message.getFrom()) { - if (!mAccount.isAnIdentity(address) && !Utility.arrayContains(replyToAddresses, address)) { - recipientView.addToAddresses(address); + if (!account.isAnIdentity(address) && !Utility.arrayContains(replyToAddresses, address)) { + addRecipientsFromAddresses(RecipientType.TO, address); } } } for (Address address : message.getRecipients(RecipientType.TO)) { - if (!mAccount.isAnIdentity(address) && !Utility.arrayContains(replyToAddresses, address)) { + if (!account.isAnIdentity(address) && !Utility.arrayContains(replyToAddresses, address)) { addToAddresses(address); } } if (message.getRecipients(RecipientType.CC).length > 0) { for (Address address : message.getRecipients(RecipientType.CC)) { - if (!mAccount.isAnIdentity(address) && !Utility.arrayContains(replyToAddresses, address)) { + if (!account.isAnIdentity(address) && !Utility.arrayContains(replyToAddresses, address)) { addCcAddresses(address); } @@ -194,12 +198,12 @@ public class RecipientPresenter { } void addToAddresses(Address... toAddresses) { - recipientView.addToAddresses(toAddresses); + addRecipientsFromAddresses(RecipientType.TO, toAddresses); } void addCcAddresses(Address... ccAddresses) { if (ccAddresses.length > 0) { - recipientView.addCcAddresses(ccAddresses); + addRecipientsFromAddresses(RecipientType.CC, ccAddresses); recipientView.setCcVisibility(true); recipientView.invalidateOptionsMenu(); } @@ -207,7 +211,7 @@ public class RecipientPresenter { void addBccAddresses(Address... bccRecipients) { if (bccRecipients.length > 0) { - recipientView.addBccAddresses(bccRecipients); + addRecipientsFromAddresses(RecipientType.BCC, bccRecipients); String bccAddress = account.getAlwaysBcc(); // If the auto-bcc is the only entry in the BCC list, don't show the Bcc fields. @@ -232,9 +236,11 @@ public class RecipientPresenter { recipientView.setBccVisibility(true); recipientView.invalidateOptionsMenu(); } - recipientView.setCryptoProvider(account.getOpenPgpProvider()); + cryptoProvider = account.getOpenPgpProvider(); + recipientView.setCryptoProvider(cryptoProvider); } + @SuppressWarnings("UnusedParameters") public void onSwitchIdentity(Identity identity) { // TODO decide what actually to do on identity switch? @@ -288,25 +294,52 @@ public class RecipientPresenter { recipientView.showCryptoStatus(allKeys, allVerified); } + @SuppressWarnings("UnusedParameters") public void onToTokenAdded(Recipient recipient) { updateCryptoStatus(); } + @SuppressWarnings("UnusedParameters") public void onToTokenRemoved(Recipient recipient) { updateCryptoStatus(); } + @SuppressWarnings("UnusedParameters") public void onCcTokenAdded(Recipient recipient) { updateCryptoStatus(); } + @SuppressWarnings("UnusedParameters") public void onCcTokenRemoved(Recipient recipient) { updateCryptoStatus(); } + @SuppressWarnings("UnusedParameters") public void onBccTokenAdded(Recipient recipient) { updateCryptoStatus(); } + @SuppressWarnings("UnusedParameters") public void onBccTokenRemoved(Recipient recipient) { updateCryptoStatus(); } + private void addRecipientsFromAddresses(final RecipientType type, final Address... addresses) { + new RecipientLoader(context, cryptoProvider, addresses) { + @Override + public void deliverResult(List result) { + Recipient[] recipientArray = result.toArray(new Recipient[result.size()]); + switch (type) { + case TO: + recipientView.addToRecipients(recipientArray); + break; + case CC: + recipientView.addCcRecipients(recipientArray); + break; + case BCC: + recipientView.addBccRecipients(recipientArray); + break; + } + stopLoading(); + } + }.startLoading(); + } + } diff --git a/k9mail/src/main/java/com/fsck/k9/activity/RecipientSelectView.java b/k9mail/src/main/java/com/fsck/k9/activity/RecipientSelectView.java index 24133143c..e9101f6aa 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/RecipientSelectView.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/RecipientSelectView.java @@ -155,9 +155,9 @@ public class RecipientSelectView extends TokenCompleteTextView implem this.cryptoProvider = cryptoProvider; } - public void addAddress(Address... addresses) { - for (Address address : addresses) { - addObject(new Recipient(address)); + public void addRecipients(Recipient... recipients) { + for (Recipient recipient : recipients) { + addObject(recipient); } } @@ -174,7 +174,7 @@ public class RecipientSelectView extends TokenCompleteTextView implem public Loader> onCreateLoader(int id, Bundle args) { String query = args != null && args.containsKey(ARG_QUERY) ? args.getString(ARG_QUERY) : ""; // mAdapter.setSearchQuery(query); - return new RecipientLoader(getContext(), query, cryptoProvider); + return new RecipientLoader(getContext(), cryptoProvider, query); } @Override diff --git a/k9mail/src/main/java/com/fsck/k9/activity/RecipientView.java b/k9mail/src/main/java/com/fsck/k9/activity/RecipientView.java index ce9c6db6a..250099e27 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/RecipientView.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/RecipientView.java @@ -113,16 +113,16 @@ public class RecipientView { fontSizes.setViewTextSize(bccView, fontSize); } - public void addToAddresses(Address... addresses) { - toView.addAddress(addresses); + public void addToRecipients(Recipient... recipients) { + toView.addRecipients(recipients); } - public void addCcAddresses(Address... addresses) { - ccView.addAddress(addresses); + public void addCcRecipients(Recipient... recipients) { + ccView.addRecipients(recipients); } - public void addBccAddresses(Address... addresses) { - bccView.addAddress(addresses); + public void addBccRecipients(Recipient... recipients) { + bccView.addRecipients(recipients); } public void setCcVisibility(boolean visible) {