blob: 8129c0dfe71193cc4c10349fc53afe74059a7578 [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
Mike Lockwoodd21eac92010-07-03 00:44:05 -040020#include <assert.h>
Mike Lockwoodd21eac92010-07-03 00:44:05 -040021#include <fcntl.h>
Mark Salyzynaeb75fc2014-03-20 12:09:01 -070022#include <inttypes.h>
23#include <limits.h>
24#include <stdio.h>
25#include <unistd.h>
Mike Lockwoodd21eac92010-07-03 00:44:05 -040026
27#include "jni.h"
28#include "JNIHelp.h"
29#include "android_runtime/AndroidRuntime.h"
Ruben Brunk87eac992013-09-09 17:44:59 -070030#include "android_runtime/Log.h"
Mike Lockwoodd21eac92010-07-03 00:44:05 -040031
32#include "MtpDatabase.h"
33#include "MtpDataPacket.h"
Mike Lockwood9df53fae2011-04-21 17:05:55 -070034#include "MtpObjectInfo.h"
Mike Lockwood828d19d2010-08-10 15:20:35 -040035#include "MtpProperty.h"
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040036#include "MtpStringBuffer.h"
Mike Lockwoodd21eac92010-07-03 00:44:05 -040037#include "MtpUtils.h"
38#include "mtp.h"
39
Mike Lockwoodc89f2222011-04-24 18:40:17 -070040extern "C" {
41#include "jhead.h"
42}
43
Mike Lockwoodd21eac92010-07-03 00:44:05 -040044using namespace android;
45
46// ----------------------------------------------------------------------------
47
Mike Lockwoodd815f792010-07-12 08:49:01 -040048static jmethodID method_beginSendObject;
49static jmethodID method_endSendObject;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040050static jmethodID method_getObjectList;
Mike Lockwood7a047c82010-08-02 10:52:20 -040051static jmethodID method_getNumObjects;
Mike Lockwood4b322ce2010-08-10 07:37:50 -040052static jmethodID method_getSupportedPlaybackFormats;
53static jmethodID method_getSupportedCaptureFormats;
54static jmethodID method_getSupportedObjectProperties;
55static jmethodID method_getSupportedDeviceProperties;
Mike Lockwood828d19d2010-08-10 15:20:35 -040056static jmethodID method_setObjectProperty;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040057static jmethodID method_getDeviceProperty;
58static jmethodID method_setDeviceProperty;
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -040059static jmethodID method_getObjectPropertyList;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040060static jmethodID method_getObjectInfo;
61static jmethodID method_getObjectFilePath;
62static jmethodID method_deleteFile;
Mike Lockwood9a2046f2010-08-03 15:30:09 -040063static jmethodID method_getObjectReferences;
64static jmethodID method_setObjectReferences;
Mike Lockwood2837eef2010-08-31 16:25:12 -040065static jmethodID method_sessionStarted;
66static jmethodID method_sessionEnded;
67
Mike Lockwoodd21eac92010-07-03 00:44:05 -040068static jfieldID field_context;
69
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -040070// MtpPropertyList fields
71static jfieldID field_mCount;
72static jfieldID field_mResult;
73static jfieldID field_mObjectHandles;
74static jfieldID field_mPropertyCodes;
75static jfieldID field_mDataTypes;
76static jfieldID field_mLongValues;
77static jfieldID field_mStringValues;
78
79
Mike Lockwoodd21eac92010-07-03 00:44:05 -040080MtpDatabase* getMtpDatabase(JNIEnv *env, jobject database) {
Ashok Bhate2e59322013-12-17 19:04:19 +000081 return (MtpDatabase *)env->GetLongField(database, field_context);
Mike Lockwoodd21eac92010-07-03 00:44:05 -040082}
83
84// ----------------------------------------------------------------------------
85
86class MyMtpDatabase : public MtpDatabase {
87private:
88 jobject mDatabase;
89 jintArray mIntBuffer;
90 jlongArray mLongBuffer;
91 jcharArray mStringBuffer;
92
93public:
94 MyMtpDatabase(JNIEnv *env, jobject client);
95 virtual ~MyMtpDatabase();
96 void cleanup(JNIEnv *env);
97
Mike Lockwoodd815f792010-07-12 08:49:01 -040098 virtual MtpObjectHandle beginSendObject(const char* path,
Mike Lockwoodd21eac92010-07-03 00:44:05 -040099 MtpObjectFormat format,
100 MtpObjectHandle parent,
101 MtpStorageID storage,
102 uint64_t size,
103 time_t modified);
104
Mike Lockwoodd815f792010-07-12 08:49:01 -0400105 virtual void endSendObject(const char* path,
106 MtpObjectHandle handle,
107 MtpObjectFormat format,
108 bool succeeded);
109
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400110 virtual MtpObjectHandleList* getObjectList(MtpStorageID storageID,
111 MtpObjectFormat format,
112 MtpObjectHandle parent);
113
Mike Lockwood7a047c82010-08-02 10:52:20 -0400114 virtual int getNumObjects(MtpStorageID storageID,
115 MtpObjectFormat format,
116 MtpObjectHandle parent);
117
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400118 // callee should delete[] the results from these
119 // results can be NULL
120 virtual MtpObjectFormatList* getSupportedPlaybackFormats();
121 virtual MtpObjectFormatList* getSupportedCaptureFormats();
122 virtual MtpObjectPropertyList* getSupportedObjectProperties(MtpObjectFormat format);
123 virtual MtpDevicePropertyList* getSupportedDeviceProperties();
124
Mike Lockwood828d19d2010-08-10 15:20:35 -0400125 virtual MtpResponseCode getObjectPropertyValue(MtpObjectHandle handle,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400126 MtpObjectProperty property,
127 MtpDataPacket& packet);
128
Mike Lockwood828d19d2010-08-10 15:20:35 -0400129 virtual MtpResponseCode setObjectPropertyValue(MtpObjectHandle handle,
130 MtpObjectProperty property,
131 MtpDataPacket& packet);
132
133 virtual MtpResponseCode getDevicePropertyValue(MtpDeviceProperty property,
134 MtpDataPacket& packet);
135
136 virtual MtpResponseCode setDevicePropertyValue(MtpDeviceProperty property,
137 MtpDataPacket& packet);
138
139 virtual MtpResponseCode resetDeviceProperty(MtpDeviceProperty property);
140
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400141 virtual MtpResponseCode getObjectPropertyList(MtpObjectHandle handle,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500142 uint32_t format, uint32_t property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400143 int groupCode, int depth,
144 MtpDataPacket& packet);
145
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400146 virtual MtpResponseCode getObjectInfo(MtpObjectHandle handle,
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700147 MtpObjectInfo& info);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400148
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700149 virtual void* getThumbnail(MtpObjectHandle handle, size_t& outThumbSize);
150
Mike Lockwood59c777a2010-08-02 10:37:41 -0400151 virtual MtpResponseCode getObjectFilePath(MtpObjectHandle handle,
Mike Lockwood365e03e2010-12-08 16:08:01 -0800152 MtpString& outFilePath,
153 int64_t& outFileLength,
154 MtpObjectFormat& outFormat);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400155 virtual MtpResponseCode deleteFile(MtpObjectHandle handle);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400156
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400157 bool getObjectPropertyInfo(MtpObjectProperty property, int& type);
158 bool getDevicePropertyInfo(MtpDeviceProperty property, int& type);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400159
160 virtual MtpObjectHandleList* getObjectReferences(MtpObjectHandle handle);
161
162 virtual MtpResponseCode setObjectReferences(MtpObjectHandle handle,
163 MtpObjectHandleList* references);
Mike Lockwood828d19d2010-08-10 15:20:35 -0400164
165 virtual MtpProperty* getObjectPropertyDesc(MtpObjectProperty property,
166 MtpObjectFormat format);
167
168 virtual MtpProperty* getDevicePropertyDesc(MtpDeviceProperty property);
Mike Lockwood2837eef2010-08-31 16:25:12 -0400169
170 virtual void sessionStarted();
171
172 virtual void sessionEnded();
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400173};
174
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400175// ----------------------------------------------------------------------------
176
177static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
178 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000179 ALOGE("An exception was thrown by callback '%s'.", methodName);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400180 LOGE_EX(env);
181 env->ExceptionClear();
182 }
183}
184
185// ----------------------------------------------------------------------------
186
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400187MyMtpDatabase::MyMtpDatabase(JNIEnv *env, jobject client)
188 : mDatabase(env->NewGlobalRef(client)),
189 mIntBuffer(NULL),
190 mLongBuffer(NULL),
191 mStringBuffer(NULL)
192{
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400193 // create buffers for out arguments
194 // we don't need to be thread-safe so this is OK
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700195 jintArray intArray = env->NewIntArray(3);
196 if (!intArray) {
197 return; // Already threw.
198 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400199 mIntBuffer = (jintArray)env->NewGlobalRef(intArray);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700200 jlongArray longArray = env->NewLongArray(2);
201 if (!longArray) {
202 return; // Already threw.
203 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400204 mLongBuffer = (jlongArray)env->NewGlobalRef(longArray);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700205 jcharArray charArray = env->NewCharArray(256);
206 if (!charArray) {
207 return; // Already threw.
208 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400209 mStringBuffer = (jcharArray)env->NewGlobalRef(charArray);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400210}
211
212void MyMtpDatabase::cleanup(JNIEnv *env) {
213 env->DeleteGlobalRef(mDatabase);
214 env->DeleteGlobalRef(mIntBuffer);
215 env->DeleteGlobalRef(mLongBuffer);
216 env->DeleteGlobalRef(mStringBuffer);
217}
218
219MyMtpDatabase::~MyMtpDatabase() {
220}
221
Mike Lockwoodd815f792010-07-12 08:49:01 -0400222MtpObjectHandle MyMtpDatabase::beginSendObject(const char* path,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400223 MtpObjectFormat format,
224 MtpObjectHandle parent,
225 MtpStorageID storage,
226 uint64_t size,
227 time_t modified) {
228 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood88394712010-09-27 10:01:00 -0400229 jstring pathStr = env->NewStringUTF(path);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400230 MtpObjectHandle result = env->CallIntMethod(mDatabase, method_beginSendObject,
Mike Lockwood88394712010-09-27 10:01:00 -0400231 pathStr, (jint)format, (jint)parent, (jint)storage,
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400232 (jlong)size, (jlong)modified);
233
Mike Lockwood88394712010-09-27 10:01:00 -0400234 if (pathStr)
235 env->DeleteLocalRef(pathStr);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400236 checkAndClearExceptionFromCallback(env, __FUNCTION__);
237 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400238}
239
Mike Lockwoodd815f792010-07-12 08:49:01 -0400240void MyMtpDatabase::endSendObject(const char* path, MtpObjectHandle handle,
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800241 MtpObjectFormat format, bool succeeded) {
Mike Lockwoodd815f792010-07-12 08:49:01 -0400242 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood88394712010-09-27 10:01:00 -0400243 jstring pathStr = env->NewStringUTF(path);
244 env->CallVoidMethod(mDatabase, method_endSendObject, pathStr,
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800245 (jint)handle, (jint)format, (jboolean)succeeded);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400246
Mike Lockwood88394712010-09-27 10:01:00 -0400247 if (pathStr)
248 env->DeleteLocalRef(pathStr);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400249 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400250}
251
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400252MtpObjectHandleList* MyMtpDatabase::getObjectList(MtpStorageID storageID,
253 MtpObjectFormat format,
254 MtpObjectHandle parent) {
255 JNIEnv* env = AndroidRuntime::getJNIEnv();
256 jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectList,
257 (jint)storageID, (jint)format, (jint)parent);
258 if (!array)
259 return NULL;
260 MtpObjectHandleList* list = new MtpObjectHandleList();
261 jint* handles = env->GetIntArrayElements(array, 0);
262 jsize length = env->GetArrayLength(array);
Mike Lockwood7a047c82010-08-02 10:52:20 -0400263 for (int i = 0; i < length; i++)
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400264 list->push(handles[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400265 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400266 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400267
268 checkAndClearExceptionFromCallback(env, __FUNCTION__);
269 return list;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400270}
271
Mike Lockwood7a047c82010-08-02 10:52:20 -0400272int MyMtpDatabase::getNumObjects(MtpStorageID storageID,
273 MtpObjectFormat format,
274 MtpObjectHandle parent) {
275 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400276 int result = env->CallIntMethod(mDatabase, method_getNumObjects,
Mike Lockwood7a047c82010-08-02 10:52:20 -0400277 (jint)storageID, (jint)format, (jint)parent);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400278
279 checkAndClearExceptionFromCallback(env, __FUNCTION__);
280 return result;
Mike Lockwood7a047c82010-08-02 10:52:20 -0400281}
282
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400283MtpObjectFormatList* MyMtpDatabase::getSupportedPlaybackFormats() {
284 JNIEnv* env = AndroidRuntime::getJNIEnv();
285 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
286 method_getSupportedPlaybackFormats);
287 if (!array)
288 return NULL;
289 MtpObjectFormatList* list = new MtpObjectFormatList();
290 jint* formats = env->GetIntArrayElements(array, 0);
291 jsize length = env->GetArrayLength(array);
292 for (int i = 0; i < length; i++)
293 list->push(formats[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400294 env->ReleaseIntArrayElements(array, formats, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400295 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400296
297 checkAndClearExceptionFromCallback(env, __FUNCTION__);
298 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400299}
300
301MtpObjectFormatList* MyMtpDatabase::getSupportedCaptureFormats() {
302 JNIEnv* env = AndroidRuntime::getJNIEnv();
303 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
304 method_getSupportedCaptureFormats);
305 if (!array)
306 return NULL;
307 MtpObjectFormatList* list = new MtpObjectFormatList();
308 jint* formats = env->GetIntArrayElements(array, 0);
309 jsize length = env->GetArrayLength(array);
310 for (int i = 0; i < length; i++)
311 list->push(formats[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400312 env->ReleaseIntArrayElements(array, formats, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400313 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400314
315 checkAndClearExceptionFromCallback(env, __FUNCTION__);
316 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400317}
318
319MtpObjectPropertyList* MyMtpDatabase::getSupportedObjectProperties(MtpObjectFormat format) {
320 JNIEnv* env = AndroidRuntime::getJNIEnv();
321 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
322 method_getSupportedObjectProperties, (jint)format);
323 if (!array)
324 return NULL;
325 MtpObjectPropertyList* list = new MtpObjectPropertyList();
326 jint* properties = env->GetIntArrayElements(array, 0);
327 jsize length = env->GetArrayLength(array);
328 for (int i = 0; i < length; i++)
329 list->push(properties[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400330 env->ReleaseIntArrayElements(array, properties, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400331 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400332
333 checkAndClearExceptionFromCallback(env, __FUNCTION__);
334 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400335}
336
337MtpDevicePropertyList* MyMtpDatabase::getSupportedDeviceProperties() {
338 JNIEnv* env = AndroidRuntime::getJNIEnv();
339 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
340 method_getSupportedDeviceProperties);
341 if (!array)
342 return NULL;
343 MtpDevicePropertyList* list = new MtpDevicePropertyList();
344 jint* properties = env->GetIntArrayElements(array, 0);
345 jsize length = env->GetArrayLength(array);
346 for (int i = 0; i < length; i++)
347 list->push(properties[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400348 env->ReleaseIntArrayElements(array, properties, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400349 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400350
351 checkAndClearExceptionFromCallback(env, __FUNCTION__);
352 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400353}
354
Mike Lockwood828d19d2010-08-10 15:20:35 -0400355MtpResponseCode MyMtpDatabase::getObjectPropertyValue(MtpObjectHandle handle,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400356 MtpObjectProperty property,
357 MtpDataPacket& packet) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400358 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400359 jobject list = env->CallObjectMethod(mDatabase, method_getObjectPropertyList,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500360 (jlong)handle, 0, (jlong)property, 0, 0);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400361 MtpResponseCode result = env->GetIntField(list, field_mResult);
362 int count = env->GetIntField(list, field_mCount);
363 if (result == MTP_RESPONSE_OK && count != 1)
364 result = MTP_RESPONSE_GENERAL_ERROR;
365
366 if (result == MTP_RESPONSE_OK) {
367 jintArray objectHandlesArray = (jintArray)env->GetObjectField(list, field_mObjectHandles);
368 jintArray propertyCodesArray = (jintArray)env->GetObjectField(list, field_mPropertyCodes);
369 jintArray dataTypesArray = (jintArray)env->GetObjectField(list, field_mDataTypes);
370 jlongArray longValuesArray = (jlongArray)env->GetObjectField(list, field_mLongValues);
371 jobjectArray stringValuesArray = (jobjectArray)env->GetObjectField(list, field_mStringValues);
372
373 jint* objectHandles = env->GetIntArrayElements(objectHandlesArray, 0);
374 jint* propertyCodes = env->GetIntArrayElements(propertyCodesArray, 0);
375 jint* dataTypes = env->GetIntArrayElements(dataTypesArray, 0);
376 jlong* longValues = (longValuesArray ? env->GetLongArrayElements(longValuesArray, 0) : NULL);
377
378 int type = dataTypes[0];
379 jlong longValue = (longValues ? longValues[0] : 0);
380
381 // special case date properties, which are strings to MTP
382 // but stored internally as a uint64
383 if (property == MTP_PROPERTY_DATE_MODIFIED || property == MTP_PROPERTY_DATE_ADDED) {
384 char date[20];
385 formatDateTime(longValue, date, sizeof(date));
386 packet.putString(date);
387 goto out;
388 }
389 // release date is stored internally as just the year
390 if (property == MTP_PROPERTY_ORIGINAL_RELEASE_DATE) {
391 char date[20];
Mark Salyzynaeb75fc2014-03-20 12:09:01 -0700392 snprintf(date, sizeof(date), "%04" PRId64 "0101T000000", longValue);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400393 packet.putString(date);
394 goto out;
395 }
396
397 switch (type) {
398 case MTP_TYPE_INT8:
399 packet.putInt8(longValue);
400 break;
401 case MTP_TYPE_UINT8:
402 packet.putUInt8(longValue);
403 break;
404 case MTP_TYPE_INT16:
405 packet.putInt16(longValue);
406 break;
407 case MTP_TYPE_UINT16:
408 packet.putUInt16(longValue);
409 break;
410 case MTP_TYPE_INT32:
411 packet.putInt32(longValue);
412 break;
413 case MTP_TYPE_UINT32:
414 packet.putUInt32(longValue);
415 break;
416 case MTP_TYPE_INT64:
417 packet.putInt64(longValue);
418 break;
419 case MTP_TYPE_UINT64:
420 packet.putUInt64(longValue);
421 break;
422 case MTP_TYPE_INT128:
423 packet.putInt128(longValue);
424 break;
425 case MTP_TYPE_UINT128:
426 packet.putInt128(longValue);
427 break;
428 case MTP_TYPE_STR:
429 {
430 jstring stringValue = (jstring)env->GetObjectArrayElement(stringValuesArray, 0);
431 if (stringValue) {
432 const char* str = env->GetStringUTFChars(stringValue, NULL);
James Dong39774722011-04-06 11:57:48 -0700433 if (str == NULL) {
434 return MTP_RESPONSE_GENERAL_ERROR;
435 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400436 packet.putString(str);
437 env->ReleaseStringUTFChars(stringValue, str);
438 } else {
439 packet.putEmptyString();
440 }
441 break;
442 }
443 default:
Steve Block3762c312012-01-06 19:20:56 +0000444 ALOGE("unsupported type in getObjectPropertyValue\n");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400445 result = MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
446 }
447out:
448 env->ReleaseIntArrayElements(objectHandlesArray, objectHandles, 0);
449 env->ReleaseIntArrayElements(propertyCodesArray, propertyCodes, 0);
450 env->ReleaseIntArrayElements(dataTypesArray, dataTypes, 0);
451 if (longValues)
452 env->ReleaseLongArrayElements(longValuesArray, longValues, 0);
453
454 env->DeleteLocalRef(objectHandlesArray);
455 env->DeleteLocalRef(propertyCodesArray);
456 env->DeleteLocalRef(dataTypesArray);
457 if (longValuesArray)
458 env->DeleteLocalRef(longValuesArray);
459 if (stringValuesArray)
460 env->DeleteLocalRef(stringValuesArray);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400461 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400462
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400463 env->DeleteLocalRef(list);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400464 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400465 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400466}
467
Mike Lockwood828d19d2010-08-10 15:20:35 -0400468MtpResponseCode MyMtpDatabase::setObjectPropertyValue(MtpObjectHandle handle,
469 MtpObjectProperty property,
470 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400471 int type;
472
473 if (!getObjectPropertyInfo(property, type))
474 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
475
476 JNIEnv* env = AndroidRuntime::getJNIEnv();
477 jlong longValue = 0;
478 jstring stringValue = NULL;
479
480 switch (type) {
481 case MTP_TYPE_INT8:
482 longValue = packet.getInt8();
483 break;
484 case MTP_TYPE_UINT8:
485 longValue = packet.getUInt8();
486 break;
487 case MTP_TYPE_INT16:
488 longValue = packet.getInt16();
489 break;
490 case MTP_TYPE_UINT16:
491 longValue = packet.getUInt16();
492 break;
493 case MTP_TYPE_INT32:
494 longValue = packet.getInt32();
495 break;
496 case MTP_TYPE_UINT32:
497 longValue = packet.getUInt32();
498 break;
499 case MTP_TYPE_INT64:
500 longValue = packet.getInt64();
501 break;
502 case MTP_TYPE_UINT64:
503 longValue = packet.getUInt64();
504 break;
505 case MTP_TYPE_STR:
506 {
507 MtpStringBuffer buffer;
508 packet.getString(buffer);
509 stringValue = env->NewStringUTF((const char *)buffer);
510 break;
511 }
512 default:
Steve Block3762c312012-01-06 19:20:56 +0000513 ALOGE("unsupported type in getObjectPropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400514 return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
515 }
516
517 jint result = env->CallIntMethod(mDatabase, method_setObjectProperty,
518 (jint)handle, (jint)property, longValue, stringValue);
Mike Lockwood88394712010-09-27 10:01:00 -0400519 if (stringValue)
520 env->DeleteLocalRef(stringValue);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400521
522 checkAndClearExceptionFromCallback(env, __FUNCTION__);
523 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400524}
525
526MtpResponseCode MyMtpDatabase::getDevicePropertyValue(MtpDeviceProperty property,
527 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400528 int type;
529
530 if (!getDevicePropertyInfo(property, type))
531 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
532
533 JNIEnv* env = AndroidRuntime::getJNIEnv();
534 jint result = env->CallIntMethod(mDatabase, method_getDeviceProperty,
535 (jint)property, mLongBuffer, mStringBuffer);
536 if (result != MTP_RESPONSE_OK) {
537 checkAndClearExceptionFromCallback(env, __FUNCTION__);
538 return result;
539 }
540
541 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
542 jlong longValue = longValues[0];
543 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
544
545 switch (type) {
546 case MTP_TYPE_INT8:
547 packet.putInt8(longValue);
548 break;
549 case MTP_TYPE_UINT8:
550 packet.putUInt8(longValue);
551 break;
552 case MTP_TYPE_INT16:
553 packet.putInt16(longValue);
554 break;
555 case MTP_TYPE_UINT16:
556 packet.putUInt16(longValue);
557 break;
558 case MTP_TYPE_INT32:
559 packet.putInt32(longValue);
560 break;
561 case MTP_TYPE_UINT32:
562 packet.putUInt32(longValue);
563 break;
564 case MTP_TYPE_INT64:
565 packet.putInt64(longValue);
566 break;
567 case MTP_TYPE_UINT64:
568 packet.putUInt64(longValue);
569 break;
570 case MTP_TYPE_INT128:
571 packet.putInt128(longValue);
572 break;
573 case MTP_TYPE_UINT128:
574 packet.putInt128(longValue);
575 break;
576 case MTP_TYPE_STR:
577 {
578 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
579 packet.putString(str);
580 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
581 break;
582 }
583 default:
Steve Block3762c312012-01-06 19:20:56 +0000584 ALOGE("unsupported type in getDevicePropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400585 return MTP_RESPONSE_INVALID_DEVICE_PROP_FORMAT;
586 }
587
588 checkAndClearExceptionFromCallback(env, __FUNCTION__);
589 return MTP_RESPONSE_OK;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400590}
591
592MtpResponseCode MyMtpDatabase::setDevicePropertyValue(MtpDeviceProperty property,
593 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400594 int type;
595
596 if (!getDevicePropertyInfo(property, type))
597 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
598
599 JNIEnv* env = AndroidRuntime::getJNIEnv();
600 jlong longValue = 0;
601 jstring stringValue = NULL;
602
603 switch (type) {
604 case MTP_TYPE_INT8:
605 longValue = packet.getInt8();
606 break;
607 case MTP_TYPE_UINT8:
608 longValue = packet.getUInt8();
609 break;
610 case MTP_TYPE_INT16:
611 longValue = packet.getInt16();
612 break;
613 case MTP_TYPE_UINT16:
614 longValue = packet.getUInt16();
615 break;
616 case MTP_TYPE_INT32:
617 longValue = packet.getInt32();
618 break;
619 case MTP_TYPE_UINT32:
620 longValue = packet.getUInt32();
621 break;
622 case MTP_TYPE_INT64:
623 longValue = packet.getInt64();
624 break;
625 case MTP_TYPE_UINT64:
626 longValue = packet.getUInt64();
627 break;
628 case MTP_TYPE_STR:
629 {
630 MtpStringBuffer buffer;
631 packet.getString(buffer);
632 stringValue = env->NewStringUTF((const char *)buffer);
633 break;
634 }
635 default:
Steve Block3762c312012-01-06 19:20:56 +0000636 ALOGE("unsupported type in setDevicePropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400637 return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
638 }
639
640 jint result = env->CallIntMethod(mDatabase, method_setDeviceProperty,
641 (jint)property, longValue, stringValue);
Mike Lockwood88394712010-09-27 10:01:00 -0400642 if (stringValue)
643 env->DeleteLocalRef(stringValue);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400644
645 checkAndClearExceptionFromCallback(env, __FUNCTION__);
646 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400647}
648
Mark Salyzynaeb75fc2014-03-20 12:09:01 -0700649MtpResponseCode MyMtpDatabase::resetDeviceProperty(MtpDeviceProperty /*property*/) {
Mike Lockwood828d19d2010-08-10 15:20:35 -0400650 return -1;
651}
652
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400653MtpResponseCode MyMtpDatabase::getObjectPropertyList(MtpObjectHandle handle,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500654 uint32_t format, uint32_t property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400655 int groupCode, int depth,
656 MtpDataPacket& packet) {
657 JNIEnv* env = AndroidRuntime::getJNIEnv();
658 jobject list = env->CallObjectMethod(mDatabase, method_getObjectPropertyList,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500659 (jlong)handle, (jint)format, (jlong)property, (jint)groupCode, (jint)depth);
660 checkAndClearExceptionFromCallback(env, __FUNCTION__);
661 if (!list)
662 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400663 int count = env->GetIntField(list, field_mCount);
664 MtpResponseCode result = env->GetIntField(list, field_mResult);
665
666 packet.putUInt32(count);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400667 if (count > 0) {
668 jintArray objectHandlesArray = (jintArray)env->GetObjectField(list, field_mObjectHandles);
669 jintArray propertyCodesArray = (jintArray)env->GetObjectField(list, field_mPropertyCodes);
670 jintArray dataTypesArray = (jintArray)env->GetObjectField(list, field_mDataTypes);
671 jlongArray longValuesArray = (jlongArray)env->GetObjectField(list, field_mLongValues);
672 jobjectArray stringValuesArray = (jobjectArray)env->GetObjectField(list, field_mStringValues);
673
674 jint* objectHandles = env->GetIntArrayElements(objectHandlesArray, 0);
675 jint* propertyCodes = env->GetIntArrayElements(propertyCodesArray, 0);
676 jint* dataTypes = env->GetIntArrayElements(dataTypesArray, 0);
677 jlong* longValues = (longValuesArray ? env->GetLongArrayElements(longValuesArray, 0) : NULL);
678
679 for (int i = 0; i < count; i++) {
680 packet.putUInt32(objectHandles[i]);
681 packet.putUInt16(propertyCodes[i]);
682 int type = dataTypes[i];
683 packet.putUInt16(type);
684
685 switch (type) {
686 case MTP_TYPE_INT8:
687 packet.putInt8(longValues[i]);
688 break;
689 case MTP_TYPE_UINT8:
690 packet.putUInt8(longValues[i]);
691 break;
692 case MTP_TYPE_INT16:
693 packet.putInt16(longValues[i]);
694 break;
695 case MTP_TYPE_UINT16:
696 packet.putUInt16(longValues[i]);
697 break;
698 case MTP_TYPE_INT32:
699 packet.putInt32(longValues[i]);
700 break;
701 case MTP_TYPE_UINT32:
702 packet.putUInt32(longValues[i]);
703 break;
704 case MTP_TYPE_INT64:
705 packet.putInt64(longValues[i]);
706 break;
707 case MTP_TYPE_UINT64:
708 packet.putUInt64(longValues[i]);
709 break;
710 case MTP_TYPE_INT128:
711 packet.putInt128(longValues[i]);
712 break;
713 case MTP_TYPE_UINT128:
714 packet.putUInt128(longValues[i]);
715 break;
716 case MTP_TYPE_STR: {
717 jstring value = (jstring)env->GetObjectArrayElement(stringValuesArray, i);
Mike Lockwood2711e492010-12-11 11:24:37 -0800718 const char *valueStr = (value ? env->GetStringUTFChars(value, NULL) : NULL);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400719 if (valueStr) {
720 packet.putString(valueStr);
721 env->ReleaseStringUTFChars(value, valueStr);
722 } else {
723 packet.putEmptyString();
724 }
725 env->DeleteLocalRef(value);
726 break;
727 }
728 default:
Steve Block3762c312012-01-06 19:20:56 +0000729 ALOGE("bad or unsupported data type in MyMtpDatabase::getObjectPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400730 break;
731 }
732 }
733
734 env->ReleaseIntArrayElements(objectHandlesArray, objectHandles, 0);
735 env->ReleaseIntArrayElements(propertyCodesArray, propertyCodes, 0);
736 env->ReleaseIntArrayElements(dataTypesArray, dataTypes, 0);
737 if (longValues)
738 env->ReleaseLongArrayElements(longValuesArray, longValues, 0);
739
740 env->DeleteLocalRef(objectHandlesArray);
741 env->DeleteLocalRef(propertyCodesArray);
742 env->DeleteLocalRef(dataTypesArray);
743 if (longValuesArray)
744 env->DeleteLocalRef(longValuesArray);
745 if (stringValuesArray)
746 env->DeleteLocalRef(stringValuesArray);
747 }
748
749 env->DeleteLocalRef(list);
750 checkAndClearExceptionFromCallback(env, __FUNCTION__);
751 return result;
752}
753
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400754MtpResponseCode MyMtpDatabase::getObjectInfo(MtpObjectHandle handle,
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700755 MtpObjectInfo& info) {
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700756 char date[20];
757 MtpString path;
758 int64_t length;
759 MtpObjectFormat format;
760
761 MtpResponseCode result = getObjectFilePath(handle, path, length, format);
762 if (result != MTP_RESPONSE_OK) {
763 return result;
764 }
765 info.mCompressedSize = (length > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)length);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400766
767 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700768 if (!env->CallBooleanMethod(mDatabase, method_getObjectInfo,
769 (jint)handle, mIntBuffer, mStringBuffer, mLongBuffer)) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400770 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700771 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400772
773 jint* intValues = env->GetIntArrayElements(mIntBuffer, 0);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700774 info.mStorageID = intValues[0];
775 info.mFormat = intValues[1];
776 info.mParent = intValues[2];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400777 env->ReleaseIntArrayElements(mIntBuffer, intValues, 0);
778
779 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700780 info.mDateCreated = longValues[0];
781 info.mDateModified = longValues[1];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400782 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
783
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700784// info.mAssociationType = (format == MTP_FORMAT_ASSOCIATION ?
Mike Lockwood828d19d2010-08-10 15:20:35 -0400785// MTP_ASSOCIATION_TYPE_GENERIC_FOLDER :
786// MTP_ASSOCIATION_TYPE_UNDEFINED);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700787 info.mAssociationType = MTP_ASSOCIATION_TYPE_UNDEFINED;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400788
789 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700790 MtpString temp(str);
791 info.mName = strdup((const char *)temp);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400792 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
793
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700794 // read EXIF data for thumbnail information
795 if (info.mFormat == MTP_FORMAT_EXIF_JPEG || info.mFormat == MTP_FORMAT_JFIF) {
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700796 ResetJpgfile();
797 // Start with an empty image information structure.
798 memset(&ImageInfo, 0, sizeof(ImageInfo));
799 ImageInfo.FlashUsed = -1;
800 ImageInfo.MeteringMode = -1;
801 ImageInfo.Whitebalance = -1;
802 strncpy(ImageInfo.FileName, (const char *)path, PATH_MAX);
803 if (ReadJpegFile((const char*)path, READ_METADATA)) {
804 Section_t* section = FindSection(M_EXIF);
805 if (section) {
806 info.mThumbCompressedSize = ImageInfo.ThumbnailSize;
807 info.mThumbFormat = MTP_FORMAT_EXIF_JPEG;
808 info.mImagePixWidth = ImageInfo.Width;
809 info.mImagePixHeight = ImageInfo.Height;
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700810 }
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700811 }
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700812 DiscardData();
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700813 }
814
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400815 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400816 return MTP_RESPONSE_OK;
817}
818
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700819void* MyMtpDatabase::getThumbnail(MtpObjectHandle handle, size_t& outThumbSize) {
820 MtpString path;
821 int64_t length;
822 MtpObjectFormat format;
823 void* result = NULL;
824 outThumbSize = 0;
825
826 if (getObjectFilePath(handle, path, length, format) == MTP_RESPONSE_OK
827 && (format == MTP_FORMAT_EXIF_JPEG || format == MTP_FORMAT_JFIF)) {
828 ResetJpgfile();
829 // Start with an empty image information structure.
830 memset(&ImageInfo, 0, sizeof(ImageInfo));
831 ImageInfo.FlashUsed = -1;
832 ImageInfo.MeteringMode = -1;
833 ImageInfo.Whitebalance = -1;
834 strncpy(ImageInfo.FileName, (const char *)path, PATH_MAX);
835 if (ReadJpegFile((const char*)path, READ_METADATA)) {
836 Section_t* section = FindSection(M_EXIF);
837 if (section) {
838 outThumbSize = ImageInfo.ThumbnailSize;
839 result = malloc(outThumbSize);
840 if (result)
841 memcpy(result, section->Data + ImageInfo.ThumbnailOffset + 8, outThumbSize);
842 }
843 DiscardData();
844 }
845 }
846
847 return result;
848}
849
Mike Lockwood59c777a2010-08-02 10:37:41 -0400850MtpResponseCode MyMtpDatabase::getObjectFilePath(MtpObjectHandle handle,
Mike Lockwood365e03e2010-12-08 16:08:01 -0800851 MtpString& outFilePath,
852 int64_t& outFileLength,
853 MtpObjectFormat& outFormat) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400854 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood59c777a2010-08-02 10:37:41 -0400855 jint result = env->CallIntMethod(mDatabase, method_getObjectFilePath,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400856 (jint)handle, mStringBuffer, mLongBuffer);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400857 if (result != MTP_RESPONSE_OK) {
858 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400859 return result;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400860 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400861
862 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
Mike Lockwood365e03e2010-12-08 16:08:01 -0800863 outFilePath.setTo(str, strlen16(str));
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400864 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
865
866 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
Mike Lockwood365e03e2010-12-08 16:08:01 -0800867 outFileLength = longValues[0];
868 outFormat = longValues[1];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400869 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700870
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400871 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400872 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400873}
874
Mike Lockwood59c777a2010-08-02 10:37:41 -0400875MtpResponseCode MyMtpDatabase::deleteFile(MtpObjectHandle handle) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400876 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400877 MtpResponseCode result = env->CallIntMethod(mDatabase, method_deleteFile, (jint)handle);
878
879 checkAndClearExceptionFromCallback(env, __FUNCTION__);
880 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400881}
882
883struct PropertyTableEntry {
884 MtpObjectProperty property;
885 int type;
886};
887
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400888static const PropertyTableEntry kObjectPropertyTable[] = {
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400889 { MTP_PROPERTY_STORAGE_ID, MTP_TYPE_UINT32 },
890 { MTP_PROPERTY_OBJECT_FORMAT, MTP_TYPE_UINT16 },
891 { MTP_PROPERTY_PROTECTION_STATUS, MTP_TYPE_UINT16 },
892 { MTP_PROPERTY_OBJECT_SIZE, MTP_TYPE_UINT64 },
893 { MTP_PROPERTY_OBJECT_FILE_NAME, MTP_TYPE_STR },
894 { MTP_PROPERTY_DATE_MODIFIED, MTP_TYPE_STR },
895 { MTP_PROPERTY_PARENT_OBJECT, MTP_TYPE_UINT32 },
896 { MTP_PROPERTY_PERSISTENT_UID, MTP_TYPE_UINT128 },
897 { MTP_PROPERTY_NAME, MTP_TYPE_STR },
Mike Lockwoodae078f72010-09-26 12:35:51 -0400898 { MTP_PROPERTY_DISPLAY_NAME, MTP_TYPE_STR },
899 { MTP_PROPERTY_DATE_ADDED, MTP_TYPE_STR },
900 { MTP_PROPERTY_ARTIST, MTP_TYPE_STR },
901 { MTP_PROPERTY_ALBUM_NAME, MTP_TYPE_STR },
902 { MTP_PROPERTY_ALBUM_ARTIST, MTP_TYPE_STR },
903 { MTP_PROPERTY_TRACK, MTP_TYPE_UINT16 },
904 { MTP_PROPERTY_ORIGINAL_RELEASE_DATE, MTP_TYPE_STR },
905 { MTP_PROPERTY_GENRE, MTP_TYPE_STR },
906 { MTP_PROPERTY_COMPOSER, MTP_TYPE_STR },
907 { MTP_PROPERTY_DURATION, MTP_TYPE_UINT32 },
908 { MTP_PROPERTY_DESCRIPTION, MTP_TYPE_STR },
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400909};
910
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400911static const PropertyTableEntry kDevicePropertyTable[] = {
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800912 { MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER, MTP_TYPE_STR },
913 { MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME, MTP_TYPE_STR },
914 { MTP_DEVICE_PROPERTY_IMAGE_SIZE, MTP_TYPE_STR },
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400915};
916
917bool MyMtpDatabase::getObjectPropertyInfo(MtpObjectProperty property, int& type) {
918 int count = sizeof(kObjectPropertyTable) / sizeof(kObjectPropertyTable[0]);
919 const PropertyTableEntry* entry = kObjectPropertyTable;
920 for (int i = 0; i < count; i++, entry++) {
921 if (entry->property == property) {
922 type = entry->type;
923 return true;
924 }
925 }
926 return false;
927}
928
929bool MyMtpDatabase::getDevicePropertyInfo(MtpDeviceProperty property, int& type) {
930 int count = sizeof(kDevicePropertyTable) / sizeof(kDevicePropertyTable[0]);
931 const PropertyTableEntry* entry = kDevicePropertyTable;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400932 for (int i = 0; i < count; i++, entry++) {
933 if (entry->property == property) {
934 type = entry->type;
935 return true;
936 }
937 }
938 return false;
939}
940
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400941MtpObjectHandleList* MyMtpDatabase::getObjectReferences(MtpObjectHandle handle) {
942 JNIEnv* env = AndroidRuntime::getJNIEnv();
943 jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectReferences,
944 (jint)handle);
945 if (!array)
946 return NULL;
947 MtpObjectHandleList* list = new MtpObjectHandleList();
948 jint* handles = env->GetIntArrayElements(array, 0);
949 jsize length = env->GetArrayLength(array);
950 for (int i = 0; i < length; i++)
951 list->push(handles[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400952 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400953 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400954
955 checkAndClearExceptionFromCallback(env, __FUNCTION__);
956 return list;
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400957}
958
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400959MtpResponseCode MyMtpDatabase::setObjectReferences(MtpObjectHandle handle,
960 MtpObjectHandleList* references) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400961 JNIEnv* env = AndroidRuntime::getJNIEnv();
962 int count = references->size();
963 jintArray array = env->NewIntArray(count);
964 if (!array) {
Steve Block3762c312012-01-06 19:20:56 +0000965 ALOGE("out of memory in setObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400966 return false;
967 }
968 jint* handles = env->GetIntArrayElements(array, 0);
969 for (int i = 0; i < count; i++)
970 handles[i] = (*references)[i];
971 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400972 MtpResponseCode result = env->CallIntMethod(mDatabase, method_setObjectReferences,
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400973 (jint)handle, array);
Mike Lockwood88394712010-09-27 10:01:00 -0400974 env->DeleteLocalRef(array);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400975
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400976 checkAndClearExceptionFromCallback(env, __FUNCTION__);
977 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400978}
979
Mike Lockwood828d19d2010-08-10 15:20:35 -0400980MtpProperty* MyMtpDatabase::getObjectPropertyDesc(MtpObjectProperty property,
981 MtpObjectFormat format) {
982 MtpProperty* result = NULL;
983 switch (property) {
984 case MTP_PROPERTY_OBJECT_FORMAT:
Mike Lockwood9b5e9c42010-12-07 18:53:50 -0800985 // use format as default value
986 result = new MtpProperty(property, MTP_TYPE_UINT16, false, format);
987 break;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400988 case MTP_PROPERTY_PROTECTION_STATUS:
Mike Lockwoodae078f72010-09-26 12:35:51 -0400989 case MTP_PROPERTY_TRACK:
Mike Lockwood828d19d2010-08-10 15:20:35 -0400990 result = new MtpProperty(property, MTP_TYPE_UINT16);
991 break;
992 case MTP_PROPERTY_STORAGE_ID:
993 case MTP_PROPERTY_PARENT_OBJECT:
Mike Lockwoodae078f72010-09-26 12:35:51 -0400994 case MTP_PROPERTY_DURATION:
Mike Lockwood828d19d2010-08-10 15:20:35 -0400995 result = new MtpProperty(property, MTP_TYPE_UINT32);
996 break;
997 case MTP_PROPERTY_OBJECT_SIZE:
998 result = new MtpProperty(property, MTP_TYPE_UINT64);
999 break;
1000 case MTP_PROPERTY_PERSISTENT_UID:
1001 result = new MtpProperty(property, MTP_TYPE_UINT128);
1002 break;
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -04001003 case MTP_PROPERTY_NAME:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001004 case MTP_PROPERTY_DISPLAY_NAME:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001005 case MTP_PROPERTY_ARTIST:
1006 case MTP_PROPERTY_ALBUM_NAME:
1007 case MTP_PROPERTY_ALBUM_ARTIST:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001008 case MTP_PROPERTY_GENRE:
1009 case MTP_PROPERTY_COMPOSER:
1010 case MTP_PROPERTY_DESCRIPTION:
Mike Lockwood828d19d2010-08-10 15:20:35 -04001011 result = new MtpProperty(property, MTP_TYPE_STR);
1012 break;
Mike Lockwood5b19af02010-11-23 18:38:55 -05001013 case MTP_PROPERTY_DATE_MODIFIED:
1014 case MTP_PROPERTY_DATE_ADDED:
1015 case MTP_PROPERTY_ORIGINAL_RELEASE_DATE:
1016 result = new MtpProperty(property, MTP_TYPE_STR);
1017 result->setFormDateTime();
1018 break;
Mike Lockwood5ebac832010-10-12 11:33:47 -04001019 case MTP_PROPERTY_OBJECT_FILE_NAME:
Mike Lockwood6a6a3af2010-10-12 14:19:51 -04001020 // We allow renaming files and folders
1021 result = new MtpProperty(property, MTP_TYPE_STR, true);
Mike Lockwood5ebac832010-10-12 11:33:47 -04001022 break;
Mike Lockwood828d19d2010-08-10 15:20:35 -04001023 }
1024
1025 return result;
1026}
1027
1028MtpProperty* MyMtpDatabase::getDevicePropertyDesc(MtpDeviceProperty property) {
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001029 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001030 MtpProperty* result = NULL;
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001031 bool writable = false;
1032
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001033 switch (property) {
1034 case MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
1035 case MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001036 writable = true;
1037 // fall through
1038 case MTP_DEVICE_PROPERTY_IMAGE_SIZE:
1039 result = new MtpProperty(property, MTP_TYPE_STR, writable);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001040
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001041 // get current value
Mike Lockwooda2a21282010-09-25 21:21:05 -04001042 jint ret = env->CallIntMethod(mDatabase, method_getDeviceProperty,
1043 (jint)property, mLongBuffer, mStringBuffer);
1044 if (ret == MTP_RESPONSE_OK) {
1045 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
1046 result->setCurrentValue(str);
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001047 // for read-only properties it is safe to assume current value is default value
1048 if (!writable)
1049 result->setDefaultValue(str);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001050 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
1051 } else {
Steve Block3762c312012-01-06 19:20:56 +00001052 ALOGE("unable to read device property, response: %04X", ret);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001053 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001054 break;
1055 }
1056
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001057 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001058 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -04001059}
1060
Mike Lockwood2837eef2010-08-31 16:25:12 -04001061void MyMtpDatabase::sessionStarted() {
1062 JNIEnv* env = AndroidRuntime::getJNIEnv();
1063 env->CallVoidMethod(mDatabase, method_sessionStarted);
1064 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1065}
1066
1067void MyMtpDatabase::sessionEnded() {
1068 JNIEnv* env = AndroidRuntime::getJNIEnv();
1069 env->CallVoidMethod(mDatabase, method_sessionEnded);
1070 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1071}
1072
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001073// ----------------------------------------------------------------------------
1074
1075static void
Mike Lockwood0cd01362010-12-30 11:54:33 -05001076android_mtp_MtpDatabase_setup(JNIEnv *env, jobject thiz)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001077{
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001078 MyMtpDatabase* database = new MyMtpDatabase(env, thiz);
Ashok Bhate2e59322013-12-17 19:04:19 +00001079 env->SetLongField(thiz, field_context, (jlong)database);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001080 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1081}
1082
1083static void
Mike Lockwood0cd01362010-12-30 11:54:33 -05001084android_mtp_MtpDatabase_finalize(JNIEnv *env, jobject thiz)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001085{
Ashok Bhate2e59322013-12-17 19:04:19 +00001086 MyMtpDatabase* database = (MyMtpDatabase *)env->GetLongField(thiz, field_context);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001087 database->cleanup(env);
1088 delete database;
Ashok Bhate2e59322013-12-17 19:04:19 +00001089 env->SetLongField(thiz, field_context, 0);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001090 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1091}
1092
Mike Lockwood31599912010-11-15 13:43:30 -05001093static jstring
Mark Salyzynaeb75fc2014-03-20 12:09:01 -07001094android_mtp_MtpPropertyGroup_format_date_time(JNIEnv *env, jobject /*thiz*/, jlong seconds)
Mike Lockwood31599912010-11-15 13:43:30 -05001095{
Mike Lockwood31599912010-11-15 13:43:30 -05001096 char date[20];
1097 formatDateTime(seconds, date, sizeof(date));
1098 return env->NewStringUTF(date);
Mike Lockwood31599912010-11-15 13:43:30 -05001099}
1100
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001101// ----------------------------------------------------------------------------
1102
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001103static JNINativeMethod gMtpDatabaseMethods[] = {
Mike Lockwood0cd01362010-12-30 11:54:33 -05001104 {"native_setup", "()V", (void *)android_mtp_MtpDatabase_setup},
1105 {"native_finalize", "()V", (void *)android_mtp_MtpDatabase_finalize},
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001106};
1107
1108static JNINativeMethod gMtpPropertyGroupMethods[] = {
Mike Lockwood31599912010-11-15 13:43:30 -05001109 {"format_date_time", "(J)Ljava/lang/String;",
Mike Lockwood0cd01362010-12-30 11:54:33 -05001110 (void *)android_mtp_MtpPropertyGroup_format_date_time},
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001111};
1112
Mike Lockwood0cd01362010-12-30 11:54:33 -05001113static const char* const kClassPathName = "android/mtp/MtpDatabase";
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001114
Mike Lockwood0cd01362010-12-30 11:54:33 -05001115int register_android_mtp_MtpDatabase(JNIEnv *env)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001116{
1117 jclass clazz;
1118
Mike Lockwood0cd01362010-12-30 11:54:33 -05001119 clazz = env->FindClass("android/mtp/MtpDatabase");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001120 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001121 ALOGE("Can't find android/mtp/MtpDatabase");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001122 return -1;
1123 }
Mike Lockwoodd815f792010-07-12 08:49:01 -04001124 method_beginSendObject = env->GetMethodID(clazz, "beginSendObject", "(Ljava/lang/String;IIIJJ)I");
1125 if (method_beginSendObject == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001126 ALOGE("Can't find beginSendObject");
Mike Lockwoodd815f792010-07-12 08:49:01 -04001127 return -1;
1128 }
Mike Lockwood7a0bd172011-01-18 11:06:19 -08001129 method_endSendObject = env->GetMethodID(clazz, "endSendObject", "(Ljava/lang/String;IIZ)V");
Mike Lockwoodd815f792010-07-12 08:49:01 -04001130 if (method_endSendObject == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001131 ALOGE("Can't find endSendObject");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001132 return -1;
1133 }
1134 method_getObjectList = env->GetMethodID(clazz, "getObjectList", "(III)[I");
1135 if (method_getObjectList == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001136 ALOGE("Can't find getObjectList");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001137 return -1;
1138 }
Mike Lockwood7a047c82010-08-02 10:52:20 -04001139 method_getNumObjects = env->GetMethodID(clazz, "getNumObjects", "(III)I");
1140 if (method_getNumObjects == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001141 ALOGE("Can't find getNumObjects");
Mike Lockwood7a047c82010-08-02 10:52:20 -04001142 return -1;
1143 }
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001144 method_getSupportedPlaybackFormats = env->GetMethodID(clazz, "getSupportedPlaybackFormats", "()[I");
1145 if (method_getSupportedPlaybackFormats == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001146 ALOGE("Can't find getSupportedPlaybackFormats");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001147 return -1;
1148 }
1149 method_getSupportedCaptureFormats = env->GetMethodID(clazz, "getSupportedCaptureFormats", "()[I");
1150 if (method_getSupportedCaptureFormats == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001151 ALOGE("Can't find getSupportedCaptureFormats");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001152 return -1;
1153 }
1154 method_getSupportedObjectProperties = env->GetMethodID(clazz, "getSupportedObjectProperties", "(I)[I");
1155 if (method_getSupportedObjectProperties == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001156 ALOGE("Can't find getSupportedObjectProperties");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001157 return -1;
1158 }
1159 method_getSupportedDeviceProperties = env->GetMethodID(clazz, "getSupportedDeviceProperties", "()[I");
1160 if (method_getSupportedDeviceProperties == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001161 ALOGE("Can't find getSupportedDeviceProperties");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001162 return -1;
1163 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001164 method_setObjectProperty = env->GetMethodID(clazz, "setObjectProperty", "(IIJLjava/lang/String;)I");
1165 if (method_setObjectProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001166 ALOGE("Can't find setObjectProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001167 return -1;
1168 }
1169 method_getDeviceProperty = env->GetMethodID(clazz, "getDeviceProperty", "(I[J[C)I");
1170 if (method_getDeviceProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001171 ALOGE("Can't find getDeviceProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001172 return -1;
1173 }
1174 method_setDeviceProperty = env->GetMethodID(clazz, "setDeviceProperty", "(IJLjava/lang/String;)I");
1175 if (method_setDeviceProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001176 ALOGE("Can't find setDeviceProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001177 return -1;
1178 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001179 method_getObjectPropertyList = env->GetMethodID(clazz, "getObjectPropertyList",
Mike Lockwood0cd01362010-12-30 11:54:33 -05001180 "(JIJII)Landroid/mtp/MtpPropertyList;");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001181 if (method_getObjectPropertyList == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001182 ALOGE("Can't find getObjectPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001183 return -1;
1184 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001185 method_getObjectInfo = env->GetMethodID(clazz, "getObjectInfo", "(I[I[C[J)Z");
1186 if (method_getObjectInfo == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001187 ALOGE("Can't find getObjectInfo");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001188 return -1;
1189 }
Mike Lockwood59c777a2010-08-02 10:37:41 -04001190 method_getObjectFilePath = env->GetMethodID(clazz, "getObjectFilePath", "(I[C[J)I");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001191 if (method_getObjectFilePath == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001192 ALOGE("Can't find getObjectFilePath");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001193 return -1;
1194 }
Mike Lockwood59c777a2010-08-02 10:37:41 -04001195 method_deleteFile = env->GetMethodID(clazz, "deleteFile", "(I)I");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001196 if (method_deleteFile == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001197 ALOGE("Can't find deleteFile");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001198 return -1;
1199 }
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001200 method_getObjectReferences = env->GetMethodID(clazz, "getObjectReferences", "(I)[I");
1201 if (method_getObjectReferences == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001202 ALOGE("Can't find getObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001203 return -1;
1204 }
1205 method_setObjectReferences = env->GetMethodID(clazz, "setObjectReferences", "(I[I)I");
1206 if (method_setObjectReferences == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001207 ALOGE("Can't find setObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001208 return -1;
1209 }
Mike Lockwood2837eef2010-08-31 16:25:12 -04001210 method_sessionStarted = env->GetMethodID(clazz, "sessionStarted", "()V");
1211 if (method_sessionStarted == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001212 ALOGE("Can't find sessionStarted");
Mike Lockwood2837eef2010-08-31 16:25:12 -04001213 return -1;
1214 }
1215 method_sessionEnded = env->GetMethodID(clazz, "sessionEnded", "()V");
1216 if (method_sessionEnded == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001217 ALOGE("Can't find sessionEnded");
Mike Lockwood2837eef2010-08-31 16:25:12 -04001218 return -1;
1219 }
1220
Ashok Bhate2e59322013-12-17 19:04:19 +00001221 field_context = env->GetFieldID(clazz, "mNativeContext", "J");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001222 if (field_context == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001223 ALOGE("Can't find MtpDatabase.mNativeContext");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001224 return -1;
1225 }
1226
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001227 // now set up fields for MtpPropertyList class
Mike Lockwood0cd01362010-12-30 11:54:33 -05001228 clazz = env->FindClass("android/mtp/MtpPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001229 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001230 ALOGE("Can't find android/mtp/MtpPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001231 return -1;
1232 }
1233 field_mCount = env->GetFieldID(clazz, "mCount", "I");
1234 if (field_mCount == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001235 ALOGE("Can't find MtpPropertyList.mCount");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001236 return -1;
1237 }
1238 field_mResult = env->GetFieldID(clazz, "mResult", "I");
1239 if (field_mResult == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001240 ALOGE("Can't find MtpPropertyList.mResult");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001241 return -1;
1242 }
1243 field_mObjectHandles = env->GetFieldID(clazz, "mObjectHandles", "[I");
1244 if (field_mObjectHandles == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001245 ALOGE("Can't find MtpPropertyList.mObjectHandles");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001246 return -1;
1247 }
1248 field_mPropertyCodes = env->GetFieldID(clazz, "mPropertyCodes", "[I");
1249 if (field_mPropertyCodes == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001250 ALOGE("Can't find MtpPropertyList.mPropertyCodes");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001251 return -1;
1252 }
1253 field_mDataTypes = env->GetFieldID(clazz, "mDataTypes", "[I");
1254 if (field_mDataTypes == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001255 ALOGE("Can't find MtpPropertyList.mDataTypes");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001256 return -1;
1257 }
1258 field_mLongValues = env->GetFieldID(clazz, "mLongValues", "[J");
1259 if (field_mLongValues == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001260 ALOGE("Can't find MtpPropertyList.mLongValues");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001261 return -1;
1262 }
1263 field_mStringValues = env->GetFieldID(clazz, "mStringValues", "[Ljava/lang/String;");
1264 if (field_mStringValues == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001265 ALOGE("Can't find MtpPropertyList.mStringValues");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001266 return -1;
1267 }
1268
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001269 if (AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -05001270 "android/mtp/MtpDatabase", gMtpDatabaseMethods, NELEM(gMtpDatabaseMethods)))
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001271 return -1;
1272
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001273 return AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -05001274 "android/mtp/MtpPropertyGroup", gMtpPropertyGroupMethods, NELEM(gMtpPropertyGroupMethods));
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001275}