SnapdraongGallery:add HEIF support

When edit HEIF image, save the new
image with HEIF format.

Change-Id: Ib26a86143176a0d3187eba7d62b6475fd1eab5af
diff --git a/Android.mk b/Android.mk
old mode 100644
new mode 100755
index a91f767..77a0d93
--- a/Android.mk
+++ b/Android.mk
@@ -11,6 +11,7 @@
 LOCAL_STATIC_JAVA_LIBRARIES += xmp_toolkit
 LOCAL_STATIC_JAVA_LIBRARIES += mp4parser
 #LOCAL_STATIC_JAVA_LIBRARIES += android-support-v8-renderscript
+LOCAL_STATIC_JAVA_LIBRARIES += androidx.heifwriter_heifwriter
 
 #LOCAL_RENDERSCRIPT_TARGET_API := 18
 #LOCAL_RENDERSCRIPT_COMPATIBILITY := 18
diff --git a/src/com/android/gallery3d/filtershow/filters/SaveWaterMark.java b/src/com/android/gallery3d/filtershow/filters/SaveWaterMark.java
old mode 100644
new mode 100755
index ffa502f..d72681f
--- a/src/com/android/gallery3d/filtershow/filters/SaveWaterMark.java
+++ b/src/com/android/gallery3d/filtershow/filters/SaveWaterMark.java
@@ -106,8 +106,15 @@
                     }
                     destinationBitmap = Bitmap.createScaledBitmap(destinationBitmap, w, h, true);
                 }
-                if (SaveImage.putExifData(destinationFile, mExif, destinationBitmap, quality)) {
-                    saveUri = SaveImage.linkNewFileToUri(context, selectedUri, destinationFile, time, false);
+                boolean saveHeif = destinationFile.getName().endsWith(SaveImage.POSTFIX_HEIC);
+                if (saveHeif) {
+                    if (SaveImage.putHeifData(destinationFile, mExif, destinationBitmap, quality)) {
+                        saveUri = SaveImage.linkNewFileToUri(context, selectedUri, destinationFile, time, false);
+                    }
+                } else {
+                    if (SaveImage.putExifData(destinationFile, mExif, destinationBitmap, quality)) {
+                        saveUri = SaveImage.linkNewFileToUri(context, selectedUri, destinationFile, time, false);
+                    }
                 }
                 destinationBitmap.recycle();
                 if (saveUri != selectedUri) {
diff --git a/src/com/android/gallery3d/filtershow/tools/SaveImage.java b/src/com/android/gallery3d/filtershow/tools/SaveImage.java
old mode 100644
new mode 100755
index 6faae66..01da06e
--- a/src/com/android/gallery3d/filtershow/tools/SaveImage.java
+++ b/src/com/android/gallery3d/filtershow/tools/SaveImage.java
@@ -63,6 +63,7 @@
 import com.android.gallery3d.filtershow.pipeline.ImagePreset;
 import com.android.gallery3d.filtershow.pipeline.ProcessingService;
 import com.android.gallery3d.util.XmpUtilHelper;
+import androidx.heifwriter.HeifWriter;
 
 /**
  * Handles saving edited photo
@@ -88,10 +89,12 @@
     private static final String POSTFIX_JPG = ".jpg";
     private static final String AUX_DIR_NAME = ".aux";
 
+    public static final String POSTFIX_HEIC = ".heic";
+
     private final Context mContext;
     private final Uri mSourceUri;
-    private final Callback mCallback;
     private final File mDestinationFile;
+    private final Callback mCallback;
     private final Uri mSelectedImageUri;
     private final Bitmap mPreviewImage;
 
@@ -174,6 +177,9 @@
         if (hasPanoPrefix(context, sourceUri)) {
             return new File(saveDirectory, PREFIX_PANO + filename + POSTFIX_JPG);
         }
+        if (hasHeifPostfix(context, sourceUri)) {
+            return new File(saveDirectory, PREFIX_IMG + filename + POSTFIX_HEIC);
+        }
         return new File(saveDirectory, PREFIX_IMG + filename + POSTFIX_JPG);
     }
 
@@ -298,6 +304,34 @@
         return ret;
     }
 
+    public static boolean putHeifData(File file, ExifInterface exif, Bitmap image,
+                                      int compressQuality) {
+        boolean ret = false;
+        String path = file.getAbsolutePath();
+        if (image != null) {
+            try {
+                HeifWriter.Builder builder =
+                        new HeifWriter.Builder(path,image.getWidth(), image.getHeight(),
+                                HeifWriter.INPUT_MODE_BITMAP);
+                builder.setQuality(compressQuality);
+                builder.setMaxImages(1);
+                builder.setPrimaryIndex(0);
+                builder.setRotation(0);
+                HeifWriter heifWriter = builder.build();
+                heifWriter.start();
+                heifWriter.addBitmap(image);
+                heifWriter.stop(3000);
+                heifWriter.close();
+                ret = true;
+            } catch (IOException|IllegalStateException e) {
+                e.printStackTrace();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        return ret;
+    }
+
     private Uri resetToOriginalImageIfNeeded(ImagePreset preset, boolean doAuxBackup) {
         Uri uri = null;
         if (!preset.hasModifications()) {
@@ -355,6 +389,9 @@
         int num_tries = 0;
         int sampleSize = 1;
 
+        File sourceFile = getLocalFileFromUri(mContext,mSourceUri);
+        boolean saveHeif = sourceFile.getName().endsWith(POSTFIX_HEIC);
+
         // If necessary, move the source file into the auxiliary directory,
         // newSourceUri is then pointing to the new location.
         // If no file is moved, newSourceUri will be the same as mSourceUri.
@@ -473,10 +510,17 @@
                 updateProgress();
 
                 // If we succeed in writing the bitmap as a jpeg, return a uri.
-                if (putExifData(mDestinationFile, exif, bitmap, quality)) {
-                    putPanoramaXMPData(mDestinationFile, xmp);
+                boolean success = false;
+                if (saveHeif) {
+                    success = putHeifData(mDestinationFile, exif, bitmap, quality);
+                } else {
+                    success = putExifData(mDestinationFile, exif, bitmap, quality);
+                }
+                if (success) {
+                    if (!saveHeif)
+                        putPanoramaXMPData(mDestinationFile, xmp);
                     // mDestinationFile will save the newSourceUri info in the XMP.
-                    if (!flatten) {
+                    if (!flatten && !saveHeif) {
                         XmpPresets.writeFilterXMP(mContext, newSourceUri,
                                 mDestinationFile, preset);
                         uri = updateFile(mContext, savedUri, mDestinationFile, time);
@@ -707,6 +751,11 @@
         return name != null && name.startsWith(PREFIX_PANO);
     }
 
+    private static boolean hasHeifPostfix(Context context, Uri src) {
+        String name = getTrueFilename(context,src);
+        return name != null && name.endsWith(POSTFIX_HEIC);
+    }
+
     /**
      * If the <code>sourceUri</code> is a local content Uri, update the
      * <code>sourceUri</code> to point to the <code>file</code>.
@@ -741,6 +790,9 @@
 
     public static Uri updateFile(Context context, Uri sourceUri, File file, long time) {
         final ContentValues values = getContentValues(context, sourceUri, file, time);
+        if (file.getName().endsWith(".heic")) {
+            values.put(Images.Media.MIME_TYPE, "image/heif");
+        }
         context.getContentResolver().update(sourceUri, values, null, null);
         return sourceUri;
     }
@@ -752,7 +804,11 @@
         time /= 1000;
         values.put(Images.Media.TITLE, file.getName());
         values.put(Images.Media.DISPLAY_NAME, file.getName());
-        values.put(Images.Media.MIME_TYPE, "image/jpeg");
+        if (file.getName().endsWith(".heic")) {
+            values.put(Images.Media.MIME_TYPE, "image/heif");
+        } else {
+            values.put(Images.Media.MIME_TYPE, "image/jpeg");
+        }
         values.put(Images.Media.DATE_TAKEN, time);
         values.put(Images.Media.DATE_MODIFIED, time);
         values.put(Images.Media.DATE_ADDED, time);