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" |
Ruben Brunk | 87eac99 | 2013-09-09 17:44:59 -0700 | [diff] [blame^] | 28 | #include "android_runtime/Log.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | using namespace android; |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 33 | static const char* const kClassMediaScannerClient = |
| 34 | "android/media/MediaScannerClient"; |
| 35 | |
| 36 | static const char* const kClassMediaScanner = |
| 37 | "android/media/MediaScanner"; |
| 38 | |
| 39 | static const char* const kRunTimeException = |
| 40 | "java/lang/RuntimeException"; |
| 41 | |
| 42 | static const char* const kIllegalArgumentException = |
| 43 | "java/lang/IllegalArgumentException"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 45 | struct fields_t { |
| 46 | jfieldID context; |
| 47 | }; |
| 48 | static fields_t fields; |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 49 | |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 50 | static status_t checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 51 | if (env->ExceptionCheck()) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 52 | ALOGE("An exception was thrown by callback '%s'.", methodName); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 53 | LOGE_EX(env); |
| 54 | env->ExceptionClear(); |
| 55 | return UNKNOWN_ERROR; |
| 56 | } |
| 57 | return OK; |
| 58 | } |
| 59 | |
Marco Nelissen | a0a73ed | 2013-02-12 18:33:41 +0000 | [diff] [blame] | 60 | // stolen from dalvik/vm/checkJni.cpp |
| 61 | static bool isValidUtf8(const char* bytes) { |
| 62 | while (*bytes != '\0') { |
| 63 | unsigned char utf8 = *(bytes++); |
| 64 | // Switch on the high four bits. |
| 65 | switch (utf8 >> 4) { |
| 66 | case 0x00: |
| 67 | case 0x01: |
| 68 | case 0x02: |
| 69 | case 0x03: |
| 70 | case 0x04: |
| 71 | case 0x05: |
| 72 | case 0x06: |
| 73 | case 0x07: |
| 74 | // Bit pattern 0xxx. No need for any extra bytes. |
| 75 | break; |
| 76 | case 0x08: |
| 77 | case 0x09: |
| 78 | case 0x0a: |
| 79 | case 0x0b: |
| 80 | case 0x0f: |
| 81 | /* |
| 82 | * Bit pattern 10xx or 1111, which are illegal start bytes. |
| 83 | * Note: 1111 is valid for normal UTF-8, but not the |
| 84 | * modified UTF-8 used here. |
| 85 | */ |
| 86 | return false; |
| 87 | case 0x0e: |
| 88 | // Bit pattern 1110, so there are two additional bytes. |
| 89 | utf8 = *(bytes++); |
| 90 | if ((utf8 & 0xc0) != 0x80) { |
| 91 | return false; |
| 92 | } |
| 93 | // Fall through to take care of the final byte. |
| 94 | case 0x0c: |
| 95 | case 0x0d: |
| 96 | // Bit pattern 110x, so there is one additional byte. |
| 97 | utf8 = *(bytes++); |
| 98 | if ((utf8 & 0xc0) != 0x80) { |
| 99 | return false; |
| 100 | } |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | class MyMediaScannerClient : public MediaScannerClient |
| 108 | { |
| 109 | public: |
| 110 | MyMediaScannerClient(JNIEnv *env, jobject client) |
| 111 | : mEnv(env), |
| 112 | mClient(env->NewGlobalRef(client)), |
| 113 | mScanFileMethodID(0), |
| 114 | mHandleStringTagMethodID(0), |
| 115 | mSetMimeTypeMethodID(0) |
| 116 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 117 | ALOGV("MyMediaScannerClient constructor"); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 118 | jclass mediaScannerClientInterface = |
| 119 | env->FindClass(kClassMediaScannerClient); |
| 120 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | if (mediaScannerClientInterface == NULL) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 122 | ALOGE("Class %s not found", kClassMediaScannerClient); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 123 | } else { |
| 124 | mScanFileMethodID = env->GetMethodID( |
| 125 | mediaScannerClientInterface, |
| 126 | "scanFile", |
Mike Lockwood | 997354e | 2011-04-24 11:15:09 -0700 | [diff] [blame] | 127 | "(Ljava/lang/String;JJZZ)V"); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 128 | |
| 129 | mHandleStringTagMethodID = env->GetMethodID( |
| 130 | mediaScannerClientInterface, |
| 131 | "handleStringTag", |
| 132 | "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 133 | |
| 134 | mSetMimeTypeMethodID = env->GetMethodID( |
| 135 | mediaScannerClientInterface, |
| 136 | "setMimeType", |
| 137 | "(Ljava/lang/String;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 140 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | virtual ~MyMediaScannerClient() |
| 142 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 143 | ALOGV("MyMediaScannerClient destructor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | mEnv->DeleteGlobalRef(mClient); |
| 145 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 146 | |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 147 | virtual status_t scanFile(const char* path, long long lastModified, |
Mike Lockwood | 997354e | 2011-04-24 11:15:09 -0700 | [diff] [blame] | 148 | long long fileSize, bool isDirectory, bool noMedia) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 150 | ALOGV("scanFile: path(%s), time(%lld), size(%lld) and isDir(%d)", |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 151 | path, lastModified, fileSize, isDirectory); |
| 152 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | jstring pathStr; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 154 | if ((pathStr = mEnv->NewStringUTF(path)) == NULL) { |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 155 | mEnv->ExceptionClear(); |
| 156 | return NO_MEMORY; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 157 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | |
Mike Lockwood | 076e05b | 2010-12-16 12:54:24 -0800 | [diff] [blame] | 159 | mEnv->CallVoidMethod(mClient, mScanFileMethodID, pathStr, lastModified, |
Mike Lockwood | 997354e | 2011-04-24 11:15:09 -0700 | [diff] [blame] | 160 | fileSize, isDirectory, noMedia); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | |
| 162 | mEnv->DeleteLocalRef(pathStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 163 | return checkAndClearExceptionFromCallback(mEnv, "scanFile"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 166 | virtual status_t handleStringTag(const char* name, const char* value) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 168 | ALOGV("handleStringTag: name(%s) and value(%s)", name, value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | jstring nameStr, valueStr; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 170 | if ((nameStr = mEnv->NewStringUTF(name)) == NULL) { |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 171 | mEnv->ExceptionClear(); |
| 172 | return NO_MEMORY; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 173 | } |
Marco Nelissen | f51f1bd | 2011-11-02 10:49:50 -0700 | [diff] [blame] | 174 | char *cleaned = NULL; |
Marco Nelissen | a0a73ed | 2013-02-12 18:33:41 +0000 | [diff] [blame] | 175 | if (!isValidUtf8(value)) { |
Marco Nelissen | f51f1bd | 2011-11-02 10:49:50 -0700 | [diff] [blame] | 176 | cleaned = strdup(value); |
| 177 | char *chp = cleaned; |
| 178 | char ch; |
| 179 | while ((ch = *chp)) { |
| 180 | if (ch & 0x80) { |
| 181 | *chp = '?'; |
| 182 | } |
| 183 | chp++; |
| 184 | } |
| 185 | value = cleaned; |
| 186 | } |
| 187 | valueStr = mEnv->NewStringUTF(value); |
| 188 | free(cleaned); |
| 189 | if (valueStr == NULL) { |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 190 | mEnv->DeleteLocalRef(nameStr); |
| 191 | mEnv->ExceptionClear(); |
| 192 | return NO_MEMORY; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 193 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 195 | mEnv->CallVoidMethod( |
| 196 | mClient, mHandleStringTagMethodID, nameStr, valueStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | |
| 198 | mEnv->DeleteLocalRef(nameStr); |
| 199 | mEnv->DeleteLocalRef(valueStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 200 | return checkAndClearExceptionFromCallback(mEnv, "handleStringTag"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 203 | virtual status_t setMimeType(const char* mimeType) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 205 | ALOGV("setMimeType: %s", mimeType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | jstring mimeTypeStr; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 207 | if ((mimeTypeStr = mEnv->NewStringUTF(mimeType)) == NULL) { |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 208 | mEnv->ExceptionClear(); |
| 209 | return NO_MEMORY; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 210 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | |
| 212 | mEnv->CallVoidMethod(mClient, mSetMimeTypeMethodID, mimeTypeStr); |
| 213 | |
| 214 | mEnv->DeleteLocalRef(mimeTypeStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 215 | return checkAndClearExceptionFromCallback(mEnv, "setMimeType"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | private: |
| 219 | JNIEnv *mEnv; |
| 220 | jobject mClient; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 221 | jmethodID mScanFileMethodID; |
| 222 | jmethodID mHandleStringTagMethodID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | jmethodID mSetMimeTypeMethodID; |
| 224 | }; |
| 225 | |
| 226 | |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 227 | static MediaScanner *getNativeScanner_l(JNIEnv* env, jobject thiz) |
| 228 | { |
| 229 | return (MediaScanner *) env->GetIntField(thiz, fields.context); |
| 230 | } |
| 231 | |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 232 | static void setNativeScanner_l(JNIEnv* env, jobject thiz, MediaScanner *s) |
| 233 | { |
| 234 | env->SetIntField(thiz, fields.context, (int)s); |
| 235 | } |
| 236 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | static void |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 238 | android_media_MediaScanner_processDirectory( |
| 239 | JNIEnv *env, jobject thiz, jstring path, jobject client) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 241 | ALOGV("processDirectory"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 242 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 243 | if (mp == NULL) { |
| 244 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 245 | return; |
| 246 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | |
| 248 | if (path == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 249 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | return; |
| 251 | } |
Mike Lockwood | c37255d | 2010-09-10 14:47:36 -0400 | [diff] [blame] | 252 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
| 254 | if (pathStr == NULL) { // Out of memory |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | return; |
| 256 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | |
| 258 | MyMediaScannerClient myClient(env, client); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 259 | MediaScanResult result = mp->processDirectory(pathStr, myClient); |
| 260 | if (result == MEDIA_SCAN_RESULT_ERROR) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 261 | ALOGE("An error occurred while scanning directory '%s'.", pathStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 262 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | env->ReleaseStringUTFChars(path, pathStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static void |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 267 | android_media_MediaScanner_processFile( |
| 268 | JNIEnv *env, jobject thiz, jstring path, |
| 269 | jstring mimeType, jobject client) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 271 | ALOGV("processFile"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 272 | |
| 273 | // Lock already hold by processDirectory |
| 274 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 275 | if (mp == NULL) { |
| 276 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 277 | return; |
| 278 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | |
| 280 | if (path == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 281 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | return; |
| 283 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | const char *pathStr = env->GetStringUTFChars(path, NULL); |
| 286 | if (pathStr == NULL) { // Out of memory |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | return; |
| 288 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 289 | |
| 290 | const char *mimeTypeStr = |
| 291 | (mimeType ? env->GetStringUTFChars(mimeType, NULL) : NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | if (mimeType && mimeTypeStr == NULL) { // Out of memory |
James Dong | c371a02 | 2011-04-06 12:16:07 -0700 | [diff] [blame] | 293 | // ReleaseStringUTFChars can be called with an exception pending. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | env->ReleaseStringUTFChars(path, pathStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | return; |
| 296 | } |
| 297 | |
| 298 | MyMediaScannerClient myClient(env, client); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 299 | MediaScanResult result = mp->processFile(pathStr, mimeTypeStr, myClient); |
| 300 | if (result == MEDIA_SCAN_RESULT_ERROR) { |
Steve Block | c6aacce | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 301 | ALOGE("An error occurred while scanning file '%s'.", pathStr); |
Jeff Brown | 2c70d4a | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 302 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | env->ReleaseStringUTFChars(path, pathStr); |
| 304 | if (mimeType) { |
| 305 | env->ReleaseStringUTFChars(mimeType, mimeTypeStr); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static void |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 310 | android_media_MediaScanner_setLocale( |
| 311 | JNIEnv *env, jobject thiz, jstring locale) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 313 | ALOGV("setLocale"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 314 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 315 | if (mp == NULL) { |
| 316 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 317 | return; |
| 318 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | |
| 320 | if (locale == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 321 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | return; |
| 323 | } |
| 324 | const char *localeStr = env->GetStringUTFChars(locale, NULL); |
| 325 | if (localeStr == NULL) { // Out of memory |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | return; |
| 327 | } |
| 328 | mp->setLocale(localeStr); |
| 329 | |
| 330 | env->ReleaseStringUTFChars(locale, localeStr); |
| 331 | } |
| 332 | |
| 333 | static jbyteArray |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 334 | android_media_MediaScanner_extractAlbumArt( |
| 335 | JNIEnv *env, jobject thiz, jobject fileDescriptor) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 337 | ALOGV("extractAlbumArt"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 338 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
| 339 | if (mp == NULL) { |
| 340 | jniThrowException(env, kRunTimeException, "No scanner available"); |
| 341 | return NULL; |
| 342 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | |
| 344 | if (fileDescriptor == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 345 | jniThrowException(env, kIllegalArgumentException, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | return NULL; |
| 347 | } |
| 348 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 349 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | char* data = mp->extractAlbumArt(fd); |
| 351 | if (!data) { |
| 352 | return NULL; |
| 353 | } |
| 354 | long len = *((long*)data); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 355 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | jbyteArray array = env->NewByteArray(len); |
| 357 | if (array != NULL) { |
| 358 | jbyte* bytes = env->GetByteArrayElements(array, NULL); |
| 359 | memcpy(bytes, data + 4, len); |
| 360 | env->ReleaseByteArrayElements(array, bytes, 0); |
| 361 | } |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 362 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | done: |
| 364 | free(data); |
| 365 | // if NewByteArray() returned NULL, an out-of-memory |
| 366 | // exception will have been raised. I just want to |
| 367 | // return null in that case. |
| 368 | env->ExceptionClear(); |
| 369 | return array; |
| 370 | } |
| 371 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 372 | // This function gets a field ID, which in turn causes class initialization. |
| 373 | // It is called from a static block in MediaScanner, which won't run until the |
| 374 | // first time an instance of this class is used. |
| 375 | static void |
| 376 | android_media_MediaScanner_native_init(JNIEnv *env) |
| 377 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 378 | ALOGV("native_init"); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 379 | jclass clazz = env->FindClass(kClassMediaScanner); |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 380 | if (clazz == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 381 | return; |
| 382 | } |
| 383 | |
| 384 | fields.context = env->GetFieldID(clazz, "mNativeContext", "I"); |
| 385 | if (fields.context == NULL) { |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | } |
| 389 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | static void |
| 391 | android_media_MediaScanner_native_setup(JNIEnv *env, jobject thiz) |
| 392 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 393 | ALOGV("native_setup"); |
Andreas Huber | 8d65dd2 | 2010-06-23 16:40:57 -0700 | [diff] [blame] | 394 | MediaScanner *mp = new StagefrightMediaScanner; |
Andreas Huber | bfb9fb1 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 395 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | if (mp == NULL) { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 397 | jniThrowException(env, kRunTimeException, "Out of memory"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | return; |
| 399 | } |
| 400 | |
| 401 | env->SetIntField(thiz, fields.context, (int)mp); |
| 402 | } |
| 403 | |
| 404 | static void |
| 405 | android_media_MediaScanner_native_finalize(JNIEnv *env, jobject thiz) |
| 406 | { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 407 | ALOGV("native_finalize"); |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 408 | MediaScanner *mp = getNativeScanner_l(env, thiz); |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 409 | if (mp == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | return; |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 411 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | delete mp; |
James Dong | 133cf8b | 2011-03-11 15:18:40 -0800 | [diff] [blame] | 413 | setNativeScanner_l(env, thiz, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | } |
| 415 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | static JNINativeMethod gMethods[] = { |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 417 | { |
| 418 | "processDirectory", |
| 419 | "(Ljava/lang/String;Landroid/media/MediaScannerClient;)V", |
| 420 | (void *)android_media_MediaScanner_processDirectory |
| 421 | }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 423 | { |
| 424 | "processFile", |
| 425 | "(Ljava/lang/String;Ljava/lang/String;Landroid/media/MediaScannerClient;)V", |
| 426 | (void *)android_media_MediaScanner_processFile |
| 427 | }, |
| 428 | |
| 429 | { |
| 430 | "setLocale", |
| 431 | "(Ljava/lang/String;)V", |
| 432 | (void *)android_media_MediaScanner_setLocale |
| 433 | }, |
| 434 | |
| 435 | { |
| 436 | "extractAlbumArt", |
| 437 | "(Ljava/io/FileDescriptor;)[B", |
| 438 | (void *)android_media_MediaScanner_extractAlbumArt |
| 439 | }, |
| 440 | |
| 441 | { |
| 442 | "native_init", |
| 443 | "()V", |
| 444 | (void *)android_media_MediaScanner_native_init |
| 445 | }, |
| 446 | |
| 447 | { |
| 448 | "native_setup", |
| 449 | "()V", |
| 450 | (void *)android_media_MediaScanner_native_setup |
| 451 | }, |
| 452 | |
| 453 | { |
| 454 | "native_finalize", |
| 455 | "()V", |
| 456 | (void *)android_media_MediaScanner_native_finalize |
| 457 | }, |
| 458 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | |
Marco Nelissen | 4935d05 | 2009-08-03 11:12:58 -0700 | [diff] [blame] | 460 | // This function only registers the native methods, and is called from |
| 461 | // JNI_OnLoad in android_media_MediaPlayer.cpp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | int register_android_media_MediaScanner(JNIEnv *env) |
| 463 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | return AndroidRuntime::registerNativeMethods(env, |
James Dong | f399752 | 2011-03-11 12:02:12 -0800 | [diff] [blame] | 465 | kClassMediaScanner, gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | } |