1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-23 18:02:15 -05:00

Fix viewing attachment with alternative MIME type

This commit is contained in:
cketti 2015-01-16 18:18:48 +01:00
parent de2eb25446
commit 658657447e
3 changed files with 27 additions and 15 deletions

View File

@ -12,6 +12,14 @@ public class AttachmentViewInfo {
public final String mimeType; public final String mimeType;
public final String displayName; public final String displayName;
public final long size; public final long size;
/**
* A content provider URI that can be used to retrieve the decoded attachment.
* <p/>
* Note: All content providers must support an alternative MIME type appended as last URI segment.
*
* @see com.fsck.k9.ui.messageview.AttachmentController#getAttachmentUriForMimeType(AttachmentViewInfo, String)
*/
public final Uri uri; public final Uri uri;
public final boolean firstClassAttachment; public final boolean firstClassAttachment;
public final Part part; public final Part part;

View File

@ -18,6 +18,7 @@ import com.fsck.k9.Account;
import com.fsck.k9.K9; import com.fsck.k9.K9;
import com.fsck.k9.Preferences; import com.fsck.k9.Preferences;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.MimeUtility;
import com.fsck.k9.mailstore.LocalStore; import com.fsck.k9.mailstore.LocalStore;
import com.fsck.k9.mailstore.LocalStore.AttachmentInfo; import com.fsck.k9.mailstore.LocalStore.AttachmentInfo;
import org.openintents.openpgp.util.ParcelFileDescriptorUtil; import org.openintents.openpgp.util.ParcelFileDescriptorUtil;
@ -25,17 +26,10 @@ import org.openintents.openpgp.util.ParcelFileDescriptorUtil;
/** /**
* A simple ContentProvider that allows file access to attachments. * A simple ContentProvider that allows file access to attachments.
* <p>
* Warning! We make heavy assumptions about the Uris used by the {@link LocalStore} for an
* {@link Account} here.
* </p>
*/ */
public class AttachmentProvider extends ContentProvider { public class AttachmentProvider extends ContentProvider {
public static final Uri CONTENT_URI = Uri.parse("content://com.fsck.k9.attachmentprovider"); 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[] DEFAULT_PROJECTION = new String[] { private static final String[] DEFAULT_PROJECTION = new String[] {
AttachmentProviderColumns._ID, AttachmentProviderColumns._ID,
AttachmentProviderColumns.DATA, AttachmentProviderColumns.DATA,
@ -53,7 +47,6 @@ public class AttachmentProvider extends ContentProvider {
return CONTENT_URI.buildUpon() return CONTENT_URI.buildUpon()
.appendPath(accountUuid) .appendPath(accountUuid)
.appendPath(Long.toString(id)) .appendPath(Long.toString(id))
.appendPath(FORMAT_RAW)
.build(); .build();
} }
@ -67,10 +60,9 @@ public class AttachmentProvider extends ContentProvider {
List<String> segments = uri.getPathSegments(); List<String> segments = uri.getPathSegments();
String accountUuid = segments.get(0); String accountUuid = segments.get(0);
String id = segments.get(1); String id = segments.get(1);
String format = segments.get(2); String mimeType = (segments.size() < 3) ? null : segments.get(2);
String mimeType = (segments.size() < 4) ? null : segments.get(3);
return getType(accountUuid, id, format, mimeType); return getType(accountUuid, id, mimeType);
} }
@Override @Override
@ -140,7 +132,7 @@ public class AttachmentProvider extends ContentProvider {
return null; return null;
} }
private String getType(String accountUuid, String id, String format, String mimeType) { private String getType(String accountUuid, String id, String mimeType) {
String type; String type;
final Account account = Preferences.getPreferences(getContext()).getAccount(accountUuid); final Account account = Preferences.getPreferences(getContext()).getAccount(accountUuid);
@ -148,14 +140,14 @@ public class AttachmentProvider extends ContentProvider {
final LocalStore localStore = LocalStore.getInstance(account, getContext()); final LocalStore localStore = LocalStore.getInstance(account, getContext());
AttachmentInfo attachmentInfo = localStore.getAttachmentInfo(id); AttachmentInfo attachmentInfo = localStore.getAttachmentInfo(id);
if (FORMAT_VIEW.equals(format) && mimeType != null) { if (mimeType != null) {
type = mimeType; type = mimeType;
} else { } else {
type = attachmentInfo.type; type = attachmentInfo.type;
} }
} catch (MessagingException e) { } catch (MessagingException e) {
Log.e(K9.LOG_TAG, "Unable to retrieve LocalStore for " + account, e); Log.e(K9.LOG_TAG, "Unable to retrieve LocalStore for " + account, e);
type = null; type = MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE;
} }
return type; return type;

View File

@ -158,14 +158,26 @@ public class AttachmentController {
} }
private Intent createViewIntentForAttachmentProviderUri(String mimeType) { private Intent createViewIntentForAttachmentProviderUri(String mimeType) {
Uri uri = getAttachmentUriForMimeType(attachment, mimeType);
Intent intent = new Intent(Intent.ACTION_VIEW); Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(attachment.uri, mimeType); intent.setDataAndType(uri, mimeType);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
addUiIntentFlags(intent); addUiIntentFlags(intent);
return intent; return intent;
} }
private Uri getAttachmentUriForMimeType(AttachmentViewInfo attachment, String mimeType) {
if (attachment.mimeType.equals(mimeType)) {
return attachment.uri;
}
return attachment.uri.buildUpon()
.appendPath(mimeType)
.build();
}
private Intent createViewIntentForFileUri(String mimeType, Uri uri) { private Intent createViewIntentForFileUri(String mimeType, Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW); Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, mimeType); intent.setDataAndType(uri, mimeType);