diff --git a/src/com/fsck/k9/activity/MessageView.java b/src/com/fsck/k9/activity/MessageView.java index b28a028ef..c40ffce4f 100644 --- a/src/com/fsck/k9/activity/MessageView.java +++ b/src/com/fsck/k9/activity/MessageView.java @@ -382,6 +382,10 @@ public class MessageView extends K9Activity implements OnClickListener AttachmentView attachment = (AttachmentView) mAttachments.getChildAt(i); attachment.viewButton.setEnabled(enabled); attachment.downloadButton.setEnabled(enabled); + + if (enabled) { + attachment.checkViewable(); + } } } }); diff --git a/src/com/fsck/k9/view/AttachmentView.java b/src/com/fsck/k9/view/AttachmentView.java index bce21a9da..0ebcd3256 100644 --- a/src/com/fsck/k9/view/AttachmentView.java +++ b/src/com/fsck/k9/view/AttachmentView.java @@ -2,6 +2,7 @@ package com.fsck.k9.view; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; @@ -100,6 +101,8 @@ public class AttachmentView extends FrameLayout || (MimeUtility.mimeTypeMatches(contentType, K9.UNACCEPTABLE_ATTACHMENT_VIEW_TYPES))) { viewButton.setVisibility(View.GONE); + } else { + checkViewable(); } if ((!MimeUtility.mimeTypeMatches(contentType, K9.ACCEPTABLE_ATTACHMENT_DOWNLOAD_TYPES)) || (MimeUtility.mimeTypeMatches(contentType, K9.UNACCEPTABLE_ATTACHMENT_DOWNLOAD_TYPES))) @@ -247,6 +250,37 @@ public class AttachmentView extends FrameLayout } } + /** + * Check the {@link PackageManager} if the phone has an application + * installed to view this type of attachment. + * If not, {@link #viewButton} is disabled. + * This should be done in any place where + * attachment.viewButton.setEnabled(enabled); is called. + * This method is safe to be called from the UI-thread. + */ + public void checkViewable() { + if (viewButton.getVisibility() == View.GONE) { + // nothing to do + return; + } + if (!viewButton.isEnabled()) { + // nothing to do + return; + } + try { + Uri uri = AttachmentProvider.getAttachmentUri( mAccount, part.getAttachmentId()); + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(uri); + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + if (intent.resolveActivity(mContext.getPackageManager()) == null) { + viewButton.setEnabled(false); + } + // currently we do not cache re result. + } catch (Exception e) { + Log.e(K9.LOG_TAG, "Cannot resolve activity to determine if we shall show the 'view'-button for an attachment", e); + } + } + public void attachmentSaved(final String filename) { Toast.makeText(mContext, String.format(