AI 149442: Integrate CL#140625 (Fix attachment uri and content uri confusion) from imode to donut.

BUG=1598754,1860250

Automated import of CL 149442
This commit is contained in:
Mihai Preda 2009-06-01 11:28:33 -07:00 committed by The Android Open Source Project
parent a8884b9b72
commit 5182d80674
4 changed files with 66 additions and 23 deletions

View File

@ -714,6 +714,29 @@ public class MessageView extends Activity
}
}
/**
* Resolve attachment id to content URI.
*
* @param attachmentUri
* @return resolved content URI
*/
private Uri resolveAttachmentIdToContentUri(long attachmentId) {
Uri attachmentUri = AttachmentProvider.getAttachmentUri(mAccount, attachmentId);
Cursor c = getContentResolver().query(attachmentUri,
new String[] { AttachmentProvider.AttachmentProviderColumns.DATA },
null, null, null);
if (c != null) {
try {
if (c.moveToFirst()) {
return Uri.parse(c.getString(0));
}
} finally {
c.close();
}
}
return attachmentUri;
}
/**
* Resolve content-id reference in src attribute of img tag to AttachmentProvider's
* content uri. This method calls itself recursively at most the number of
@ -737,15 +760,11 @@ public class MessageView extends Activity
contentId != null &&
part instanceof LocalAttachmentBodyPart) {
LocalAttachmentBodyPart attachment = (LocalAttachmentBodyPart)part;
Uri contentUri = AttachmentProvider.getAttachmentUri(
mAccount,
attachment.getAttachmentId());
if (contentUri != null) {
// Regexp which matches ' src="cid:contentId"'.
String contentIdRe = "\\s+(?i)src=\"cid(?-i):\\Q" + contentId + "\\E\"";
// Replace all occurrences of src attribute with ' src="content://contentUri"'.
text = text.replaceAll(contentIdRe, " src=\"" + contentUri + "\"");
}
Uri contentUri = resolveAttachmentIdToContentUri(attachment.getAttachmentId());
// Regexp which matches ' src="cid:contentId"'.
String contentIdRe = "\\s+(?i)src=\"cid(?-i):\\Q" + contentId + "\\E\"";
// Replace all occurrences of src attribute with ' src="content://contentUri"'.
text = text.replaceAll(contentIdRe, " src=\"" + contentUri + "\"");
}
if (part.getBody() instanceof Multipart) {
@ -1047,9 +1066,7 @@ public class MessageView extends Activity
try {
File file = createUniqueFile(Environment.getExternalStorageDirectory(),
attachment.name);
Uri uri = AttachmentProvider.getAttachmentUri(
mAccount,
attachment.part.getAttachmentId());
Uri uri = resolveAttachmentIdToContentUri(attachment.part.getAttachmentId());
InputStream in = getContentResolver().openInputStream(uri);
OutputStream out = new FileOutputStream(file);
IOUtils.copy(in, out);
@ -1065,9 +1082,7 @@ public class MessageView extends Activity
}
else {
try {
Uri uri = AttachmentProvider.getAttachmentUri(
mAccount,
attachment.part.getAttachmentId());
Uri uri = resolveAttachmentIdToContentUri(attachment.part.getAttachmentId());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

View File

@ -1339,6 +1339,7 @@ public class LocalStore extends Store implements PersistentDataCallbacks {
cv.put("content_uri", contentUri != null ? contentUri.toString() : null);
cv.put("size", size);
cv.put("content_id", contentId);
cv.put("message_id", messageId);
mDb.update(
"attachments",
cv,

View File

@ -155,12 +155,23 @@ public class AttachmentProvider extends ContentProvider {
File file = new File(dir, filename);
if (!file.exists()) {
Uri attachmentUri = getAttachmentUri(dbName, Long.parseLong(id));
String type = getType(attachmentUri);
Cursor c = query(attachmentUri,
new String[] { AttachmentProviderColumns.DATA }, null, null, null);
if (c != null) {
try {
if (c.moveToFirst()) {
attachmentUri = Uri.parse(c.getString(0));
}
} finally {
c.close();
}
}
String type = getContext().getContentResolver().getType(attachmentUri);
try {
FileInputStream in = new FileInputStream(
new File(getContext().getDatabasePath(dbName + "_att"), id));
InputStream in =
getContext().getContentResolver().openInputStream(attachmentUri);
Bitmap thumbnail = createThumbnail(type, in);
thumbnail = thumbnail.createScaledBitmap(thumbnail, width, height, true);
thumbnail = Bitmap.createScaledBitmap(thumbnail, width, height, true);
FileOutputStream out = new FileOutputStream(file);
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
@ -207,13 +218,14 @@ public class AttachmentProvider extends ContentProvider {
String path = getContext().getDatabasePath(dbName).getAbsolutePath();
String name = null;
int size = -1;
String contentUri = null;
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = SQLiteDatabase.openDatabase(path, null, 0);
cursor = db.query(
"attachments",
new String[] { "name", "size" },
new String[] { "name", "size", "content_uri" },
"id = ?",
new String[] { id },
null,
@ -224,6 +236,7 @@ public class AttachmentProvider extends ContentProvider {
}
name = cursor.getString(0);
size = cursor.getInt(1);
contentUri = cursor.getString(2);
}
finally {
if (cursor != null) {
@ -242,7 +255,7 @@ public class AttachmentProvider extends ContentProvider {
values[i] = id;
}
else if (AttachmentProviderColumns.DATA.equals(column)) {
values[i] = uri.toString();
values[i] = contentUri;
}
else if (AttachmentProviderColumns.DISPLAY_NAME.equals(column)) {
values[i] = name;

View File

@ -30,8 +30,10 @@ import com.android.email.mail.MessageTestUtils.TextBuilder;
import com.android.email.mail.internet.BinaryTempFileBody;
import com.android.email.mail.store.LocalStore;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.MediumTest;
@ -236,8 +238,20 @@ public class MessageViewTests
final String actual5 = a.resolveInlineImage(null, msg4, 0);
assertNull(actual5);
}
/**
* Test for resolveAttachmentIdToContentUri.
*/
public void testResolveAttachmentIdToContentUri() throws MessagingException, IOException {
final ContentResolver contentResolver = mContext.getContentResolver();
final MessageView a = getActivity();
// create attachments tables.
LocalStore.newInstance(mAccount.getLocalStoreUri(), mContext, null);
final String dbPath = mContext.getDatabasePath(mAccount.getUuid() + ".db").toString();
final SQLiteDatabase db = SQLiteDatabase.openDatabase(dbPath, null, 0);
// TODO write unit test
}
/**
* Mock Messaging controller, so we can drive its callbacks. This probably should be
* generalized since we're likely to use for other tests eventually.