Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 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 | |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 28 | #include <memory> |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 29 | #include <string> |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 30 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 31 | #include "jni.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 32 | #include <nativehelper/JNIHelp.h> |
| 33 | #include <nativehelper/ScopedPrimitiveArray.h> |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 34 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 35 | #include "android_runtime/AndroidRuntime.h" |
Ruben Brunk | 87eac99 | 2013-09-09 17:44:59 -0700 | [diff] [blame] | 36 | #include "android_runtime/Log.h" |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 37 | #include "nativehelper/ScopedLocalRef.h" |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 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" |
Daichi Hirono | 787821b | 2016-03-24 21:05:51 +0900 | [diff] [blame] | 45 | #include "MtpProperty.h" |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 46 | |
| 47 | using namespace android; |
| 48 | |
| 49 | // ---------------------------------------------------------------------------- |
| 50 | |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 51 | namespace { |
| 52 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 53 | static jfieldID field_context; |
| 54 | |
| 55 | jclass clazz_deviceInfo; |
| 56 | jclass clazz_storageInfo; |
| 57 | jclass clazz_objectInfo; |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 58 | jclass clazz_event; |
| 59 | jclass clazz_io_exception; |
| 60 | jclass clazz_operation_canceled_exception; |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 61 | |
| 62 | jmethodID constructor_deviceInfo; |
| 63 | jmethodID constructor_storageInfo; |
| 64 | jmethodID constructor_objectInfo; |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 65 | jmethodID constructor_event; |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 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; |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 72 | static jfieldID field_deviceInfo_operationsSupported; |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 73 | static jfieldID field_deviceInfo_eventsSupported; |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 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 | |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 104 | // MtpEvent fields |
| 105 | static jfieldID field_event_eventCode; |
Daichi Hirono | 2a9a433 | 2016-01-11 13:33:41 +0900 | [diff] [blame] | 106 | static jfieldID field_event_parameter1; |
| 107 | static jfieldID field_event_parameter2; |
| 108 | static jfieldID field_event_parameter3; |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 109 | |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 110 | class JavaArrayWriter { |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 111 | public: |
| 112 | JavaArrayWriter(JNIEnv* env, jbyteArray array) : |
| 113 | mEnv(env), mArray(array), mSize(mEnv->GetArrayLength(mArray)) {} |
| 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 | } |
| 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 | } |
Daichi Hirono | 3ad1a8ee | 2016-01-07 13:06:11 +0900 | [diff] [blame] | 124 | |
| 125 | private: |
| 126 | JNIEnv* mEnv; |
| 127 | jbyteArray mArray; |
| 128 | jsize mSize; |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } |
| 132 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 133 | MtpDevice* get_device_from_object(JNIEnv* env, jobject javaDevice) |
| 134 | { |
Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 135 | return (MtpDevice*)env->GetLongField(javaDevice, field_context); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 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 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 187 | // ---------------------------------------------------------------------------- |
| 188 | |
| 189 | static jboolean |
| 190 | android_mtp_MtpDevice_open(JNIEnv *env, jobject thiz, jstring deviceName, jint fd) |
| 191 | { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 192 | const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL); |
James Dong | 3977472 | 2011-04-06 11:57:48 -0700 | [diff] [blame] | 193 | if (deviceNameStr == NULL) { |
Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 194 | return JNI_FALSE; |
James Dong | 3977472 | 2011-04-06 11:57:48 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Philip P. Moltmann | b828b77 | 2016-09-13 16:35:45 -0700 | [diff] [blame] | 197 | // The passed in fd is maintained by the UsbDeviceConnection |
| 198 | fd = dup(fd); |
| 199 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 200 | MtpDevice* device = MtpDevice::open(deviceNameStr, fd); |
| 201 | env->ReleaseStringUTFChars(deviceName, deviceNameStr); |
| 202 | |
| 203 | if (device) |
Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 204 | env->SetLongField(thiz, field_context, (jlong)device); |
| 205 | return (jboolean)(device != NULL); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static void |
| 209 | android_mtp_MtpDevice_close(JNIEnv *env, jobject thiz) |
| 210 | { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 211 | MtpDevice* device = get_device_from_object(env, thiz); |
| 212 | if (device) { |
| 213 | device->close(); |
| 214 | delete device; |
Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 215 | env->SetLongField(thiz, field_context, 0); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 216 | } |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static jobject |
| 220 | android_mtp_MtpDevice_get_device_info(JNIEnv *env, jobject thiz) |
| 221 | { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 222 | MtpDevice* device = get_device_from_object(env, thiz); |
| 223 | if (!device) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 224 | ALOGD("android_mtp_MtpDevice_get_device_info device is null"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 225 | return NULL; |
| 226 | } |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 227 | std::unique_ptr<MtpDeviceInfo> deviceInfo(device->getDeviceInfo()); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 228 | if (!deviceInfo) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 229 | ALOGD("android_mtp_MtpDevice_get_device_info deviceInfo is null"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 230 | return NULL; |
| 231 | } |
| 232 | jobject info = env->NewObject(clazz_deviceInfo, constructor_deviceInfo); |
| 233 | if (info == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 234 | ALOGE("Could not create a MtpDeviceInfo object"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 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)); |
Daichi Hirono | 399df70 | 2016-04-13 11:07:44 +0900 | [diff] [blame] | 250 | assert(deviceInfo->mOperations); |
| 251 | { |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 252 | const size_t size = deviceInfo->mOperations->size(); |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 253 | ScopedLocalRef<jintArray> operations(env, static_cast<jintArray>(env->NewIntArray(size))); |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 254 | { |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 255 | ScopedIntArrayRW elements(env, operations.get()); |
| 256 | if (elements.get() == NULL) { |
| 257 | ALOGE("Could not create operationsSupported element."); |
| 258 | return NULL; |
| 259 | } |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 260 | for (size_t i = 0; i < size; ++i) { |
Jerry Zhang | d9f3005 | 2018-03-27 15:29:09 -0700 | [diff] [blame] | 261 | elements[i] = static_cast<int>(deviceInfo->mOperations->at(i)); |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 262 | } |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 263 | env->SetObjectField(info, field_deviceInfo_operationsSupported, operations.get()); |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 264 | } |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 265 | } |
Daichi Hirono | 399df70 | 2016-04-13 11:07:44 +0900 | [diff] [blame] | 266 | assert(deviceInfo->mEvents); |
| 267 | { |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 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) { |
Jerry Zhang | d9f3005 | 2018-03-27 15:29:09 -0700 | [diff] [blame] | 277 | elements[i] = static_cast<int>(deviceInfo->mEvents->at(i)); |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 278 | } |
| 279 | env->SetObjectField(info, field_deviceInfo_eventsSupported, events.get()); |
| 280 | } |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 281 | } |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 282 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 283 | return info; |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static jintArray |
| 287 | android_mtp_MtpDevice_get_storage_ids(JNIEnv *env, jobject thiz) |
| 288 | { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 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? |
Jerry Zhang | d9f3005 | 2018-03-27 15:29:09 -0700 | [diff] [blame] | 299 | env->SetIntArrayRegion(array, 0, length, (const jint *)storageIDs->data()); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 300 | |
| 301 | delete storageIDs; |
| 302 | return array; |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static jobject |
| 306 | android_mtp_MtpDevice_get_storage_info(JNIEnv *env, jobject thiz, jint storageID) |
| 307 | { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 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) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 317 | ALOGE("Could not create a MtpStorageInfo object"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 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; |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static jintArray |
| 340 | android_mtp_MtpDevice_get_object_handles(JNIEnv *env, jobject thiz, |
| 341 | jint storageID, jint format, jint objectID) |
| 342 | { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 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? |
Jerry Zhang | d9f3005 | 2018-03-27 15:29:09 -0700 | [diff] [blame] | 353 | env->SetIntArrayRegion(array, 0, length, (const jint *)handles->data()); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 354 | |
| 355 | delete handles; |
| 356 | return array; |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static jobject |
| 360 | android_mtp_MtpDevice_get_object_info(JNIEnv *env, jobject thiz, jint objectID) |
| 361 | { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 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) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 370 | ALOGE("Could not create a MtpObjectInfo object"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 371 | delete objectInfo; |
| 372 | return NULL; |
| 373 | } |
| 374 | |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 375 | fill_jobject_from_object_info(env, info, objectInfo); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 376 | delete objectInfo; |
| 377 | return info; |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 378 | } |
| 379 | |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 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 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 392 | static jbyteArray |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 393 | android_mtp_MtpDevice_get_object(JNIEnv *env, jobject thiz, jint objectID, jlong objectSizeLong) |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 394 | { |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 395 | uint32_t objectSize; |
| 396 | if (!check_uint32_arg(env, "objectSize", objectSizeLong, &objectSize)) { |
| 397 | return nullptr; |
| 398 | } |
| 399 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 400 | MtpDevice* device = get_device_from_object(env, thiz); |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 401 | if (!device) { |
| 402 | return nullptr; |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 403 | } |
| 404 | |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 405 | ScopedLocalRef<jbyteArray> array(env, env->NewByteArray(objectSize)); |
| 406 | if (!array.get()) { |
| 407 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 408 | return nullptr; |
| 409 | } |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 410 | |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 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 | |
Daichi Hirono | 2dd4825 | 2016-01-12 15:46:41 +0900 | [diff] [blame] | 419 | static jlong |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 420 | android_mtp_MtpDevice_get_partial_object(JNIEnv *env, |
| 421 | jobject thiz, |
| 422 | jint objectID, |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 423 | jlong offsetLong, |
| 424 | jlong sizeLong, |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 425 | jbyteArray array) |
| 426 | { |
Daichi Hirono | 3ad1a8ee | 2016-01-07 13:06:11 +0900 | [diff] [blame] | 427 | if (!array) { |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 428 | jniThrowException(env, "java/lang/IllegalArgumentException", "Array must not be null."); |
| 429 | return -1; |
| 430 | } |
| 431 | |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 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)) { |
Daichi Hirono | 2dd4825 | 2016-01-12 15:46:41 +0900 | [diff] [blame] | 436 | return -1; |
| 437 | } |
| 438 | |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 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); |
Andreas Gampe | 9c180dd | 2016-01-06 21:15:09 -0800 | [diff] [blame] | 446 | uint32_t written_size; |
Daichi Hirono | 2dd4825 | 2016-01-12 15:46:41 +0900 | [diff] [blame] | 447 | const bool success = device->readPartialObject( |
Andreas Gampe | 9c180dd | 2016-01-06 21:15:09 -0800 | [diff] [blame] | 448 | objectID, offset, size, &written_size, JavaArrayWriter::writeTo, &writer); |
| 449 | if (!success) { |
Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 450 | jniThrowException(env, "java/io/IOException", "Failed to read data."); |
| 451 | return -1; |
| 452 | } |
Daichi Hirono | 2dd4825 | 2016-01-12 15:46:41 +0900 | [diff] [blame] | 453 | return static_cast<jlong>(written_size); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 454 | } |
| 455 | |
Daichi Hirono | 038832b | 2016-01-22 19:34:25 +0900 | [diff] [blame] | 456 | static jint |
| 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 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 510 | static jbyteArray |
| 511 | android_mtp_MtpDevice_get_thumbnail(JNIEnv *env, jobject thiz, jint objectID) |
| 512 | { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 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; |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static jboolean |
| 529 | android_mtp_MtpDevice_delete_object(JNIEnv *env, jobject thiz, jint object_id) |
| 530 | { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 531 | MtpDevice* device = get_device_from_object(env, thiz); |
Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 532 | if (device && device->deleteObject(object_id)) { |
| 533 | return JNI_TRUE; |
| 534 | } else { |
| 535 | return JNI_FALSE; |
| 536 | } |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 537 | } |
| 538 | |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 539 | static jint |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 540 | android_mtp_MtpDevice_get_parent(JNIEnv *env, jobject thiz, jint object_id) |
| 541 | { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 542 | MtpDevice* device = get_device_from_object(env, thiz); |
| 543 | if (device) |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 544 | return static_cast<jint>(device->getParent(object_id)); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 545 | else |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 546 | return -1; |
| 547 | } |
| 548 | |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 549 | static jint |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 550 | android_mtp_MtpDevice_get_storage_id(JNIEnv *env, jobject thiz, jint object_id) |
| 551 | { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 552 | MtpDevice* device = get_device_from_object(env, thiz); |
| 553 | if (device) |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 554 | return static_cast<jint>(device->getStorageID(object_id)); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 555 | else |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 556 | return -1; |
| 557 | } |
| 558 | |
| 559 | static jboolean |
| 560 | android_mtp_MtpDevice_import_file(JNIEnv *env, jobject thiz, jint object_id, jstring dest_path) |
| 561 | { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 562 | MtpDevice* device = get_device_from_object(env, thiz); |
| 563 | if (device) { |
| 564 | const char *destPathStr = env->GetStringUTFChars(dest_path, NULL); |
James Dong | 3977472 | 2011-04-06 11:57:48 -0700 | [diff] [blame] | 565 | if (destPathStr == NULL) { |
Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 566 | return JNI_FALSE; |
James Dong | 3977472 | 2011-04-06 11:57:48 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 569 | jboolean result = device->readObject(object_id, destPathStr, AID_SDCARD_RW, 0664); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 570 | env->ReleaseStringUTFChars(dest_path, destPathStr); |
| 571 | return result; |
| 572 | } |
Mike Lockwood | c1b9bbb | 2011-07-13 11:06:57 -0400 | [diff] [blame] | 573 | |
Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 574 | return JNI_FALSE; |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 575 | } |
| 576 | |
Tomasz Mikolajewski | 74d4ff8 | 2015-08-04 18:34:03 +0900 | [diff] [blame] | 577 | static jboolean |
| 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 | |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 587 | static jboolean |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 588 | android_mtp_MtpDevice_send_object( |
| 589 | JNIEnv *env, jobject thiz, jint object_id, jlong sizeLong, jint fd) |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 590 | { |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 591 | uint32_t size; |
| 592 | if (!check_uint32_arg(env, "size", sizeLong, &size)) |
| 593 | return JNI_FALSE; |
| 594 | |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 595 | MtpDevice* device = get_device_from_object(env, thiz); |
| 596 | if (!device) |
| 597 | return JNI_FALSE; |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 598 | |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 599 | return device->sendObject(object_id, size, fd); |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static jobject |
| 603 | android_mtp_MtpDevice_send_object_info(JNIEnv *env, jobject thiz, jobject info) |
| 604 | { |
| 605 | MtpDevice* device = get_device_from_object(env, thiz); |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 606 | if (!device) { |
Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame] | 607 | return NULL; |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 608 | } |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 609 | |
| 610 | // Updating existing objects is not supported. |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 611 | if (env->GetIntField(info, field_objectInfo_handle) != -1) { |
Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame] | 612 | return NULL; |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 613 | } |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 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); |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 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 | } |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 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); |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 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 | } |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 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 | |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 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 | |
| 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 | } |
Daichi Hirono | 2a9a433 | 2016-01-11 13:33:41 +0900 | [diff] [blame] | 686 | uint32_t parameters[3]; |
| 687 | const int eventCode = device->reapEventRequest(seq, ¶meters); |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 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); |
Daichi Hirono | 2a9a433 | 2016-01-11 13:33:41 +0900 | [diff] [blame] | 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])); |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 697 | return result; |
| 698 | } |
| 699 | |
| 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 | |
Daichi Hirono | 1337deb | 2016-03-28 08:53:15 +0900 | [diff] [blame] | 709 | // Returns object size in 64-bit integer. If the MTP device does not support the property, it |
| 710 | // throws IOException. |
Daichi Hirono | 787821b | 2016-03-24 21:05:51 +0900 | [diff] [blame] | 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 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 745 | // ---------------------------------------------------------------------------- |
| 746 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 747 | static const JNINativeMethod gMethods[] = { |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 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}, |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 760 | {"native_get_object", "(IJ)[B",(void *)android_mtp_MtpDevice_get_object}, |
Daichi Hirono | 2dd4825 | 2016-01-12 15:46:41 +0900 | [diff] [blame] | 761 | {"native_get_partial_object", "(IJJ[B)J", (void *)android_mtp_MtpDevice_get_partial_object}, |
Daichi Hirono | 038832b | 2016-01-22 19:34:25 +0900 | [diff] [blame] | 762 | {"native_get_partial_object_64", "(IJJ[B)I", |
| 763 | (void *)android_mtp_MtpDevice_get_partial_object_64}, |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 764 | {"native_get_thumbnail", "(I)[B",(void *)android_mtp_MtpDevice_get_thumbnail}, |
| 765 | {"native_delete_object", "(I)Z", (void *)android_mtp_MtpDevice_delete_object}, |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 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}, |
Tomasz Mikolajewski | 74d4ff8 | 2015-08-04 18:34:03 +0900 | [diff] [blame] | 768 | {"native_import_file", "(ILjava/lang/String;)Z", |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 769 | (void *)android_mtp_MtpDevice_import_file}, |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 770 | {"native_import_file", "(II)Z",(void *)android_mtp_MtpDevice_import_file_to_fd}, |
Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 771 | {"native_send_object", "(IJI)Z",(void *)android_mtp_MtpDevice_send_object}, |
Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 772 | {"native_send_object_info", "(Landroid/mtp/MtpObjectInfo;)Landroid/mtp/MtpObjectInfo;", |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 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}, |
Daichi Hirono | 787821b | 2016-03-24 21:05:51 +0900 | [diff] [blame] | 778 | |
| 779 | {"native_get_object_size_long", "(II)J", (void *)android_mtp_MtpDevice_get_object_size_long}, |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 780 | }; |
| 781 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 782 | int register_android_mtp_MtpDevice(JNIEnv *env) |
| 783 | { |
| 784 | jclass clazz; |
| 785 | |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 786 | ALOGD("register_android_mtp_MtpDevice\n"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 787 | |
| 788 | clazz = env->FindClass("android/mtp/MtpDeviceInfo"); |
| 789 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 790 | ALOGE("Can't find android/mtp/MtpDeviceInfo"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 791 | return -1; |
| 792 | } |
| 793 | constructor_deviceInfo = env->GetMethodID(clazz, "<init>", "()V"); |
| 794 | if (constructor_deviceInfo == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 795 | ALOGE("Can't find android/mtp/MtpDeviceInfo constructor"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 796 | return -1; |
| 797 | } |
| 798 | field_deviceInfo_manufacturer = env->GetFieldID(clazz, "mManufacturer", "Ljava/lang/String;"); |
| 799 | if (field_deviceInfo_manufacturer == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 800 | ALOGE("Can't find MtpDeviceInfo.mManufacturer"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 801 | return -1; |
| 802 | } |
| 803 | field_deviceInfo_model = env->GetFieldID(clazz, "mModel", "Ljava/lang/String;"); |
| 804 | if (field_deviceInfo_model == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 805 | ALOGE("Can't find MtpDeviceInfo.mModel"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 806 | return -1; |
| 807 | } |
| 808 | field_deviceInfo_version = env->GetFieldID(clazz, "mVersion", "Ljava/lang/String;"); |
| 809 | if (field_deviceInfo_version == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 810 | ALOGE("Can't find MtpDeviceInfo.mVersion"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 811 | return -1; |
| 812 | } |
| 813 | field_deviceInfo_serialNumber = env->GetFieldID(clazz, "mSerialNumber", "Ljava/lang/String;"); |
| 814 | if (field_deviceInfo_serialNumber == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 815 | ALOGE("Can't find MtpDeviceInfo.mSerialNumber"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 816 | return -1; |
| 817 | } |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 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 | } |
Daichi Hirono | 148954a | 2016-01-21 19:55:45 +0900 | [diff] [blame] | 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 | } |
Mike Lockwood | 40304e2 | 2011-02-11 08:19:11 -0500 | [diff] [blame] | 828 | clazz_deviceInfo = (jclass)env->NewGlobalRef(clazz); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 829 | |
| 830 | clazz = env->FindClass("android/mtp/MtpStorageInfo"); |
| 831 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 832 | ALOGE("Can't find android/mtp/MtpStorageInfo"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 833 | return -1; |
| 834 | } |
| 835 | constructor_storageInfo = env->GetMethodID(clazz, "<init>", "()V"); |
| 836 | if (constructor_storageInfo == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 837 | ALOGE("Can't find android/mtp/MtpStorageInfo constructor"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 838 | return -1; |
| 839 | } |
| 840 | field_storageInfo_storageId = env->GetFieldID(clazz, "mStorageId", "I"); |
| 841 | if (field_storageInfo_storageId == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 842 | ALOGE("Can't find MtpStorageInfo.mStorageId"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 843 | return -1; |
| 844 | } |
| 845 | field_storageInfo_maxCapacity = env->GetFieldID(clazz, "mMaxCapacity", "J"); |
| 846 | if (field_storageInfo_maxCapacity == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 847 | ALOGE("Can't find MtpStorageInfo.mMaxCapacity"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 848 | return -1; |
| 849 | } |
| 850 | field_storageInfo_freeSpace = env->GetFieldID(clazz, "mFreeSpace", "J"); |
| 851 | if (field_storageInfo_freeSpace == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 852 | ALOGE("Can't find MtpStorageInfo.mFreeSpace"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 853 | return -1; |
| 854 | } |
| 855 | field_storageInfo_description = env->GetFieldID(clazz, "mDescription", "Ljava/lang/String;"); |
| 856 | if (field_storageInfo_description == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 857 | ALOGE("Can't find MtpStorageInfo.mDescription"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 858 | return -1; |
| 859 | } |
| 860 | field_storageInfo_volumeIdentifier = env->GetFieldID(clazz, "mVolumeIdentifier", "Ljava/lang/String;"); |
| 861 | if (field_storageInfo_volumeIdentifier == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 862 | ALOGE("Can't find MtpStorageInfo.mVolumeIdentifier"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 863 | return -1; |
| 864 | } |
Mike Lockwood | 40304e2 | 2011-02-11 08:19:11 -0500 | [diff] [blame] | 865 | clazz_storageInfo = (jclass)env->NewGlobalRef(clazz); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 866 | |
| 867 | clazz = env->FindClass("android/mtp/MtpObjectInfo"); |
| 868 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 869 | ALOGE("Can't find android/mtp/MtpObjectInfo"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 870 | return -1; |
| 871 | } |
| 872 | constructor_objectInfo = env->GetMethodID(clazz, "<init>", "()V"); |
| 873 | if (constructor_objectInfo == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 874 | ALOGE("Can't find android/mtp/MtpObjectInfo constructor"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 875 | return -1; |
| 876 | } |
| 877 | field_objectInfo_handle = env->GetFieldID(clazz, "mHandle", "I"); |
| 878 | if (field_objectInfo_handle == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 879 | ALOGE("Can't find MtpObjectInfo.mHandle"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 880 | return -1; |
| 881 | } |
| 882 | field_objectInfo_storageId = env->GetFieldID(clazz, "mStorageId", "I"); |
| 883 | if (field_objectInfo_storageId == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 884 | ALOGE("Can't find MtpObjectInfo.mStorageId"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 885 | return -1; |
| 886 | } |
| 887 | field_objectInfo_format = env->GetFieldID(clazz, "mFormat", "I"); |
| 888 | if (field_objectInfo_format == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 889 | ALOGE("Can't find MtpObjectInfo.mFormat"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 890 | return -1; |
| 891 | } |
| 892 | field_objectInfo_protectionStatus = env->GetFieldID(clazz, "mProtectionStatus", "I"); |
| 893 | if (field_objectInfo_protectionStatus == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 894 | ALOGE("Can't find MtpObjectInfo.mProtectionStatus"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 895 | return -1; |
| 896 | } |
| 897 | field_objectInfo_compressedSize = env->GetFieldID(clazz, "mCompressedSize", "I"); |
| 898 | if (field_objectInfo_compressedSize == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 899 | ALOGE("Can't find MtpObjectInfo.mCompressedSize"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 900 | return -1; |
| 901 | } |
| 902 | field_objectInfo_thumbFormat = env->GetFieldID(clazz, "mThumbFormat", "I"); |
| 903 | if (field_objectInfo_thumbFormat == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 904 | ALOGE("Can't find MtpObjectInfo.mThumbFormat"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 905 | return -1; |
| 906 | } |
| 907 | field_objectInfo_thumbCompressedSize = env->GetFieldID(clazz, "mThumbCompressedSize", "I"); |
| 908 | if (field_objectInfo_thumbCompressedSize == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 909 | ALOGE("Can't find MtpObjectInfo.mThumbCompressedSize"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 910 | return -1; |
| 911 | } |
| 912 | field_objectInfo_thumbPixWidth = env->GetFieldID(clazz, "mThumbPixWidth", "I"); |
| 913 | if (field_objectInfo_thumbPixWidth == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 914 | ALOGE("Can't find MtpObjectInfo.mThumbPixWidth"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 915 | return -1; |
| 916 | } |
| 917 | field_objectInfo_thumbPixHeight = env->GetFieldID(clazz, "mThumbPixHeight", "I"); |
| 918 | if (field_objectInfo_thumbPixHeight == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 919 | ALOGE("Can't find MtpObjectInfo.mThumbPixHeight"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 920 | return -1; |
| 921 | } |
| 922 | field_objectInfo_imagePixWidth = env->GetFieldID(clazz, "mImagePixWidth", "I"); |
| 923 | if (field_objectInfo_imagePixWidth == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 924 | ALOGE("Can't find MtpObjectInfo.mImagePixWidth"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 925 | return -1; |
| 926 | } |
| 927 | field_objectInfo_imagePixHeight = env->GetFieldID(clazz, "mImagePixHeight", "I"); |
| 928 | if (field_objectInfo_imagePixHeight == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 929 | ALOGE("Can't find MtpObjectInfo.mImagePixHeight"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 930 | return -1; |
| 931 | } |
| 932 | field_objectInfo_imagePixDepth = env->GetFieldID(clazz, "mImagePixDepth", "I"); |
| 933 | if (field_objectInfo_imagePixDepth == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 934 | ALOGE("Can't find MtpObjectInfo.mImagePixDepth"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 935 | return -1; |
| 936 | } |
| 937 | field_objectInfo_parent = env->GetFieldID(clazz, "mParent", "I"); |
| 938 | if (field_objectInfo_parent == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 939 | ALOGE("Can't find MtpObjectInfo.mParent"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 940 | return -1; |
| 941 | } |
| 942 | field_objectInfo_associationType = env->GetFieldID(clazz, "mAssociationType", "I"); |
| 943 | if (field_objectInfo_associationType == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 944 | ALOGE("Can't find MtpObjectInfo.mAssociationType"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 945 | return -1; |
| 946 | } |
| 947 | field_objectInfo_associationDesc = env->GetFieldID(clazz, "mAssociationDesc", "I"); |
| 948 | if (field_objectInfo_associationDesc == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 949 | ALOGE("Can't find MtpObjectInfo.mAssociationDesc"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 950 | return -1; |
| 951 | } |
| 952 | field_objectInfo_sequenceNumber = env->GetFieldID(clazz, "mSequenceNumber", "I"); |
| 953 | if (field_objectInfo_sequenceNumber == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 954 | ALOGE("Can't find MtpObjectInfo.mSequenceNumber"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 955 | return -1; |
| 956 | } |
| 957 | field_objectInfo_name = env->GetFieldID(clazz, "mName", "Ljava/lang/String;"); |
| 958 | if (field_objectInfo_name == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 959 | ALOGE("Can't find MtpObjectInfo.mName"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 960 | return -1; |
| 961 | } |
| 962 | field_objectInfo_dateCreated = env->GetFieldID(clazz, "mDateCreated", "J"); |
| 963 | if (field_objectInfo_dateCreated == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 964 | ALOGE("Can't find MtpObjectInfo.mDateCreated"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 965 | return -1; |
| 966 | } |
| 967 | field_objectInfo_dateModified = env->GetFieldID(clazz, "mDateModified", "J"); |
| 968 | if (field_objectInfo_dateModified == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 969 | ALOGE("Can't find MtpObjectInfo.mDateModified"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 970 | return -1; |
| 971 | } |
| 972 | field_objectInfo_keywords = env->GetFieldID(clazz, "mKeywords", "Ljava/lang/String;"); |
| 973 | if (field_objectInfo_keywords == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 974 | ALOGE("Can't find MtpObjectInfo.mKeywords"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 975 | return -1; |
| 976 | } |
Mike Lockwood | 40304e2 | 2011-02-11 08:19:11 -0500 | [diff] [blame] | 977 | clazz_objectInfo = (jclass)env->NewGlobalRef(clazz); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 978 | |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 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 | } |
Daichi Hirono | 2a9a433 | 2016-01-11 13:33:41 +0900 | [diff] [blame] | 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 | } |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 1009 | clazz_event = (jclass)env->NewGlobalRef(clazz); |
| 1010 | |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 1011 | clazz = env->FindClass("android/mtp/MtpDevice"); |
| 1012 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1013 | ALOGE("Can't find android/mtp/MtpDevice"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 1014 | return -1; |
| 1015 | } |
Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 1016 | field_context = env->GetFieldID(clazz, "mNativeContext", "J"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 1017 | if (field_context == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1018 | ALOGE("Can't find MtpDevice.mNativeContext"); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 1019 | return -1; |
| 1020 | } |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 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); |
Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 1033 | |
| 1034 | return AndroidRuntime::registerNativeMethods(env, |
| 1035 | "android/mtp/MtpDevice", gMethods, NELEM(gMethods)); |
| 1036 | } |