James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "MediaScannerJNI" |
| 20 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <utils/threads.h> |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 22 | #include <media/mediascanner.h> |
| 23 | #include <media/stagefright/StagefrightMediaScanner.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | #include "jni.h" |
| 26 | #include "JNIHelp.h" |
| 27 | #include "android_runtime/AndroidRuntime.h" |
| 28 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | using namespace android; |
| 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 32 | static const char* const kClassMediaScannerClient = |
| 33 | "android/media/MediaScannerClient"; |
| 34 | |
| 35 | static const char* const kClassMediaScanner = |
| 36 | "android/media/MediaScanner"; |
| 37 | |
| 38 | static const char* const kRunTimeException = |
| 39 | "java/lang/RuntimeException"; |
| 40 | |
| 41 | static const char* const kIllegalArgumentException = |
| 42 | "java/lang/IllegalArgumentException"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 44 | struct fields_t { |
| 45 | jfieldID context; |
| 46 | }; |
| 47 | static fields_t fields; |
| 48 | static Mutex sLock; |
| 49 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | class MyMediaScannerClient : public MediaScannerClient |
| 51 | { |
| 52 | public: |
| 53 | MyMediaScannerClient(JNIEnv *env, jobject client) |
| 54 | : mEnv(env), |
| 55 | mClient(env->NewGlobalRef(client)), |
| 56 | mScanFileMethodID(0), |
| 57 | mHandleStringTagMethodID(0), |
| 58 | mSetMimeTypeMethodID(0) |
| 59 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 60 | LOGV("MyMediaScannerClient constructor"); |
| 61 | jclass mediaScannerClientInterface = |
| 62 | env->FindClass(kClassMediaScannerClient); |
| 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | if (mediaScannerClientInterface == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 65 | LOGE("Class %s not found", kClassMediaScannerClient); |
| 66 | } else { |
| 67 | mScanFileMethodID = env->GetMethodID( |
| 68 | mediaScannerClientInterface, |
| 69 | "scanFile", |
Mike Lockwood | 997354e | 2011-04-24 11:15:09 -0700 | [diff] [blame^] | 70 | "(Ljava/lang/String;JJZZ)V"); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 71 | |
| 72 | mHandleStringTagMethodID = env->GetMethodID( |
| 73 | mediaScannerClientInterface, |
| 74 | "handleStringTag", |
| 75 | "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 76 | |
| 77 | mSetMimeTypeMethodID = env->GetMethodID( |
| 78 | mediaScannerClientInterface, |
| 79 | "setMimeType", |
| 80 | "(Ljava/lang/String;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 83 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | virtual ~MyMediaScannerClient() |
| 85 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 86 | LOGV("MyMediaScannerClient destructor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | mEnv->DeleteGlobalRef(mClient); |
| 88 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 89 | |
| 90 | // Returns true if it succeeded, false if an exception occured |
| 91 | // in the Java code |
Mike Lockwood | 076e05b | 2010-12-16 12:54:24 -0800 | [diff] [blame] | 92 | virtual bool scanFile(const char* path, long long lastModified, |
Mike Lockwood | 997354e | 2011-04-24 11:15:09 -0700 | [diff] [blame^] | 93 | long long fileSize, bool isDirectory, bool noMedia) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 95 | LOGV("scanFile: path(%s), time(%lld), size(%lld) and isDir(%d)", |
| 96 | path, lastModified, fileSize, isDirectory); |
| 97 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | jstring pathStr; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 99 | if ((pathStr = mEnv->NewStringUTF(path)) == NULL) { |
| 100 | return false; |
| 101 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | |
Mike Lockwood | 076e05b | 2010-12-16 12:54:24 -0800 | [diff] [blame] | 103 | mEnv->CallVoidMethod(mClient, mScanFileMethodID, pathStr, lastModified, |
Mike Lockwood | 997354e | 2011-04-24 11:15:09 -0700 | [diff] [blame^] | 104 | fileSize, isDirectory, noMedia); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | |
| 106 | mEnv->DeleteLocalRef(pathStr); |
| 107 | return (!mEnv->ExceptionCheck()); |
| 108 | } |
| 109 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 110 | // Returns true if it succeeded, false if an exception occured |
| 111 | // in the Java code |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | virtual bool handleStringTag(const char* name, const char* value) |
| 113 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 114 | LOGV("handleStringTag: name(%s) and value(%s)", name, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | jstring nameStr, valueStr; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 116 | if ((nameStr = mEnv->NewStringUTF(name)) == NULL) { |
| 117 | return false; |
| 118 | } |
| 119 | if ((valueStr = mEnv->NewStringUTF(value)) == NULL) { |
| 120 | return false; |
| 121 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 123 | mEnv->CallVoidMethod( |
| 124 | mClient, mHandleStringTagMethodID, nameStr, valueStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | |
| 126 | mEnv->DeleteLocalRef(nameStr); |
| 127 | mEnv->DeleteLocalRef(valueStr); |
| 128 | return (!mEnv->ExceptionCheck()); |
| 129 | } |
| 130 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 131 | // Returns true if it succeeded, false if an exception occured |
| 132 | // in the Java code |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | virtual bool setMimeType(const char* mimeType) |
| 134 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 135 | LOGV("setMimeType: %s", mimeType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | jstring mimeTypeStr; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 137 | if ((mimeTypeStr = mEnv->NewStringUTF(mimeType)) == NULL) { |
| 138 | return false; |
| 139 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | |
| 141 | mEnv->CallVoidMethod(mClient, mSetMimeTypeMethodID, mimeTypeStr); |
| 142 | |
| 143 | mEnv->DeleteLocalRef(mimeTypeStr); |
| 144 | return (!mEnv->ExceptionCheck()); |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | JNIEnv *mEnv; |
| 149 | jobject mClient; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 150 | jmethodID mScanFileMethodID; |
| 151 | jmethodID mHandleStringTagMethodID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | jmethodID mSetMimeTypeMethodID; |
| 153 | }; |
| 154 | |
| 155 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | static bool ExceptionCheck(void* env) |
| 157 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 158 | LOGV("ExceptionCheck"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | return ((JNIEnv *)env)->ExceptionCheck(); |
| 160 | } |
| 161 | |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 162 | // Call this method with sLock hold |
| 163 | static MediaScanner *getNativeScanner_l(JNIEnv* env, jobject thiz) |
| 164 | { |
| 165 | return (MediaScanner *) env->GetIntField(thiz, fields.context); |
| 166 | } |
| 167 | |
| 168 | // Call this method with sLock hold |
| 169 | static void setNativeScanner_l(JNIEnv* env, jobject thiz, MediaScanner *s) |
| 170 | { |
| 171 | env->SetIntField(thiz, fields.context, (int)s); |
| 172 | } |
| 173 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | static void |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 175 | android_media_MediaScanner_processDirectory( |
| 176 | JNIEnv *env, jobject thiz, jstring path, jobject client) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 178 | LOGV("processDirectory"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 179 | Mutex::Autolock l(sLock); |
| 180 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 181 | if (mp == NULL) { |
| 182 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 183 | return; |
| 184 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | |
| 186 | if (path == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 187 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | return; |
| 189 | } |
Mike Lockwood | c37255d | 2010-09-10 14:47:36 -0400 | [diff] [blame] | 190 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
| 192 | if (pathStr == NULL) { // Out of memory |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | return; |
| 194 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | |
| 196 | MyMediaScannerClient myClient(env, client); |
Mike Lockwood | c37255d | 2010-09-10 14:47:36 -0400 | [diff] [blame] | 197 | mp->processDirectory(pathStr, myClient, ExceptionCheck, env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | env->ReleaseStringUTFChars(path, pathStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static void |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 202 | android_media_MediaScanner_processFile( |
| 203 | JNIEnv *env, jobject thiz, jstring path, |
| 204 | jstring mimeType, jobject client) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 206 | LOGV("processFile"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 207 | |
| 208 | // Lock already hold by processDirectory |
| 209 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 210 | if (mp == NULL) { |
| 211 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 212 | return; |
| 213 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | |
| 215 | if (path == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 216 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | return; |
| 218 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 219 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
| 221 | if (pathStr == NULL) { // Out of memory |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | return; |
| 223 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 224 | |
| 225 | const char *mimeTypeStr = |
| 226 | (mimeType ? env->GetStringUTFChars(mimeType, NULL) : NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | if (mimeType && mimeTypeStr == NULL) { // Out of memory |
James Dong | c371a02 | 2011-04-06 12:16:07 -0700 | [diff] [blame] | 228 | // ReleaseStringUTFChars can be called with an exception pending. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | env->ReleaseStringUTFChars(path, pathStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | return; |
| 231 | } |
| 232 | |
| 233 | MyMediaScannerClient myClient(env, client); |
| 234 | mp->processFile(pathStr, mimeTypeStr, myClient); |
| 235 | env->ReleaseStringUTFChars(path, pathStr); |
| 236 | if (mimeType) { |
| 237 | env->ReleaseStringUTFChars(mimeType, mimeTypeStr); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static void |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 242 | android_media_MediaScanner_setLocale( |
| 243 | JNIEnv *env, jobject thiz, jstring locale) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 245 | LOGV("setLocale"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 246 | Mutex::Autolock l(sLock); |
| 247 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 248 | if (mp == NULL) { |
| 249 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 250 | return; |
| 251 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | |
| 253 | if (locale == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 254 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | return; |
| 256 | } |
| 257 | const char *localeStr = env->GetStringUTFChars(locale, NULL); |
| 258 | if (localeStr == NULL) { // Out of memory |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | mp->setLocale(localeStr); |
| 262 | |
| 263 | env->ReleaseStringUTFChars(locale, localeStr); |
| 264 | } |
| 265 | |
| 266 | static jbyteArray |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 267 | android_media_MediaScanner_extractAlbumArt( |
| 268 | JNIEnv *env, jobject thiz, jobject fileDescriptor) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 270 | LOGV("extractAlbumArt"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 271 | Mutex::Autolock l(sLock); |
| 272 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 273 | if (mp == NULL) { |
| 274 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 275 | return NULL; |
| 276 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | |
| 278 | if (fileDescriptor == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 279 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | return NULL; |
| 281 | } |
| 282 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 283 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | char* data = mp->extractAlbumArt(fd); |
| 285 | if (!data) { |
| 286 | return NULL; |
| 287 | } |
| 288 | long len = *((long*)data); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | jbyteArray array = env->NewByteArray(len); |
| 291 | if (array != NULL) { |
| 292 | jbyte* bytes = env->GetByteArrayElements(array, NULL); |
| 293 | memcpy(bytes, data + 4, len); |
| 294 | env->ReleaseByteArrayElements(array, bytes, 0); |
| 295 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 296 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | done: |
| 298 | free(data); |
| 299 | // if NewByteArray() returned NULL, an out-of-memory |
| 300 | // exception will have been raised. I just want to |
| 301 | // return null in that case. |
| 302 | env->ExceptionClear(); |
| 303 | return array; |
| 304 | } |
| 305 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 306 | // This function gets a field ID, which in turn causes class initialization. |
| 307 | // It is called from a static block in MediaScanner, which won't run until the |
| 308 | // first time an instance of this class is used. |
| 309 | static void |
| 310 | android_media_MediaScanner_native_init(JNIEnv *env) |
| 311 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 312 | LOGV("native_init"); |
| 313 | jclass clazz = env->FindClass(kClassMediaScanner); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 314 | if (clazz == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | |
| 318 | fields.context = env->GetFieldID(clazz, "mNativeContext", "I"); |
| 319 | if (fields.context == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 320 | return; |
| 321 | } |
| 322 | } |
| 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | static void |
| 325 | android_media_MediaScanner_native_setup(JNIEnv *env, jobject thiz) |
| 326 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 327 | LOGV("native_setup"); |
Andreas Huber | 8d65dd2 | 2010-06-23 16:40:57 -0700 | [diff] [blame] | 328 | MediaScanner *mp = new StagefrightMediaScanner; |
Andreas Huber | bfb9fb1 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 329 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | if (mp == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 331 | jniThrowException(env, kRunTimeException, "Out of memory"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | return; |
| 333 | } |
| 334 | |
| 335 | env->SetIntField(thiz, fields.context, (int)mp); |
| 336 | } |
| 337 | |
| 338 | static void |
| 339 | android_media_MediaScanner_native_finalize(JNIEnv *env, jobject thiz) |
| 340 | { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 341 | LOGV("native_finalize"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 342 | Mutex::Autolock l(sLock); |
| 343 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 344 | if (mp == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | return; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 346 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | delete mp; |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 348 | setNativeScanner_l(env, thiz, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | } |
| 350 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | static JNINativeMethod gMethods[] = { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 352 | { |
| 353 | "processDirectory", |
| 354 | "(Ljava/lang/String;Landroid/media/MediaScannerClient;)V", |
| 355 | (void *)android_media_MediaScanner_processDirectory |
| 356 | }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 358 | { |
| 359 | "processFile", |
| 360 | "(Ljava/lang/String;Ljava/lang/String;Landroid/media/MediaScannerClient;)V", |
| 361 | (void *)android_media_MediaScanner_processFile |
| 362 | }, |
| 363 | |
| 364 | { |
| 365 | "setLocale", |
| 366 | "(Ljava/lang/String;)V", |
| 367 | (void *)android_media_MediaScanner_setLocale |
| 368 | }, |
| 369 | |
| 370 | { |
| 371 | "extractAlbumArt", |
| 372 | "(Ljava/io/FileDescriptor;)[B", |
| 373 | (void *)android_media_MediaScanner_extractAlbumArt |
| 374 | }, |
| 375 | |
| 376 | { |
| 377 | "native_init", |
| 378 | "()V", |
| 379 | (void *)android_media_MediaScanner_native_init |
| 380 | }, |
| 381 | |
| 382 | { |
| 383 | "native_setup", |
| 384 | "()V", |
| 385 | (void *)android_media_MediaScanner_native_setup |
| 386 | }, |
| 387 | |
| 388 | { |
| 389 | "native_finalize", |
| 390 | "()V", |
| 391 | (void *)android_media_MediaScanner_native_finalize |
| 392 | }, |
| 393 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 395 | // This function only registers the native methods, and is called from |
| 396 | // JNI_OnLoad in android_media_MediaPlayer.cpp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | int register_android_media_MediaScanner(JNIEnv *env) |
| 398 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | return AndroidRuntime::registerNativeMethods(env, |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 400 | kClassMediaScanner, gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | } |