Merge "Fix misleading transcoding exception" into sc-dev
diff --git a/apex/framework/java/android/provider/MediaStore.java b/apex/framework/java/android/provider/MediaStore.java
index 103e09e..2a17e34 100644
--- a/apex/framework/java/android/provider/MediaStore.java
+++ b/apex/framework/java/android/provider/MediaStore.java
@@ -928,13 +928,19 @@
             @NonNull ParcelFileDescriptor fileDescriptor) throws IOException {
         Bundle input = new Bundle();
         input.putParcelable(EXTRA_FILE_DESCRIPTOR, fileDescriptor);
+        ParcelFileDescriptor pfd;
         try {
             Bundle output = context.getContentResolver().call(AUTHORITY,
                     GET_ORIGINAL_MEDIA_FORMAT_FILE_DESCRIPTOR_CALL, null, input);
-            return output.getParcelable(EXTRA_FILE_DESCRIPTOR);
+            pfd = output.getParcelable(EXTRA_FILE_DESCRIPTOR);
         } catch (Exception e) {
             throw new IOException(e);
         }
+
+        if (pfd == null) {
+            throw new IOException("Input file descriptor already original");
+        }
+        return pfd;
     }
 
     /**
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index a16f6eb..2a8cb3b 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -5697,10 +5697,12 @@
                         extras.getParcelable(MediaStore.EXTRA_FILE_DESCRIPTOR);
                 try {
                     File file = getFileFromFileDescriptor(inputPfd);
-                    boolean supportsTranscode = mTranscodeHelper.supportsTranscode(file.getPath());
-                    if (!supportsTranscode) {
-                        throw new IllegalArgumentException(
-                                "Input file descriptor is already original");
+                    boolean isModernFormat = mTranscodeHelper.isModernFormat(file.getPath());
+                    if (!isModernFormat) {
+                        // Return an empty bundle instead of throwing an exception in the special
+                        // case where the file is not a modern format. This avoids a misleading
+                        // warning in android.database.DatabaseUtils#writeExceptionToParcel
+                        return new Bundle();
                     }
 
                     FuseDaemon fuseDaemon = getFuseDaemonForFile(file);
diff --git a/src/com/android/providers/media/TranscodeHelper.java b/src/com/android/providers/media/TranscodeHelper.java
index 596db57..e1ffbca 100644
--- a/src/com/android/providers/media/TranscodeHelper.java
+++ b/src/com/android/providers/media/TranscodeHelper.java
@@ -792,7 +792,19 @@
         return isHevc(mimeType) || isHdr10Plus(colorStandard, colorTransfer);
     }
 
-    public boolean supportsTranscode(String path) {
+    public boolean isModernFormat(String filePath) {
+        if (supportsTranscode(filePath)) {
+            Pair<Integer, Long> result = getFileFlagsAndDurationMs(filePath);
+            int fileFlags = result.first;
+            if (fileFlags != 0) {
+                // File is HEVC or HDR
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public static boolean supportsTranscode(String path) {
         File file = new File(path);
         String name = file.getName();
         final String cameraRelativePath =