Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [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_TAG "MtpClientJNI" |
| 18 | #include "utils/Log.h" |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <assert.h> |
| 22 | #include <limits.h> |
| 23 | #include <unistd.h> |
| 24 | #include <fcntl.h> |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 25 | |
| 26 | #include "jni.h" |
| 27 | #include "JNIHelp.h" |
| 28 | #include "android_runtime/AndroidRuntime.h" |
| 29 | |
| 30 | #include "MtpClient.h" |
| 31 | #include "MtpDevice.h" |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 32 | #include "MtpObjectInfo.h" |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 33 | |
Mike Lockwood | 81ea83d | 2010-06-30 17:49:41 -0400 | [diff] [blame] | 34 | using namespace android; |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
| 38 | static jmethodID method_deviceAdded; |
| 39 | static jmethodID method_deviceRemoved; |
| 40 | static jfieldID field_context; |
| 41 | |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 42 | static struct file_descriptor_offsets_t |
| 43 | { |
| 44 | jclass mClass; |
| 45 | jmethodID mConstructor; |
| 46 | jfieldID mDescriptor; |
| 47 | } gFileDescriptorOffsets; |
| 48 | |
| 49 | static struct parcel_file_descriptor_offsets_t |
| 50 | { |
| 51 | jclass mClass; |
| 52 | jmethodID mConstructor; |
| 53 | } gParcelFileDescriptorOffsets; |
| 54 | |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 55 | #ifdef HAVE_ANDROID_OS |
| 56 | |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 57 | static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 58 | if (env->ExceptionCheck()) { |
| 59 | LOGE("An exception was thrown by callback '%s'.", methodName); |
| 60 | LOGE_EX(env); |
| 61 | env->ExceptionClear(); |
| 62 | } |
| 63 | } |
| 64 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 65 | class MyClient : public MtpClient { |
| 66 | private: |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 67 | virtual void deviceAdded(MtpDevice *device); |
| 68 | virtual void deviceRemoved(MtpDevice *device); |
| 69 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 70 | jobject mClient; |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 71 | MtpDevice* mEventDevice; |
| 72 | |
| 73 | public: |
| 74 | MyClient(JNIEnv *env, jobject client); |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 75 | void cleanup(JNIEnv *env); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | MtpClient* get_client_from_object(JNIEnv* env, jobject javaClient) |
| 79 | { |
| 80 | return (MtpClient*)env->GetIntField(javaClient, field_context); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | MyClient::MyClient(JNIEnv *env, jobject client) |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 85 | : mClient(env->NewGlobalRef(client)) |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 86 | { |
| 87 | } |
| 88 | |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 89 | void MyClient::cleanup(JNIEnv *env) { |
| 90 | env->DeleteGlobalRef(mClient); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 93 | void MyClient::deviceAdded(MtpDevice *device) { |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 94 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 95 | const char* name = device->getDeviceName(); |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 96 | LOGD("MyClient::deviceAdded %s\n", name); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 97 | |
| 98 | env->CallVoidMethod(mClient, method_deviceAdded, device->getID()); |
| 99 | |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 100 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 103 | void MyClient::deviceRemoved(MtpDevice *device) { |
| 104 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 105 | const char* name = device->getDeviceName(); |
| 106 | LOGD("MyClient::deviceRemoved %s\n", name); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 107 | |
| 108 | env->CallVoidMethod(mClient, method_deviceRemoved, device->getID()); |
| 109 | |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 110 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 113 | #endif // HAVE_ANDROID_OS |
| 114 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 115 | // ---------------------------------------------------------------------------- |
| 116 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 117 | static void |
| 118 | android_media_MtpClient_setup(JNIEnv *env, jobject thiz) |
| 119 | { |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 120 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 121 | LOGD("setup\n"); |
| 122 | MyClient* client = new MyClient(env, thiz); |
| 123 | client->start(); |
| 124 | env->SetIntField(thiz, field_context, (int)client); |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 125 | #endif |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static void |
| 129 | android_media_MtpClient_finalize(JNIEnv *env, jobject thiz) |
| 130 | { |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 131 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 132 | LOGD("finalize\n"); |
| 133 | MyClient *client = (MyClient *)env->GetIntField(thiz, field_context); |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 134 | client->cleanup(env); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 135 | delete client; |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 136 | env->SetIntField(thiz, field_context, 0); |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 137 | #endif |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static jboolean |
| 141 | android_media_MtpClient_start(JNIEnv *env, jobject thiz) |
| 142 | { |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 143 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 144 | LOGD("start\n"); |
| 145 | MyClient *client = (MyClient *)env->GetIntField(thiz, field_context); |
| 146 | return client->start(); |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 147 | #else |
| 148 | return false; |
| 149 | #endif |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static void |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 153 | android_media_MtpClient_stop(JNIEnv *env, jobject thiz) |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 154 | { |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 155 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 156 | LOGD("stop\n"); |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 157 | MyClient *client = (MyClient *)env->GetIntField(thiz, field_context); |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 158 | client->stop(); |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 159 | #endif |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Mike Lockwood | e0a89f6 | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 162 | static jboolean |
| 163 | android_media_MtpClient_delete_object(JNIEnv *env, jobject thiz, |
| 164 | jint device_id, jint object_id) |
| 165 | { |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 166 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | e0a89f6 | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 167 | MyClient *client = (MyClient *)env->GetIntField(thiz, field_context); |
| 168 | MtpDevice* device = client->getDevice(device_id); |
| 169 | if (device) |
| 170 | return device->deleteObject(object_id); |
| 171 | else |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 172 | #endif |
Mike Lockwood | e0a89f6 | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | static jint |
| 177 | android_media_MtpClient_get_parent(JNIEnv *env, jobject thiz, |
| 178 | jint device_id, jint object_id) |
| 179 | { |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 180 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | e0a89f6 | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 181 | MyClient *client = (MyClient *)env->GetIntField(thiz, field_context); |
| 182 | MtpDevice* device = client->getDevice(device_id); |
| 183 | if (device) |
| 184 | return device->getParent(object_id); |
| 185 | else |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 186 | #endif |
Mike Lockwood | e0a89f6 | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | static jint |
| 191 | android_media_MtpClient_get_storage_id(JNIEnv *env, jobject thiz, |
| 192 | jint device_id, jint object_id) |
| 193 | { |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 194 | #ifdef HAVE_ANDROID_OS |
Mike Lockwood | e0a89f6 | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 195 | MyClient *client = (MyClient *)env->GetIntField(thiz, field_context); |
| 196 | MtpDevice* device = client->getDevice(device_id); |
| 197 | if (device) |
| 198 | return device->getStorageID(object_id); |
| 199 | else |
Mike Lockwood | ff164a7 | 2010-07-15 15:01:17 -0400 | [diff] [blame] | 200 | #endif |
Mike Lockwood | e0a89f6 | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 201 | return -1; |
| 202 | } |
| 203 | |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 204 | static jobject |
| 205 | android_media_MtpClient_open_file(JNIEnv *env, jobject thiz, |
| 206 | jint device_id, jint object_id) |
| 207 | { |
| 208 | #ifdef HAVE_ANDROID_OS |
| 209 | MyClient *client = (MyClient *)env->GetIntField(thiz, field_context); |
| 210 | MtpDevice* device = client->getDevice(device_id); |
| 211 | if (!device) |
| 212 | return NULL; |
| 213 | |
| 214 | MtpObjectInfo* info = device->getObjectInfo(object_id); |
| 215 | if (!info) |
| 216 | return NULL; |
| 217 | int object_size = info->mCompressedSize; |
| 218 | delete info; |
| 219 | int fd = device->readObject(object_id, object_size); |
| 220 | if (fd < 0) |
| 221 | return NULL; |
| 222 | |
| 223 | jobject fileDescriptor = env->NewObject(gFileDescriptorOffsets.mClass, |
| 224 | gFileDescriptorOffsets.mConstructor); |
| 225 | if (fileDescriptor != NULL) { |
| 226 | env->SetIntField(fileDescriptor, gFileDescriptorOffsets.mDescriptor, fd); |
| 227 | } else { |
| 228 | return NULL; |
| 229 | } |
| 230 | return env->NewObject(gParcelFileDescriptorOffsets.mClass, |
| 231 | gParcelFileDescriptorOffsets.mConstructor, fileDescriptor); |
| 232 | #endif |
| 233 | return NULL; |
| 234 | } |
| 235 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 236 | // ---------------------------------------------------------------------------- |
| 237 | |
| 238 | static JNINativeMethod gMethods[] = { |
| 239 | {"native_setup", "()V", (void *)android_media_MtpClient_setup}, |
| 240 | {"native_finalize", "()V", (void *)android_media_MtpClient_finalize}, |
Mike Lockwood | d0e1a9f | 2010-07-01 11:32:08 -0400 | [diff] [blame] | 241 | {"native_start", "()Z", (void *)android_media_MtpClient_start}, |
| 242 | {"native_stop", "()V", (void *)android_media_MtpClient_stop}, |
Mike Lockwood | e0a89f6 | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 243 | {"native_delete_object", "(II)Z", (void *)android_media_MtpClient_delete_object}, |
| 244 | {"native_get_parent", "(II)I", (void *)android_media_MtpClient_get_parent}, |
| 245 | {"native_get_storage_id", "(II)I", (void *)android_media_MtpClient_get_storage_id}, |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 246 | {"native_open_file", "(II)Landroid/os/ParcelFileDescriptor;", |
| 247 | (void *)android_media_MtpClient_open_file}, |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | static const char* const kClassPathName = "android/media/MtpClient"; |
| 251 | |
| 252 | int register_android_media_MtpClient(JNIEnv *env) |
| 253 | { |
| 254 | jclass clazz; |
| 255 | |
| 256 | LOGD("register_android_media_MtpClient\n"); |
| 257 | |
| 258 | clazz = env->FindClass("android/media/MtpClient"); |
| 259 | if (clazz == NULL) { |
| 260 | LOGE("Can't find android/media/MtpClient"); |
| 261 | return -1; |
| 262 | } |
| 263 | method_deviceAdded = env->GetMethodID(clazz, "deviceAdded", "(I)V"); |
| 264 | if (method_deviceAdded == NULL) { |
| 265 | LOGE("Can't find deviceAdded"); |
| 266 | return -1; |
| 267 | } |
| 268 | method_deviceRemoved = env->GetMethodID(clazz, "deviceRemoved", "(I)V"); |
| 269 | if (method_deviceRemoved == NULL) { |
| 270 | LOGE("Can't find deviceRemoved"); |
| 271 | return -1; |
| 272 | } |
| 273 | field_context = env->GetFieldID(clazz, "mNativeContext", "I"); |
| 274 | if (field_context == NULL) { |
| 275 | LOGE("Can't find MtpClient.mNativeContext"); |
| 276 | return -1; |
| 277 | } |
| 278 | |
Mike Lockwood | bc4cb0b | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 279 | clazz = env->FindClass("java/io/FileDescriptor"); |
| 280 | LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor"); |
| 281 | gFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz); |
| 282 | gFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "()V"); |
| 283 | gFileDescriptorOffsets.mDescriptor = env->GetFieldID(clazz, "descriptor", "I"); |
| 284 | LOG_FATAL_IF(gFileDescriptorOffsets.mDescriptor == NULL, |
| 285 | "Unable to find descriptor field in java.io.FileDescriptor"); |
| 286 | |
| 287 | clazz = env->FindClass("android/os/ParcelFileDescriptor"); |
| 288 | LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor"); |
| 289 | gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz); |
| 290 | gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V"); |
| 291 | LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL, |
| 292 | "Unable to find constructor for android.os.ParcelFileDescriptor"); |
| 293 | |
Mike Lockwood | 755fd61 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 294 | return AndroidRuntime::registerNativeMethods(env, |
| 295 | "android/media/MtpClient", gMethods, NELEM(gMethods)); |
| 296 | } |