/**
  * Returns the mime type for a given attachment. There are three possible results: - If thumbnail
  * Uri, always returns "image/png" (even if there's no attachment) - If the attachment does not
  * exist, returns null - Returns the mime type of the attachment
  */
 @Override
 public String getType(Uri uri) {
   long callingId = Binder.clearCallingIdentity();
   try {
     List<String> segments = uri.getPathSegments();
     String id = segments.get(1);
     int match = sURIMatcher.match(uri);
     String format = (match == ATTACHMENTS_CACHED_FILE_ACCESS) ? null : segments.get(2);
     if (AttachmentUtilities.FORMAT_THUMBNAIL.equals(format)) {
       return "image/png";
     } else {
       uri =
           (match == ATTACHMENTS_CACHED_FILE_ACCESS)
               ? rebuildUri(uri)
               : ContentUris.withAppendedId(Attachment.CONTENT_URI, Long.parseLong(id));
       Cursor c =
           getContext().getContentResolver().query(uri, MIME_TYPE_PROJECTION, null, null, null);
       try {
         if (c.moveToFirst()) {
           String mimeType = c.getString(MIME_TYPE_COLUMN_MIME_TYPE);
           String fileName = c.getString(MIME_TYPE_COLUMN_FILENAME);
           mimeType = AttachmentUtilities.inferMimeType(fileName, mimeType);
           return mimeType;
         }
       } finally {
         c.close();
       }
       return null;
     }
   } finally {
     Binder.restoreCallingIdentity(callingId);
   }
 }
  /**
   * Convert a MIME Part object into an Attachment object. Separated for unit testing.
   *
   * @param part MIME part object to convert
   * @return Populated Account object
   * @throws MessagingException
   */
  @VisibleForTesting
  protected static Attachment mimePartToAttachment(final Part part) throws MessagingException {
    // Transfer fields from mime format to provider format
    final String contentType = MimeUtility.unfoldAndDecode(part.getContentType());

    String name = MimeUtility.getHeaderParameter(contentType, "name");
    if (TextUtils.isEmpty(name)) {
      final String contentDisposition = MimeUtility.unfoldAndDecode(part.getDisposition());
      name = MimeUtility.getHeaderParameter(contentDisposition, "filename");
    }

    // Incoming attachment: Try to pull size from disposition (if not downloaded yet)
    long size = 0;
    final String disposition = part.getDisposition();
    if (!TextUtils.isEmpty(disposition)) {
      String s = MimeUtility.getHeaderParameter(disposition, "size");
      if (!TextUtils.isEmpty(s)) {
        try {
          size = Long.parseLong(s);
        } catch (final NumberFormatException e) {
          LogUtils.d(LogUtils.TAG, e, "Could not decode size \"%s\" from attachment part", size);
        }
      }
    }

    // Get partId for unloaded IMAP attachments (if any)
    // This is only provided (and used) when we have structure but not the actual attachment
    final String[] partIds = part.getHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA);
    final String partId = partIds != null ? partIds[0] : null;

    final Attachment localAttachment = new Attachment();

    // Run the mime type through inferMimeType in case we have something generic and can do
    // better using the filename extension
    localAttachment.mMimeType = AttachmentUtilities.inferMimeType(name, part.getMimeType());
    localAttachment.mFileName = name;
    localAttachment.mSize = size;
    localAttachment.mContentId = part.getContentId();
    localAttachment.setContentUri(null); // Will be rewritten by saveAttachmentBody
    localAttachment.mLocation = partId;
    localAttachment.mEncoding = "B"; // TODO - convert other known encodings

    return localAttachment;
  }