blob: 6b0bd0d9f8b74b7e435c958bf40f6a61a49543f3 [file] [log] [blame]
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001/*
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"
Ruben Brunk87eac992013-09-09 17:44:59 -070029#include "android_runtime/Log.h"
Mike Lockwoodd21eac92010-07-03 00:44:05 -040030
31#include "MtpDatabase.h"
32#include "MtpDataPacket.h"
Mike Lockwood9df53fae2011-04-21 17:05:55 -070033#include "MtpObjectInfo.h"
Mike Lockwood828d19d2010-08-10 15:20:35 -040034#include "MtpProperty.h"
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040035#include "MtpStringBuffer.h"
Mike Lockwoodd21eac92010-07-03 00:44:05 -040036#include "MtpUtils.h"
37#include "mtp.h"
38
Mike Lockwoodc89f2222011-04-24 18:40:17 -070039extern "C" {
Marco Nelissen3cd393c2014-01-10 10:39:27 -080040#include "libexif/exif-content.h"
41#include "libexif/exif-data.h"
42#include "libexif/exif-tag.h"
43#include "libexif/exif-utils.h"
Mike Lockwoodc89f2222011-04-24 18:40:17 -070044}
45
Mike Lockwoodd21eac92010-07-03 00:44:05 -040046using namespace android;
47
48// ----------------------------------------------------------------------------
49
Mike Lockwoodd815f792010-07-12 08:49:01 -040050static jmethodID method_beginSendObject;
51static jmethodID method_endSendObject;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040052static jmethodID method_getObjectList;
Mike Lockwood7a047c82010-08-02 10:52:20 -040053static jmethodID method_getNumObjects;
Mike Lockwood4b322ce2010-08-10 07:37:50 -040054static jmethodID method_getSupportedPlaybackFormats;
55static jmethodID method_getSupportedCaptureFormats;
56static jmethodID method_getSupportedObjectProperties;
57static jmethodID method_getSupportedDeviceProperties;
Mike Lockwood828d19d2010-08-10 15:20:35 -040058static jmethodID method_setObjectProperty;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040059static jmethodID method_getDeviceProperty;
60static jmethodID method_setDeviceProperty;
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -040061static jmethodID method_getObjectPropertyList;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040062static jmethodID method_getObjectInfo;
63static jmethodID method_getObjectFilePath;
64static jmethodID method_deleteFile;
Mike Lockwood9a2046f2010-08-03 15:30:09 -040065static jmethodID method_getObjectReferences;
66static jmethodID method_setObjectReferences;
Mike Lockwood2837eef2010-08-31 16:25:12 -040067static jmethodID method_sessionStarted;
68static jmethodID method_sessionEnded;
69
Mike Lockwoodd21eac92010-07-03 00:44:05 -040070static jfieldID field_context;
71
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -040072// MtpPropertyList fields
73static jfieldID field_mCount;
74static jfieldID field_mResult;
75static jfieldID field_mObjectHandles;
76static jfieldID field_mPropertyCodes;
77static jfieldID field_mDataTypes;
78static jfieldID field_mLongValues;
79static jfieldID field_mStringValues;
80
81
Mike Lockwoodd21eac92010-07-03 00:44:05 -040082MtpDatabase* getMtpDatabase(JNIEnv *env, jobject database) {
Ashok Bhate2e59322013-12-17 19:04:19 +000083 return (MtpDatabase *)env->GetLongField(database, field_context);
Mike Lockwoodd21eac92010-07-03 00:44:05 -040084}
85
86// ----------------------------------------------------------------------------
87
88class MyMtpDatabase : public MtpDatabase {
89private:
90 jobject mDatabase;
91 jintArray mIntBuffer;
92 jlongArray mLongBuffer;
93 jcharArray mStringBuffer;
94
95public:
96 MyMtpDatabase(JNIEnv *env, jobject client);
97 virtual ~MyMtpDatabase();
98 void cleanup(JNIEnv *env);
99
Mike Lockwoodd815f792010-07-12 08:49:01 -0400100 virtual MtpObjectHandle beginSendObject(const char* path,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400101 MtpObjectFormat format,
102 MtpObjectHandle parent,
103 MtpStorageID storage,
104 uint64_t size,
105 time_t modified);
106
Mike Lockwoodd815f792010-07-12 08:49:01 -0400107 virtual void endSendObject(const char* path,
108 MtpObjectHandle handle,
109 MtpObjectFormat format,
110 bool succeeded);
111
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400112 virtual MtpObjectHandleList* getObjectList(MtpStorageID storageID,
113 MtpObjectFormat format,
114 MtpObjectHandle parent);
115
Mike Lockwood7a047c82010-08-02 10:52:20 -0400116 virtual int getNumObjects(MtpStorageID storageID,
117 MtpObjectFormat format,
118 MtpObjectHandle parent);
119
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400120 // callee should delete[] the results from these
121 // results can be NULL
122 virtual MtpObjectFormatList* getSupportedPlaybackFormats();
123 virtual MtpObjectFormatList* getSupportedCaptureFormats();
124 virtual MtpObjectPropertyList* getSupportedObjectProperties(MtpObjectFormat format);
125 virtual MtpDevicePropertyList* getSupportedDeviceProperties();
126
Mike Lockwood828d19d2010-08-10 15:20:35 -0400127 virtual MtpResponseCode getObjectPropertyValue(MtpObjectHandle handle,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400128 MtpObjectProperty property,
129 MtpDataPacket& packet);
130
Mike Lockwood828d19d2010-08-10 15:20:35 -0400131 virtual MtpResponseCode setObjectPropertyValue(MtpObjectHandle handle,
132 MtpObjectProperty property,
133 MtpDataPacket& packet);
134
135 virtual MtpResponseCode getDevicePropertyValue(MtpDeviceProperty property,
136 MtpDataPacket& packet);
137
138 virtual MtpResponseCode setDevicePropertyValue(MtpDeviceProperty property,
139 MtpDataPacket& packet);
140
141 virtual MtpResponseCode resetDeviceProperty(MtpDeviceProperty property);
142
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400143 virtual MtpResponseCode getObjectPropertyList(MtpObjectHandle handle,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500144 uint32_t format, uint32_t property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400145 int groupCode, int depth,
146 MtpDataPacket& packet);
147
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400148 virtual MtpResponseCode getObjectInfo(MtpObjectHandle handle,
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700149 MtpObjectInfo& info);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400150
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700151 virtual void* getThumbnail(MtpObjectHandle handle, size_t& outThumbSize);
152
Mike Lockwood59c777a2010-08-02 10:37:41 -0400153 virtual MtpResponseCode getObjectFilePath(MtpObjectHandle handle,
Mike Lockwood365e03e2010-12-08 16:08:01 -0800154 MtpString& outFilePath,
155 int64_t& outFileLength,
156 MtpObjectFormat& outFormat);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400157 virtual MtpResponseCode deleteFile(MtpObjectHandle handle);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400158
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400159 bool getObjectPropertyInfo(MtpObjectProperty property, int& type);
160 bool getDevicePropertyInfo(MtpDeviceProperty property, int& type);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400161
162 virtual MtpObjectHandleList* getObjectReferences(MtpObjectHandle handle);
163
164 virtual MtpResponseCode setObjectReferences(MtpObjectHandle handle,
165 MtpObjectHandleList* references);
Mike Lockwood828d19d2010-08-10 15:20:35 -0400166
167 virtual MtpProperty* getObjectPropertyDesc(MtpObjectProperty property,
168 MtpObjectFormat format);
169
170 virtual MtpProperty* getDevicePropertyDesc(MtpDeviceProperty property);
Mike Lockwood2837eef2010-08-31 16:25:12 -0400171
172 virtual void sessionStarted();
173
174 virtual void sessionEnded();
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400175};
176
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400177// ----------------------------------------------------------------------------
178
179static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
180 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000181 ALOGE("An exception was thrown by callback '%s'.", methodName);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400182 LOGE_EX(env);
183 env->ExceptionClear();
184 }
185}
186
187// ----------------------------------------------------------------------------
188
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400189MyMtpDatabase::MyMtpDatabase(JNIEnv *env, jobject client)
190 : mDatabase(env->NewGlobalRef(client)),
191 mIntBuffer(NULL),
192 mLongBuffer(NULL),
193 mStringBuffer(NULL)
194{
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400195 // create buffers for out arguments
196 // we don't need to be thread-safe so this is OK
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700197 jintArray intArray = env->NewIntArray(3);
198 if (!intArray) {
199 return; // Already threw.
200 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400201 mIntBuffer = (jintArray)env->NewGlobalRef(intArray);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700202 jlongArray longArray = env->NewLongArray(2);
203 if (!longArray) {
204 return; // Already threw.
205 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400206 mLongBuffer = (jlongArray)env->NewGlobalRef(longArray);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700207 jcharArray charArray = env->NewCharArray(256);
208 if (!charArray) {
209 return; // Already threw.
210 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400211 mStringBuffer = (jcharArray)env->NewGlobalRef(charArray);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400212}
213
214void MyMtpDatabase::cleanup(JNIEnv *env) {
215 env->DeleteGlobalRef(mDatabase);
216 env->DeleteGlobalRef(mIntBuffer);
217 env->DeleteGlobalRef(mLongBuffer);
218 env->DeleteGlobalRef(mStringBuffer);
219}
220
221MyMtpDatabase::~MyMtpDatabase() {
222}
223
Mike Lockwoodd815f792010-07-12 08:49:01 -0400224MtpObjectHandle MyMtpDatabase::beginSendObject(const char* path,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400225 MtpObjectFormat format,
226 MtpObjectHandle parent,
227 MtpStorageID storage,
228 uint64_t size,
229 time_t modified) {
230 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood88394712010-09-27 10:01:00 -0400231 jstring pathStr = env->NewStringUTF(path);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400232 MtpObjectHandle result = env->CallIntMethod(mDatabase, method_beginSendObject,
Mike Lockwood88394712010-09-27 10:01:00 -0400233 pathStr, (jint)format, (jint)parent, (jint)storage,
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400234 (jlong)size, (jlong)modified);
235
Mike Lockwood88394712010-09-27 10:01:00 -0400236 if (pathStr)
237 env->DeleteLocalRef(pathStr);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400238 checkAndClearExceptionFromCallback(env, __FUNCTION__);
239 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400240}
241
Mike Lockwoodd815f792010-07-12 08:49:01 -0400242void MyMtpDatabase::endSendObject(const char* path, MtpObjectHandle handle,
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800243 MtpObjectFormat format, bool succeeded) {
Mike Lockwoodd815f792010-07-12 08:49:01 -0400244 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood88394712010-09-27 10:01:00 -0400245 jstring pathStr = env->NewStringUTF(path);
246 env->CallVoidMethod(mDatabase, method_endSendObject, pathStr,
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800247 (jint)handle, (jint)format, (jboolean)succeeded);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400248
Mike Lockwood88394712010-09-27 10:01:00 -0400249 if (pathStr)
250 env->DeleteLocalRef(pathStr);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400251 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400252}
253
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400254MtpObjectHandleList* MyMtpDatabase::getObjectList(MtpStorageID storageID,
255 MtpObjectFormat format,
256 MtpObjectHandle parent) {
257 JNIEnv* env = AndroidRuntime::getJNIEnv();
258 jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectList,
259 (jint)storageID, (jint)format, (jint)parent);
260 if (!array)
261 return NULL;
262 MtpObjectHandleList* list = new MtpObjectHandleList();
263 jint* handles = env->GetIntArrayElements(array, 0);
264 jsize length = env->GetArrayLength(array);
Mike Lockwood7a047c82010-08-02 10:52:20 -0400265 for (int i = 0; i < length; i++)
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400266 list->push(handles[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400267 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400268 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400269
270 checkAndClearExceptionFromCallback(env, __FUNCTION__);
271 return list;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400272}
273
Mike Lockwood7a047c82010-08-02 10:52:20 -0400274int MyMtpDatabase::getNumObjects(MtpStorageID storageID,
275 MtpObjectFormat format,
276 MtpObjectHandle parent) {
277 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400278 int result = env->CallIntMethod(mDatabase, method_getNumObjects,
Mike Lockwood7a047c82010-08-02 10:52:20 -0400279 (jint)storageID, (jint)format, (jint)parent);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400280
281 checkAndClearExceptionFromCallback(env, __FUNCTION__);
282 return result;
Mike Lockwood7a047c82010-08-02 10:52:20 -0400283}
284
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400285MtpObjectFormatList* MyMtpDatabase::getSupportedPlaybackFormats() {
286 JNIEnv* env = AndroidRuntime::getJNIEnv();
287 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
288 method_getSupportedPlaybackFormats);
289 if (!array)
290 return NULL;
291 MtpObjectFormatList* list = new MtpObjectFormatList();
292 jint* formats = env->GetIntArrayElements(array, 0);
293 jsize length = env->GetArrayLength(array);
294 for (int i = 0; i < length; i++)
295 list->push(formats[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400296 env->ReleaseIntArrayElements(array, formats, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400297 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400298
299 checkAndClearExceptionFromCallback(env, __FUNCTION__);
300 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400301}
302
303MtpObjectFormatList* MyMtpDatabase::getSupportedCaptureFormats() {
304 JNIEnv* env = AndroidRuntime::getJNIEnv();
305 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
306 method_getSupportedCaptureFormats);
307 if (!array)
308 return NULL;
309 MtpObjectFormatList* list = new MtpObjectFormatList();
310 jint* formats = env->GetIntArrayElements(array, 0);
311 jsize length = env->GetArrayLength(array);
312 for (int i = 0; i < length; i++)
313 list->push(formats[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400314 env->ReleaseIntArrayElements(array, formats, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400315 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400316
317 checkAndClearExceptionFromCallback(env, __FUNCTION__);
318 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400319}
320
321MtpObjectPropertyList* MyMtpDatabase::getSupportedObjectProperties(MtpObjectFormat format) {
322 JNIEnv* env = AndroidRuntime::getJNIEnv();
323 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
324 method_getSupportedObjectProperties, (jint)format);
325 if (!array)
326 return NULL;
327 MtpObjectPropertyList* list = new MtpObjectPropertyList();
328 jint* properties = env->GetIntArrayElements(array, 0);
329 jsize length = env->GetArrayLength(array);
330 for (int i = 0; i < length; i++)
331 list->push(properties[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400332 env->ReleaseIntArrayElements(array, properties, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400333 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400334
335 checkAndClearExceptionFromCallback(env, __FUNCTION__);
336 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400337}
338
339MtpDevicePropertyList* MyMtpDatabase::getSupportedDeviceProperties() {
340 JNIEnv* env = AndroidRuntime::getJNIEnv();
341 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
342 method_getSupportedDeviceProperties);
343 if (!array)
344 return NULL;
345 MtpDevicePropertyList* list = new MtpDevicePropertyList();
346 jint* properties = env->GetIntArrayElements(array, 0);
347 jsize length = env->GetArrayLength(array);
348 for (int i = 0; i < length; i++)
349 list->push(properties[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400350 env->ReleaseIntArrayElements(array, properties, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400351 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400352
353 checkAndClearExceptionFromCallback(env, __FUNCTION__);
354 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400355}
356
Mike Lockwood828d19d2010-08-10 15:20:35 -0400357MtpResponseCode MyMtpDatabase::getObjectPropertyValue(MtpObjectHandle handle,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400358 MtpObjectProperty property,
359 MtpDataPacket& packet) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400360 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400361 jobject list = env->CallObjectMethod(mDatabase, method_getObjectPropertyList,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500362 (jlong)handle, 0, (jlong)property, 0, 0);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400363 MtpResponseCode result = env->GetIntField(list, field_mResult);
364 int count = env->GetIntField(list, field_mCount);
365 if (result == MTP_RESPONSE_OK && count != 1)
366 result = MTP_RESPONSE_GENERAL_ERROR;
367
368 if (result == MTP_RESPONSE_OK) {
369 jintArray objectHandlesArray = (jintArray)env->GetObjectField(list, field_mObjectHandles);
370 jintArray propertyCodesArray = (jintArray)env->GetObjectField(list, field_mPropertyCodes);
371 jintArray dataTypesArray = (jintArray)env->GetObjectField(list, field_mDataTypes);
372 jlongArray longValuesArray = (jlongArray)env->GetObjectField(list, field_mLongValues);
373 jobjectArray stringValuesArray = (jobjectArray)env->GetObjectField(list, field_mStringValues);
374
375 jint* objectHandles = env->GetIntArrayElements(objectHandlesArray, 0);
376 jint* propertyCodes = env->GetIntArrayElements(propertyCodesArray, 0);
377 jint* dataTypes = env->GetIntArrayElements(dataTypesArray, 0);
378 jlong* longValues = (longValuesArray ? env->GetLongArrayElements(longValuesArray, 0) : NULL);
379
380 int type = dataTypes[0];
381 jlong longValue = (longValues ? longValues[0] : 0);
382
383 // special case date properties, which are strings to MTP
384 // but stored internally as a uint64
385 if (property == MTP_PROPERTY_DATE_MODIFIED || property == MTP_PROPERTY_DATE_ADDED) {
386 char date[20];
387 formatDateTime(longValue, date, sizeof(date));
388 packet.putString(date);
389 goto out;
390 }
391 // release date is stored internally as just the year
392 if (property == MTP_PROPERTY_ORIGINAL_RELEASE_DATE) {
393 char date[20];
394 snprintf(date, sizeof(date), "%04lld0101T000000", longValue);
395 packet.putString(date);
396 goto out;
397 }
398
399 switch (type) {
400 case MTP_TYPE_INT8:
401 packet.putInt8(longValue);
402 break;
403 case MTP_TYPE_UINT8:
404 packet.putUInt8(longValue);
405 break;
406 case MTP_TYPE_INT16:
407 packet.putInt16(longValue);
408 break;
409 case MTP_TYPE_UINT16:
410 packet.putUInt16(longValue);
411 break;
412 case MTP_TYPE_INT32:
413 packet.putInt32(longValue);
414 break;
415 case MTP_TYPE_UINT32:
416 packet.putUInt32(longValue);
417 break;
418 case MTP_TYPE_INT64:
419 packet.putInt64(longValue);
420 break;
421 case MTP_TYPE_UINT64:
422 packet.putUInt64(longValue);
423 break;
424 case MTP_TYPE_INT128:
425 packet.putInt128(longValue);
426 break;
427 case MTP_TYPE_UINT128:
428 packet.putInt128(longValue);
429 break;
430 case MTP_TYPE_STR:
431 {
432 jstring stringValue = (jstring)env->GetObjectArrayElement(stringValuesArray, 0);
433 if (stringValue) {
434 const char* str = env->GetStringUTFChars(stringValue, NULL);
James Dong39774722011-04-06 11:57:48 -0700435 if (str == NULL) {
436 return MTP_RESPONSE_GENERAL_ERROR;
437 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400438 packet.putString(str);
439 env->ReleaseStringUTFChars(stringValue, str);
440 } else {
441 packet.putEmptyString();
442 }
443 break;
444 }
445 default:
Steve Block3762c312012-01-06 19:20:56 +0000446 ALOGE("unsupported type in getObjectPropertyValue\n");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400447 result = MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
448 }
449out:
450 env->ReleaseIntArrayElements(objectHandlesArray, objectHandles, 0);
451 env->ReleaseIntArrayElements(propertyCodesArray, propertyCodes, 0);
452 env->ReleaseIntArrayElements(dataTypesArray, dataTypes, 0);
453 if (longValues)
454 env->ReleaseLongArrayElements(longValuesArray, longValues, 0);
455
456 env->DeleteLocalRef(objectHandlesArray);
457 env->DeleteLocalRef(propertyCodesArray);
458 env->DeleteLocalRef(dataTypesArray);
459 if (longValuesArray)
460 env->DeleteLocalRef(longValuesArray);
461 if (stringValuesArray)
462 env->DeleteLocalRef(stringValuesArray);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400463 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400464
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400465 env->DeleteLocalRef(list);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400466 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400467 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400468}
469
Mike Lockwood828d19d2010-08-10 15:20:35 -0400470MtpResponseCode MyMtpDatabase::setObjectPropertyValue(MtpObjectHandle handle,
471 MtpObjectProperty property,
472 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400473 int type;
474
475 if (!getObjectPropertyInfo(property, type))
476 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
477
478 JNIEnv* env = AndroidRuntime::getJNIEnv();
479 jlong longValue = 0;
480 jstring stringValue = NULL;
481
482 switch (type) {
483 case MTP_TYPE_INT8:
484 longValue = packet.getInt8();
485 break;
486 case MTP_TYPE_UINT8:
487 longValue = packet.getUInt8();
488 break;
489 case MTP_TYPE_INT16:
490 longValue = packet.getInt16();
491 break;
492 case MTP_TYPE_UINT16:
493 longValue = packet.getUInt16();
494 break;
495 case MTP_TYPE_INT32:
496 longValue = packet.getInt32();
497 break;
498 case MTP_TYPE_UINT32:
499 longValue = packet.getUInt32();
500 break;
501 case MTP_TYPE_INT64:
502 longValue = packet.getInt64();
503 break;
504 case MTP_TYPE_UINT64:
505 longValue = packet.getUInt64();
506 break;
507 case MTP_TYPE_STR:
508 {
509 MtpStringBuffer buffer;
510 packet.getString(buffer);
511 stringValue = env->NewStringUTF((const char *)buffer);
512 break;
513 }
514 default:
Steve Block3762c312012-01-06 19:20:56 +0000515 ALOGE("unsupported type in getObjectPropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400516 return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
517 }
518
519 jint result = env->CallIntMethod(mDatabase, method_setObjectProperty,
520 (jint)handle, (jint)property, longValue, stringValue);
Mike Lockwood88394712010-09-27 10:01:00 -0400521 if (stringValue)
522 env->DeleteLocalRef(stringValue);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400523
524 checkAndClearExceptionFromCallback(env, __FUNCTION__);
525 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400526}
527
528MtpResponseCode MyMtpDatabase::getDevicePropertyValue(MtpDeviceProperty property,
529 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400530 int type;
531
532 if (!getDevicePropertyInfo(property, type))
533 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
534
535 JNIEnv* env = AndroidRuntime::getJNIEnv();
536 jint result = env->CallIntMethod(mDatabase, method_getDeviceProperty,
537 (jint)property, mLongBuffer, mStringBuffer);
538 if (result != MTP_RESPONSE_OK) {
539 checkAndClearExceptionFromCallback(env, __FUNCTION__);
540 return result;
541 }
542
543 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
544 jlong longValue = longValues[0];
545 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
546
547 switch (type) {
548 case MTP_TYPE_INT8:
549 packet.putInt8(longValue);
550 break;
551 case MTP_TYPE_UINT8:
552 packet.putUInt8(longValue);
553 break;
554 case MTP_TYPE_INT16:
555 packet.putInt16(longValue);
556 break;
557 case MTP_TYPE_UINT16:
558 packet.putUInt16(longValue);
559 break;
560 case MTP_TYPE_INT32:
561 packet.putInt32(longValue);
562 break;
563 case MTP_TYPE_UINT32:
564 packet.putUInt32(longValue);
565 break;
566 case MTP_TYPE_INT64:
567 packet.putInt64(longValue);
568 break;
569 case MTP_TYPE_UINT64:
570 packet.putUInt64(longValue);
571 break;
572 case MTP_TYPE_INT128:
573 packet.putInt128(longValue);
574 break;
575 case MTP_TYPE_UINT128:
576 packet.putInt128(longValue);
577 break;
578 case MTP_TYPE_STR:
579 {
580 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
581 packet.putString(str);
582 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
583 break;
584 }
585 default:
Steve Block3762c312012-01-06 19:20:56 +0000586 ALOGE("unsupported type in getDevicePropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400587 return MTP_RESPONSE_INVALID_DEVICE_PROP_FORMAT;
588 }
589
590 checkAndClearExceptionFromCallback(env, __FUNCTION__);
591 return MTP_RESPONSE_OK;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400592}
593
594MtpResponseCode MyMtpDatabase::setDevicePropertyValue(MtpDeviceProperty property,
595 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400596 int type;
597
598 if (!getDevicePropertyInfo(property, type))
599 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
600
601 JNIEnv* env = AndroidRuntime::getJNIEnv();
602 jlong longValue = 0;
603 jstring stringValue = NULL;
604
605 switch (type) {
606 case MTP_TYPE_INT8:
607 longValue = packet.getInt8();
608 break;
609 case MTP_TYPE_UINT8:
610 longValue = packet.getUInt8();
611 break;
612 case MTP_TYPE_INT16:
613 longValue = packet.getInt16();
614 break;
615 case MTP_TYPE_UINT16:
616 longValue = packet.getUInt16();
617 break;
618 case MTP_TYPE_INT32:
619 longValue = packet.getInt32();
620 break;
621 case MTP_TYPE_UINT32:
622 longValue = packet.getUInt32();
623 break;
624 case MTP_TYPE_INT64:
625 longValue = packet.getInt64();
626 break;
627 case MTP_TYPE_UINT64:
628 longValue = packet.getUInt64();
629 break;
630 case MTP_TYPE_STR:
631 {
632 MtpStringBuffer buffer;
633 packet.getString(buffer);
634 stringValue = env->NewStringUTF((const char *)buffer);
635 break;
636 }
637 default:
Steve Block3762c312012-01-06 19:20:56 +0000638 ALOGE("unsupported type in setDevicePropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400639 return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
640 }
641
642 jint result = env->CallIntMethod(mDatabase, method_setDeviceProperty,
643 (jint)property, longValue, stringValue);
Mike Lockwood88394712010-09-27 10:01:00 -0400644 if (stringValue)
645 env->DeleteLocalRef(stringValue);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400646
647 checkAndClearExceptionFromCallback(env, __FUNCTION__);
648 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400649}
650
651MtpResponseCode MyMtpDatabase::resetDeviceProperty(MtpDeviceProperty property) {
652 return -1;
653}
654
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400655MtpResponseCode MyMtpDatabase::getObjectPropertyList(MtpObjectHandle handle,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500656 uint32_t format, uint32_t property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400657 int groupCode, int depth,
658 MtpDataPacket& packet) {
659 JNIEnv* env = AndroidRuntime::getJNIEnv();
660 jobject list = env->CallObjectMethod(mDatabase, method_getObjectPropertyList,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500661 (jlong)handle, (jint)format, (jlong)property, (jint)groupCode, (jint)depth);
662 checkAndClearExceptionFromCallback(env, __FUNCTION__);
663 if (!list)
664 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400665 int count = env->GetIntField(list, field_mCount);
666 MtpResponseCode result = env->GetIntField(list, field_mResult);
667
668 packet.putUInt32(count);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400669 if (count > 0) {
670 jintArray objectHandlesArray = (jintArray)env->GetObjectField(list, field_mObjectHandles);
671 jintArray propertyCodesArray = (jintArray)env->GetObjectField(list, field_mPropertyCodes);
672 jintArray dataTypesArray = (jintArray)env->GetObjectField(list, field_mDataTypes);
673 jlongArray longValuesArray = (jlongArray)env->GetObjectField(list, field_mLongValues);
674 jobjectArray stringValuesArray = (jobjectArray)env->GetObjectField(list, field_mStringValues);
675
676 jint* objectHandles = env->GetIntArrayElements(objectHandlesArray, 0);
677 jint* propertyCodes = env->GetIntArrayElements(propertyCodesArray, 0);
678 jint* dataTypes = env->GetIntArrayElements(dataTypesArray, 0);
679 jlong* longValues = (longValuesArray ? env->GetLongArrayElements(longValuesArray, 0) : NULL);
680
681 for (int i = 0; i < count; i++) {
682 packet.putUInt32(objectHandles[i]);
683 packet.putUInt16(propertyCodes[i]);
684 int type = dataTypes[i];
685 packet.putUInt16(type);
686
687 switch (type) {
688 case MTP_TYPE_INT8:
689 packet.putInt8(longValues[i]);
690 break;
691 case MTP_TYPE_UINT8:
692 packet.putUInt8(longValues[i]);
693 break;
694 case MTP_TYPE_INT16:
695 packet.putInt16(longValues[i]);
696 break;
697 case MTP_TYPE_UINT16:
698 packet.putUInt16(longValues[i]);
699 break;
700 case MTP_TYPE_INT32:
701 packet.putInt32(longValues[i]);
702 break;
703 case MTP_TYPE_UINT32:
704 packet.putUInt32(longValues[i]);
705 break;
706 case MTP_TYPE_INT64:
707 packet.putInt64(longValues[i]);
708 break;
709 case MTP_TYPE_UINT64:
710 packet.putUInt64(longValues[i]);
711 break;
712 case MTP_TYPE_INT128:
713 packet.putInt128(longValues[i]);
714 break;
715 case MTP_TYPE_UINT128:
716 packet.putUInt128(longValues[i]);
717 break;
718 case MTP_TYPE_STR: {
719 jstring value = (jstring)env->GetObjectArrayElement(stringValuesArray, i);
Mike Lockwood2711e492010-12-11 11:24:37 -0800720 const char *valueStr = (value ? env->GetStringUTFChars(value, NULL) : NULL);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400721 if (valueStr) {
722 packet.putString(valueStr);
723 env->ReleaseStringUTFChars(value, valueStr);
724 } else {
725 packet.putEmptyString();
726 }
727 env->DeleteLocalRef(value);
728 break;
729 }
730 default:
Steve Block3762c312012-01-06 19:20:56 +0000731 ALOGE("bad or unsupported data type in MyMtpDatabase::getObjectPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400732 break;
733 }
734 }
735
736 env->ReleaseIntArrayElements(objectHandlesArray, objectHandles, 0);
737 env->ReleaseIntArrayElements(propertyCodesArray, propertyCodes, 0);
738 env->ReleaseIntArrayElements(dataTypesArray, dataTypes, 0);
739 if (longValues)
740 env->ReleaseLongArrayElements(longValuesArray, longValues, 0);
741
742 env->DeleteLocalRef(objectHandlesArray);
743 env->DeleteLocalRef(propertyCodesArray);
744 env->DeleteLocalRef(dataTypesArray);
745 if (longValuesArray)
746 env->DeleteLocalRef(longValuesArray);
747 if (stringValuesArray)
748 env->DeleteLocalRef(stringValuesArray);
749 }
750
751 env->DeleteLocalRef(list);
752 checkAndClearExceptionFromCallback(env, __FUNCTION__);
753 return result;
754}
755
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800756static void foreachentry(ExifEntry *entry, void *user) {
757 char buf[1024];
758 ALOGI("entry %x, format %d, size %d: %s",
759 entry->tag, entry->format, entry->size, exif_entry_get_value(entry, buf, sizeof(buf)));
760}
761
762static void foreachcontent(ExifContent *content, void *user) {
763 ALOGI("content %d", exif_content_get_ifd(content));
764 exif_content_foreach_entry(content, foreachentry, user);
765}
766
767static long getLongFromExifEntry(ExifEntry *e) {
768 ExifByteOrder o = exif_data_get_byte_order(e->parent->parent);
769 return exif_get_long(e->data, o);
770}
771
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400772MtpResponseCode MyMtpDatabase::getObjectInfo(MtpObjectHandle handle,
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700773 MtpObjectInfo& info) {
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700774 char date[20];
775 MtpString path;
776 int64_t length;
777 MtpObjectFormat format;
778
779 MtpResponseCode result = getObjectFilePath(handle, path, length, format);
780 if (result != MTP_RESPONSE_OK) {
781 return result;
782 }
783 info.mCompressedSize = (length > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)length);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400784
785 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700786 if (!env->CallBooleanMethod(mDatabase, method_getObjectInfo,
787 (jint)handle, mIntBuffer, mStringBuffer, mLongBuffer)) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400788 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700789 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400790
791 jint* intValues = env->GetIntArrayElements(mIntBuffer, 0);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700792 info.mStorageID = intValues[0];
793 info.mFormat = intValues[1];
794 info.mParent = intValues[2];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400795 env->ReleaseIntArrayElements(mIntBuffer, intValues, 0);
796
797 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700798 info.mDateCreated = longValues[0];
799 info.mDateModified = longValues[1];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400800 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
801
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700802// info.mAssociationType = (format == MTP_FORMAT_ASSOCIATION ?
Mike Lockwood828d19d2010-08-10 15:20:35 -0400803// MTP_ASSOCIATION_TYPE_GENERIC_FOLDER :
804// MTP_ASSOCIATION_TYPE_UNDEFINED);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700805 info.mAssociationType = MTP_ASSOCIATION_TYPE_UNDEFINED;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400806
807 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700808 MtpString temp(str);
809 info.mName = strdup((const char *)temp);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400810 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
811
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700812 // read EXIF data for thumbnail information
813 if (info.mFormat == MTP_FORMAT_EXIF_JPEG || info.mFormat == MTP_FORMAT_JFIF) {
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800814
815 ExifData *exifdata = exif_data_new_from_file(path);
816 if (exifdata) {
817 //exif_data_foreach_content(exifdata, foreachcontent, NULL);
818
819 // XXX get this from exif, or parse jpeg header instead?
820 ExifEntry *w = exif_content_get_entry(
821 exifdata->ifd[EXIF_IFD_EXIF], EXIF_TAG_PIXEL_X_DIMENSION);
822 ExifEntry *h = exif_content_get_entry(
823 exifdata->ifd[EXIF_IFD_EXIF], EXIF_TAG_PIXEL_Y_DIMENSION);
824 info.mThumbCompressedSize = exifdata->data ? exifdata->size : 0;
825 info.mThumbFormat = MTP_FORMAT_EXIF_JPEG;
Marco Nelissen0937eed2014-01-22 15:10:57 -0800826 info.mImagePixWidth = w ? getLongFromExifEntry(w) : 0;
827 info.mImagePixHeight = h ? getLongFromExifEntry(h) : 0;
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800828 exif_data_unref(exifdata);
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700829 }
830 }
831
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400832 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400833 return MTP_RESPONSE_OK;
834}
835
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700836void* MyMtpDatabase::getThumbnail(MtpObjectHandle handle, size_t& outThumbSize) {
837 MtpString path;
838 int64_t length;
839 MtpObjectFormat format;
840 void* result = NULL;
841 outThumbSize = 0;
842
843 if (getObjectFilePath(handle, path, length, format) == MTP_RESPONSE_OK
844 && (format == MTP_FORMAT_EXIF_JPEG || format == MTP_FORMAT_JFIF)) {
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800845
846 ExifData *exifdata = exif_data_new_from_file(path);
847 if (exifdata) {
848 if (exifdata->data) {
849 result = malloc(exifdata->size);
850 if (result) {
851 memcpy(result, exifdata->data, exifdata->size);
852 }
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700853 }
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800854 exif_data_unref(exifdata);
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700855 }
856 }
857
858 return result;
859}
860
Mike Lockwood59c777a2010-08-02 10:37:41 -0400861MtpResponseCode MyMtpDatabase::getObjectFilePath(MtpObjectHandle handle,
Mike Lockwood365e03e2010-12-08 16:08:01 -0800862 MtpString& outFilePath,
863 int64_t& outFileLength,
864 MtpObjectFormat& outFormat) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400865 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood59c777a2010-08-02 10:37:41 -0400866 jint result = env->CallIntMethod(mDatabase, method_getObjectFilePath,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400867 (jint)handle, mStringBuffer, mLongBuffer);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400868 if (result != MTP_RESPONSE_OK) {
869 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400870 return result;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400871 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400872
873 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
Mike Lockwood365e03e2010-12-08 16:08:01 -0800874 outFilePath.setTo(str, strlen16(str));
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400875 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
876
877 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
Mike Lockwood365e03e2010-12-08 16:08:01 -0800878 outFileLength = longValues[0];
879 outFormat = longValues[1];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400880 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700881
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400882 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400883 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400884}
885
Mike Lockwood59c777a2010-08-02 10:37:41 -0400886MtpResponseCode MyMtpDatabase::deleteFile(MtpObjectHandle handle) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400887 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400888 MtpResponseCode result = env->CallIntMethod(mDatabase, method_deleteFile, (jint)handle);
889
890 checkAndClearExceptionFromCallback(env, __FUNCTION__);
891 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400892}
893
894struct PropertyTableEntry {
895 MtpObjectProperty property;
896 int type;
897};
898
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400899static const PropertyTableEntry kObjectPropertyTable[] = {
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400900 { MTP_PROPERTY_STORAGE_ID, MTP_TYPE_UINT32 },
901 { MTP_PROPERTY_OBJECT_FORMAT, MTP_TYPE_UINT16 },
902 { MTP_PROPERTY_PROTECTION_STATUS, MTP_TYPE_UINT16 },
903 { MTP_PROPERTY_OBJECT_SIZE, MTP_TYPE_UINT64 },
904 { MTP_PROPERTY_OBJECT_FILE_NAME, MTP_TYPE_STR },
905 { MTP_PROPERTY_DATE_MODIFIED, MTP_TYPE_STR },
906 { MTP_PROPERTY_PARENT_OBJECT, MTP_TYPE_UINT32 },
907 { MTP_PROPERTY_PERSISTENT_UID, MTP_TYPE_UINT128 },
908 { MTP_PROPERTY_NAME, MTP_TYPE_STR },
Mike Lockwoodae078f72010-09-26 12:35:51 -0400909 { MTP_PROPERTY_DISPLAY_NAME, MTP_TYPE_STR },
910 { MTP_PROPERTY_DATE_ADDED, MTP_TYPE_STR },
911 { MTP_PROPERTY_ARTIST, MTP_TYPE_STR },
912 { MTP_PROPERTY_ALBUM_NAME, MTP_TYPE_STR },
913 { MTP_PROPERTY_ALBUM_ARTIST, MTP_TYPE_STR },
914 { MTP_PROPERTY_TRACK, MTP_TYPE_UINT16 },
915 { MTP_PROPERTY_ORIGINAL_RELEASE_DATE, MTP_TYPE_STR },
916 { MTP_PROPERTY_GENRE, MTP_TYPE_STR },
917 { MTP_PROPERTY_COMPOSER, MTP_TYPE_STR },
918 { MTP_PROPERTY_DURATION, MTP_TYPE_UINT32 },
919 { MTP_PROPERTY_DESCRIPTION, MTP_TYPE_STR },
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400920};
921
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400922static const PropertyTableEntry kDevicePropertyTable[] = {
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800923 { MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER, MTP_TYPE_STR },
924 { MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME, MTP_TYPE_STR },
925 { MTP_DEVICE_PROPERTY_IMAGE_SIZE, MTP_TYPE_STR },
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400926};
927
928bool MyMtpDatabase::getObjectPropertyInfo(MtpObjectProperty property, int& type) {
929 int count = sizeof(kObjectPropertyTable) / sizeof(kObjectPropertyTable[0]);
930 const PropertyTableEntry* entry = kObjectPropertyTable;
931 for (int i = 0; i < count; i++, entry++) {
932 if (entry->property == property) {
933 type = entry->type;
934 return true;
935 }
936 }
937 return false;
938}
939
940bool MyMtpDatabase::getDevicePropertyInfo(MtpDeviceProperty property, int& type) {
941 int count = sizeof(kDevicePropertyTable) / sizeof(kDevicePropertyTable[0]);
942 const PropertyTableEntry* entry = kDevicePropertyTable;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400943 for (int i = 0; i < count; i++, entry++) {
944 if (entry->property == property) {
945 type = entry->type;
946 return true;
947 }
948 }
949 return false;
950}
951
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400952MtpObjectHandleList* MyMtpDatabase::getObjectReferences(MtpObjectHandle handle) {
953 JNIEnv* env = AndroidRuntime::getJNIEnv();
954 jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectReferences,
955 (jint)handle);
956 if (!array)
957 return NULL;
958 MtpObjectHandleList* list = new MtpObjectHandleList();
959 jint* handles = env->GetIntArrayElements(array, 0);
960 jsize length = env->GetArrayLength(array);
961 for (int i = 0; i < length; i++)
962 list->push(handles[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400963 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400964 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400965
966 checkAndClearExceptionFromCallback(env, __FUNCTION__);
967 return list;
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400968}
969
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400970MtpResponseCode MyMtpDatabase::setObjectReferences(MtpObjectHandle handle,
971 MtpObjectHandleList* references) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400972 JNIEnv* env = AndroidRuntime::getJNIEnv();
973 int count = references->size();
974 jintArray array = env->NewIntArray(count);
975 if (!array) {
Steve Block3762c312012-01-06 19:20:56 +0000976 ALOGE("out of memory in setObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400977 return false;
978 }
979 jint* handles = env->GetIntArrayElements(array, 0);
980 for (int i = 0; i < count; i++)
981 handles[i] = (*references)[i];
982 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400983 MtpResponseCode result = env->CallIntMethod(mDatabase, method_setObjectReferences,
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400984 (jint)handle, array);
Mike Lockwood88394712010-09-27 10:01:00 -0400985 env->DeleteLocalRef(array);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400986
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400987 checkAndClearExceptionFromCallback(env, __FUNCTION__);
988 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400989}
990
Mike Lockwood828d19d2010-08-10 15:20:35 -0400991MtpProperty* MyMtpDatabase::getObjectPropertyDesc(MtpObjectProperty property,
992 MtpObjectFormat format) {
993 MtpProperty* result = NULL;
994 switch (property) {
995 case MTP_PROPERTY_OBJECT_FORMAT:
Mike Lockwood9b5e9c42010-12-07 18:53:50 -0800996 // use format as default value
997 result = new MtpProperty(property, MTP_TYPE_UINT16, false, format);
998 break;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400999 case MTP_PROPERTY_PROTECTION_STATUS:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001000 case MTP_PROPERTY_TRACK:
Mike Lockwood828d19d2010-08-10 15:20:35 -04001001 result = new MtpProperty(property, MTP_TYPE_UINT16);
1002 break;
1003 case MTP_PROPERTY_STORAGE_ID:
1004 case MTP_PROPERTY_PARENT_OBJECT:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001005 case MTP_PROPERTY_DURATION:
Mike Lockwood828d19d2010-08-10 15:20:35 -04001006 result = new MtpProperty(property, MTP_TYPE_UINT32);
1007 break;
1008 case MTP_PROPERTY_OBJECT_SIZE:
1009 result = new MtpProperty(property, MTP_TYPE_UINT64);
1010 break;
1011 case MTP_PROPERTY_PERSISTENT_UID:
1012 result = new MtpProperty(property, MTP_TYPE_UINT128);
1013 break;
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -04001014 case MTP_PROPERTY_NAME:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001015 case MTP_PROPERTY_DISPLAY_NAME:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001016 case MTP_PROPERTY_ARTIST:
1017 case MTP_PROPERTY_ALBUM_NAME:
1018 case MTP_PROPERTY_ALBUM_ARTIST:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001019 case MTP_PROPERTY_GENRE:
1020 case MTP_PROPERTY_COMPOSER:
1021 case MTP_PROPERTY_DESCRIPTION:
Mike Lockwood828d19d2010-08-10 15:20:35 -04001022 result = new MtpProperty(property, MTP_TYPE_STR);
1023 break;
Mike Lockwood5b19af02010-11-23 18:38:55 -05001024 case MTP_PROPERTY_DATE_MODIFIED:
1025 case MTP_PROPERTY_DATE_ADDED:
1026 case MTP_PROPERTY_ORIGINAL_RELEASE_DATE:
1027 result = new MtpProperty(property, MTP_TYPE_STR);
1028 result->setFormDateTime();
1029 break;
Mike Lockwood5ebac832010-10-12 11:33:47 -04001030 case MTP_PROPERTY_OBJECT_FILE_NAME:
Mike Lockwood6a6a3af2010-10-12 14:19:51 -04001031 // We allow renaming files and folders
1032 result = new MtpProperty(property, MTP_TYPE_STR, true);
Mike Lockwood5ebac832010-10-12 11:33:47 -04001033 break;
Mike Lockwood828d19d2010-08-10 15:20:35 -04001034 }
1035
1036 return result;
1037}
1038
1039MtpProperty* MyMtpDatabase::getDevicePropertyDesc(MtpDeviceProperty property) {
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001040 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001041 MtpProperty* result = NULL;
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001042 bool writable = false;
1043
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001044 switch (property) {
1045 case MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
1046 case MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001047 writable = true;
1048 // fall through
1049 case MTP_DEVICE_PROPERTY_IMAGE_SIZE:
1050 result = new MtpProperty(property, MTP_TYPE_STR, writable);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001051
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001052 // get current value
Mike Lockwooda2a21282010-09-25 21:21:05 -04001053 jint ret = env->CallIntMethod(mDatabase, method_getDeviceProperty,
1054 (jint)property, mLongBuffer, mStringBuffer);
1055 if (ret == MTP_RESPONSE_OK) {
1056 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
1057 result->setCurrentValue(str);
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001058 // for read-only properties it is safe to assume current value is default value
1059 if (!writable)
1060 result->setDefaultValue(str);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001061 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
1062 } else {
Steve Block3762c312012-01-06 19:20:56 +00001063 ALOGE("unable to read device property, response: %04X", ret);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001064 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001065 break;
1066 }
1067
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001068 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001069 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -04001070}
1071
Mike Lockwood2837eef2010-08-31 16:25:12 -04001072void MyMtpDatabase::sessionStarted() {
1073 JNIEnv* env = AndroidRuntime::getJNIEnv();
1074 env->CallVoidMethod(mDatabase, method_sessionStarted);
1075 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1076}
1077
1078void MyMtpDatabase::sessionEnded() {
1079 JNIEnv* env = AndroidRuntime::getJNIEnv();
1080 env->CallVoidMethod(mDatabase, method_sessionEnded);
1081 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1082}
1083
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001084// ----------------------------------------------------------------------------
1085
1086static void
Mike Lockwood0cd01362010-12-30 11:54:33 -05001087android_mtp_MtpDatabase_setup(JNIEnv *env, jobject thiz)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001088{
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001089 MyMtpDatabase* database = new MyMtpDatabase(env, thiz);
Ashok Bhate2e59322013-12-17 19:04:19 +00001090 env->SetLongField(thiz, field_context, (jlong)database);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001091 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1092}
1093
1094static void
Mike Lockwood0cd01362010-12-30 11:54:33 -05001095android_mtp_MtpDatabase_finalize(JNIEnv *env, jobject thiz)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001096{
Ashok Bhate2e59322013-12-17 19:04:19 +00001097 MyMtpDatabase* database = (MyMtpDatabase *)env->GetLongField(thiz, field_context);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001098 database->cleanup(env);
1099 delete database;
Ashok Bhate2e59322013-12-17 19:04:19 +00001100 env->SetLongField(thiz, field_context, 0);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001101 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1102}
1103
Mike Lockwood31599912010-11-15 13:43:30 -05001104static jstring
Mike Lockwood0cd01362010-12-30 11:54:33 -05001105android_mtp_MtpPropertyGroup_format_date_time(JNIEnv *env, jobject thiz, jlong seconds)
Mike Lockwood31599912010-11-15 13:43:30 -05001106{
Mike Lockwood31599912010-11-15 13:43:30 -05001107 char date[20];
1108 formatDateTime(seconds, date, sizeof(date));
1109 return env->NewStringUTF(date);
Mike Lockwood31599912010-11-15 13:43:30 -05001110}
1111
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001112// ----------------------------------------------------------------------------
1113
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001114static JNINativeMethod gMtpDatabaseMethods[] = {
Mike Lockwood0cd01362010-12-30 11:54:33 -05001115 {"native_setup", "()V", (void *)android_mtp_MtpDatabase_setup},
1116 {"native_finalize", "()V", (void *)android_mtp_MtpDatabase_finalize},
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001117};
1118
1119static JNINativeMethod gMtpPropertyGroupMethods[] = {
Mike Lockwood31599912010-11-15 13:43:30 -05001120 {"format_date_time", "(J)Ljava/lang/String;",
Mike Lockwood0cd01362010-12-30 11:54:33 -05001121 (void *)android_mtp_MtpPropertyGroup_format_date_time},
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001122};
1123
Mike Lockwood0cd01362010-12-30 11:54:33 -05001124static const char* const kClassPathName = "android/mtp/MtpDatabase";
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001125
Mike Lockwood0cd01362010-12-30 11:54:33 -05001126int register_android_mtp_MtpDatabase(JNIEnv *env)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001127{
1128 jclass clazz;
1129
Mike Lockwood0cd01362010-12-30 11:54:33 -05001130 clazz = env->FindClass("android/mtp/MtpDatabase");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001131 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001132 ALOGE("Can't find android/mtp/MtpDatabase");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001133 return -1;
1134 }
Mike Lockwoodd815f792010-07-12 08:49:01 -04001135 method_beginSendObject = env->GetMethodID(clazz, "beginSendObject", "(Ljava/lang/String;IIIJJ)I");
1136 if (method_beginSendObject == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001137 ALOGE("Can't find beginSendObject");
Mike Lockwoodd815f792010-07-12 08:49:01 -04001138 return -1;
1139 }
Mike Lockwood7a0bd172011-01-18 11:06:19 -08001140 method_endSendObject = env->GetMethodID(clazz, "endSendObject", "(Ljava/lang/String;IIZ)V");
Mike Lockwoodd815f792010-07-12 08:49:01 -04001141 if (method_endSendObject == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001142 ALOGE("Can't find endSendObject");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001143 return -1;
1144 }
1145 method_getObjectList = env->GetMethodID(clazz, "getObjectList", "(III)[I");
1146 if (method_getObjectList == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001147 ALOGE("Can't find getObjectList");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001148 return -1;
1149 }
Mike Lockwood7a047c82010-08-02 10:52:20 -04001150 method_getNumObjects = env->GetMethodID(clazz, "getNumObjects", "(III)I");
1151 if (method_getNumObjects == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001152 ALOGE("Can't find getNumObjects");
Mike Lockwood7a047c82010-08-02 10:52:20 -04001153 return -1;
1154 }
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001155 method_getSupportedPlaybackFormats = env->GetMethodID(clazz, "getSupportedPlaybackFormats", "()[I");
1156 if (method_getSupportedPlaybackFormats == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001157 ALOGE("Can't find getSupportedPlaybackFormats");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001158 return -1;
1159 }
1160 method_getSupportedCaptureFormats = env->GetMethodID(clazz, "getSupportedCaptureFormats", "()[I");
1161 if (method_getSupportedCaptureFormats == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001162 ALOGE("Can't find getSupportedCaptureFormats");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001163 return -1;
1164 }
1165 method_getSupportedObjectProperties = env->GetMethodID(clazz, "getSupportedObjectProperties", "(I)[I");
1166 if (method_getSupportedObjectProperties == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001167 ALOGE("Can't find getSupportedObjectProperties");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001168 return -1;
1169 }
1170 method_getSupportedDeviceProperties = env->GetMethodID(clazz, "getSupportedDeviceProperties", "()[I");
1171 if (method_getSupportedDeviceProperties == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001172 ALOGE("Can't find getSupportedDeviceProperties");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001173 return -1;
1174 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001175 method_setObjectProperty = env->GetMethodID(clazz, "setObjectProperty", "(IIJLjava/lang/String;)I");
1176 if (method_setObjectProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001177 ALOGE("Can't find setObjectProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001178 return -1;
1179 }
1180 method_getDeviceProperty = env->GetMethodID(clazz, "getDeviceProperty", "(I[J[C)I");
1181 if (method_getDeviceProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001182 ALOGE("Can't find getDeviceProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001183 return -1;
1184 }
1185 method_setDeviceProperty = env->GetMethodID(clazz, "setDeviceProperty", "(IJLjava/lang/String;)I");
1186 if (method_setDeviceProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001187 ALOGE("Can't find setDeviceProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001188 return -1;
1189 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001190 method_getObjectPropertyList = env->GetMethodID(clazz, "getObjectPropertyList",
Mike Lockwood0cd01362010-12-30 11:54:33 -05001191 "(JIJII)Landroid/mtp/MtpPropertyList;");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001192 if (method_getObjectPropertyList == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001193 ALOGE("Can't find getObjectPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001194 return -1;
1195 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001196 method_getObjectInfo = env->GetMethodID(clazz, "getObjectInfo", "(I[I[C[J)Z");
1197 if (method_getObjectInfo == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001198 ALOGE("Can't find getObjectInfo");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001199 return -1;
1200 }
Mike Lockwood59c777a2010-08-02 10:37:41 -04001201 method_getObjectFilePath = env->GetMethodID(clazz, "getObjectFilePath", "(I[C[J)I");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001202 if (method_getObjectFilePath == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001203 ALOGE("Can't find getObjectFilePath");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001204 return -1;
1205 }
Mike Lockwood59c777a2010-08-02 10:37:41 -04001206 method_deleteFile = env->GetMethodID(clazz, "deleteFile", "(I)I");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001207 if (method_deleteFile == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001208 ALOGE("Can't find deleteFile");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001209 return -1;
1210 }
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001211 method_getObjectReferences = env->GetMethodID(clazz, "getObjectReferences", "(I)[I");
1212 if (method_getObjectReferences == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001213 ALOGE("Can't find getObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001214 return -1;
1215 }
1216 method_setObjectReferences = env->GetMethodID(clazz, "setObjectReferences", "(I[I)I");
1217 if (method_setObjectReferences == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001218 ALOGE("Can't find setObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001219 return -1;
1220 }
Mike Lockwood2837eef2010-08-31 16:25:12 -04001221 method_sessionStarted = env->GetMethodID(clazz, "sessionStarted", "()V");
1222 if (method_sessionStarted == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001223 ALOGE("Can't find sessionStarted");
Mike Lockwood2837eef2010-08-31 16:25:12 -04001224 return -1;
1225 }
1226 method_sessionEnded = env->GetMethodID(clazz, "sessionEnded", "()V");
1227 if (method_sessionEnded == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001228 ALOGE("Can't find sessionEnded");
Mike Lockwood2837eef2010-08-31 16:25:12 -04001229 return -1;
1230 }
1231
Ashok Bhate2e59322013-12-17 19:04:19 +00001232 field_context = env->GetFieldID(clazz, "mNativeContext", "J");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001233 if (field_context == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001234 ALOGE("Can't find MtpDatabase.mNativeContext");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001235 return -1;
1236 }
1237
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001238 // now set up fields for MtpPropertyList class
Mike Lockwood0cd01362010-12-30 11:54:33 -05001239 clazz = env->FindClass("android/mtp/MtpPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001240 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001241 ALOGE("Can't find android/mtp/MtpPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001242 return -1;
1243 }
1244 field_mCount = env->GetFieldID(clazz, "mCount", "I");
1245 if (field_mCount == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001246 ALOGE("Can't find MtpPropertyList.mCount");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001247 return -1;
1248 }
1249 field_mResult = env->GetFieldID(clazz, "mResult", "I");
1250 if (field_mResult == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001251 ALOGE("Can't find MtpPropertyList.mResult");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001252 return -1;
1253 }
1254 field_mObjectHandles = env->GetFieldID(clazz, "mObjectHandles", "[I");
1255 if (field_mObjectHandles == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001256 ALOGE("Can't find MtpPropertyList.mObjectHandles");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001257 return -1;
1258 }
1259 field_mPropertyCodes = env->GetFieldID(clazz, "mPropertyCodes", "[I");
1260 if (field_mPropertyCodes == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001261 ALOGE("Can't find MtpPropertyList.mPropertyCodes");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001262 return -1;
1263 }
1264 field_mDataTypes = env->GetFieldID(clazz, "mDataTypes", "[I");
1265 if (field_mDataTypes == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001266 ALOGE("Can't find MtpPropertyList.mDataTypes");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001267 return -1;
1268 }
1269 field_mLongValues = env->GetFieldID(clazz, "mLongValues", "[J");
1270 if (field_mLongValues == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001271 ALOGE("Can't find MtpPropertyList.mLongValues");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001272 return -1;
1273 }
1274 field_mStringValues = env->GetFieldID(clazz, "mStringValues", "[Ljava/lang/String;");
1275 if (field_mStringValues == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001276 ALOGE("Can't find MtpPropertyList.mStringValues");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001277 return -1;
1278 }
1279
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001280 if (AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -05001281 "android/mtp/MtpDatabase", gMtpDatabaseMethods, NELEM(gMtpDatabaseMethods)))
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001282 return -1;
1283
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001284 return AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -05001285 "android/mtp/MtpPropertyGroup", gMtpPropertyGroupMethods, NELEM(gMtpPropertyGroupMethods));
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001286}