Use correct attachment provider authority

* Make sure new code works with old Attachment rows

Bug: 7130972
Change-Id: Ia5d258930f39266517eead535a92bb4380fdb594
This commit is contained in:
Marc Blank 2012-09-08 10:36:32 -07:00
parent fdb1635868
commit 6e5bccf2c9
7 changed files with 57 additions and 30 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2487,7 +2487,7 @@ outer:
ArrayList<com.android.mail.providers.Attachment> uiAtts =
new ArrayList<com.android.mail.providers.Attachment>();
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 =
@ -3459,7 +3459,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;
@ -3629,7 +3629,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;

View File

@ -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) {