blob: 72ce3cce03252cb0fa4e30b2f8bf677fb7ae455a [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" {
40#include "jhead.h"
41}
42
Mike Lockwoodd21eac92010-07-03 00:44:05 -040043using namespace android;
44
45// ----------------------------------------------------------------------------
46
Mike Lockwoodd815f792010-07-12 08:49:01 -040047static jmethodID method_beginSendObject;
48static jmethodID method_endSendObject;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040049static jmethodID method_getObjectList;
Mike Lockwood7a047c82010-08-02 10:52:20 -040050static jmethodID method_getNumObjects;
Mike Lockwood4b322ce2010-08-10 07:37:50 -040051static jmethodID method_getSupportedPlaybackFormats;
52static jmethodID method_getSupportedCaptureFormats;
53static jmethodID method_getSupportedObjectProperties;
54static jmethodID method_getSupportedDeviceProperties;
Mike Lockwood828d19d2010-08-10 15:20:35 -040055static jmethodID method_setObjectProperty;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040056static jmethodID method_getDeviceProperty;
57static jmethodID method_setDeviceProperty;
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -040058static jmethodID method_getObjectPropertyList;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040059static jmethodID method_getObjectInfo;
60static jmethodID method_getObjectFilePath;
61static jmethodID method_deleteFile;
Mike Lockwood9a2046f2010-08-03 15:30:09 -040062static jmethodID method_getObjectReferences;
63static jmethodID method_setObjectReferences;
Mike Lockwood2837eef2010-08-31 16:25:12 -040064static jmethodID method_sessionStarted;
65static jmethodID method_sessionEnded;
66
Mike Lockwoodd21eac92010-07-03 00:44:05 -040067static jfieldID field_context;
68
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -040069// MtpPropertyList fields
70static jfieldID field_mCount;
71static jfieldID field_mResult;
72static jfieldID field_mObjectHandles;
73static jfieldID field_mPropertyCodes;
74static jfieldID field_mDataTypes;
75static jfieldID field_mLongValues;
76static jfieldID field_mStringValues;
77
78
Mike Lockwoodd21eac92010-07-03 00:44:05 -040079MtpDatabase* getMtpDatabase(JNIEnv *env, jobject database) {
Ashok Bhate2e59322013-12-17 19:04:19 +000080 return (MtpDatabase *)env->GetLongField(database, field_context);
Mike Lockwoodd21eac92010-07-03 00:44:05 -040081}
82
83// ----------------------------------------------------------------------------
84
85class MyMtpDatabase : public MtpDatabase {
86private:
87 jobject mDatabase;
88 jintArray mIntBuffer;
89 jlongArray mLongBuffer;
90 jcharArray mStringBuffer;
91
92public:
93 MyMtpDatabase(JNIEnv *env, jobject client);
94 virtual ~MyMtpDatabase();
95 void cleanup(JNIEnv *env);
96
Mike Lockwoodd815f792010-07-12 08:49:01 -040097 virtual MtpObjectHandle beginSendObject(const char* path,
Mike Lockwoodd21eac92010-07-03 00:44:05 -040098 MtpObjectFormat format,
99 MtpObjectHandle parent,
100 MtpStorageID storage,
101 uint64_t size,
102 time_t modified);
103
Mike Lockwoodd815f792010-07-12 08:49:01 -0400104 virtual void endSendObject(const char* path,
105 MtpObjectHandle handle,
106 MtpObjectFormat format,
107 bool succeeded);
108
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400109 virtual MtpObjectHandleList* getObjectList(MtpStorageID storageID,
110 MtpObjectFormat format,
111 MtpObjectHandle parent);
112
Mike Lockwood7a047c82010-08-02 10:52:20 -0400113 virtual int getNumObjects(MtpStorageID storageID,
114 MtpObjectFormat format,
115 MtpObjectHandle parent);
116
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400117 // callee should delete[] the results from these
118 // results can be NULL
119 virtual MtpObjectFormatList* getSupportedPlaybackFormats();
120 virtual MtpObjectFormatList* getSupportedCaptureFormats();
121 virtual MtpObjectPropertyList* getSupportedObjectProperties(MtpObjectFormat format);
122 virtual MtpDevicePropertyList* getSupportedDeviceProperties();
123
Mike Lockwood828d19d2010-08-10 15:20:35 -0400124 virtual MtpResponseCode getObjectPropertyValue(MtpObjectHandle handle,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400125 MtpObjectProperty property,
126 MtpDataPacket& packet);
127
Mike Lockwood828d19d2010-08-10 15:20:35 -0400128 virtual MtpResponseCode setObjectPropertyValue(MtpObjectHandle handle,
129 MtpObjectProperty property,
130 MtpDataPacket& packet);
131
132 virtual MtpResponseCode getDevicePropertyValue(MtpDeviceProperty property,
133 MtpDataPacket& packet);
134
135 virtual MtpResponseCode setDevicePropertyValue(MtpDeviceProperty property,
136 MtpDataPacket& packet);
137
138 virtual MtpResponseCode resetDeviceProperty(MtpDeviceProperty property);
139
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400140 virtual MtpResponseCode getObjectPropertyList(MtpObjectHandle handle,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500141 uint32_t format, uint32_t property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400142 int groupCode, int depth,
143 MtpDataPacket& packet);
144
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400145 virtual MtpResponseCode getObjectInfo(MtpObjectHandle handle,
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700146 MtpObjectInfo& info);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400147
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700148 virtual void* getThumbnail(MtpObjectHandle handle, size_t& outThumbSize);
149
Mike Lockwood59c777a2010-08-02 10:37:41 -0400150 virtual MtpResponseCode getObjectFilePath(MtpObjectHandle handle,
Mike Lockwood365e03e2010-12-08 16:08:01 -0800151 MtpString& outFilePath,
152 int64_t& outFileLength,
153 MtpObjectFormat& outFormat);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400154 virtual MtpResponseCode deleteFile(MtpObjectHandle handle);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400155
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400156 bool getObjectPropertyInfo(MtpObjectProperty property, int& type);
157 bool getDevicePropertyInfo(MtpDeviceProperty property, int& type);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400158
159 virtual MtpObjectHandleList* getObjectReferences(MtpObjectHandle handle);
160
161 virtual MtpResponseCode setObjectReferences(MtpObjectHandle handle,
162 MtpObjectHandleList* references);
Mike Lockwood828d19d2010-08-10 15:20:35 -0400163
164 virtual MtpProperty* getObjectPropertyDesc(MtpObjectProperty property,
165 MtpObjectFormat format);
166
167 virtual MtpProperty* getDevicePropertyDesc(MtpDeviceProperty property);
Mike Lockwood2837eef2010-08-31 16:25:12 -0400168
169 virtual void sessionStarted();
170
171 virtual void sessionEnded();
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400172};
173
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400174// ----------------------------------------------------------------------------
175
176static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
177 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000178 ALOGE("An exception was thrown by callback '%s'.", methodName);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400179 LOGE_EX(env);
180 env->ExceptionClear();
181 }
182}
183
184// ----------------------------------------------------------------------------
185
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400186MyMtpDatabase::MyMtpDatabase(JNIEnv *env, jobject client)
187 : mDatabase(env->NewGlobalRef(client)),
188 mIntBuffer(NULL),
189 mLongBuffer(NULL),
190 mStringBuffer(NULL)
191{
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400192 // create buffers for out arguments
193 // we don't need to be thread-safe so this is OK
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700194 jintArray intArray = env->NewIntArray(3);
195 if (!intArray) {
196 return; // Already threw.
197 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400198 mIntBuffer = (jintArray)env->NewGlobalRef(intArray);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700199 jlongArray longArray = env->NewLongArray(2);
200 if (!longArray) {
201 return; // Already threw.
202 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400203 mLongBuffer = (jlongArray)env->NewGlobalRef(longArray);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700204 jcharArray charArray = env->NewCharArray(256);
205 if (!charArray) {
206 return; // Already threw.
207 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400208 mStringBuffer = (jcharArray)env->NewGlobalRef(charArray);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400209}
210
211void MyMtpDatabase::cleanup(JNIEnv *env) {
212 env->DeleteGlobalRef(mDatabase);
213 env->DeleteGlobalRef(mIntBuffer);
214 env->DeleteGlobalRef(mLongBuffer);
215 env->DeleteGlobalRef(mStringBuffer);
216}
217
218MyMtpDatabase::~MyMtpDatabase() {
219}
220
Mike Lockwoodd815f792010-07-12 08:49:01 -0400221MtpObjectHandle MyMtpDatabase::beginSendObject(const char* path,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400222 MtpObjectFormat format,
223 MtpObjectHandle parent,
224 MtpStorageID storage,
225 uint64_t size,
226 time_t modified) {
227 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood88394712010-09-27 10:01:00 -0400228 jstring pathStr = env->NewStringUTF(path);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400229 MtpObjectHandle result = env->CallIntMethod(mDatabase, method_beginSendObject,
Mike Lockwood88394712010-09-27 10:01:00 -0400230 pathStr, (jint)format, (jint)parent, (jint)storage,
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400231 (jlong)size, (jlong)modified);
232
Mike Lockwood88394712010-09-27 10:01:00 -0400233 if (pathStr)
234 env->DeleteLocalRef(pathStr);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400235 checkAndClearExceptionFromCallback(env, __FUNCTION__);
236 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400237}
238
Mike Lockwoodd815f792010-07-12 08:49:01 -0400239void MyMtpDatabase::endSendObject(const char* path, MtpObjectHandle handle,
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800240 MtpObjectFormat format, bool succeeded) {
Mike Lockwoodd815f792010-07-12 08:49:01 -0400241 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood88394712010-09-27 10:01:00 -0400242 jstring pathStr = env->NewStringUTF(path);
243 env->CallVoidMethod(mDatabase, method_endSendObject, pathStr,
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800244 (jint)handle, (jint)format, (jboolean)succeeded);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400245
Mike Lockwood88394712010-09-27 10:01:00 -0400246 if (pathStr)
247 env->DeleteLocalRef(pathStr);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400248 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400249}
250
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400251MtpObjectHandleList* MyMtpDatabase::getObjectList(MtpStorageID storageID,
252 MtpObjectFormat format,
253 MtpObjectHandle parent) {
254 JNIEnv* env = AndroidRuntime::getJNIEnv();
255 jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectList,
256 (jint)storageID, (jint)format, (jint)parent);
257 if (!array)
258 return NULL;
259 MtpObjectHandleList* list = new MtpObjectHandleList();
260 jint* handles = env->GetIntArrayElements(array, 0);
261 jsize length = env->GetArrayLength(array);
Mike Lockwood7a047c82010-08-02 10:52:20 -0400262 for (int i = 0; i < length; i++)
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400263 list->push(handles[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400264 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400265 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400266
267 checkAndClearExceptionFromCallback(env, __FUNCTION__);
268 return list;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400269}
270
Mike Lockwood7a047c82010-08-02 10:52:20 -0400271int MyMtpDatabase::getNumObjects(MtpStorageID storageID,
272 MtpObjectFormat format,
273 MtpObjectHandle parent) {
274 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400275 int result = env->CallIntMethod(mDatabase, method_getNumObjects,
Mike Lockwood7a047c82010-08-02 10:52:20 -0400276 (jint)storageID, (jint)format, (jint)parent);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400277
278 checkAndClearExceptionFromCallback(env, __FUNCTION__);
279 return result;
Mike Lockwood7a047c82010-08-02 10:52:20 -0400280}
281
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400282MtpObjectFormatList* MyMtpDatabase::getSupportedPlaybackFormats() {
283 JNIEnv* env = AndroidRuntime::getJNIEnv();
284 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
285 method_getSupportedPlaybackFormats);
286 if (!array)
287 return NULL;
288 MtpObjectFormatList* list = new MtpObjectFormatList();
289 jint* formats = env->GetIntArrayElements(array, 0);
290 jsize length = env->GetArrayLength(array);
291 for (int i = 0; i < length; i++)
292 list->push(formats[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400293 env->ReleaseIntArrayElements(array, formats, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400294 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400295
296 checkAndClearExceptionFromCallback(env, __FUNCTION__);
297 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400298}
299
300MtpObjectFormatList* MyMtpDatabase::getSupportedCaptureFormats() {
301 JNIEnv* env = AndroidRuntime::getJNIEnv();
302 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
303 method_getSupportedCaptureFormats);
304 if (!array)
305 return NULL;
306 MtpObjectFormatList* list = new MtpObjectFormatList();
307 jint* formats = env->GetIntArrayElements(array, 0);
308 jsize length = env->GetArrayLength(array);
309 for (int i = 0; i < length; i++)
310 list->push(formats[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400311 env->ReleaseIntArrayElements(array, formats, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400312 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400313
314 checkAndClearExceptionFromCallback(env, __FUNCTION__);
315 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400316}
317
318MtpObjectPropertyList* MyMtpDatabase::getSupportedObjectProperties(MtpObjectFormat format) {
319 JNIEnv* env = AndroidRuntime::getJNIEnv();
320 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
321 method_getSupportedObjectProperties, (jint)format);
322 if (!array)
323 return NULL;
324 MtpObjectPropertyList* list = new MtpObjectPropertyList();
325 jint* properties = env->GetIntArrayElements(array, 0);
326 jsize length = env->GetArrayLength(array);
327 for (int i = 0; i < length; i++)
328 list->push(properties[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400329 env->ReleaseIntArrayElements(array, properties, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400330 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400331
332 checkAndClearExceptionFromCallback(env, __FUNCTION__);
333 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400334}
335
336MtpDevicePropertyList* MyMtpDatabase::getSupportedDeviceProperties() {
337 JNIEnv* env = AndroidRuntime::getJNIEnv();
338 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
339 method_getSupportedDeviceProperties);
340 if (!array)
341 return NULL;
342 MtpDevicePropertyList* list = new MtpDevicePropertyList();
343 jint* properties = env->GetIntArrayElements(array, 0);
344 jsize length = env->GetArrayLength(array);
345 for (int i = 0; i < length; i++)
346 list->push(properties[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400347 env->ReleaseIntArrayElements(array, properties, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400348 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400349
350 checkAndClearExceptionFromCallback(env, __FUNCTION__);
351 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400352}
353
Mike Lockwood828d19d2010-08-10 15:20:35 -0400354MtpResponseCode MyMtpDatabase::getObjectPropertyValue(MtpObjectHandle handle,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400355 MtpObjectProperty property,
356 MtpDataPacket& packet) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400357 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400358 jobject list = env->CallObjectMethod(mDatabase, method_getObjectPropertyList,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500359 (jlong)handle, 0, (jlong)property, 0, 0);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400360 MtpResponseCode result = env->GetIntField(list, field_mResult);
361 int count = env->GetIntField(list, field_mCount);
362 if (result == MTP_RESPONSE_OK && count != 1)
363 result = MTP_RESPONSE_GENERAL_ERROR;
364
365 if (result == MTP_RESPONSE_OK) {
366 jintArray objectHandlesArray = (jintArray)env->GetObjectField(list, field_mObjectHandles);
367 jintArray propertyCodesArray = (jintArray)env->GetObjectField(list, field_mPropertyCodes);
368 jintArray dataTypesArray = (jintArray)env->GetObjectField(list, field_mDataTypes);
369 jlongArray longValuesArray = (jlongArray)env->GetObjectField(list, field_mLongValues);
370 jobjectArray stringValuesArray = (jobjectArray)env->GetObjectField(list, field_mStringValues);
371
372 jint* objectHandles = env->GetIntArrayElements(objectHandlesArray, 0);
373 jint* propertyCodes = env->GetIntArrayElements(propertyCodesArray, 0);
374 jint* dataTypes = env->GetIntArrayElements(dataTypesArray, 0);
375 jlong* longValues = (longValuesArray ? env->GetLongArrayElements(longValuesArray, 0) : NULL);
376
377 int type = dataTypes[0];
378 jlong longValue = (longValues ? longValues[0] : 0);
379
380 // special case date properties, which are strings to MTP
381 // but stored internally as a uint64
382 if (property == MTP_PROPERTY_DATE_MODIFIED || property == MTP_PROPERTY_DATE_ADDED) {
383 char date[20];
384 formatDateTime(longValue, date, sizeof(date));
385 packet.putString(date);
386 goto out;
387 }
388 // release date is stored internally as just the year
389 if (property == MTP_PROPERTY_ORIGINAL_RELEASE_DATE) {
390 char date[20];
391 snprintf(date, sizeof(date), "%04lld0101T000000", longValue);
392 packet.putString(date);
393 goto out;
394 }
395
396 switch (type) {
397 case MTP_TYPE_INT8:
398 packet.putInt8(longValue);
399 break;
400 case MTP_TYPE_UINT8:
401 packet.putUInt8(longValue);
402 break;
403 case MTP_TYPE_INT16:
404 packet.putInt16(longValue);
405 break;
406 case MTP_TYPE_UINT16:
407 packet.putUInt16(longValue);
408 break;
409 case MTP_TYPE_INT32:
410 packet.putInt32(longValue);
411 break;
412 case MTP_TYPE_UINT32:
413 packet.putUInt32(longValue);
414 break;
415 case MTP_TYPE_INT64:
416 packet.putInt64(longValue);
417 break;
418 case MTP_TYPE_UINT64:
419 packet.putUInt64(longValue);
420 break;
421 case MTP_TYPE_INT128:
422 packet.putInt128(longValue);
423 break;
424 case MTP_TYPE_UINT128:
425 packet.putInt128(longValue);
426 break;
427 case MTP_TYPE_STR:
428 {
429 jstring stringValue = (jstring)env->GetObjectArrayElement(stringValuesArray, 0);
430 if (stringValue) {
431 const char* str = env->GetStringUTFChars(stringValue, NULL);
James Dong39774722011-04-06 11:57:48 -0700432 if (str == NULL) {
433 return MTP_RESPONSE_GENERAL_ERROR;
434 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400435 packet.putString(str);
436 env->ReleaseStringUTFChars(stringValue, str);
437 } else {
438 packet.putEmptyString();
439 }
440 break;
441 }
442 default:
Steve Block3762c312012-01-06 19:20:56 +0000443 ALOGE("unsupported type in getObjectPropertyValue\n");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400444 result = MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
445 }
446out:
447 env->ReleaseIntArrayElements(objectHandlesArray, objectHandles, 0);
448 env->ReleaseIntArrayElements(propertyCodesArray, propertyCodes, 0);
449 env->ReleaseIntArrayElements(dataTypesArray, dataTypes, 0);
450 if (longValues)
451 env->ReleaseLongArrayElements(longValuesArray, longValues, 0);
452
453 env->DeleteLocalRef(objectHandlesArray);
454 env->DeleteLocalRef(propertyCodesArray);
455 env->DeleteLocalRef(dataTypesArray);
456 if (longValuesArray)
457 env->DeleteLocalRef(longValuesArray);
458 if (stringValuesArray)
459 env->DeleteLocalRef(stringValuesArray);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400460 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400461
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400462 env->DeleteLocalRef(list);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400463 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400464 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400465}
466
Mike Lockwood828d19d2010-08-10 15:20:35 -0400467MtpResponseCode MyMtpDatabase::setObjectPropertyValue(MtpObjectHandle handle,
468 MtpObjectProperty property,
469 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400470 int type;
471
472 if (!getObjectPropertyInfo(property, type))
473 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
474
475 JNIEnv* env = AndroidRuntime::getJNIEnv();
476 jlong longValue = 0;
477 jstring stringValue = NULL;
478
479 switch (type) {
480 case MTP_TYPE_INT8:
481 longValue = packet.getInt8();
482 break;
483 case MTP_TYPE_UINT8:
484 longValue = packet.getUInt8();
485 break;
486 case MTP_TYPE_INT16:
487 longValue = packet.getInt16();
488 break;
489 case MTP_TYPE_UINT16:
490 longValue = packet.getUInt16();
491 break;
492 case MTP_TYPE_INT32:
493 longValue = packet.getInt32();
494 break;
495 case MTP_TYPE_UINT32:
496 longValue = packet.getUInt32();
497 break;
498 case MTP_TYPE_INT64:
499 longValue = packet.getInt64();
500 break;
501 case MTP_TYPE_UINT64:
502 longValue = packet.getUInt64();
503 break;
504 case MTP_TYPE_STR:
505 {
506 MtpStringBuffer buffer;
507 packet.getString(buffer);
508 stringValue = env->NewStringUTF((const char *)buffer);
509 break;
510 }
511 default:
Steve Block3762c312012-01-06 19:20:56 +0000512 ALOGE("unsupported type in getObjectPropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400513 return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
514 }
515
516 jint result = env->CallIntMethod(mDatabase, method_setObjectProperty,
517 (jint)handle, (jint)property, longValue, stringValue);
Mike Lockwood88394712010-09-27 10:01:00 -0400518 if (stringValue)
519 env->DeleteLocalRef(stringValue);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400520
521 checkAndClearExceptionFromCallback(env, __FUNCTION__);
522 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400523}
524
525MtpResponseCode MyMtpDatabase::getDevicePropertyValue(MtpDeviceProperty property,
526 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400527 int type;
528
529 if (!getDevicePropertyInfo(property, type))
530 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
531
532 JNIEnv* env = AndroidRuntime::getJNIEnv();
533 jint result = env->CallIntMethod(mDatabase, method_getDeviceProperty,
534 (jint)property, mLongBuffer, mStringBuffer);
535 if (result != MTP_RESPONSE_OK) {
536 checkAndClearExceptionFromCallback(env, __FUNCTION__);
537 return result;
538 }
539
540 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
541 jlong longValue = longValues[0];
542 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
543
544 switch (type) {
545 case MTP_TYPE_INT8:
546 packet.putInt8(longValue);
547 break;
548 case MTP_TYPE_UINT8:
549 packet.putUInt8(longValue);
550 break;
551 case MTP_TYPE_INT16:
552 packet.putInt16(longValue);
553 break;
554 case MTP_TYPE_UINT16:
555 packet.putUInt16(longValue);
556 break;
557 case MTP_TYPE_INT32:
558 packet.putInt32(longValue);
559 break;
560 case MTP_TYPE_UINT32:
561 packet.putUInt32(longValue);
562 break;
563 case MTP_TYPE_INT64:
564 packet.putInt64(longValue);
565 break;
566 case MTP_TYPE_UINT64:
567 packet.putUInt64(longValue);
568 break;
569 case MTP_TYPE_INT128:
570 packet.putInt128(longValue);
571 break;
572 case MTP_TYPE_UINT128:
573 packet.putInt128(longValue);
574 break;
575 case MTP_TYPE_STR:
576 {
577 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
578 packet.putString(str);
579 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
580 break;
581 }
582 default:
Steve Block3762c312012-01-06 19:20:56 +0000583 ALOGE("unsupported type in getDevicePropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400584 return MTP_RESPONSE_INVALID_DEVICE_PROP_FORMAT;
585 }
586
587 checkAndClearExceptionFromCallback(env, __FUNCTION__);
588 return MTP_RESPONSE_OK;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400589}
590
591MtpResponseCode MyMtpDatabase::setDevicePropertyValue(MtpDeviceProperty property,
592 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400593 int type;
594
595 if (!getDevicePropertyInfo(property, type))
596 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
597
598 JNIEnv* env = AndroidRuntime::getJNIEnv();
599 jlong longValue = 0;
600 jstring stringValue = NULL;
601
602 switch (type) {
603 case MTP_TYPE_INT8:
604 longValue = packet.getInt8();
605 break;
606 case MTP_TYPE_UINT8:
607 longValue = packet.getUInt8();
608 break;
609 case MTP_TYPE_INT16:
610 longValue = packet.getInt16();
611 break;
612 case MTP_TYPE_UINT16:
613 longValue = packet.getUInt16();
614 break;
615 case MTP_TYPE_INT32:
616 longValue = packet.getInt32();
617 break;
618 case MTP_TYPE_UINT32:
619 longValue = packet.getUInt32();
620 break;
621 case MTP_TYPE_INT64:
622 longValue = packet.getInt64();
623 break;
624 case MTP_TYPE_UINT64:
625 longValue = packet.getUInt64();
626 break;
627 case MTP_TYPE_STR:
628 {
629 MtpStringBuffer buffer;
630 packet.getString(buffer);
631 stringValue = env->NewStringUTF((const char *)buffer);
632 break;
633 }
634 default:
Steve Block3762c312012-01-06 19:20:56 +0000635 ALOGE("unsupported type in setDevicePropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400636 return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
637 }
638
639 jint result = env->CallIntMethod(mDatabase, method_setDeviceProperty,
640 (jint)property, longValue, stringValue);
Mike Lockwood88394712010-09-27 10:01:00 -0400641 if (stringValue)
642 env->DeleteLocalRef(stringValue);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400643
644 checkAndClearExceptionFromCallback(env, __FUNCTION__);
645 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400646}
647
648MtpResponseCode MyMtpDatabase::resetDeviceProperty(MtpDeviceProperty property) {
649 return -1;
650}
651
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400652MtpResponseCode MyMtpDatabase::getObjectPropertyList(MtpObjectHandle handle,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500653 uint32_t format, uint32_t property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400654 int groupCode, int depth,
655 MtpDataPacket& packet) {
656 JNIEnv* env = AndroidRuntime::getJNIEnv();
657 jobject list = env->CallObjectMethod(mDatabase, method_getObjectPropertyList,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500658 (jlong)handle, (jint)format, (jlong)property, (jint)groupCode, (jint)depth);
659 checkAndClearExceptionFromCallback(env, __FUNCTION__);
660 if (!list)
661 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400662 int count = env->GetIntField(list, field_mCount);
663 MtpResponseCode result = env->GetIntField(list, field_mResult);
664
665 packet.putUInt32(count);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400666 if (count > 0) {
667 jintArray objectHandlesArray = (jintArray)env->GetObjectField(list, field_mObjectHandles);
668 jintArray propertyCodesArray = (jintArray)env->GetObjectField(list, field_mPropertyCodes);
669 jintArray dataTypesArray = (jintArray)env->GetObjectField(list, field_mDataTypes);
670 jlongArray longValuesArray = (jlongArray)env->GetObjectField(list, field_mLongValues);
671 jobjectArray stringValuesArray = (jobjectArray)env->GetObjectField(list, field_mStringValues);
672
673 jint* objectHandles = env->GetIntArrayElements(objectHandlesArray, 0);
674 jint* propertyCodes = env->GetIntArrayElements(propertyCodesArray, 0);
675 jint* dataTypes = env->GetIntArrayElements(dataTypesArray, 0);
676 jlong* longValues = (longValuesArray ? env->GetLongArrayElements(longValuesArray, 0) : NULL);
677
678 for (int i = 0; i < count; i++) {
679 packet.putUInt32(objectHandles[i]);
680 packet.putUInt16(propertyCodes[i]);
681 int type = dataTypes[i];
682 packet.putUInt16(type);
683
684 switch (type) {
685 case MTP_TYPE_INT8:
686 packet.putInt8(longValues[i]);
687 break;
688 case MTP_TYPE_UINT8:
689 packet.putUInt8(longValues[i]);
690 break;
691 case MTP_TYPE_INT16:
692 packet.putInt16(longValues[i]);
693 break;
694 case MTP_TYPE_UINT16:
695 packet.putUInt16(longValues[i]);
696 break;
697 case MTP_TYPE_INT32:
698 packet.putInt32(longValues[i]);
699 break;
700 case MTP_TYPE_UINT32:
701 packet.putUInt32(longValues[i]);
702 break;
703 case MTP_TYPE_INT64:
704 packet.putInt64(longValues[i]);
705 break;
706 case MTP_TYPE_UINT64:
707 packet.putUInt64(longValues[i]);
708 break;
709 case MTP_TYPE_INT128:
710 packet.putInt128(longValues[i]);
711 break;
712 case MTP_TYPE_UINT128:
713 packet.putUInt128(longValues[i]);
714 break;
715 case MTP_TYPE_STR: {
716 jstring value = (jstring)env->GetObjectArrayElement(stringValuesArray, i);
Mike Lockwood2711e492010-12-11 11:24:37 -0800717 const char *valueStr = (value ? env->GetStringUTFChars(value, NULL) : NULL);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400718 if (valueStr) {
719 packet.putString(valueStr);
720 env->ReleaseStringUTFChars(value, valueStr);
721 } else {
722 packet.putEmptyString();
723 }
724 env->DeleteLocalRef(value);
725 break;
726 }
727 default:
Steve Block3762c312012-01-06 19:20:56 +0000728 ALOGE("bad or unsupported data type in MyMtpDatabase::getObjectPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400729 break;
730 }
731 }
732
733 env->ReleaseIntArrayElements(objectHandlesArray, objectHandles, 0);
734 env->ReleaseIntArrayElements(propertyCodesArray, propertyCodes, 0);
735 env->ReleaseIntArrayElements(dataTypesArray, dataTypes, 0);
736 if (longValues)
737 env->ReleaseLongArrayElements(longValuesArray, longValues, 0);
738
739 env->DeleteLocalRef(objectHandlesArray);
740 env->DeleteLocalRef(propertyCodesArray);
741 env->DeleteLocalRef(dataTypesArray);
742 if (longValuesArray)
743 env->DeleteLocalRef(longValuesArray);
744 if (stringValuesArray)
745 env->DeleteLocalRef(stringValuesArray);
746 }
747
748 env->DeleteLocalRef(list);
749 checkAndClearExceptionFromCallback(env, __FUNCTION__);
750 return result;
751}
752
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400753MtpResponseCode MyMtpDatabase::getObjectInfo(MtpObjectHandle handle,
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700754 MtpObjectInfo& info) {
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700755 char date[20];
756 MtpString path;
757 int64_t length;
758 MtpObjectFormat format;
759
760 MtpResponseCode result = getObjectFilePath(handle, path, length, format);
761 if (result != MTP_RESPONSE_OK) {
762 return result;
763 }
764 info.mCompressedSize = (length > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)length);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400765
766 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700767 if (!env->CallBooleanMethod(mDatabase, method_getObjectInfo,
768 (jint)handle, mIntBuffer, mStringBuffer, mLongBuffer)) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400769 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700770 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400771
772 jint* intValues = env->GetIntArrayElements(mIntBuffer, 0);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700773 info.mStorageID = intValues[0];
774 info.mFormat = intValues[1];
775 info.mParent = intValues[2];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400776 env->ReleaseIntArrayElements(mIntBuffer, intValues, 0);
777
778 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700779 info.mDateCreated = longValues[0];
780 info.mDateModified = longValues[1];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400781 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
782
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700783// info.mAssociationType = (format == MTP_FORMAT_ASSOCIATION ?
Mike Lockwood828d19d2010-08-10 15:20:35 -0400784// MTP_ASSOCIATION_TYPE_GENERIC_FOLDER :
785// MTP_ASSOCIATION_TYPE_UNDEFINED);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700786 info.mAssociationType = MTP_ASSOCIATION_TYPE_UNDEFINED;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400787
788 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700789 MtpString temp(str);
790 info.mName = strdup((const char *)temp);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400791 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
792
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700793 // read EXIF data for thumbnail information
794 if (info.mFormat == MTP_FORMAT_EXIF_JPEG || info.mFormat == MTP_FORMAT_JFIF) {
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700795 ResetJpgfile();
796 // Start with an empty image information structure.
797 memset(&ImageInfo, 0, sizeof(ImageInfo));
798 ImageInfo.FlashUsed = -1;
799 ImageInfo.MeteringMode = -1;
800 ImageInfo.Whitebalance = -1;
801 strncpy(ImageInfo.FileName, (const char *)path, PATH_MAX);
802 if (ReadJpegFile((const char*)path, READ_METADATA)) {
803 Section_t* section = FindSection(M_EXIF);
804 if (section) {
805 info.mThumbCompressedSize = ImageInfo.ThumbnailSize;
806 info.mThumbFormat = MTP_FORMAT_EXIF_JPEG;
807 info.mImagePixWidth = ImageInfo.Width;
808 info.mImagePixHeight = ImageInfo.Height;
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700809 }
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700810 }
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700811 DiscardData();
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700812 }
813
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400814 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400815 return MTP_RESPONSE_OK;
816}
817
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700818void* MyMtpDatabase::getThumbnail(MtpObjectHandle handle, size_t& outThumbSize) {
819 MtpString path;
820 int64_t length;
821 MtpObjectFormat format;
822 void* result = NULL;
823 outThumbSize = 0;
824
825 if (getObjectFilePath(handle, path, length, format) == MTP_RESPONSE_OK
826 && (format == MTP_FORMAT_EXIF_JPEG || format == MTP_FORMAT_JFIF)) {
827 ResetJpgfile();
828 // Start with an empty image information structure.
829 memset(&ImageInfo, 0, sizeof(ImageInfo));
830 ImageInfo.FlashUsed = -1;
831 ImageInfo.MeteringMode = -1;
832 ImageInfo.Whitebalance = -1;
833 strncpy(ImageInfo.FileName, (const char *)path, PATH_MAX);
834 if (ReadJpegFile((const char*)path, READ_METADATA)) {
835 Section_t* section = FindSection(M_EXIF);
836 if (section) {
837 outThumbSize = ImageInfo.ThumbnailSize;
838 result = malloc(outThumbSize);
839 if (result)
840 memcpy(result, section->Data + ImageInfo.ThumbnailOffset + 8, outThumbSize);
841 }
842 DiscardData();
843 }
844 }
845
846 return result;
847}
848
Mike Lockwood59c777a2010-08-02 10:37:41 -0400849MtpResponseCode MyMtpDatabase::getObjectFilePath(MtpObjectHandle handle,
Mike Lockwood365e03e2010-12-08 16:08:01 -0800850 MtpString& outFilePath,
851 int64_t& outFileLength,
852 MtpObjectFormat& outFormat) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400853 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood59c777a2010-08-02 10:37:41 -0400854 jint result = env->CallIntMethod(mDatabase, method_getObjectFilePath,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400855 (jint)handle, mStringBuffer, mLongBuffer);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400856 if (result != MTP_RESPONSE_OK) {
857 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400858 return result;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400859 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400860
861 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
Mike Lockwood365e03e2010-12-08 16:08:01 -0800862 outFilePath.setTo(str, strlen16(str));
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400863 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
864
865 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
Mike Lockwood365e03e2010-12-08 16:08:01 -0800866 outFileLength = longValues[0];
867 outFormat = longValues[1];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400868 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700869
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400870 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400871 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400872}
873
Mike Lockwood59c777a2010-08-02 10:37:41 -0400874MtpResponseCode MyMtpDatabase::deleteFile(MtpObjectHandle handle) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400875 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400876 MtpResponseCode result = env->CallIntMethod(mDatabase, method_deleteFile, (jint)handle);
877
878 checkAndClearExceptionFromCallback(env, __FUNCTION__);
879 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400880}
881
882struct PropertyTableEntry {
883 MtpObjectProperty property;
884 int type;
885};
886
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400887static const PropertyTableEntry kObjectPropertyTable[] = {
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400888 { MTP_PROPERTY_STORAGE_ID, MTP_TYPE_UINT32 },
889 { MTP_PROPERTY_OBJECT_FORMAT, MTP_TYPE_UINT16 },
890 { MTP_PROPERTY_PROTECTION_STATUS, MTP_TYPE_UINT16 },
891 { MTP_PROPERTY_OBJECT_SIZE, MTP_TYPE_UINT64 },
892 { MTP_PROPERTY_OBJECT_FILE_NAME, MTP_TYPE_STR },
893 { MTP_PROPERTY_DATE_MODIFIED, MTP_TYPE_STR },
894 { MTP_PROPERTY_PARENT_OBJECT, MTP_TYPE_UINT32 },
895 { MTP_PROPERTY_PERSISTENT_UID, MTP_TYPE_UINT128 },
896 { MTP_PROPERTY_NAME, MTP_TYPE_STR },
Mike Lockwoodae078f72010-09-26 12:35:51 -0400897 { MTP_PROPERTY_DISPLAY_NAME, MTP_TYPE_STR },
898 { MTP_PROPERTY_DATE_ADDED, MTP_TYPE_STR },
899 { MTP_PROPERTY_ARTIST, MTP_TYPE_STR },
900 { MTP_PROPERTY_ALBUM_NAME, MTP_TYPE_STR },
901 { MTP_PROPERTY_ALBUM_ARTIST, MTP_TYPE_STR },
902 { MTP_PROPERTY_TRACK, MTP_TYPE_UINT16 },
903 { MTP_PROPERTY_ORIGINAL_RELEASE_DATE, MTP_TYPE_STR },
904 { MTP_PROPERTY_GENRE, MTP_TYPE_STR },
905 { MTP_PROPERTY_COMPOSER, MTP_TYPE_STR },
906 { MTP_PROPERTY_DURATION, MTP_TYPE_UINT32 },
907 { MTP_PROPERTY_DESCRIPTION, MTP_TYPE_STR },
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400908};
909
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400910static const PropertyTableEntry kDevicePropertyTable[] = {
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800911 { MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER, MTP_TYPE_STR },
912 { MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME, MTP_TYPE_STR },
913 { MTP_DEVICE_PROPERTY_IMAGE_SIZE, MTP_TYPE_STR },
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400914};
915
916bool MyMtpDatabase::getObjectPropertyInfo(MtpObjectProperty property, int& type) {
917 int count = sizeof(kObjectPropertyTable) / sizeof(kObjectPropertyTable[0]);
918 const PropertyTableEntry* entry = kObjectPropertyTable;
919 for (int i = 0; i < count; i++, entry++) {
920 if (entry->property == property) {
921 type = entry->type;
922 return true;
923 }
924 }
925 return false;
926}
927
928bool MyMtpDatabase::getDevicePropertyInfo(MtpDeviceProperty property, int& type) {
929 int count = sizeof(kDevicePropertyTable) / sizeof(kDevicePropertyTable[0]);
930 const PropertyTableEntry* entry = kDevicePropertyTable;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400931 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
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400940MtpObjectHandleList* MyMtpDatabase::getObjectReferences(MtpObjectHandle handle) {
941 JNIEnv* env = AndroidRuntime::getJNIEnv();
942 jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectReferences,
943 (jint)handle);
944 if (!array)
945 return NULL;
946 MtpObjectHandleList* list = new MtpObjectHandleList();
947 jint* handles = env->GetIntArrayElements(array, 0);
948 jsize length = env->GetArrayLength(array);
949 for (int i = 0; i < length; i++)
950 list->push(handles[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400951 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400952 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400953
954 checkAndClearExceptionFromCallback(env, __FUNCTION__);
955 return list;
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400956}
957
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400958MtpResponseCode MyMtpDatabase::setObjectReferences(MtpObjectHandle handle,
959 MtpObjectHandleList* references) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400960 JNIEnv* env = AndroidRuntime::getJNIEnv();
961 int count = references->size();
962 jintArray array = env->NewIntArray(count);
963 if (!array) {
Steve Block3762c312012-01-06 19:20:56 +0000964 ALOGE("out of memory in setObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400965 return false;
966 }
967 jint* handles = env->GetIntArrayElements(array, 0);
968 for (int i = 0; i < count; i++)
969 handles[i] = (*references)[i];
970 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400971 MtpResponseCode result = env->CallIntMethod(mDatabase, method_setObjectReferences,
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400972 (jint)handle, array);
Mike Lockwood88394712010-09-27 10:01:00 -0400973 env->DeleteLocalRef(array);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400974
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400975 checkAndClearExceptionFromCallback(env, __FUNCTION__);
976 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400977}
978
Mike Lockwood828d19d2010-08-10 15:20:35 -0400979MtpProperty* MyMtpDatabase::getObjectPropertyDesc(MtpObjectProperty property,
980 MtpObjectFormat format) {
981 MtpProperty* result = NULL;
982 switch (property) {
983 case MTP_PROPERTY_OBJECT_FORMAT:
Mike Lockwood9b5e9c42010-12-07 18:53:50 -0800984 // use format as default value
985 result = new MtpProperty(property, MTP_TYPE_UINT16, false, format);
986 break;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400987 case MTP_PROPERTY_PROTECTION_STATUS:
Mike Lockwoodae078f72010-09-26 12:35:51 -0400988 case MTP_PROPERTY_TRACK:
Mike Lockwood828d19d2010-08-10 15:20:35 -0400989 result = new MtpProperty(property, MTP_TYPE_UINT16);
990 break;
991 case MTP_PROPERTY_STORAGE_ID:
992 case MTP_PROPERTY_PARENT_OBJECT:
Mike Lockwoodae078f72010-09-26 12:35:51 -0400993 case MTP_PROPERTY_DURATION:
Mike Lockwood828d19d2010-08-10 15:20:35 -0400994 result = new MtpProperty(property, MTP_TYPE_UINT32);
995 break;
996 case MTP_PROPERTY_OBJECT_SIZE:
997 result = new MtpProperty(property, MTP_TYPE_UINT64);
998 break;
999 case MTP_PROPERTY_PERSISTENT_UID:
1000 result = new MtpProperty(property, MTP_TYPE_UINT128);
1001 break;
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -04001002 case MTP_PROPERTY_NAME:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001003 case MTP_PROPERTY_DISPLAY_NAME:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001004 case MTP_PROPERTY_ARTIST:
1005 case MTP_PROPERTY_ALBUM_NAME:
1006 case MTP_PROPERTY_ALBUM_ARTIST:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001007 case MTP_PROPERTY_GENRE:
1008 case MTP_PROPERTY_COMPOSER:
1009 case MTP_PROPERTY_DESCRIPTION:
Mike Lockwood828d19d2010-08-10 15:20:35 -04001010 result = new MtpProperty(property, MTP_TYPE_STR);
1011 break;
Mike Lockwood5b19af02010-11-23 18:38:55 -05001012 case MTP_PROPERTY_DATE_MODIFIED:
1013 case MTP_PROPERTY_DATE_ADDED:
1014 case MTP_PROPERTY_ORIGINAL_RELEASE_DATE:
1015 result = new MtpProperty(property, MTP_TYPE_STR);
1016 result->setFormDateTime();
1017 break;
Mike Lockwood5ebac832010-10-12 11:33:47 -04001018 case MTP_PROPERTY_OBJECT_FILE_NAME:
Mike Lockwood6a6a3af2010-10-12 14:19:51 -04001019 // We allow renaming files and folders
1020 result = new MtpProperty(property, MTP_TYPE_STR, true);
Mike Lockwood5ebac832010-10-12 11:33:47 -04001021 break;
Mike Lockwood828d19d2010-08-10 15:20:35 -04001022 }
1023
1024 return result;
1025}
1026
1027MtpProperty* MyMtpDatabase::getDevicePropertyDesc(MtpDeviceProperty property) {
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001028 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001029 MtpProperty* result = NULL;
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001030 bool writable = false;
1031
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001032 switch (property) {
1033 case MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
1034 case MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001035 writable = true;
1036 // fall through
1037 case MTP_DEVICE_PROPERTY_IMAGE_SIZE:
1038 result = new MtpProperty(property, MTP_TYPE_STR, writable);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001039
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001040 // get current value
Mike Lockwooda2a21282010-09-25 21:21:05 -04001041 jint ret = env->CallIntMethod(mDatabase, method_getDeviceProperty,
1042 (jint)property, mLongBuffer, mStringBuffer);
1043 if (ret == MTP_RESPONSE_OK) {
1044 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
1045 result->setCurrentValue(str);
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001046 // for read-only properties it is safe to assume current value is default value
1047 if (!writable)
1048 result->setDefaultValue(str);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001049 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
1050 } else {
Steve Block3762c312012-01-06 19:20:56 +00001051 ALOGE("unable to read device property, response: %04X", ret);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001052 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001053 break;
1054 }
1055
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001056 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001057 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -04001058}
1059
Mike Lockwood2837eef2010-08-31 16:25:12 -04001060void MyMtpDatabase::sessionStarted() {
1061 JNIEnv* env = AndroidRuntime::getJNIEnv();
1062 env->CallVoidMethod(mDatabase, method_sessionStarted);
1063 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1064}
1065
1066void MyMtpDatabase::sessionEnded() {
1067 JNIEnv* env = AndroidRuntime::getJNIEnv();
1068 env->CallVoidMethod(mDatabase, method_sessionEnded);
1069 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1070}
1071
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001072// ----------------------------------------------------------------------------
1073
1074static void
Mike Lockwood0cd01362010-12-30 11:54:33 -05001075android_mtp_MtpDatabase_setup(JNIEnv *env, jobject thiz)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001076{
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001077 MyMtpDatabase* database = new MyMtpDatabase(env, thiz);
Ashok Bhate2e59322013-12-17 19:04:19 +00001078 env->SetLongField(thiz, field_context, (jlong)database);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001079 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1080}
1081
1082static void
Mike Lockwood0cd01362010-12-30 11:54:33 -05001083android_mtp_MtpDatabase_finalize(JNIEnv *env, jobject thiz)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001084{
Ashok Bhate2e59322013-12-17 19:04:19 +00001085 MyMtpDatabase* database = (MyMtpDatabase *)env->GetLongField(thiz, field_context);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001086 database->cleanup(env);
1087 delete database;
Ashok Bhate2e59322013-12-17 19:04:19 +00001088 env->SetLongField(thiz, field_context, 0);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001089 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1090}
1091
Mike Lockwood31599912010-11-15 13:43:30 -05001092static jstring
Mike Lockwood0cd01362010-12-30 11:54:33 -05001093android_mtp_MtpPropertyGroup_format_date_time(JNIEnv *env, jobject thiz, jlong seconds)
Mike Lockwood31599912010-11-15 13:43:30 -05001094{
Mike Lockwood31599912010-11-15 13:43:30 -05001095 char date[20];
1096 formatDateTime(seconds, date, sizeof(date));
1097 return env->NewStringUTF(date);
Mike Lockwood31599912010-11-15 13:43:30 -05001098}
1099
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001100// ----------------------------------------------------------------------------
1101
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001102static JNINativeMethod gMtpDatabaseMethods[] = {
Mike Lockwood0cd01362010-12-30 11:54:33 -05001103 {"native_setup", "()V", (void *)android_mtp_MtpDatabase_setup},
1104 {"native_finalize", "()V", (void *)android_mtp_MtpDatabase_finalize},
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001105};
1106
1107static JNINativeMethod gMtpPropertyGroupMethods[] = {
Mike Lockwood31599912010-11-15 13:43:30 -05001108 {"format_date_time", "(J)Ljava/lang/String;",
Mike Lockwood0cd01362010-12-30 11:54:33 -05001109 (void *)android_mtp_MtpPropertyGroup_format_date_time},
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001110};
1111
Mike Lockwood0cd01362010-12-30 11:54:33 -05001112static const char* const kClassPathName = "android/mtp/MtpDatabase";
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001113
Mike Lockwood0cd01362010-12-30 11:54:33 -05001114int register_android_mtp_MtpDatabase(JNIEnv *env)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001115{
1116 jclass clazz;
1117
Mike Lockwood0cd01362010-12-30 11:54:33 -05001118 clazz = env->FindClass("android/mtp/MtpDatabase");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001119 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001120 ALOGE("Can't find android/mtp/MtpDatabase");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001121 return -1;
1122 }
Mike Lockwoodd815f792010-07-12 08:49:01 -04001123 method_beginSendObject = env->GetMethodID(clazz, "beginSendObject", "(Ljava/lang/String;IIIJJ)I");
1124 if (method_beginSendObject == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001125 ALOGE("Can't find beginSendObject");
Mike Lockwoodd815f792010-07-12 08:49:01 -04001126 return -1;
1127 }
Mike Lockwood7a0bd172011-01-18 11:06:19 -08001128 method_endSendObject = env->GetMethodID(clazz, "endSendObject", "(Ljava/lang/String;IIZ)V");
Mike Lockwoodd815f792010-07-12 08:49:01 -04001129 if (method_endSendObject == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001130 ALOGE("Can't find endSendObject");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001131 return -1;
1132 }
1133 method_getObjectList = env->GetMethodID(clazz, "getObjectList", "(III)[I");
1134 if (method_getObjectList == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001135 ALOGE("Can't find getObjectList");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001136 return -1;
1137 }
Mike Lockwood7a047c82010-08-02 10:52:20 -04001138 method_getNumObjects = env->GetMethodID(clazz, "getNumObjects", "(III)I");
1139 if (method_getNumObjects == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001140 ALOGE("Can't find getNumObjects");
Mike Lockwood7a047c82010-08-02 10:52:20 -04001141 return -1;
1142 }
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001143 method_getSupportedPlaybackFormats = env->GetMethodID(clazz, "getSupportedPlaybackFormats", "()[I");
1144 if (method_getSupportedPlaybackFormats == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001145 ALOGE("Can't find getSupportedPlaybackFormats");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001146 return -1;
1147 }
1148 method_getSupportedCaptureFormats = env->GetMethodID(clazz, "getSupportedCaptureFormats", "()[I");
1149 if (method_getSupportedCaptureFormats == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001150 ALOGE("Can't find getSupportedCaptureFormats");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001151 return -1;
1152 }
1153 method_getSupportedObjectProperties = env->GetMethodID(clazz, "getSupportedObjectProperties", "(I)[I");
1154 if (method_getSupportedObjectProperties == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001155 ALOGE("Can't find getSupportedObjectProperties");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001156 return -1;
1157 }
1158 method_getSupportedDeviceProperties = env->GetMethodID(clazz, "getSupportedDeviceProperties", "()[I");
1159 if (method_getSupportedDeviceProperties == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001160 ALOGE("Can't find getSupportedDeviceProperties");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001161 return -1;
1162 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001163 method_setObjectProperty = env->GetMethodID(clazz, "setObjectProperty", "(IIJLjava/lang/String;)I");
1164 if (method_setObjectProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001165 ALOGE("Can't find setObjectProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001166 return -1;
1167 }
1168 method_getDeviceProperty = env->GetMethodID(clazz, "getDeviceProperty", "(I[J[C)I");
1169 if (method_getDeviceProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001170 ALOGE("Can't find getDeviceProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001171 return -1;
1172 }
1173 method_setDeviceProperty = env->GetMethodID(clazz, "setDeviceProperty", "(IJLjava/lang/String;)I");
1174 if (method_setDeviceProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001175 ALOGE("Can't find setDeviceProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001176 return -1;
1177 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001178 method_getObjectPropertyList = env->GetMethodID(clazz, "getObjectPropertyList",
Mike Lockwood0cd01362010-12-30 11:54:33 -05001179 "(JIJII)Landroid/mtp/MtpPropertyList;");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001180 if (method_getObjectPropertyList == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001181 ALOGE("Can't find getObjectPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001182 return -1;
1183 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001184 method_getObjectInfo = env->GetMethodID(clazz, "getObjectInfo", "(I[I[C[J)Z");
1185 if (method_getObjectInfo == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001186 ALOGE("Can't find getObjectInfo");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001187 return -1;
1188 }
Mike Lockwood59c777a2010-08-02 10:37:41 -04001189 method_getObjectFilePath = env->GetMethodID(clazz, "getObjectFilePath", "(I[C[J)I");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001190 if (method_getObjectFilePath == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001191 ALOGE("Can't find getObjectFilePath");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001192 return -1;
1193 }
Mike Lockwood59c777a2010-08-02 10:37:41 -04001194 method_deleteFile = env->GetMethodID(clazz, "deleteFile", "(I)I");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001195 if (method_deleteFile == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001196 ALOGE("Can't find deleteFile");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001197 return -1;
1198 }
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001199 method_getObjectReferences = env->GetMethodID(clazz, "getObjectReferences", "(I)[I");
1200 if (method_getObjectReferences == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001201 ALOGE("Can't find getObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001202 return -1;
1203 }
1204 method_setObjectReferences = env->GetMethodID(clazz, "setObjectReferences", "(I[I)I");
1205 if (method_setObjectReferences == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001206 ALOGE("Can't find setObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001207 return -1;
1208 }
Mike Lockwood2837eef2010-08-31 16:25:12 -04001209 method_sessionStarted = env->GetMethodID(clazz, "sessionStarted", "()V");
1210 if (method_sessionStarted == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001211 ALOGE("Can't find sessionStarted");
Mike Lockwood2837eef2010-08-31 16:25:12 -04001212 return -1;
1213 }
1214 method_sessionEnded = env->GetMethodID(clazz, "sessionEnded", "()V");
1215 if (method_sessionEnded == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001216 ALOGE("Can't find sessionEnded");
Mike Lockwood2837eef2010-08-31 16:25:12 -04001217 return -1;
1218 }
1219
Ashok Bhate2e59322013-12-17 19:04:19 +00001220 field_context = env->GetFieldID(clazz, "mNativeContext", "J");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001221 if (field_context == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001222 ALOGE("Can't find MtpDatabase.mNativeContext");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001223 return -1;
1224 }
1225
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001226 // now set up fields for MtpPropertyList class
Mike Lockwood0cd01362010-12-30 11:54:33 -05001227 clazz = env->FindClass("android/mtp/MtpPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001228 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001229 ALOGE("Can't find android/mtp/MtpPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001230 return -1;
1231 }
1232 field_mCount = env->GetFieldID(clazz, "mCount", "I");
1233 if (field_mCount == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001234 ALOGE("Can't find MtpPropertyList.mCount");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001235 return -1;
1236 }
1237 field_mResult = env->GetFieldID(clazz, "mResult", "I");
1238 if (field_mResult == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001239 ALOGE("Can't find MtpPropertyList.mResult");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001240 return -1;
1241 }
1242 field_mObjectHandles = env->GetFieldID(clazz, "mObjectHandles", "[I");
1243 if (field_mObjectHandles == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001244 ALOGE("Can't find MtpPropertyList.mObjectHandles");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001245 return -1;
1246 }
1247 field_mPropertyCodes = env->GetFieldID(clazz, "mPropertyCodes", "[I");
1248 if (field_mPropertyCodes == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001249 ALOGE("Can't find MtpPropertyList.mPropertyCodes");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001250 return -1;
1251 }
1252 field_mDataTypes = env->GetFieldID(clazz, "mDataTypes", "[I");
1253 if (field_mDataTypes == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001254 ALOGE("Can't find MtpPropertyList.mDataTypes");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001255 return -1;
1256 }
1257 field_mLongValues = env->GetFieldID(clazz, "mLongValues", "[J");
1258 if (field_mLongValues == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001259 ALOGE("Can't find MtpPropertyList.mLongValues");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001260 return -1;
1261 }
1262 field_mStringValues = env->GetFieldID(clazz, "mStringValues", "[Ljava/lang/String;");
1263 if (field_mStringValues == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001264 ALOGE("Can't find MtpPropertyList.mStringValues");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001265 return -1;
1266 }
1267
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001268 if (AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -05001269 "android/mtp/MtpDatabase", gMtpDatabaseMethods, NELEM(gMtpDatabaseMethods)))
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001270 return -1;
1271
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001272 return AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -05001273 "android/mtp/MtpPropertyGroup", gMtpPropertyGroupMethods, NELEM(gMtpPropertyGroupMethods));
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001274}