blob: ea75a18a9628c1ff8220ab10ff5379738f7bfab3 [file] [log] [blame]
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "MtpDatabaseJNI"
18#include "utils/Log.h"
19
20#include <stdio.h>
21#include <assert.h>
22#include <limits.h>
23#include <unistd.h>
24#include <fcntl.h>
25
26#include "jni.h"
27#include "JNIHelp.h"
28#include "android_runtime/AndroidRuntime.h"
Ruben Brunk87eac992013-09-09 17:44:59 -070029#include "android_runtime/Log.h"
Mike Lockwoodd21eac92010-07-03 00:44:05 -040030
31#include "MtpDatabase.h"
32#include "MtpDataPacket.h"
Mike Lockwood9df53fae2011-04-21 17:05:55 -070033#include "MtpObjectInfo.h"
Mike Lockwood828d19d2010-08-10 15:20:35 -040034#include "MtpProperty.h"
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040035#include "MtpStringBuffer.h"
Mike Lockwoodd21eac92010-07-03 00:44:05 -040036#include "MtpUtils.h"
37#include "mtp.h"
38
Mike Lockwoodc89f2222011-04-24 18:40:17 -070039extern "C" {
Marco Nelissen3cd393c2014-01-10 10:39:27 -080040#include "libexif/exif-content.h"
41#include "libexif/exif-data.h"
42#include "libexif/exif-tag.h"
43#include "libexif/exif-utils.h"
Mike Lockwoodc89f2222011-04-24 18:40:17 -070044}
45
Mike Lockwoodd21eac92010-07-03 00:44:05 -040046using namespace android;
47
48// ----------------------------------------------------------------------------
49
Mike Lockwoodd815f792010-07-12 08:49:01 -040050static jmethodID method_beginSendObject;
51static jmethodID method_endSendObject;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040052static jmethodID method_getObjectList;
Mike Lockwood7a047c82010-08-02 10:52:20 -040053static jmethodID method_getNumObjects;
Mike Lockwood4b322ce2010-08-10 07:37:50 -040054static jmethodID method_getSupportedPlaybackFormats;
55static jmethodID method_getSupportedCaptureFormats;
56static jmethodID method_getSupportedObjectProperties;
57static jmethodID method_getSupportedDeviceProperties;
Mike Lockwood828d19d2010-08-10 15:20:35 -040058static jmethodID method_setObjectProperty;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040059static jmethodID method_getDeviceProperty;
60static jmethodID method_setDeviceProperty;
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -040061static jmethodID method_getObjectPropertyList;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040062static jmethodID method_getObjectInfo;
63static jmethodID method_getObjectFilePath;
64static jmethodID method_deleteFile;
Mike Lockwood9a2046f2010-08-03 15:30:09 -040065static jmethodID method_getObjectReferences;
66static jmethodID method_setObjectReferences;
Mike Lockwood2837eef2010-08-31 16:25:12 -040067static jmethodID method_sessionStarted;
68static jmethodID method_sessionEnded;
69
Mike Lockwoodd21eac92010-07-03 00:44:05 -040070static jfieldID field_context;
Mike Lockwood56c85242014-03-07 13:29:08 -080071static jfieldID field_batteryLevel;
72static jfieldID field_batteryScale;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040073
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -040074// MtpPropertyList fields
75static jfieldID field_mCount;
76static jfieldID field_mResult;
77static jfieldID field_mObjectHandles;
78static jfieldID field_mPropertyCodes;
79static jfieldID field_mDataTypes;
80static jfieldID field_mLongValues;
81static jfieldID field_mStringValues;
82
83
Mike Lockwoodd21eac92010-07-03 00:44:05 -040084MtpDatabase* getMtpDatabase(JNIEnv *env, jobject database) {
Ashok Bhate2e59322013-12-17 19:04:19 +000085 return (MtpDatabase *)env->GetLongField(database, field_context);
Mike Lockwoodd21eac92010-07-03 00:44:05 -040086}
87
88// ----------------------------------------------------------------------------
89
90class MyMtpDatabase : public MtpDatabase {
91private:
92 jobject mDatabase;
93 jintArray mIntBuffer;
94 jlongArray mLongBuffer;
95 jcharArray mStringBuffer;
96
97public:
98 MyMtpDatabase(JNIEnv *env, jobject client);
99 virtual ~MyMtpDatabase();
100 void cleanup(JNIEnv *env);
101
Mike Lockwoodd815f792010-07-12 08:49:01 -0400102 virtual MtpObjectHandle beginSendObject(const char* path,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400103 MtpObjectFormat format,
104 MtpObjectHandle parent,
105 MtpStorageID storage,
106 uint64_t size,
107 time_t modified);
108
Mike Lockwoodd815f792010-07-12 08:49:01 -0400109 virtual void endSendObject(const char* path,
110 MtpObjectHandle handle,
111 MtpObjectFormat format,
112 bool succeeded);
113
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400114 virtual MtpObjectHandleList* getObjectList(MtpStorageID storageID,
115 MtpObjectFormat format,
116 MtpObjectHandle parent);
117
Mike Lockwood7a047c82010-08-02 10:52:20 -0400118 virtual int getNumObjects(MtpStorageID storageID,
119 MtpObjectFormat format,
120 MtpObjectHandle parent);
121
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400122 // callee should delete[] the results from these
123 // results can be NULL
124 virtual MtpObjectFormatList* getSupportedPlaybackFormats();
125 virtual MtpObjectFormatList* getSupportedCaptureFormats();
126 virtual MtpObjectPropertyList* getSupportedObjectProperties(MtpObjectFormat format);
127 virtual MtpDevicePropertyList* getSupportedDeviceProperties();
128
Mike Lockwood828d19d2010-08-10 15:20:35 -0400129 virtual MtpResponseCode getObjectPropertyValue(MtpObjectHandle handle,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400130 MtpObjectProperty property,
131 MtpDataPacket& packet);
132
Mike Lockwood828d19d2010-08-10 15:20:35 -0400133 virtual MtpResponseCode setObjectPropertyValue(MtpObjectHandle handle,
134 MtpObjectProperty property,
135 MtpDataPacket& packet);
136
137 virtual MtpResponseCode getDevicePropertyValue(MtpDeviceProperty property,
138 MtpDataPacket& packet);
139
140 virtual MtpResponseCode setDevicePropertyValue(MtpDeviceProperty property,
141 MtpDataPacket& packet);
142
143 virtual MtpResponseCode resetDeviceProperty(MtpDeviceProperty property);
144
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400145 virtual MtpResponseCode getObjectPropertyList(MtpObjectHandle handle,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500146 uint32_t format, uint32_t property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400147 int groupCode, int depth,
148 MtpDataPacket& packet);
149
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400150 virtual MtpResponseCode getObjectInfo(MtpObjectHandle handle,
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700151 MtpObjectInfo& info);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400152
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700153 virtual void* getThumbnail(MtpObjectHandle handle, size_t& outThumbSize);
154
Mike Lockwood59c777a2010-08-02 10:37:41 -0400155 virtual MtpResponseCode getObjectFilePath(MtpObjectHandle handle,
Mike Lockwood365e03e2010-12-08 16:08:01 -0800156 MtpString& outFilePath,
157 int64_t& outFileLength,
158 MtpObjectFormat& outFormat);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400159 virtual MtpResponseCode deleteFile(MtpObjectHandle handle);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400160
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400161 bool getObjectPropertyInfo(MtpObjectProperty property, int& type);
162 bool getDevicePropertyInfo(MtpDeviceProperty property, int& type);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400163
164 virtual MtpObjectHandleList* getObjectReferences(MtpObjectHandle handle);
165
166 virtual MtpResponseCode setObjectReferences(MtpObjectHandle handle,
167 MtpObjectHandleList* references);
Mike Lockwood828d19d2010-08-10 15:20:35 -0400168
169 virtual MtpProperty* getObjectPropertyDesc(MtpObjectProperty property,
170 MtpObjectFormat format);
171
172 virtual MtpProperty* getDevicePropertyDesc(MtpDeviceProperty property);
Mike Lockwood2837eef2010-08-31 16:25:12 -0400173
174 virtual void sessionStarted();
175
176 virtual void sessionEnded();
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400177};
178
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400179// ----------------------------------------------------------------------------
180
181static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
182 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000183 ALOGE("An exception was thrown by callback '%s'.", methodName);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400184 LOGE_EX(env);
185 env->ExceptionClear();
186 }
187}
188
189// ----------------------------------------------------------------------------
190
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400191MyMtpDatabase::MyMtpDatabase(JNIEnv *env, jobject client)
192 : mDatabase(env->NewGlobalRef(client)),
193 mIntBuffer(NULL),
194 mLongBuffer(NULL),
195 mStringBuffer(NULL)
196{
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400197 // create buffers for out arguments
198 // we don't need to be thread-safe so this is OK
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700199 jintArray intArray = env->NewIntArray(3);
200 if (!intArray) {
201 return; // Already threw.
202 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400203 mIntBuffer = (jintArray)env->NewGlobalRef(intArray);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700204 jlongArray longArray = env->NewLongArray(2);
205 if (!longArray) {
206 return; // Already threw.
207 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400208 mLongBuffer = (jlongArray)env->NewGlobalRef(longArray);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700209 jcharArray charArray = env->NewCharArray(256);
210 if (!charArray) {
211 return; // Already threw.
212 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400213 mStringBuffer = (jcharArray)env->NewGlobalRef(charArray);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400214}
215
216void MyMtpDatabase::cleanup(JNIEnv *env) {
217 env->DeleteGlobalRef(mDatabase);
218 env->DeleteGlobalRef(mIntBuffer);
219 env->DeleteGlobalRef(mLongBuffer);
220 env->DeleteGlobalRef(mStringBuffer);
221}
222
223MyMtpDatabase::~MyMtpDatabase() {
224}
225
Mike Lockwoodd815f792010-07-12 08:49:01 -0400226MtpObjectHandle MyMtpDatabase::beginSendObject(const char* path,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400227 MtpObjectFormat format,
228 MtpObjectHandle parent,
229 MtpStorageID storage,
230 uint64_t size,
231 time_t modified) {
232 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood88394712010-09-27 10:01:00 -0400233 jstring pathStr = env->NewStringUTF(path);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400234 MtpObjectHandle result = env->CallIntMethod(mDatabase, method_beginSendObject,
Mike Lockwood88394712010-09-27 10:01:00 -0400235 pathStr, (jint)format, (jint)parent, (jint)storage,
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400236 (jlong)size, (jlong)modified);
237
Mike Lockwood88394712010-09-27 10:01:00 -0400238 if (pathStr)
239 env->DeleteLocalRef(pathStr);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400240 checkAndClearExceptionFromCallback(env, __FUNCTION__);
241 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400242}
243
Mike Lockwoodd815f792010-07-12 08:49:01 -0400244void MyMtpDatabase::endSendObject(const char* path, MtpObjectHandle handle,
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800245 MtpObjectFormat format, bool succeeded) {
Mike Lockwoodd815f792010-07-12 08:49:01 -0400246 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood88394712010-09-27 10:01:00 -0400247 jstring pathStr = env->NewStringUTF(path);
248 env->CallVoidMethod(mDatabase, method_endSendObject, pathStr,
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800249 (jint)handle, (jint)format, (jboolean)succeeded);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400250
Mike Lockwood88394712010-09-27 10:01:00 -0400251 if (pathStr)
252 env->DeleteLocalRef(pathStr);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400253 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400254}
255
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400256MtpObjectHandleList* MyMtpDatabase::getObjectList(MtpStorageID storageID,
257 MtpObjectFormat format,
258 MtpObjectHandle parent) {
259 JNIEnv* env = AndroidRuntime::getJNIEnv();
260 jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectList,
261 (jint)storageID, (jint)format, (jint)parent);
262 if (!array)
263 return NULL;
264 MtpObjectHandleList* list = new MtpObjectHandleList();
265 jint* handles = env->GetIntArrayElements(array, 0);
266 jsize length = env->GetArrayLength(array);
Mike Lockwood7a047c82010-08-02 10:52:20 -0400267 for (int i = 0; i < length; i++)
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400268 list->push(handles[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400269 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400270 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400271
272 checkAndClearExceptionFromCallback(env, __FUNCTION__);
273 return list;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400274}
275
Mike Lockwood7a047c82010-08-02 10:52:20 -0400276int MyMtpDatabase::getNumObjects(MtpStorageID storageID,
277 MtpObjectFormat format,
278 MtpObjectHandle parent) {
279 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400280 int result = env->CallIntMethod(mDatabase, method_getNumObjects,
Mike Lockwood7a047c82010-08-02 10:52:20 -0400281 (jint)storageID, (jint)format, (jint)parent);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400282
283 checkAndClearExceptionFromCallback(env, __FUNCTION__);
284 return result;
Mike Lockwood7a047c82010-08-02 10:52:20 -0400285}
286
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400287MtpObjectFormatList* MyMtpDatabase::getSupportedPlaybackFormats() {
288 JNIEnv* env = AndroidRuntime::getJNIEnv();
289 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
290 method_getSupportedPlaybackFormats);
291 if (!array)
292 return NULL;
293 MtpObjectFormatList* list = new MtpObjectFormatList();
294 jint* formats = env->GetIntArrayElements(array, 0);
295 jsize length = env->GetArrayLength(array);
296 for (int i = 0; i < length; i++)
297 list->push(formats[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400298 env->ReleaseIntArrayElements(array, formats, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400299 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400300
301 checkAndClearExceptionFromCallback(env, __FUNCTION__);
302 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400303}
304
305MtpObjectFormatList* MyMtpDatabase::getSupportedCaptureFormats() {
306 JNIEnv* env = AndroidRuntime::getJNIEnv();
307 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
308 method_getSupportedCaptureFormats);
309 if (!array)
310 return NULL;
311 MtpObjectFormatList* list = new MtpObjectFormatList();
312 jint* formats = env->GetIntArrayElements(array, 0);
313 jsize length = env->GetArrayLength(array);
314 for (int i = 0; i < length; i++)
315 list->push(formats[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400316 env->ReleaseIntArrayElements(array, formats, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400317 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400318
319 checkAndClearExceptionFromCallback(env, __FUNCTION__);
320 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400321}
322
323MtpObjectPropertyList* MyMtpDatabase::getSupportedObjectProperties(MtpObjectFormat format) {
324 JNIEnv* env = AndroidRuntime::getJNIEnv();
325 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
326 method_getSupportedObjectProperties, (jint)format);
327 if (!array)
328 return NULL;
329 MtpObjectPropertyList* list = new MtpObjectPropertyList();
330 jint* properties = env->GetIntArrayElements(array, 0);
331 jsize length = env->GetArrayLength(array);
332 for (int i = 0; i < length; i++)
333 list->push(properties[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400334 env->ReleaseIntArrayElements(array, properties, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400335 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400336
337 checkAndClearExceptionFromCallback(env, __FUNCTION__);
338 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400339}
340
341MtpDevicePropertyList* MyMtpDatabase::getSupportedDeviceProperties() {
342 JNIEnv* env = AndroidRuntime::getJNIEnv();
343 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
344 method_getSupportedDeviceProperties);
345 if (!array)
346 return NULL;
347 MtpDevicePropertyList* list = new MtpDevicePropertyList();
348 jint* properties = env->GetIntArrayElements(array, 0);
349 jsize length = env->GetArrayLength(array);
350 for (int i = 0; i < length; i++)
351 list->push(properties[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400352 env->ReleaseIntArrayElements(array, properties, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400353 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400354
355 checkAndClearExceptionFromCallback(env, __FUNCTION__);
356 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400357}
358
Mike Lockwood828d19d2010-08-10 15:20:35 -0400359MtpResponseCode MyMtpDatabase::getObjectPropertyValue(MtpObjectHandle handle,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400360 MtpObjectProperty property,
361 MtpDataPacket& packet) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400362 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400363 jobject list = env->CallObjectMethod(mDatabase, method_getObjectPropertyList,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500364 (jlong)handle, 0, (jlong)property, 0, 0);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400365 MtpResponseCode result = env->GetIntField(list, field_mResult);
366 int count = env->GetIntField(list, field_mCount);
367 if (result == MTP_RESPONSE_OK && count != 1)
368 result = MTP_RESPONSE_GENERAL_ERROR;
369
370 if (result == MTP_RESPONSE_OK) {
371 jintArray objectHandlesArray = (jintArray)env->GetObjectField(list, field_mObjectHandles);
372 jintArray propertyCodesArray = (jintArray)env->GetObjectField(list, field_mPropertyCodes);
373 jintArray dataTypesArray = (jintArray)env->GetObjectField(list, field_mDataTypes);
374 jlongArray longValuesArray = (jlongArray)env->GetObjectField(list, field_mLongValues);
375 jobjectArray stringValuesArray = (jobjectArray)env->GetObjectField(list, field_mStringValues);
376
377 jint* objectHandles = env->GetIntArrayElements(objectHandlesArray, 0);
378 jint* propertyCodes = env->GetIntArrayElements(propertyCodesArray, 0);
379 jint* dataTypes = env->GetIntArrayElements(dataTypesArray, 0);
380 jlong* longValues = (longValuesArray ? env->GetLongArrayElements(longValuesArray, 0) : NULL);
381
382 int type = dataTypes[0];
383 jlong longValue = (longValues ? longValues[0] : 0);
384
385 // special case date properties, which are strings to MTP
386 // but stored internally as a uint64
387 if (property == MTP_PROPERTY_DATE_MODIFIED || property == MTP_PROPERTY_DATE_ADDED) {
388 char date[20];
389 formatDateTime(longValue, date, sizeof(date));
390 packet.putString(date);
391 goto out;
392 }
393 // release date is stored internally as just the year
394 if (property == MTP_PROPERTY_ORIGINAL_RELEASE_DATE) {
395 char date[20];
396 snprintf(date, sizeof(date), "%04lld0101T000000", longValue);
397 packet.putString(date);
398 goto out;
399 }
400
401 switch (type) {
402 case MTP_TYPE_INT8:
403 packet.putInt8(longValue);
404 break;
405 case MTP_TYPE_UINT8:
406 packet.putUInt8(longValue);
407 break;
408 case MTP_TYPE_INT16:
409 packet.putInt16(longValue);
410 break;
411 case MTP_TYPE_UINT16:
412 packet.putUInt16(longValue);
413 break;
414 case MTP_TYPE_INT32:
415 packet.putInt32(longValue);
416 break;
417 case MTP_TYPE_UINT32:
418 packet.putUInt32(longValue);
419 break;
420 case MTP_TYPE_INT64:
421 packet.putInt64(longValue);
422 break;
423 case MTP_TYPE_UINT64:
424 packet.putUInt64(longValue);
425 break;
426 case MTP_TYPE_INT128:
427 packet.putInt128(longValue);
428 break;
429 case MTP_TYPE_UINT128:
430 packet.putInt128(longValue);
431 break;
432 case MTP_TYPE_STR:
433 {
434 jstring stringValue = (jstring)env->GetObjectArrayElement(stringValuesArray, 0);
435 if (stringValue) {
436 const char* str = env->GetStringUTFChars(stringValue, NULL);
James Dong39774722011-04-06 11:57:48 -0700437 if (str == NULL) {
438 return MTP_RESPONSE_GENERAL_ERROR;
439 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400440 packet.putString(str);
441 env->ReleaseStringUTFChars(stringValue, str);
442 } else {
443 packet.putEmptyString();
444 }
445 break;
446 }
447 default:
Steve Block3762c312012-01-06 19:20:56 +0000448 ALOGE("unsupported type in getObjectPropertyValue\n");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400449 result = MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
450 }
451out:
452 env->ReleaseIntArrayElements(objectHandlesArray, objectHandles, 0);
453 env->ReleaseIntArrayElements(propertyCodesArray, propertyCodes, 0);
454 env->ReleaseIntArrayElements(dataTypesArray, dataTypes, 0);
455 if (longValues)
456 env->ReleaseLongArrayElements(longValuesArray, longValues, 0);
457
458 env->DeleteLocalRef(objectHandlesArray);
459 env->DeleteLocalRef(propertyCodesArray);
460 env->DeleteLocalRef(dataTypesArray);
461 if (longValuesArray)
462 env->DeleteLocalRef(longValuesArray);
463 if (stringValuesArray)
464 env->DeleteLocalRef(stringValuesArray);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400465 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400466
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400467 env->DeleteLocalRef(list);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400468 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400469 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400470}
471
Mike Lockwood828d19d2010-08-10 15:20:35 -0400472MtpResponseCode MyMtpDatabase::setObjectPropertyValue(MtpObjectHandle handle,
473 MtpObjectProperty property,
474 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400475 int type;
476
477 if (!getObjectPropertyInfo(property, type))
478 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
479
480 JNIEnv* env = AndroidRuntime::getJNIEnv();
481 jlong longValue = 0;
482 jstring stringValue = NULL;
483
484 switch (type) {
485 case MTP_TYPE_INT8:
486 longValue = packet.getInt8();
487 break;
488 case MTP_TYPE_UINT8:
489 longValue = packet.getUInt8();
490 break;
491 case MTP_TYPE_INT16:
492 longValue = packet.getInt16();
493 break;
494 case MTP_TYPE_UINT16:
495 longValue = packet.getUInt16();
496 break;
497 case MTP_TYPE_INT32:
498 longValue = packet.getInt32();
499 break;
500 case MTP_TYPE_UINT32:
501 longValue = packet.getUInt32();
502 break;
503 case MTP_TYPE_INT64:
504 longValue = packet.getInt64();
505 break;
506 case MTP_TYPE_UINT64:
507 longValue = packet.getUInt64();
508 break;
509 case MTP_TYPE_STR:
510 {
511 MtpStringBuffer buffer;
512 packet.getString(buffer);
513 stringValue = env->NewStringUTF((const char *)buffer);
514 break;
515 }
516 default:
Steve Block3762c312012-01-06 19:20:56 +0000517 ALOGE("unsupported type in getObjectPropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400518 return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
519 }
520
521 jint result = env->CallIntMethod(mDatabase, method_setObjectProperty,
522 (jint)handle, (jint)property, longValue, stringValue);
Mike Lockwood88394712010-09-27 10:01:00 -0400523 if (stringValue)
524 env->DeleteLocalRef(stringValue);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400525
526 checkAndClearExceptionFromCallback(env, __FUNCTION__);
527 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400528}
529
530MtpResponseCode MyMtpDatabase::getDevicePropertyValue(MtpDeviceProperty property,
531 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400532 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood56c85242014-03-07 13:29:08 -0800533
534 if (property == MTP_DEVICE_PROPERTY_BATTERY_LEVEL) {
535 // special case - implemented here instead of Java
536 packet.putUInt8((uint8_t)env->GetIntField(mDatabase, field_batteryLevel));
537 return MTP_RESPONSE_OK;
538 } else {
539 int type;
540
541 if (!getDevicePropertyInfo(property, type))
542 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
543
544 jint result = env->CallIntMethod(mDatabase, method_getDeviceProperty,
545 (jint)property, mLongBuffer, mStringBuffer);
546 if (result != MTP_RESPONSE_OK) {
547 checkAndClearExceptionFromCallback(env, __FUNCTION__);
548 return result;
549 }
550
551 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
552 jlong longValue = longValues[0];
553 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
554
555 switch (type) {
556 case MTP_TYPE_INT8:
557 packet.putInt8(longValue);
558 break;
559 case MTP_TYPE_UINT8:
560 packet.putUInt8(longValue);
561 break;
562 case MTP_TYPE_INT16:
563 packet.putInt16(longValue);
564 break;
565 case MTP_TYPE_UINT16:
566 packet.putUInt16(longValue);
567 break;
568 case MTP_TYPE_INT32:
569 packet.putInt32(longValue);
570 break;
571 case MTP_TYPE_UINT32:
572 packet.putUInt32(longValue);
573 break;
574 case MTP_TYPE_INT64:
575 packet.putInt64(longValue);
576 break;
577 case MTP_TYPE_UINT64:
578 packet.putUInt64(longValue);
579 break;
580 case MTP_TYPE_INT128:
581 packet.putInt128(longValue);
582 break;
583 case MTP_TYPE_UINT128:
584 packet.putInt128(longValue);
585 break;
586 case MTP_TYPE_STR:
587 {
588 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
589 packet.putString(str);
590 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
591 break;
592 }
593 default:
594 ALOGE("unsupported type in getDevicePropertyValue\n");
595 return MTP_RESPONSE_INVALID_DEVICE_PROP_FORMAT;
596 }
597
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400598 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood56c85242014-03-07 13:29:08 -0800599 return MTP_RESPONSE_OK;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400600 }
Mike Lockwood828d19d2010-08-10 15:20:35 -0400601}
602
603MtpResponseCode MyMtpDatabase::setDevicePropertyValue(MtpDeviceProperty property,
604 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400605 int type;
606
607 if (!getDevicePropertyInfo(property, type))
608 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
609
610 JNIEnv* env = AndroidRuntime::getJNIEnv();
611 jlong longValue = 0;
612 jstring stringValue = NULL;
613
614 switch (type) {
615 case MTP_TYPE_INT8:
616 longValue = packet.getInt8();
617 break;
618 case MTP_TYPE_UINT8:
619 longValue = packet.getUInt8();
620 break;
621 case MTP_TYPE_INT16:
622 longValue = packet.getInt16();
623 break;
624 case MTP_TYPE_UINT16:
625 longValue = packet.getUInt16();
626 break;
627 case MTP_TYPE_INT32:
628 longValue = packet.getInt32();
629 break;
630 case MTP_TYPE_UINT32:
631 longValue = packet.getUInt32();
632 break;
633 case MTP_TYPE_INT64:
634 longValue = packet.getInt64();
635 break;
636 case MTP_TYPE_UINT64:
637 longValue = packet.getUInt64();
638 break;
639 case MTP_TYPE_STR:
640 {
641 MtpStringBuffer buffer;
642 packet.getString(buffer);
643 stringValue = env->NewStringUTF((const char *)buffer);
644 break;
645 }
646 default:
Steve Block3762c312012-01-06 19:20:56 +0000647 ALOGE("unsupported type in setDevicePropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400648 return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
649 }
650
651 jint result = env->CallIntMethod(mDatabase, method_setDeviceProperty,
652 (jint)property, longValue, stringValue);
Mike Lockwood88394712010-09-27 10:01:00 -0400653 if (stringValue)
654 env->DeleteLocalRef(stringValue);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400655
656 checkAndClearExceptionFromCallback(env, __FUNCTION__);
657 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400658}
659
660MtpResponseCode MyMtpDatabase::resetDeviceProperty(MtpDeviceProperty property) {
661 return -1;
662}
663
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400664MtpResponseCode MyMtpDatabase::getObjectPropertyList(MtpObjectHandle handle,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500665 uint32_t format, uint32_t property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400666 int groupCode, int depth,
667 MtpDataPacket& packet) {
668 JNIEnv* env = AndroidRuntime::getJNIEnv();
669 jobject list = env->CallObjectMethod(mDatabase, method_getObjectPropertyList,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500670 (jlong)handle, (jint)format, (jlong)property, (jint)groupCode, (jint)depth);
671 checkAndClearExceptionFromCallback(env, __FUNCTION__);
672 if (!list)
673 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400674 int count = env->GetIntField(list, field_mCount);
675 MtpResponseCode result = env->GetIntField(list, field_mResult);
676
677 packet.putUInt32(count);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400678 if (count > 0) {
679 jintArray objectHandlesArray = (jintArray)env->GetObjectField(list, field_mObjectHandles);
680 jintArray propertyCodesArray = (jintArray)env->GetObjectField(list, field_mPropertyCodes);
681 jintArray dataTypesArray = (jintArray)env->GetObjectField(list, field_mDataTypes);
682 jlongArray longValuesArray = (jlongArray)env->GetObjectField(list, field_mLongValues);
683 jobjectArray stringValuesArray = (jobjectArray)env->GetObjectField(list, field_mStringValues);
684
685 jint* objectHandles = env->GetIntArrayElements(objectHandlesArray, 0);
686 jint* propertyCodes = env->GetIntArrayElements(propertyCodesArray, 0);
687 jint* dataTypes = env->GetIntArrayElements(dataTypesArray, 0);
688 jlong* longValues = (longValuesArray ? env->GetLongArrayElements(longValuesArray, 0) : NULL);
689
690 for (int i = 0; i < count; i++) {
691 packet.putUInt32(objectHandles[i]);
692 packet.putUInt16(propertyCodes[i]);
693 int type = dataTypes[i];
694 packet.putUInt16(type);
695
696 switch (type) {
697 case MTP_TYPE_INT8:
698 packet.putInt8(longValues[i]);
699 break;
700 case MTP_TYPE_UINT8:
701 packet.putUInt8(longValues[i]);
702 break;
703 case MTP_TYPE_INT16:
704 packet.putInt16(longValues[i]);
705 break;
706 case MTP_TYPE_UINT16:
707 packet.putUInt16(longValues[i]);
708 break;
709 case MTP_TYPE_INT32:
710 packet.putInt32(longValues[i]);
711 break;
712 case MTP_TYPE_UINT32:
713 packet.putUInt32(longValues[i]);
714 break;
715 case MTP_TYPE_INT64:
716 packet.putInt64(longValues[i]);
717 break;
718 case MTP_TYPE_UINT64:
719 packet.putUInt64(longValues[i]);
720 break;
721 case MTP_TYPE_INT128:
722 packet.putInt128(longValues[i]);
723 break;
724 case MTP_TYPE_UINT128:
725 packet.putUInt128(longValues[i]);
726 break;
727 case MTP_TYPE_STR: {
728 jstring value = (jstring)env->GetObjectArrayElement(stringValuesArray, i);
Mike Lockwood2711e492010-12-11 11:24:37 -0800729 const char *valueStr = (value ? env->GetStringUTFChars(value, NULL) : NULL);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400730 if (valueStr) {
731 packet.putString(valueStr);
732 env->ReleaseStringUTFChars(value, valueStr);
733 } else {
734 packet.putEmptyString();
735 }
736 env->DeleteLocalRef(value);
737 break;
738 }
739 default:
Steve Block3762c312012-01-06 19:20:56 +0000740 ALOGE("bad or unsupported data type in MyMtpDatabase::getObjectPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400741 break;
742 }
743 }
744
745 env->ReleaseIntArrayElements(objectHandlesArray, objectHandles, 0);
746 env->ReleaseIntArrayElements(propertyCodesArray, propertyCodes, 0);
747 env->ReleaseIntArrayElements(dataTypesArray, dataTypes, 0);
748 if (longValues)
749 env->ReleaseLongArrayElements(longValuesArray, longValues, 0);
750
751 env->DeleteLocalRef(objectHandlesArray);
752 env->DeleteLocalRef(propertyCodesArray);
753 env->DeleteLocalRef(dataTypesArray);
754 if (longValuesArray)
755 env->DeleteLocalRef(longValuesArray);
756 if (stringValuesArray)
757 env->DeleteLocalRef(stringValuesArray);
758 }
759
760 env->DeleteLocalRef(list);
761 checkAndClearExceptionFromCallback(env, __FUNCTION__);
762 return result;
763}
764
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800765static void foreachentry(ExifEntry *entry, void *user) {
766 char buf[1024];
767 ALOGI("entry %x, format %d, size %d: %s",
768 entry->tag, entry->format, entry->size, exif_entry_get_value(entry, buf, sizeof(buf)));
769}
770
771static void foreachcontent(ExifContent *content, void *user) {
772 ALOGI("content %d", exif_content_get_ifd(content));
773 exif_content_foreach_entry(content, foreachentry, user);
774}
775
776static long getLongFromExifEntry(ExifEntry *e) {
777 ExifByteOrder o = exif_data_get_byte_order(e->parent->parent);
778 return exif_get_long(e->data, o);
779}
780
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400781MtpResponseCode MyMtpDatabase::getObjectInfo(MtpObjectHandle handle,
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700782 MtpObjectInfo& info) {
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700783 char date[20];
784 MtpString path;
785 int64_t length;
786 MtpObjectFormat format;
787
788 MtpResponseCode result = getObjectFilePath(handle, path, length, format);
789 if (result != MTP_RESPONSE_OK) {
790 return result;
791 }
792 info.mCompressedSize = (length > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)length);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400793
794 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700795 if (!env->CallBooleanMethod(mDatabase, method_getObjectInfo,
796 (jint)handle, mIntBuffer, mStringBuffer, mLongBuffer)) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400797 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700798 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400799
800 jint* intValues = env->GetIntArrayElements(mIntBuffer, 0);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700801 info.mStorageID = intValues[0];
802 info.mFormat = intValues[1];
803 info.mParent = intValues[2];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400804 env->ReleaseIntArrayElements(mIntBuffer, intValues, 0);
805
806 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700807 info.mDateCreated = longValues[0];
808 info.mDateModified = longValues[1];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400809 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
810
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700811// info.mAssociationType = (format == MTP_FORMAT_ASSOCIATION ?
Mike Lockwood828d19d2010-08-10 15:20:35 -0400812// MTP_ASSOCIATION_TYPE_GENERIC_FOLDER :
813// MTP_ASSOCIATION_TYPE_UNDEFINED);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700814 info.mAssociationType = MTP_ASSOCIATION_TYPE_UNDEFINED;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400815
816 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700817 MtpString temp(str);
818 info.mName = strdup((const char *)temp);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400819 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
820
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700821 // read EXIF data for thumbnail information
822 if (info.mFormat == MTP_FORMAT_EXIF_JPEG || info.mFormat == MTP_FORMAT_JFIF) {
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800823
824 ExifData *exifdata = exif_data_new_from_file(path);
825 if (exifdata) {
826 //exif_data_foreach_content(exifdata, foreachcontent, NULL);
827
828 // XXX get this from exif, or parse jpeg header instead?
829 ExifEntry *w = exif_content_get_entry(
830 exifdata->ifd[EXIF_IFD_EXIF], EXIF_TAG_PIXEL_X_DIMENSION);
831 ExifEntry *h = exif_content_get_entry(
832 exifdata->ifd[EXIF_IFD_EXIF], EXIF_TAG_PIXEL_Y_DIMENSION);
833 info.mThumbCompressedSize = exifdata->data ? exifdata->size : 0;
834 info.mThumbFormat = MTP_FORMAT_EXIF_JPEG;
Marco Nelissen0937eed2014-01-22 15:10:57 -0800835 info.mImagePixWidth = w ? getLongFromExifEntry(w) : 0;
836 info.mImagePixHeight = h ? getLongFromExifEntry(h) : 0;
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800837 exif_data_unref(exifdata);
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700838 }
839 }
840
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400841 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400842 return MTP_RESPONSE_OK;
843}
844
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700845void* MyMtpDatabase::getThumbnail(MtpObjectHandle handle, size_t& outThumbSize) {
846 MtpString path;
847 int64_t length;
848 MtpObjectFormat format;
849 void* result = NULL;
850 outThumbSize = 0;
851
852 if (getObjectFilePath(handle, path, length, format) == MTP_RESPONSE_OK
853 && (format == MTP_FORMAT_EXIF_JPEG || format == MTP_FORMAT_JFIF)) {
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800854
855 ExifData *exifdata = exif_data_new_from_file(path);
856 if (exifdata) {
857 if (exifdata->data) {
858 result = malloc(exifdata->size);
859 if (result) {
860 memcpy(result, exifdata->data, exifdata->size);
861 }
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700862 }
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800863 exif_data_unref(exifdata);
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700864 }
865 }
866
867 return result;
868}
869
Mike Lockwood59c777a2010-08-02 10:37:41 -0400870MtpResponseCode MyMtpDatabase::getObjectFilePath(MtpObjectHandle handle,
Mike Lockwood365e03e2010-12-08 16:08:01 -0800871 MtpString& outFilePath,
872 int64_t& outFileLength,
873 MtpObjectFormat& outFormat) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400874 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood59c777a2010-08-02 10:37:41 -0400875 jint result = env->CallIntMethod(mDatabase, method_getObjectFilePath,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400876 (jint)handle, mStringBuffer, mLongBuffer);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400877 if (result != MTP_RESPONSE_OK) {
878 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400879 return result;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400880 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400881
882 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
Mike Lockwood365e03e2010-12-08 16:08:01 -0800883 outFilePath.setTo(str, strlen16(str));
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400884 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
885
886 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
Mike Lockwood365e03e2010-12-08 16:08:01 -0800887 outFileLength = longValues[0];
888 outFormat = longValues[1];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400889 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700890
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400891 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400892 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400893}
894
Mike Lockwood59c777a2010-08-02 10:37:41 -0400895MtpResponseCode MyMtpDatabase::deleteFile(MtpObjectHandle handle) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400896 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400897 MtpResponseCode result = env->CallIntMethod(mDatabase, method_deleteFile, (jint)handle);
898
899 checkAndClearExceptionFromCallback(env, __FUNCTION__);
900 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400901}
902
903struct PropertyTableEntry {
904 MtpObjectProperty property;
905 int type;
906};
907
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400908static const PropertyTableEntry kObjectPropertyTable[] = {
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400909 { MTP_PROPERTY_STORAGE_ID, MTP_TYPE_UINT32 },
910 { MTP_PROPERTY_OBJECT_FORMAT, MTP_TYPE_UINT16 },
911 { MTP_PROPERTY_PROTECTION_STATUS, MTP_TYPE_UINT16 },
912 { MTP_PROPERTY_OBJECT_SIZE, MTP_TYPE_UINT64 },
913 { MTP_PROPERTY_OBJECT_FILE_NAME, MTP_TYPE_STR },
914 { MTP_PROPERTY_DATE_MODIFIED, MTP_TYPE_STR },
915 { MTP_PROPERTY_PARENT_OBJECT, MTP_TYPE_UINT32 },
916 { MTP_PROPERTY_PERSISTENT_UID, MTP_TYPE_UINT128 },
917 { MTP_PROPERTY_NAME, MTP_TYPE_STR },
Mike Lockwoodae078f72010-09-26 12:35:51 -0400918 { MTP_PROPERTY_DISPLAY_NAME, MTP_TYPE_STR },
919 { MTP_PROPERTY_DATE_ADDED, MTP_TYPE_STR },
920 { MTP_PROPERTY_ARTIST, MTP_TYPE_STR },
921 { MTP_PROPERTY_ALBUM_NAME, MTP_TYPE_STR },
922 { MTP_PROPERTY_ALBUM_ARTIST, MTP_TYPE_STR },
923 { MTP_PROPERTY_TRACK, MTP_TYPE_UINT16 },
924 { MTP_PROPERTY_ORIGINAL_RELEASE_DATE, MTP_TYPE_STR },
925 { MTP_PROPERTY_GENRE, MTP_TYPE_STR },
926 { MTP_PROPERTY_COMPOSER, MTP_TYPE_STR },
927 { MTP_PROPERTY_DURATION, MTP_TYPE_UINT32 },
928 { MTP_PROPERTY_DESCRIPTION, MTP_TYPE_STR },
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400929};
930
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400931static const PropertyTableEntry kDevicePropertyTable[] = {
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800932 { MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER, MTP_TYPE_STR },
933 { MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME, MTP_TYPE_STR },
934 { MTP_DEVICE_PROPERTY_IMAGE_SIZE, MTP_TYPE_STR },
Mike Lockwood56c85242014-03-07 13:29:08 -0800935 { MTP_DEVICE_PROPERTY_BATTERY_LEVEL, MTP_TYPE_UINT8 },
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400936};
937
938bool MyMtpDatabase::getObjectPropertyInfo(MtpObjectProperty property, int& type) {
939 int count = sizeof(kObjectPropertyTable) / sizeof(kObjectPropertyTable[0]);
940 const PropertyTableEntry* entry = kObjectPropertyTable;
941 for (int i = 0; i < count; i++, entry++) {
942 if (entry->property == property) {
943 type = entry->type;
944 return true;
945 }
946 }
947 return false;
948}
949
950bool MyMtpDatabase::getDevicePropertyInfo(MtpDeviceProperty property, int& type) {
951 int count = sizeof(kDevicePropertyTable) / sizeof(kDevicePropertyTable[0]);
952 const PropertyTableEntry* entry = kDevicePropertyTable;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400953 for (int i = 0; i < count; i++, entry++) {
954 if (entry->property == property) {
955 type = entry->type;
956 return true;
957 }
958 }
959 return false;
960}
961
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400962MtpObjectHandleList* MyMtpDatabase::getObjectReferences(MtpObjectHandle handle) {
963 JNIEnv* env = AndroidRuntime::getJNIEnv();
964 jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectReferences,
965 (jint)handle);
966 if (!array)
967 return NULL;
968 MtpObjectHandleList* list = new MtpObjectHandleList();
969 jint* handles = env->GetIntArrayElements(array, 0);
970 jsize length = env->GetArrayLength(array);
971 for (int i = 0; i < length; i++)
972 list->push(handles[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400973 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400974 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400975
976 checkAndClearExceptionFromCallback(env, __FUNCTION__);
977 return list;
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400978}
979
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400980MtpResponseCode MyMtpDatabase::setObjectReferences(MtpObjectHandle handle,
981 MtpObjectHandleList* references) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400982 JNIEnv* env = AndroidRuntime::getJNIEnv();
983 int count = references->size();
984 jintArray array = env->NewIntArray(count);
985 if (!array) {
Steve Block3762c312012-01-06 19:20:56 +0000986 ALOGE("out of memory in setObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400987 return false;
988 }
989 jint* handles = env->GetIntArrayElements(array, 0);
990 for (int i = 0; i < count; i++)
991 handles[i] = (*references)[i];
992 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400993 MtpResponseCode result = env->CallIntMethod(mDatabase, method_setObjectReferences,
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400994 (jint)handle, array);
Mike Lockwood88394712010-09-27 10:01:00 -0400995 env->DeleteLocalRef(array);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400996
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400997 checkAndClearExceptionFromCallback(env, __FUNCTION__);
998 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400999}
1000
Mike Lockwood828d19d2010-08-10 15:20:35 -04001001MtpProperty* MyMtpDatabase::getObjectPropertyDesc(MtpObjectProperty property,
1002 MtpObjectFormat format) {
1003 MtpProperty* result = NULL;
1004 switch (property) {
1005 case MTP_PROPERTY_OBJECT_FORMAT:
Mike Lockwood9b5e9c42010-12-07 18:53:50 -08001006 // use format as default value
1007 result = new MtpProperty(property, MTP_TYPE_UINT16, false, format);
1008 break;
Mike Lockwood828d19d2010-08-10 15:20:35 -04001009 case MTP_PROPERTY_PROTECTION_STATUS:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001010 case MTP_PROPERTY_TRACK:
Mike Lockwood828d19d2010-08-10 15:20:35 -04001011 result = new MtpProperty(property, MTP_TYPE_UINT16);
1012 break;
1013 case MTP_PROPERTY_STORAGE_ID:
1014 case MTP_PROPERTY_PARENT_OBJECT:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001015 case MTP_PROPERTY_DURATION:
Mike Lockwood828d19d2010-08-10 15:20:35 -04001016 result = new MtpProperty(property, MTP_TYPE_UINT32);
1017 break;
1018 case MTP_PROPERTY_OBJECT_SIZE:
1019 result = new MtpProperty(property, MTP_TYPE_UINT64);
1020 break;
1021 case MTP_PROPERTY_PERSISTENT_UID:
1022 result = new MtpProperty(property, MTP_TYPE_UINT128);
1023 break;
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -04001024 case MTP_PROPERTY_NAME:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001025 case MTP_PROPERTY_DISPLAY_NAME:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001026 case MTP_PROPERTY_ARTIST:
1027 case MTP_PROPERTY_ALBUM_NAME:
1028 case MTP_PROPERTY_ALBUM_ARTIST:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001029 case MTP_PROPERTY_GENRE:
1030 case MTP_PROPERTY_COMPOSER:
1031 case MTP_PROPERTY_DESCRIPTION:
Mike Lockwood828d19d2010-08-10 15:20:35 -04001032 result = new MtpProperty(property, MTP_TYPE_STR);
1033 break;
Mike Lockwood5b19af02010-11-23 18:38:55 -05001034 case MTP_PROPERTY_DATE_MODIFIED:
1035 case MTP_PROPERTY_DATE_ADDED:
1036 case MTP_PROPERTY_ORIGINAL_RELEASE_DATE:
1037 result = new MtpProperty(property, MTP_TYPE_STR);
1038 result->setFormDateTime();
1039 break;
Mike Lockwood5ebac832010-10-12 11:33:47 -04001040 case MTP_PROPERTY_OBJECT_FILE_NAME:
Mike Lockwood6a6a3af2010-10-12 14:19:51 -04001041 // We allow renaming files and folders
1042 result = new MtpProperty(property, MTP_TYPE_STR, true);
Mike Lockwood5ebac832010-10-12 11:33:47 -04001043 break;
Mike Lockwood828d19d2010-08-10 15:20:35 -04001044 }
1045
1046 return result;
1047}
1048
1049MtpProperty* MyMtpDatabase::getDevicePropertyDesc(MtpDeviceProperty property) {
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001050 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001051 MtpProperty* result = NULL;
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001052 bool writable = false;
1053
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001054 switch (property) {
1055 case MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
1056 case MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001057 writable = true;
1058 // fall through
Mike Lockwood56c85242014-03-07 13:29:08 -08001059 case MTP_DEVICE_PROPERTY_IMAGE_SIZE: {
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001060 result = new MtpProperty(property, MTP_TYPE_STR, writable);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001061
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001062 // get current value
Mike Lockwooda2a21282010-09-25 21:21:05 -04001063 jint ret = env->CallIntMethod(mDatabase, method_getDeviceProperty,
1064 (jint)property, mLongBuffer, mStringBuffer);
1065 if (ret == MTP_RESPONSE_OK) {
1066 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
1067 result->setCurrentValue(str);
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001068 // for read-only properties it is safe to assume current value is default value
1069 if (!writable)
1070 result->setDefaultValue(str);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001071 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
1072 } else {
Steve Block3762c312012-01-06 19:20:56 +00001073 ALOGE("unable to read device property, response: %04X", ret);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001074 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001075 break;
Mike Lockwood56c85242014-03-07 13:29:08 -08001076 }
1077 case MTP_DEVICE_PROPERTY_BATTERY_LEVEL:
1078 result = new MtpProperty(property, MTP_TYPE_UINT8);
1079 result->setFormRange(0, env->GetIntField(mDatabase, field_batteryScale), 1);
1080 result->mCurrentValue.u.u8 = (uint8_t)env->GetIntField(mDatabase, field_batteryLevel);
1081 break;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001082 }
1083
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001084 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001085 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -04001086}
1087
Mike Lockwood2837eef2010-08-31 16:25:12 -04001088void MyMtpDatabase::sessionStarted() {
1089 JNIEnv* env = AndroidRuntime::getJNIEnv();
1090 env->CallVoidMethod(mDatabase, method_sessionStarted);
1091 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1092}
1093
1094void MyMtpDatabase::sessionEnded() {
1095 JNIEnv* env = AndroidRuntime::getJNIEnv();
1096 env->CallVoidMethod(mDatabase, method_sessionEnded);
1097 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1098}
1099
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001100// ----------------------------------------------------------------------------
1101
1102static void
Mike Lockwood0cd01362010-12-30 11:54:33 -05001103android_mtp_MtpDatabase_setup(JNIEnv *env, jobject thiz)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001104{
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001105 MyMtpDatabase* database = new MyMtpDatabase(env, thiz);
Ashok Bhate2e59322013-12-17 19:04:19 +00001106 env->SetLongField(thiz, field_context, (jlong)database);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001107 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1108}
1109
1110static void
Mike Lockwood0cd01362010-12-30 11:54:33 -05001111android_mtp_MtpDatabase_finalize(JNIEnv *env, jobject thiz)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001112{
Ashok Bhate2e59322013-12-17 19:04:19 +00001113 MyMtpDatabase* database = (MyMtpDatabase *)env->GetLongField(thiz, field_context);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001114 database->cleanup(env);
1115 delete database;
Ashok Bhate2e59322013-12-17 19:04:19 +00001116 env->SetLongField(thiz, field_context, 0);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001117 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1118}
1119
Mike Lockwood31599912010-11-15 13:43:30 -05001120static jstring
Mike Lockwood0cd01362010-12-30 11:54:33 -05001121android_mtp_MtpPropertyGroup_format_date_time(JNIEnv *env, jobject thiz, jlong seconds)
Mike Lockwood31599912010-11-15 13:43:30 -05001122{
Mike Lockwood31599912010-11-15 13:43:30 -05001123 char date[20];
1124 formatDateTime(seconds, date, sizeof(date));
1125 return env->NewStringUTF(date);
Mike Lockwood31599912010-11-15 13:43:30 -05001126}
1127
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001128// ----------------------------------------------------------------------------
1129
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001130static JNINativeMethod gMtpDatabaseMethods[] = {
Mike Lockwood0cd01362010-12-30 11:54:33 -05001131 {"native_setup", "()V", (void *)android_mtp_MtpDatabase_setup},
1132 {"native_finalize", "()V", (void *)android_mtp_MtpDatabase_finalize},
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001133};
1134
1135static JNINativeMethod gMtpPropertyGroupMethods[] = {
Mike Lockwood31599912010-11-15 13:43:30 -05001136 {"format_date_time", "(J)Ljava/lang/String;",
Mike Lockwood0cd01362010-12-30 11:54:33 -05001137 (void *)android_mtp_MtpPropertyGroup_format_date_time},
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001138};
1139
Mike Lockwood0cd01362010-12-30 11:54:33 -05001140static const char* const kClassPathName = "android/mtp/MtpDatabase";
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001141
Mike Lockwood0cd01362010-12-30 11:54:33 -05001142int register_android_mtp_MtpDatabase(JNIEnv *env)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001143{
1144 jclass clazz;
1145
Mike Lockwood0cd01362010-12-30 11:54:33 -05001146 clazz = env->FindClass("android/mtp/MtpDatabase");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001147 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001148 ALOGE("Can't find android/mtp/MtpDatabase");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001149 return -1;
1150 }
Mike Lockwoodd815f792010-07-12 08:49:01 -04001151 method_beginSendObject = env->GetMethodID(clazz, "beginSendObject", "(Ljava/lang/String;IIIJJ)I");
1152 if (method_beginSendObject == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001153 ALOGE("Can't find beginSendObject");
Mike Lockwoodd815f792010-07-12 08:49:01 -04001154 return -1;
1155 }
Mike Lockwood7a0bd172011-01-18 11:06:19 -08001156 method_endSendObject = env->GetMethodID(clazz, "endSendObject", "(Ljava/lang/String;IIZ)V");
Mike Lockwoodd815f792010-07-12 08:49:01 -04001157 if (method_endSendObject == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001158 ALOGE("Can't find endSendObject");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001159 return -1;
1160 }
1161 method_getObjectList = env->GetMethodID(clazz, "getObjectList", "(III)[I");
1162 if (method_getObjectList == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001163 ALOGE("Can't find getObjectList");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001164 return -1;
1165 }
Mike Lockwood7a047c82010-08-02 10:52:20 -04001166 method_getNumObjects = env->GetMethodID(clazz, "getNumObjects", "(III)I");
1167 if (method_getNumObjects == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001168 ALOGE("Can't find getNumObjects");
Mike Lockwood7a047c82010-08-02 10:52:20 -04001169 return -1;
1170 }
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001171 method_getSupportedPlaybackFormats = env->GetMethodID(clazz, "getSupportedPlaybackFormats", "()[I");
1172 if (method_getSupportedPlaybackFormats == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001173 ALOGE("Can't find getSupportedPlaybackFormats");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001174 return -1;
1175 }
1176 method_getSupportedCaptureFormats = env->GetMethodID(clazz, "getSupportedCaptureFormats", "()[I");
1177 if (method_getSupportedCaptureFormats == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001178 ALOGE("Can't find getSupportedCaptureFormats");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001179 return -1;
1180 }
1181 method_getSupportedObjectProperties = env->GetMethodID(clazz, "getSupportedObjectProperties", "(I)[I");
1182 if (method_getSupportedObjectProperties == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001183 ALOGE("Can't find getSupportedObjectProperties");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001184 return -1;
1185 }
1186 method_getSupportedDeviceProperties = env->GetMethodID(clazz, "getSupportedDeviceProperties", "()[I");
1187 if (method_getSupportedDeviceProperties == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001188 ALOGE("Can't find getSupportedDeviceProperties");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001189 return -1;
1190 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001191 method_setObjectProperty = env->GetMethodID(clazz, "setObjectProperty", "(IIJLjava/lang/String;)I");
1192 if (method_setObjectProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001193 ALOGE("Can't find setObjectProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001194 return -1;
1195 }
1196 method_getDeviceProperty = env->GetMethodID(clazz, "getDeviceProperty", "(I[J[C)I");
1197 if (method_getDeviceProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001198 ALOGE("Can't find getDeviceProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001199 return -1;
1200 }
1201 method_setDeviceProperty = env->GetMethodID(clazz, "setDeviceProperty", "(IJLjava/lang/String;)I");
1202 if (method_setDeviceProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001203 ALOGE("Can't find setDeviceProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001204 return -1;
1205 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001206 method_getObjectPropertyList = env->GetMethodID(clazz, "getObjectPropertyList",
Mike Lockwood0cd01362010-12-30 11:54:33 -05001207 "(JIJII)Landroid/mtp/MtpPropertyList;");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001208 if (method_getObjectPropertyList == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001209 ALOGE("Can't find getObjectPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001210 return -1;
1211 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001212 method_getObjectInfo = env->GetMethodID(clazz, "getObjectInfo", "(I[I[C[J)Z");
1213 if (method_getObjectInfo == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001214 ALOGE("Can't find getObjectInfo");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001215 return -1;
1216 }
Mike Lockwood59c777a2010-08-02 10:37:41 -04001217 method_getObjectFilePath = env->GetMethodID(clazz, "getObjectFilePath", "(I[C[J)I");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001218 if (method_getObjectFilePath == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001219 ALOGE("Can't find getObjectFilePath");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001220 return -1;
1221 }
Mike Lockwood59c777a2010-08-02 10:37:41 -04001222 method_deleteFile = env->GetMethodID(clazz, "deleteFile", "(I)I");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001223 if (method_deleteFile == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001224 ALOGE("Can't find deleteFile");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001225 return -1;
1226 }
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001227 method_getObjectReferences = env->GetMethodID(clazz, "getObjectReferences", "(I)[I");
1228 if (method_getObjectReferences == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001229 ALOGE("Can't find getObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001230 return -1;
1231 }
1232 method_setObjectReferences = env->GetMethodID(clazz, "setObjectReferences", "(I[I)I");
1233 if (method_setObjectReferences == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001234 ALOGE("Can't find setObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001235 return -1;
1236 }
Mike Lockwood2837eef2010-08-31 16:25:12 -04001237 method_sessionStarted = env->GetMethodID(clazz, "sessionStarted", "()V");
1238 if (method_sessionStarted == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001239 ALOGE("Can't find sessionStarted");
Mike Lockwood2837eef2010-08-31 16:25:12 -04001240 return -1;
1241 }
1242 method_sessionEnded = env->GetMethodID(clazz, "sessionEnded", "()V");
1243 if (method_sessionEnded == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001244 ALOGE("Can't find sessionEnded");
Mike Lockwood2837eef2010-08-31 16:25:12 -04001245 return -1;
1246 }
1247
Ashok Bhate2e59322013-12-17 19:04:19 +00001248 field_context = env->GetFieldID(clazz, "mNativeContext", "J");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001249 if (field_context == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001250 ALOGE("Can't find MtpDatabase.mNativeContext");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001251 return -1;
1252 }
Mike Lockwood56c85242014-03-07 13:29:08 -08001253 field_batteryLevel = env->GetFieldID(clazz, "mBatteryLevel", "I");
1254 if (field_batteryLevel == NULL) {
1255 ALOGE("Can't find MtpDatabase.mBatteryLevel");
1256 return -1;
1257 }
1258 field_batteryScale = env->GetFieldID(clazz, "mBatteryScale", "I");
1259 if (field_batteryScale == NULL) {
1260 ALOGE("Can't find MtpDatabase.mBatteryScale");
1261 return -1;
1262 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001263
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001264 // now set up fields for MtpPropertyList class
Mike Lockwood0cd01362010-12-30 11:54:33 -05001265 clazz = env->FindClass("android/mtp/MtpPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001266 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001267 ALOGE("Can't find android/mtp/MtpPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001268 return -1;
1269 }
1270 field_mCount = env->GetFieldID(clazz, "mCount", "I");
1271 if (field_mCount == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001272 ALOGE("Can't find MtpPropertyList.mCount");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001273 return -1;
1274 }
1275 field_mResult = env->GetFieldID(clazz, "mResult", "I");
1276 if (field_mResult == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001277 ALOGE("Can't find MtpPropertyList.mResult");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001278 return -1;
1279 }
1280 field_mObjectHandles = env->GetFieldID(clazz, "mObjectHandles", "[I");
1281 if (field_mObjectHandles == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001282 ALOGE("Can't find MtpPropertyList.mObjectHandles");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001283 return -1;
1284 }
1285 field_mPropertyCodes = env->GetFieldID(clazz, "mPropertyCodes", "[I");
1286 if (field_mPropertyCodes == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001287 ALOGE("Can't find MtpPropertyList.mPropertyCodes");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001288 return -1;
1289 }
1290 field_mDataTypes = env->GetFieldID(clazz, "mDataTypes", "[I");
1291 if (field_mDataTypes == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001292 ALOGE("Can't find MtpPropertyList.mDataTypes");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001293 return -1;
1294 }
1295 field_mLongValues = env->GetFieldID(clazz, "mLongValues", "[J");
1296 if (field_mLongValues == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001297 ALOGE("Can't find MtpPropertyList.mLongValues");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001298 return -1;
1299 }
1300 field_mStringValues = env->GetFieldID(clazz, "mStringValues", "[Ljava/lang/String;");
1301 if (field_mStringValues == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001302 ALOGE("Can't find MtpPropertyList.mStringValues");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001303 return -1;
1304 }
1305
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001306 if (AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -05001307 "android/mtp/MtpDatabase", gMtpDatabaseMethods, NELEM(gMtpDatabaseMethods)))
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001308 return -1;
1309
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001310 return AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -05001311 "android/mtp/MtpPropertyGroup", gMtpPropertyGroupMethods, NELEM(gMtpPropertyGroupMethods));
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001312}