compose: load recipient data for addresses before adding tokens

This commit is contained in:
Vincent Breitmoser 2015-12-01 17:01:37 +01:00 committed by cketti
parent 8d42cdca74
commit 2bb284bd36
6 changed files with 92 additions and 30 deletions

View file

@ -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();

View file

@ -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 {

View file

@ -47,17 +47,26 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
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<Recipient> 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<List<Recipient>> {
ArrayList<Recipient> recipients = new ArrayList<Recipient>();
HashMap<String,Recipient> recipientMap = new HashMap<String, Recipient>();
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<List<Recipient>> {
}
}
private void loadContactData(ArrayList<Recipient> recipients, HashMap<String, Recipient> recipientMap) {
private void fillContactDataFromAddresses(Address[] addresses, ArrayList<Recipient> recipients,
HashMap<String, Recipient> 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<Recipient> recipients, HashMap<String, Recipient> recipientMap) {
Uri queryUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_FILTER_URI,
Uri.encode(query));
@ -138,7 +166,7 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
}
private void loadCryptoStatusData(HashMap<String, Recipient> recipientMap) {
private void fillCryptoStatusData(HashMap<String, Recipient> recipientMap) {
ArrayList<String> recipientArrayList = new ArrayList<String>(recipientMap.keySet());
String[] recipientAddresses = recipientArrayList.toArray(new String[ recipientArrayList.size() ]);

View file

@ -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<Recipient> 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();
}
}

View file

@ -155,9 +155,9 @@ public class RecipientSelectView extends TokenCompleteTextView<Recipient> 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<Recipient> implem
public Loader<List<Recipient>> 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

View file

@ -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) {