1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-08-13 17:03:48 -04:00

Extend AttachmentProvider to be able to get "raw" attachment

AttachmentProvider can now return an attachment with an unmodified
MIME type. However, when viewing attachments this is not desirable. So
we try hard to return a sensible MIME type (use extension to look up
a MIME type if it's originally "application/octet-stream"; or replace
with canonical MIME type if it's known to be wrong, e.g.
"image/jpg" -> "image/jpeg")
This commit is contained in:
cketti 2011-03-24 23:36:59 +01:00
parent 7c5c29e87e
commit 522634fba7
2 changed files with 29 additions and 14 deletions

View File

@ -30,6 +30,7 @@ public class AttachmentProvider extends ContentProvider {
public static final Uri CONTENT_URI = Uri.parse("content://com.fsck.k9.attachmentprovider");
private static final String FORMAT_RAW = "RAW";
private static final String FORMAT_VIEW = "VIEW";
private static final String FORMAT_THUMBNAIL = "THUMBNAIL";
public static class AttachmentProviderColumns {
@ -40,7 +41,11 @@ public class AttachmentProvider extends ContentProvider {
}
public static Uri getAttachmentUri(Account account, long id) {
return getAttachmentUri(account.getUuid(), id);
return getAttachmentUri(account.getUuid(), id, true);
}
public static Uri getAttachmentUriForViewing(Account account, long id) {
return getAttachmentUri(account.getUuid(), id, false);
}
public static Uri getAttachmentThumbnailUri(Account account, long id, int width, int height) {
@ -53,11 +58,11 @@ public class AttachmentProvider extends ContentProvider {
.build();
}
private static Uri getAttachmentUri(String db, long id) {
private static Uri getAttachmentUri(String db, long id, boolean raw) {
return CONTENT_URI.buildUpon()
.appendPath(db)
.appendPath(Long.toString(id))
.appendPath(FORMAT_RAW)
.appendPath(raw ? FORMAT_RAW : FORMAT_VIEW)
.build();
}
@ -98,6 +103,11 @@ public class AttachmentProvider extends ContentProvider {
String dbName = segments.get(0);
String id = segments.get(1);
String format = segments.get(2);
return getType(dbName, id, format);
}
private String getType(String dbName, String id, String format) {
if (FORMAT_THUMBNAIL.equals(format)) {
return "image/png";
} else {
@ -107,15 +117,21 @@ public class AttachmentProvider extends ContentProvider {
final LocalStore localStore = LocalStore.getLocalInstance(account, K9.app);
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);
if (FORMAT_VIEW.equals(format)) {
// When viewing the attachment we want the MIME type to be
// as sensible as possible. So we fix it up if necessary.
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);
}
} else {
// Some messages contain wrong MIME types. See if we know better.
return MimeUtility.canonicalizeMimeType(attachmentInfo.type);
// When accessing the "raw" message we deliver the original MIME type.
return attachmentInfo.type;
}
} catch (MessagingException e) {
Log.e(K9.LOG_TAG, "Unable to retrieve LocalStore for " + account, e);
return null;
@ -158,8 +174,7 @@ public class AttachmentProvider extends ContentProvider {
File dir = getContext().getCacheDir();
File file = new File(dir, filename);
if (!file.exists()) {
Uri attachmentUri = getAttachmentUri(dbName, Long.parseLong(id));
String type = getType(attachmentUri);
String type = getType(dbName, id, FORMAT_VIEW);
try {
FileInputStream in = new FileInputStream(getFile(dbName, id));
try {

View File

@ -196,7 +196,7 @@ public class AttachmentView extends FrameLayout {
public void showFile() {
Uri uri = AttachmentProvider.getAttachmentUri(mAccount, part.getAttachmentId());
Uri uri = AttachmentProvider.getAttachmentUriForViewing(mAccount, part.getAttachmentId());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
@ -227,7 +227,7 @@ public class AttachmentView extends FrameLayout {
return;
}
try {
Uri uri = AttachmentProvider.getAttachmentUri(mAccount, part.getAttachmentId());
Uri uri = AttachmentProvider.getAttachmentUriForViewing(mAccount, part.getAttachmentId());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);