Home | History | Annotate | Download | only in jni
      1 /*
      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"
     31 #include "android_runtime/Log.h"
     32 #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 
     40 using namespace android;
     41 
     42 // ----------------------------------------------------------------------------
     43 
     44 static jfieldID field_context;
     45 
     46 jclass clazz_deviceInfo;
     47 jclass clazz_storageInfo;
     48 jclass clazz_objectInfo;
     49 
     50 jmethodID constructor_deviceInfo;
     51 jmethodID constructor_storageInfo;
     52 jmethodID constructor_objectInfo;
     53 
     54 // MtpDeviceInfo fields
     55 static jfieldID field_deviceInfo_manufacturer;
     56 static jfieldID field_deviceInfo_model;
     57 static jfieldID field_deviceInfo_version;
     58 static jfieldID field_deviceInfo_serialNumber;
     59 
     60 // MtpStorageInfo fields
     61 static jfieldID field_storageInfo_storageId;
     62 static jfieldID field_storageInfo_maxCapacity;
     63 static jfieldID field_storageInfo_freeSpace;
     64 static jfieldID field_storageInfo_description;
     65 static jfieldID field_storageInfo_volumeIdentifier;
     66 
     67 // MtpObjectInfo fields
     68 static jfieldID field_objectInfo_handle;
     69 static jfieldID field_objectInfo_storageId;
     70 static jfieldID field_objectInfo_format;
     71 static jfieldID field_objectInfo_protectionStatus;
     72 static jfieldID field_objectInfo_compressedSize;
     73 static jfieldID field_objectInfo_thumbFormat;
     74 static jfieldID field_objectInfo_thumbCompressedSize;
     75 static jfieldID field_objectInfo_thumbPixWidth;
     76 static jfieldID field_objectInfo_thumbPixHeight;
     77 static jfieldID field_objectInfo_imagePixWidth;
     78 static jfieldID field_objectInfo_imagePixHeight;
     79 static jfieldID field_objectInfo_imagePixDepth;
     80 static jfieldID field_objectInfo_parent;
     81 static jfieldID field_objectInfo_associationType;
     82 static jfieldID field_objectInfo_associationDesc;
     83 static jfieldID field_objectInfo_sequenceNumber;
     84 static jfieldID field_objectInfo_name;
     85 static jfieldID field_objectInfo_dateCreated;
     86 static jfieldID field_objectInfo_dateModified;
     87 static jfieldID field_objectInfo_keywords;
     88 
     89 MtpDevice* get_device_from_object(JNIEnv* env, jobject javaDevice)
     90 {
     91     return (MtpDevice*)env->GetLongField(javaDevice, field_context);
     92 }
     93 
     94 static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
     95     if (env->ExceptionCheck()) {
     96         ALOGE("An exception was thrown by callback '%s'.", methodName);
     97         LOGE_EX(env);
     98         env->ExceptionClear();
     99     }
    100 }
    101 
    102 // ----------------------------------------------------------------------------
    103 
    104 static jboolean
    105 android_mtp_MtpDevice_open(JNIEnv *env, jobject thiz, jstring deviceName, jint fd)
    106 {
    107     const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL);
    108     if (deviceNameStr == NULL) {
    109         return JNI_FALSE;
    110     }
    111 
    112     MtpDevice* device = MtpDevice::open(deviceNameStr, fd);
    113     env->ReleaseStringUTFChars(deviceName, deviceNameStr);
    114 
    115     if (device)
    116         env->SetLongField(thiz, field_context,  (jlong)device);
    117     return (jboolean)(device != NULL);
    118 }
    119 
    120 static void
    121 android_mtp_MtpDevice_close(JNIEnv *env, jobject thiz)
    122 {
    123     MtpDevice* device = get_device_from_object(env, thiz);
    124     if (device) {
    125         device->close();
    126         delete device;
    127         env->SetLongField(thiz, field_context, 0);
    128     }
    129 }
    130 
    131 static jobject
    132 android_mtp_MtpDevice_get_device_info(JNIEnv *env, jobject thiz)
    133 {
    134     MtpDevice* device = get_device_from_object(env, thiz);
    135     if (!device) {
    136         ALOGD("android_mtp_MtpDevice_get_device_info device is null");
    137         return NULL;
    138     }
    139     MtpDeviceInfo* deviceInfo = device->getDeviceInfo();
    140     if (!deviceInfo) {
    141         ALOGD("android_mtp_MtpDevice_get_device_info deviceInfo is null");
    142         return NULL;
    143     }
    144     jobject info = env->NewObject(clazz_deviceInfo, constructor_deviceInfo);
    145     if (info == NULL) {
    146         ALOGE("Could not create a MtpDeviceInfo object");
    147         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;
    166 }
    167 
    168 static jintArray
    169 android_mtp_MtpDevice_get_storage_ids(JNIEnv *env, jobject thiz)
    170 {
    171     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;
    185 }
    186 
    187 static jobject
    188 android_mtp_MtpDevice_get_storage_info(JNIEnv *env, jobject thiz, jint storageID)
    189 {
    190     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) {
    199         ALOGE("Could not create a MtpStorageInfo object");
    200         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;
    219 }
    220 
    221 static jintArray
    222 android_mtp_MtpDevice_get_object_handles(JNIEnv *env, jobject thiz,
    223         jint storageID, jint format, jint objectID)
    224 {
    225     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;
    239 }
    240 
    241 static jobject
    242 android_mtp_MtpDevice_get_object_info(JNIEnv *env, jobject thiz, jint objectID)
    243 {
    244     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) {
    252         ALOGE("Could not create a MtpObjectInfo object");
    253         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)
    292         env->SetLongField(info, field_objectInfo_dateCreated, objectInfo->mDateCreated * 1000LL);
    293     if (objectInfo->mDateModified)
    294         env->SetLongField(info, field_objectInfo_dateModified, objectInfo->mDateModified * 1000LL);
    295     if (objectInfo->mKeywords)
    296         env->SetObjectField(info, field_objectInfo_keywords,
    297             env->NewStringUTF(objectInfo->mKeywords));
    298 
    299     delete objectInfo;
    300     return info;
    301 }
    302 
    303 struct get_object_callback_data {
    304     JNIEnv *env;
    305     jbyteArray array;
    306 };
    307 
    308 static 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 
    315 static jbyteArray
    316 android_mtp_MtpDevice_get_object(JNIEnv *env, jobject thiz, jint objectID, jint objectSize)
    317 {
    318     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;
    334     return NULL;
    335 }
    336 
    337 static jbyteArray
    338 android_mtp_MtpDevice_get_thumbnail(JNIEnv *env, jobject thiz, jint objectID)
    339 {
    340     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;
    353 }
    354 
    355 static jboolean
    356 android_mtp_MtpDevice_delete_object(JNIEnv *env, jobject thiz, jint object_id)
    357 {
    358     MtpDevice* device = get_device_from_object(env, thiz);
    359     if (device && device->deleteObject(object_id)) {
    360         return JNI_TRUE;
    361     } else {
    362         return JNI_FALSE;
    363     }
    364 }
    365 
    366 static jlong
    367 android_mtp_MtpDevice_get_parent(JNIEnv *env, jobject thiz, jint object_id)
    368 {
    369     MtpDevice* device = get_device_from_object(env, thiz);
    370     if (device)
    371         return (jlong)device->getParent(object_id);
    372     else
    373         return -1;
    374 }
    375 
    376 static jlong
    377 android_mtp_MtpDevice_get_storage_id(JNIEnv *env, jobject thiz, jint object_id)
    378 {
    379     MtpDevice* device = get_device_from_object(env, thiz);
    380     if (device)
    381         return (jlong)device->getStorageID(object_id);
    382     else
    383         return -1;
    384 }
    385 
    386 static jboolean
    387 android_mtp_MtpDevice_import_file(JNIEnv *env, jobject thiz, jint object_id, jstring dest_path)
    388 {
    389     MtpDevice* device = get_device_from_object(env, thiz);
    390     if (device) {
    391         const char *destPathStr = env->GetStringUTFChars(dest_path, NULL);
    392         if (destPathStr == NULL) {
    393             return JNI_FALSE;
    394         }
    395 
    396         jboolean result = device->readObject(object_id, destPathStr, AID_SDCARD_RW, 0664);
    397         env->ReleaseStringUTFChars(dest_path, destPathStr);
    398         return result;
    399     }
    400 
    401     return JNI_FALSE;
    402 }
    403 
    404 // ----------------------------------------------------------------------------
    405 
    406 static 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 
    428 static const char* const kClassPathName = "android/mtp/MtpDevice";
    429 
    430 int register_android_mtp_MtpDevice(JNIEnv *env)
    431 {
    432     jclass clazz;
    433 
    434     ALOGD("register_android_mtp_MtpDevice\n");
    435 
    436     clazz = env->FindClass("android/mtp/MtpDeviceInfo");
    437     if (clazz == NULL) {
    438         ALOGE("Can't find android/mtp/MtpDeviceInfo");
    439         return -1;
    440     }
    441     constructor_deviceInfo = env->GetMethodID(clazz, "<init>", "()V");
    442     if (constructor_deviceInfo == NULL) {
    443         ALOGE("Can't find android/mtp/MtpDeviceInfo constructor");
    444         return -1;
    445     }
    446     field_deviceInfo_manufacturer = env->GetFieldID(clazz, "mManufacturer", "Ljava/lang/String;");
    447     if (field_deviceInfo_manufacturer == NULL) {
    448         ALOGE("Can't find MtpDeviceInfo.mManufacturer");
    449         return -1;
    450     }
    451     field_deviceInfo_model = env->GetFieldID(clazz, "mModel", "Ljava/lang/String;");
    452     if (field_deviceInfo_model == NULL) {
    453         ALOGE("Can't find MtpDeviceInfo.mModel");
    454         return -1;
    455     }
    456     field_deviceInfo_version = env->GetFieldID(clazz, "mVersion", "Ljava/lang/String;");
    457     if (field_deviceInfo_version == NULL) {
    458         ALOGE("Can't find MtpDeviceInfo.mVersion");
    459         return -1;
    460     }
    461     field_deviceInfo_serialNumber = env->GetFieldID(clazz, "mSerialNumber", "Ljava/lang/String;");
    462     if (field_deviceInfo_serialNumber == NULL) {
    463         ALOGE("Can't find MtpDeviceInfo.mSerialNumber");
    464         return -1;
    465     }
    466     clazz_deviceInfo = (jclass)env->NewGlobalRef(clazz);
    467 
    468     clazz = env->FindClass("android/mtp/MtpStorageInfo");
    469     if (clazz == NULL) {
    470         ALOGE("Can't find android/mtp/MtpStorageInfo");
    471         return -1;
    472     }
    473     constructor_storageInfo = env->GetMethodID(clazz, "<init>", "()V");
    474     if (constructor_storageInfo == NULL) {
    475         ALOGE("Can't find android/mtp/MtpStorageInfo constructor");
    476         return -1;
    477     }
    478     field_storageInfo_storageId = env->GetFieldID(clazz, "mStorageId", "I");
    479     if (field_storageInfo_storageId == NULL) {
    480         ALOGE("Can't find MtpStorageInfo.mStorageId");
    481         return -1;
    482     }
    483     field_storageInfo_maxCapacity = env->GetFieldID(clazz, "mMaxCapacity", "J");
    484     if (field_storageInfo_maxCapacity == NULL) {
    485         ALOGE("Can't find MtpStorageInfo.mMaxCapacity");
    486         return -1;
    487     }
    488     field_storageInfo_freeSpace = env->GetFieldID(clazz, "mFreeSpace", "J");
    489     if (field_storageInfo_freeSpace == NULL) {
    490         ALOGE("Can't find MtpStorageInfo.mFreeSpace");
    491         return -1;
    492     }
    493     field_storageInfo_description = env->GetFieldID(clazz, "mDescription", "Ljava/lang/String;");
    494     if (field_storageInfo_description == NULL) {
    495         ALOGE("Can't find MtpStorageInfo.mDescription");
    496         return -1;
    497     }
    498     field_storageInfo_volumeIdentifier = env->GetFieldID(clazz, "mVolumeIdentifier", "Ljava/lang/String;");
    499     if (field_storageInfo_volumeIdentifier == NULL) {
    500         ALOGE("Can't find MtpStorageInfo.mVolumeIdentifier");
    501         return -1;
    502     }
    503     clazz_storageInfo = (jclass)env->NewGlobalRef(clazz);
    504 
    505     clazz = env->FindClass("android/mtp/MtpObjectInfo");
    506     if (clazz == NULL) {
    507         ALOGE("Can't find android/mtp/MtpObjectInfo");
    508         return -1;
    509     }
    510     constructor_objectInfo = env->GetMethodID(clazz, "<init>", "()V");
    511     if (constructor_objectInfo == NULL) {
    512         ALOGE("Can't find android/mtp/MtpObjectInfo constructor");
    513         return -1;
    514     }
    515     field_objectInfo_handle = env->GetFieldID(clazz, "mHandle", "I");
    516     if (field_objectInfo_handle == NULL) {
    517         ALOGE("Can't find MtpObjectInfo.mHandle");
    518         return -1;
    519     }
    520     field_objectInfo_storageId = env->GetFieldID(clazz, "mStorageId", "I");
    521     if (field_objectInfo_storageId == NULL) {
    522         ALOGE("Can't find MtpObjectInfo.mStorageId");
    523         return -1;
    524     }
    525     field_objectInfo_format = env->GetFieldID(clazz, "mFormat", "I");
    526     if (field_objectInfo_format == NULL) {
    527         ALOGE("Can't find MtpObjectInfo.mFormat");
    528         return -1;
    529     }
    530     field_objectInfo_protectionStatus = env->GetFieldID(clazz, "mProtectionStatus", "I");
    531     if (field_objectInfo_protectionStatus == NULL) {
    532         ALOGE("Can't find MtpObjectInfo.mProtectionStatus");
    533         return -1;
    534     }
    535     field_objectInfo_compressedSize = env->GetFieldID(clazz, "mCompressedSize", "I");
    536     if (field_objectInfo_compressedSize == NULL) {
    537         ALOGE("Can't find MtpObjectInfo.mCompressedSize");
    538         return -1;
    539     }
    540     field_objectInfo_thumbFormat = env->GetFieldID(clazz, "mThumbFormat", "I");
    541     if (field_objectInfo_thumbFormat == NULL) {
    542         ALOGE("Can't find MtpObjectInfo.mThumbFormat");
    543         return -1;
    544     }
    545     field_objectInfo_thumbCompressedSize = env->GetFieldID(clazz, "mThumbCompressedSize", "I");
    546     if (field_objectInfo_thumbCompressedSize == NULL) {
    547         ALOGE("Can't find MtpObjectInfo.mThumbCompressedSize");
    548         return -1;
    549     }
    550     field_objectInfo_thumbPixWidth = env->GetFieldID(clazz, "mThumbPixWidth", "I");
    551     if (field_objectInfo_thumbPixWidth == NULL) {
    552         ALOGE("Can't find MtpObjectInfo.mThumbPixWidth");
    553         return -1;
    554     }
    555     field_objectInfo_thumbPixHeight = env->GetFieldID(clazz, "mThumbPixHeight", "I");
    556     if (field_objectInfo_thumbPixHeight == NULL) {
    557         ALOGE("Can't find MtpObjectInfo.mThumbPixHeight");
    558         return -1;
    559     }
    560     field_objectInfo_imagePixWidth = env->GetFieldID(clazz, "mImagePixWidth", "I");
    561     if (field_objectInfo_imagePixWidth == NULL) {
    562         ALOGE("Can't find MtpObjectInfo.mImagePixWidth");
    563         return -1;
    564     }
    565     field_objectInfo_imagePixHeight = env->GetFieldID(clazz, "mImagePixHeight", "I");
    566     if (field_objectInfo_imagePixHeight == NULL) {
    567         ALOGE("Can't find MtpObjectInfo.mImagePixHeight");
    568         return -1;
    569     }
    570     field_objectInfo_imagePixDepth = env->GetFieldID(clazz, "mImagePixDepth", "I");
    571     if (field_objectInfo_imagePixDepth == NULL) {
    572         ALOGE("Can't find MtpObjectInfo.mImagePixDepth");
    573         return -1;
    574     }
    575     field_objectInfo_parent = env->GetFieldID(clazz, "mParent", "I");
    576     if (field_objectInfo_parent == NULL) {
    577         ALOGE("Can't find MtpObjectInfo.mParent");
    578         return -1;
    579     }
    580     field_objectInfo_associationType = env->GetFieldID(clazz, "mAssociationType", "I");
    581     if (field_objectInfo_associationType == NULL) {
    582         ALOGE("Can't find MtpObjectInfo.mAssociationType");
    583         return -1;
    584     }
    585     field_objectInfo_associationDesc = env->GetFieldID(clazz, "mAssociationDesc", "I");
    586     if (field_objectInfo_associationDesc == NULL) {
    587         ALOGE("Can't find MtpObjectInfo.mAssociationDesc");
    588         return -1;
    589     }
    590     field_objectInfo_sequenceNumber = env->GetFieldID(clazz, "mSequenceNumber", "I");
    591     if (field_objectInfo_sequenceNumber == NULL) {
    592         ALOGE("Can't find MtpObjectInfo.mSequenceNumber");
    593         return -1;
    594     }
    595     field_objectInfo_name = env->GetFieldID(clazz, "mName", "Ljava/lang/String;");
    596     if (field_objectInfo_name == NULL) {
    597         ALOGE("Can't find MtpObjectInfo.mName");
    598         return -1;
    599     }
    600     field_objectInfo_dateCreated = env->GetFieldID(clazz, "mDateCreated", "J");
    601     if (field_objectInfo_dateCreated == NULL) {
    602         ALOGE("Can't find MtpObjectInfo.mDateCreated");
    603         return -1;
    604     }
    605     field_objectInfo_dateModified = env->GetFieldID(clazz, "mDateModified", "J");
    606     if (field_objectInfo_dateModified == NULL) {
    607         ALOGE("Can't find MtpObjectInfo.mDateModified");
    608         return -1;
    609     }
    610     field_objectInfo_keywords = env->GetFieldID(clazz, "mKeywords", "Ljava/lang/String;");
    611     if (field_objectInfo_keywords == NULL) {
    612         ALOGE("Can't find MtpObjectInfo.mKeywords");
    613         return -1;
    614     }
    615     clazz_objectInfo = (jclass)env->NewGlobalRef(clazz);
    616 
    617     clazz = env->FindClass("android/mtp/MtpDevice");
    618     if (clazz == NULL) {
    619         ALOGE("Can't find android/mtp/MtpDevice");
    620         return -1;
    621     }
    622     field_context = env->GetFieldID(clazz, "mNativeContext", "J");
    623     if (field_context == NULL) {
    624         ALOGE("Can't find MtpDevice.mNativeContext");
    625         return -1;
    626     }
    627 
    628     return AndroidRuntime::registerNativeMethods(env,
    629                 "android/mtp/MtpDevice", gMethods, NELEM(gMethods));
    630 }
    631