/**
  * 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.getAttachmentUriForViewing(mAccount, part.getAttachmentId());
     Intent intent = new Intent(Intent.ACTION_VIEW);
     intent.setData(uri);
     intent.addFlags(
         Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
     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);
   }
 }
 private Bitmap getPreviewIcon() {
   Bitmap icon = null;
   try {
     InputStream input =
         mContext
             .getContentResolver()
             .openInputStream(
                 AttachmentProvider.getAttachmentThumbnailUri(
                     mAccount, part.getAttachmentId(), 62, 62));
     icon = BitmapFactory.decodeStream(input);
     input.close();
   } catch (Exception e) {
     /*
      * We don't care what happened, we just return null for the preview icon.
      */
   }
   return icon;
 }
 /**
  * Writes the attachment onto the given path
  *
  * @param directory: the base dir where the file should be saved.
  */
 public void writeFile(File directory) {
   try {
     String filename = Utility.sanitizeFilename(name);
     File file = Utility.createUniqueFile(directory, filename);
     Uri uri = AttachmentProvider.getAttachmentUri(mAccount, part.getAttachmentId());
     InputStream in = mContext.getContentResolver().openInputStream(uri);
     OutputStream out = new FileOutputStream(file);
     IOUtils.copy(in, out);
     out.flush();
     out.close();
     in.close();
     attachmentSaved(file.toString());
     new MediaScannerNotifier(mContext, file);
   } catch (IOException ioe) {
     if (K9.DEBUG) {
       Log.e(K9.LOG_TAG, "Error saving attachment", ioe);
     }
     attachmentNotSaved();
   }
 }
  public void showFile() {
    Uri uri = AttachmentProvider.getAttachmentUriForViewing(mAccount, part.getAttachmentId());
    Intent intent = new Intent(Intent.ACTION_VIEW);
    // We explicitly set the ContentType in addition to the URI because some attachment viewers
    // (such as Polaris office 3.0.x) choke on documents without a mime type
    intent.setDataAndType(uri, contentType);
    intent.addFlags(
        Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

    try {
      mContext.startActivity(intent);
    } catch (Exception e) {
      Log.e(K9.LOG_TAG, "Could not display attachment of type " + contentType, e);
      Toast toast =
          Toast.makeText(
              mContext,
              mContext.getString(R.string.message_view_no_viewer, contentType),
              Toast.LENGTH_LONG);
      toast.show();
    }
  }