diff options
author | 2020-11-13 20:12:04 +0000 | |
---|---|---|
committer | 2021-01-15 22:39:58 +0000 | |
commit | 223e3b5c8874c110eb48b7e6d9fba4ef22dab5b8 (patch) | |
tree | 7f8057abd33971d86e4cde7cf191f7a4930285f6 | |
parent | 558c605944d0c7f766e2916bd8f238dd9db760ec (diff) |
Support transcode via MTP
Add a helper method to open files via MediaProvider ContentResolver.
Note that we are optimizing for the current default transcode option - B
(which is to transcode) and therefore we include the bundle only when we
do not want to transcode.
Test: Manual
Bug: 158466651
Change-Id: I48436ef143feb889b523a9c0e620c2312157ef89
-rwxr-xr-x | media/java/android/mtp/MtpDatabase.java | 31 | ||||
-rw-r--r-- | media/jni/android_mtp_MtpDatabase.cpp | 14 |
2 files changed, 45 insertions, 0 deletions
diff --git a/media/java/android/mtp/MtpDatabase.java b/media/java/android/mtp/MtpDatabase.java index 798bf6e2f8ee..4f27b197273c 100755 --- a/media/java/android/mtp/MtpDatabase.java +++ b/media/java/android/mtp/MtpDatabase.java @@ -27,10 +27,14 @@ import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; +import android.media.ApplicationMediaCapabilities; import android.media.ExifInterface; +import android.media.MediaFormat; import android.media.ThumbnailUtils; import android.net.Uri; import android.os.BatteryManager; +import android.os.Bundle; +import android.os.RemoteException; import android.os.SystemProperties; import android.os.storage.StorageVolume; import android.provider.MediaStore; @@ -52,6 +56,7 @@ import com.google.android.collect.Sets; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; @@ -754,6 +759,32 @@ public class MtpDatabase implements AutoCloseable { return MtpConstants.RESPONSE_OK; } + @VisibleForNative + private int openFilePath(String path, boolean transcode) { + Uri uri = MediaStore.scanFile(mContext.getContentResolver(), new File(path)); + if (uri == null) { + Log.i(TAG, "Failed to obtain URI for openFile with transcode support: " + path); + return -1; + } + + try { + Log.i(TAG, "openFile with transcode support: " + path); + // TODO(b/158466651): Pass the |transcode| variable as flag to openFile + Bundle bundle = null; + if (!transcode) { + bundle = new Bundle(); + bundle.putParcelable(MediaStore.EXTRA_MEDIA_CAPABILITIES, + new ApplicationMediaCapabilities.Builder().addSupportedVideoMimeType( + MediaFormat.MIMETYPE_VIDEO_HEVC).build()); + } + return mMediaProvider.openTypedAssetFileDescriptor(uri, "*/*", bundle) + .getParcelFileDescriptor().detachFd(); + } catch (RemoteException | FileNotFoundException e) { + Log.w(TAG, "Failed to openFile with transcode support: " + path, e); + return -1; + } + } + private int getObjectFormat(int handle) { MtpStorageManager.MtpObject obj = mManager.getObject(handle); if (obj == null) { diff --git a/media/jni/android_mtp_MtpDatabase.cpp b/media/jni/android_mtp_MtpDatabase.cpp index 17189fd08688..4efdcaccf7a1 100644 --- a/media/jni/android_mtp_MtpDatabase.cpp +++ b/media/jni/android_mtp_MtpDatabase.cpp @@ -63,6 +63,7 @@ static jmethodID method_setDeviceProperty; static jmethodID method_getObjectPropertyList; static jmethodID method_getObjectInfo; static jmethodID method_getObjectFilePath; +static jmethodID method_openFilePath; static jmethodID method_getThumbnailInfo; static jmethodID method_getThumbnailData; static jmethodID method_beginDeleteObject; @@ -160,6 +161,7 @@ public: MtpStringBuffer& outFilePath, int64_t& outFileLength, MtpObjectFormat& outFormat); + virtual int openFilePath(const char* path, bool transcode); virtual MtpResponseCode beginDeleteObject(MtpObjectHandle handle); virtual void endDeleteObject(MtpObjectHandle handle, bool succeeded); @@ -969,6 +971,17 @@ MtpResponseCode MtpDatabase::getObjectFilePath(MtpObjectHandle handle, return result; } +int MtpDatabase::openFilePath(const char* path, bool transcode) { + JNIEnv* env = AndroidRuntime::getJNIEnv(); + jstring pathStr = env->NewStringUTF(path); + jint result = env->CallIntMethod(mDatabase, method_openFilePath, pathStr, transcode); + + if (result < 0) { + checkAndClearExceptionFromCallback(env, __FUNCTION__); + } + return result; +} + MtpResponseCode MtpDatabase::beginDeleteObject(MtpObjectHandle handle) { JNIEnv* env = AndroidRuntime::getJNIEnv(); MtpResponseCode result = env->CallIntMethod(mDatabase, method_beginDeleteObject, (jint)handle); @@ -1333,6 +1346,7 @@ int register_android_mtp_MtpDatabase(JNIEnv *env) GET_METHOD_ID(getObjectPropertyList, clazz, "(IIIII)Landroid/mtp/MtpPropertyList;"); GET_METHOD_ID(getObjectInfo, clazz, "(I[I[C[J)Z"); GET_METHOD_ID(getObjectFilePath, clazz, "(I[C[J)I"); + GET_METHOD_ID(openFilePath, clazz, "(Ljava/lang/String;Z)I"); GET_METHOD_ID(getThumbnailInfo, clazz, "(I[J)Z"); GET_METHOD_ID(getThumbnailData, clazz, "(I)[B"); GET_METHOD_ID(beginDeleteObject, clazz, "(I)I"); |