The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 1 | /* //device/libs/media_jni/MediaScanner.cpp |
| 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "MediaScanner" |
| 19 | #include "utils/Log.h" |
| 20 | |
| 21 | #include <media/mediascanner.h> |
| 22 | #include <stdio.h> |
| 23 | #include <assert.h> |
| 24 | #include <limits.h> |
| 25 | #include <unistd.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <utils/threads.h> |
| 28 | |
| 29 | #include "jni.h" |
| 30 | #include "JNIHelp.h" |
| 31 | #include "android_runtime/AndroidRuntime.h" |
| 32 | |
| 33 | |
| 34 | // ---------------------------------------------------------------------------- |
| 35 | |
| 36 | using namespace android; |
| 37 | |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | |
| 40 | struct fields_t { |
| 41 | jfieldID context; |
| 42 | }; |
| 43 | static fields_t fields; |
| 44 | |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | |
| 47 | class MyMediaScannerClient : public MediaScannerClient |
| 48 | { |
| 49 | public: |
| 50 | MyMediaScannerClient(JNIEnv *env, jobject client) |
| 51 | : mEnv(env), |
| 52 | mClient(env->NewGlobalRef(client)), |
| 53 | mScanFileMethodID(0), |
| 54 | mHandleStringTagMethodID(0), |
| 55 | mSetMimeTypeMethodID(0) |
| 56 | { |
| 57 | jclass mediaScannerClientInterface = env->FindClass("android/media/MediaScannerClient"); |
| 58 | if (mediaScannerClientInterface == NULL) { |
| 59 | fprintf(stderr, "android/media/MediaScannerClient not found\n"); |
| 60 | } |
| 61 | else { |
| 62 | mScanFileMethodID = env->GetMethodID(mediaScannerClientInterface, "scanFile", |
| 63 | "(Ljava/lang/String;JJ)V"); |
| 64 | mHandleStringTagMethodID = env->GetMethodID(mediaScannerClientInterface, "handleStringTag", |
| 65 | "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 66 | mSetMimeTypeMethodID = env->GetMethodID(mediaScannerClientInterface, "setMimeType", |
| 67 | "(Ljava/lang/String;)V"); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | virtual ~MyMediaScannerClient() |
| 72 | { |
| 73 | mEnv->DeleteGlobalRef(mClient); |
| 74 | } |
| 75 | |
| 76 | // returns true if it succeeded, false if an exception occured in the Java code |
| 77 | virtual bool scanFile(const char* path, long long lastModified, long long fileSize) |
| 78 | { |
| 79 | jstring pathStr; |
| 80 | if ((pathStr = mEnv->NewStringUTF(path)) == NULL) return false; |
| 81 | |
| 82 | mEnv->CallVoidMethod(mClient, mScanFileMethodID, pathStr, lastModified, fileSize); |
| 83 | |
| 84 | mEnv->DeleteLocalRef(pathStr); |
| 85 | return (!mEnv->ExceptionCheck()); |
| 86 | } |
| 87 | |
| 88 | // returns true if it succeeded, false if an exception occured in the Java code |
| 89 | virtual bool handleStringTag(const char* name, const char* value) |
| 90 | { |
| 91 | jstring nameStr, valueStr; |
| 92 | if ((nameStr = mEnv->NewStringUTF(name)) == NULL) return false; |
| 93 | if ((valueStr = mEnv->NewStringUTF(value)) == NULL) return false; |
| 94 | |
| 95 | mEnv->CallVoidMethod(mClient, mHandleStringTagMethodID, nameStr, valueStr); |
| 96 | |
| 97 | mEnv->DeleteLocalRef(nameStr); |
| 98 | mEnv->DeleteLocalRef(valueStr); |
| 99 | return (!mEnv->ExceptionCheck()); |
| 100 | } |
| 101 | |
| 102 | // returns true if it succeeded, false if an exception occured in the Java code |
| 103 | virtual bool setMimeType(const char* mimeType) |
| 104 | { |
| 105 | jstring mimeTypeStr; |
| 106 | if ((mimeTypeStr = mEnv->NewStringUTF(mimeType)) == NULL) return false; |
| 107 | |
| 108 | mEnv->CallVoidMethod(mClient, mSetMimeTypeMethodID, mimeTypeStr); |
| 109 | |
| 110 | mEnv->DeleteLocalRef(mimeTypeStr); |
| 111 | return (!mEnv->ExceptionCheck()); |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | JNIEnv *mEnv; |
| 116 | jobject mClient; |
| 117 | jmethodID mScanFileMethodID; |
| 118 | jmethodID mHandleStringTagMethodID; |
| 119 | jmethodID mSetMimeTypeMethodID; |
| 120 | }; |
| 121 | |
| 122 | |
| 123 | // ---------------------------------------------------------------------------- |
| 124 | |
| 125 | static bool ExceptionCheck(void* env) |
| 126 | { |
| 127 | return ((JNIEnv *)env)->ExceptionCheck(); |
| 128 | } |
| 129 | |
| 130 | static void |
| 131 | android_media_MediaScanner_processDirectory(JNIEnv *env, jobject thiz, jstring path, jstring extensions, jobject client) |
| 132 | { |
| 133 | MediaScanner *mp = (MediaScanner *)env->GetIntField(thiz, fields.context); |
| 134 | |
| 135 | if (path == NULL) { |
| 136 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 137 | return; |
| 138 | } |
| 139 | if (extensions == NULL) { |
| 140 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
| 145 | if (pathStr == NULL) { // Out of memory |
| 146 | jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); |
| 147 | return; |
| 148 | } |
| 149 | const char *extensionsStr = env->GetStringUTFChars(extensions, NULL); |
| 150 | if (extensionsStr == NULL) { // Out of memory |
| 151 | env->ReleaseStringUTFChars(path, pathStr); |
| 152 | jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | MyMediaScannerClient myClient(env, client); |
| 157 | mp->processDirectory(pathStr, extensionsStr, myClient, ExceptionCheck, env); |
| 158 | env->ReleaseStringUTFChars(path, pathStr); |
| 159 | env->ReleaseStringUTFChars(extensions, extensionsStr); |
| 160 | } |
| 161 | |
| 162 | static void |
| 163 | android_media_MediaScanner_processFile(JNIEnv *env, jobject thiz, jstring path, jstring mimeType, jobject client) |
| 164 | { |
| 165 | MediaScanner *mp = (MediaScanner *)env->GetIntField(thiz, fields.context); |
| 166 | |
| 167 | if (path == NULL) { |
| 168 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
| 173 | if (pathStr == NULL) { // Out of memory |
| 174 | jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); |
| 175 | return; |
| 176 | } |
| 177 | const char *mimeTypeStr = (mimeType ? env->GetStringUTFChars(mimeType, NULL) : NULL); |
| 178 | if (mimeType && mimeTypeStr == NULL) { // Out of memory |
| 179 | env->ReleaseStringUTFChars(path, pathStr); |
| 180 | jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | MyMediaScannerClient myClient(env, client); |
| 185 | mp->processFile(pathStr, mimeTypeStr, myClient); |
| 186 | env->ReleaseStringUTFChars(path, pathStr); |
| 187 | if (mimeType) { |
| 188 | env->ReleaseStringUTFChars(mimeType, mimeTypeStr); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | android_media_MediaScanner_setLocale(JNIEnv *env, jobject thiz, jstring locale) |
| 194 | { |
| 195 | MediaScanner *mp = (MediaScanner *)env->GetIntField(thiz, fields.context); |
| 196 | |
| 197 | if (locale == NULL) { |
| 198 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 199 | return; |
| 200 | } |
| 201 | const char *localeStr = env->GetStringUTFChars(locale, NULL); |
| 202 | if (localeStr == NULL) { // Out of memory |
| 203 | jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); |
| 204 | return; |
| 205 | } |
| 206 | mp->setLocale(localeStr); |
| 207 | |
| 208 | env->ReleaseStringUTFChars(locale, localeStr); |
| 209 | } |
| 210 | |
| 211 | static jbyteArray |
| 212 | android_media_MediaScanner_extractAlbumArt(JNIEnv *env, jobject thiz, jobject fileDescriptor) |
| 213 | { |
| 214 | MediaScanner *mp = (MediaScanner *)env->GetIntField(thiz, fields.context); |
| 215 | |
| 216 | if (fileDescriptor == NULL) { |
| 217 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 218 | return NULL; |
| 219 | } |
| 220 | |
| 221 | int fd = getParcelFileDescriptorFD(env, fileDescriptor); |
| 222 | char* data = mp->extractAlbumArt(fd); |
| 223 | if (!data) { |
| 224 | return NULL; |
| 225 | } |
| 226 | long len = *((long*)data); |
| 227 | |
| 228 | jbyteArray array = env->NewByteArray(len); |
| 229 | if (array != NULL) { |
| 230 | jbyte* bytes = env->GetByteArrayElements(array, NULL); |
| 231 | memcpy(bytes, data + 4, len); |
| 232 | env->ReleaseByteArrayElements(array, bytes, 0); |
| 233 | } |
| 234 | |
| 235 | done: |
| 236 | free(data); |
| 237 | // if NewByteArray() returned NULL, an out-of-memory |
| 238 | // exception will have been raised. I just want to |
| 239 | // return null in that case. |
| 240 | env->ExceptionClear(); |
| 241 | return array; |
| 242 | } |
| 243 | |
| 244 | static void |
| 245 | android_media_MediaScanner_native_setup(JNIEnv *env, jobject thiz) |
| 246 | { |
| 247 | MediaScanner *mp = new MediaScanner(); |
| 248 | if (mp == NULL) { |
| 249 | jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | env->SetIntField(thiz, fields.context, (int)mp); |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | android_media_MediaScanner_native_finalize(JNIEnv *env, jobject thiz) |
| 258 | { |
| 259 | MediaScanner *mp = (MediaScanner *)env->GetIntField(thiz, fields.context); |
| 260 | |
| 261 | //printf("##### android_media_MediaScanner_native_finalize: ctx=0x%p\n", ctx); |
| 262 | |
| 263 | if (mp == 0) |
| 264 | return; |
| 265 | |
| 266 | delete mp; |
| 267 | } |
| 268 | |
| 269 | // ---------------------------------------------------------------------------- |
| 270 | |
| 271 | static JNINativeMethod gMethods[] = { |
| 272 | {"processDirectory", "(Ljava/lang/String;Ljava/lang/String;Landroid/media/MediaScannerClient;)V", |
| 273 | (void *)android_media_MediaScanner_processDirectory}, |
| 274 | {"processFile", "(Ljava/lang/String;Ljava/lang/String;Landroid/media/MediaScannerClient;)V", |
| 275 | (void *)android_media_MediaScanner_processFile}, |
| 276 | {"setLocale", "(Ljava/lang/String;)V", (void *)android_media_MediaScanner_setLocale}, |
| 277 | {"extractAlbumArt", "(Ljava/io/FileDescriptor;)[B", (void *)android_media_MediaScanner_extractAlbumArt}, |
| 278 | {"native_setup", "()V", (void *)android_media_MediaScanner_native_setup}, |
| 279 | {"native_finalize", "()V", (void *)android_media_MediaScanner_native_finalize}, |
| 280 | }; |
| 281 | |
| 282 | static const char* const kClassPathName = "android/media/MediaScanner"; |
| 283 | |
| 284 | int register_android_media_MediaScanner(JNIEnv *env) |
| 285 | { |
| 286 | jclass clazz; |
| 287 | |
| 288 | clazz = env->FindClass("android/media/MediaScanner"); |
| 289 | if (clazz == NULL) { |
| 290 | LOGE("Can't find android/media/MediaScanner"); |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | fields.context = env->GetFieldID(clazz, "mNativeContext", "I"); |
| 295 | if (fields.context == NULL) { |
| 296 | LOGE("Can't find MediaScanner.mNativeContext"); |
| 297 | return -1; |
| 298 | } |
| 299 | |
| 300 | return AndroidRuntime::registerNativeMethods(env, |
| 301 | "android/media/MediaScanner", gMethods, NELEM(gMethods)); |
| 302 | } |
| 303 | |
| 304 | |