Move MIME type fixing to AttachmentProvider

This commit is contained in:
cketti 2011-03-24 23:07:46 +01:00
parent f3e4618702
commit 7c5c29e87e
2 changed files with 16 additions and 35 deletions

View File

@ -988,49 +988,18 @@ public class LocalStore extends Store implements Serializable {
}
public String getAttachmentType(final String attachmentId) throws UnavailableStorageException {
return database.execute(false, new DbCallback<String>() {
@Override
public String doDbWork(final SQLiteDatabase db) throws WrappedException {
Cursor cursor = null;
try {
cursor = db.query(
"attachments",
new String[] { "mime_type", "name" },
"id = ?",
new String[] { attachmentId },
null,
null,
null);
cursor.moveToFirst();
String type = cursor.getString(0);
String name = cursor.getString(1);
cursor.close();
if (MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE.equalsIgnoreCase(type)) {
type = MimeUtility.getMimeTypeByExtension(name);
}
return type;
} finally {
if (cursor != null) {
cursor.close();
}
}
}
});
}
public AttachmentInfo getAttachmentInfo(final String attachmentId) throws UnavailableStorageException {
return database.execute(false, new DbCallback<AttachmentInfo>() {
@Override
public AttachmentInfo doDbWork(final SQLiteDatabase db) throws WrappedException {
String name;
String type;
int size;
Cursor cursor = null;
try {
cursor = db.query(
"attachments",
new String[] { "name", "size" },
new String[] { "name", "size", "mime_type" },
"id = ?",
new String[] { attachmentId },
null,
@ -1041,9 +1010,11 @@ public class LocalStore extends Store implements Serializable {
}
name = cursor.getString(0);
size = cursor.getInt(1);
type = cursor.getString(2);
final AttachmentInfo attachmentInfo = new AttachmentInfo();
attachmentInfo.name = name;
attachmentInfo.size = size;
attachmentInfo.type = type;
return attachmentInfo;
} finally {
if (cursor != null) {
@ -1057,6 +1028,7 @@ public class LocalStore extends Store implements Serializable {
public static class AttachmentInfo {
public String name;
public int size;
public String type;
}
public void createFolders(final List<LocalFolder> foldersToCreate, final int visibleLimit) throws UnavailableStorageException {

View File

@ -105,8 +105,17 @@ public class AttachmentProvider extends ContentProvider {
try {
final LocalStore localStore = LocalStore.getLocalInstance(account, K9.app);
return MimeUtility.canonicalizeMimeType(localStore.getAttachmentType(id));
AttachmentInfo attachmentInfo = localStore.getAttachmentInfo(id);
if (MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE.equalsIgnoreCase(attachmentInfo.type)) {
// If the MIME type is the generic "application/octet-stream"
// we try to find a better one by looking at the file extension.
return MimeUtility.getMimeTypeByExtension(attachmentInfo.name);
} else {
// Some messages contain wrong MIME types. See if we know better.
return MimeUtility.canonicalizeMimeType(attachmentInfo.type);
}
} catch (MessagingException e) {
Log.e(K9.LOG_TAG, "Unable to retrieve LocalStore for " + account, e);
return null;