Merge "Use correct attachment provider authority" into jb-ub-mail

This commit is contained in:
Marc Blank 2012-09-09 10:45:37 -07:00 committed by Android (Google) Code Review
commit 19e9291c9a
7 changed files with 57 additions and 30 deletions

View File

@ -234,7 +234,7 @@ public class Rfc822Output {
inStream = new ByteArrayInputStream(attachment.mContentBytes); inStream = new ByteArrayInputStream(attachment.mContentBytes);
} else { } else {
// try to open the file // try to open the file
Uri fileUri = Uri.parse(attachment.mContentUri); Uri fileUri = Uri.parse(attachment.getContentUri());
inStream = context.getContentResolver().openInputStream(fileUri); inStream = context.getContentResolver().openInputStream(fileUri);
} }
// switch to output stream for base64 text output // switch to output stream for base64 text output

View File

@ -1185,18 +1185,21 @@ public abstract class EmailContent {
public static Uri CONTENT_URI; public static Uri CONTENT_URI;
// This must be used with an appended id: ContentUris.withAppendedId(MESSAGE_ID_URI, id) // This must be used with an appended id: ContentUris.withAppendedId(MESSAGE_ID_URI, id)
public static Uri MESSAGE_ID_URI; public static Uri MESSAGE_ID_URI;
public static String ATTACHMENT_PROVIDER_URI_PREFIX;
public static void initAttachment() { public static void initAttachment() {
CONTENT_URI = Uri.parse(EmailContent.CONTENT_URI + "/attachment"); CONTENT_URI = Uri.parse(EmailContent.CONTENT_URI + "/attachment");
MESSAGE_ID_URI = Uri.parse( MESSAGE_ID_URI = Uri.parse(
EmailContent.CONTENT_URI + "/attachment/message"); EmailContent.CONTENT_URI + "/attachment/message");
ATTACHMENT_PROVIDER_URI_PREFIX = "content://" + EmailContent.EMAIL_PACKAGE_NAME +
".attachmentprovider";
} }
public String mFileName; public String mFileName;
public String mMimeType; public String mMimeType;
public long mSize; public long mSize;
public String mContentId; public String mContentId;
public String mContentUri; private String mContentUri;
public long mMessageKey; public long mMessageKey;
public String mLocation; public String mLocation;
public String mEncoding; public String mEncoding;
@ -1272,6 +1275,29 @@ public abstract class EmailContent {
mBaseUri = CONTENT_URI; 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 * Restore an Attachment from the database, given its unique id
* @param context * @param context

View File

@ -30,7 +30,6 @@ import android.util.Log;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import com.android.emailcommon.Logging; import com.android.emailcommon.Logging;
import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.EmailContent.Attachment; import com.android.emailcommon.provider.EmailContent.Attachment;
import com.android.emailcommon.provider.EmailContent.AttachmentColumns; import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
import com.android.emailcommon.provider.EmailContent.Body; import com.android.emailcommon.provider.EmailContent.Body;
@ -139,8 +138,7 @@ public class AttachmentUtilities {
private static Uri sUri; private static Uri sUri;
public static Uri getAttachmentUri(long accountId, long id) { public static Uri getAttachmentUri(long accountId, long id) {
if (sUri == null) { if (sUri == null) {
sUri = Uri.parse("content://" + EmailContent.EMAIL_PACKAGE_NAME + sUri = Uri.parse(Attachment.ATTACHMENT_PROVIDER_URI_PREFIX);
".attachmentprovider");
} }
return sUri.buildUpon() return sUri.buildUpon()
.appendPath(Long.toString(accountId)) .appendPath(Long.toString(accountId))

View File

@ -745,25 +745,28 @@ public class Utility {
return false; return false;
} else if (attachment.mContentBytes != null) { } else if (attachment.mContentBytes != null) {
return true; return true;
} else if (TextUtils.isEmpty(attachment.mContentUri)) { } else {
return false; String contentUri = attachment.getContentUri();
} if (TextUtils.isEmpty(contentUri)) {
try { return false;
Uri fileUri = Uri.parse(attachment.mContentUri); }
try { try {
InputStream inStream = context.getContentResolver().openInputStream(fileUri); Uri fileUri = Uri.parse(contentUri);
try { try {
inStream.close(); InputStream inStream = context.getContentResolver().openInputStream(fileUri);
} catch (IOException e) { try {
// Nothing to be done if can't close the stream inStream.close();
} } catch (IOException e) {
return true; // Nothing to be done if can't close the stream
} catch (FileNotFoundException e) { }
return true;
} catch (FileNotFoundException e) {
return false;
}
} catch (RuntimeException re) {
Log.w(Logging.LOG_TAG, "attachmentExists RuntimeException=" + re);
return false; 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: " + Log.d(Logging.LOG_TAG, "Unloaded attachment isn't marked for download: " +
att.mFileName + ", #" + att.mId); att.mFileName + ", #" + att.mId);
Attachment.delete(context, Attachment.CONTENT_URI, 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 // In this case, the attachment file is gone from the cache; let's clear the
// contentUri; this should be a very unusual case // contentUri; this should be a very unusual case
ContentValues cv = new ContentValues(); ContentValues cv = new ContentValues();

View File

@ -220,7 +220,7 @@ public class LegacyConversions {
localAttachment.mFileName = name; localAttachment.mFileName = name;
localAttachment.mSize = size; // May be reset below if file handled localAttachment.mSize = size; // May be reset below if file handled
localAttachment.mContentId = part.getContentId(); 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.mMessageKey = localMessage.mId;
localAttachment.mLocation = partId; localAttachment.mLocation = partId;
localAttachment.mEncoding = "B"; // TODO - convert other known encodings localAttachment.mEncoding = "B"; // TODO - convert other known encodings
@ -314,7 +314,7 @@ public class LegacyConversions {
accountId, attachmentId).toString(); accountId, attachmentId).toString();
localAttachment.mSize = copySize; localAttachment.mSize = copySize;
localAttachment.mContentUri = contentUriString; localAttachment.setContentUri(contentUriString);
// update the attachment in the database as well // update the attachment in the database as well
ContentValues cv = new ContentValues(); ContentValues cv = new ContentValues();

View File

@ -2490,7 +2490,7 @@ outer:
ArrayList<com.android.mail.providers.Attachment> uiAtts = ArrayList<com.android.mail.providers.Attachment> uiAtts =
new ArrayList<com.android.mail.providers.Attachment>(); new ArrayList<com.android.mail.providers.Attachment>();
for (Attachment att : atts) { for (Attachment att : atts) {
if (att.mContentId != null && att.mContentUri != null) { if (att.mContentId != null && att.getContentUri() != null) {
continue; continue;
} }
com.android.mail.providers.Attachment uiAtt = com.android.mail.providers.Attachment uiAtt =
@ -3463,7 +3463,7 @@ outer:
private Attachment convertUiAttachmentToAttachment( private Attachment convertUiAttachmentToAttachment(
com.android.mail.providers.Attachment uiAtt) { com.android.mail.providers.Attachment uiAtt) {
Attachment att = new Attachment(); Attachment att = new Attachment();
att.mContentUri = uiAtt.contentUri.toString(); att.setContentUri(uiAtt.contentUri.toString());
att.mFileName = uiAtt.name; att.mFileName = uiAtt.name;
att.mMimeType = uiAtt.contentType; att.mMimeType = uiAtt.contentType;
att.mSize = uiAtt.size; att.mSize = uiAtt.size;
@ -3633,7 +3633,7 @@ outer:
attClone.mMessageKey = 0; attClone.mMessageKey = 0;
// If we're sending this, it's not loaded, and we're not smart forwarding // 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 // 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)) { ((account.mFlags & Account.FLAGS_SUPPORTS_SMART_FORWARD) == 0)) {
attClone.mFlags |= Attachment.FLAG_DOWNLOAD_FORWARD; attClone.mFlags |= Attachment.FLAG_DOWNLOAD_FORWARD;
hasUnloadedAttachments = true; hasUnloadedAttachments = true;

View File

@ -980,8 +980,8 @@ public class AttachmentDownloadService extends Service implements Runnable {
suffix = fileName.substring(lastDot); suffix = fileName.substring(lastDot);
} }
pw.print(" Suffix: " + suffix); pw.print(" Suffix: " + suffix);
if (att.mContentUri != null) { if (att.getContentUri() != null) {
pw.print(" ContentUri: " + att.mContentUri); pw.print(" ContentUri: " + att.getContentUri());
} }
pw.print(" Mime: "); pw.print(" Mime: ");
if (att.mMimeType != null) { if (att.mMimeType != null) {