From 49dbaf034cd9df4c6837de985d50090dd9b0d279 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 1 Apr 2014 02:38:47 +0200 Subject: [PATCH] Try to use the correct identity with OpenPGP API's EXTRA_ACCOUNT_NAME --- src/com/fsck/k9/activity/MessageCompose.java | 32 +++-------- src/com/fsck/k9/crypto/OpenPgpApiHelper.java | 30 ++++++++++ src/com/fsck/k9/helper/IdentityHelper.java | 60 ++++++++++++++++++++ src/com/fsck/k9/view/MessageOpenPgpView.java | 9 ++- 4 files changed, 103 insertions(+), 28 deletions(-) create mode 100644 src/com/fsck/k9/crypto/OpenPgpApiHelper.java create mode 100644 src/com/fsck/k9/helper/IdentityHelper.java diff --git a/src/com/fsck/k9/activity/MessageCompose.java b/src/com/fsck/k9/activity/MessageCompose.java index 9a2563d96..8dad95547 100644 --- a/src/com/fsck/k9/activity/MessageCompose.java +++ b/src/com/fsck/k9/activity/MessageCompose.java @@ -67,11 +67,13 @@ import com.fsck.k9.activity.misc.Attachment; import com.fsck.k9.controller.MessagingController; import com.fsck.k9.controller.MessagingListener; import com.fsck.k9.crypto.CryptoProvider; +import com.fsck.k9.crypto.OpenPgpApiHelper; import com.fsck.k9.crypto.PgpData; import com.fsck.k9.fragment.ProgressDialogFragment; import com.fsck.k9.helper.ContactItem; import com.fsck.k9.helper.Contacts; import com.fsck.k9.helper.HtmlConverter; +import com.fsck.k9.helper.IdentityHelper; import com.fsck.k9.helper.StringUtils; import com.fsck.k9.helper.Utility; import com.fsck.k9.mail.Address; @@ -2005,7 +2007,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, intent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); // this follows user id format of OpenPGP to allow key generation based on it // includes account number to make it unique - String accName = mIdentity.getName() + " <" + mIdentity.getEmail() + ">"; + String accName = OpenPgpApiHelper.buildAccountName(mIdentity); intent.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, accName); final InputStream is = getOpenPgpInputStream(); @@ -2988,30 +2990,10 @@ public class MessageCompose extends K9Activity implements OnClickListener, populateUIWithQuotedMessage(mAccount.isDefaultQuotedTextShown()); if (mAction == Action.REPLY || mAction == Action.REPLY_ALL) { - Identity useIdentity = null; - for (Address address : message.getRecipients(RecipientType.TO)) { - Identity identity = mAccount.findIdentity(address); - if (identity != null) { - useIdentity = identity; - break; - } - } - if (useIdentity == null) { - if (message.getRecipients(RecipientType.CC).length > 0) { - for (Address address : message.getRecipients(RecipientType.CC)) { - Identity identity = mAccount.findIdentity(address); - if (identity != null) { - useIdentity = identity; - break; - } - } - } - } - if (useIdentity != null) { - Identity defaultIdentity = mAccount.getIdentity(0); - if (useIdentity != defaultIdentity) { - switchToIdentity(useIdentity); - } + Identity useIdentity = IdentityHelper.getRecipientIdentityFromMessage(mAccount, message); + Identity defaultIdentity = mAccount.getIdentity(0); + if (useIdentity != defaultIdentity) { + switchToIdentity(useIdentity); } } diff --git a/src/com/fsck/k9/crypto/OpenPgpApiHelper.java b/src/com/fsck/k9/crypto/OpenPgpApiHelper.java new file mode 100644 index 000000000..f69e61472 --- /dev/null +++ b/src/com/fsck/k9/crypto/OpenPgpApiHelper.java @@ -0,0 +1,30 @@ +package com.fsck.k9.crypto; + +import android.text.TextUtils; + +import com.fsck.k9.Identity; + + +public class OpenPgpApiHelper { + + /** + * Create an "account name" from the supplied identity for use with the OpenPgp API's + * EXTRA_ACCOUNT_NAME. + * + * @return A string with the following format: + * display name <user@example.com> + * + * @see org.openintents.openpgp.util.OpenPgpApi#EXTRA_ACCOUNT_NAME + */ + public static String buildAccountName(Identity identity) { + StringBuilder sb = new StringBuilder(); + + String name = identity.getName(); + if (!TextUtils.isEmpty(name)) { + sb.append(name).append(" "); + } + sb.append("<").append(identity.getEmail()).append(">"); + + return sb.toString(); + } +} diff --git a/src/com/fsck/k9/helper/IdentityHelper.java b/src/com/fsck/k9/helper/IdentityHelper.java new file mode 100644 index 000000000..8c9023b72 --- /dev/null +++ b/src/com/fsck/k9/helper/IdentityHelper.java @@ -0,0 +1,60 @@ +package com.fsck.k9.helper; + +import android.util.Log; + +import com.fsck.k9.Account; +import com.fsck.k9.Identity; +import com.fsck.k9.K9; +import com.fsck.k9.mail.Address; +import com.fsck.k9.mail.Message; +import com.fsck.k9.mail.MessagingException; + +public class IdentityHelper { + + /** + * Find the identity a message was sent to. + * + * @param account + * The account the message belongs to. + * @param message + * The message to get the recipients from. + * + * @return The identity the message was sent to, or the account's default identity if it + * couldn't be determined which identity this message was sent to. + * + * @see Account#findIdentity(com.fsck.k9.mail.Address) + */ + public static Identity getRecipientIdentityFromMessage(Account account, Message message) { + Identity recipient = null; + + try { + for (Address address : message.getRecipients(Message.RecipientType.TO)) { + Identity identity = account.findIdentity(address); + if (identity != null) { + recipient = identity; + break; + } + } + if (recipient == null) { + Address[] ccAddresses = message.getRecipients(Message.RecipientType.CC); + if (ccAddresses.length > 0) { + for (Address address : ccAddresses) { + Identity identity = account.findIdentity(address); + if (identity != null) { + recipient = identity; + break; + } + } + } + } + } catch (MessagingException e) { + Log.w(K9.LOG_TAG, "Error finding the identity this message was sent to", e); + } + + if (recipient == null) { + recipient = account.getIdentity(0); + } + + return recipient; + } +} diff --git a/src/com/fsck/k9/view/MessageOpenPgpView.java b/src/com/fsck/k9/view/MessageOpenPgpView.java index aa560f81c..0b4f2c808 100644 --- a/src/com/fsck/k9/view/MessageOpenPgpView.java +++ b/src/com/fsck/k9/view/MessageOpenPgpView.java @@ -29,10 +29,13 @@ import android.widget.TextView; import android.widget.Toast; import com.fsck.k9.Account; +import com.fsck.k9.Identity; import com.fsck.k9.K9; import com.fsck.k9.R; import com.fsck.k9.crypto.CryptoHelper; +import com.fsck.k9.crypto.OpenPgpApiHelper; import com.fsck.k9.fragment.MessageViewFragment; +import com.fsck.k9.helper.IdentityHelper; import com.fsck.k9.mail.Message; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Part; @@ -266,9 +269,9 @@ public class MessageOpenPgpView extends LinearLayout { private void decryptVerify(Intent intent) { intent.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); intent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); - // this follows user id format of OpenPGP to allow key generation based on it - // includes account number to make it unique - String accName = mAccount.getName() + " <" + mAccount.getEmail() + ">"; + + Identity identity = IdentityHelper.getRecipientIdentityFromMessage(mAccount, mMessage); + String accName = OpenPgpApiHelper.buildAccountName(identity); intent.putExtra(OpenPgpApi.EXTRA_ACCOUNT_NAME, accName); InputStream is = null;