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