Merge "Support sharing with image/video/audio/vcard and text fully"
diff --git a/src/com/android/messaging/sms/MmsUtils.java b/src/com/android/messaging/sms/MmsUtils.java
index fbecd8b..df0db34 100644
--- a/src/com/android/messaging/sms/MmsUtils.java
+++ b/src/com/android/messaging/sms/MmsUtils.java
@@ -333,20 +333,26 @@
             String srcName;
             if (part.isAttachment()) {
                 String contentType = part.getContentType();
+                final String extension = ContentType.getExtensionFromMimeType(contentType);
                 if (ContentType.isImageType(contentType)) {
-                    // There's a good chance that if we selected the image from our media picker the
-                    // content type is image/*. Fix the content type here for gifs so that we only
-                    // need to open the input stream once. All other gif vs static image checks will
-                    // only have to do a string comparison which is much cheaper.
-                    final boolean isGif = ImageUtils.isGif(contentType, part.getContentUri());
-                    contentType = isGif ? ContentType.IMAGE_GIF : contentType;
-                    srcName = String.format(isGif ? "image%06d.gif" : "image%06d.jpg", index);
+                    if (extension != null) {
+                        srcName = String.format("image%06d.%s", index, extension);
+                    } else {
+                        // There's a good chance that if we selected the image from our media picker
+                        // the content type is image/*. Fix the content type here for gifs so that
+                        // we only need to open the input stream once. All other gif vs static image
+                        // checks will only have to do a string comparison which is much cheaper.
+                        final boolean isGif = ImageUtils.isGif(contentType, part.getContentUri());
+                        contentType = isGif ? ContentType.IMAGE_GIF : contentType;
+                        srcName = String.format(isGif ? "image%06d.gif" : "image%06d.jpg", index);
+                    }
                     smilBody.append(String.format(sSmilImagePart, srcName));
                     totalLength += addPicturePart(context, pb, index, part,
                             widthLimit, heightLimit, bytesPerImage, srcName, contentType);
                     hasVisualAttachment = true;
                 } else if (ContentType.isVideoType(contentType)) {
-                    srcName = String.format("video%06d.mp4", index);
+                    srcName = String.format("video%06d.%s", index,
+                            extension != null ? extension : "mp4");
                     final int length = addVideoPart(context, pb, part, srcName);
                     totalLength += length;
                     smilBody.append(String.format(sSmilVideoPart, srcName,
@@ -358,7 +364,8 @@
                     smilBody.append(String.format(sSmilPart, srcName));
                     hasNonVisualAttachment = true;
                 } else if (ContentType.isAudioType(contentType)) {
-                    srcName = String.format("recording%06d.amr", index);
+                    srcName = String.format("recording%06d.%s",
+                            index, extension != null ? extension : "amr");
                     totalLength += addOtherPart(context, pb, part, srcName);
                     final int duration = getMediaDurationMs(context, part, -1);
                     Assert.isTrue(duration != -1);
diff --git a/src/com/android/messaging/ui/AsyncImageView.java b/src/com/android/messaging/ui/AsyncImageView.java
index e37966a..7d057b3 100644
--- a/src/com/android/messaging/ui/AsyncImageView.java
+++ b/src/com/android/messaging/ui/AsyncImageView.java
@@ -125,6 +125,8 @@
                 return;
             }
             unbindView();
+        } else {
+            mDetachedRequestDescriptor = null;
         }
         setImage(null);
         resetTransientViewStates();
diff --git a/src/com/android/messaging/ui/conversation/ConversationFragment.java b/src/com/android/messaging/ui/conversation/ConversationFragment.java
index fbc6730..77e33ac 100644
--- a/src/com/android/messaging/ui/conversation/ConversationFragment.java
+++ b/src/com/android/messaging/ui/conversation/ConversationFragment.java
@@ -564,6 +564,7 @@
                             @Override
                             public void run() {
                                 view.setAlpha(1);
+                                dispatchAddFinished(holder);
                             }
                         });
                     mPopupTransitionAnimation.startAfterLayoutComplete();
diff --git a/src/com/android/messaging/util/ContentType.java b/src/com/android/messaging/util/ContentType.java
index bb4a7b2..75fff93 100644
--- a/src/com/android/messaging/util/ContentType.java
+++ b/src/com/android/messaging/util/ContentType.java
@@ -168,6 +168,12 @@
         return contentType;
     }
 
+    public static String getExtensionFromMimeType(final String mimeType) {
+        final MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
+        final String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
+        return extension;
+    }
+
     /**
      * Get the common file extension for a given content type
      * @param contentType The content type
diff --git a/src/com/android/messaging/util/FileUtil.java b/src/com/android/messaging/util/FileUtil.java
index e35e79b..7d59aa7 100644
--- a/src/com/android/messaging/util/FileUtil.java
+++ b/src/com/android/messaging/util/FileUtil.java
@@ -21,7 +21,6 @@
 import android.net.Uri;
 import android.os.Environment;
 import android.text.TextUtils;
-import android.webkit.MimeTypeMap;
 
 import com.android.messaging.Factory;
 import com.android.messaging.R;
@@ -62,9 +61,7 @@
      *   actually creating the file.
      */
     public static File getNewFile(File directory, String contentType) throws IOException {
-        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
-        String fileExtension = mimeTypeMap.getExtensionFromMimeType(contentType);
-
+        String fileExtension = ContentType.getExtensionFromMimeType(contentType);
         final Context context = Factory.get().getApplicationContext();
         String fileNameFormat = context.getString(ContentType.isImageType(contentType)
                 ? R.string.new_image_file_name_format : R.string.new_file_name_format);
diff --git a/src/com/android/messaging/util/exif/ExifParser.java b/src/com/android/messaging/util/exif/ExifParser.java
index 4b6cf68..46f253e 100644
--- a/src/com/android/messaging/util/exif/ExifParser.java
+++ b/src/com/android/messaging/util/exif/ExifParser.java
@@ -224,8 +224,8 @@
         mIfdType = IfdId.TYPE_IFD_0;
         if (isIfdRequested(IfdId.TYPE_IFD_0) || needToParseOffsetsInCurrentIfd()) {
             registerIfd(IfdId.TYPE_IFD_0, offset);
-            if (offset != DEFAULT_IFD0_OFFSET) {
-                mDataAboveIfd0 = new byte[(int) offset - DEFAULT_IFD0_OFFSET];
+            if (mIfd0Position > DEFAULT_IFD0_OFFSET) {
+                mDataAboveIfd0 = new byte[mIfd0Position - DEFAULT_IFD0_OFFSET];
                 read(mDataAboveIfd0);
             }
         }
@@ -558,7 +558,9 @@
             }
             // Some invalid images put some undefined data before IFD0.
             // Read the data here.
-            if ((offset < mIfd0Position) && (dataFormat == ExifTag.TYPE_UNDEFINED)) {
+            if (mDataAboveIfd0 != null
+                    && offset < mIfd0Position
+                    && dataFormat == ExifTag.TYPE_UNDEFINED) {
                 byte[] buf = new byte[(int) numOfComp];
                 System.arraycopy(mDataAboveIfd0, (int) offset - DEFAULT_IFD0_OFFSET,
                         buf, 0, (int) numOfComp);