Merge "MTP: Add thumbnail generation for JPG" am: 5aac2c84ba am: d68b21d215 am: fe8e0b235f

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1657579

Change-Id: Ia6f614fe65c560681b2f076046a7e4295a3f3e86
diff --git a/media/java/android/mtp/MtpDatabase.java b/media/java/android/mtp/MtpDatabase.java
index 6141b7f..860d88a 100755
--- a/media/java/android/mtp/MtpDatabase.java
+++ b/media/java/android/mtp/MtpDatabase.java
@@ -805,8 +805,10 @@
             ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
 
-            if (byteStream.size() > MAX_THUMB_SIZE)
+            if (byteStream.size() > MAX_THUMB_SIZE) {
+                Log.w(TAG, "getThumbnailProcess: size=" + byteStream.size());
                 return null;
+            }
 
             byte[] byteArray = byteStream.toByteArray();
 
@@ -836,7 +838,15 @@
                     outLongs[0] = thumbOffsetAndSize != null ? thumbOffsetAndSize[1] : 0;
                     outLongs[1] = exif.getAttributeInt(ExifInterface.TAG_PIXEL_X_DIMENSION, 0);
                     outLongs[2] = exif.getAttributeInt(ExifInterface.TAG_PIXEL_Y_DIMENSION, 0);
-                    return true;
+                    if (exif.getThumbnailRange() != null) {
+                        if ((outLongs[0] == 0) || (outLongs[1] == 0) || (outLongs[2] == 0)) {
+                            Log.d(TAG, "getThumbnailInfo: check thumb info:"
+                                    + thumbOffsetAndSize[0] + "," + thumbOffsetAndSize[1]
+                                    + "," + outLongs[1] + "," + outLongs[2]);
+                        }
+
+                        return true;
+                    }
                 } catch (IOException e) {
                     // ignore and fall through
                 }
@@ -869,7 +879,9 @@
             case MtpConstants.FORMAT_JFIF:
                 try {
                     ExifInterface exif = new ExifInterface(path);
-                    return exif.getThumbnail();
+
+                    if (exif.getThumbnailRange() != null)
+                        return exif.getThumbnail();
                 } catch (IOException e) {
                     // ignore and fall through
                 }
diff --git a/media/tests/MtpTests/res/raw/test_bad_thumb.jpg b/media/tests/MtpTests/res/raw/test_bad_thumb.jpg
index e69de29..78ac703 100644
--- a/media/tests/MtpTests/res/raw/test_bad_thumb.jpg
+++ b/media/tests/MtpTests/res/raw/test_bad_thumb.jpg
Binary files differ
diff --git a/media/tests/MtpTests/src/android/mtp/MtpDatabaseTest.java b/media/tests/MtpTests/src/android/mtp/MtpDatabaseTest.java
index dfbafe4..48be6fe 100644
--- a/media/tests/MtpTests/src/android/mtp/MtpDatabaseTest.java
+++ b/media/tests/MtpTests/src/android/mtp/MtpDatabaseTest.java
@@ -271,9 +271,10 @@
 
         Log.d(TAG, "testMtpDatabaseThumbnail: Test bad JPG");
 
-        testThumbnail(handleJpgBadThumb, jpgfileBadThumb, false);
+// Now we support to generate thumbnail if embedded thumbnail is corrupted or not existed
+        testThumbnail(handleJpgBadThumb, jpgfileBadThumb, true);
 
-        testThumbnail(handleJpgNoThumb, jpgFileNoThumb, false);
+        testThumbnail(handleJpgNoThumb, jpgFileNoThumb, true);
 
         testThumbnail(handleJpgBad, jpgfileBad, false);