From e0a89f611d952d477acaae0316f2ccba7237466b Mon Sep 17 00:00:00 2001 From: Mike Lockwood Date: Fri, 11 Jun 2010 16:34:52 -0400 Subject: [PATCH] MTP: Add host support for deleting objects. For example, deleting pictures on a digital camera. Signed-off-by: Mike Lockwood --- core/java/android/provider/Mtp.java | 4 +++ core/jni/android_media_MtpClient.cpp | 40 ++++++++++++++++++++++++- media/java/android/media/MtpClient.java | 15 ++++++++++ media/java/android/media/MtpCursor.java | 10 +++++++ media/mtp/MtpDevice.cpp | 28 +++++++++++++++++ media/mtp/MtpDevice.h | 3 ++ 6 files changed, 99 insertions(+), 1 deletion(-) diff --git a/core/java/android/provider/Mtp.java b/core/java/android/provider/Mtp.java index 3906d230e8cf4..b85226a4a8a31 100644 --- a/core/java/android/provider/Mtp.java +++ b/core/java/android/provider/Mtp.java @@ -63,6 +63,10 @@ public final class Mtp return Uri.parse(CONTENT_AUTHORITY_DEVICE_SLASH + deviceID + "/storage"); } + public static Uri getContentUri(int deviceID, int storageID) { + return Uri.parse(CONTENT_AUTHORITY_DEVICE_SLASH + deviceID + "/storage/" + storageID); + } + /** * Storage unit identifier *

Type: TEXT

diff --git a/core/jni/android_media_MtpClient.cpp b/core/jni/android_media_MtpClient.cpp index 0c04255936bb1..56eed2ca89b47 100644 --- a/core/jni/android_media_MtpClient.cpp +++ b/core/jni/android_media_MtpClient.cpp @@ -182,13 +182,51 @@ android_media_MtpClient_wait_for_event(JNIEnv *env, jobject thiz) client->waitForEvent(env); } +static jboolean +android_media_MtpClient_delete_object(JNIEnv *env, jobject thiz, + jint device_id, jint object_id) +{ + MyClient *client = (MyClient *)env->GetIntField(thiz, field_context); + MtpDevice* device = client->getDevice(device_id); + if (device) + return device->deleteObject(object_id); + else + return NULL; +} + +static jint +android_media_MtpClient_get_parent(JNIEnv *env, jobject thiz, + jint device_id, jint object_id) +{ + MyClient *client = (MyClient *)env->GetIntField(thiz, field_context); + MtpDevice* device = client->getDevice(device_id); + if (device) + return device->getParent(object_id); + else + return -1; +} + +static jint +android_media_MtpClient_get_storage_id(JNIEnv *env, jobject thiz, + jint device_id, jint object_id) +{ + MyClient *client = (MyClient *)env->GetIntField(thiz, field_context); + MtpDevice* device = client->getDevice(device_id); + if (device) + return device->getStorageID(object_id); + else + return -1; +} + // ---------------------------------------------------------------------------- static JNINativeMethod gMethods[] = { {"native_setup", "()V", (void *)android_media_MtpClient_setup}, {"native_finalize", "()V", (void *)android_media_MtpClient_finalize}, {"native_wait_for_event", "()V", (void *)android_media_MtpClient_wait_for_event}, - + {"native_delete_object", "(II)Z", (void *)android_media_MtpClient_delete_object}, + {"native_get_parent", "(II)I", (void *)android_media_MtpClient_get_parent}, + {"native_get_storage_id", "(II)I", (void *)android_media_MtpClient_get_storage_id}, }; static const char* const kClassPathName = "android/media/MtpClient"; diff --git a/media/java/android/media/MtpClient.java b/media/java/android/media/MtpClient.java index 946a400cfa8e2..a6d3ec94cc099 100644 --- a/media/java/android/media/MtpClient.java +++ b/media/java/android/media/MtpClient.java @@ -43,6 +43,18 @@ public class MtpClient { mEventThread.start(); } + public boolean deleteObject(int deviceID, int objectID) { + return native_delete_object(deviceID, objectID); + } + + public int getParent(int deviceID, int objectID) { + return native_get_parent(deviceID, objectID); + } + + public int getStorageID(int deviceID, int objectID) { + return native_get_storage_id(deviceID, objectID); + } + private class MtpEventThread extends Thread { private boolean mDone; @@ -77,4 +89,7 @@ public class MtpClient { private native final void native_setup(); private native final void native_finalize(); private native void native_wait_for_event(); + private native boolean native_delete_object(int deviceID, int objectID); + private native int native_get_parent(int deviceID, int objectID); + private native int native_get_storage_id(int deviceID, int objectID); } \ No newline at end of file diff --git a/media/java/android/media/MtpCursor.java b/media/java/android/media/MtpCursor.java index cf3bc09db25f8..6ecfd0df4dcfd 100644 --- a/media/java/android/media/MtpCursor.java +++ b/media/java/android/media/MtpCursor.java @@ -108,6 +108,16 @@ public final class MtpCursor extends AbstractWindowedCursor { return mCount; } + @Override + public boolean requery() { + Log.d(TAG, "requery"); + mCount = NO_COUNT; + if (mWindow != null) { + mWindow.clear(); + } + return super.requery(); + } + private void fillWindow(int startPos) { if (mWindow == null) { // If there isn't a window set already it will only be accessed locally diff --git a/media/mtp/MtpDevice.cpp b/media/mtp/MtpDevice.cpp index ee352174e4142..3aa8a35309530 100644 --- a/media/mtp/MtpDevice.cpp +++ b/media/mtp/MtpDevice.cpp @@ -179,6 +179,8 @@ MtpObjectHandleList* MtpDevice::getObjectHandles(MtpStorageID storageID, } MtpObjectInfo* MtpDevice::getObjectInfo(MtpObjectHandle handle) { + // FIXME - we might want to add some caching here + mRequest.reset(); mRequest.setParameter(1, handle); if (!sendRequest(MTP_OPERATION_GET_OBJECT_INFO)) @@ -205,7 +207,33 @@ void* MtpDevice::getThumbnail(MtpObjectHandle handle, int& outLength) { } outLength = 0; return NULL; +} +bool MtpDevice::deleteObject(MtpObjectHandle handle) { + mRequest.reset(); + mRequest.setParameter(1, handle); + if (sendRequest(MTP_OPERATION_DELETE_OBJECT)) { + MtpResponseCode ret = readResponse(); + if (ret == MTP_RESPONSE_OK) + return true; + } + return false; +} + +MtpObjectHandle MtpDevice::getParent(MtpObjectHandle handle) { + MtpObjectInfo* info = getObjectInfo(handle); + if (info) + return info->mParent; + else + return -1; +} + +MtpObjectHandle MtpDevice::getStorageID(MtpObjectHandle handle) { + MtpObjectInfo* info = getObjectInfo(handle); + if (info) + return info->mStorageID; + else + return -1; } MtpProperty* MtpDevice::getDevicePropDesc(MtpDeviceProperty code) { diff --git a/media/mtp/MtpDevice.h b/media/mtp/MtpDevice.h index 0ee930f7dd5b4..9b0519791db32 100644 --- a/media/mtp/MtpDevice.h +++ b/media/mtp/MtpDevice.h @@ -71,6 +71,9 @@ public: MtpObjectHandleList* getObjectHandles(MtpStorageID storageID, MtpObjectFormat format, MtpObjectHandle parent); MtpObjectInfo* getObjectInfo(MtpObjectHandle handle); void* getThumbnail(MtpObjectHandle handle, int& outLength); + bool deleteObject(MtpObjectHandle handle); + MtpObjectHandle getParent(MtpObjectHandle handle); + MtpObjectHandle getStorageID(MtpObjectHandle handle); MtpProperty* getDevicePropDesc(MtpDeviceProperty code);