From 2ca39c020f17e0815626df0fec417bacda2dc837 Mon Sep 17 00:00:00 2001 From: Manish Singh Date: Mon, 1 Feb 2021 21:36:59 +0000 Subject: [PATCH] Pass the media capabilities uid when opening media FileSystemProvider should pass the uid of the calling app by setting the EXTRA_MEDIA_CAPABILITIES_UID when opening a media, so that TranscodeHelper can make decision to transcode or not transcode based on the calling uid, and not based upon the Provider's uid. BUG=176157500 Test: manual testing. Change-Id: Ie08b94cf79c86e9f87639e4fbdd82b2732771187 --- .../internal/content/FileSystemProvider.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/core/java/com/android/internal/content/FileSystemProvider.java b/core/java/com/android/internal/content/FileSystemProvider.java index 0ede1b86b524d..e602cd2c8890e 100644 --- a/core/java/com/android/internal/content/FileSystemProvider.java +++ b/core/java/com/android/internal/content/FileSystemProvider.java @@ -28,6 +28,7 @@ import android.database.MatrixCursor; import android.database.MatrixCursor.RowBuilder; import android.graphics.Point; import android.net.Uri; +import android.os.Binder; import android.os.Bundle; import android.os.CancellationSignal; import android.os.FileObserver; @@ -504,7 +505,7 @@ public abstract class FileSystemProvider extends DocumentsProvider { final int pfdMode = ParcelFileDescriptor.parseMode(mode); if (pfdMode == ParcelFileDescriptor.MODE_READ_ONLY || visibleFile == null) { - return ParcelFileDescriptor.open(file, pfdMode); + return openFileForRead(file); } else { try { // When finished writing, kick off media scanner @@ -519,6 +520,24 @@ public abstract class FileSystemProvider extends DocumentsProvider { } } + private ParcelFileDescriptor openFileForRead(final File target) throws FileNotFoundException { + final Uri uri = MediaStore.scanFile(getContext().getContentResolver(), target); + + // Passing the calling uid via EXTRA_MEDIA_CAPABILITIES_UID, so that the decision to + // transcode or not transcode can be made based upon the calling app's uid, and not based + // upon the Provider's uid. + final Bundle opts = new Bundle(); + opts.putInt(MediaStore.EXTRA_MEDIA_CAPABILITIES_UID, Binder.getCallingUid()); + + final AssetFileDescriptor afd = + getContext().getContentResolver().openTypedAssetFileDescriptor(uri, "*/*", opts); + if (afd == null) { + return null; + } + + return afd.getParcelFileDescriptor(); + } + /** * Test if the file matches the query arguments. *