Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 223e3b5c authored by Zim's avatar Zim Committed by Manish Singh
Browse files

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
parent 558c6059
Loading
Loading
Loading
Loading
+31 −0
Original line number Original line Diff line number Diff line
@@ -27,10 +27,14 @@ import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Bitmap;
import android.media.ApplicationMediaCapabilities;
import android.media.ExifInterface;
import android.media.ExifInterface;
import android.media.MediaFormat;
import android.media.ThumbnailUtils;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.os.SystemProperties;
import android.os.storage.StorageVolume;
import android.os.storage.StorageVolume;
import android.provider.MediaStore;
import android.provider.MediaStore;
@@ -52,6 +56,7 @@ import com.google.android.collect.Sets;


import java.io.ByteArrayOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Paths;
@@ -754,6 +759,32 @@ public class MtpDatabase implements AutoCloseable {
        return MtpConstants.RESPONSE_OK;
        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) {
    private int getObjectFormat(int handle) {
        MtpStorageManager.MtpObject obj = mManager.getObject(handle);
        MtpStorageManager.MtpObject obj = mManager.getObject(handle);
        if (obj == null) {
        if (obj == null) {
+14 −0
Original line number Original line Diff line number Diff line
@@ -63,6 +63,7 @@ static jmethodID method_setDeviceProperty;
static jmethodID method_getObjectPropertyList;
static jmethodID method_getObjectPropertyList;
static jmethodID method_getObjectInfo;
static jmethodID method_getObjectInfo;
static jmethodID method_getObjectFilePath;
static jmethodID method_getObjectFilePath;
static jmethodID method_openFilePath;
static jmethodID method_getThumbnailInfo;
static jmethodID method_getThumbnailInfo;
static jmethodID method_getThumbnailData;
static jmethodID method_getThumbnailData;
static jmethodID method_beginDeleteObject;
static jmethodID method_beginDeleteObject;
@@ -160,6 +161,7 @@ public:
                                            MtpStringBuffer& outFilePath,
                                            MtpStringBuffer& outFilePath,
                                            int64_t& outFileLength,
                                            int64_t& outFileLength,
                                            MtpObjectFormat& outFormat);
                                            MtpObjectFormat& outFormat);
    virtual int                     openFilePath(const char* path, bool transcode);
    virtual MtpResponseCode         beginDeleteObject(MtpObjectHandle handle);
    virtual MtpResponseCode         beginDeleteObject(MtpObjectHandle handle);
    virtual void                    endDeleteObject(MtpObjectHandle handle, bool succeeded);
    virtual void                    endDeleteObject(MtpObjectHandle handle, bool succeeded);


@@ -969,6 +971,17 @@ MtpResponseCode MtpDatabase::getObjectFilePath(MtpObjectHandle handle,
    return result;
    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) {
MtpResponseCode MtpDatabase::beginDeleteObject(MtpObjectHandle handle) {
    JNIEnv* env = AndroidRuntime::getJNIEnv();
    JNIEnv* env = AndroidRuntime::getJNIEnv();
    MtpResponseCode result = env->CallIntMethod(mDatabase, method_beginDeleteObject, (jint)handle);
    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(getObjectPropertyList, clazz, "(IIIII)Landroid/mtp/MtpPropertyList;");
    GET_METHOD_ID(getObjectInfo, clazz, "(I[I[C[J)Z");
    GET_METHOD_ID(getObjectInfo, clazz, "(I[I[C[J)Z");
    GET_METHOD_ID(getObjectFilePath, clazz, "(I[C[J)I");
    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(getThumbnailInfo, clazz, "(I[J)Z");
    GET_METHOD_ID(getThumbnailData, clazz, "(I)[B");
    GET_METHOD_ID(getThumbnailData, clazz, "(I)[B");
    GET_METHOD_ID(beginDeleteObject, clazz, "(I)I");
    GET_METHOD_ID(beginDeleteObject, clazz, "(I)I");