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

Commit 2b51f47a authored by Mike Lockwood's avatar Mike Lockwood Committed by Android (Google) Code Review
Browse files

Merge changes Iccc3a445,I82ce80a4

* changes:
  MTP: Implement GetNumObjects
  Clean up MtpDatabase API.
parents 13ee57e8 7a047c89
Loading
Loading
Loading
Loading
+39 −6
Original line number Diff line number Diff line
@@ -164,6 +164,33 @@ public class MtpDatabase {
        return null;
    }

    private int getNumObjects(int storageID, int format, int parent) {
        // we can ignore storageID until we support multiple storages
        Log.d(TAG, "getObjectList parent: " + parent);
        Cursor c = null;
        try {
            if (format != 0) {
                c = mMediaProvider.query(mObjectsUri, ID_PROJECTION,
                            PARENT_FORMAT_WHERE,
                            new String[] { Integer.toString(parent), Integer.toString(format) },
                             null);
            } else {
                c = mMediaProvider.query(mObjectsUri, ID_PROJECTION,
                            PARENT_WHERE, new String[] { Integer.toString(parent) }, null);
            }
            if (c != null) {
                return c.getCount();
            }
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in getNumObjects", e);
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return -1;
    }

    private int getObjectProperty(int handle, int property,
                            long[] outIntValue, char[] outStringValue) {
        Log.d(TAG, "getObjectProperty: " + property);
@@ -271,7 +298,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 +309,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;
        }
    }

+30 −40
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ using namespace android;
static jmethodID method_beginSendObject;
static jmethodID method_endSendObject;
static jmethodID method_getObjectList;
static jmethodID method_getNumObjects;
static jmethodID method_getObjectProperty;
static jmethodID method_getObjectInfo;
static jmethodID method_getObjectFilePath;
@@ -80,6 +81,10 @@ public:
                                    MtpObjectFormat format,
                                    MtpObjectHandle parent);

    virtual int                     getNumObjects(MtpStorageID storageID,
                                            MtpObjectFormat format,
                                            MtpObjectHandle parent);

    virtual MtpResponseCode         getObjectProperty(MtpObjectHandle handle,
                                            MtpObjectProperty property,
                                            MtpDataPacket& packet);
@@ -87,17 +92,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);
};
@@ -171,15 +169,20 @@ MtpObjectHandleList* MyMtpDatabase::getObjectList(MtpStorageID storageID,
    MtpObjectHandleList* list = new MtpObjectHandleList();
    jint* handles = env->GetIntArrayElements(array, 0);
    jsize length = env->GetArrayLength(array);
LOGD("getObjectList length: %d", length);
    for (int i = 0; i < length; i++) {
LOGD("push: %d", handles[i]);
    for (int i = 0; i < length; i++)
        list->push(handles[i]);
    }
   env->ReleaseIntArrayElements(array, handles, 0);
   return list;
}

int MyMtpDatabase::getNumObjects(MtpStorageID storageID,
                                MtpObjectFormat format,
                                MtpObjectHandle parent) {
    JNIEnv* env = AndroidRuntime::getJNIEnv();
    return env->CallIntMethod(mDatabase, method_getNumObjects,
                (jint)storageID, (jint)format, (jint)parent);
}

MtpResponseCode MyMtpDatabase::getObjectProperty(MtpObjectHandle handle,
                                            MtpObjectProperty property,
                                            MtpDataPacket& packet) {
@@ -290,14 +293,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 +310,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 {
@@ -432,6 +417,11 @@ int register_android_media_MtpDatabase(JNIEnv *env)
        LOGE("Can't find getObjectList");
        return -1;
    }
    method_getNumObjects = env->GetMethodID(clazz, "getNumObjects", "(III)I");
    if (method_getNumObjects == NULL) {
        LOGE("Can't find getNumObjects");
        return -1;
    }
    method_getObjectProperty = env->GetMethodID(clazz, "getObjectProperty", "(II[J[C)I");
    if (method_getObjectProperty == NULL) {
        LOGE("Can't find getObjectProperty");
@@ -442,12 +432,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;
+6 −6
Original line number Diff line number Diff line
@@ -47,6 +47,10 @@ public:
                                            MtpObjectFormat format,
                                            MtpObjectHandle parent) = 0;

    virtual int                     getNumObjects(MtpStorageID storageID,
                                            MtpObjectFormat format,
                                            MtpObjectHandle parent) = 0;

    virtual MtpResponseCode         getObjectProperty(MtpObjectHandle handle,
                                            MtpObjectProperty property,
                                            MtpDataPacket& packet) = 0;
@@ -54,14 +58,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
+45 −24
Original line number Diff line number Diff line
@@ -55,10 +55,10 @@ static const MtpOperationCode kSupportedOperationCodes[] = {
//    MTP_OPERATION_SELF_TEST,
//    MTP_OPERATION_SET_OBJECT_PROTECTION,
//    MTP_OPERATION_POWER_DOWN,
    MTP_OPERATION_GET_DEVICE_PROP_DESC,
    MTP_OPERATION_GET_DEVICE_PROP_VALUE,
    MTP_OPERATION_SET_DEVICE_PROP_VALUE,
    MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
//    MTP_OPERATION_GET_DEVICE_PROP_DESC,
//    MTP_OPERATION_GET_DEVICE_PROP_VALUE,
//    MTP_OPERATION_SET_DEVICE_PROP_VALUE,
//    MTP_OPERATION_RESET_DEVICE_PROP_VALUE,
//    MTP_OPERATION_TERMINATE_OPEN_CAPTURE,
//    MTP_OPERATION_MOVE_OBJECT,
//    MTP_OPERATION_COPY_OBJECT,
@@ -67,7 +67,7 @@ static const MtpOperationCode kSupportedOperationCodes[] = {
    MTP_OPERATION_GET_OBJECT_PROPS_SUPPORTED,
//    MTP_OPERATION_GET_OBJECT_PROP_DESC,
    MTP_OPERATION_GET_OBJECT_PROP_VALUE,
    MTP_OPERATION_SET_OBJECT_PROP_VALUE,
//    MTP_OPERATION_SET_OBJECT_PROP_VALUE,
//    MTP_OPERATION_GET_OBJECT_REFERENCES,
//    MTP_OPERATION_SET_OBJECT_REFERENCES,
//    MTP_OPERATION_SKIP,
@@ -308,6 +308,9 @@ bool MtpServer::handleRequest() {
        case MTP_OPERATION_GET_OBJECT_HANDLES:
            response = doGetObjectHandles();
            break;
        case MTP_OPERATION_GET_NUM_OBJECTS:
            response = doGetNumObjects();
            break;
        case MTP_OPERATION_GET_OBJECT_PROP_VALUE:
            response = doGetObjectPropValue();
            break;
@@ -454,6 +457,26 @@ MtpResponseCode MtpServer::doGetObjectHandles() {
    return MTP_RESPONSE_OK;
}

MtpResponseCode MtpServer::doGetNumObjects() {
    if (!mSessionOpen)
        return MTP_RESPONSE_SESSION_NOT_OPEN;
    MtpStorageID storageID = mRequest.getParameter(1);      // 0xFFFFFFFF for all storage
    MtpObjectFormat format = mRequest.getParameter(2);      // 0 for all formats
    MtpObjectHandle parent = mRequest.getParameter(3);      // 0xFFFFFFFF for objects with no parent
                                                            // 0x00000000 for all objects?
    if (parent == 0xFFFFFFFF)
        parent = 0;

    int count = mDatabase->getNumObjects(storageID, format, parent);
    if (count >= 0) {
        mResponse.setParameter(1, count);
        return MTP_RESPONSE_OK;
    } else {
        mResponse.setParameter(1, 0);
        return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
    }
}

MtpResponseCode MtpServer::doGetObjectPropValue() {
    MtpObjectHandle handle = mRequest.getParameter(1);
    MtpObjectProperty property = mRequest.getParameter(2);
@@ -470,10 +493,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 +537,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 +572,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 +663,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() {
+1 −0
Original line number Diff line number Diff line
@@ -94,6 +94,7 @@ private:
    MtpResponseCode     doGetStorageInfo();
    MtpResponseCode     doGetObjectPropsSupported();
    MtpResponseCode     doGetObjectHandles();
    MtpResponseCode     doGetNumObjects();
    MtpResponseCode     doGetObjectPropValue();
    MtpResponseCode     doGetObjectInfo();
    MtpResponseCode     doGetObject();