Cache whether we have sent an email or not

This commit is contained in:
str4d
2015-01-07 05:09:06 +00:00
parent 788e74e93d
commit 73ebc991a8
2 changed files with 98 additions and 63 deletions

View File

@@ -521,6 +521,9 @@ public class NewEmailFragment extends Fragment {
// Set the text and add attachments // Set the text and add attachments
email.setContent(mContent.getText().toString(), attachments); email.setContent(mContent.getText().toString(), attachments);
// Cache the fact that we sent this email
BoteHelper.setEmailSent(email, true);
// Send the email // Send the email
I2PBote.getInstance().sendEmail(email); I2PBote.getInstance().sendEmail(email);

View File

@@ -54,7 +54,8 @@ public class BoteHelper extends GeneralHelper {
* Get the translated name of the folder. * Get the translated name of the folder.
* Built-in folders are special-cased; other folders are created by the * Built-in folders are special-cased; other folders are created by the
* user, so their name is already "translated". * user, so their name is already "translated".
* @param ctx Android Context to get strings from. *
* @param ctx Android Context to get strings from.
* @param folder The folder. * @param folder The folder.
* @return The name of the folder. * @return The name of the folder.
*/ */
@@ -75,7 +76,8 @@ public class BoteHelper extends GeneralHelper {
/** /**
* Get the translated name of the folder with the number of * Get the translated name of the folder with the number of
* new messages it contains appended. * new messages it contains appended.
* @param ctx Android Context to get strings from. *
* @param ctx Android Context to get strings from.
* @param folder The folder. * @param folder The folder.
* @return The name of the folder. * @return The name of the folder.
* @throws PasswordException * @throws PasswordException
@@ -111,12 +113,13 @@ public class BoteHelper extends GeneralHelper {
return (emailDest == null ? address return (emailDest == null ? address
: (name.isEmpty() ? emailDest.substring(0, 10) : (name.isEmpty() ? emailDest.substring(0, 10)
: name + " <" + emailDest.substring(0, 10) + "...>")); : name + " <" + emailDest.substring(0, 10) + "...>"));
} }
/** /**
* Get a Bitmap containing the picture for the contact or identity * Get a Bitmap containing the picture for the contact or identity
* corresponding to the given address. * corresponding to the given address.
*
* @param address the address to get a picture for. * @param address the address to get a picture for.
* @return a Bitmap, or null if no picture was found. * @return a Bitmap, or null if no picture was found.
* @throws PasswordException * @throws PasswordException
@@ -137,6 +140,7 @@ public class BoteHelper extends GeneralHelper {
/** /**
* Get a Bitmap containing the picture for the contact or identity * Get a Bitmap containing the picture for the contact or identity
* corresponding to the given Destination. * corresponding to the given Destination.
*
* @param base64dest the Destination to get a picture for. * @param base64dest the Destination to get a picture for.
* @return a Bitmap, or null if no picture was found. * @return a Bitmap, or null if no picture was found.
* @throws PasswordException * @throws PasswordException
@@ -187,8 +191,8 @@ public class BoteHelper extends GeneralHelper {
// Check if the string contains chars in angle brackets // Check if the string contains chars in angle brackets
int ltIndex = address.indexOf('<'); int ltIndex = address.indexOf('<');
int gtIndex = address.indexOf('>', ltIndex); int gtIndex = address.indexOf('>', ltIndex);
if (ltIndex>=0 && gtIndex>0) if (ltIndex >= 0 && gtIndex > 0)
identifier = address.substring(ltIndex+1, gtIndex); identifier = address.substring(ltIndex + 1, gtIndex);
else else
identifier = address; identifier = address;
} }
@@ -202,72 +206,99 @@ public class BoteHelper extends GeneralHelper {
return bitmap; return bitmap;
} }
private static final String PROPERTY_SENT = "sent";
public static void setEmailSent(Email email, boolean isSent) {
email.getMetadata().setProperty(PROPERTY_SENT, isSent ? "true" : "false");
}
/**
* Determines if we sent this email, either anonymously or from a local identity.
*
* @param email The Email to query metadata for
* @return true if we sent this email, false otherwise
* @throws PasswordException
* @throws IOException
* @throws GeneralSecurityException
* @throws MessagingException
*/
public static boolean isSentEmail(Email email) throws PasswordException, IOException, GeneralSecurityException, MessagingException { public static boolean isSentEmail(Email email) throws PasswordException, IOException, GeneralSecurityException, MessagingException {
// Is the sender anonymous and we are not the recipient? boolean isSent;
if (email.isAnonymous()) {
Address[] recipients = email.getAllRecipients(); if (email.getMetadata().containsKey(PROPERTY_SENT)) {
for (Address recipient : recipients) { String sentStr = email.getMetadata().getProperty(PROPERTY_SENT);
String toDest = EmailDestination.extractBase64Dest(recipient.toString()); isSent = "true".equals(sentStr);
if (toDest != null && getIdentity(toDest) != null) } else {
// We are a recipient // Figure it out
return false; // Is the sender anonymous?
if (email.isAnonymous()) {
// Assume we sent it unless we are a recipient
isSent = true;
Address[] recipients = email.getAllRecipients();
for (Address recipient : recipients) {
String toDest = EmailDestination.extractBase64Dest(recipient.toString());
if (toDest != null && getIdentity(toDest) != null) {
// We are a recipient
isSent = false;
break;
}
}
} else {
// Are we the sender?
String fromAddress = email.getOneFromAddress();
String fromDest = EmailDestination.extractBase64Dest(fromAddress);
isSent = (fromDest != null && getIdentity(fromDest) != null);
} }
// We are not a recipient
return true; // Cache for next time
setEmailSent(email, isSent);
} }
// Are we the sender? return isSent;
String fromAddress = email.getOneFromAddress();
String fromDest = EmailDestination.extractBase64Dest(fromAddress);
if ((fromDest != null && getIdentity(fromDest) != null))
return true;
// We are not the sender
return false;
} }
public static String getEmailStatusText(Context ctx, Email email, boolean full) { public static String getEmailStatusText(Context ctx, Email email, boolean full) {
Resources res = ctx.getResources(); Resources res = ctx.getResources();
EmailStatus emailStatus = getEmailStatus(email); EmailStatus emailStatus = getEmailStatus(email);
switch (emailStatus.getStatus()) { switch (emailStatus.getStatus()) {
case QUEUED: case QUEUED:
return res.getString(R.string.queued); return res.getString(R.string.queued);
case SENDING: case SENDING:
return res.getString(R.string.sending); return res.getString(R.string.sending);
case SENT_TO: case SENT_TO:
if (full) if (full)
return res.getString(R.string.sent_to, return res.getString(R.string.sent_to,
(Integer) emailStatus.getParam1(), (Integer) emailStatus.getParam2()); (Integer) emailStatus.getParam1(), (Integer) emailStatus.getParam2());
else else
return res.getString(R.string.sent_to_short, return res.getString(R.string.sent_to_short,
(Integer) emailStatus.getParam1(), (Integer) emailStatus.getParam2()); (Integer) emailStatus.getParam1(), (Integer) emailStatus.getParam2());
case EMAIL_SENT: case EMAIL_SENT:
return res.getString(R.string.email_sent); return res.getString(R.string.email_sent);
case GATEWAY_DISABLED: case GATEWAY_DISABLED:
return res.getString(R.string.gateway_disabled); return res.getString(R.string.gateway_disabled);
case NO_IDENTITY_MATCHES: case NO_IDENTITY_MATCHES:
if (full) if (full)
return res.getString(R.string.no_identity_matches, return res.getString(R.string.no_identity_matches,
emailStatus.getParam1()); emailStatus.getParam1());
case INVALID_RECIPIENT: case INVALID_RECIPIENT:
if (full) if (full)
return res.getString(R.string.invalid_recipient, return res.getString(R.string.invalid_recipient,
emailStatus.getParam1()); emailStatus.getParam1());
case ERROR_CREATING_PACKETS: case ERROR_CREATING_PACKETS:
if (full) if (full)
return res.getString(R.string.error_creating_packets, return res.getString(R.string.error_creating_packets,
emailStatus.getParam1()); emailStatus.getParam1());
case ERROR_SENDING: case ERROR_SENDING:
if (full) if (full)
return res.getString(R.string.error_sending, return res.getString(R.string.error_sending,
emailStatus.getParam1()); emailStatus.getParam1());
case ERROR_SAVING_METADATA: case ERROR_SAVING_METADATA:
if (full) if (full)
return res.getString(R.string.error_saving_metadata, return res.getString(R.string.error_saving_metadata,
emailStatus.getParam1()); emailStatus.getParam1());
default: default:
// Short string for errors and unknown status // Short string for errors and unknown status
return res.getString(R.string.error); return res.getString(R.string.error);
} }
} }
@@ -308,6 +339,7 @@ public class BoteHelper extends GeneralHelper {
public interface RequestPasswordListener { public interface RequestPasswordListener {
public void onPasswordVerified(); public void onPasswordVerified();
public void onPasswordCanceled(); public void onPasswordCanceled();
} }
@@ -427,9 +459,9 @@ public class BoteHelper extends GeneralHelper {
* Attempt to revoke any URI permissions that were granted on an Email's attachments. * Attempt to revoke any URI permissions that were granted on an Email's attachments.
* This is best-effort; exceptions are silently ignored. * This is best-effort; exceptions are silently ignored.
* *
* @param context the Context in which permissions were granted * @param context the Context in which permissions were granted
* @param folderName where the Email is * @param folderName where the Email is
* @param email the Email to revoke permissions for * @param email the Email to revoke permissions for
*/ */
public static void revokeAttachmentUriPermissions(Context context, String folderName, Email email) { public static void revokeAttachmentUriPermissions(Context context, String folderName, Email email) {
List<Part> parts; List<Part> parts;