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 | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 33 | #include "MtpStringBuffer.h" |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 34 | #include "MtpUtils.h" |
| 35 | #include "mtp.h" |
| 36 | |
| 37 | using namespace android; |
| 38 | |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 41 | static jmethodID method_beginSendObject; |
| 42 | static jmethodID method_endSendObject; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 43 | static jmethodID method_getObjectList; |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 44 | static jmethodID method_getNumObjects; |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 45 | static jmethodID method_getSupportedPlaybackFormats; |
| 46 | static jmethodID method_getSupportedCaptureFormats; |
| 47 | static jmethodID method_getSupportedObjectProperties; |
| 48 | static jmethodID method_getSupportedDeviceProperties; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 49 | static jmethodID method_getObjectProperty; |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 50 | static jmethodID method_setObjectProperty; |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 51 | static jmethodID method_getDeviceProperty; |
| 52 | static jmethodID method_setDeviceProperty; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 53 | static jmethodID method_getObjectInfo; |
| 54 | static jmethodID method_getObjectFilePath; |
| 55 | static jmethodID method_deleteFile; |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 56 | static jmethodID method_getObjectReferences; |
| 57 | static jmethodID method_setObjectReferences; |
Mike Lockwood | 2837eef | 2010-08-31 16:25:12 -0400 | [diff] [blame] | 58 | static jmethodID method_sessionStarted; |
| 59 | static jmethodID method_sessionEnded; |
| 60 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 61 | static jfieldID field_context; |
| 62 | |
| 63 | MtpDatabase* getMtpDatabase(JNIEnv *env, jobject database) { |
| 64 | return (MtpDatabase *)env->GetIntField(database, field_context); |
| 65 | } |
| 66 | |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 67 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 68 | // ---------------------------------------------------------------------------- |
| 69 | |
| 70 | class MyMtpDatabase : public MtpDatabase { |
| 71 | private: |
| 72 | jobject mDatabase; |
| 73 | jintArray mIntBuffer; |
| 74 | jlongArray mLongBuffer; |
| 75 | jcharArray mStringBuffer; |
| 76 | |
| 77 | public: |
| 78 | MyMtpDatabase(JNIEnv *env, jobject client); |
| 79 | virtual ~MyMtpDatabase(); |
| 80 | void cleanup(JNIEnv *env); |
| 81 | |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 82 | virtual MtpObjectHandle beginSendObject(const char* path, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 83 | MtpObjectFormat format, |
| 84 | MtpObjectHandle parent, |
| 85 | MtpStorageID storage, |
| 86 | uint64_t size, |
| 87 | time_t modified); |
| 88 | |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 89 | virtual void endSendObject(const char* path, |
| 90 | MtpObjectHandle handle, |
| 91 | MtpObjectFormat format, |
| 92 | bool succeeded); |
| 93 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 94 | virtual MtpObjectHandleList* getObjectList(MtpStorageID storageID, |
| 95 | MtpObjectFormat format, |
| 96 | MtpObjectHandle parent); |
| 97 | |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 98 | virtual int getNumObjects(MtpStorageID storageID, |
| 99 | MtpObjectFormat format, |
| 100 | MtpObjectHandle parent); |
| 101 | |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 102 | // callee should delete[] the results from these |
| 103 | // results can be NULL |
| 104 | virtual MtpObjectFormatList* getSupportedPlaybackFormats(); |
| 105 | virtual MtpObjectFormatList* getSupportedCaptureFormats(); |
| 106 | virtual MtpObjectPropertyList* getSupportedObjectProperties(MtpObjectFormat format); |
| 107 | virtual MtpDevicePropertyList* getSupportedDeviceProperties(); |
| 108 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 109 | virtual MtpResponseCode getObjectPropertyValue(MtpObjectHandle handle, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 110 | MtpObjectProperty property, |
| 111 | MtpDataPacket& packet); |
| 112 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 113 | virtual MtpResponseCode setObjectPropertyValue(MtpObjectHandle handle, |
| 114 | MtpObjectProperty property, |
| 115 | MtpDataPacket& packet); |
| 116 | |
| 117 | virtual MtpResponseCode getDevicePropertyValue(MtpDeviceProperty property, |
| 118 | MtpDataPacket& packet); |
| 119 | |
| 120 | virtual MtpResponseCode setDevicePropertyValue(MtpDeviceProperty property, |
| 121 | MtpDataPacket& packet); |
| 122 | |
| 123 | virtual MtpResponseCode resetDeviceProperty(MtpDeviceProperty property); |
| 124 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 125 | virtual MtpResponseCode getObjectInfo(MtpObjectHandle handle, |
| 126 | MtpDataPacket& packet); |
| 127 | |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 128 | virtual MtpResponseCode getObjectFilePath(MtpObjectHandle handle, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 129 | MtpString& filePath, |
| 130 | int64_t& fileLength); |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 131 | virtual MtpResponseCode deleteFile(MtpObjectHandle handle); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 132 | |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 133 | bool getObjectPropertyInfo(MtpObjectProperty property, int& type); |
| 134 | bool getDevicePropertyInfo(MtpDeviceProperty property, int& type); |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 135 | |
| 136 | virtual MtpObjectHandleList* getObjectReferences(MtpObjectHandle handle); |
| 137 | |
| 138 | virtual MtpResponseCode setObjectReferences(MtpObjectHandle handle, |
| 139 | MtpObjectHandleList* references); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 140 | |
| 141 | virtual MtpProperty* getObjectPropertyDesc(MtpObjectProperty property, |
| 142 | MtpObjectFormat format); |
| 143 | |
| 144 | virtual MtpProperty* getDevicePropertyDesc(MtpDeviceProperty property); |
Mike Lockwood | 2837eef | 2010-08-31 16:25:12 -0400 | [diff] [blame] | 145 | |
| 146 | virtual void sessionStarted(); |
| 147 | |
| 148 | virtual void sessionEnded(); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 149 | }; |
| 150 | |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 151 | // ---------------------------------------------------------------------------- |
| 152 | |
| 153 | static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 154 | if (env->ExceptionCheck()) { |
| 155 | LOGE("An exception was thrown by callback '%s'.", methodName); |
| 156 | LOGE_EX(env); |
| 157 | env->ExceptionClear(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // ---------------------------------------------------------------------------- |
| 162 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 163 | MyMtpDatabase::MyMtpDatabase(JNIEnv *env, jobject client) |
| 164 | : mDatabase(env->NewGlobalRef(client)), |
| 165 | mIntBuffer(NULL), |
| 166 | mLongBuffer(NULL), |
| 167 | mStringBuffer(NULL) |
| 168 | { |
| 169 | jintArray intArray; |
| 170 | jlongArray longArray; |
| 171 | jcharArray charArray; |
| 172 | |
| 173 | // create buffers for out arguments |
| 174 | // we don't need to be thread-safe so this is OK |
| 175 | intArray = env->NewIntArray(3); |
| 176 | if (!intArray) |
| 177 | goto out_of_memory; |
| 178 | mIntBuffer = (jintArray)env->NewGlobalRef(intArray); |
| 179 | longArray = env->NewLongArray(2); |
| 180 | if (!longArray) |
| 181 | goto out_of_memory; |
| 182 | mLongBuffer = (jlongArray)env->NewGlobalRef(longArray); |
| 183 | charArray = env->NewCharArray(256); |
| 184 | if (!charArray) |
| 185 | goto out_of_memory; |
| 186 | mStringBuffer = (jcharArray)env->NewGlobalRef(charArray); |
| 187 | return; |
| 188 | |
| 189 | out_of_memory: |
| 190 | env->ThrowNew(env->FindClass("java/lang/OutOfMemoryError"), NULL); |
| 191 | } |
| 192 | |
| 193 | void MyMtpDatabase::cleanup(JNIEnv *env) { |
| 194 | env->DeleteGlobalRef(mDatabase); |
| 195 | env->DeleteGlobalRef(mIntBuffer); |
| 196 | env->DeleteGlobalRef(mLongBuffer); |
| 197 | env->DeleteGlobalRef(mStringBuffer); |
| 198 | } |
| 199 | |
| 200 | MyMtpDatabase::~MyMtpDatabase() { |
| 201 | } |
| 202 | |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 203 | MtpObjectHandle MyMtpDatabase::beginSendObject(const char* path, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 204 | MtpObjectFormat format, |
| 205 | MtpObjectHandle parent, |
| 206 | MtpStorageID storage, |
| 207 | uint64_t size, |
| 208 | time_t modified) { |
| 209 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 210 | MtpObjectHandle result = env->CallIntMethod(mDatabase, method_beginSendObject, |
| 211 | env->NewStringUTF(path), (jint)format, (jint)parent, (jint)storage, |
| 212 | (jlong)size, (jlong)modified); |
| 213 | |
| 214 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 215 | return result; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 216 | } |
| 217 | |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 218 | void MyMtpDatabase::endSendObject(const char* path, MtpObjectHandle handle, |
| 219 | MtpObjectFormat format, bool succeeded) { |
| 220 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 221 | env->CallVoidMethod(mDatabase, method_endSendObject, env->NewStringUTF(path), |
| 222 | (jint)handle, (jint)format, (jboolean)succeeded); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 223 | |
| 224 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 225 | } |
| 226 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 227 | MtpObjectHandleList* MyMtpDatabase::getObjectList(MtpStorageID storageID, |
| 228 | MtpObjectFormat format, |
| 229 | MtpObjectHandle parent) { |
| 230 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 231 | jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectList, |
| 232 | (jint)storageID, (jint)format, (jint)parent); |
| 233 | if (!array) |
| 234 | return NULL; |
| 235 | MtpObjectHandleList* list = new MtpObjectHandleList(); |
| 236 | jint* handles = env->GetIntArrayElements(array, 0); |
| 237 | jsize length = env->GetArrayLength(array); |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 238 | for (int i = 0; i < length; i++) |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 239 | list->push(handles[i]); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 240 | env->ReleaseIntArrayElements(array, handles, 0); |
| 241 | |
| 242 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 243 | return list; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 244 | } |
| 245 | |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 246 | int MyMtpDatabase::getNumObjects(MtpStorageID storageID, |
| 247 | MtpObjectFormat format, |
| 248 | MtpObjectHandle parent) { |
| 249 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 250 | int result = env->CallIntMethod(mDatabase, method_getNumObjects, |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 251 | (jint)storageID, (jint)format, (jint)parent); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 252 | |
| 253 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 254 | return result; |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 255 | } |
| 256 | |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 257 | MtpObjectFormatList* MyMtpDatabase::getSupportedPlaybackFormats() { |
| 258 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 259 | jintArray array = (jintArray)env->CallObjectMethod(mDatabase, |
| 260 | method_getSupportedPlaybackFormats); |
| 261 | if (!array) |
| 262 | return NULL; |
| 263 | MtpObjectFormatList* list = new MtpObjectFormatList(); |
| 264 | jint* formats = env->GetIntArrayElements(array, 0); |
| 265 | jsize length = env->GetArrayLength(array); |
| 266 | for (int i = 0; i < length; i++) |
| 267 | list->push(formats[i]); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 268 | env->ReleaseIntArrayElements(array, formats, 0); |
| 269 | |
| 270 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 271 | return list; |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | MtpObjectFormatList* MyMtpDatabase::getSupportedCaptureFormats() { |
| 275 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 276 | jintArray array = (jintArray)env->CallObjectMethod(mDatabase, |
| 277 | method_getSupportedCaptureFormats); |
| 278 | if (!array) |
| 279 | return NULL; |
| 280 | MtpObjectFormatList* list = new MtpObjectFormatList(); |
| 281 | jint* formats = env->GetIntArrayElements(array, 0); |
| 282 | jsize length = env->GetArrayLength(array); |
| 283 | for (int i = 0; i < length; i++) |
| 284 | list->push(formats[i]); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 285 | env->ReleaseIntArrayElements(array, formats, 0); |
| 286 | |
| 287 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 288 | return list; |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | MtpObjectPropertyList* MyMtpDatabase::getSupportedObjectProperties(MtpObjectFormat format) { |
| 292 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 293 | jintArray array = (jintArray)env->CallObjectMethod(mDatabase, |
| 294 | method_getSupportedObjectProperties, (jint)format); |
| 295 | if (!array) |
| 296 | return NULL; |
| 297 | MtpObjectPropertyList* list = new MtpObjectPropertyList(); |
| 298 | jint* properties = env->GetIntArrayElements(array, 0); |
| 299 | jsize length = env->GetArrayLength(array); |
| 300 | for (int i = 0; i < length; i++) |
| 301 | list->push(properties[i]); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 302 | env->ReleaseIntArrayElements(array, properties, 0); |
| 303 | |
| 304 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 305 | return list; |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | MtpDevicePropertyList* MyMtpDatabase::getSupportedDeviceProperties() { |
| 309 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 310 | jintArray array = (jintArray)env->CallObjectMethod(mDatabase, |
| 311 | method_getSupportedDeviceProperties); |
| 312 | if (!array) |
| 313 | return NULL; |
| 314 | MtpDevicePropertyList* list = new MtpDevicePropertyList(); |
| 315 | jint* properties = env->GetIntArrayElements(array, 0); |
| 316 | jsize length = env->GetArrayLength(array); |
| 317 | for (int i = 0; i < length; i++) |
| 318 | list->push(properties[i]); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 319 | env->ReleaseIntArrayElements(array, properties, 0); |
| 320 | |
| 321 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 322 | return list; |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 323 | } |
| 324 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 325 | MtpResponseCode MyMtpDatabase::getObjectPropertyValue(MtpObjectHandle handle, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 326 | MtpObjectProperty property, |
| 327 | MtpDataPacket& packet) { |
| 328 | int type; |
| 329 | |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 330 | if (!getObjectPropertyInfo(property, type)) |
| 331 | return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 332 | |
| 333 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 334 | jint result = env->CallIntMethod(mDatabase, method_getObjectProperty, |
| 335 | (jint)handle, (jint)property, mLongBuffer, mStringBuffer); |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 336 | if (result != MTP_RESPONSE_OK) { |
| 337 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 338 | return result; |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 339 | } |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 340 | |
| 341 | jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0); |
| 342 | jlong longValue = longValues[0]; |
| 343 | env->ReleaseLongArrayElements(mLongBuffer, longValues, 0); |
| 344 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 345 | // special case MTP_PROPERTY_DATE_MODIFIED, which is a string to MTP |
| 346 | // but stored internally as a uint64 |
| 347 | if (property == MTP_PROPERTY_DATE_MODIFIED) { |
| 348 | char date[20]; |
| 349 | formatDateTime(longValue, date, sizeof(date)); |
| 350 | packet.putString(date); |
| 351 | return MTP_RESPONSE_OK; |
| 352 | } |
| 353 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 354 | switch (type) { |
| 355 | case MTP_TYPE_INT8: |
| 356 | packet.putInt8(longValue); |
| 357 | break; |
| 358 | case MTP_TYPE_UINT8: |
| 359 | packet.putUInt8(longValue); |
| 360 | break; |
| 361 | case MTP_TYPE_INT16: |
| 362 | packet.putInt16(longValue); |
| 363 | break; |
| 364 | case MTP_TYPE_UINT16: |
| 365 | packet.putUInt16(longValue); |
| 366 | break; |
| 367 | case MTP_TYPE_INT32: |
| 368 | packet.putInt32(longValue); |
| 369 | break; |
| 370 | case MTP_TYPE_UINT32: |
| 371 | packet.putUInt32(longValue); |
| 372 | break; |
| 373 | case MTP_TYPE_INT64: |
| 374 | packet.putInt64(longValue); |
| 375 | break; |
| 376 | case MTP_TYPE_UINT64: |
| 377 | packet.putUInt64(longValue); |
| 378 | break; |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 379 | case MTP_TYPE_INT128: |
| 380 | packet.putInt128(longValue); |
| 381 | break; |
| 382 | case MTP_TYPE_UINT128: |
| 383 | packet.putInt128(longValue); |
| 384 | break; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 385 | case MTP_TYPE_STR: |
| 386 | { |
| 387 | jchar* str = env->GetCharArrayElements(mStringBuffer, 0); |
| 388 | packet.putString(str); |
| 389 | env->ReleaseCharArrayElements(mStringBuffer, str, 0); |
| 390 | break; |
| 391 | } |
| 392 | default: |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 393 | LOGE("unsupported type in getObjectPropertyValue\n"); |
| 394 | return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 395 | } |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 396 | |
| 397 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 398 | return MTP_RESPONSE_OK; |
| 399 | } |
| 400 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 401 | MtpResponseCode MyMtpDatabase::setObjectPropertyValue(MtpObjectHandle handle, |
| 402 | MtpObjectProperty property, |
| 403 | MtpDataPacket& packet) { |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 404 | int type; |
| 405 | |
| 406 | if (!getObjectPropertyInfo(property, type)) |
| 407 | return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED; |
| 408 | |
| 409 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 410 | jlong longValue = 0; |
| 411 | jstring stringValue = NULL; |
| 412 | |
| 413 | switch (type) { |
| 414 | case MTP_TYPE_INT8: |
| 415 | longValue = packet.getInt8(); |
| 416 | break; |
| 417 | case MTP_TYPE_UINT8: |
| 418 | longValue = packet.getUInt8(); |
| 419 | break; |
| 420 | case MTP_TYPE_INT16: |
| 421 | longValue = packet.getInt16(); |
| 422 | break; |
| 423 | case MTP_TYPE_UINT16: |
| 424 | longValue = packet.getUInt16(); |
| 425 | break; |
| 426 | case MTP_TYPE_INT32: |
| 427 | longValue = packet.getInt32(); |
| 428 | break; |
| 429 | case MTP_TYPE_UINT32: |
| 430 | longValue = packet.getUInt32(); |
| 431 | break; |
| 432 | case MTP_TYPE_INT64: |
| 433 | longValue = packet.getInt64(); |
| 434 | break; |
| 435 | case MTP_TYPE_UINT64: |
| 436 | longValue = packet.getUInt64(); |
| 437 | break; |
| 438 | case MTP_TYPE_STR: |
| 439 | { |
| 440 | MtpStringBuffer buffer; |
| 441 | packet.getString(buffer); |
| 442 | stringValue = env->NewStringUTF((const char *)buffer); |
| 443 | break; |
| 444 | } |
| 445 | default: |
| 446 | LOGE("unsupported type in getObjectPropertyValue\n"); |
| 447 | return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT; |
| 448 | } |
| 449 | |
| 450 | jint result = env->CallIntMethod(mDatabase, method_setObjectProperty, |
| 451 | (jint)handle, (jint)property, longValue, stringValue); |
| 452 | |
| 453 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 454 | return result; |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | MtpResponseCode MyMtpDatabase::getDevicePropertyValue(MtpDeviceProperty property, |
| 458 | MtpDataPacket& packet) { |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 459 | |
| 460 | int type; |
| 461 | |
| 462 | if (!getDevicePropertyInfo(property, type)) |
| 463 | return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED; |
| 464 | |
| 465 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 466 | jint result = env->CallIntMethod(mDatabase, method_getDeviceProperty, |
| 467 | (jint)property, mLongBuffer, mStringBuffer); |
| 468 | if (result != MTP_RESPONSE_OK) { |
| 469 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 470 | return result; |
| 471 | } |
| 472 | |
| 473 | jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0); |
| 474 | jlong longValue = longValues[0]; |
| 475 | env->ReleaseLongArrayElements(mLongBuffer, longValues, 0); |
| 476 | |
| 477 | switch (type) { |
| 478 | case MTP_TYPE_INT8: |
| 479 | packet.putInt8(longValue); |
| 480 | break; |
| 481 | case MTP_TYPE_UINT8: |
| 482 | packet.putUInt8(longValue); |
| 483 | break; |
| 484 | case MTP_TYPE_INT16: |
| 485 | packet.putInt16(longValue); |
| 486 | break; |
| 487 | case MTP_TYPE_UINT16: |
| 488 | packet.putUInt16(longValue); |
| 489 | break; |
| 490 | case MTP_TYPE_INT32: |
| 491 | packet.putInt32(longValue); |
| 492 | break; |
| 493 | case MTP_TYPE_UINT32: |
| 494 | packet.putUInt32(longValue); |
| 495 | break; |
| 496 | case MTP_TYPE_INT64: |
| 497 | packet.putInt64(longValue); |
| 498 | break; |
| 499 | case MTP_TYPE_UINT64: |
| 500 | packet.putUInt64(longValue); |
| 501 | break; |
| 502 | case MTP_TYPE_INT128: |
| 503 | packet.putInt128(longValue); |
| 504 | break; |
| 505 | case MTP_TYPE_UINT128: |
| 506 | packet.putInt128(longValue); |
| 507 | break; |
| 508 | case MTP_TYPE_STR: |
| 509 | { |
| 510 | jchar* str = env->GetCharArrayElements(mStringBuffer, 0); |
| 511 | packet.putString(str); |
| 512 | env->ReleaseCharArrayElements(mStringBuffer, str, 0); |
| 513 | break; |
| 514 | } |
| 515 | default: |
| 516 | LOGE("unsupported type in getDevicePropertyValue\n"); |
| 517 | return MTP_RESPONSE_INVALID_DEVICE_PROP_FORMAT; |
| 518 | } |
| 519 | |
| 520 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 521 | return MTP_RESPONSE_OK; |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | MtpResponseCode MyMtpDatabase::setDevicePropertyValue(MtpDeviceProperty property, |
| 525 | MtpDataPacket& packet) { |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 526 | int type; |
| 527 | |
| 528 | if (!getDevicePropertyInfo(property, type)) |
| 529 | return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED; |
| 530 | |
| 531 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 532 | jlong longValue = 0; |
| 533 | jstring stringValue = NULL; |
| 534 | |
| 535 | switch (type) { |
| 536 | case MTP_TYPE_INT8: |
| 537 | longValue = packet.getInt8(); |
| 538 | break; |
| 539 | case MTP_TYPE_UINT8: |
| 540 | longValue = packet.getUInt8(); |
| 541 | break; |
| 542 | case MTP_TYPE_INT16: |
| 543 | longValue = packet.getInt16(); |
| 544 | break; |
| 545 | case MTP_TYPE_UINT16: |
| 546 | longValue = packet.getUInt16(); |
| 547 | break; |
| 548 | case MTP_TYPE_INT32: |
| 549 | longValue = packet.getInt32(); |
| 550 | break; |
| 551 | case MTP_TYPE_UINT32: |
| 552 | longValue = packet.getUInt32(); |
| 553 | break; |
| 554 | case MTP_TYPE_INT64: |
| 555 | longValue = packet.getInt64(); |
| 556 | break; |
| 557 | case MTP_TYPE_UINT64: |
| 558 | longValue = packet.getUInt64(); |
| 559 | break; |
| 560 | case MTP_TYPE_STR: |
| 561 | { |
| 562 | MtpStringBuffer buffer; |
| 563 | packet.getString(buffer); |
| 564 | stringValue = env->NewStringUTF((const char *)buffer); |
| 565 | break; |
| 566 | } |
| 567 | default: |
| 568 | LOGE("unsupported type in setDevicePropertyValue\n"); |
| 569 | return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT; |
| 570 | } |
| 571 | |
| 572 | jint result = env->CallIntMethod(mDatabase, method_setDeviceProperty, |
| 573 | (jint)property, longValue, stringValue); |
| 574 | |
| 575 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 576 | return result; |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | MtpResponseCode MyMtpDatabase::resetDeviceProperty(MtpDeviceProperty property) { |
| 580 | return -1; |
| 581 | } |
| 582 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 583 | MtpResponseCode MyMtpDatabase::getObjectInfo(MtpObjectHandle handle, |
| 584 | MtpDataPacket& packet) { |
| 585 | char date[20]; |
| 586 | |
| 587 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 588 | jboolean result = env->CallBooleanMethod(mDatabase, method_getObjectInfo, |
| 589 | (jint)handle, mIntBuffer, mStringBuffer, mLongBuffer); |
| 590 | if (!result) |
| 591 | return MTP_RESPONSE_INVALID_OBJECT_HANDLE; |
| 592 | |
| 593 | jint* intValues = env->GetIntArrayElements(mIntBuffer, 0); |
| 594 | MtpStorageID storageID = intValues[0]; |
| 595 | MtpObjectFormat format = intValues[1]; |
| 596 | MtpObjectHandle parent = intValues[2]; |
| 597 | env->ReleaseIntArrayElements(mIntBuffer, intValues, 0); |
| 598 | |
| 599 | jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0); |
| 600 | uint64_t size = longValues[0]; |
| 601 | uint64_t modified = longValues[1]; |
| 602 | env->ReleaseLongArrayElements(mLongBuffer, longValues, 0); |
| 603 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 604 | // int associationType = (format == MTP_FORMAT_ASSOCIATION ? |
| 605 | // MTP_ASSOCIATION_TYPE_GENERIC_FOLDER : |
| 606 | // MTP_ASSOCIATION_TYPE_UNDEFINED); |
| 607 | int associationType = MTP_ASSOCIATION_TYPE_UNDEFINED; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 608 | |
| 609 | packet.putUInt32(storageID); |
| 610 | packet.putUInt16(format); |
| 611 | packet.putUInt16(0); // protection status |
| 612 | packet.putUInt32((size > 0xFFFFFFFFLL ? 0xFFFFFFFF : size)); |
| 613 | packet.putUInt16(0); // thumb format |
| 614 | packet.putUInt32(0); // thumb compressed size |
| 615 | packet.putUInt32(0); // thumb pix width |
| 616 | packet.putUInt32(0); // thumb pix height |
| 617 | packet.putUInt32(0); // image pix width |
| 618 | packet.putUInt32(0); // image pix height |
| 619 | packet.putUInt32(0); // image bit depth |
| 620 | packet.putUInt32(parent); |
| 621 | packet.putUInt16(associationType); |
| 622 | packet.putUInt32(0); // association desc |
| 623 | packet.putUInt32(0); // sequence number |
| 624 | |
| 625 | jchar* str = env->GetCharArrayElements(mStringBuffer, 0); |
| 626 | packet.putString(str); // file name |
| 627 | env->ReleaseCharArrayElements(mStringBuffer, str, 0); |
| 628 | |
| 629 | packet.putEmptyString(); |
| 630 | formatDateTime(modified, date, sizeof(date)); |
| 631 | packet.putString(date); // date modified |
| 632 | packet.putEmptyString(); // keywords |
| 633 | |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 634 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 635 | return MTP_RESPONSE_OK; |
| 636 | } |
| 637 | |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 638 | MtpResponseCode MyMtpDatabase::getObjectFilePath(MtpObjectHandle handle, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 639 | MtpString& filePath, |
| 640 | int64_t& fileLength) { |
| 641 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 642 | jint result = env->CallIntMethod(mDatabase, method_getObjectFilePath, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 643 | (jint)handle, mStringBuffer, mLongBuffer); |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 644 | if (result != MTP_RESPONSE_OK) { |
| 645 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 646 | return result; |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 647 | } |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 648 | |
| 649 | jchar* str = env->GetCharArrayElements(mStringBuffer, 0); |
| 650 | filePath.setTo(str, strlen16(str)); |
| 651 | env->ReleaseCharArrayElements(mStringBuffer, str, 0); |
| 652 | |
| 653 | jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0); |
| 654 | fileLength = longValues[0]; |
| 655 | env->ReleaseLongArrayElements(mLongBuffer, longValues, 0); |
| 656 | |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 657 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 658 | return result; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 659 | } |
| 660 | |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 661 | MtpResponseCode MyMtpDatabase::deleteFile(MtpObjectHandle handle) { |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 662 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 663 | MtpResponseCode result = env->CallIntMethod(mDatabase, method_deleteFile, (jint)handle); |
| 664 | |
| 665 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 666 | return result; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | struct PropertyTableEntry { |
| 670 | MtpObjectProperty property; |
| 671 | int type; |
| 672 | }; |
| 673 | |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 674 | static const PropertyTableEntry kObjectPropertyTable[] = { |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 675 | { MTP_PROPERTY_PARENT_OBJECT, MTP_TYPE_UINT32 }, |
| 676 | { MTP_PROPERTY_STORAGE_ID, MTP_TYPE_UINT32 }, |
Mike Lockwood | c642e8a | 2010-08-09 14:17:52 -0400 | [diff] [blame] | 677 | { MTP_PROPERTY_OBJECT_FORMAT, MTP_TYPE_UINT16 }, |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 678 | { MTP_PROPERTY_OBJECT_FILE_NAME, MTP_TYPE_STR }, |
| 679 | { MTP_PROPERTY_OBJECT_SIZE, MTP_TYPE_UINT64 }, |
| 680 | { MTP_PROPERTY_DATE_MODIFIED, MTP_TYPE_STR }, |
| 681 | }; |
| 682 | |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 683 | static const PropertyTableEntry kDevicePropertyTable[] = { |
| 684 | { MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER, MTP_TYPE_STR }, |
| 685 | { MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME, MTP_TYPE_STR }, |
| 686 | }; |
| 687 | |
| 688 | bool MyMtpDatabase::getObjectPropertyInfo(MtpObjectProperty property, int& type) { |
| 689 | int count = sizeof(kObjectPropertyTable) / sizeof(kObjectPropertyTable[0]); |
| 690 | const PropertyTableEntry* entry = kObjectPropertyTable; |
| 691 | for (int i = 0; i < count; i++, entry++) { |
| 692 | if (entry->property == property) { |
| 693 | type = entry->type; |
| 694 | return true; |
| 695 | } |
| 696 | } |
| 697 | return false; |
| 698 | } |
| 699 | |
| 700 | bool MyMtpDatabase::getDevicePropertyInfo(MtpDeviceProperty property, int& type) { |
| 701 | int count = sizeof(kDevicePropertyTable) / sizeof(kDevicePropertyTable[0]); |
| 702 | const PropertyTableEntry* entry = kDevicePropertyTable; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 703 | for (int i = 0; i < count; i++, entry++) { |
| 704 | if (entry->property == property) { |
| 705 | type = entry->type; |
| 706 | return true; |
| 707 | } |
| 708 | } |
| 709 | return false; |
| 710 | } |
| 711 | |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 712 | MtpObjectHandleList* MyMtpDatabase::getObjectReferences(MtpObjectHandle handle) { |
| 713 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 714 | jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectReferences, |
| 715 | (jint)handle); |
| 716 | if (!array) |
| 717 | return NULL; |
| 718 | MtpObjectHandleList* list = new MtpObjectHandleList(); |
| 719 | jint* handles = env->GetIntArrayElements(array, 0); |
| 720 | jsize length = env->GetArrayLength(array); |
| 721 | for (int i = 0; i < length; i++) |
| 722 | list->push(handles[i]); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 723 | env->ReleaseIntArrayElements(array, handles, 0); |
| 724 | |
| 725 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 726 | return list; |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 727 | } |
| 728 | |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 729 | MtpResponseCode MyMtpDatabase::setObjectReferences(MtpObjectHandle handle, |
| 730 | MtpObjectHandleList* references) { |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 731 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 732 | int count = references->size(); |
| 733 | jintArray array = env->NewIntArray(count); |
| 734 | if (!array) { |
| 735 | LOGE("out of memory in setObjectReferences"); |
| 736 | return false; |
| 737 | } |
| 738 | jint* handles = env->GetIntArrayElements(array, 0); |
| 739 | for (int i = 0; i < count; i++) |
| 740 | handles[i] = (*references)[i]; |
| 741 | env->ReleaseIntArrayElements(array, handles, 0); |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 742 | MtpResponseCode result = env->CallIntMethod(mDatabase, method_setObjectReferences, |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 743 | (jint)handle, array); |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 744 | |
Mike Lockwood | 0a7fa0a | 2010-08-24 11:25:28 -0400 | [diff] [blame] | 745 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 746 | return result; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 747 | } |
| 748 | |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 749 | MtpProperty* MyMtpDatabase::getObjectPropertyDesc(MtpObjectProperty property, |
| 750 | MtpObjectFormat format) { |
| 751 | MtpProperty* result = NULL; |
| 752 | switch (property) { |
| 753 | case MTP_PROPERTY_OBJECT_FORMAT: |
| 754 | case MTP_PROPERTY_PROTECTION_STATUS: |
| 755 | result = new MtpProperty(property, MTP_TYPE_UINT16); |
| 756 | break; |
| 757 | case MTP_PROPERTY_STORAGE_ID: |
| 758 | case MTP_PROPERTY_PARENT_OBJECT: |
| 759 | result = new MtpProperty(property, MTP_TYPE_UINT32); |
| 760 | break; |
| 761 | case MTP_PROPERTY_OBJECT_SIZE: |
| 762 | result = new MtpProperty(property, MTP_TYPE_UINT64); |
| 763 | break; |
| 764 | case MTP_PROPERTY_PERSISTENT_UID: |
| 765 | result = new MtpProperty(property, MTP_TYPE_UINT128); |
| 766 | break; |
| 767 | case MTP_PROPERTY_OBJECT_FILE_NAME: |
| 768 | case MTP_PROPERTY_DATE_MODIFIED: |
| 769 | result = new MtpProperty(property, MTP_TYPE_STR); |
| 770 | break; |
| 771 | } |
| 772 | |
| 773 | return result; |
| 774 | } |
| 775 | |
| 776 | MtpProperty* MyMtpDatabase::getDevicePropertyDesc(MtpDeviceProperty property) { |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 777 | MtpProperty* result = NULL; |
| 778 | switch (property) { |
| 779 | case MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER: |
| 780 | case MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME: |
| 781 | // writeable string properties |
| 782 | result = new MtpProperty(property, MTP_TYPE_STR, true); |
| 783 | break; |
| 784 | } |
| 785 | |
| 786 | return result; |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 787 | } |
| 788 | |
Mike Lockwood | 2837eef | 2010-08-31 16:25:12 -0400 | [diff] [blame] | 789 | void MyMtpDatabase::sessionStarted() { |
| 790 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 791 | env->CallVoidMethod(mDatabase, method_sessionStarted); |
| 792 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 793 | } |
| 794 | |
| 795 | void MyMtpDatabase::sessionEnded() { |
| 796 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 797 | env->CallVoidMethod(mDatabase, method_sessionEnded); |
| 798 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 799 | } |
| 800 | |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 801 | #endif // HAVE_ANDROID_OS |
| 802 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 803 | // ---------------------------------------------------------------------------- |
| 804 | |
| 805 | static void |
| 806 | android_media_MtpDatabase_setup(JNIEnv *env, jobject thiz) |
| 807 | { |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 808 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 809 | LOGD("setup\n"); |
| 810 | MyMtpDatabase* database = new MyMtpDatabase(env, thiz); |
| 811 | env->SetIntField(thiz, field_context, (int)database); |
| 812 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 813 | #endif |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static void |
| 817 | android_media_MtpDatabase_finalize(JNIEnv *env, jobject thiz) |
| 818 | { |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 819 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 820 | LOGD("finalize\n"); |
| 821 | MyMtpDatabase* database = (MyMtpDatabase *)env->GetIntField(thiz, field_context); |
| 822 | database->cleanup(env); |
| 823 | delete database; |
| 824 | env->SetIntField(thiz, field_context, 0); |
| 825 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 826 | #endif |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | // ---------------------------------------------------------------------------- |
| 830 | |
| 831 | static JNINativeMethod gMethods[] = { |
| 832 | {"native_setup", "()V", (void *)android_media_MtpDatabase_setup}, |
| 833 | {"native_finalize", "()V", (void *)android_media_MtpDatabase_finalize}, |
| 834 | }; |
| 835 | |
| 836 | static const char* const kClassPathName = "android/media/MtpDatabase"; |
| 837 | |
| 838 | int register_android_media_MtpDatabase(JNIEnv *env) |
| 839 | { |
| 840 | jclass clazz; |
| 841 | |
| 842 | LOGD("register_android_media_MtpDatabase\n"); |
| 843 | |
| 844 | clazz = env->FindClass("android/media/MtpDatabase"); |
| 845 | if (clazz == NULL) { |
| 846 | LOGE("Can't find android/media/MtpDatabase"); |
| 847 | return -1; |
| 848 | } |
Mike Lockwood | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 849 | method_beginSendObject = env->GetMethodID(clazz, "beginSendObject", "(Ljava/lang/String;IIIJJ)I"); |
| 850 | if (method_beginSendObject == NULL) { |
| 851 | LOGE("Can't find beginSendObject"); |
| 852 | return -1; |
| 853 | } |
| 854 | method_endSendObject = env->GetMethodID(clazz, "endSendObject", "(Ljava/lang/String;IIZ)V"); |
| 855 | if (method_endSendObject == NULL) { |
| 856 | LOGE("Can't find endSendObject"); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 857 | return -1; |
| 858 | } |
| 859 | method_getObjectList = env->GetMethodID(clazz, "getObjectList", "(III)[I"); |
| 860 | if (method_getObjectList == NULL) { |
| 861 | LOGE("Can't find getObjectList"); |
| 862 | return -1; |
| 863 | } |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 864 | method_getNumObjects = env->GetMethodID(clazz, "getNumObjects", "(III)I"); |
| 865 | if (method_getNumObjects == NULL) { |
| 866 | LOGE("Can't find getNumObjects"); |
| 867 | return -1; |
| 868 | } |
Mike Lockwood | 4b322ce | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 869 | method_getSupportedPlaybackFormats = env->GetMethodID(clazz, "getSupportedPlaybackFormats", "()[I"); |
| 870 | if (method_getSupportedPlaybackFormats == NULL) { |
| 871 | LOGE("Can't find getSupportedPlaybackFormats"); |
| 872 | return -1; |
| 873 | } |
| 874 | method_getSupportedCaptureFormats = env->GetMethodID(clazz, "getSupportedCaptureFormats", "()[I"); |
| 875 | if (method_getSupportedCaptureFormats == NULL) { |
| 876 | LOGE("Can't find getSupportedCaptureFormats"); |
| 877 | return -1; |
| 878 | } |
| 879 | method_getSupportedObjectProperties = env->GetMethodID(clazz, "getSupportedObjectProperties", "(I)[I"); |
| 880 | if (method_getSupportedObjectProperties == NULL) { |
| 881 | LOGE("Can't find getSupportedObjectProperties"); |
| 882 | return -1; |
| 883 | } |
| 884 | method_getSupportedDeviceProperties = env->GetMethodID(clazz, "getSupportedDeviceProperties", "()[I"); |
| 885 | if (method_getSupportedDeviceProperties == NULL) { |
| 886 | LOGE("Can't find getSupportedDeviceProperties"); |
| 887 | return -1; |
| 888 | } |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 889 | method_getObjectProperty = env->GetMethodID(clazz, "getObjectProperty", "(II[J[C)I"); |
| 890 | if (method_getObjectProperty == NULL) { |
| 891 | LOGE("Can't find getObjectProperty"); |
| 892 | return -1; |
| 893 | } |
Mike Lockwood | 59e3f0d | 2010-09-02 14:57:30 -0400 | [diff] [blame^] | 894 | method_setObjectProperty = env->GetMethodID(clazz, "setObjectProperty", "(IIJLjava/lang/String;)I"); |
| 895 | if (method_setObjectProperty == NULL) { |
| 896 | LOGE("Can't find setObjectProperty"); |
| 897 | return -1; |
| 898 | } |
| 899 | method_getDeviceProperty = env->GetMethodID(clazz, "getDeviceProperty", "(I[J[C)I"); |
| 900 | if (method_getDeviceProperty == NULL) { |
| 901 | LOGE("Can't find getDeviceProperty"); |
| 902 | return -1; |
| 903 | } |
| 904 | method_setDeviceProperty = env->GetMethodID(clazz, "setDeviceProperty", "(IJLjava/lang/String;)I"); |
| 905 | if (method_setDeviceProperty == NULL) { |
| 906 | LOGE("Can't find setDeviceProperty"); |
| 907 | return -1; |
| 908 | } |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 909 | method_getObjectInfo = env->GetMethodID(clazz, "getObjectInfo", "(I[I[C[J)Z"); |
| 910 | if (method_getObjectInfo == NULL) { |
| 911 | LOGE("Can't find getObjectInfo"); |
| 912 | return -1; |
| 913 | } |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 914 | method_getObjectFilePath = env->GetMethodID(clazz, "getObjectFilePath", "(I[C[J)I"); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 915 | if (method_getObjectFilePath == NULL) { |
| 916 | LOGE("Can't find getObjectFilePath"); |
| 917 | return -1; |
| 918 | } |
Mike Lockwood | 59c777a | 2010-08-02 10:37:41 -0400 | [diff] [blame] | 919 | method_deleteFile = env->GetMethodID(clazz, "deleteFile", "(I)I"); |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 920 | if (method_deleteFile == NULL) { |
| 921 | LOGE("Can't find deleteFile"); |
| 922 | return -1; |
| 923 | } |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 924 | method_getObjectReferences = env->GetMethodID(clazz, "getObjectReferences", "(I)[I"); |
| 925 | if (method_getObjectReferences == NULL) { |
| 926 | LOGE("Can't find getObjectReferences"); |
| 927 | return -1; |
| 928 | } |
| 929 | method_setObjectReferences = env->GetMethodID(clazz, "setObjectReferences", "(I[I)I"); |
| 930 | if (method_setObjectReferences == NULL) { |
| 931 | LOGE("Can't find setObjectReferences"); |
| 932 | return -1; |
| 933 | } |
Mike Lockwood | 2837eef | 2010-08-31 16:25:12 -0400 | [diff] [blame] | 934 | method_sessionStarted = env->GetMethodID(clazz, "sessionStarted", "()V"); |
| 935 | if (method_sessionStarted == NULL) { |
| 936 | LOGE("Can't find sessionStarted"); |
| 937 | return -1; |
| 938 | } |
| 939 | method_sessionEnded = env->GetMethodID(clazz, "sessionEnded", "()V"); |
| 940 | if (method_sessionEnded == NULL) { |
| 941 | LOGE("Can't find sessionEnded"); |
| 942 | return -1; |
| 943 | } |
| 944 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 945 | field_context = env->GetFieldID(clazz, "mNativeContext", "I"); |
| 946 | if (field_context == NULL) { |
| 947 | LOGE("Can't find MtpDatabase.mNativeContext"); |
| 948 | return -1; |
| 949 | } |
| 950 | |
| 951 | return AndroidRuntime::registerNativeMethods(env, |
| 952 | "android/media/MtpDatabase", gMethods, NELEM(gMethods)); |
| 953 | } |