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