mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-23 18:02:15 -05:00
Change the way the best view intent is determined
First we try the original MIME type unless it's application/octet-stream. Then we try the MIME type inferred from the attachment's file extension. Then we fall back to application/octet-stream. In all cases we first try the content:// URI, then a file:// URI.
This commit is contained in:
parent
87ca0d3d2a
commit
d9b6e10cbe
@ -1138,20 +1138,8 @@ public class MimeUtility {
|
|||||||
return p.matcher(mimeType).matches();
|
return p.matcher(mimeType).matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static boolean isDefaultMimeType(String mimeType) {
|
||||||
* Returns true if the given mimeType matches any of the matchAgainst specifications.
|
return DEFAULT_ATTACHMENT_MIME_TYPE.equalsIgnoreCase(mimeType);
|
||||||
* @param mimeType A MIME type to check.
|
|
||||||
* @param matchAgainst An array of MIME types to check against. May include wildcards such
|
|
||||||
* as image/* or * /*.
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static boolean mimeTypeMatches(String mimeType, String[] matchAgainst) {
|
|
||||||
for (String matchType : matchAgainst) {
|
|
||||||
if (mimeTypeMatches(mimeType, matchType)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -267,89 +267,68 @@ public class AttachmentView extends FrameLayout implements OnClickListener, OnLo
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Intent getBestViewIntentAndSaveFileIfNecessary() {
|
private Intent getBestViewIntentAndSaveFileIfNecessary() {
|
||||||
IntentAndResolvedActivitiesCount resultForContentUri = getBestViewIntentForContentUri();
|
String inferredMimeType = MimeUtility.getMimeTypeByExtension(name);
|
||||||
if (resultForContentUri.getActivitiesCount() > 0) {
|
|
||||||
return resultForContentUri.getIntent();
|
IntentAndResolvedActivitiesCount resolvedIntentInfo;
|
||||||
|
if (MimeUtility.isDefaultMimeType(contentType)) {
|
||||||
|
resolvedIntentInfo = getBestViewIntentForMimeType(inferredMimeType);
|
||||||
|
} else {
|
||||||
|
resolvedIntentInfo = getBestViewIntentForMimeType(contentType);
|
||||||
|
if (!resolvedIntentInfo.hasResolvedActivities() && !inferredMimeType.equals(contentType)) {
|
||||||
|
resolvedIntentInfo = getBestViewIntentForMimeType(inferredMimeType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IntentAndResolvedActivitiesCount resultForFileUri = getBestViewIntentForFileUri();
|
if (!resolvedIntentInfo.hasResolvedActivities()) {
|
||||||
if (resultForFileUri.getActivitiesCount() > 0) {
|
resolvedIntentInfo = getBestViewIntentForMimeType(MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
Intent viewIntent;
|
||||||
|
if (resolvedIntentInfo.hasResolvedActivities() && resolvedIntentInfo.containsFileUri()) {
|
||||||
try {
|
try {
|
||||||
File tempFile = TemporaryAttachmentStore.getFile(context, name);
|
File tempFile = TemporaryAttachmentStore.getFile(context, name);
|
||||||
writeAttachmentToStorage(tempFile);
|
writeAttachmentToStorage(tempFile);
|
||||||
return createViewIntentForFileUri(resultForFileUri.getMimeType(), Uri.fromFile(tempFile));
|
viewIntent = createViewIntentForFileUri(resolvedIntentInfo.getMimeType(), Uri.fromFile(tempFile));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (K9.DEBUG) {
|
if (K9.DEBUG) {
|
||||||
Log.e(K9.LOG_TAG, "Error while saving attachment to use file:// URI with ACTION_VIEW Intent", e);
|
Log.e(K9.LOG_TAG, "Error while saving attachment to use file:// URI with ACTION_VIEW Intent", e);
|
||||||
}
|
}
|
||||||
|
viewIntent = createViewIntentForAttachmentProviderUri(MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return resultForContentUri.getIntent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private IntentAndResolvedActivitiesCount getBestViewIntentForContentUri() {
|
|
||||||
Intent intent;
|
|
||||||
int activitiesCount;
|
|
||||||
|
|
||||||
Uri originalMimeTypeUri = AttachmentProvider.getAttachmentUriForViewing(account, part.getAttachmentId(),
|
|
||||||
contentType, name);
|
|
||||||
Intent originalMimeTypeIntent = createViewIntentForContentUri(contentType, originalMimeTypeUri);
|
|
||||||
int originalMimeTypeActivitiesCount = getResolvedIntentActivitiesCount(originalMimeTypeIntent);
|
|
||||||
|
|
||||||
String inferredMimeType = MimeUtility.getMimeTypeByExtension(name);
|
|
||||||
if (inferredMimeType.equals(contentType)) {
|
|
||||||
intent = originalMimeTypeIntent;
|
|
||||||
activitiesCount = originalMimeTypeActivitiesCount;
|
|
||||||
} else {
|
} else {
|
||||||
Uri inferredMimeTypeUri = AttachmentProvider.getAttachmentUriForViewing(account, part.getAttachmentId(),
|
viewIntent = resolvedIntentInfo.getIntent();
|
||||||
inferredMimeType, name);
|
|
||||||
Intent inferredMimeTypeIntent = createViewIntentForContentUri(inferredMimeType, inferredMimeTypeUri);
|
|
||||||
int inferredMimeTypeActivitiesCount = getResolvedIntentActivitiesCount(inferredMimeTypeIntent);
|
|
||||||
|
|
||||||
if (inferredMimeTypeActivitiesCount > originalMimeTypeActivitiesCount) {
|
|
||||||
intent = inferredMimeTypeIntent;
|
|
||||||
activitiesCount = inferredMimeTypeActivitiesCount;
|
|
||||||
} else {
|
|
||||||
intent = originalMimeTypeIntent;
|
|
||||||
activitiesCount = originalMimeTypeActivitiesCount;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new IntentAndResolvedActivitiesCount(intent, activitiesCount);
|
return viewIntent;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IntentAndResolvedActivitiesCount getBestViewIntentForFileUri() {
|
private IntentAndResolvedActivitiesCount getBestViewIntentForMimeType(String mimeType) {
|
||||||
Intent intent;
|
Intent contentUriIntent = createViewIntentForAttachmentProviderUri(mimeType);
|
||||||
int activitiesCount;
|
int contentUriActivitiesCount = getResolvedIntentActivitiesCount(contentUriIntent);
|
||||||
|
|
||||||
|
if (contentUriActivitiesCount > 0) {
|
||||||
|
return new IntentAndResolvedActivitiesCount(contentUriIntent, contentUriActivitiesCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
Uri dummyFileUri = getDummyFileUri();
|
||||||
|
Intent fileUriIntent = createViewIntentForFileUri(mimeType, dummyFileUri);
|
||||||
|
int fileUriActivitiesCount = getResolvedIntentActivitiesCount(fileUriIntent);
|
||||||
|
|
||||||
|
if (fileUriActivitiesCount > 0) {
|
||||||
|
return new IntentAndResolvedActivitiesCount(fileUriIntent, fileUriActivitiesCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new IntentAndResolvedActivitiesCount(contentUriIntent, contentUriActivitiesCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Uri getDummyFileUri() {
|
||||||
File dummyFile = new File(FileHelper.sanitizeFilename(name));
|
File dummyFile = new File(FileHelper.sanitizeFilename(name));
|
||||||
Uri fileUri = Uri.fromFile(dummyFile);
|
return Uri.fromFile(dummyFile);
|
||||||
|
|
||||||
Intent originalMimeTypeIntent = createViewIntentForFileUri(contentType, fileUri);
|
|
||||||
int originalMimeTypeActivitiesCount = getResolvedIntentActivitiesCount(originalMimeTypeIntent);
|
|
||||||
|
|
||||||
String inferredMimeType = MimeUtility.getMimeTypeByExtension(name);
|
|
||||||
if (inferredMimeType.equals(contentType)) {
|
|
||||||
intent = originalMimeTypeIntent;
|
|
||||||
activitiesCount = originalMimeTypeActivitiesCount;
|
|
||||||
} else {
|
|
||||||
Intent inferredMimeTypeIntent = createViewIntentForFileUri(inferredMimeType, fileUri);
|
|
||||||
int inferredMimeTypeActivitiesCount = getResolvedIntentActivitiesCount(inferredMimeTypeIntent);
|
|
||||||
|
|
||||||
if (inferredMimeTypeActivitiesCount > originalMimeTypeActivitiesCount) {
|
|
||||||
intent = inferredMimeTypeIntent;
|
|
||||||
activitiesCount = inferredMimeTypeActivitiesCount;
|
|
||||||
} else {
|
|
||||||
intent = originalMimeTypeIntent;
|
|
||||||
activitiesCount = originalMimeTypeActivitiesCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new IntentAndResolvedActivitiesCount(intent, activitiesCount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Intent createViewIntentForContentUri(String mimeType, Uri uri) {
|
private Intent createViewIntentForAttachmentProviderUri(String mimeType) {
|
||||||
|
Uri uri = AttachmentProvider.getAttachmentUriForViewing(account, part.getAttachmentId(), mimeType, name);
|
||||||
|
|
||||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||||
intent.setDataAndType(uri, mimeType);
|
intent.setDataAndType(uri, mimeType);
|
||||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||||
@ -422,13 +401,17 @@ public class AttachmentView extends FrameLayout implements OnClickListener, OnLo
|
|||||||
return intent;
|
return intent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getActivitiesCount() {
|
public boolean hasResolvedActivities() {
|
||||||
return activitiesCount;
|
return activitiesCount > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMimeType() {
|
public String getMimeType() {
|
||||||
return intent.getType();
|
return intent.getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean containsFileUri() {
|
||||||
|
return "file".equals(intent.getData().getScheme());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class LoadAndDisplayThumbnailAsyncTask extends AsyncTask<Void, Void, Bitmap> {
|
private class LoadAndDisplayThumbnailAsyncTask extends AsyncTask<Void, Void, Bitmap> {
|
||||||
|
Loading…
Reference in New Issue
Block a user