Merge "Only decode EXIF orientation for JPEG images." into jb-ub-mail-ur11
diff --git a/src/com/android/bitmap/DecodeTask.java b/src/com/android/bitmap/DecodeTask.java
index 2210ec6..8fa11f1 100644
--- a/src/com/android/bitmap/DecodeTask.java
+++ b/src/com/android/bitmap/DecodeTask.java
@@ -54,6 +54,7 @@
     public interface Request {
         AssetFileDescriptor createFd() throws IOException;
         InputStream createInputStream() throws IOException;
+        boolean hasOrientationExif() throws IOException;
     }
 
     /**
@@ -130,20 +131,26 @@
             Trace.endSection();
 
             Trace.beginSection("get orientation");
-            if (fd != null) {
-                // Creating an input stream from the file descriptor makes it useless afterwards.
-                Trace.beginSection("create fd and stream");
-                final AssetFileDescriptor orientationFd = mKey.createFd();
-                in = orientationFd.createInputStream();
-                Trace.endSection();
-            }
-            final int orientation = Exif.getOrientation(in, byteSize);
-            if (fd != null) {
-                try {
-                    // Close the temporary file descriptor.
-                    in.close();
-                } catch (IOException ex) {
+            final int orientation;
+            if (mKey.hasOrientationExif()) {
+                if (fd != null) {
+                    // Creating an input stream from the file descriptor makes it useless
+                    // afterwards.
+                    Trace.beginSection("create fd and stream");
+                    final AssetFileDescriptor orientationFd = mKey.createFd();
+                    in = orientationFd.createInputStream();
+                    Trace.endSection();
                 }
+                orientation = Exif.getOrientation(in, byteSize);
+                if (fd != null) {
+                    try {
+                        // Close the temporary file descriptor.
+                        in.close();
+                    } catch (IOException ex) {
+                    }
+                }
+            } else {
+                orientation = 0;
             }
             final boolean isNotRotatedOr180 = orientation == 0 || orientation == 180;
             Trace.endSection();
diff --git a/src/com/android/mail/bitmap/ImageAttachmentRequest.java b/src/com/android/mail/bitmap/ImageAttachmentRequest.java
index 6c58772..b680db5 100644
--- a/src/com/android/mail/bitmap/ImageAttachmentRequest.java
+++ b/src/com/android/mail/bitmap/ImageAttachmentRequest.java
@@ -1,6 +1,5 @@
 package com.android.mail.bitmap;
 
-import android.content.ContentResolver;
 import android.content.Context;
 import android.content.res.AssetFileDescriptor;
 import android.database.Cursor;
@@ -25,6 +24,9 @@
     private final int mRendition;
     public final int mDestW;
 
+    private Uri mCachedUri;
+    private String mCachedMimeType;
+
     public ImageAttachmentRequest(final Context context, final String lookupUri,
             final int rendition, final int destW) {
         mContext = context;
@@ -78,26 +80,40 @@
 
     @Override
     public AssetFileDescriptor createFd() throws IOException {
-        AssetFileDescriptor result = null;
+        if (mCachedUri == null) {
+            cacheValues();
+        }
+        return mContext.getContentResolver().openAssetFileDescriptor(mCachedUri, "r");
+    }
+
+    private void cacheValues() throws IOException {
         Cursor cursor = null;
-        final ContentResolver cr = mContext.getContentResolver();
         try {
-            cursor = cr.query(Uri.parse(mLookupUri), UIProvider.ATTACHMENT_PROJECTION, null, null,
-                    null);
+            cursor = mContext.getContentResolver().query(Uri.parse(mLookupUri),
+                    UIProvider.ATTACHMENT_PROJECTION, null, null, null);
             if (cursor != null && cursor.moveToFirst()) {
                 final Attachment a = new Attachment(cursor);
-                result = cr.openAssetFileDescriptor(a.getUriForRendition(mRendition), "r");
+                mCachedUri = a.getUriForRendition(mRendition);
+                final String mimeType = a.getContentType();
+                mCachedMimeType = mimeType != null ? mimeType.toLowerCase() : null;
             }
         } finally {
             if (cursor != null) {
                 cursor.close();
             }
         }
-        return result;
     }
 
     @Override
     public InputStream createInputStream() throws IOException {
         return null;
     }
+
+    @Override
+    public boolean hasOrientationExif() throws IOException {
+        if (mCachedUri == null) {
+            cacheValues();
+        }
+        return mCachedMimeType == null || mCachedMimeType.equals("image/jpeg");
+    }
 }