1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-23 18:02:15 -05:00

Try to use the correct identity with OpenPGP API's EXTRA_ACCOUNT_NAME

This commit is contained in:
cketti 2014-04-01 02:38:47 +02:00
parent cc8353d255
commit 49dbaf034c
4 changed files with 103 additions and 28 deletions

View File

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

View File

@ -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
* <code>EXTRA_ACCOUNT_NAME</code>.
*
* @return A string with the following format:
* <code>display name &lt;user@example.com&gt;</code>
*
* @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();
}
}

View File

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

View File

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