Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "MtpDatabaseJNI" |
| 18 | #include "utils/Log.h" |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <assert.h> |
| 22 | #include <limits.h> |
| 23 | #include <unistd.h> |
| 24 | #include <fcntl.h> |
| 25 | |
| 26 | #include "jni.h" |
| 27 | #include "JNIHelp.h" |
| 28 | #include "android_runtime/AndroidRuntime.h" |
| 29 | |
| 30 | #include "MtpDatabase.h" |
| 31 | #include "MtpDataPacket.h" |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 32 | #include "MtpProperty.h" |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 33 | #include "MtpUtils.h" |
| 34 | #include "mtp.h" |
| 35 | |
| 36 | using namespace android; |
| 37 | |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 40 | static jmethodID method_beginSendObject; |
| 41 | static jmethodID method_endSendObject; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 42 | static jmethodID method_getObjectList; |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 43 | static jmethodID method_getNumObjects; |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 44 | static jmethodID method_getSupportedPlaybackFormats; |
| 45 | static jmethodID method_getSupportedCaptureFormats; |
| 46 | static jmethodID method_getSupportedObjectProperties; |
| 47 | static jmethodID method_getSupportedDeviceProperties; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 48 | static jmethodID method_getObjectProperty; |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 49 | static jmethodID method_setObjectProperty; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 50 | static jmethodID method_getObjectInfo; |
| 51 | static jmethodID method_getObjectFilePath; |
| 52 | static jmethodID method_deleteFile; |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 53 | static jmethodID method_getObjectReferences; |
| 54 | static jmethodID method_setObjectReferences; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 55 | static jfieldID field_context; |
| 56 | |
| 57 | MtpDatabase* getMtpDatabase(JNIEnv *env, jobject database) { |
| 58 | return (MtpDatabase *)env->GetIntField(database, field_context); |
| 59 | } |
| 60 | |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 61 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 62 | // ---------------------------------------------------------------------------- |
| 63 | |
| 64 | class MyMtpDatabase : public MtpDatabase { |
| 65 | private: |
| 66 | jobject mDatabase; |
| 67 | jintArray mIntBuffer; |
| 68 | jlongArray mLongBuffer; |
| 69 | jcharArray mStringBuffer; |
| 70 | |
| 71 | public: |
| 72 | MyMtpDatabase(JNIEnv *env, jobject client); |
| 73 | virtual ~MyMtpDatabase(); |
| 74 | void cleanup(JNIEnv *env); |
| 75 | |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 76 | virtual MtpObjectHandle beginSendObject(const char* path, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 77 | MtpObjectFormat format, |
| 78 | MtpObjectHandle parent, |
| 79 | MtpStorageID storage, |
| 80 | uint64_t size, |
| 81 | time_t modified); |
| 82 | |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 83 | virtual void endSendObject(const char* path, |
| 84 | MtpObjectHandle handle, |
| 85 | MtpObjectFormat format, |
| 86 | bool succeeded); |
| 87 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 88 | virtual MtpObjectHandleList* getObjectList(MtpStorageID storageID, |
| 89 | MtpObjectFormat format, |
| 90 | MtpObjectHandle parent); |
| 91 | |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 92 | virtual int getNumObjects(MtpStorageID storageID, |
| 93 | MtpObjectFormat format, |
| 94 | MtpObjectHandle parent); |
| 95 | |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 96 | // callee should delete[] the results from these |
| 97 | // results can be NULL |
| 98 | virtual MtpObjectFormatList* getSupportedPlaybackFormats(); |
| 99 | virtual MtpObjectFormatList* getSupportedCaptureFormats(); |
| 100 | virtual MtpObjectPropertyList* getSupportedObjectProperties(MtpObjectFormat format); |
| 101 | virtual MtpDevicePropertyList* getSupportedDeviceProperties(); |
| 102 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 103 | virtual MtpResponseCode getObjectPropertyValue(MtpObjectHandle handle, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 104 | MtpObjectProperty property, |
| 105 | MtpDataPacket& packet); |
| 106 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 107 | virtual MtpResponseCode setObjectPropertyValue(MtpObjectHandle handle, |
| 108 | MtpObjectProperty property, |
| 109 | MtpDataPacket& packet); |
| 110 | |
| 111 | virtual MtpResponseCode getDevicePropertyValue(MtpDeviceProperty property, |
| 112 | MtpDataPacket& packet); |
| 113 | |
| 114 | virtual MtpResponseCode setDevicePropertyValue(MtpDeviceProperty property, |
| 115 | MtpDataPacket& packet); |
| 116 | |
| 117 | virtual MtpResponseCode resetDeviceProperty(MtpDeviceProperty property); |
| 118 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 119 | virtual MtpResponseCode getObjectInfo(MtpObjectHandle handle, |
| 120 | MtpDataPacket& packet); |
| 121 | |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 122 | virtual MtpResponseCode getObjectFilePath(MtpObjectHandle handle, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 123 | MtpString& filePath, |
| 124 | int64_t& fileLength); |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 125 | virtual MtpResponseCode deleteFile(MtpObjectHandle handle); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 126 | |
| 127 | bool getPropertyInfo(MtpObjectProperty property, int& type); |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 128 | |
| 129 | virtual MtpObjectHandleList* getObjectReferences(MtpObjectHandle handle); |
| 130 | |
| 131 | virtual MtpResponseCode setObjectReferences(MtpObjectHandle handle, |
| 132 | MtpObjectHandleList* references); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 133 | |
| 134 | virtual MtpProperty* getObjectPropertyDesc(MtpObjectProperty property, |
| 135 | MtpObjectFormat format); |
| 136 | |
| 137 | virtual MtpProperty* getDevicePropertyDesc(MtpDeviceProperty property); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 138 | }; |
| 139 | |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 140 | // ---------------------------------------------------------------------------- |
| 141 | |
| 142 | static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 143 | if (env->ExceptionCheck()) { |
| 144 | LOGE("An exception was thrown by callback '%s'.", methodName); |
| 145 | LOGE_EX(env); |
| 146 | env->ExceptionClear(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // ---------------------------------------------------------------------------- |
| 151 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 152 | MyMtpDatabase::MyMtpDatabase(JNIEnv *env, jobject client) |
| 153 | : mDatabase(env->NewGlobalRef(client)), |
| 154 | mIntBuffer(NULL), |
| 155 | mLongBuffer(NULL), |
| 156 | mStringBuffer(NULL) |
| 157 | { |
| 158 | jintArray intArray; |
| 159 | jlongArray longArray; |
| 160 | jcharArray charArray; |
| 161 | |
| 162 | // create buffers for out arguments |
| 163 | // we don't need to be thread-safe so this is OK |
| 164 | intArray = env->NewIntArray(3); |
| 165 | if (!intArray) |
| 166 | goto out_of_memory; |
| 167 | mIntBuffer = (jintArray)env->NewGlobalRef(intArray); |
| 168 | longArray = env->NewLongArray(2); |
| 169 | if (!longArray) |
| 170 | goto out_of_memory; |
| 171 | mLongBuffer = (jlongArray)env->NewGlobalRef(longArray); |
| 172 | charArray = env->NewCharArray(256); |
| 173 | if (!charArray) |
| 174 | goto out_of_memory; |
| 175 | mStringBuffer = (jcharArray)env->NewGlobalRef(charArray); |
| 176 | return; |
| 177 | |
| 178 | out_of_memory: |
| 179 | env->ThrowNew(env->FindClass("java/lang/OutOfMemoryError"), NULL); |
| 180 | } |
| 181 | |
| 182 | void MyMtpDatabase::cleanup(JNIEnv *env) { |
| 183 | env->DeleteGlobalRef(mDatabase); |
| 184 | env->DeleteGlobalRef(mIntBuffer); |
| 185 | env->DeleteGlobalRef(mLongBuffer); |
| 186 | env->DeleteGlobalRef(mStringBuffer); |
| 187 | } |
| 188 | |
| 189 | MyMtpDatabase::~MyMtpDatabase() { |
| 190 | } |
| 191 | |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 192 | MtpObjectHandle MyMtpDatabase::beginSendObject(const char* path, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 193 | MtpObjectFormat format, |
| 194 | MtpObjectHandle parent, |
| 195 | MtpStorageID storage, |
| 196 | uint64_t size, |
| 197 | time_t modified) { |
| 198 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 199 | MtpObjectHandle result = env->CallIntMethod(mDatabase, method_beginSendObject, |
| 200 | env->NewStringUTF(path), (jint)format, (jint)parent, (jint)storage, |
| 201 | (jlong)size, (jlong)modified); |
| 202 | |
| 203 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 204 | return result; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 207 | void MyMtpDatabase::endSendObject(const char* path, MtpObjectHandle handle, |
| 208 | MtpObjectFormat format, bool succeeded) { |
| 209 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 210 | env->CallVoidMethod(mDatabase, method_endSendObject, env->NewStringUTF(path), |
| 211 | (jint)handle, (jint)format, (jboolean)succeeded); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 212 | |
| 213 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 216 | MtpObjectHandleList* MyMtpDatabase::getObjectList(MtpStorageID storageID, |
| 217 | MtpObjectFormat format, |
| 218 | MtpObjectHandle parent) { |
| 219 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 220 | jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectList, |
| 221 | (jint)storageID, (jint)format, (jint)parent); |
| 222 | if (!array) |
| 223 | return NULL; |
| 224 | MtpObjectHandleList* list = new MtpObjectHandleList(); |
| 225 | jint* handles = env->GetIntArrayElements(array, 0); |
| 226 | jsize length = env->GetArrayLength(array); |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 227 | for (int i = 0; i < length; i++) |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 228 | list->push(handles[i]); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 229 | env->ReleaseIntArrayElements(array, handles, 0); |
| 230 | |
| 231 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 232 | return list; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 235 | int MyMtpDatabase::getNumObjects(MtpStorageID storageID, |
| 236 | MtpObjectFormat format, |
| 237 | MtpObjectHandle parent) { |
| 238 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 239 | int result = env->CallIntMethod(mDatabase, method_getNumObjects, |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 240 | (jint)storageID, (jint)format, (jint)parent); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 241 | |
| 242 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 243 | return result; |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 244 | } |
| 245 | |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 246 | MtpObjectFormatList* MyMtpDatabase::getSupportedPlaybackFormats() { |
| 247 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 248 | jintArray array = (jintArray)env->CallObjectMethod(mDatabase, |
| 249 | method_getSupportedPlaybackFormats); |
| 250 | if (!array) |
| 251 | return NULL; |
| 252 | MtpObjectFormatList* list = new MtpObjectFormatList(); |
| 253 | jint* formats = env->GetIntArrayElements(array, 0); |
| 254 | jsize length = env->GetArrayLength(array); |
| 255 | for (int i = 0; i < length; i++) |
| 256 | list->push(formats[i]); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 257 | env->ReleaseIntArrayElements(array, formats, 0); |
| 258 | |
| 259 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 260 | return list; |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | MtpObjectFormatList* MyMtpDatabase::getSupportedCaptureFormats() { |
| 264 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 265 | jintArray array = (jintArray)env->CallObjectMethod(mDatabase, |
| 266 | method_getSupportedCaptureFormats); |
| 267 | if (!array) |
| 268 | return NULL; |
| 269 | MtpObjectFormatList* list = new MtpObjectFormatList(); |
| 270 | jint* formats = env->GetIntArrayElements(array, 0); |
| 271 | jsize length = env->GetArrayLength(array); |
| 272 | for (int i = 0; i < length; i++) |
| 273 | list->push(formats[i]); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 274 | env->ReleaseIntArrayElements(array, formats, 0); |
| 275 | |
| 276 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 277 | return list; |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | MtpObjectPropertyList* MyMtpDatabase::getSupportedObjectProperties(MtpObjectFormat format) { |
| 281 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 282 | jintArray array = (jintArray)env->CallObjectMethod(mDatabase, |
| 283 | method_getSupportedObjectProperties, (jint)format); |
| 284 | if (!array) |
| 285 | return NULL; |
| 286 | MtpObjectPropertyList* list = new MtpObjectPropertyList(); |
| 287 | jint* properties = env->GetIntArrayElements(array, 0); |
| 288 | jsize length = env->GetArrayLength(array); |
| 289 | for (int i = 0; i < length; i++) |
| 290 | list->push(properties[i]); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 291 | env->ReleaseIntArrayElements(array, properties, 0); |
| 292 | |
| 293 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 294 | return list; |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | MtpDevicePropertyList* MyMtpDatabase::getSupportedDeviceProperties() { |
| 298 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 299 | jintArray array = (jintArray)env->CallObjectMethod(mDatabase, |
| 300 | method_getSupportedDeviceProperties); |
| 301 | if (!array) |
| 302 | return NULL; |
| 303 | MtpDevicePropertyList* list = new MtpDevicePropertyList(); |
| 304 | jint* properties = env->GetIntArrayElements(array, 0); |
| 305 | jsize length = env->GetArrayLength(array); |
| 306 | for (int i = 0; i < length; i++) |
| 307 | list->push(properties[i]); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 308 | env->ReleaseIntArrayElements(array, properties, 0); |
| 309 | |
| 310 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 311 | return list; |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 314 | MtpResponseCode MyMtpDatabase::getObjectPropertyValue(MtpObjectHandle handle, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 315 | MtpObjectProperty property, |
| 316 | MtpDataPacket& packet) { |
| 317 | int type; |
| 318 | |
| 319 | if (!getPropertyInfo(property, type)) |
| 320 | return MTP_RESPONSE_INVALID_OBJECT_PROP_CODE; |
| 321 | |
| 322 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 323 | jint result = env->CallIntMethod(mDatabase, method_getObjectProperty, |
| 324 | (jint)handle, (jint)property, mLongBuffer, mStringBuffer); |
| 325 | if (result != MTP_RESPONSE_OK) |
| 326 | return result; |
| 327 | |
| 328 | jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0); |
| 329 | jlong longValue = longValues[0]; |
| 330 | env->ReleaseLongArrayElements(mLongBuffer, longValues, 0); |
| 331 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 332 | // special case MTP_PROPERTY_DATE_MODIFIED, which is a string to MTP |
| 333 | // but stored internally as a uint64 |
| 334 | if (property == MTP_PROPERTY_DATE_MODIFIED) { |
| 335 | char date[20]; |
| 336 | formatDateTime(longValue, date, sizeof(date)); |
| 337 | packet.putString(date); |
| 338 | return MTP_RESPONSE_OK; |
| 339 | } |
| 340 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 341 | switch (type) { |
| 342 | case MTP_TYPE_INT8: |
| 343 | packet.putInt8(longValue); |
| 344 | break; |
| 345 | case MTP_TYPE_UINT8: |
| 346 | packet.putUInt8(longValue); |
| 347 | break; |
| 348 | case MTP_TYPE_INT16: |
| 349 | packet.putInt16(longValue); |
| 350 | break; |
| 351 | case MTP_TYPE_UINT16: |
| 352 | packet.putUInt16(longValue); |
| 353 | break; |
| 354 | case MTP_TYPE_INT32: |
| 355 | packet.putInt32(longValue); |
| 356 | break; |
| 357 | case MTP_TYPE_UINT32: |
| 358 | packet.putUInt32(longValue); |
| 359 | break; |
| 360 | case MTP_TYPE_INT64: |
| 361 | packet.putInt64(longValue); |
| 362 | break; |
| 363 | case MTP_TYPE_UINT64: |
| 364 | packet.putUInt64(longValue); |
| 365 | break; |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 366 | case MTP_TYPE_INT128: |
| 367 | packet.putInt128(longValue); |
| 368 | break; |
| 369 | case MTP_TYPE_UINT128: |
| 370 | packet.putInt128(longValue); |
| 371 | break; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 372 | case MTP_TYPE_STR: |
| 373 | { |
| 374 | jchar* str = env->GetCharArrayElements(mStringBuffer, 0); |
| 375 | packet.putString(str); |
| 376 | env->ReleaseCharArrayElements(mStringBuffer, str, 0); |
| 377 | break; |
| 378 | } |
| 379 | default: |
| 380 | LOGE("unsupported object type\n"); |
| 381 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
| 382 | } |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 383 | |
| 384 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 385 | return MTP_RESPONSE_OK; |
| 386 | } |
| 387 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 388 | MtpResponseCode MyMtpDatabase::setObjectPropertyValue(MtpObjectHandle handle, |
| 389 | MtpObjectProperty property, |
| 390 | MtpDataPacket& packet) { |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | MtpResponseCode MyMtpDatabase::getDevicePropertyValue(MtpDeviceProperty property, |
| 395 | MtpDataPacket& packet) { |
| 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | MtpResponseCode MyMtpDatabase::setDevicePropertyValue(MtpDeviceProperty property, |
| 400 | MtpDataPacket& packet) { |
| 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | MtpResponseCode MyMtpDatabase::resetDeviceProperty(MtpDeviceProperty property) { |
| 405 | return -1; |
| 406 | } |
| 407 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 408 | MtpResponseCode MyMtpDatabase::getObjectInfo(MtpObjectHandle handle, |
| 409 | MtpDataPacket& packet) { |
| 410 | char date[20]; |
| 411 | |
| 412 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 413 | jboolean result = env->CallBooleanMethod(mDatabase, method_getObjectInfo, |
| 414 | (jint)handle, mIntBuffer, mStringBuffer, mLongBuffer); |
| 415 | if (!result) |
| 416 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
| 417 | |
| 418 | jint* intValues = env->GetIntArrayElements(mIntBuffer, 0); |
| 419 | MtpStorageID storageID = intValues[0]; |
| 420 | MtpObjectFormat format = intValues[1]; |
| 421 | MtpObjectHandle parent = intValues[2]; |
| 422 | env->ReleaseIntArrayElements(mIntBuffer, intValues, 0); |
| 423 | |
| 424 | jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0); |
| 425 | uint64_t size = longValues[0]; |
| 426 | uint64_t modified = longValues[1]; |
| 427 | env->ReleaseLongArrayElements(mLongBuffer, longValues, 0); |
| 428 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 429 | // int associationType = (format == MTP_FORMAT_ASSOCIATION ? |
| 430 | // MTP_ASSOCIATION_TYPE_GENERIC_FOLDER : |
| 431 | // MTP_ASSOCIATION_TYPE_UNDEFINED); |
| 432 | int associationType = MTP_ASSOCIATION_TYPE_UNDEFINED; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 433 | |
| 434 | packet.putUInt32(storageID); |
| 435 | packet.putUInt16(format); |
| 436 | packet.putUInt16(0); // protection status |
| 437 | packet.putUInt32((size > 0xFFFFFFFFLL ? 0xFFFFFFFF : size)); |
| 438 | packet.putUInt16(0); // thumb format |
| 439 | packet.putUInt32(0); // thumb compressed size |
| 440 | packet.putUInt32(0); // thumb pix width |
| 441 | packet.putUInt32(0); // thumb pix height |
| 442 | packet.putUInt32(0); // image pix width |
| 443 | packet.putUInt32(0); // image pix height |
| 444 | packet.putUInt32(0); // image bit depth |
| 445 | packet.putUInt32(parent); |
| 446 | packet.putUInt16(associationType); |
| 447 | packet.putUInt32(0); // association desc |
| 448 | packet.putUInt32(0); // sequence number |
| 449 | |
| 450 | jchar* str = env->GetCharArrayElements(mStringBuffer, 0); |
| 451 | packet.putString(str); // file name |
| 452 | env->ReleaseCharArrayElements(mStringBuffer, str, 0); |
| 453 | |
| 454 | packet.putEmptyString(); |
| 455 | formatDateTime(modified, date, sizeof(date)); |
| 456 | packet.putString(date); // date modified |
| 457 | packet.putEmptyString(); // keywords |
| 458 | |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 459 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 460 | return MTP_RESPONSE_OK; |
| 461 | } |
| 462 | |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 463 | MtpResponseCode MyMtpDatabase::getObjectFilePath(MtpObjectHandle handle, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 464 | MtpString& filePath, |
| 465 | int64_t& fileLength) { |
| 466 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 467 | jint result = env->CallIntMethod(mDatabase, method_getObjectFilePath, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 468 | (jint)handle, mStringBuffer, mLongBuffer); |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 469 | if (result != MTP_RESPONSE_OK) |
| 470 | return result; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 471 | |
| 472 | jchar* str = env->GetCharArrayElements(mStringBuffer, 0); |
| 473 | filePath.setTo(str, strlen16(str)); |
| 474 | env->ReleaseCharArrayElements(mStringBuffer, str, 0); |
| 475 | |
| 476 | jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0); |
| 477 | fileLength = longValues[0]; |
| 478 | env->ReleaseLongArrayElements(mLongBuffer, longValues, 0); |
| 479 | |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 480 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 481 | return result; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 482 | } |
| 483 | |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 484 | MtpResponseCode MyMtpDatabase::deleteFile(MtpObjectHandle handle) { |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 485 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 486 | MtpResponseCode result = env->CallIntMethod(mDatabase, method_deleteFile, (jint)handle); |
| 487 | |
| 488 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 489 | return result; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | struct PropertyTableEntry { |
| 493 | MtpObjectProperty property; |
| 494 | int type; |
| 495 | }; |
| 496 | |
| 497 | static const PropertyTableEntry kPropertyTable[] = { |
| 498 | { MTP_PROPERTY_PARENT_OBJECT, MTP_TYPE_UINT32 }, |
| 499 | { MTP_PROPERTY_STORAGE_ID, MTP_TYPE_UINT32 }, |
Mike Lockwood | c642e8a | 2010-08-09 14:17:52 -0400 | [diff] [blame] | 500 | { MTP_PROPERTY_OBJECT_FORMAT, MTP_TYPE_UINT16 }, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 501 | { MTP_PROPERTY_OBJECT_FILE_NAME, MTP_TYPE_STR }, |
| 502 | { MTP_PROPERTY_OBJECT_SIZE, MTP_TYPE_UINT64 }, |
| 503 | { MTP_PROPERTY_DATE_MODIFIED, MTP_TYPE_STR }, |
| 504 | }; |
| 505 | |
| 506 | bool MyMtpDatabase::getPropertyInfo(MtpObjectProperty property, int& type) { |
| 507 | int count = sizeof(kPropertyTable) / sizeof(kPropertyTable[0]); |
| 508 | const PropertyTableEntry* entry = kPropertyTable; |
| 509 | for (int i = 0; i < count; i++, entry++) { |
| 510 | if (entry->property == property) { |
| 511 | type = entry->type; |
| 512 | return true; |
| 513 | } |
| 514 | } |
| 515 | return false; |
| 516 | } |
| 517 | |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 518 | MtpObjectHandleList* MyMtpDatabase::getObjectReferences(MtpObjectHandle handle) { |
| 519 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 520 | jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectReferences, |
| 521 | (jint)handle); |
| 522 | if (!array) |
| 523 | return NULL; |
| 524 | MtpObjectHandleList* list = new MtpObjectHandleList(); |
| 525 | jint* handles = env->GetIntArrayElements(array, 0); |
| 526 | jsize length = env->GetArrayLength(array); |
| 527 | for (int i = 0; i < length; i++) |
| 528 | list->push(handles[i]); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 529 | env->ReleaseIntArrayElements(array, handles, 0); |
| 530 | |
| 531 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 532 | return list; |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 533 | } |
| 534 | |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 535 | MtpResponseCode MyMtpDatabase::setObjectReferences(MtpObjectHandle handle, |
| 536 | MtpObjectHandleList* references) { |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 537 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 538 | int count = references->size(); |
| 539 | jintArray array = env->NewIntArray(count); |
| 540 | if (!array) { |
| 541 | LOGE("out of memory in setObjectReferences"); |
| 542 | return false; |
| 543 | } |
| 544 | jint* handles = env->GetIntArrayElements(array, 0); |
| 545 | for (int i = 0; i < count; i++) |
| 546 | handles[i] = (*references)[i]; |
| 547 | env->ReleaseIntArrayElements(array, handles, 0); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 548 | MtpResponseCode result = env->CallIntMethod(mDatabase, method_setObjectReferences, |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 549 | (jint)handle, array); |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 550 | |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 551 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 552 | return result; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 553 | } |
| 554 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 555 | MtpProperty* MyMtpDatabase::getObjectPropertyDesc(MtpObjectProperty property, |
| 556 | MtpObjectFormat format) { |
| 557 | MtpProperty* result = NULL; |
| 558 | switch (property) { |
| 559 | case MTP_PROPERTY_OBJECT_FORMAT: |
| 560 | case MTP_PROPERTY_PROTECTION_STATUS: |
| 561 | result = new MtpProperty(property, MTP_TYPE_UINT16); |
| 562 | break; |
| 563 | case MTP_PROPERTY_STORAGE_ID: |
| 564 | case MTP_PROPERTY_PARENT_OBJECT: |
| 565 | result = new MtpProperty(property, MTP_TYPE_UINT32); |
| 566 | break; |
| 567 | case MTP_PROPERTY_OBJECT_SIZE: |
| 568 | result = new MtpProperty(property, MTP_TYPE_UINT64); |
| 569 | break; |
| 570 | case MTP_PROPERTY_PERSISTENT_UID: |
| 571 | result = new MtpProperty(property, MTP_TYPE_UINT128); |
| 572 | break; |
| 573 | case MTP_PROPERTY_OBJECT_FILE_NAME: |
| 574 | case MTP_PROPERTY_DATE_MODIFIED: |
| 575 | result = new MtpProperty(property, MTP_TYPE_STR); |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | return result; |
| 580 | } |
| 581 | |
| 582 | MtpProperty* MyMtpDatabase::getDevicePropertyDesc(MtpDeviceProperty property) { |
| 583 | return NULL; |
| 584 | } |
| 585 | |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 586 | #endif // HAVE_ANDROID_OS |
| 587 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 588 | // ---------------------------------------------------------------------------- |
| 589 | |
| 590 | static void |
| 591 | android_media_MtpDatabase_setup(JNIEnv *env, jobject thiz) |
| 592 | { |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 593 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 594 | LOGD("setup\n"); |
| 595 | MyMtpDatabase* database = new MyMtpDatabase(env, thiz); |
| 596 | env->SetIntField(thiz, field_context, (int)database); |
| 597 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 598 | #endif |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | static void |
| 602 | android_media_MtpDatabase_finalize(JNIEnv *env, jobject thiz) |
| 603 | { |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 604 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 605 | LOGD("finalize\n"); |
| 606 | MyMtpDatabase* database = (MyMtpDatabase *)env->GetIntField(thiz, field_context); |
| 607 | database->cleanup(env); |
| 608 | delete database; |
| 609 | env->SetIntField(thiz, field_context, 0); |
| 610 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 611 | #endif |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | // ---------------------------------------------------------------------------- |
| 615 | |
| 616 | static JNINativeMethod gMethods[] = { |
| 617 | {"native_setup", "()V", (void *)android_media_MtpDatabase_setup}, |
| 618 | {"native_finalize", "()V", (void *)android_media_MtpDatabase_finalize}, |
| 619 | }; |
| 620 | |
| 621 | static const char* const kClassPathName = "android/media/MtpDatabase"; |
| 622 | |
| 623 | int register_android_media_MtpDatabase(JNIEnv *env) |
| 624 | { |
| 625 | jclass clazz; |
| 626 | |
| 627 | LOGD("register_android_media_MtpDatabase\n"); |
| 628 | |
| 629 | clazz = env->FindClass("android/media/MtpDatabase"); |
| 630 | if (clazz == NULL) { |
| 631 | LOGE("Can't find android/media/MtpDatabase"); |
| 632 | return -1; |
| 633 | } |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 634 | method_beginSendObject = env->GetMethodID(clazz, "beginSendObject", "(Ljava/lang/String;IIIJJ)I"); |
| 635 | if (method_beginSendObject == NULL) { |
| 636 | LOGE("Can't find beginSendObject"); |
| 637 | return -1; |
| 638 | } |
| 639 | method_endSendObject = env->GetMethodID(clazz, "endSendObject", "(Ljava/lang/String;IIZ)V"); |
| 640 | if (method_endSendObject == NULL) { |
| 641 | LOGE("Can't find endSendObject"); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 642 | return -1; |
| 643 | } |
| 644 | method_getObjectList = env->GetMethodID(clazz, "getObjectList", "(III)[I"); |
| 645 | if (method_getObjectList == NULL) { |
| 646 | LOGE("Can't find getObjectList"); |
| 647 | return -1; |
| 648 | } |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 649 | method_getNumObjects = env->GetMethodID(clazz, "getNumObjects", "(III)I"); |
| 650 | if (method_getNumObjects == NULL) { |
| 651 | LOGE("Can't find getNumObjects"); |
| 652 | return -1; |
| 653 | } |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 654 | method_getSupportedPlaybackFormats = env->GetMethodID(clazz, "getSupportedPlaybackFormats", "()[I"); |
| 655 | if (method_getSupportedPlaybackFormats == NULL) { |
| 656 | LOGE("Can't find getSupportedPlaybackFormats"); |
| 657 | return -1; |
| 658 | } |
| 659 | method_getSupportedCaptureFormats = env->GetMethodID(clazz, "getSupportedCaptureFormats", "()[I"); |
| 660 | if (method_getSupportedCaptureFormats == NULL) { |
| 661 | LOGE("Can't find getSupportedCaptureFormats"); |
| 662 | return -1; |
| 663 | } |
| 664 | method_getSupportedObjectProperties = env->GetMethodID(clazz, "getSupportedObjectProperties", "(I)[I"); |
| 665 | if (method_getSupportedObjectProperties == NULL) { |
| 666 | LOGE("Can't find getSupportedObjectProperties"); |
| 667 | return -1; |
| 668 | } |
| 669 | method_getSupportedDeviceProperties = env->GetMethodID(clazz, "getSupportedDeviceProperties", "()[I"); |
| 670 | if (method_getSupportedDeviceProperties == NULL) { |
| 671 | LOGE("Can't find getSupportedDeviceProperties"); |
| 672 | return -1; |
| 673 | } |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 674 | method_getObjectProperty = env->GetMethodID(clazz, "getObjectProperty", "(II[J[C)I"); |
| 675 | if (method_getObjectProperty == NULL) { |
| 676 | LOGE("Can't find getObjectProperty"); |
| 677 | return -1; |
| 678 | } |
| 679 | method_getObjectInfo = env->GetMethodID(clazz, "getObjectInfo", "(I[I[C[J)Z"); |
| 680 | if (method_getObjectInfo == NULL) { |
| 681 | LOGE("Can't find getObjectInfo"); |
| 682 | return -1; |
| 683 | } |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 684 | method_getObjectFilePath = env->GetMethodID(clazz, "getObjectFilePath", "(I[C[J)I"); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 685 | if (method_getObjectFilePath == NULL) { |
| 686 | LOGE("Can't find getObjectFilePath"); |
| 687 | return -1; |
| 688 | } |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 689 | method_deleteFile = env->GetMethodID(clazz, "deleteFile", "(I)I"); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 690 | if (method_deleteFile == NULL) { |
| 691 | LOGE("Can't find deleteFile"); |
| 692 | return -1; |
| 693 | } |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 694 | method_getObjectReferences = env->GetMethodID(clazz, "getObjectReferences", "(I)[I"); |
| 695 | if (method_getObjectReferences == NULL) { |
| 696 | LOGE("Can't find getObjectReferences"); |
| 697 | return -1; |
| 698 | } |
| 699 | method_setObjectReferences = env->GetMethodID(clazz, "setObjectReferences", "(I[I)I"); |
| 700 | if (method_setObjectReferences == NULL) { |
| 701 | LOGE("Can't find setObjectReferences"); |
| 702 | return -1; |
| 703 | } |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 704 | field_context = env->GetFieldID(clazz, "mNativeContext", "I"); |
| 705 | if (field_context == NULL) { |
| 706 | LOGE("Can't find MtpDatabase.mNativeContext"); |
| 707 | return -1; |
| 708 | } |
| 709 | |
| 710 | return AndroidRuntime::registerNativeMethods(env, |
| 711 | "android/media/MtpDatabase", gMethods, NELEM(gMethods)); |
| 712 | } |