• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
29 #include <string>
30 
31 #include "jni.h"
32 #include <nativehelper/JNIHelp.h>
33 #include <nativehelper/ScopedPrimitiveArray.h>
34 
35 #include "android_runtime/AndroidRuntime.h"
36 #include "android_runtime/Log.h"
37 #include "nativehelper/ScopedLocalRef.h"
38 #include "private/android_filesystem_config.h"
39 
40 #include "MtpTypes.h"
41 #include "MtpDevice.h"
42 #include "MtpDeviceInfo.h"
43 #include "MtpStorageInfo.h"
44 #include "MtpObjectInfo.h"
45 #include "MtpProperty.h"
46 
47 using namespace android;
48 
49 // ----------------------------------------------------------------------------
50 
51 namespace {
52 
53 static jfieldID field_context;
54 
55 jclass clazz_deviceInfo;
56 jclass clazz_storageInfo;
57 jclass clazz_objectInfo;
58 jclass clazz_event;
59 jclass clazz_io_exception;
60 jclass clazz_operation_canceled_exception;
61 
62 jmethodID constructor_deviceInfo;
63 jmethodID constructor_storageInfo;
64 jmethodID constructor_objectInfo;
65 jmethodID constructor_event;
66 
67 // MtpDeviceInfo fields
68 static jfieldID field_deviceInfo_manufacturer;
69 static jfieldID field_deviceInfo_model;
70 static jfieldID field_deviceInfo_version;
71 static jfieldID field_deviceInfo_serialNumber;
72 static jfieldID field_deviceInfo_operationsSupported;
73 static jfieldID field_deviceInfo_eventsSupported;
74 
75 // MtpStorageInfo fields
76 static jfieldID field_storageInfo_storageId;
77 static jfieldID field_storageInfo_maxCapacity;
78 static jfieldID field_storageInfo_freeSpace;
79 static jfieldID field_storageInfo_description;
80 static jfieldID field_storageInfo_volumeIdentifier;
81 
82 // MtpObjectInfo fields
83 static jfieldID field_objectInfo_handle;
84 static jfieldID field_objectInfo_storageId;
85 static jfieldID field_objectInfo_format;
86 static jfieldID field_objectInfo_protectionStatus;
87 static jfieldID field_objectInfo_compressedSize;
88 static jfieldID field_objectInfo_thumbFormat;
89 static jfieldID field_objectInfo_thumbCompressedSize;
90 static jfieldID field_objectInfo_thumbPixWidth;
91 static jfieldID field_objectInfo_thumbPixHeight;
92 static jfieldID field_objectInfo_imagePixWidth;
93 static jfieldID field_objectInfo_imagePixHeight;
94 static jfieldID field_objectInfo_imagePixDepth;
95 static jfieldID field_objectInfo_parent;
96 static jfieldID field_objectInfo_associationType;
97 static jfieldID field_objectInfo_associationDesc;
98 static jfieldID field_objectInfo_sequenceNumber;
99 static jfieldID field_objectInfo_name;
100 static jfieldID field_objectInfo_dateCreated;
101 static jfieldID field_objectInfo_dateModified;
102 static jfieldID field_objectInfo_keywords;
103 
104 // MtpEvent fields
105 static jfieldID field_event_eventCode;
106 static jfieldID field_event_parameter1;
107 static jfieldID field_event_parameter2;
108 static jfieldID field_event_parameter3;
109 
110 class JavaArrayWriter {
111 public:
JavaArrayWriter(JNIEnv * env,jbyteArray array)112     JavaArrayWriter(JNIEnv* env, jbyteArray array) :
113         mEnv(env), mArray(array), mSize(mEnv->GetArrayLength(mArray)) {}
write(void * data,uint32_t offset,uint32_t length)114     bool write(void* data, uint32_t offset, uint32_t length) {
115         if (static_cast<uint32_t>(mSize) < offset + length) {
116             return false;
117         }
118         mEnv->SetByteArrayRegion(mArray, offset, length, static_cast<jbyte*>(data));
119         return true;
120     }
writeTo(void * data,uint32_t offset,uint32_t length,void * clientData)121     static bool writeTo(void* data, uint32_t offset, uint32_t length, void* clientData) {
122         return static_cast<JavaArrayWriter*>(clientData)->write(data, offset, length);
123     }
124 
125 private:
126     JNIEnv* mEnv;
127     jbyteArray mArray;
128     jsize mSize;
129 };
130 
131 }
132 
get_device_from_object(JNIEnv * env,jobject javaDevice)133 MtpDevice* get_device_from_object(JNIEnv* env, jobject javaDevice)
134 {
135     return (MtpDevice*)env->GetLongField(javaDevice, field_context);
136 }
137 
fill_jobject_from_object_info(JNIEnv * env,jobject object,MtpObjectInfo * objectInfo)138 void fill_jobject_from_object_info(JNIEnv* env, jobject object, MtpObjectInfo* objectInfo) {
139     if (objectInfo->mHandle)
140         env->SetIntField(object, field_objectInfo_handle, objectInfo->mHandle);
141     if (objectInfo->mStorageID)
142         env->SetIntField(object, field_objectInfo_storageId, objectInfo->mStorageID);
143     if (objectInfo->mFormat)
144         env->SetIntField(object, field_objectInfo_format, objectInfo->mFormat);
145     if (objectInfo->mProtectionStatus)
146         env->SetIntField(object, field_objectInfo_protectionStatus, objectInfo->mProtectionStatus);
147     if (objectInfo->mCompressedSize)
148         env->SetIntField(object, field_objectInfo_compressedSize, objectInfo->mCompressedSize);
149     if (objectInfo->mThumbFormat)
150         env->SetIntField(object, field_objectInfo_thumbFormat, objectInfo->mThumbFormat);
151     if (objectInfo->mThumbCompressedSize) {
152         env->SetIntField(object, field_objectInfo_thumbCompressedSize,
153                 objectInfo->mThumbCompressedSize);
154     }
155     if (objectInfo->mThumbPixWidth)
156         env->SetIntField(object, field_objectInfo_thumbPixWidth, objectInfo->mThumbPixWidth);
157     if (objectInfo->mThumbPixHeight)
158         env->SetIntField(object, field_objectInfo_thumbPixHeight, objectInfo->mThumbPixHeight);
159     if (objectInfo->mImagePixWidth)
160         env->SetIntField(object, field_objectInfo_imagePixWidth, objectInfo->mImagePixWidth);
161     if (objectInfo->mImagePixHeight)
162         env->SetIntField(object, field_objectInfo_imagePixHeight, objectInfo->mImagePixHeight);
163     if (objectInfo->mImagePixDepth)
164         env->SetIntField(object, field_objectInfo_imagePixDepth, objectInfo->mImagePixDepth);
165     if (objectInfo->mParent)
166         env->SetIntField(object, field_objectInfo_parent, objectInfo->mParent);
167     if (objectInfo->mAssociationType)
168         env->SetIntField(object, field_objectInfo_associationType, objectInfo->mAssociationType);
169     if (objectInfo->mAssociationDesc)
170         env->SetIntField(object, field_objectInfo_associationDesc, objectInfo->mAssociationDesc);
171     if (objectInfo->mSequenceNumber)
172         env->SetIntField(object, field_objectInfo_sequenceNumber, objectInfo->mSequenceNumber);
173     if (objectInfo->mName)
174         env->SetObjectField(object, field_objectInfo_name, env->NewStringUTF(objectInfo->mName));
175     if (objectInfo->mDateCreated)
176         env->SetLongField(object, field_objectInfo_dateCreated, objectInfo->mDateCreated * 1000LL);
177     if (objectInfo->mDateModified) {
178         env->SetLongField(object, field_objectInfo_dateModified,
179                 objectInfo->mDateModified * 1000LL);
180     }
181     if (objectInfo->mKeywords) {
182         env->SetObjectField(object, field_objectInfo_keywords,
183             env->NewStringUTF(objectInfo->mKeywords));
184     }
185 }
186 
187 // ----------------------------------------------------------------------------
188 
189 static jboolean
android_mtp_MtpDevice_open(JNIEnv * env,jobject thiz,jstring deviceName,jint fd)190 android_mtp_MtpDevice_open(JNIEnv *env, jobject thiz, jstring deviceName, jint fd)
191 {
192     const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL);
193     if (deviceNameStr == NULL) {
194         return JNI_FALSE;
195     }
196 
197     // The passed in fd is maintained by the UsbDeviceConnection
198     fd = dup(fd);
199 
200     MtpDevice* device = MtpDevice::open(deviceNameStr, fd);
201     env->ReleaseStringUTFChars(deviceName, deviceNameStr);
202 
203     if (device)
204         env->SetLongField(thiz, field_context,  (jlong)device);
205     return (jboolean)(device != NULL);
206 }
207 
208 static void
android_mtp_MtpDevice_close(JNIEnv * env,jobject thiz)209 android_mtp_MtpDevice_close(JNIEnv *env, jobject thiz)
210 {
211     MtpDevice* device = get_device_from_object(env, thiz);
212     if (device) {
213         device->close();
214         delete device;
215         env->SetLongField(thiz, field_context, 0);
216     }
217 }
218 
219 static jobject
android_mtp_MtpDevice_get_device_info(JNIEnv * env,jobject thiz)220 android_mtp_MtpDevice_get_device_info(JNIEnv *env, jobject thiz)
221 {
222     MtpDevice* device = get_device_from_object(env, thiz);
223     if (!device) {
224         ALOGD("android_mtp_MtpDevice_get_device_info device is null");
225         return NULL;
226     }
227     std::unique_ptr<MtpDeviceInfo> deviceInfo(device->getDeviceInfo());
228     if (!deviceInfo) {
229         ALOGD("android_mtp_MtpDevice_get_device_info deviceInfo is null");
230         return NULL;
231     }
232     jobject info = env->NewObject(clazz_deviceInfo, constructor_deviceInfo);
233     if (info == NULL) {
234         ALOGE("Could not create a MtpDeviceInfo object");
235         return NULL;
236     }
237 
238     if (deviceInfo->mManufacturer)
239         env->SetObjectField(info, field_deviceInfo_manufacturer,
240             env->NewStringUTF(deviceInfo->mManufacturer));
241     if (deviceInfo->mModel)
242         env->SetObjectField(info, field_deviceInfo_model,
243             env->NewStringUTF(deviceInfo->mModel));
244     if (deviceInfo->mVersion)
245         env->SetObjectField(info, field_deviceInfo_version,
246             env->NewStringUTF(deviceInfo->mVersion));
247     if (deviceInfo->mSerial)
248         env->SetObjectField(info, field_deviceInfo_serialNumber,
249             env->NewStringUTF(deviceInfo->mSerial));
250     assert(deviceInfo->mOperations);
251     {
252         const size_t size = deviceInfo->mOperations->size();
253         ScopedLocalRef<jintArray> operations(env, static_cast<jintArray>(env->NewIntArray(size)));
254         {
255             ScopedIntArrayRW elements(env, operations.get());
256             if (elements.get() == NULL) {
257                 ALOGE("Could not create operationsSupported element.");
258                 return NULL;
259             }
260             for (size_t i = 0; i < size; ++i) {
261                 elements[i] = deviceInfo->mOperations->itemAt(i);
262             }
263             env->SetObjectField(info, field_deviceInfo_operationsSupported, operations.get());
264         }
265     }
266     assert(deviceInfo->mEvents);
267     {
268         const size_t size = deviceInfo->mEvents->size();
269         ScopedLocalRef<jintArray> events(env, static_cast<jintArray>(env->NewIntArray(size)));
270         {
271             ScopedIntArrayRW elements(env, events.get());
272             if (elements.get() == NULL) {
273                 ALOGE("Could not create eventsSupported element.");
274                 return NULL;
275             }
276             for (size_t i = 0; i < size; ++i) {
277                 elements[i] = deviceInfo->mEvents->itemAt(i);
278             }
279             env->SetObjectField(info, field_deviceInfo_eventsSupported, events.get());
280         }
281     }
282 
283     return info;
284 }
285 
286 static jintArray
android_mtp_MtpDevice_get_storage_ids(JNIEnv * env,jobject thiz)287 android_mtp_MtpDevice_get_storage_ids(JNIEnv *env, jobject thiz)
288 {
289     MtpDevice* device = get_device_from_object(env, thiz);
290     if (!device)
291         return NULL;
292     MtpStorageIDList* storageIDs = device->getStorageIDs();
293     if (!storageIDs)
294         return NULL;
295 
296     int length = storageIDs->size();
297     jintArray array = env->NewIntArray(length);
298     // FIXME is this cast safe?
299     env->SetIntArrayRegion(array, 0, length, (const jint *)storageIDs->array());
300 
301     delete storageIDs;
302     return array;
303 }
304 
305 static jobject
android_mtp_MtpDevice_get_storage_info(JNIEnv * env,jobject thiz,jint storageID)306 android_mtp_MtpDevice_get_storage_info(JNIEnv *env, jobject thiz, jint storageID)
307 {
308     MtpDevice* device = get_device_from_object(env, thiz);
309     if (!device)
310         return NULL;
311     MtpStorageInfo* storageInfo = device->getStorageInfo(storageID);
312     if (!storageInfo)
313         return NULL;
314 
315     jobject info = env->NewObject(clazz_storageInfo, constructor_storageInfo);
316     if (info == NULL) {
317         ALOGE("Could not create a MtpStorageInfo object");
318         delete storageInfo;
319         return NULL;
320     }
321 
322     if (storageInfo->mStorageID)
323         env->SetIntField(info, field_storageInfo_storageId, storageInfo->mStorageID);
324     if (storageInfo->mMaxCapacity)
325         env->SetLongField(info, field_storageInfo_maxCapacity, storageInfo->mMaxCapacity);
326     if (storageInfo->mFreeSpaceBytes)
327         env->SetLongField(info, field_storageInfo_freeSpace, storageInfo->mFreeSpaceBytes);
328     if (storageInfo->mStorageDescription)
329         env->SetObjectField(info, field_storageInfo_description,
330             env->NewStringUTF(storageInfo->mStorageDescription));
331     if (storageInfo->mVolumeIdentifier)
332         env->SetObjectField(info, field_storageInfo_volumeIdentifier,
333             env->NewStringUTF(storageInfo->mVolumeIdentifier));
334 
335     delete storageInfo;
336     return info;
337 }
338 
339 static jintArray
android_mtp_MtpDevice_get_object_handles(JNIEnv * env,jobject thiz,jint storageID,jint format,jint objectID)340 android_mtp_MtpDevice_get_object_handles(JNIEnv *env, jobject thiz,
341         jint storageID, jint format, jint objectID)
342 {
343     MtpDevice* device = get_device_from_object(env, thiz);
344     if (!device)
345         return NULL;
346     MtpObjectHandleList* handles = device->getObjectHandles(storageID, format, objectID);
347     if (!handles)
348         return NULL;
349 
350     int length = handles->size();
351     jintArray array = env->NewIntArray(length);
352     // FIXME is this cast safe?
353     env->SetIntArrayRegion(array, 0, length, (const jint *)handles->array());
354 
355     delete handles;
356     return array;
357 }
358 
359 static jobject
android_mtp_MtpDevice_get_object_info(JNIEnv * env,jobject thiz,jint objectID)360 android_mtp_MtpDevice_get_object_info(JNIEnv *env, jobject thiz, jint objectID)
361 {
362     MtpDevice* device = get_device_from_object(env, thiz);
363     if (!device)
364         return NULL;
365     MtpObjectInfo* objectInfo = device->getObjectInfo(objectID);
366     if (!objectInfo)
367         return NULL;
368     jobject info = env->NewObject(clazz_objectInfo, constructor_objectInfo);
369     if (info == NULL) {
370         ALOGE("Could not create a MtpObjectInfo object");
371         delete objectInfo;
372         return NULL;
373     }
374 
375     fill_jobject_from_object_info(env, info, objectInfo);
376     delete objectInfo;
377     return info;
378 }
379 
check_uint32_arg(JNIEnv * env,const char * name,jlong value,uint32_t * out)380 bool check_uint32_arg(JNIEnv *env, const char* name, jlong value, uint32_t* out) {
381     if (value < 0 || 0xffffffff < value) {
382         jniThrowException(
383                 env,
384                 "java/lang/IllegalArgumentException",
385                 (std::string("argument must be a 32-bit unsigned integer: ") + name).c_str());
386         return false;
387     }
388     *out = static_cast<uint32_t>(value);
389     return true;
390 }
391 
392 static jbyteArray
android_mtp_MtpDevice_get_object(JNIEnv * env,jobject thiz,jint objectID,jlong objectSizeLong)393 android_mtp_MtpDevice_get_object(JNIEnv *env, jobject thiz, jint objectID, jlong objectSizeLong)
394 {
395     uint32_t objectSize;
396     if (!check_uint32_arg(env, "objectSize", objectSizeLong, &objectSize)) {
397         return nullptr;
398     }
399 
400     MtpDevice* device = get_device_from_object(env, thiz);
401     if (!device) {
402         return nullptr;
403     }
404 
405     ScopedLocalRef<jbyteArray> array(env, env->NewByteArray(objectSize));
406     if (!array.get()) {
407         jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
408         return nullptr;
409     }
410 
411     JavaArrayWriter writer(env, array.get());
412 
413     if (device->readObject(objectID, JavaArrayWriter::writeTo, objectSize, &writer)) {
414         return array.release();
415     }
416     return nullptr;
417 }
418 
419 static jlong
android_mtp_MtpDevice_get_partial_object(JNIEnv * env,jobject thiz,jint objectID,jlong offsetLong,jlong sizeLong,jbyteArray array)420 android_mtp_MtpDevice_get_partial_object(JNIEnv *env,
421                                          jobject thiz,
422                                          jint objectID,
423                                          jlong offsetLong,
424                                          jlong sizeLong,
425                                          jbyteArray array)
426 {
427     if (!array) {
428         jniThrowException(env, "java/lang/IllegalArgumentException", "Array must not be null.");
429         return -1;
430     }
431 
432     uint32_t offset;
433     uint32_t size;
434     if (!check_uint32_arg(env, "offset", offsetLong, &offset) ||
435             !check_uint32_arg(env, "size", sizeLong, &size)) {
436         return -1;
437     }
438 
439     MtpDevice* const device = get_device_from_object(env, thiz);
440     if (!device) {
441         jniThrowException(env, "java/io/IOException", "Failed to obtain MtpDevice.");
442         return -1;
443     }
444 
445     JavaArrayWriter writer(env, array);
446     uint32_t written_size;
447     const bool success = device->readPartialObject(
448             objectID, offset, size, &written_size, JavaArrayWriter::writeTo, &writer);
449     if (!success) {
450         jniThrowException(env, "java/io/IOException", "Failed to read data.");
451         return -1;
452     }
453     return static_cast<jlong>(written_size);
454 }
455 
456 static jint
android_mtp_MtpDevice_get_partial_object_64(JNIEnv * env,jobject thiz,jint objectID,jlong offset,jlong size,jbyteArray array)457 android_mtp_MtpDevice_get_partial_object_64(JNIEnv *env,
458                                             jobject thiz,
459                                             jint objectID,
460                                             jlong offset,
461                                             jlong size,
462                                             jbyteArray array) {
463     if (!array) {
464         jniThrowException(env, "java/lang/IllegalArgumentException", "Array must not be null.");
465         return -1;
466     }
467 
468     if (offset < 0) {
469         jniThrowException(
470                 env,
471                 "java/lang/IllegalArgumentException",
472                 "Offset argument must not be a negative value.");
473         return -1;
474     }
475 
476     if (size < 0 || 0xffffffffL < size) {
477         jniThrowException(
478                 env,
479                 "java/lang/IllegalArgumentException",
480                 "Size argument must be a 32-bit unsigned integer.");
481         return -1;
482     }
483 
484     MtpDevice* const device = get_device_from_object(env, thiz);
485     if (!device) {
486         jniThrowException(env, "java/io/IOException", "Failed to obtain MtpDevice.");
487         return -1;
488     }
489 
490     const uint32_t native_object_handle = static_cast<uint32_t>(objectID);
491     const uint64_t native_offset = static_cast<uint64_t>(offset);
492     const uint32_t native_size = static_cast<uint32_t>(size);
493 
494     JavaArrayWriter writer(env, array);
495     uint32_t written_size;
496     const bool success = device->readPartialObject64(
497             native_object_handle,
498             native_offset,
499             native_size,
500             &written_size,
501             JavaArrayWriter::writeTo,
502             &writer);
503     if (!success) {
504         jniThrowException(env, "java/io/IOException", "Failed to read data.");
505         return -1;
506     }
507     return static_cast<jint>(written_size);
508 }
509 
510 static jbyteArray
android_mtp_MtpDevice_get_thumbnail(JNIEnv * env,jobject thiz,jint objectID)511 android_mtp_MtpDevice_get_thumbnail(JNIEnv *env, jobject thiz, jint objectID)
512 {
513     MtpDevice* device = get_device_from_object(env, thiz);
514     if (!device)
515         return NULL;
516 
517     int length;
518     void* thumbnail = device->getThumbnail(objectID, length);
519     if (! thumbnail)
520         return NULL;
521     jbyteArray array = env->NewByteArray(length);
522     env->SetByteArrayRegion(array, 0, length, (const jbyte *)thumbnail);
523 
524     free(thumbnail);
525     return array;
526 }
527 
528 static jboolean
android_mtp_MtpDevice_delete_object(JNIEnv * env,jobject thiz,jint object_id)529 android_mtp_MtpDevice_delete_object(JNIEnv *env, jobject thiz, jint object_id)
530 {
531     MtpDevice* device = get_device_from_object(env, thiz);
532     if (device && device->deleteObject(object_id)) {
533         return JNI_TRUE;
534     } else {
535         return JNI_FALSE;
536     }
537 }
538 
539 static jint
android_mtp_MtpDevice_get_parent(JNIEnv * env,jobject thiz,jint object_id)540 android_mtp_MtpDevice_get_parent(JNIEnv *env, jobject thiz, jint object_id)
541 {
542     MtpDevice* device = get_device_from_object(env, thiz);
543     if (device)
544         return static_cast<jint>(device->getParent(object_id));
545     else
546         return -1;
547 }
548 
549 static jint
android_mtp_MtpDevice_get_storage_id(JNIEnv * env,jobject thiz,jint object_id)550 android_mtp_MtpDevice_get_storage_id(JNIEnv *env, jobject thiz, jint object_id)
551 {
552     MtpDevice* device = get_device_from_object(env, thiz);
553     if (device)
554         return static_cast<jint>(device->getStorageID(object_id));
555     else
556         return -1;
557 }
558 
559 static jboolean
android_mtp_MtpDevice_import_file(JNIEnv * env,jobject thiz,jint object_id,jstring dest_path)560 android_mtp_MtpDevice_import_file(JNIEnv *env, jobject thiz, jint object_id, jstring dest_path)
561 {
562     MtpDevice* device = get_device_from_object(env, thiz);
563     if (device) {
564         const char *destPathStr = env->GetStringUTFChars(dest_path, NULL);
565         if (destPathStr == NULL) {
566             return JNI_FALSE;
567         }
568 
569         jboolean result = device->readObject(object_id, destPathStr, AID_SDCARD_RW, 0664);
570         env->ReleaseStringUTFChars(dest_path, destPathStr);
571         return result;
572     }
573 
574     return JNI_FALSE;
575 }
576 
577 static jboolean
android_mtp_MtpDevice_import_file_to_fd(JNIEnv * env,jobject thiz,jint object_id,jint fd)578 android_mtp_MtpDevice_import_file_to_fd(JNIEnv *env, jobject thiz, jint object_id, jint fd)
579 {
580     MtpDevice* device = get_device_from_object(env, thiz);
581     if (device)
582         return device->readObject(object_id, fd);
583     else
584         return JNI_FALSE;
585 }
586 
587 static jboolean
android_mtp_MtpDevice_send_object(JNIEnv * env,jobject thiz,jint object_id,jlong sizeLong,jint fd)588 android_mtp_MtpDevice_send_object(
589         JNIEnv *env, jobject thiz, jint object_id, jlong sizeLong, jint fd)
590 {
591     uint32_t size;
592     if (!check_uint32_arg(env, "size", sizeLong, &size))
593         return JNI_FALSE;
594 
595     MtpDevice* device = get_device_from_object(env, thiz);
596     if (!device)
597         return JNI_FALSE;
598 
599     return device->sendObject(object_id, size, fd);
600 }
601 
602 static jobject
android_mtp_MtpDevice_send_object_info(JNIEnv * env,jobject thiz,jobject info)603 android_mtp_MtpDevice_send_object_info(JNIEnv *env, jobject thiz, jobject info)
604 {
605     MtpDevice* device = get_device_from_object(env, thiz);
606     if (!device) {
607         return NULL;
608     }
609 
610     // Updating existing objects is not supported.
611     if (env->GetIntField(info, field_objectInfo_handle) != -1) {
612         return NULL;
613     }
614 
615     MtpObjectInfo* object_info = new MtpObjectInfo(-1);
616     object_info->mStorageID = env->GetIntField(info, field_objectInfo_storageId);
617     object_info->mFormat = env->GetIntField(info, field_objectInfo_format);
618     object_info->mProtectionStatus = env->GetIntField(info, field_objectInfo_protectionStatus);
619     object_info->mCompressedSize = env->GetIntField(info, field_objectInfo_compressedSize);
620     object_info->mThumbFormat = env->GetIntField(info, field_objectInfo_thumbFormat);
621     object_info->mThumbCompressedSize =
622             env->GetIntField(info, field_objectInfo_thumbCompressedSize);
623     object_info->mThumbPixWidth = env->GetIntField(info, field_objectInfo_thumbPixWidth);
624     object_info->mThumbPixHeight = env->GetIntField(info, field_objectInfo_thumbPixHeight);
625     object_info->mImagePixWidth = env->GetIntField(info, field_objectInfo_imagePixWidth);
626     object_info->mImagePixHeight = env->GetIntField(info, field_objectInfo_imagePixHeight);
627     object_info->mImagePixDepth = env->GetIntField(info, field_objectInfo_imagePixDepth);
628     object_info->mParent = env->GetIntField(info, field_objectInfo_parent);
629     object_info->mAssociationType = env->GetIntField(info, field_objectInfo_associationType);
630     object_info->mAssociationDesc = env->GetIntField(info, field_objectInfo_associationDesc);
631     object_info->mSequenceNumber = env->GetIntField(info, field_objectInfo_sequenceNumber);
632 
633     jstring name_jstring = (jstring) env->GetObjectField(info, field_objectInfo_name);
634     if (name_jstring != NULL) {
635         const char* name_string = env->GetStringUTFChars(name_jstring, NULL);
636         object_info->mName = strdup(name_string);
637         env->ReleaseStringUTFChars(name_jstring, name_string);
638     }
639 
640     object_info->mDateCreated = env->GetLongField(info, field_objectInfo_dateCreated) / 1000LL;
641     object_info->mDateModified = env->GetLongField(info, field_objectInfo_dateModified) / 1000LL;
642 
643     jstring keywords_jstring = (jstring) env->GetObjectField(info, field_objectInfo_keywords);
644     if (keywords_jstring != NULL) {
645         const char* keywords_string = env->GetStringUTFChars(keywords_jstring, NULL);
646         object_info->mKeywords = strdup(keywords_string);
647         env->ReleaseStringUTFChars(keywords_jstring, keywords_string);
648     }
649 
650     int object_handle = device->sendObjectInfo(object_info);
651     if (object_handle == -1) {
652         delete object_info;
653         return NULL;
654     }
655 
656     object_info->mHandle = object_handle;
657     jobject result = env->NewObject(clazz_objectInfo, constructor_objectInfo);
658     if (result == NULL) {
659         ALOGE("Could not create a MtpObjectInfo object");
660         delete object_info;
661         return NULL;
662     }
663 
664     fill_jobject_from_object_info(env, result, object_info);
665     delete object_info;
666     return result;
667 }
668 
android_mtp_MtpDevice_submit_event_request(JNIEnv * env,jobject thiz)669 static jint android_mtp_MtpDevice_submit_event_request(JNIEnv *env, jobject thiz)
670 {
671     MtpDevice* const device = get_device_from_object(env, thiz);
672     if (!device) {
673         env->ThrowNew(clazz_io_exception, "");
674         return -1;
675     }
676     return device->submitEventRequest();
677 }
678 
android_mtp_MtpDevice_reap_event_request(JNIEnv * env,jobject thiz,jint seq)679 static jobject android_mtp_MtpDevice_reap_event_request(JNIEnv *env, jobject thiz, jint seq)
680 {
681     MtpDevice* const device = get_device_from_object(env, thiz);
682     if (!device) {
683         env->ThrowNew(clazz_io_exception, "");
684         return NULL;
685     }
686     uint32_t parameters[3];
687     const int eventCode = device->reapEventRequest(seq, &parameters);
688     if (eventCode <= 0) {
689         env->ThrowNew(clazz_operation_canceled_exception, "");
690         return NULL;
691     }
692     jobject result = env->NewObject(clazz_event, constructor_event);
693     env->SetIntField(result, field_event_eventCode, eventCode);
694     env->SetIntField(result, field_event_parameter1, static_cast<jint>(parameters[0]));
695     env->SetIntField(result, field_event_parameter2, static_cast<jint>(parameters[1]));
696     env->SetIntField(result, field_event_parameter3, static_cast<jint>(parameters[2]));
697     return result;
698 }
699 
android_mtp_MtpDevice_discard_event_request(JNIEnv * env,jobject thiz,jint seq)700 static void android_mtp_MtpDevice_discard_event_request(JNIEnv *env, jobject thiz, jint seq)
701 {
702     MtpDevice* const device = get_device_from_object(env, thiz);
703     if (!device) {
704         return;
705     }
706     device->discardEventRequest(seq);
707 }
708 
709 // Returns object size in 64-bit integer. If the MTP device does not support the property, it
710 // throws IOException.
android_mtp_MtpDevice_get_object_size_long(JNIEnv * env,jobject thiz,jint handle,jint format)711 static jlong android_mtp_MtpDevice_get_object_size_long(
712         JNIEnv *env, jobject thiz, jint handle, jint format) {
713     MtpDevice* const device = get_device_from_object(env, thiz);
714     if (!device) {
715         env->ThrowNew(clazz_io_exception, "Failed to obtain MtpDevice.");
716         return 0;
717     }
718 
719     std::unique_ptr<MtpProperty> property(
720             device->getObjectPropDesc(MTP_PROPERTY_OBJECT_SIZE, format));
721     if (!property) {
722         env->ThrowNew(clazz_io_exception, "Failed to obtain property desc.");
723         return 0;
724     }
725 
726     if (property->getDataType() != MTP_TYPE_UINT64) {
727         env->ThrowNew(clazz_io_exception, "Unexpected property data type.");
728         return 0;
729     }
730 
731     if (!device->getObjectPropValue(handle, property.get())) {
732         env->ThrowNew(clazz_io_exception, "Failed to obtain property value.");
733         return 0;
734     }
735 
736     const jlong object_size = static_cast<jlong>(property->getCurrentValue().u.u64);
737     if (object_size < 0) {
738         env->ThrowNew(clazz_io_exception, "Object size is too large to express as jlong.");
739         return 0;
740     }
741 
742     return object_size;
743 }
744 
745 // ----------------------------------------------------------------------------
746 
747 static const JNINativeMethod gMethods[] = {
748     {"native_open",             "(Ljava/lang/String;I)Z",
749                                         (void *)android_mtp_MtpDevice_open},
750     {"native_close",            "()V",  (void *)android_mtp_MtpDevice_close},
751     {"native_get_device_info",  "()Landroid/mtp/MtpDeviceInfo;",
752                                         (void *)android_mtp_MtpDevice_get_device_info},
753     {"native_get_storage_ids",  "()[I", (void *)android_mtp_MtpDevice_get_storage_ids},
754     {"native_get_storage_info", "(I)Landroid/mtp/MtpStorageInfo;",
755                                         (void *)android_mtp_MtpDevice_get_storage_info},
756     {"native_get_object_handles","(III)[I",
757                                         (void *)android_mtp_MtpDevice_get_object_handles},
758     {"native_get_object_info",  "(I)Landroid/mtp/MtpObjectInfo;",
759                                         (void *)android_mtp_MtpDevice_get_object_info},
760     {"native_get_object",       "(IJ)[B",(void *)android_mtp_MtpDevice_get_object},
761     {"native_get_partial_object", "(IJJ[B)J", (void *)android_mtp_MtpDevice_get_partial_object},
762     {"native_get_partial_object_64", "(IJJ[B)I",
763                                         (void *)android_mtp_MtpDevice_get_partial_object_64},
764     {"native_get_thumbnail",    "(I)[B",(void *)android_mtp_MtpDevice_get_thumbnail},
765     {"native_delete_object",    "(I)Z", (void *)android_mtp_MtpDevice_delete_object},
766     {"native_get_parent",       "(I)I", (void *)android_mtp_MtpDevice_get_parent},
767     {"native_get_storage_id",   "(I)I", (void *)android_mtp_MtpDevice_get_storage_id},
768     {"native_import_file",      "(ILjava/lang/String;)Z",
769                                         (void *)android_mtp_MtpDevice_import_file},
770     {"native_import_file",      "(II)Z",(void *)android_mtp_MtpDevice_import_file_to_fd},
771     {"native_send_object",      "(IJI)Z",(void *)android_mtp_MtpDevice_send_object},
772     {"native_send_object_info", "(Landroid/mtp/MtpObjectInfo;)Landroid/mtp/MtpObjectInfo;",
773                                         (void *)android_mtp_MtpDevice_send_object_info},
774     {"native_submit_event_request",  "()I", (void *)android_mtp_MtpDevice_submit_event_request},
775     {"native_reap_event_request",   "(I)Landroid/mtp/MtpEvent;",
776                                             (void *)android_mtp_MtpDevice_reap_event_request},
777     {"native_discard_event_request", "(I)V", (void *)android_mtp_MtpDevice_discard_event_request},
778 
779     {"native_get_object_size_long", "(II)J", (void *)android_mtp_MtpDevice_get_object_size_long},
780 };
781 
register_android_mtp_MtpDevice(JNIEnv * env)782 int register_android_mtp_MtpDevice(JNIEnv *env)
783 {
784     jclass clazz;
785 
786     ALOGD("register_android_mtp_MtpDevice\n");
787 
788     clazz = env->FindClass("android/mtp/MtpDeviceInfo");
789     if (clazz == NULL) {
790         ALOGE("Can't find android/mtp/MtpDeviceInfo");
791         return -1;
792     }
793     constructor_deviceInfo = env->GetMethodID(clazz, "<init>", "()V");
794     if (constructor_deviceInfo == NULL) {
795         ALOGE("Can't find android/mtp/MtpDeviceInfo constructor");
796         return -1;
797     }
798     field_deviceInfo_manufacturer = env->GetFieldID(clazz, "mManufacturer", "Ljava/lang/String;");
799     if (field_deviceInfo_manufacturer == NULL) {
800         ALOGE("Can't find MtpDeviceInfo.mManufacturer");
801         return -1;
802     }
803     field_deviceInfo_model = env->GetFieldID(clazz, "mModel", "Ljava/lang/String;");
804     if (field_deviceInfo_model == NULL) {
805         ALOGE("Can't find MtpDeviceInfo.mModel");
806         return -1;
807     }
808     field_deviceInfo_version = env->GetFieldID(clazz, "mVersion", "Ljava/lang/String;");
809     if (field_deviceInfo_version == NULL) {
810         ALOGE("Can't find MtpDeviceInfo.mVersion");
811         return -1;
812     }
813     field_deviceInfo_serialNumber = env->GetFieldID(clazz, "mSerialNumber", "Ljava/lang/String;");
814     if (field_deviceInfo_serialNumber == NULL) {
815         ALOGE("Can't find MtpDeviceInfo.mSerialNumber");
816         return -1;
817     }
818     field_deviceInfo_operationsSupported = env->GetFieldID(clazz, "mOperationsSupported", "[I");
819     if (field_deviceInfo_operationsSupported == NULL) {
820         ALOGE("Can't find MtpDeviceInfo.mOperationsSupported");
821         return -1;
822     }
823     field_deviceInfo_eventsSupported = env->GetFieldID(clazz, "mEventsSupported", "[I");
824     if (field_deviceInfo_eventsSupported == NULL) {
825         ALOGE("Can't find MtpDeviceInfo.mEventsSupported");
826         return -1;
827     }
828     clazz_deviceInfo = (jclass)env->NewGlobalRef(clazz);
829 
830     clazz = env->FindClass("android/mtp/MtpStorageInfo");
831     if (clazz == NULL) {
832         ALOGE("Can't find android/mtp/MtpStorageInfo");
833         return -1;
834     }
835     constructor_storageInfo = env->GetMethodID(clazz, "<init>", "()V");
836     if (constructor_storageInfo == NULL) {
837         ALOGE("Can't find android/mtp/MtpStorageInfo constructor");
838         return -1;
839     }
840     field_storageInfo_storageId = env->GetFieldID(clazz, "mStorageId", "I");
841     if (field_storageInfo_storageId == NULL) {
842         ALOGE("Can't find MtpStorageInfo.mStorageId");
843         return -1;
844     }
845     field_storageInfo_maxCapacity = env->GetFieldID(clazz, "mMaxCapacity", "J");
846     if (field_storageInfo_maxCapacity == NULL) {
847         ALOGE("Can't find MtpStorageInfo.mMaxCapacity");
848         return -1;
849     }
850     field_storageInfo_freeSpace = env->GetFieldID(clazz, "mFreeSpace", "J");
851     if (field_storageInfo_freeSpace == NULL) {
852         ALOGE("Can't find MtpStorageInfo.mFreeSpace");
853         return -1;
854     }
855     field_storageInfo_description = env->GetFieldID(clazz, "mDescription", "Ljava/lang/String;");
856     if (field_storageInfo_description == NULL) {
857         ALOGE("Can't find MtpStorageInfo.mDescription");
858         return -1;
859     }
860     field_storageInfo_volumeIdentifier = env->GetFieldID(clazz, "mVolumeIdentifier", "Ljava/lang/String;");
861     if (field_storageInfo_volumeIdentifier == NULL) {
862         ALOGE("Can't find MtpStorageInfo.mVolumeIdentifier");
863         return -1;
864     }
865     clazz_storageInfo = (jclass)env->NewGlobalRef(clazz);
866 
867     clazz = env->FindClass("android/mtp/MtpObjectInfo");
868     if (clazz == NULL) {
869         ALOGE("Can't find android/mtp/MtpObjectInfo");
870         return -1;
871     }
872     constructor_objectInfo = env->GetMethodID(clazz, "<init>", "()V");
873     if (constructor_objectInfo == NULL) {
874         ALOGE("Can't find android/mtp/MtpObjectInfo constructor");
875         return -1;
876     }
877     field_objectInfo_handle = env->GetFieldID(clazz, "mHandle", "I");
878     if (field_objectInfo_handle == NULL) {
879         ALOGE("Can't find MtpObjectInfo.mHandle");
880         return -1;
881     }
882     field_objectInfo_storageId = env->GetFieldID(clazz, "mStorageId", "I");
883     if (field_objectInfo_storageId == NULL) {
884         ALOGE("Can't find MtpObjectInfo.mStorageId");
885         return -1;
886     }
887     field_objectInfo_format = env->GetFieldID(clazz, "mFormat", "I");
888     if (field_objectInfo_format == NULL) {
889         ALOGE("Can't find MtpObjectInfo.mFormat");
890         return -1;
891     }
892     field_objectInfo_protectionStatus = env->GetFieldID(clazz, "mProtectionStatus", "I");
893     if (field_objectInfo_protectionStatus == NULL) {
894         ALOGE("Can't find MtpObjectInfo.mProtectionStatus");
895         return -1;
896     }
897     field_objectInfo_compressedSize = env->GetFieldID(clazz, "mCompressedSize", "I");
898     if (field_objectInfo_compressedSize == NULL) {
899         ALOGE("Can't find MtpObjectInfo.mCompressedSize");
900         return -1;
901     }
902     field_objectInfo_thumbFormat = env->GetFieldID(clazz, "mThumbFormat", "I");
903     if (field_objectInfo_thumbFormat == NULL) {
904         ALOGE("Can't find MtpObjectInfo.mThumbFormat");
905         return -1;
906     }
907     field_objectInfo_thumbCompressedSize = env->GetFieldID(clazz, "mThumbCompressedSize", "I");
908     if (field_objectInfo_thumbCompressedSize == NULL) {
909         ALOGE("Can't find MtpObjectInfo.mThumbCompressedSize");
910         return -1;
911     }
912     field_objectInfo_thumbPixWidth = env->GetFieldID(clazz, "mThumbPixWidth", "I");
913     if (field_objectInfo_thumbPixWidth == NULL) {
914         ALOGE("Can't find MtpObjectInfo.mThumbPixWidth");
915         return -1;
916     }
917     field_objectInfo_thumbPixHeight = env->GetFieldID(clazz, "mThumbPixHeight", "I");
918     if (field_objectInfo_thumbPixHeight == NULL) {
919         ALOGE("Can't find MtpObjectInfo.mThumbPixHeight");
920         return -1;
921     }
922     field_objectInfo_imagePixWidth = env->GetFieldID(clazz, "mImagePixWidth", "I");
923     if (field_objectInfo_imagePixWidth == NULL) {
924         ALOGE("Can't find MtpObjectInfo.mImagePixWidth");
925         return -1;
926     }
927     field_objectInfo_imagePixHeight = env->GetFieldID(clazz, "mImagePixHeight", "I");
928     if (field_objectInfo_imagePixHeight == NULL) {
929         ALOGE("Can't find MtpObjectInfo.mImagePixHeight");
930         return -1;
931     }
932     field_objectInfo_imagePixDepth = env->GetFieldID(clazz, "mImagePixDepth", "I");
933     if (field_objectInfo_imagePixDepth == NULL) {
934         ALOGE("Can't find MtpObjectInfo.mImagePixDepth");
935         return -1;
936     }
937     field_objectInfo_parent = env->GetFieldID(clazz, "mParent", "I");
938     if (field_objectInfo_parent == NULL) {
939         ALOGE("Can't find MtpObjectInfo.mParent");
940         return -1;
941     }
942     field_objectInfo_associationType = env->GetFieldID(clazz, "mAssociationType", "I");
943     if (field_objectInfo_associationType == NULL) {
944         ALOGE("Can't find MtpObjectInfo.mAssociationType");
945         return -1;
946     }
947     field_objectInfo_associationDesc = env->GetFieldID(clazz, "mAssociationDesc", "I");
948     if (field_objectInfo_associationDesc == NULL) {
949         ALOGE("Can't find MtpObjectInfo.mAssociationDesc");
950         return -1;
951     }
952     field_objectInfo_sequenceNumber = env->GetFieldID(clazz, "mSequenceNumber", "I");
953     if (field_objectInfo_sequenceNumber == NULL) {
954         ALOGE("Can't find MtpObjectInfo.mSequenceNumber");
955         return -1;
956     }
957     field_objectInfo_name = env->GetFieldID(clazz, "mName", "Ljava/lang/String;");
958     if (field_objectInfo_name == NULL) {
959         ALOGE("Can't find MtpObjectInfo.mName");
960         return -1;
961     }
962     field_objectInfo_dateCreated = env->GetFieldID(clazz, "mDateCreated", "J");
963     if (field_objectInfo_dateCreated == NULL) {
964         ALOGE("Can't find MtpObjectInfo.mDateCreated");
965         return -1;
966     }
967     field_objectInfo_dateModified = env->GetFieldID(clazz, "mDateModified", "J");
968     if (field_objectInfo_dateModified == NULL) {
969         ALOGE("Can't find MtpObjectInfo.mDateModified");
970         return -1;
971     }
972     field_objectInfo_keywords = env->GetFieldID(clazz, "mKeywords", "Ljava/lang/String;");
973     if (field_objectInfo_keywords == NULL) {
974         ALOGE("Can't find MtpObjectInfo.mKeywords");
975         return -1;
976     }
977     clazz_objectInfo = (jclass)env->NewGlobalRef(clazz);
978 
979     clazz = env->FindClass("android/mtp/MtpEvent");
980     if (clazz == NULL) {
981         ALOGE("Can't find android/mtp/MtpEvent");
982         return -1;
983     }
984     constructor_event = env->GetMethodID(clazz, "<init>", "()V");
985     if (constructor_event == NULL) {
986         ALOGE("Can't find android/mtp/MtpEvent constructor");
987         return -1;
988     }
989     field_event_eventCode = env->GetFieldID(clazz, "mEventCode", "I");
990     if (field_event_eventCode == NULL) {
991         ALOGE("Can't find MtpObjectInfo.mEventCode");
992         return -1;
993     }
994     field_event_parameter1 = env->GetFieldID(clazz, "mParameter1", "I");
995     if (field_event_parameter1 == NULL) {
996         ALOGE("Can't find MtpObjectInfo.mParameter1");
997         return -1;
998     }
999     field_event_parameter2 = env->GetFieldID(clazz, "mParameter2", "I");
1000     if (field_event_parameter2 == NULL) {
1001         ALOGE("Can't find MtpObjectInfo.mParameter2");
1002         return -1;
1003     }
1004     field_event_parameter3 = env->GetFieldID(clazz, "mParameter3", "I");
1005     if (field_event_parameter3 == NULL) {
1006         ALOGE("Can't find MtpObjectInfo.mParameter3");
1007         return -1;
1008     }
1009     clazz_event = (jclass)env->NewGlobalRef(clazz);
1010 
1011     clazz = env->FindClass("android/mtp/MtpDevice");
1012     if (clazz == NULL) {
1013         ALOGE("Can't find android/mtp/MtpDevice");
1014         return -1;
1015     }
1016     field_context = env->GetFieldID(clazz, "mNativeContext", "J");
1017     if (field_context == NULL) {
1018         ALOGE("Can't find MtpDevice.mNativeContext");
1019         return -1;
1020     }
1021     clazz = env->FindClass("java/io/IOException");
1022     if (clazz == NULL) {
1023         ALOGE("Can't find java.io.IOException");
1024         return -1;
1025     }
1026     clazz_io_exception = (jclass)env->NewGlobalRef(clazz);
1027     clazz = env->FindClass("android/os/OperationCanceledException");
1028     if (clazz == NULL) {
1029         ALOGE("Can't find android.os.OperationCanceledException");
1030         return -1;
1031     }
1032     clazz_operation_canceled_exception = (jclass)env->NewGlobalRef(clazz);
1033 
1034     return AndroidRuntime::registerNativeMethods(env,
1035                 "android/mtp/MtpDevice", gMethods, NELEM(gMethods));
1036 }
1037