diff --git a/emailcommon/src/com/android/emailcommon/internet/Rfc822Output.java b/emailcommon/src/com/android/emailcommon/internet/Rfc822Output.java index 4622ebf40..6d8b21abd 100644 --- a/emailcommon/src/com/android/emailcommon/internet/Rfc822Output.java +++ b/emailcommon/src/com/android/emailcommon/internet/Rfc822Output.java @@ -234,7 +234,7 @@ public class Rfc822Output { inStream = new ByteArrayInputStream(attachment.mContentBytes); } else { // try to open the file - Uri fileUri = Uri.parse(attachment.mContentUri); + Uri fileUri = Uri.parse(attachment.getContentUri()); inStream = context.getContentResolver().openInputStream(fileUri); } // switch to output stream for base64 text output diff --git a/emailcommon/src/com/android/emailcommon/provider/EmailContent.java b/emailcommon/src/com/android/emailcommon/provider/EmailContent.java index ce4c8832b..4e2d26aaf 100755 --- a/emailcommon/src/com/android/emailcommon/provider/EmailContent.java +++ b/emailcommon/src/com/android/emailcommon/provider/EmailContent.java @@ -1185,18 +1185,21 @@ public abstract class EmailContent { public static Uri CONTENT_URI; // This must be used with an appended id: ContentUris.withAppendedId(MESSAGE_ID_URI, id) public static Uri MESSAGE_ID_URI; + public static String ATTACHMENT_PROVIDER_URI_PREFIX; public static void initAttachment() { CONTENT_URI = Uri.parse(EmailContent.CONTENT_URI + "/attachment"); MESSAGE_ID_URI = Uri.parse( EmailContent.CONTENT_URI + "/attachment/message"); + ATTACHMENT_PROVIDER_URI_PREFIX = "content://" + EmailContent.EMAIL_PACKAGE_NAME + + ".attachmentprovider"; } public String mFileName; public String mMimeType; public long mSize; public String mContentId; - public String mContentUri; + private String mContentUri; public long mMessageKey; public String mLocation; public String mEncoding; @@ -1272,6 +1275,29 @@ public abstract class EmailContent { mBaseUri = CONTENT_URI; } + public void setContentUri(String contentUri) { + mContentUri = contentUri; + } + + public String getContentUri() { + if (mContentUri == null) return null; + if (mContentUri.startsWith(ATTACHMENT_PROVIDER_URI_PREFIX)) { + return mContentUri; + } else { + // In an upgrade scenario, we may still have legacy attachment Uri's + // Skip past content:// + int prefix = mContentUri.indexOf('/', 10); + if (prefix > 0) { + // Create a proper uri string using the actual provider + return ATTACHMENT_PROVIDER_URI_PREFIX + "/" + mContentUri.substring(prefix); + } else { + Log.e("Attachment", "Improper contentUri format: " + mContentUri); + // Belt & suspenders; can't really happen + return mContentUri; + } + } + } + /** * Restore an Attachment from the database, given its unique id * @param context diff --git a/emailcommon/src/com/android/emailcommon/utility/AttachmentUtilities.java b/emailcommon/src/com/android/emailcommon/utility/AttachmentUtilities.java index 564e9a8ee..7040eaa47 100644 --- a/emailcommon/src/com/android/emailcommon/utility/AttachmentUtilities.java +++ b/emailcommon/src/com/android/emailcommon/utility/AttachmentUtilities.java @@ -30,7 +30,6 @@ import android.util.Log; import android.webkit.MimeTypeMap; import com.android.emailcommon.Logging; -import com.android.emailcommon.provider.EmailContent; import com.android.emailcommon.provider.EmailContent.Attachment; import com.android.emailcommon.provider.EmailContent.AttachmentColumns; import com.android.emailcommon.provider.EmailContent.Body; @@ -139,8 +138,7 @@ public class AttachmentUtilities { private static Uri sUri; public static Uri getAttachmentUri(long accountId, long id) { if (sUri == null) { - sUri = Uri.parse("content://" + EmailContent.EMAIL_PACKAGE_NAME + - ".attachmentprovider"); + sUri = Uri.parse(Attachment.ATTACHMENT_PROVIDER_URI_PREFIX); } return sUri.buildUpon() .appendPath(Long.toString(accountId)) diff --git a/emailcommon/src/com/android/emailcommon/utility/Utility.java b/emailcommon/src/com/android/emailcommon/utility/Utility.java index b74d4b3e0..111553eba 100644 --- a/emailcommon/src/com/android/emailcommon/utility/Utility.java +++ b/emailcommon/src/com/android/emailcommon/utility/Utility.java @@ -745,25 +745,28 @@ public class Utility { return false; } else if (attachment.mContentBytes != null) { return true; - } else if (TextUtils.isEmpty(attachment.mContentUri)) { - return false; - } - try { - Uri fileUri = Uri.parse(attachment.mContentUri); - try { - InputStream inStream = context.getContentResolver().openInputStream(fileUri); - try { - inStream.close(); - } catch (IOException e) { - // Nothing to be done if can't close the stream - } - return true; - } catch (FileNotFoundException e) { + } else { + String contentUri = attachment.getContentUri(); + if (TextUtils.isEmpty(contentUri)) { + return false; + } + try { + Uri fileUri = Uri.parse(contentUri); + try { + InputStream inStream = context.getContentResolver().openInputStream(fileUri); + try { + inStream.close(); + } catch (IOException e) { + // Nothing to be done if can't close the stream + } + return true; + } catch (FileNotFoundException e) { + return false; + } + } catch (RuntimeException re) { + Log.w(Logging.LOG_TAG, "attachmentExists RuntimeException=" + re); return false; } - } catch (RuntimeException re) { - Log.w(Logging.LOG_TAG, "attachmentExists RuntimeException=" + re); - return false; } } @@ -790,7 +793,7 @@ public class Utility { Log.d(Logging.LOG_TAG, "Unloaded attachment isn't marked for download: " + att.mFileName + ", #" + att.mId); Attachment.delete(context, Attachment.CONTENT_URI, att.mId); - } else if (att.mContentUri != null) { + } else if (att.getContentUri() != null) { // In this case, the attachment file is gone from the cache; let's clear the // contentUri; this should be a very unusual case ContentValues cv = new ContentValues(); diff --git a/src/com/android/email/LegacyConversions.java b/src/com/android/email/LegacyConversions.java index 5ca2a1703..fcff16b53 100644 --- a/src/com/android/email/LegacyConversions.java +++ b/src/com/android/email/LegacyConversions.java @@ -220,7 +220,7 @@ public class LegacyConversions { localAttachment.mFileName = name; localAttachment.mSize = size; // May be reset below if file handled localAttachment.mContentId = part.getContentId(); - localAttachment.mContentUri = null; // Will be rewritten by saveAttachmentBody + localAttachment.setContentUri(null); // Will be rewritten by saveAttachmentBody localAttachment.mMessageKey = localMessage.mId; localAttachment.mLocation = partId; localAttachment.mEncoding = "B"; // TODO - convert other known encodings @@ -314,7 +314,7 @@ public class LegacyConversions { accountId, attachmentId).toString(); localAttachment.mSize = copySize; - localAttachment.mContentUri = contentUriString; + localAttachment.setContentUri(contentUriString); // update the attachment in the database as well ContentValues cv = new ContentValues(); diff --git a/src/com/android/email/provider/EmailProvider.java b/src/com/android/email/provider/EmailProvider.java index 77587801e..e9701c77d 100644 --- a/src/com/android/email/provider/EmailProvider.java +++ b/src/com/android/email/provider/EmailProvider.java @@ -2490,7 +2490,7 @@ outer: ArrayList uiAtts = new ArrayList(); for (Attachment att : atts) { - if (att.mContentId != null && att.mContentUri != null) { + if (att.mContentId != null && att.getContentUri() != null) { continue; } com.android.mail.providers.Attachment uiAtt = @@ -3463,7 +3463,7 @@ outer: private Attachment convertUiAttachmentToAttachment( com.android.mail.providers.Attachment uiAtt) { Attachment att = new Attachment(); - att.mContentUri = uiAtt.contentUri.toString(); + att.setContentUri(uiAtt.contentUri.toString()); att.mFileName = uiAtt.name; att.mMimeType = uiAtt.contentType; att.mSize = uiAtt.size; @@ -3633,7 +3633,7 @@ outer: attClone.mMessageKey = 0; // If we're sending this, it's not loaded, and we're not smart forwarding // add the download flag, so that ADS will start up - if (mailbox.mType == Mailbox.TYPE_OUTBOX && att.mContentUri == null && + if (mailbox.mType == Mailbox.TYPE_OUTBOX && att.getContentUri() == null && ((account.mFlags & Account.FLAGS_SUPPORTS_SMART_FORWARD) == 0)) { attClone.mFlags |= Attachment.FLAG_DOWNLOAD_FORWARD; hasUnloadedAttachments = true; diff --git a/src/com/android/email/service/AttachmentDownloadService.java b/src/com/android/email/service/AttachmentDownloadService.java index e86e03f37..25356982c 100644 --- a/src/com/android/email/service/AttachmentDownloadService.java +++ b/src/com/android/email/service/AttachmentDownloadService.java @@ -980,8 +980,8 @@ public class AttachmentDownloadService extends Service implements Runnable { suffix = fileName.substring(lastDot); } pw.print(" Suffix: " + suffix); - if (att.mContentUri != null) { - pw.print(" ContentUri: " + att.mContentUri); + if (att.getContentUri() != null) { + pw.print(" ContentUri: " + att.getContentUri()); } pw.print(" Mime: "); if (att.mMimeType != null) {