blob: 19b54a6d8f7847413b37688f20ea11a020f73224 [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" {
Marco Nelissen3cd393c2014-01-10 10:39:27 -080041#include "libexif/exif-content.h"
42#include "libexif/exif-data.h"
43#include "libexif/exif-tag.h"
44#include "libexif/exif-utils.h"
Mike Lockwoodc89f2222011-04-24 18:40:17 -070045}
46
Mike Lockwoodd21eac92010-07-03 00:44:05 -040047using namespace android;
48
49// ----------------------------------------------------------------------------
50
Mike Lockwoodd815f792010-07-12 08:49:01 -040051static jmethodID method_beginSendObject;
52static jmethodID method_endSendObject;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040053static jmethodID method_getObjectList;
Mike Lockwood7a047c82010-08-02 10:52:20 -040054static jmethodID method_getNumObjects;
Mike Lockwood4b322ce2010-08-10 07:37:50 -040055static jmethodID method_getSupportedPlaybackFormats;
56static jmethodID method_getSupportedCaptureFormats;
57static jmethodID method_getSupportedObjectProperties;
58static jmethodID method_getSupportedDeviceProperties;
Mike Lockwood828d19d2010-08-10 15:20:35 -040059static jmethodID method_setObjectProperty;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040060static jmethodID method_getDeviceProperty;
61static jmethodID method_setDeviceProperty;
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -040062static jmethodID method_getObjectPropertyList;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040063static jmethodID method_getObjectInfo;
64static jmethodID method_getObjectFilePath;
65static jmethodID method_deleteFile;
Mike Lockwood9a2046f2010-08-03 15:30:09 -040066static jmethodID method_getObjectReferences;
67static jmethodID method_setObjectReferences;
Mike Lockwood2837eef2010-08-31 16:25:12 -040068static jmethodID method_sessionStarted;
69static jmethodID method_sessionEnded;
70
Mike Lockwoodd21eac92010-07-03 00:44:05 -040071static jfieldID field_context;
Mike Lockwood56c85242014-03-07 13:29:08 -080072static jfieldID field_batteryLevel;
73static jfieldID field_batteryScale;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040074
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -040075// MtpPropertyList fields
76static jfieldID field_mCount;
77static jfieldID field_mResult;
78static jfieldID field_mObjectHandles;
79static jfieldID field_mPropertyCodes;
80static jfieldID field_mDataTypes;
81static jfieldID field_mLongValues;
82static jfieldID field_mStringValues;
83
84
Mike Lockwoodd21eac92010-07-03 00:44:05 -040085MtpDatabase* getMtpDatabase(JNIEnv *env, jobject database) {
Ashok Bhate2e59322013-12-17 19:04:19 +000086 return (MtpDatabase *)env->GetLongField(database, field_context);
Mike Lockwoodd21eac92010-07-03 00:44:05 -040087}
88
89// ----------------------------------------------------------------------------
90
91class MyMtpDatabase : public MtpDatabase {
92private:
93 jobject mDatabase;
94 jintArray mIntBuffer;
95 jlongArray mLongBuffer;
96 jcharArray mStringBuffer;
97
98public:
99 MyMtpDatabase(JNIEnv *env, jobject client);
100 virtual ~MyMtpDatabase();
101 void cleanup(JNIEnv *env);
102
Mike Lockwoodd815f792010-07-12 08:49:01 -0400103 virtual MtpObjectHandle beginSendObject(const char* path,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400104 MtpObjectFormat format,
105 MtpObjectHandle parent,
106 MtpStorageID storage,
107 uint64_t size,
108 time_t modified);
109
Mike Lockwoodd815f792010-07-12 08:49:01 -0400110 virtual void endSendObject(const char* path,
111 MtpObjectHandle handle,
112 MtpObjectFormat format,
113 bool succeeded);
114
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400115 virtual MtpObjectHandleList* getObjectList(MtpStorageID storageID,
116 MtpObjectFormat format,
117 MtpObjectHandle parent);
118
Mike Lockwood7a047c82010-08-02 10:52:20 -0400119 virtual int getNumObjects(MtpStorageID storageID,
120 MtpObjectFormat format,
121 MtpObjectHandle parent);
122
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400123 // callee should delete[] the results from these
124 // results can be NULL
125 virtual MtpObjectFormatList* getSupportedPlaybackFormats();
126 virtual MtpObjectFormatList* getSupportedCaptureFormats();
127 virtual MtpObjectPropertyList* getSupportedObjectProperties(MtpObjectFormat format);
128 virtual MtpDevicePropertyList* getSupportedDeviceProperties();
129
Mike Lockwood828d19d2010-08-10 15:20:35 -0400130 virtual MtpResponseCode getObjectPropertyValue(MtpObjectHandle handle,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400131 MtpObjectProperty property,
132 MtpDataPacket& packet);
133
Mike Lockwood828d19d2010-08-10 15:20:35 -0400134 virtual MtpResponseCode setObjectPropertyValue(MtpObjectHandle handle,
135 MtpObjectProperty property,
136 MtpDataPacket& packet);
137
138 virtual MtpResponseCode getDevicePropertyValue(MtpDeviceProperty property,
139 MtpDataPacket& packet);
140
141 virtual MtpResponseCode setDevicePropertyValue(MtpDeviceProperty property,
142 MtpDataPacket& packet);
143
144 virtual MtpResponseCode resetDeviceProperty(MtpDeviceProperty property);
145
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400146 virtual MtpResponseCode getObjectPropertyList(MtpObjectHandle handle,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500147 uint32_t format, uint32_t property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400148 int groupCode, int depth,
149 MtpDataPacket& packet);
150
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400151 virtual MtpResponseCode getObjectInfo(MtpObjectHandle handle,
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700152 MtpObjectInfo& info);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400153
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700154 virtual void* getThumbnail(MtpObjectHandle handle, size_t& outThumbSize);
155
Mike Lockwood59c777a2010-08-02 10:37:41 -0400156 virtual MtpResponseCode getObjectFilePath(MtpObjectHandle handle,
Mike Lockwood365e03e2010-12-08 16:08:01 -0800157 MtpString& outFilePath,
158 int64_t& outFileLength,
159 MtpObjectFormat& outFormat);
Mike Lockwood59c777a2010-08-02 10:37:41 -0400160 virtual MtpResponseCode deleteFile(MtpObjectHandle handle);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400161
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400162 bool getObjectPropertyInfo(MtpObjectProperty property, int& type);
163 bool getDevicePropertyInfo(MtpDeviceProperty property, int& type);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400164
165 virtual MtpObjectHandleList* getObjectReferences(MtpObjectHandle handle);
166
167 virtual MtpResponseCode setObjectReferences(MtpObjectHandle handle,
168 MtpObjectHandleList* references);
Mike Lockwood828d19d2010-08-10 15:20:35 -0400169
170 virtual MtpProperty* getObjectPropertyDesc(MtpObjectProperty property,
171 MtpObjectFormat format);
172
173 virtual MtpProperty* getDevicePropertyDesc(MtpDeviceProperty property);
Mike Lockwood2837eef2010-08-31 16:25:12 -0400174
175 virtual void sessionStarted();
176
177 virtual void sessionEnded();
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400178};
179
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400180// ----------------------------------------------------------------------------
181
182static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
183 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000184 ALOGE("An exception was thrown by callback '%s'.", methodName);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400185 LOGE_EX(env);
186 env->ExceptionClear();
187 }
188}
189
190// ----------------------------------------------------------------------------
191
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400192MyMtpDatabase::MyMtpDatabase(JNIEnv *env, jobject client)
193 : mDatabase(env->NewGlobalRef(client)),
194 mIntBuffer(NULL),
195 mLongBuffer(NULL),
196 mStringBuffer(NULL)
197{
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400198 // create buffers for out arguments
199 // we don't need to be thread-safe so this is OK
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700200 jintArray intArray = env->NewIntArray(3);
201 if (!intArray) {
202 return; // Already threw.
203 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400204 mIntBuffer = (jintArray)env->NewGlobalRef(intArray);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700205 jlongArray longArray = env->NewLongArray(2);
206 if (!longArray) {
207 return; // Already threw.
208 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400209 mLongBuffer = (jlongArray)env->NewGlobalRef(longArray);
Elliott Hughes15dd15f2011-04-08 17:42:34 -0700210 jcharArray charArray = env->NewCharArray(256);
211 if (!charArray) {
212 return; // Already threw.
213 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400214 mStringBuffer = (jcharArray)env->NewGlobalRef(charArray);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400215}
216
217void MyMtpDatabase::cleanup(JNIEnv *env) {
218 env->DeleteGlobalRef(mDatabase);
219 env->DeleteGlobalRef(mIntBuffer);
220 env->DeleteGlobalRef(mLongBuffer);
221 env->DeleteGlobalRef(mStringBuffer);
222}
223
224MyMtpDatabase::~MyMtpDatabase() {
225}
226
Mike Lockwoodd815f792010-07-12 08:49:01 -0400227MtpObjectHandle MyMtpDatabase::beginSendObject(const char* path,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400228 MtpObjectFormat format,
229 MtpObjectHandle parent,
230 MtpStorageID storage,
231 uint64_t size,
232 time_t modified) {
233 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood88394712010-09-27 10:01:00 -0400234 jstring pathStr = env->NewStringUTF(path);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400235 MtpObjectHandle result = env->CallIntMethod(mDatabase, method_beginSendObject,
Mike Lockwood88394712010-09-27 10:01:00 -0400236 pathStr, (jint)format, (jint)parent, (jint)storage,
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400237 (jlong)size, (jlong)modified);
238
Mike Lockwood88394712010-09-27 10:01:00 -0400239 if (pathStr)
240 env->DeleteLocalRef(pathStr);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400241 checkAndClearExceptionFromCallback(env, __FUNCTION__);
242 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400243}
244
Mike Lockwoodd815f792010-07-12 08:49:01 -0400245void MyMtpDatabase::endSendObject(const char* path, MtpObjectHandle handle,
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800246 MtpObjectFormat format, bool succeeded) {
Mike Lockwoodd815f792010-07-12 08:49:01 -0400247 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood88394712010-09-27 10:01:00 -0400248 jstring pathStr = env->NewStringUTF(path);
249 env->CallVoidMethod(mDatabase, method_endSendObject, pathStr,
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800250 (jint)handle, (jint)format, (jboolean)succeeded);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400251
Mike Lockwood88394712010-09-27 10:01:00 -0400252 if (pathStr)
253 env->DeleteLocalRef(pathStr);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400254 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400255}
256
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400257MtpObjectHandleList* MyMtpDatabase::getObjectList(MtpStorageID storageID,
258 MtpObjectFormat format,
259 MtpObjectHandle parent) {
260 JNIEnv* env = AndroidRuntime::getJNIEnv();
261 jintArray array = (jintArray)env->CallObjectMethod(mDatabase, method_getObjectList,
262 (jint)storageID, (jint)format, (jint)parent);
263 if (!array)
264 return NULL;
265 MtpObjectHandleList* list = new MtpObjectHandleList();
266 jint* handles = env->GetIntArrayElements(array, 0);
267 jsize length = env->GetArrayLength(array);
Mike Lockwood7a047c82010-08-02 10:52:20 -0400268 for (int i = 0; i < length; i++)
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400269 list->push(handles[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400270 env->ReleaseIntArrayElements(array, handles, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400271 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400272
273 checkAndClearExceptionFromCallback(env, __FUNCTION__);
274 return list;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400275}
276
Mike Lockwood7a047c82010-08-02 10:52:20 -0400277int MyMtpDatabase::getNumObjects(MtpStorageID storageID,
278 MtpObjectFormat format,
279 MtpObjectHandle parent) {
280 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400281 int result = env->CallIntMethod(mDatabase, method_getNumObjects,
Mike Lockwood7a047c82010-08-02 10:52:20 -0400282 (jint)storageID, (jint)format, (jint)parent);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400283
284 checkAndClearExceptionFromCallback(env, __FUNCTION__);
285 return result;
Mike Lockwood7a047c82010-08-02 10:52:20 -0400286}
287
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400288MtpObjectFormatList* MyMtpDatabase::getSupportedPlaybackFormats() {
289 JNIEnv* env = AndroidRuntime::getJNIEnv();
290 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
291 method_getSupportedPlaybackFormats);
292 if (!array)
293 return NULL;
294 MtpObjectFormatList* list = new MtpObjectFormatList();
295 jint* formats = env->GetIntArrayElements(array, 0);
296 jsize length = env->GetArrayLength(array);
297 for (int i = 0; i < length; i++)
298 list->push(formats[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400299 env->ReleaseIntArrayElements(array, formats, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400300 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400301
302 checkAndClearExceptionFromCallback(env, __FUNCTION__);
303 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400304}
305
306MtpObjectFormatList* MyMtpDatabase::getSupportedCaptureFormats() {
307 JNIEnv* env = AndroidRuntime::getJNIEnv();
308 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
309 method_getSupportedCaptureFormats);
310 if (!array)
311 return NULL;
312 MtpObjectFormatList* list = new MtpObjectFormatList();
313 jint* formats = env->GetIntArrayElements(array, 0);
314 jsize length = env->GetArrayLength(array);
315 for (int i = 0; i < length; i++)
316 list->push(formats[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400317 env->ReleaseIntArrayElements(array, formats, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400318 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400319
320 checkAndClearExceptionFromCallback(env, __FUNCTION__);
321 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400322}
323
324MtpObjectPropertyList* MyMtpDatabase::getSupportedObjectProperties(MtpObjectFormat format) {
325 JNIEnv* env = AndroidRuntime::getJNIEnv();
326 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
327 method_getSupportedObjectProperties, (jint)format);
328 if (!array)
329 return NULL;
330 MtpObjectPropertyList* list = new MtpObjectPropertyList();
331 jint* properties = env->GetIntArrayElements(array, 0);
332 jsize length = env->GetArrayLength(array);
333 for (int i = 0; i < length; i++)
334 list->push(properties[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400335 env->ReleaseIntArrayElements(array, properties, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400336 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400337
338 checkAndClearExceptionFromCallback(env, __FUNCTION__);
339 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400340}
341
342MtpDevicePropertyList* MyMtpDatabase::getSupportedDeviceProperties() {
343 JNIEnv* env = AndroidRuntime::getJNIEnv();
344 jintArray array = (jintArray)env->CallObjectMethod(mDatabase,
345 method_getSupportedDeviceProperties);
346 if (!array)
347 return NULL;
348 MtpDevicePropertyList* list = new MtpDevicePropertyList();
349 jint* properties = env->GetIntArrayElements(array, 0);
350 jsize length = env->GetArrayLength(array);
351 for (int i = 0; i < length; i++)
352 list->push(properties[i]);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400353 env->ReleaseIntArrayElements(array, properties, 0);
Mike Lockwood88394712010-09-27 10:01:00 -0400354 env->DeleteLocalRef(array);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400355
356 checkAndClearExceptionFromCallback(env, __FUNCTION__);
357 return list;
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400358}
359
Mike Lockwood828d19d2010-08-10 15:20:35 -0400360MtpResponseCode MyMtpDatabase::getObjectPropertyValue(MtpObjectHandle handle,
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400361 MtpObjectProperty property,
362 MtpDataPacket& packet) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400363 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400364 jobject list = env->CallObjectMethod(mDatabase, method_getObjectPropertyList,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500365 (jlong)handle, 0, (jlong)property, 0, 0);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400366 MtpResponseCode result = env->GetIntField(list, field_mResult);
367 int count = env->GetIntField(list, field_mCount);
368 if (result == MTP_RESPONSE_OK && count != 1)
369 result = MTP_RESPONSE_GENERAL_ERROR;
370
371 if (result == MTP_RESPONSE_OK) {
372 jintArray objectHandlesArray = (jintArray)env->GetObjectField(list, field_mObjectHandles);
373 jintArray propertyCodesArray = (jintArray)env->GetObjectField(list, field_mPropertyCodes);
374 jintArray dataTypesArray = (jintArray)env->GetObjectField(list, field_mDataTypes);
375 jlongArray longValuesArray = (jlongArray)env->GetObjectField(list, field_mLongValues);
376 jobjectArray stringValuesArray = (jobjectArray)env->GetObjectField(list, field_mStringValues);
377
378 jint* objectHandles = env->GetIntArrayElements(objectHandlesArray, 0);
379 jint* propertyCodes = env->GetIntArrayElements(propertyCodesArray, 0);
380 jint* dataTypes = env->GetIntArrayElements(dataTypesArray, 0);
381 jlong* longValues = (longValuesArray ? env->GetLongArrayElements(longValuesArray, 0) : NULL);
382
383 int type = dataTypes[0];
384 jlong longValue = (longValues ? longValues[0] : 0);
385
386 // special case date properties, which are strings to MTP
387 // but stored internally as a uint64
388 if (property == MTP_PROPERTY_DATE_MODIFIED || property == MTP_PROPERTY_DATE_ADDED) {
389 char date[20];
390 formatDateTime(longValue, date, sizeof(date));
391 packet.putString(date);
392 goto out;
393 }
394 // release date is stored internally as just the year
395 if (property == MTP_PROPERTY_ORIGINAL_RELEASE_DATE) {
396 char date[20];
Mark Salyzynaeb75fc2014-03-20 12:09:01 -0700397 snprintf(date, sizeof(date), "%04" PRId64 "0101T000000", longValue);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400398 packet.putString(date);
399 goto out;
400 }
401
402 switch (type) {
403 case MTP_TYPE_INT8:
404 packet.putInt8(longValue);
405 break;
406 case MTP_TYPE_UINT8:
407 packet.putUInt8(longValue);
408 break;
409 case MTP_TYPE_INT16:
410 packet.putInt16(longValue);
411 break;
412 case MTP_TYPE_UINT16:
413 packet.putUInt16(longValue);
414 break;
415 case MTP_TYPE_INT32:
416 packet.putInt32(longValue);
417 break;
418 case MTP_TYPE_UINT32:
419 packet.putUInt32(longValue);
420 break;
421 case MTP_TYPE_INT64:
422 packet.putInt64(longValue);
423 break;
424 case MTP_TYPE_UINT64:
425 packet.putUInt64(longValue);
426 break;
427 case MTP_TYPE_INT128:
428 packet.putInt128(longValue);
429 break;
430 case MTP_TYPE_UINT128:
431 packet.putInt128(longValue);
432 break;
433 case MTP_TYPE_STR:
434 {
435 jstring stringValue = (jstring)env->GetObjectArrayElement(stringValuesArray, 0);
Martin Blumenstingl17a24c52014-05-31 15:50:38 +0200436 const char* str = (stringValue ? env->GetStringUTFChars(stringValue, NULL) : NULL);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400437 if (stringValue) {
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400438 packet.putString(str);
439 env->ReleaseStringUTFChars(stringValue, str);
440 } else {
441 packet.putEmptyString();
442 }
Martin Blumenstingl17a24c52014-05-31 15:50:38 +0200443 env->DeleteLocalRef(stringValue);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400444 break;
445 }
446 default:
Steve Block3762c312012-01-06 19:20:56 +0000447 ALOGE("unsupported type in getObjectPropertyValue\n");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400448 result = MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
449 }
450out:
451 env->ReleaseIntArrayElements(objectHandlesArray, objectHandles, 0);
452 env->ReleaseIntArrayElements(propertyCodesArray, propertyCodes, 0);
453 env->ReleaseIntArrayElements(dataTypesArray, dataTypes, 0);
454 if (longValues)
455 env->ReleaseLongArrayElements(longValuesArray, longValues, 0);
456
457 env->DeleteLocalRef(objectHandlesArray);
458 env->DeleteLocalRef(propertyCodesArray);
459 env->DeleteLocalRef(dataTypesArray);
460 if (longValuesArray)
461 env->DeleteLocalRef(longValuesArray);
462 if (stringValuesArray)
463 env->DeleteLocalRef(stringValuesArray);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400464 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400465
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400466 env->DeleteLocalRef(list);
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400467 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400468 return result;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400469}
470
Mike Lockwood828d19d2010-08-10 15:20:35 -0400471MtpResponseCode MyMtpDatabase::setObjectPropertyValue(MtpObjectHandle handle,
472 MtpObjectProperty property,
473 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400474 int type;
475
476 if (!getObjectPropertyInfo(property, type))
477 return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
478
479 JNIEnv* env = AndroidRuntime::getJNIEnv();
480 jlong longValue = 0;
481 jstring stringValue = NULL;
482
483 switch (type) {
484 case MTP_TYPE_INT8:
485 longValue = packet.getInt8();
486 break;
487 case MTP_TYPE_UINT8:
488 longValue = packet.getUInt8();
489 break;
490 case MTP_TYPE_INT16:
491 longValue = packet.getInt16();
492 break;
493 case MTP_TYPE_UINT16:
494 longValue = packet.getUInt16();
495 break;
496 case MTP_TYPE_INT32:
497 longValue = packet.getInt32();
498 break;
499 case MTP_TYPE_UINT32:
500 longValue = packet.getUInt32();
501 break;
502 case MTP_TYPE_INT64:
503 longValue = packet.getInt64();
504 break;
505 case MTP_TYPE_UINT64:
506 longValue = packet.getUInt64();
507 break;
508 case MTP_TYPE_STR:
509 {
510 MtpStringBuffer buffer;
511 packet.getString(buffer);
512 stringValue = env->NewStringUTF((const char *)buffer);
513 break;
514 }
515 default:
Martin Blumenstingl986b46d2014-05-31 15:53:00 +0200516 ALOGE("unsupported type in setObjectPropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400517 return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
518 }
519
520 jint result = env->CallIntMethod(mDatabase, method_setObjectProperty,
521 (jint)handle, (jint)property, longValue, stringValue);
Mike Lockwood88394712010-09-27 10:01:00 -0400522 if (stringValue)
523 env->DeleteLocalRef(stringValue);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400524
525 checkAndClearExceptionFromCallback(env, __FUNCTION__);
526 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400527}
528
529MtpResponseCode MyMtpDatabase::getDevicePropertyValue(MtpDeviceProperty property,
530 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400531 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood56c85242014-03-07 13:29:08 -0800532
533 if (property == MTP_DEVICE_PROPERTY_BATTERY_LEVEL) {
534 // special case - implemented here instead of Java
535 packet.putUInt8((uint8_t)env->GetIntField(mDatabase, field_batteryLevel));
536 return MTP_RESPONSE_OK;
537 } else {
538 int type;
539
540 if (!getDevicePropertyInfo(property, type))
541 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
542
543 jint result = env->CallIntMethod(mDatabase, method_getDeviceProperty,
544 (jint)property, mLongBuffer, mStringBuffer);
545 if (result != MTP_RESPONSE_OK) {
546 checkAndClearExceptionFromCallback(env, __FUNCTION__);
547 return result;
548 }
549
550 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
551 jlong longValue = longValues[0];
552 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
553
554 switch (type) {
555 case MTP_TYPE_INT8:
556 packet.putInt8(longValue);
557 break;
558 case MTP_TYPE_UINT8:
559 packet.putUInt8(longValue);
560 break;
561 case MTP_TYPE_INT16:
562 packet.putInt16(longValue);
563 break;
564 case MTP_TYPE_UINT16:
565 packet.putUInt16(longValue);
566 break;
567 case MTP_TYPE_INT32:
568 packet.putInt32(longValue);
569 break;
570 case MTP_TYPE_UINT32:
571 packet.putUInt32(longValue);
572 break;
573 case MTP_TYPE_INT64:
574 packet.putInt64(longValue);
575 break;
576 case MTP_TYPE_UINT64:
577 packet.putUInt64(longValue);
578 break;
579 case MTP_TYPE_INT128:
580 packet.putInt128(longValue);
581 break;
582 case MTP_TYPE_UINT128:
583 packet.putInt128(longValue);
584 break;
585 case MTP_TYPE_STR:
586 {
587 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
588 packet.putString(str);
589 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
590 break;
591 }
592 default:
593 ALOGE("unsupported type in getDevicePropertyValue\n");
594 return MTP_RESPONSE_INVALID_DEVICE_PROP_FORMAT;
595 }
596
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400597 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood56c85242014-03-07 13:29:08 -0800598 return MTP_RESPONSE_OK;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400599 }
Mike Lockwood828d19d2010-08-10 15:20:35 -0400600}
601
602MtpResponseCode MyMtpDatabase::setDevicePropertyValue(MtpDeviceProperty property,
603 MtpDataPacket& packet) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400604 int type;
605
606 if (!getDevicePropertyInfo(property, type))
607 return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
608
609 JNIEnv* env = AndroidRuntime::getJNIEnv();
610 jlong longValue = 0;
611 jstring stringValue = NULL;
612
613 switch (type) {
614 case MTP_TYPE_INT8:
615 longValue = packet.getInt8();
616 break;
617 case MTP_TYPE_UINT8:
618 longValue = packet.getUInt8();
619 break;
620 case MTP_TYPE_INT16:
621 longValue = packet.getInt16();
622 break;
623 case MTP_TYPE_UINT16:
624 longValue = packet.getUInt16();
625 break;
626 case MTP_TYPE_INT32:
627 longValue = packet.getInt32();
628 break;
629 case MTP_TYPE_UINT32:
630 longValue = packet.getUInt32();
631 break;
632 case MTP_TYPE_INT64:
633 longValue = packet.getInt64();
634 break;
635 case MTP_TYPE_UINT64:
636 longValue = packet.getUInt64();
637 break;
638 case MTP_TYPE_STR:
639 {
640 MtpStringBuffer buffer;
641 packet.getString(buffer);
642 stringValue = env->NewStringUTF((const char *)buffer);
643 break;
644 }
645 default:
Steve Block3762c312012-01-06 19:20:56 +0000646 ALOGE("unsupported type in setDevicePropertyValue\n");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400647 return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
648 }
649
650 jint result = env->CallIntMethod(mDatabase, method_setDeviceProperty,
651 (jint)property, longValue, stringValue);
Mike Lockwood88394712010-09-27 10:01:00 -0400652 if (stringValue)
653 env->DeleteLocalRef(stringValue);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400654
655 checkAndClearExceptionFromCallback(env, __FUNCTION__);
656 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -0400657}
658
Mark Salyzynaeb75fc2014-03-20 12:09:01 -0700659MtpResponseCode MyMtpDatabase::resetDeviceProperty(MtpDeviceProperty /*property*/) {
Mike Lockwood828d19d2010-08-10 15:20:35 -0400660 return -1;
661}
662
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400663MtpResponseCode MyMtpDatabase::getObjectPropertyList(MtpObjectHandle handle,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500664 uint32_t format, uint32_t property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400665 int groupCode, int depth,
666 MtpDataPacket& packet) {
667 JNIEnv* env = AndroidRuntime::getJNIEnv();
668 jobject list = env->CallObjectMethod(mDatabase, method_getObjectPropertyList,
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500669 (jlong)handle, (jint)format, (jlong)property, (jint)groupCode, (jint)depth);
670 checkAndClearExceptionFromCallback(env, __FUNCTION__);
671 if (!list)
672 return MTP_RESPONSE_GENERAL_ERROR;
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400673 int count = env->GetIntField(list, field_mCount);
674 MtpResponseCode result = env->GetIntField(list, field_mResult);
675
676 packet.putUInt32(count);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400677 if (count > 0) {
678 jintArray objectHandlesArray = (jintArray)env->GetObjectField(list, field_mObjectHandles);
679 jintArray propertyCodesArray = (jintArray)env->GetObjectField(list, field_mPropertyCodes);
680 jintArray dataTypesArray = (jintArray)env->GetObjectField(list, field_mDataTypes);
681 jlongArray longValuesArray = (jlongArray)env->GetObjectField(list, field_mLongValues);
682 jobjectArray stringValuesArray = (jobjectArray)env->GetObjectField(list, field_mStringValues);
683
684 jint* objectHandles = env->GetIntArrayElements(objectHandlesArray, 0);
685 jint* propertyCodes = env->GetIntArrayElements(propertyCodesArray, 0);
686 jint* dataTypes = env->GetIntArrayElements(dataTypesArray, 0);
687 jlong* longValues = (longValuesArray ? env->GetLongArrayElements(longValuesArray, 0) : NULL);
688
689 for (int i = 0; i < count; i++) {
690 packet.putUInt32(objectHandles[i]);
691 packet.putUInt16(propertyCodes[i]);
692 int type = dataTypes[i];
693 packet.putUInt16(type);
694
695 switch (type) {
696 case MTP_TYPE_INT8:
697 packet.putInt8(longValues[i]);
698 break;
699 case MTP_TYPE_UINT8:
700 packet.putUInt8(longValues[i]);
701 break;
702 case MTP_TYPE_INT16:
703 packet.putInt16(longValues[i]);
704 break;
705 case MTP_TYPE_UINT16:
706 packet.putUInt16(longValues[i]);
707 break;
708 case MTP_TYPE_INT32:
709 packet.putInt32(longValues[i]);
710 break;
711 case MTP_TYPE_UINT32:
712 packet.putUInt32(longValues[i]);
713 break;
714 case MTP_TYPE_INT64:
715 packet.putInt64(longValues[i]);
716 break;
717 case MTP_TYPE_UINT64:
718 packet.putUInt64(longValues[i]);
719 break;
720 case MTP_TYPE_INT128:
721 packet.putInt128(longValues[i]);
722 break;
723 case MTP_TYPE_UINT128:
724 packet.putUInt128(longValues[i]);
725 break;
726 case MTP_TYPE_STR: {
727 jstring value = (jstring)env->GetObjectArrayElement(stringValuesArray, i);
Mike Lockwood2711e492010-12-11 11:24:37 -0800728 const char *valueStr = (value ? env->GetStringUTFChars(value, NULL) : NULL);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400729 if (valueStr) {
730 packet.putString(valueStr);
731 env->ReleaseStringUTFChars(value, valueStr);
732 } else {
733 packet.putEmptyString();
734 }
735 env->DeleteLocalRef(value);
736 break;
737 }
738 default:
Steve Block3762c312012-01-06 19:20:56 +0000739 ALOGE("bad or unsupported data type in MyMtpDatabase::getObjectPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400740 break;
741 }
742 }
743
744 env->ReleaseIntArrayElements(objectHandlesArray, objectHandles, 0);
745 env->ReleaseIntArrayElements(propertyCodesArray, propertyCodes, 0);
746 env->ReleaseIntArrayElements(dataTypesArray, dataTypes, 0);
747 if (longValues)
748 env->ReleaseLongArrayElements(longValuesArray, longValues, 0);
749
750 env->DeleteLocalRef(objectHandlesArray);
751 env->DeleteLocalRef(propertyCodesArray);
752 env->DeleteLocalRef(dataTypesArray);
753 if (longValuesArray)
754 env->DeleteLocalRef(longValuesArray);
755 if (stringValuesArray)
756 env->DeleteLocalRef(stringValuesArray);
757 }
758
759 env->DeleteLocalRef(list);
760 checkAndClearExceptionFromCallback(env, __FUNCTION__);
761 return result;
762}
763
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800764static void foreachentry(ExifEntry *entry, void *user) {
765 char buf[1024];
766 ALOGI("entry %x, format %d, size %d: %s",
767 entry->tag, entry->format, entry->size, exif_entry_get_value(entry, buf, sizeof(buf)));
768}
769
770static void foreachcontent(ExifContent *content, void *user) {
771 ALOGI("content %d", exif_content_get_ifd(content));
772 exif_content_foreach_entry(content, foreachentry, user);
773}
774
775static long getLongFromExifEntry(ExifEntry *e) {
776 ExifByteOrder o = exif_data_get_byte_order(e->parent->parent);
777 return exif_get_long(e->data, o);
778}
779
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400780MtpResponseCode MyMtpDatabase::getObjectInfo(MtpObjectHandle handle,
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700781 MtpObjectInfo& info) {
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700782 char date[20];
783 MtpString path;
784 int64_t length;
785 MtpObjectFormat format;
786
787 MtpResponseCode result = getObjectFilePath(handle, path, length, format);
788 if (result != MTP_RESPONSE_OK) {
789 return result;
790 }
791 info.mCompressedSize = (length > 0xFFFFFFFFLL ? 0xFFFFFFFF : (uint32_t)length);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400792
793 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700794 if (!env->CallBooleanMethod(mDatabase, method_getObjectInfo,
795 (jint)handle, mIntBuffer, mStringBuffer, mLongBuffer)) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400796 return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700797 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400798
799 jint* intValues = env->GetIntArrayElements(mIntBuffer, 0);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700800 info.mStorageID = intValues[0];
801 info.mFormat = intValues[1];
802 info.mParent = intValues[2];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400803 env->ReleaseIntArrayElements(mIntBuffer, intValues, 0);
804
805 jlong* longValues = env->GetLongArrayElements(mLongBuffer, 0);
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700806 info.mDateCreated = longValues[0];
807 info.mDateModified = longValues[1];
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400808 env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
809
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700810// info.mAssociationType = (format == MTP_FORMAT_ASSOCIATION ?
Mike Lockwood828d19d2010-08-10 15:20:35 -0400811// MTP_ASSOCIATION_TYPE_GENERIC_FOLDER :
812// MTP_ASSOCIATION_TYPE_UNDEFINED);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700813 info.mAssociationType = MTP_ASSOCIATION_TYPE_UNDEFINED;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400814
815 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700816 MtpString temp(str);
817 info.mName = strdup((const char *)temp);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400818 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
819
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700820 // read EXIF data for thumbnail information
821 if (info.mFormat == MTP_FORMAT_EXIF_JPEG || info.mFormat == MTP_FORMAT_JFIF) {
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800822
823 ExifData *exifdata = exif_data_new_from_file(path);
824 if (exifdata) {
825 //exif_data_foreach_content(exifdata, foreachcontent, NULL);
826
827 // XXX get this from exif, or parse jpeg header instead?
828 ExifEntry *w = exif_content_get_entry(
829 exifdata->ifd[EXIF_IFD_EXIF], EXIF_TAG_PIXEL_X_DIMENSION);
830 ExifEntry *h = exif_content_get_entry(
831 exifdata->ifd[EXIF_IFD_EXIF], EXIF_TAG_PIXEL_Y_DIMENSION);
832 info.mThumbCompressedSize = exifdata->data ? exifdata->size : 0;
833 info.mThumbFormat = MTP_FORMAT_EXIF_JPEG;
Marco Nelissen0937eed2014-01-22 15:10:57 -0800834 info.mImagePixWidth = w ? getLongFromExifEntry(w) : 0;
835 info.mImagePixHeight = h ? getLongFromExifEntry(h) : 0;
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800836 exif_data_unref(exifdata);
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700837 }
838 }
839
Mike Lockwood0a7fa0a2010-08-24 11:25:28 -0400840 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400841 return MTP_RESPONSE_OK;
842}
843
Mike Lockwoodc89f2222011-04-24 18:40:17 -0700844void* MyMtpDatabase::getThumbnail(MtpObjectHandle handle, size_t& outThumbSize) {
845 MtpString path;
846 int64_t length;
847 MtpObjectFormat format;
848 void* result = NULL;
849 outThumbSize = 0;
850
851 if (getObjectFilePath(handle, path, length, format) == MTP_RESPONSE_OK
852 && (format == MTP_FORMAT_EXIF_JPEG || format == MTP_FORMAT_JFIF)) {
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800853
854 ExifData *exifdata = exif_data_new_from_file(path);
855 if (exifdata) {
856 if (exifdata->data) {
857 result = malloc(exifdata->size);
858 if (result) {
859 memcpy(result, exifdata->data, exifdata->size);
Mike Lockwoodc4367722014-04-21 08:49:30 -0700860 outThumbSize = exifdata->size;
Marco Nelissen3cd393c2014-01-10 10:39:27 -0800861 }
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) {
Mike Lockwood92b53bc2014-03-13 14:51:29 -07001003 static const int channelEnum[] = {
1004 1, // mono
1005 2, // stereo
1006 3, // 2.1
1007 4, // 3
1008 5, // 3.1
1009 6, // 4
1010 7, // 4.1
1011 8, // 5
1012 9, // 5.1
1013 };
1014 static const int bitrateEnum[] = {
1015 1, // fixed rate
1016 2, // variable rate
1017 };
1018
Mike Lockwood828d19d2010-08-10 15:20:35 -04001019 MtpProperty* result = NULL;
1020 switch (property) {
1021 case MTP_PROPERTY_OBJECT_FORMAT:
Mike Lockwood9b5e9c42010-12-07 18:53:50 -08001022 // use format as default value
1023 result = new MtpProperty(property, MTP_TYPE_UINT16, false, format);
1024 break;
Mike Lockwood828d19d2010-08-10 15:20:35 -04001025 case MTP_PROPERTY_PROTECTION_STATUS:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001026 case MTP_PROPERTY_TRACK:
Mike Lockwood828d19d2010-08-10 15:20:35 -04001027 result = new MtpProperty(property, MTP_TYPE_UINT16);
1028 break;
1029 case MTP_PROPERTY_STORAGE_ID:
1030 case MTP_PROPERTY_PARENT_OBJECT:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001031 case MTP_PROPERTY_DURATION:
Mike Lockwood92b53bc2014-03-13 14:51:29 -07001032 case MTP_PROPERTY_AUDIO_WAVE_CODEC:
Mike Lockwood828d19d2010-08-10 15:20:35 -04001033 result = new MtpProperty(property, MTP_TYPE_UINT32);
1034 break;
1035 case MTP_PROPERTY_OBJECT_SIZE:
1036 result = new MtpProperty(property, MTP_TYPE_UINT64);
1037 break;
1038 case MTP_PROPERTY_PERSISTENT_UID:
1039 result = new MtpProperty(property, MTP_TYPE_UINT128);
1040 break;
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -04001041 case MTP_PROPERTY_NAME:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001042 case MTP_PROPERTY_DISPLAY_NAME:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001043 case MTP_PROPERTY_ARTIST:
1044 case MTP_PROPERTY_ALBUM_NAME:
1045 case MTP_PROPERTY_ALBUM_ARTIST:
Mike Lockwoodae078f72010-09-26 12:35:51 -04001046 case MTP_PROPERTY_GENRE:
1047 case MTP_PROPERTY_COMPOSER:
1048 case MTP_PROPERTY_DESCRIPTION:
Mike Lockwood828d19d2010-08-10 15:20:35 -04001049 result = new MtpProperty(property, MTP_TYPE_STR);
1050 break;
Mike Lockwood5b19af02010-11-23 18:38:55 -05001051 case MTP_PROPERTY_DATE_MODIFIED:
1052 case MTP_PROPERTY_DATE_ADDED:
1053 case MTP_PROPERTY_ORIGINAL_RELEASE_DATE:
1054 result = new MtpProperty(property, MTP_TYPE_STR);
1055 result->setFormDateTime();
1056 break;
Mike Lockwood5ebac832010-10-12 11:33:47 -04001057 case MTP_PROPERTY_OBJECT_FILE_NAME:
Mike Lockwood6a6a3af2010-10-12 14:19:51 -04001058 // We allow renaming files and folders
1059 result = new MtpProperty(property, MTP_TYPE_STR, true);
Mike Lockwood5ebac832010-10-12 11:33:47 -04001060 break;
Mike Lockwood92b53bc2014-03-13 14:51:29 -07001061 case MTP_PROPERTY_BITRATE_TYPE:
1062 result = new MtpProperty(property, MTP_TYPE_UINT16);
1063 result->setFormEnum(bitrateEnum, sizeof(bitrateEnum)/sizeof(bitrateEnum[0]));
1064 break;
1065 case MTP_PROPERTY_AUDIO_BITRATE:
1066 result = new MtpProperty(property, MTP_TYPE_UINT32);
1067 result->setFormRange(1, 1536000, 1);
1068 break;
1069 case MTP_PROPERTY_NUMBER_OF_CHANNELS:
1070 result = new MtpProperty(property, MTP_TYPE_UINT16);
1071 result->setFormEnum(channelEnum, sizeof(channelEnum)/sizeof(channelEnum[0]));
1072 break;
1073 case MTP_PROPERTY_SAMPLE_RATE:
1074 result = new MtpProperty(property, MTP_TYPE_UINT32);
1075 result->setFormRange(8000, 48000, 1);
1076 break;
Mike Lockwood828d19d2010-08-10 15:20:35 -04001077 }
1078
1079 return result;
1080}
1081
1082MtpProperty* MyMtpDatabase::getDevicePropertyDesc(MtpDeviceProperty property) {
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001083 JNIEnv* env = AndroidRuntime::getJNIEnv();
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001084 MtpProperty* result = NULL;
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001085 bool writable = false;
1086
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001087 switch (property) {
1088 case MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
1089 case MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001090 writable = true;
1091 // fall through
Mike Lockwood56c85242014-03-07 13:29:08 -08001092 case MTP_DEVICE_PROPERTY_IMAGE_SIZE: {
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001093 result = new MtpProperty(property, MTP_TYPE_STR, writable);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001094
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001095 // get current value
Mike Lockwooda2a21282010-09-25 21:21:05 -04001096 jint ret = env->CallIntMethod(mDatabase, method_getDeviceProperty,
1097 (jint)property, mLongBuffer, mStringBuffer);
1098 if (ret == MTP_RESPONSE_OK) {
1099 jchar* str = env->GetCharArrayElements(mStringBuffer, 0);
1100 result->setCurrentValue(str);
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001101 // for read-only properties it is safe to assume current value is default value
1102 if (!writable)
1103 result->setDefaultValue(str);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001104 env->ReleaseCharArrayElements(mStringBuffer, str, 0);
1105 } else {
Steve Block3762c312012-01-06 19:20:56 +00001106 ALOGE("unable to read device property, response: %04X", ret);
Mike Lockwooda2a21282010-09-25 21:21:05 -04001107 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001108 break;
Mike Lockwood56c85242014-03-07 13:29:08 -08001109 }
1110 case MTP_DEVICE_PROPERTY_BATTERY_LEVEL:
1111 result = new MtpProperty(property, MTP_TYPE_UINT8);
1112 result->setFormRange(0, env->GetIntField(mDatabase, field_batteryScale), 1);
1113 result->mCurrentValue.u.u8 = (uint8_t)env->GetIntField(mDatabase, field_batteryLevel);
1114 break;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001115 }
1116
Mike Lockwoodea93fa12010-12-07 10:41:35 -08001117 checkAndClearExceptionFromCallback(env, __FUNCTION__);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001118 return result;
Mike Lockwood828d19d2010-08-10 15:20:35 -04001119}
1120
Mike Lockwood2837eef2010-08-31 16:25:12 -04001121void MyMtpDatabase::sessionStarted() {
1122 JNIEnv* env = AndroidRuntime::getJNIEnv();
1123 env->CallVoidMethod(mDatabase, method_sessionStarted);
1124 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1125}
1126
1127void MyMtpDatabase::sessionEnded() {
1128 JNIEnv* env = AndroidRuntime::getJNIEnv();
1129 env->CallVoidMethod(mDatabase, method_sessionEnded);
1130 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1131}
1132
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001133// ----------------------------------------------------------------------------
1134
1135static void
Mike Lockwood0cd01362010-12-30 11:54:33 -05001136android_mtp_MtpDatabase_setup(JNIEnv *env, jobject thiz)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001137{
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001138 MyMtpDatabase* database = new MyMtpDatabase(env, thiz);
Ashok Bhate2e59322013-12-17 19:04:19 +00001139 env->SetLongField(thiz, field_context, (jlong)database);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001140 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1141}
1142
1143static void
Mike Lockwood0cd01362010-12-30 11:54:33 -05001144android_mtp_MtpDatabase_finalize(JNIEnv *env, jobject thiz)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001145{
Ashok Bhate2e59322013-12-17 19:04:19 +00001146 MyMtpDatabase* database = (MyMtpDatabase *)env->GetLongField(thiz, field_context);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001147 database->cleanup(env);
1148 delete database;
Ashok Bhate2e59322013-12-17 19:04:19 +00001149 env->SetLongField(thiz, field_context, 0);
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001150 checkAndClearExceptionFromCallback(env, __FUNCTION__);
1151}
1152
Mike Lockwood31599912010-11-15 13:43:30 -05001153static jstring
Mark Salyzynaeb75fc2014-03-20 12:09:01 -07001154android_mtp_MtpPropertyGroup_format_date_time(JNIEnv *env, jobject /*thiz*/, jlong seconds)
Mike Lockwood31599912010-11-15 13:43:30 -05001155{
Mike Lockwood31599912010-11-15 13:43:30 -05001156 char date[20];
1157 formatDateTime(seconds, date, sizeof(date));
1158 return env->NewStringUTF(date);
Mike Lockwood31599912010-11-15 13:43:30 -05001159}
1160
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001161// ----------------------------------------------------------------------------
1162
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001163static JNINativeMethod gMtpDatabaseMethods[] = {
Mike Lockwood0cd01362010-12-30 11:54:33 -05001164 {"native_setup", "()V", (void *)android_mtp_MtpDatabase_setup},
1165 {"native_finalize", "()V", (void *)android_mtp_MtpDatabase_finalize},
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001166};
1167
1168static JNINativeMethod gMtpPropertyGroupMethods[] = {
Mike Lockwood31599912010-11-15 13:43:30 -05001169 {"format_date_time", "(J)Ljava/lang/String;",
Mike Lockwood0cd01362010-12-30 11:54:33 -05001170 (void *)android_mtp_MtpPropertyGroup_format_date_time},
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001171};
1172
Mike Lockwood0cd01362010-12-30 11:54:33 -05001173static const char* const kClassPathName = "android/mtp/MtpDatabase";
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001174
Mike Lockwood0cd01362010-12-30 11:54:33 -05001175int register_android_mtp_MtpDatabase(JNIEnv *env)
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001176{
1177 jclass clazz;
1178
Mike Lockwood0cd01362010-12-30 11:54:33 -05001179 clazz = env->FindClass("android/mtp/MtpDatabase");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001180 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001181 ALOGE("Can't find android/mtp/MtpDatabase");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001182 return -1;
1183 }
Mike Lockwoodd815f792010-07-12 08:49:01 -04001184 method_beginSendObject = env->GetMethodID(clazz, "beginSendObject", "(Ljava/lang/String;IIIJJ)I");
1185 if (method_beginSendObject == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001186 ALOGE("Can't find beginSendObject");
Mike Lockwoodd815f792010-07-12 08:49:01 -04001187 return -1;
1188 }
Mike Lockwood7a0bd172011-01-18 11:06:19 -08001189 method_endSendObject = env->GetMethodID(clazz, "endSendObject", "(Ljava/lang/String;IIZ)V");
Mike Lockwoodd815f792010-07-12 08:49:01 -04001190 if (method_endSendObject == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001191 ALOGE("Can't find endSendObject");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001192 return -1;
1193 }
1194 method_getObjectList = env->GetMethodID(clazz, "getObjectList", "(III)[I");
1195 if (method_getObjectList == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001196 ALOGE("Can't find getObjectList");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001197 return -1;
1198 }
Mike Lockwood7a047c82010-08-02 10:52:20 -04001199 method_getNumObjects = env->GetMethodID(clazz, "getNumObjects", "(III)I");
1200 if (method_getNumObjects == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001201 ALOGE("Can't find getNumObjects");
Mike Lockwood7a047c82010-08-02 10:52:20 -04001202 return -1;
1203 }
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001204 method_getSupportedPlaybackFormats = env->GetMethodID(clazz, "getSupportedPlaybackFormats", "()[I");
1205 if (method_getSupportedPlaybackFormats == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001206 ALOGE("Can't find getSupportedPlaybackFormats");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001207 return -1;
1208 }
1209 method_getSupportedCaptureFormats = env->GetMethodID(clazz, "getSupportedCaptureFormats", "()[I");
1210 if (method_getSupportedCaptureFormats == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001211 ALOGE("Can't find getSupportedCaptureFormats");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001212 return -1;
1213 }
1214 method_getSupportedObjectProperties = env->GetMethodID(clazz, "getSupportedObjectProperties", "(I)[I");
1215 if (method_getSupportedObjectProperties == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001216 ALOGE("Can't find getSupportedObjectProperties");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001217 return -1;
1218 }
1219 method_getSupportedDeviceProperties = env->GetMethodID(clazz, "getSupportedDeviceProperties", "()[I");
1220 if (method_getSupportedDeviceProperties == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001221 ALOGE("Can't find getSupportedDeviceProperties");
Mike Lockwood4b322ce2010-08-10 07:37:50 -04001222 return -1;
1223 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001224 method_setObjectProperty = env->GetMethodID(clazz, "setObjectProperty", "(IIJLjava/lang/String;)I");
1225 if (method_setObjectProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001226 ALOGE("Can't find setObjectProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001227 return -1;
1228 }
1229 method_getDeviceProperty = env->GetMethodID(clazz, "getDeviceProperty", "(I[J[C)I");
1230 if (method_getDeviceProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001231 ALOGE("Can't find getDeviceProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001232 return -1;
1233 }
1234 method_setDeviceProperty = env->GetMethodID(clazz, "setDeviceProperty", "(IJLjava/lang/String;)I");
1235 if (method_setDeviceProperty == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001236 ALOGE("Can't find setDeviceProperty");
Mike Lockwood59e3f0d2010-09-02 14:57:30 -04001237 return -1;
1238 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001239 method_getObjectPropertyList = env->GetMethodID(clazz, "getObjectPropertyList",
Mike Lockwood0cd01362010-12-30 11:54:33 -05001240 "(JIJII)Landroid/mtp/MtpPropertyList;");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001241 if (method_getObjectPropertyList == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001242 ALOGE("Can't find getObjectPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001243 return -1;
1244 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001245 method_getObjectInfo = env->GetMethodID(clazz, "getObjectInfo", "(I[I[C[J)Z");
1246 if (method_getObjectInfo == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001247 ALOGE("Can't find getObjectInfo");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001248 return -1;
1249 }
Mike Lockwood59c777a2010-08-02 10:37:41 -04001250 method_getObjectFilePath = env->GetMethodID(clazz, "getObjectFilePath", "(I[C[J)I");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001251 if (method_getObjectFilePath == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001252 ALOGE("Can't find getObjectFilePath");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001253 return -1;
1254 }
Mike Lockwood59c777a2010-08-02 10:37:41 -04001255 method_deleteFile = env->GetMethodID(clazz, "deleteFile", "(I)I");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001256 if (method_deleteFile == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001257 ALOGE("Can't find deleteFile");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001258 return -1;
1259 }
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001260 method_getObjectReferences = env->GetMethodID(clazz, "getObjectReferences", "(I)[I");
1261 if (method_getObjectReferences == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001262 ALOGE("Can't find getObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001263 return -1;
1264 }
1265 method_setObjectReferences = env->GetMethodID(clazz, "setObjectReferences", "(I[I)I");
1266 if (method_setObjectReferences == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001267 ALOGE("Can't find setObjectReferences");
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001268 return -1;
1269 }
Mike Lockwood2837eef2010-08-31 16:25:12 -04001270 method_sessionStarted = env->GetMethodID(clazz, "sessionStarted", "()V");
1271 if (method_sessionStarted == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001272 ALOGE("Can't find sessionStarted");
Mike Lockwood2837eef2010-08-31 16:25:12 -04001273 return -1;
1274 }
1275 method_sessionEnded = env->GetMethodID(clazz, "sessionEnded", "()V");
1276 if (method_sessionEnded == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001277 ALOGE("Can't find sessionEnded");
Mike Lockwood2837eef2010-08-31 16:25:12 -04001278 return -1;
1279 }
1280
Ashok Bhate2e59322013-12-17 19:04:19 +00001281 field_context = env->GetFieldID(clazz, "mNativeContext", "J");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001282 if (field_context == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001283 ALOGE("Can't find MtpDatabase.mNativeContext");
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001284 return -1;
1285 }
Mike Lockwood56c85242014-03-07 13:29:08 -08001286 field_batteryLevel = env->GetFieldID(clazz, "mBatteryLevel", "I");
1287 if (field_batteryLevel == NULL) {
1288 ALOGE("Can't find MtpDatabase.mBatteryLevel");
1289 return -1;
1290 }
1291 field_batteryScale = env->GetFieldID(clazz, "mBatteryScale", "I");
1292 if (field_batteryScale == NULL) {
1293 ALOGE("Can't find MtpDatabase.mBatteryScale");
1294 return -1;
1295 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001296
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001297 // now set up fields for MtpPropertyList class
Mike Lockwood0cd01362010-12-30 11:54:33 -05001298 clazz = env->FindClass("android/mtp/MtpPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001299 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001300 ALOGE("Can't find android/mtp/MtpPropertyList");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001301 return -1;
1302 }
1303 field_mCount = env->GetFieldID(clazz, "mCount", "I");
1304 if (field_mCount == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001305 ALOGE("Can't find MtpPropertyList.mCount");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001306 return -1;
1307 }
1308 field_mResult = env->GetFieldID(clazz, "mResult", "I");
1309 if (field_mResult == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001310 ALOGE("Can't find MtpPropertyList.mResult");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001311 return -1;
1312 }
1313 field_mObjectHandles = env->GetFieldID(clazz, "mObjectHandles", "[I");
1314 if (field_mObjectHandles == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001315 ALOGE("Can't find MtpPropertyList.mObjectHandles");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001316 return -1;
1317 }
1318 field_mPropertyCodes = env->GetFieldID(clazz, "mPropertyCodes", "[I");
1319 if (field_mPropertyCodes == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001320 ALOGE("Can't find MtpPropertyList.mPropertyCodes");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001321 return -1;
1322 }
1323 field_mDataTypes = env->GetFieldID(clazz, "mDataTypes", "[I");
1324 if (field_mDataTypes == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001325 ALOGE("Can't find MtpPropertyList.mDataTypes");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001326 return -1;
1327 }
1328 field_mLongValues = env->GetFieldID(clazz, "mLongValues", "[J");
1329 if (field_mLongValues == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001330 ALOGE("Can't find MtpPropertyList.mLongValues");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001331 return -1;
1332 }
1333 field_mStringValues = env->GetFieldID(clazz, "mStringValues", "[Ljava/lang/String;");
1334 if (field_mStringValues == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001335 ALOGE("Can't find MtpPropertyList.mStringValues");
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -04001336 return -1;
1337 }
1338
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001339 if (AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -05001340 "android/mtp/MtpDatabase", gMtpDatabaseMethods, NELEM(gMtpDatabaseMethods)))
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001341 return -1;
1342
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001343 return AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -05001344 "android/mtp/MtpPropertyGroup", gMtpPropertyGroupMethods, NELEM(gMtpPropertyGroupMethods));
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001345}