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

Commit 59c777a2 authored by Mike Lockwood's avatar Mike Lockwood
Browse files

Clean up MtpDatabase API.



Return MTP response codes instead of booleans for success or failure.
Remove some unused code.

Change-Id: I82ce80a4d7779233264e3caf139ebd0cece12f5c
Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 37b4a3c2
Loading
Loading
Loading
Loading
+12 −6
Original line number Diff line number Diff line
@@ -271,7 +271,7 @@ public class MtpDatabase {
        return false;
    }

    private boolean getObjectFilePath(int handle, char[] outFilePath, long[] outFileLength) {
    private int getObjectFilePath(int handle, char[] outFilePath, long[] outFileLength) {
        Log.d(TAG, "getObjectFilePath: " + handle);
        Cursor c = null;
        try {
@@ -282,26 +282,32 @@ public class MtpDatabase {
                path.getChars(0, path.length(), outFilePath, 0);
                outFilePath[path.length()] = 0;
                outFileLength[0] = c.getLong(2);
                return true;
                return MTP_RESPONSE_OK;
            } else {
                return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
            }
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in getObjectFilePath", e);
            return MTP_RESPONSE_GENERAL_ERROR;
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return false;
    }

    private boolean deleteFile(int handle) {
    private int deleteFile(int handle) {
        Log.d(TAG, "deleteFile: " + handle);
        Uri uri = MtpObjects.getContentUri(mVolumeName, handle);
        try {
            return (mMediaProvider.delete(uri, null, null) == 1);
            if (mMediaProvider.delete(uri, null, null) == 1) {
                return MTP_RESPONSE_OK;
            } else {
                return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
            }
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in deleteFile", e);
            return false;
            return MTP_RESPONSE_GENERAL_ERROR;
        }
    }

+11 −36
Original line number Diff line number Diff line
@@ -87,17 +87,10 @@ public:
    virtual MtpResponseCode         getObjectInfo(MtpObjectHandle handle,
                                            MtpDataPacket& packet);

    virtual bool                    getObjectFilePath(MtpObjectHandle handle,
    virtual MtpResponseCode         getObjectFilePath(MtpObjectHandle handle,
                                            MtpString& filePath,
                                            int64_t& fileLength);
    virtual bool                    deleteFile(MtpObjectHandle handle);

    // helper for media scanner
    virtual MtpObjectHandle*        getFileList(int& outCount);

    virtual void                    beginTransaction();
    virtual void                    commitTransaction();
    virtual void                    rollbackTransaction();
    virtual MtpResponseCode         deleteFile(MtpObjectHandle handle);

    bool                            getPropertyInfo(MtpObjectProperty property, int& type);
};
@@ -290,14 +283,14 @@ MtpResponseCode MyMtpDatabase::getObjectInfo(MtpObjectHandle handle,
    return MTP_RESPONSE_OK;
}

bool MyMtpDatabase::getObjectFilePath(MtpObjectHandle handle,
MtpResponseCode MyMtpDatabase::getObjectFilePath(MtpObjectHandle handle,
                                            MtpString& filePath,
                                            int64_t& fileLength) {
    JNIEnv* env = AndroidRuntime::getJNIEnv();
    jboolean result = env->CallBooleanMethod(mDatabase, method_getObjectFilePath,
    jint result = env->CallIntMethod(mDatabase, method_getObjectFilePath,
                (jint)handle, mStringBuffer, mLongBuffer);
    if (!result)
        return false;
    if (result != MTP_RESPONSE_OK)
        return result;

    jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
    filePath.setTo(str, strlen16(str));
@@ -307,30 +300,12 @@ bool MyMtpDatabase::getObjectFilePath(MtpObjectHandle handle,
    fileLength = longValues[0];
    env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
    
    return true;
    return result;
}

bool MyMtpDatabase::deleteFile(MtpObjectHandle handle) {
MtpResponseCode MyMtpDatabase::deleteFile(MtpObjectHandle handle) {
    JNIEnv* env = AndroidRuntime::getJNIEnv();
    return env->CallBooleanMethod(mDatabase, method_deleteFile, (jint)handle); 
}

    // helper for media scanner
MtpObjectHandle* MyMtpDatabase::getFileList(int& outCount) {
    // REMOVE ME
    return NULL;
}

void MyMtpDatabase::beginTransaction() {
    // REMOVE ME
}

void MyMtpDatabase::commitTransaction() {
    // REMOVE ME
}

void MyMtpDatabase::rollbackTransaction() {
    // REMOVE ME
    return env->CallIntMethod(mDatabase, method_deleteFile, (jint)handle);
}

struct PropertyTableEntry {
@@ -442,12 +417,12 @@ int register_android_media_MtpDatabase(JNIEnv *env)
        LOGE("Can't find getObjectInfo");
        return -1;
    }
    method_getObjectFilePath = env->GetMethodID(clazz, "getObjectFilePath", "(I[C[J)Z");
    method_getObjectFilePath = env->GetMethodID(clazz, "getObjectFilePath", "(I[C[J)I");
    if (method_getObjectFilePath == NULL) {
        LOGE("Can't find getObjectFilePath");
        return -1;
    }
    method_deleteFile = env->GetMethodID(clazz, "deleteFile", "(I)Z");
    method_deleteFile = env->GetMethodID(clazz, "deleteFile", "(I)I");
    if (method_deleteFile == NULL) {
        LOGE("Can't find deleteFile");
        return -1;
+2 −6
Original line number Diff line number Diff line
@@ -54,14 +54,10 @@ public:
    virtual MtpResponseCode         getObjectInfo(MtpObjectHandle handle,
                                            MtpDataPacket& packet) = 0;

    virtual bool                    getObjectFilePath(MtpObjectHandle handle,
    virtual MtpResponseCode         getObjectFilePath(MtpObjectHandle handle,
                                            MtpString& filePath,
                                            int64_t& fileLength) = 0;
    virtual bool                    deleteFile(MtpObjectHandle handle) = 0;

    virtual void                    beginTransaction() = 0;
    virtual void                    commitTransaction() = 0;
    virtual void                    rollbackTransaction() = 0;
    virtual MtpResponseCode         deleteFile(MtpObjectHandle handle) = 0;
};

}; // namespace android
+17 −19
Original line number Diff line number Diff line
@@ -470,10 +470,11 @@ MtpResponseCode MtpServer::doGetObject() {
    MtpObjectHandle handle = mRequest.getParameter(1);
    MtpString pathBuf;
    int64_t fileLength;
    if (!mDatabase->getObjectFilePath(handle, pathBuf, fileLength))
        return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
    const char* filePath = (const char *)pathBuf;
    int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength);
    if (result != MTP_RESPONSE_OK)
        return result;

    const char* filePath = (const char *)pathBuf;
    mtp_file_range  mfr;
    mfr.fd = open(filePath, O_RDONLY);
    if (mfr.fd < 0) {
@@ -513,8 +514,9 @@ MtpResponseCode MtpServer::doSendObjectInfo() {
        parent = 0;
    } else {
        int64_t dummy;
        if (!mDatabase->getObjectFilePath(parent, path, dummy))
            return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
        int result = mDatabase->getObjectFilePath(parent, path, dummy);
        if (result != MTP_RESPONSE_OK)
            return result;
    }

    // read only the fields we need
@@ -547,14 +549,11 @@ MtpResponseCode MtpServer::doSendObjectInfo() {
        path += "/";
    path += (const char *)name;

    mDatabase->beginTransaction();
    MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
            format, parent, storageID, mSendObjectFileSize, modifiedTime);
    if (handle == kInvalidObjectHandle) {
        mDatabase->rollbackTransaction();
        return MTP_RESPONSE_GENERAL_ERROR;
    }
    mDatabase->commitTransaction();

  if (format == MTP_FORMAT_ASSOCIATION) {
        mode_t mask = umask(0);
@@ -641,17 +640,16 @@ MtpResponseCode MtpServer::doDeleteObject() {

    MtpString filePath;
    int64_t fileLength;
    if (!mDatabase->getObjectFilePath(handle, filePath, fileLength))
        return MTP_RESPONSE_INVALID_OBJECT_HANDLE;

    int result = mDatabase->getObjectFilePath(handle, filePath, fileLength);
    if (result == MTP_RESPONSE_OK) {
        LOGV("deleting %s", (const char *)filePath);
        // one of these should work
        rmdir((const char *)filePath);
        unlink((const char *)filePath);

    mDatabase->deleteFile(handle);

    return MTP_RESPONSE_OK;
        return mDatabase->deleteFile(handle);
    } else {
        return result;
    }
}

MtpResponseCode MtpServer::doGetObjectPropDesc() {