Improve exception handling for MediaStore#getOriginalMediaFormatFileDescriptor

Throw Parcel-writable exceptions from MediaProvider for MediaStore#getOriginalMediaFormatFileDescriptor
so that they can be automatically caught in MediaStore via Binder call.

Test: CtsMediaProviderTranscodeTests
Bug: 185818398
Change-Id: Ia931c2a5c9aa990803dddeaf78df58f805fdfb4d
diff --git a/apex/framework/java/android/provider/MediaStore.java b/apex/framework/java/android/provider/MediaStore.java
index 13ad86a..103e09e 100644
--- a/apex/framework/java/android/provider/MediaStore.java
+++ b/apex/framework/java/android/provider/MediaStore.java
@@ -928,10 +928,13 @@
             @NonNull ParcelFileDescriptor fileDescriptor) throws IOException {
         Bundle input = new Bundle();
         input.putParcelable(EXTRA_FILE_DESCRIPTOR, fileDescriptor);
-
-        Bundle output = context.getContentResolver().call(AUTHORITY,
-                GET_ORIGINAL_MEDIA_FORMAT_FILE_DESCRIPTOR_CALL, null, input);
-        return output.getParcelable(EXTRA_FILE_DESCRIPTOR);
+        try {
+            Bundle output = context.getContentResolver().call(AUTHORITY,
+                    GET_ORIGINAL_MEDIA_FORMAT_FILE_DESCRIPTOR_CALL, null, input);
+            return output.getParcelable(EXTRA_FILE_DESCRIPTOR);
+        } catch (Exception e) {
+            throw new IOException(e);
+        }
     }
 
     /**
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index 2df5d7e..7f54407 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -5707,13 +5707,15 @@
                     File file = getFileFromFileDescriptor(inputPfd);
                     boolean supportsTranscode = mTranscodeHelper.supportsTranscode(file.getPath());
                     if (!supportsTranscode) {
-                        throw new IOException("Input file descriptor is already original");
+                        throw new IllegalArgumentException(
+                                "Input file descriptor is already original");
                     }
 
                     FuseDaemon fuseDaemon = getFuseDaemonForFile(file);
                     String outputPath = fuseDaemon.getOriginalMediaFormatFilePath(inputPfd);
                     if (TextUtils.isEmpty(outputPath)) {
-                        throw new IOException("Invalid path for original media format file");
+                        throw new IllegalArgumentException(
+                                "Invalid path for original media format file");
                     }
 
                     int posixMode = Os.fcntlInt(inputPfd.getFileDescriptor(), F_GETFL,
@@ -5728,9 +5730,9 @@
                     res.putParcelable(MediaStore.EXTRA_FILE_DESCRIPTOR, outputPfd);
                     return res;
                 } catch (IOException e) {
-                    throw new RuntimeException(e);
+                    throw new IllegalStateException(e);
                 } catch (ErrnoException e) {
-                    throw new RuntimeException(
+                    throw new IllegalStateException(
                             "Failed to fetch access mode for file descriptor", e);
                 }
             }