blob: 8e013a03316e26212a94903a5cf6affedc159685 [file] [log] [blame]
Mike Lockwood8182e722010-12-30 15:38:45 -05001/*
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_NDEBUG 0
18
19#define LOG_TAG "MtpDeviceJNI"
20#include "utils/Log.h"
21
22#include <stdio.h>
23#include <assert.h>
24#include <limits.h>
25#include <unistd.h>
26#include <fcntl.h>
27
28#include "jni.h"
29#include "JNIHelp.h"
30#include "android_runtime/AndroidRuntime.h"
Ruben Brunk87eac992013-09-09 17:44:59 -070031#include "android_runtime/Log.h"
Mike Lockwood8182e722010-12-30 15:38:45 -050032#include "private/android_filesystem_config.h"
33
34#include "MtpTypes.h"
35#include "MtpDevice.h"
36#include "MtpDeviceInfo.h"
37#include "MtpStorageInfo.h"
38#include "MtpObjectInfo.h"
39
40using namespace android;
41
42// ----------------------------------------------------------------------------
43
44static jfieldID field_context;
45
46jclass clazz_deviceInfo;
47jclass clazz_storageInfo;
48jclass clazz_objectInfo;
49
50jmethodID constructor_deviceInfo;
51jmethodID constructor_storageInfo;
52jmethodID constructor_objectInfo;
53
54// MtpDeviceInfo fields
55static jfieldID field_deviceInfo_manufacturer;
56static jfieldID field_deviceInfo_model;
57static jfieldID field_deviceInfo_version;
58static jfieldID field_deviceInfo_serialNumber;
59
60// MtpStorageInfo fields
61static jfieldID field_storageInfo_storageId;
62static jfieldID field_storageInfo_maxCapacity;
63static jfieldID field_storageInfo_freeSpace;
64static jfieldID field_storageInfo_description;
65static jfieldID field_storageInfo_volumeIdentifier;
66
67// MtpObjectInfo fields
68static jfieldID field_objectInfo_handle;
69static jfieldID field_objectInfo_storageId;
70static jfieldID field_objectInfo_format;
71static jfieldID field_objectInfo_protectionStatus;
72static jfieldID field_objectInfo_compressedSize;
73static jfieldID field_objectInfo_thumbFormat;
74static jfieldID field_objectInfo_thumbCompressedSize;
75static jfieldID field_objectInfo_thumbPixWidth;
76static jfieldID field_objectInfo_thumbPixHeight;
77static jfieldID field_objectInfo_imagePixWidth;
78static jfieldID field_objectInfo_imagePixHeight;
79static jfieldID field_objectInfo_imagePixDepth;
80static jfieldID field_objectInfo_parent;
81static jfieldID field_objectInfo_associationType;
82static jfieldID field_objectInfo_associationDesc;
83static jfieldID field_objectInfo_sequenceNumber;
84static jfieldID field_objectInfo_name;
85static jfieldID field_objectInfo_dateCreated;
86static jfieldID field_objectInfo_dateModified;
87static jfieldID field_objectInfo_keywords;
88
Mike Lockwood8182e722010-12-30 15:38:45 -050089MtpDevice* get_device_from_object(JNIEnv* env, jobject javaDevice)
90{
Ashok Bhate2e59322013-12-17 19:04:19 +000091 return (MtpDevice*)env->GetLongField(javaDevice, field_context);
Mike Lockwood8182e722010-12-30 15:38:45 -050092}
93
94static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
95 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +000096 ALOGE("An exception was thrown by callback '%s'.", methodName);
Mike Lockwood8182e722010-12-30 15:38:45 -050097 LOGE_EX(env);
98 env->ExceptionClear();
99 }
100}
101
Mike Lockwood8182e722010-12-30 15:38:45 -0500102// ----------------------------------------------------------------------------
103
104static jboolean
105android_mtp_MtpDevice_open(JNIEnv *env, jobject thiz, jstring deviceName, jint fd)
106{
Mike Lockwood8182e722010-12-30 15:38:45 -0500107 const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL);
James Dong39774722011-04-06 11:57:48 -0700108 if (deviceNameStr == NULL) {
Ashok Bhate2e59322013-12-17 19:04:19 +0000109 return JNI_FALSE;
James Dong39774722011-04-06 11:57:48 -0700110 }
111
Mike Lockwood8182e722010-12-30 15:38:45 -0500112 MtpDevice* device = MtpDevice::open(deviceNameStr, fd);
113 env->ReleaseStringUTFChars(deviceName, deviceNameStr);
114
115 if (device)
Ashok Bhate2e59322013-12-17 19:04:19 +0000116 env->SetLongField(thiz, field_context, (jlong)device);
117 return (jboolean)(device != NULL);
Mike Lockwood8182e722010-12-30 15:38:45 -0500118}
119
120static void
121android_mtp_MtpDevice_close(JNIEnv *env, jobject thiz)
122{
Mike Lockwood8182e722010-12-30 15:38:45 -0500123 MtpDevice* device = get_device_from_object(env, thiz);
124 if (device) {
125 device->close();
126 delete device;
Ashok Bhate2e59322013-12-17 19:04:19 +0000127 env->SetLongField(thiz, field_context, 0);
Mike Lockwood8182e722010-12-30 15:38:45 -0500128 }
Mike Lockwood8182e722010-12-30 15:38:45 -0500129}
130
131static jobject
132android_mtp_MtpDevice_get_device_info(JNIEnv *env, jobject thiz)
133{
Mike Lockwood8182e722010-12-30 15:38:45 -0500134 MtpDevice* device = get_device_from_object(env, thiz);
135 if (!device) {
Steve Block5baa3a62011-12-20 16:23:08 +0000136 ALOGD("android_mtp_MtpDevice_get_device_info device is null");
Mike Lockwood8182e722010-12-30 15:38:45 -0500137 return NULL;
138 }
139 MtpDeviceInfo* deviceInfo = device->getDeviceInfo();
140 if (!deviceInfo) {
Steve Block5baa3a62011-12-20 16:23:08 +0000141 ALOGD("android_mtp_MtpDevice_get_device_info deviceInfo is null");
Mike Lockwood8182e722010-12-30 15:38:45 -0500142 return NULL;
143 }
144 jobject info = env->NewObject(clazz_deviceInfo, constructor_deviceInfo);
145 if (info == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000146 ALOGE("Could not create a MtpDeviceInfo object");
Mike Lockwood8182e722010-12-30 15:38:45 -0500147 delete deviceInfo;
148 return NULL;
149 }
150
151 if (deviceInfo->mManufacturer)
152 env->SetObjectField(info, field_deviceInfo_manufacturer,
153 env->NewStringUTF(deviceInfo->mManufacturer));
154 if (deviceInfo->mModel)
155 env->SetObjectField(info, field_deviceInfo_model,
156 env->NewStringUTF(deviceInfo->mModel));
157 if (deviceInfo->mVersion)
158 env->SetObjectField(info, field_deviceInfo_version,
159 env->NewStringUTF(deviceInfo->mVersion));
160 if (deviceInfo->mSerial)
161 env->SetObjectField(info, field_deviceInfo_serialNumber,
162 env->NewStringUTF(deviceInfo->mSerial));
163
164 delete deviceInfo;
165 return info;
Mike Lockwood8182e722010-12-30 15:38:45 -0500166}
167
168static jintArray
169android_mtp_MtpDevice_get_storage_ids(JNIEnv *env, jobject thiz)
170{
Mike Lockwood8182e722010-12-30 15:38:45 -0500171 MtpDevice* device = get_device_from_object(env, thiz);
172 if (!device)
173 return NULL;
174 MtpStorageIDList* storageIDs = device->getStorageIDs();
175 if (!storageIDs)
176 return NULL;
177
178 int length = storageIDs->size();
179 jintArray array = env->NewIntArray(length);
180 // FIXME is this cast safe?
181 env->SetIntArrayRegion(array, 0, length, (const jint *)storageIDs->array());
182
183 delete storageIDs;
184 return array;
Mike Lockwood8182e722010-12-30 15:38:45 -0500185}
186
187static jobject
188android_mtp_MtpDevice_get_storage_info(JNIEnv *env, jobject thiz, jint storageID)
189{
Mike Lockwood8182e722010-12-30 15:38:45 -0500190 MtpDevice* device = get_device_from_object(env, thiz);
191 if (!device)
192 return NULL;
193 MtpStorageInfo* storageInfo = device->getStorageInfo(storageID);
194 if (!storageInfo)
195 return NULL;
196
197 jobject info = env->NewObject(clazz_storageInfo, constructor_storageInfo);
198 if (info == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000199 ALOGE("Could not create a MtpStorageInfo object");
Mike Lockwood8182e722010-12-30 15:38:45 -0500200 delete storageInfo;
201 return NULL;
202 }
203
204 if (storageInfo->mStorageID)
205 env->SetIntField(info, field_storageInfo_storageId, storageInfo->mStorageID);
206 if (storageInfo->mMaxCapacity)
207 env->SetLongField(info, field_storageInfo_maxCapacity, storageInfo->mMaxCapacity);
208 if (storageInfo->mFreeSpaceBytes)
209 env->SetLongField(info, field_storageInfo_freeSpace, storageInfo->mFreeSpaceBytes);
210 if (storageInfo->mStorageDescription)
211 env->SetObjectField(info, field_storageInfo_description,
212 env->NewStringUTF(storageInfo->mStorageDescription));
213 if (storageInfo->mVolumeIdentifier)
214 env->SetObjectField(info, field_storageInfo_volumeIdentifier,
215 env->NewStringUTF(storageInfo->mVolumeIdentifier));
216
217 delete storageInfo;
218 return info;
Mike Lockwood8182e722010-12-30 15:38:45 -0500219}
220
221static jintArray
222android_mtp_MtpDevice_get_object_handles(JNIEnv *env, jobject thiz,
223 jint storageID, jint format, jint objectID)
224{
Mike Lockwood8182e722010-12-30 15:38:45 -0500225 MtpDevice* device = get_device_from_object(env, thiz);
226 if (!device)
227 return NULL;
228 MtpObjectHandleList* handles = device->getObjectHandles(storageID, format, objectID);
229 if (!handles)
230 return NULL;
231
232 int length = handles->size();
233 jintArray array = env->NewIntArray(length);
234 // FIXME is this cast safe?
235 env->SetIntArrayRegion(array, 0, length, (const jint *)handles->array());
236
237 delete handles;
238 return array;
Mike Lockwood8182e722010-12-30 15:38:45 -0500239}
240
241static jobject
242android_mtp_MtpDevice_get_object_info(JNIEnv *env, jobject thiz, jint objectID)
243{
Mike Lockwood8182e722010-12-30 15:38:45 -0500244 MtpDevice* device = get_device_from_object(env, thiz);
245 if (!device)
246 return NULL;
247 MtpObjectInfo* objectInfo = device->getObjectInfo(objectID);
248 if (!objectInfo)
249 return NULL;
250 jobject info = env->NewObject(clazz_objectInfo, constructor_objectInfo);
251 if (info == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000252 ALOGE("Could not create a MtpObjectInfo object");
Mike Lockwood8182e722010-12-30 15:38:45 -0500253 delete objectInfo;
254 return NULL;
255 }
256
257 if (objectInfo->mHandle)
258 env->SetIntField(info, field_objectInfo_handle, objectInfo->mHandle);
259 if (objectInfo->mStorageID)
260 env->SetIntField(info, field_objectInfo_storageId, objectInfo->mStorageID);
261 if (objectInfo->mFormat)
262 env->SetIntField(info, field_objectInfo_format, objectInfo->mFormat);
263 if (objectInfo->mProtectionStatus)
264 env->SetIntField(info, field_objectInfo_protectionStatus, objectInfo->mProtectionStatus);
265 if (objectInfo->mCompressedSize)
266 env->SetIntField(info, field_objectInfo_compressedSize, objectInfo->mCompressedSize);
267 if (objectInfo->mThumbFormat)
268 env->SetIntField(info, field_objectInfo_thumbFormat, objectInfo->mThumbFormat);
269 if (objectInfo->mThumbCompressedSize)
270 env->SetIntField(info, field_objectInfo_thumbCompressedSize, objectInfo->mThumbCompressedSize);
271 if (objectInfo->mThumbPixWidth)
272 env->SetIntField(info, field_objectInfo_thumbPixWidth, objectInfo->mThumbPixWidth);
273 if (objectInfo->mThumbPixHeight)
274 env->SetIntField(info, field_objectInfo_thumbPixHeight, objectInfo->mThumbPixHeight);
275 if (objectInfo->mImagePixWidth)
276 env->SetIntField(info, field_objectInfo_imagePixWidth, objectInfo->mImagePixWidth);
277 if (objectInfo->mImagePixHeight)
278 env->SetIntField(info, field_objectInfo_imagePixHeight, objectInfo->mImagePixHeight);
279 if (objectInfo->mImagePixDepth)
280 env->SetIntField(info, field_objectInfo_imagePixDepth, objectInfo->mImagePixDepth);
281 if (objectInfo->mParent)
282 env->SetIntField(info, field_objectInfo_parent, objectInfo->mParent);
283 if (objectInfo->mAssociationType)
284 env->SetIntField(info, field_objectInfo_associationType, objectInfo->mAssociationType);
285 if (objectInfo->mAssociationDesc)
286 env->SetIntField(info, field_objectInfo_associationDesc, objectInfo->mAssociationDesc);
287 if (objectInfo->mSequenceNumber)
288 env->SetIntField(info, field_objectInfo_sequenceNumber, objectInfo->mSequenceNumber);
289 if (objectInfo->mName)
290 env->SetObjectField(info, field_objectInfo_name, env->NewStringUTF(objectInfo->mName));
291 if (objectInfo->mDateCreated)
Mike Lockwoodb966b9d2011-03-09 17:28:33 -0500292 env->SetLongField(info, field_objectInfo_dateCreated, objectInfo->mDateCreated * 1000LL);
Mike Lockwood8182e722010-12-30 15:38:45 -0500293 if (objectInfo->mDateModified)
Mike Lockwoodb966b9d2011-03-09 17:28:33 -0500294 env->SetLongField(info, field_objectInfo_dateModified, objectInfo->mDateModified * 1000LL);
Mike Lockwood8182e722010-12-30 15:38:45 -0500295 if (objectInfo->mKeywords)
296 env->SetObjectField(info, field_objectInfo_keywords,
297 env->NewStringUTF(objectInfo->mKeywords));
298
299 delete objectInfo;
300 return info;
Mike Lockwood8182e722010-12-30 15:38:45 -0500301}
302
303struct get_object_callback_data {
304 JNIEnv *env;
305 jbyteArray array;
306};
307
308static bool get_object_callback(void* data, int offset, int length, void* clientData)
309{
310 get_object_callback_data* cbData = (get_object_callback_data *)clientData;
311 cbData->env->SetByteArrayRegion(cbData->array, offset, length, (jbyte *)data);
312 return true;
313}
314
315static jbyteArray
316android_mtp_MtpDevice_get_object(JNIEnv *env, jobject thiz, jint objectID, jint objectSize)
317{
Mike Lockwood8182e722010-12-30 15:38:45 -0500318 MtpDevice* device = get_device_from_object(env, thiz);
319 if (!device)
320 return NULL;
321
322 jbyteArray array = env->NewByteArray(objectSize);
323 if (!array) {
324 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
325 return NULL;
326 }
327
328 get_object_callback_data data;
329 data.env = env;
330 data.array = array;
331
332 if (device->readObject(objectID, get_object_callback, objectSize, &data))
333 return array;
Mike Lockwood8182e722010-12-30 15:38:45 -0500334 return NULL;
335}
336
337static jbyteArray
338android_mtp_MtpDevice_get_thumbnail(JNIEnv *env, jobject thiz, jint objectID)
339{
Mike Lockwood8182e722010-12-30 15:38:45 -0500340 MtpDevice* device = get_device_from_object(env, thiz);
341 if (!device)
342 return NULL;
343
344 int length;
345 void* thumbnail = device->getThumbnail(objectID, length);
346 if (! thumbnail)
347 return NULL;
348 jbyteArray array = env->NewByteArray(length);
349 env->SetByteArrayRegion(array, 0, length, (const jbyte *)thumbnail);
350
351 free(thumbnail);
352 return array;
Mike Lockwood8182e722010-12-30 15:38:45 -0500353}
354
355static jboolean
356android_mtp_MtpDevice_delete_object(JNIEnv *env, jobject thiz, jint object_id)
357{
Mike Lockwood8182e722010-12-30 15:38:45 -0500358 MtpDevice* device = get_device_from_object(env, thiz);
Ashok Bhate2e59322013-12-17 19:04:19 +0000359 if (device && device->deleteObject(object_id)) {
360 return JNI_TRUE;
361 } else {
362 return JNI_FALSE;
363 }
Mike Lockwood8182e722010-12-30 15:38:45 -0500364}
365
366static jlong
367android_mtp_MtpDevice_get_parent(JNIEnv *env, jobject thiz, jint object_id)
368{
Mike Lockwood8182e722010-12-30 15:38:45 -0500369 MtpDevice* device = get_device_from_object(env, thiz);
370 if (device)
Ashok Bhate2e59322013-12-17 19:04:19 +0000371 return (jlong)device->getParent(object_id);
Mike Lockwood8182e722010-12-30 15:38:45 -0500372 else
Mike Lockwood8182e722010-12-30 15:38:45 -0500373 return -1;
374}
375
376static jlong
377android_mtp_MtpDevice_get_storage_id(JNIEnv *env, jobject thiz, jint object_id)
378{
Mike Lockwood8182e722010-12-30 15:38:45 -0500379 MtpDevice* device = get_device_from_object(env, thiz);
380 if (device)
Ashok Bhate2e59322013-12-17 19:04:19 +0000381 return (jlong)device->getStorageID(object_id);
Mike Lockwood8182e722010-12-30 15:38:45 -0500382 else
Mike Lockwood8182e722010-12-30 15:38:45 -0500383 return -1;
384}
385
386static jboolean
387android_mtp_MtpDevice_import_file(JNIEnv *env, jobject thiz, jint object_id, jstring dest_path)
388{
Mike Lockwood8182e722010-12-30 15:38:45 -0500389 MtpDevice* device = get_device_from_object(env, thiz);
390 if (device) {
391 const char *destPathStr = env->GetStringUTFChars(dest_path, NULL);
James Dong39774722011-04-06 11:57:48 -0700392 if (destPathStr == NULL) {
Ashok Bhate2e59322013-12-17 19:04:19 +0000393 return JNI_FALSE;
James Dong39774722011-04-06 11:57:48 -0700394 }
395
Ashok Bhate2e59322013-12-17 19:04:19 +0000396 jboolean result = device->readObject(object_id, destPathStr, AID_SDCARD_RW, 0664);
Mike Lockwood8182e722010-12-30 15:38:45 -0500397 env->ReleaseStringUTFChars(dest_path, destPathStr);
398 return result;
399 }
Mike Lockwoodc1b9bbb2011-07-13 11:06:57 -0400400
Ashok Bhate2e59322013-12-17 19:04:19 +0000401 return JNI_FALSE;
Mike Lockwood8182e722010-12-30 15:38:45 -0500402}
403
404// ----------------------------------------------------------------------------
405
406static JNINativeMethod gMethods[] = {
407 {"native_open", "(Ljava/lang/String;I)Z",
408 (void *)android_mtp_MtpDevice_open},
409 {"native_close", "()V", (void *)android_mtp_MtpDevice_close},
410 {"native_get_device_info", "()Landroid/mtp/MtpDeviceInfo;",
411 (void *)android_mtp_MtpDevice_get_device_info},
412 {"native_get_storage_ids", "()[I", (void *)android_mtp_MtpDevice_get_storage_ids},
413 {"native_get_storage_info", "(I)Landroid/mtp/MtpStorageInfo;",
414 (void *)android_mtp_MtpDevice_get_storage_info},
415 {"native_get_object_handles","(III)[I",
416 (void *)android_mtp_MtpDevice_get_object_handles},
417 {"native_get_object_info", "(I)Landroid/mtp/MtpObjectInfo;",
418 (void *)android_mtp_MtpDevice_get_object_info},
419 {"native_get_object", "(II)[B",(void *)android_mtp_MtpDevice_get_object},
420 {"native_get_thumbnail", "(I)[B",(void *)android_mtp_MtpDevice_get_thumbnail},
421 {"native_delete_object", "(I)Z", (void *)android_mtp_MtpDevice_delete_object},
422 {"native_get_parent", "(I)J", (void *)android_mtp_MtpDevice_get_parent},
423 {"native_get_storage_id", "(I)J", (void *)android_mtp_MtpDevice_get_storage_id},
424 {"native_import_file", "(ILjava/lang/String;)Z",
425 (void *)android_mtp_MtpDevice_import_file},
426};
427
428static const char* const kClassPathName = "android/mtp/MtpDevice";
429
430int register_android_mtp_MtpDevice(JNIEnv *env)
431{
432 jclass clazz;
433
Steve Block5baa3a62011-12-20 16:23:08 +0000434 ALOGD("register_android_mtp_MtpDevice\n");
Mike Lockwood8182e722010-12-30 15:38:45 -0500435
436 clazz = env->FindClass("android/mtp/MtpDeviceInfo");
437 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000438 ALOGE("Can't find android/mtp/MtpDeviceInfo");
Mike Lockwood8182e722010-12-30 15:38:45 -0500439 return -1;
440 }
441 constructor_deviceInfo = env->GetMethodID(clazz, "<init>", "()V");
442 if (constructor_deviceInfo == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000443 ALOGE("Can't find android/mtp/MtpDeviceInfo constructor");
Mike Lockwood8182e722010-12-30 15:38:45 -0500444 return -1;
445 }
446 field_deviceInfo_manufacturer = env->GetFieldID(clazz, "mManufacturer", "Ljava/lang/String;");
447 if (field_deviceInfo_manufacturer == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000448 ALOGE("Can't find MtpDeviceInfo.mManufacturer");
Mike Lockwood8182e722010-12-30 15:38:45 -0500449 return -1;
450 }
451 field_deviceInfo_model = env->GetFieldID(clazz, "mModel", "Ljava/lang/String;");
452 if (field_deviceInfo_model == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000453 ALOGE("Can't find MtpDeviceInfo.mModel");
Mike Lockwood8182e722010-12-30 15:38:45 -0500454 return -1;
455 }
456 field_deviceInfo_version = env->GetFieldID(clazz, "mVersion", "Ljava/lang/String;");
457 if (field_deviceInfo_version == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000458 ALOGE("Can't find MtpDeviceInfo.mVersion");
Mike Lockwood8182e722010-12-30 15:38:45 -0500459 return -1;
460 }
461 field_deviceInfo_serialNumber = env->GetFieldID(clazz, "mSerialNumber", "Ljava/lang/String;");
462 if (field_deviceInfo_serialNumber == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000463 ALOGE("Can't find MtpDeviceInfo.mSerialNumber");
Mike Lockwood8182e722010-12-30 15:38:45 -0500464 return -1;
465 }
Mike Lockwood40304e22011-02-11 08:19:11 -0500466 clazz_deviceInfo = (jclass)env->NewGlobalRef(clazz);
Mike Lockwood8182e722010-12-30 15:38:45 -0500467
468 clazz = env->FindClass("android/mtp/MtpStorageInfo");
469 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000470 ALOGE("Can't find android/mtp/MtpStorageInfo");
Mike Lockwood8182e722010-12-30 15:38:45 -0500471 return -1;
472 }
473 constructor_storageInfo = env->GetMethodID(clazz, "<init>", "()V");
474 if (constructor_storageInfo == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000475 ALOGE("Can't find android/mtp/MtpStorageInfo constructor");
Mike Lockwood8182e722010-12-30 15:38:45 -0500476 return -1;
477 }
478 field_storageInfo_storageId = env->GetFieldID(clazz, "mStorageId", "I");
479 if (field_storageInfo_storageId == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000480 ALOGE("Can't find MtpStorageInfo.mStorageId");
Mike Lockwood8182e722010-12-30 15:38:45 -0500481 return -1;
482 }
483 field_storageInfo_maxCapacity = env->GetFieldID(clazz, "mMaxCapacity", "J");
484 if (field_storageInfo_maxCapacity == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000485 ALOGE("Can't find MtpStorageInfo.mMaxCapacity");
Mike Lockwood8182e722010-12-30 15:38:45 -0500486 return -1;
487 }
488 field_storageInfo_freeSpace = env->GetFieldID(clazz, "mFreeSpace", "J");
489 if (field_storageInfo_freeSpace == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000490 ALOGE("Can't find MtpStorageInfo.mFreeSpace");
Mike Lockwood8182e722010-12-30 15:38:45 -0500491 return -1;
492 }
493 field_storageInfo_description = env->GetFieldID(clazz, "mDescription", "Ljava/lang/String;");
494 if (field_storageInfo_description == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000495 ALOGE("Can't find MtpStorageInfo.mDescription");
Mike Lockwood8182e722010-12-30 15:38:45 -0500496 return -1;
497 }
498 field_storageInfo_volumeIdentifier = env->GetFieldID(clazz, "mVolumeIdentifier", "Ljava/lang/String;");
499 if (field_storageInfo_volumeIdentifier == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000500 ALOGE("Can't find MtpStorageInfo.mVolumeIdentifier");
Mike Lockwood8182e722010-12-30 15:38:45 -0500501 return -1;
502 }
Mike Lockwood40304e22011-02-11 08:19:11 -0500503 clazz_storageInfo = (jclass)env->NewGlobalRef(clazz);
Mike Lockwood8182e722010-12-30 15:38:45 -0500504
505 clazz = env->FindClass("android/mtp/MtpObjectInfo");
506 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000507 ALOGE("Can't find android/mtp/MtpObjectInfo");
Mike Lockwood8182e722010-12-30 15:38:45 -0500508 return -1;
509 }
510 constructor_objectInfo = env->GetMethodID(clazz, "<init>", "()V");
511 if (constructor_objectInfo == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000512 ALOGE("Can't find android/mtp/MtpObjectInfo constructor");
Mike Lockwood8182e722010-12-30 15:38:45 -0500513 return -1;
514 }
515 field_objectInfo_handle = env->GetFieldID(clazz, "mHandle", "I");
516 if (field_objectInfo_handle == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000517 ALOGE("Can't find MtpObjectInfo.mHandle");
Mike Lockwood8182e722010-12-30 15:38:45 -0500518 return -1;
519 }
520 field_objectInfo_storageId = env->GetFieldID(clazz, "mStorageId", "I");
521 if (field_objectInfo_storageId == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000522 ALOGE("Can't find MtpObjectInfo.mStorageId");
Mike Lockwood8182e722010-12-30 15:38:45 -0500523 return -1;
524 }
525 field_objectInfo_format = env->GetFieldID(clazz, "mFormat", "I");
526 if (field_objectInfo_format == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000527 ALOGE("Can't find MtpObjectInfo.mFormat");
Mike Lockwood8182e722010-12-30 15:38:45 -0500528 return -1;
529 }
530 field_objectInfo_protectionStatus = env->GetFieldID(clazz, "mProtectionStatus", "I");
531 if (field_objectInfo_protectionStatus == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000532 ALOGE("Can't find MtpObjectInfo.mProtectionStatus");
Mike Lockwood8182e722010-12-30 15:38:45 -0500533 return -1;
534 }
535 field_objectInfo_compressedSize = env->GetFieldID(clazz, "mCompressedSize", "I");
536 if (field_objectInfo_compressedSize == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000537 ALOGE("Can't find MtpObjectInfo.mCompressedSize");
Mike Lockwood8182e722010-12-30 15:38:45 -0500538 return -1;
539 }
540 field_objectInfo_thumbFormat = env->GetFieldID(clazz, "mThumbFormat", "I");
541 if (field_objectInfo_thumbFormat == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000542 ALOGE("Can't find MtpObjectInfo.mThumbFormat");
Mike Lockwood8182e722010-12-30 15:38:45 -0500543 return -1;
544 }
545 field_objectInfo_thumbCompressedSize = env->GetFieldID(clazz, "mThumbCompressedSize", "I");
546 if (field_objectInfo_thumbCompressedSize == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000547 ALOGE("Can't find MtpObjectInfo.mThumbCompressedSize");
Mike Lockwood8182e722010-12-30 15:38:45 -0500548 return -1;
549 }
550 field_objectInfo_thumbPixWidth = env->GetFieldID(clazz, "mThumbPixWidth", "I");
551 if (field_objectInfo_thumbPixWidth == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000552 ALOGE("Can't find MtpObjectInfo.mThumbPixWidth");
Mike Lockwood8182e722010-12-30 15:38:45 -0500553 return -1;
554 }
555 field_objectInfo_thumbPixHeight = env->GetFieldID(clazz, "mThumbPixHeight", "I");
556 if (field_objectInfo_thumbPixHeight == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000557 ALOGE("Can't find MtpObjectInfo.mThumbPixHeight");
Mike Lockwood8182e722010-12-30 15:38:45 -0500558 return -1;
559 }
560 field_objectInfo_imagePixWidth = env->GetFieldID(clazz, "mImagePixWidth", "I");
561 if (field_objectInfo_imagePixWidth == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000562 ALOGE("Can't find MtpObjectInfo.mImagePixWidth");
Mike Lockwood8182e722010-12-30 15:38:45 -0500563 return -1;
564 }
565 field_objectInfo_imagePixHeight = env->GetFieldID(clazz, "mImagePixHeight", "I");
566 if (field_objectInfo_imagePixHeight == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000567 ALOGE("Can't find MtpObjectInfo.mImagePixHeight");
Mike Lockwood8182e722010-12-30 15:38:45 -0500568 return -1;
569 }
570 field_objectInfo_imagePixDepth = env->GetFieldID(clazz, "mImagePixDepth", "I");
571 if (field_objectInfo_imagePixDepth == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000572 ALOGE("Can't find MtpObjectInfo.mImagePixDepth");
Mike Lockwood8182e722010-12-30 15:38:45 -0500573 return -1;
574 }
575 field_objectInfo_parent = env->GetFieldID(clazz, "mParent", "I");
576 if (field_objectInfo_parent == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000577 ALOGE("Can't find MtpObjectInfo.mParent");
Mike Lockwood8182e722010-12-30 15:38:45 -0500578 return -1;
579 }
580 field_objectInfo_associationType = env->GetFieldID(clazz, "mAssociationType", "I");
581 if (field_objectInfo_associationType == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000582 ALOGE("Can't find MtpObjectInfo.mAssociationType");
Mike Lockwood8182e722010-12-30 15:38:45 -0500583 return -1;
584 }
585 field_objectInfo_associationDesc = env->GetFieldID(clazz, "mAssociationDesc", "I");
586 if (field_objectInfo_associationDesc == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000587 ALOGE("Can't find MtpObjectInfo.mAssociationDesc");
Mike Lockwood8182e722010-12-30 15:38:45 -0500588 return -1;
589 }
590 field_objectInfo_sequenceNumber = env->GetFieldID(clazz, "mSequenceNumber", "I");
591 if (field_objectInfo_sequenceNumber == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000592 ALOGE("Can't find MtpObjectInfo.mSequenceNumber");
Mike Lockwood8182e722010-12-30 15:38:45 -0500593 return -1;
594 }
595 field_objectInfo_name = env->GetFieldID(clazz, "mName", "Ljava/lang/String;");
596 if (field_objectInfo_name == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000597 ALOGE("Can't find MtpObjectInfo.mName");
Mike Lockwood8182e722010-12-30 15:38:45 -0500598 return -1;
599 }
600 field_objectInfo_dateCreated = env->GetFieldID(clazz, "mDateCreated", "J");
601 if (field_objectInfo_dateCreated == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000602 ALOGE("Can't find MtpObjectInfo.mDateCreated");
Mike Lockwood8182e722010-12-30 15:38:45 -0500603 return -1;
604 }
605 field_objectInfo_dateModified = env->GetFieldID(clazz, "mDateModified", "J");
606 if (field_objectInfo_dateModified == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000607 ALOGE("Can't find MtpObjectInfo.mDateModified");
Mike Lockwood8182e722010-12-30 15:38:45 -0500608 return -1;
609 }
610 field_objectInfo_keywords = env->GetFieldID(clazz, "mKeywords", "Ljava/lang/String;");
611 if (field_objectInfo_keywords == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000612 ALOGE("Can't find MtpObjectInfo.mKeywords");
Mike Lockwood8182e722010-12-30 15:38:45 -0500613 return -1;
614 }
Mike Lockwood40304e22011-02-11 08:19:11 -0500615 clazz_objectInfo = (jclass)env->NewGlobalRef(clazz);
Mike Lockwood8182e722010-12-30 15:38:45 -0500616
617 clazz = env->FindClass("android/mtp/MtpDevice");
618 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000619 ALOGE("Can't find android/mtp/MtpDevice");
Mike Lockwood8182e722010-12-30 15:38:45 -0500620 return -1;
621 }
Ashok Bhate2e59322013-12-17 19:04:19 +0000622 field_context = env->GetFieldID(clazz, "mNativeContext", "J");
Mike Lockwood8182e722010-12-30 15:38:45 -0500623 if (field_context == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000624 ALOGE("Can't find MtpDevice.mNativeContext");
Mike Lockwood8182e722010-12-30 15:38:45 -0500625 return -1;
626 }
627
628 return AndroidRuntime::registerNativeMethods(env,
629 "android/mtp/MtpDevice", gMethods, NELEM(gMethods));
630}