2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "android_drm_DrmManagerClient"
19 #include <utils/Log.h>
22 #include <nativehelper/JNIHelp.h>
23 #include <nativehelper/ScopedLocalRef.h>
24 #include <android_runtime/AndroidRuntime.h>
26 #include <drm/DrmInfo.h>
27 #include <drm/DrmRights.h>
28 #include <drm/DrmInfoEvent.h>
29 #include <drm/DrmInfoStatus.h>
30 #include <drm/DrmInfoRequest.h>
31 #include <drm/DrmSupportInfo.h>
32 #include <drm/DrmConstraints.h>
33 #include <drm/DrmMetadata.h>
34 #include <drm/DrmConvertedStatus.h>
35 #include <drm/drm_framework_common.h>
37 #include <DrmManagerClientImpl.h>
39 using namespace android;
42 * Utility class used to extract the value from the provided java object.
43 * May need to add some utility function to create java object.
47 static String8 getStringValue(JNIEnv* env, jobject object, const char* fieldName);
49 static char* getByteArrayValue(
50 JNIEnv* env, jobject object, const char* fieldName, int* dataLength);
52 static char* getByteArrayValue(
53 JNIEnv* env, jbyteArray byteArray, int* dataLength);
55 static String8 getStringValue(JNIEnv* env, jstring string);
57 static int getIntValue(JNIEnv* env, jobject object, const char* fieldName);
60 String8 Utility::getStringValue(JNIEnv* env, jobject object, const char* fieldName) {
61 /* Look for the instance field with the name fieldName */
63 = env->GetFieldID(env->GetObjectClass(object), fieldName , "Ljava/lang/String;");
65 if (NULL != fieldID) {
66 jstring valueString = (jstring) env->GetObjectField(object, fieldID);
67 return Utility::getStringValue(env, valueString);
70 String8 dataString("");
74 String8 Utility::getStringValue(JNIEnv* env, jstring string) {
75 String8 dataString("");
77 if (NULL != string && string != env->NewStringUTF("")) {
78 char* bytes = const_cast< char* > (env->GetStringUTFChars(string, NULL));
80 const int length = strlen(bytes) + 1;
81 char *data = new char[length];
82 strncpy(data, bytes, length);
83 dataString = String8(data);
85 env->ReleaseStringUTFChars(string, bytes);
86 delete [] data; data = NULL;
91 char* Utility::getByteArrayValue(
92 JNIEnv* env, jobject object, const char* fieldName, int* dataLength) {
96 jfieldID fieldID = env->GetFieldID(env->GetObjectClass(object), fieldName , "[B");
98 if (NULL != fieldID) {
99 jbyteArray byteArray = (jbyteArray) env->GetObjectField(object, fieldID);
100 return Utility::getByteArrayValue(env, byteArray, dataLength);
105 char* Utility::getByteArrayValue(JNIEnv* env, jbyteArray byteArray, int* dataLength) {
107 if (NULL != byteArray) {
108 jint length = env->GetArrayLength(byteArray);
110 *dataLength = length;
111 if (0 < *dataLength) {
112 data = new char[length];
113 env->GetByteArrayRegion(byteArray, (jint)0, length, (jbyte *) data);
119 int Utility::getIntValue(JNIEnv* env, jobject object, const char* fieldName) {
123 /* Get a reference to obj’s class */
124 jclass clazz = env->GetObjectClass(object);
125 /* Look for the instance field with the name fieldName */
126 fieldID = env->GetFieldID(clazz, fieldName , "I");
128 if (NULL != fieldID) {
129 intValue = (int) env->GetIntField(object, fieldID);
135 class JNIOnInfoListener : public DrmManagerClient::OnInfoListener {
137 JNIOnInfoListener(JNIEnv* env, jobject thiz, jobject weak_thiz);
139 virtual ~JNIOnInfoListener();
140 void onInfo(const DrmInfoEvent& event);
148 JNIOnInfoListener::JNIOnInfoListener(JNIEnv* env, jobject thiz, jobject weak_thiz) {
149 jclass clazz = env->GetObjectClass(thiz);
152 ALOGE("Can't find android/drm/DrmManagerClient");
153 jniThrowException(env, "java/lang/Exception", NULL);
156 mClass = (jclass)env->NewGlobalRef(clazz);
157 mObject = env->NewGlobalRef(weak_thiz);
160 JNIOnInfoListener::~JNIOnInfoListener() {
161 JNIEnv *env = AndroidRuntime::getJNIEnv();
162 env->DeleteGlobalRef(mObject);
163 env->DeleteGlobalRef(mClass);
166 void JNIOnInfoListener::onInfo(const DrmInfoEvent& event) {
167 jint uniqueId = event.getUniqueId();
168 jint type = event.getType();
169 JNIEnv *env = AndroidRuntime::getJNIEnv();
170 jstring message = env->NewStringUTF(event.getMessage().string());
171 ALOGV("JNIOnInfoListener::onInfo => %d | %d | %s", uniqueId, type, event.getMessage().string());
173 env->CallStaticVoidMethod(
175 env->GetStaticMethodID(mClass, "notify", "(Ljava/lang/Object;IILjava/lang/String;)V"),
176 mObject, uniqueId, type, message);
181 static sp<DrmManagerClientImpl> setDrmManagerClientImpl(
182 JNIEnv* env, jobject thiz, const sp<DrmManagerClientImpl>& client) {
183 Mutex::Autolock l(sLock);
184 jclass clazz = env->FindClass("android/drm/DrmManagerClient");
185 jfieldID fieldId = env->GetFieldID(clazz, "mNativeContext", "J");
187 jlong oldHandle = env->GetLongField(thiz, fieldId);
188 sp<DrmManagerClientImpl> old = reinterpret_cast<DrmManagerClientImpl*>(oldHandle);
190 client->incStrong(thiz);
193 old->decStrong(thiz);
195 env->SetLongField(thiz, fieldId, reinterpret_cast<jlong>(client.get()));
199 static sp<DrmManagerClientImpl> getDrmManagerClientImpl(JNIEnv* env, jobject thiz) {
200 Mutex::Autolock l(sLock);
201 jclass clazz = env->FindClass("android/drm/DrmManagerClient");
202 jfieldID fieldId = env->GetFieldID(clazz, "mNativeContext", "J");
204 jlong clientHandle = env->GetLongField(thiz, fieldId);
205 DrmManagerClientImpl* const client = reinterpret_cast<DrmManagerClientImpl*>(clientHandle);
206 return sp<DrmManagerClientImpl>(client);
209 static jint android_drm_DrmManagerClient_initialize(
210 JNIEnv* env, jobject thiz) {
211 ALOGV("initialize - Enter");
214 sp<DrmManagerClientImpl> drmManager = DrmManagerClientImpl::create(&uniqueId, false);
215 drmManager->addClient(uniqueId);
217 setDrmManagerClientImpl(env, thiz, drmManager);
218 ALOGV("initialize - Exit");
219 return static_cast<jint>(uniqueId);
222 static void android_drm_DrmManagerClient_setListeners(
223 JNIEnv* env, jobject thiz, jint uniqueId, jobject weak_thiz) {
224 ALOGV("setListeners - Enter");
226 // Set the listener to DrmManager
227 sp<DrmManagerClient::OnInfoListener> listener = new JNIOnInfoListener(env, thiz, weak_thiz);
228 getDrmManagerClientImpl(env, thiz)->setOnInfoListener(uniqueId, listener);
230 ALOGV("setListeners - Exit");
233 static void android_drm_DrmManagerClient_release(
234 JNIEnv* env, jobject thiz, jint uniqueId) {
235 ALOGV("release - Enter");
236 getDrmManagerClientImpl(env, thiz)->remove(uniqueId);
237 getDrmManagerClientImpl(env, thiz)->setOnInfoListener(uniqueId, NULL);
239 sp<DrmManagerClientImpl> oldClient = setDrmManagerClientImpl(env, thiz, NULL);
240 if (oldClient != NULL) {
241 oldClient->setOnInfoListener(uniqueId, NULL);
242 oldClient->removeClient(uniqueId);
244 ALOGV("release - Exit");
247 static jobject android_drm_DrmManagerClient_getConstraintsFromContent(
248 JNIEnv* env, jobject thiz, jint uniqueId, jstring jpath, jint usage) {
249 ALOGV("GetConstraints - Enter");
251 const String8 pathString = Utility::getStringValue(env, jpath);
252 DrmConstraints* pConstraints
253 = getDrmManagerClientImpl(env, thiz)->getConstraints(uniqueId, &pathString, usage);
255 jclass localRef = env->FindClass("android/content/ContentValues");
256 jmethodID ContentValues_putByteArray =
257 env->GetMethodID(localRef, "put", "(Ljava/lang/String;[B)V");
258 jmethodID ContentValues_putString =
259 env->GetMethodID(localRef, "put", "(Ljava/lang/String;Ljava/lang/String;)V");
260 jmethodID ContentValues_constructor = env->GetMethodID(localRef, "<init>", "()V");
261 jobject constraints = NULL;
263 if (NULL != localRef && NULL != pConstraints) {
264 // create the java DrmConstraints object
265 constraints = env->NewObject(localRef, ContentValues_constructor);
267 DrmConstraints::KeyIterator keyIt = pConstraints->keyIterator();
268 while (keyIt.hasNext()) {
269 String8 key = keyIt.next();
271 // insert the entry<constraintKey, constraintValue> to newly created java object
272 if (DrmConstraints::EXTENDED_METADATA == key) {
273 const char* value = pConstraints->getAsByteArray(&key);
275 ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(strlen(value)));
276 ScopedLocalRef<jstring> keyString(env, env->NewStringUTF(key.string()));
277 env->SetByteArrayRegion(dataArray.get(), 0, strlen(value), (jbyte*)value);
278 env->CallVoidMethod(constraints, ContentValues_putByteArray,
279 keyString.get(), dataArray.get());
282 String8 value = pConstraints->get(key);
283 ScopedLocalRef<jstring> keyString(env, env->NewStringUTF(key.string()));
284 ScopedLocalRef<jstring> valueString(env, env->NewStringUTF(value.string()));
285 env->CallVoidMethod(constraints, ContentValues_putString,
286 keyString.get(), valueString.get());
291 delete pConstraints; pConstraints = NULL;
292 ALOGV("GetConstraints - Exit");
296 static jobject android_drm_DrmManagerClient_getMetadataFromContent(
297 JNIEnv* env, jobject thiz, jint uniqueId, jstring jpath) {
298 ALOGV("GetMetadata - Enter");
299 const String8 pathString = Utility::getStringValue(env, jpath);
300 DrmMetadata* pMetadata =
301 getDrmManagerClientImpl(env, thiz)->getMetadata(uniqueId, &pathString);
303 jobject metadata = NULL;
305 jclass localRef = env->FindClass("android/content/ContentValues");
306 jmethodID ContentValues_putString =
307 env->GetMethodID(localRef, "put", "(Ljava/lang/String;Ljava/lang/String;)V");
309 if (NULL != localRef && NULL != pMetadata) {
310 // Get the constructor id
311 jmethodID constructorId = NULL;
312 constructorId = env->GetMethodID(localRef, "<init>", "()V");
313 if (NULL != constructorId) {
314 // create the java DrmMetadata object
315 metadata = env->NewObject(localRef, constructorId);
316 if (NULL != metadata) {
317 DrmMetadata::KeyIterator keyIt = pMetadata->keyIterator();
318 while (keyIt.hasNext()) {
319 String8 key = keyIt.next();
320 // insert the entry<constraintKey, constraintValue>
321 // to newly created java object
322 String8 value = pMetadata->get(key);
323 ScopedLocalRef<jstring> keyString(env, env->NewStringUTF(key.string()));
324 ScopedLocalRef<jstring> valueString(env, env->NewStringUTF(value.string()));
325 env->CallVoidMethod(metadata, ContentValues_putString,
326 keyString.get(), valueString.get());
331 delete pMetadata; pMetadata = NULL;
332 ALOGV("GetMetadata - Exit");
336 static jobjectArray android_drm_DrmManagerClient_getAllSupportInfo(
337 JNIEnv* env, jobject thiz, jint uniqueId) {
338 ALOGV("GetAllSupportInfo - Enter");
339 DrmSupportInfo* drmSupportInfoArray = NULL;
342 getDrmManagerClientImpl(env, thiz)->getAllSupportInfo(uniqueId, &length, &drmSupportInfoArray);
344 jclass clazz = env->FindClass("android/drm/DrmSupportInfo");
346 jobjectArray array = (jobjectArray)env->NewObjectArray(length, clazz, NULL);
348 for (int i = 0; i < length; i++) {
349 DrmSupportInfo info = drmSupportInfoArray[i];
351 jobject drmSupportInfo = env->NewObject(clazz, env->GetMethodID(clazz, "<init>", "()V"));
353 jmethodID addMimeTypeId
354 = env->GetMethodID(clazz, "addMimeType", "(Ljava/lang/String;)V");
355 jmethodID addFileSuffixId
356 = env->GetMethodID(clazz, "addFileSuffix", "(Ljava/lang/String;)V");
359 drmSupportInfo, env->GetMethodID(clazz, "setDescription", "(Ljava/lang/String;)V"),
360 env->NewStringUTF(info.getDescription().string()));
362 DrmSupportInfo::MimeTypeIterator iterator = info.getMimeTypeIterator();
363 while (iterator.hasNext()) {
364 String8 value = iterator.next();
365 env->CallVoidMethod(drmSupportInfo, addMimeTypeId, env->NewStringUTF(value.string()));
368 DrmSupportInfo::FileSuffixIterator it = info.getFileSuffixIterator();
369 while (it.hasNext()) {
370 String8 value = it.next();
372 drmSupportInfo, addFileSuffixId, env->NewStringUTF(value.string()));
375 env->SetObjectArrayElement(array, i, drmSupportInfo);
378 delete [] drmSupportInfoArray; drmSupportInfoArray = NULL;
379 ALOGV("GetAllSupportInfo - Exit");
383 static void android_drm_DrmManagerClient_installDrmEngine(
384 JNIEnv* /* env */, jobject /* thiz */, jint /* uniqueId */,
385 jstring /* engineFilePath */) {
386 ALOGV("installDrmEngine - Enter");
387 //getDrmManagerClient(env, thiz)
388 // ->installDrmEngine(uniqueId, Utility::getStringValue(env, engineFilePath));
389 ALOGV("installDrmEngine - Exit");
392 static jint android_drm_DrmManagerClient_saveRights(
393 JNIEnv* env, jobject thiz, jint uniqueId,
394 jobject drmRights, jstring rightsPath, jstring contentPath) {
395 ALOGV("saveRights - Enter");
396 int result = DRM_ERROR_UNKNOWN;
398 char* mData = Utility::getByteArrayValue(env, drmRights, "mData", &dataLength);
401 DrmRights rights(DrmBuffer(mData, dataLength),
402 Utility::getStringValue(env, drmRights, "mMimeType"),
403 Utility::getStringValue(env, drmRights, "mAccountId"),
404 Utility::getStringValue(env, drmRights, "mSubscriptionId"));
405 result = getDrmManagerClientImpl(env, thiz)
406 ->saveRights(uniqueId, rights, Utility::getStringValue(env, rightsPath),
407 Utility::getStringValue(env, contentPath));
410 delete[] mData; mData = NULL;
411 ALOGV("saveRights - Exit");
412 return static_cast<jint>(result);
415 static jboolean android_drm_DrmManagerClient_canHandle(
416 JNIEnv* env, jobject thiz, jint uniqueId, jstring path, jstring mimeType) {
417 ALOGV("canHandle - Enter");
419 = getDrmManagerClientImpl(env, thiz)
420 ->canHandle(uniqueId, Utility::getStringValue(env, path),
421 Utility::getStringValue(env, mimeType));
422 ALOGV("canHandle - Exit");
426 static jobject android_drm_DrmManagerClient_processDrmInfo(
427 JNIEnv* env, jobject thiz, jint uniqueId, jobject drmInfoObject) {
428 ALOGV("processDrmInfo - Enter");
430 const String8 mMimeType = Utility::getStringValue(env, drmInfoObject, "mMimeType");
431 char* mData = Utility::getByteArrayValue(env, drmInfoObject, "mData", &dataLength);
432 int mInfoType = Utility::getIntValue(env, drmInfoObject, "mInfoType");
434 const DrmBuffer buffer(mData, dataLength);
435 DrmInfo drmInfo(mInfoType, buffer, mMimeType);
437 jclass clazz = env->FindClass("android/drm/DrmInfo");
438 jmethodID DrmInfo_get = env->GetMethodID(clazz, "get", "(Ljava/lang/String;)Ljava/lang/Object;");
440 = env->CallObjectMethod(drmInfoObject,
441 env->GetMethodID(clazz, "keyIterator", "()Ljava/util/Iterator;"));
443 jclass Iterator_class = env->FindClass("java/util/Iterator");
444 jmethodID Iterator_hasNext = env->GetMethodID(Iterator_class, "hasNext", "()Z");
445 jmethodID Iterator_next = env->GetMethodID(Iterator_class, "next", "()Ljava/lang/Object;");
447 jclass Object_class = env->FindClass("java/lang/Object");
448 jmethodID Object_toString = env->GetMethodID(Object_class, "toString", "()Ljava/lang/String;");
450 while (env->CallBooleanMethod(keyIterator, Iterator_hasNext)) {
451 ScopedLocalRef<jstring> key(env,
452 (jstring) env->CallObjectMethod(keyIterator, Iterator_next));
453 ScopedLocalRef<jobject> valueObject(env,
454 env->CallObjectMethod(drmInfoObject, DrmInfo_get, key.get()));
455 ScopedLocalRef<jstring> valString(env, NULL);
456 if (NULL != valueObject.get()) {
457 valString.reset((jstring) env->CallObjectMethod(valueObject.get(), Object_toString));
460 String8 keyString = Utility::getStringValue(env, key.get());
461 String8 valueString = Utility::getStringValue(env, valString.get());
462 ALOGV("Key: %s | Value: %s", keyString.string(), valueString.string());
464 drmInfo.put(keyString, valueString);
467 DrmInfoStatus* pDrmInfoStatus
468 = getDrmManagerClientImpl(env, thiz)->processDrmInfo(uniqueId, &drmInfo);
470 jclass localRef = env->FindClass("android/drm/DrmInfoStatus");
471 jobject drmInfoStatus = NULL;
473 if (NULL != localRef && NULL != pDrmInfoStatus) {
474 int statusCode = pDrmInfoStatus->statusCode;
475 int infoType = pDrmInfoStatus->infoType;
477 jbyteArray dataArray = NULL;
478 if (NULL != pDrmInfoStatus->drmBuffer) {
479 int length = pDrmInfoStatus->drmBuffer->length;
480 dataArray = env->NewByteArray(length);
481 env->SetByteArrayRegion(
482 dataArray, 0, length, (jbyte*) pDrmInfoStatus->drmBuffer->data);
484 delete [] pDrmInfoStatus->drmBuffer->data;
485 delete pDrmInfoStatus->drmBuffer; pDrmInfoStatus->drmBuffer = NULL;
487 jclass clazz = env->FindClass("android/drm/ProcessedData");
488 jmethodID constructorId
489 = env->GetMethodID(clazz, "<init>", "([BLjava/lang/String;Ljava/lang/String;)V");
490 jobject processedData = env->NewObject(clazz, constructorId, dataArray,
491 env->NewStringUTF((drmInfo.get(DrmInfoRequest::ACCOUNT_ID)).string()),
492 env->NewStringUTF((drmInfo.get(DrmInfoRequest::SUBSCRIPTION_ID)).string()));
495 = env->GetMethodID(localRef,
496 "<init>", "(IILandroid/drm/ProcessedData;Ljava/lang/String;)V");
498 drmInfoStatus = env->NewObject(localRef, constructorId, statusCode, infoType,
499 processedData, env->NewStringUTF(pDrmInfoStatus->mimeType.string()));
502 delete[] mData; mData = NULL;
503 delete pDrmInfoStatus; pDrmInfoStatus = NULL;
505 ALOGV("processDrmInfo - Exit");
506 return drmInfoStatus;
509 static jobject android_drm_DrmManagerClient_acquireDrmInfo(
510 JNIEnv* env, jobject thiz, jint uniqueId, jobject drmInfoRequest) {
511 ALOGV("acquireDrmInfo Enter");
512 const String8 mMimeType = Utility::getStringValue(env, drmInfoRequest, "mMimeType");
513 int mInfoType = Utility::getIntValue(env, drmInfoRequest, "mInfoType");
515 DrmInfoRequest drmInfoReq(mInfoType, mMimeType);
517 jclass clazz = env->FindClass("android/drm/DrmInfoRequest");
519 = env->CallObjectMethod(drmInfoRequest,
520 env->GetMethodID(clazz, "keyIterator", "()Ljava/util/Iterator;"));
521 jmethodID DrmInfoRequest_get = env->GetMethodID(clazz,
522 "get", "(Ljava/lang/String;)Ljava/lang/Object;");
524 jclass Iterator_class = env->FindClass("java/util/Iterator");
525 jmethodID Iterator_hasNext = env->GetMethodID(Iterator_class, "hasNext", "()Z");
526 jmethodID Iterator_next = env->GetMethodID(Iterator_class, "next", "()Ljava/lang/Object;");
528 while (env->CallBooleanMethod(keyIterator, Iterator_hasNext)) {
529 ScopedLocalRef<jstring> key(env,
530 (jstring) env->CallObjectMethod(keyIterator, Iterator_next));
531 ScopedLocalRef<jstring> value(env,
532 (jstring) env->CallObjectMethod(drmInfoRequest, DrmInfoRequest_get, key.get()));
534 String8 keyString = Utility::getStringValue(env, key.get());
535 String8 valueString = Utility::getStringValue(env, value.get());
536 ALOGV("Key: %s | Value: %s", keyString.string(), valueString.string());
538 drmInfoReq.put(keyString, valueString);
541 DrmInfo* pDrmInfo = getDrmManagerClientImpl(env, thiz)->acquireDrmInfo(uniqueId, &drmInfoReq);
543 jobject drmInfoObject = NULL;
545 if (NULL != pDrmInfo) {
546 jclass localRef = env->FindClass("android/drm/DrmInfo");
548 if (NULL != localRef) {
549 int length = pDrmInfo->getData().length;
551 jbyteArray dataArray = env->NewByteArray(length);
552 env->SetByteArrayRegion(dataArray, 0, length, (jbyte*)pDrmInfo->getData().data);
555 = env->NewObject(localRef,
556 env->GetMethodID(localRef, "<init>", "(I[BLjava/lang/String;)V"),
557 mInfoType, dataArray, env->NewStringUTF(pDrmInfo->getMimeType().string()));
559 DrmInfo::KeyIterator it = pDrmInfo->keyIterator();
560 jmethodID putMethodId
561 = env->GetMethodID(localRef, "put", "(Ljava/lang/String;Ljava/lang/Object;)V");
563 while (it.hasNext()) {
564 String8 key = it.next();
565 String8 value = pDrmInfo->get(key);
566 ScopedLocalRef<jstring> keyString(env, env->NewStringUTF(key.string()));
567 ScopedLocalRef<jstring> valueString(env, env->NewStringUTF(value.string()));
568 env->CallVoidMethod(drmInfoObject, putMethodId,
569 keyString.get(), valueString.get());
572 delete [] pDrmInfo->getData().data;
575 delete pDrmInfo; pDrmInfo = NULL;
577 ALOGV("acquireDrmInfo Exit");
578 return drmInfoObject;
581 static jint android_drm_DrmManagerClient_getDrmObjectType(
582 JNIEnv* env, jobject thiz, jint uniqueId, jstring path, jstring mimeType) {
583 ALOGV("getDrmObjectType Enter");
585 = getDrmManagerClientImpl(env, thiz)
586 ->getDrmObjectType(uniqueId, Utility::getStringValue(env, path),
587 Utility::getStringValue(env, mimeType));
588 ALOGV("getDrmObjectType Exit");
589 return static_cast<jint>(drmObjectType);
592 static jstring android_drm_DrmManagerClient_getOriginalMimeType(
593 JNIEnv* env, jobject thiz, jint uniqueId, jstring path, jobject fileDescriptor) {
594 ALOGV("getOriginalMimeType Enter");
596 int fd = (fileDescriptor == NULL)
598 : jniGetFDFromFileDescriptor(env, fileDescriptor);
601 = getDrmManagerClientImpl(env, thiz)
602 ->getOriginalMimeType(uniqueId,
603 Utility::getStringValue(env, path), fd);
604 ALOGV("getOriginalMimeType Exit");
605 return env->NewStringUTF(mimeType.string());
608 static jint android_drm_DrmManagerClient_checkRightsStatus(
609 JNIEnv* env, jobject thiz, jint uniqueId, jstring path, int action) {
610 ALOGV("checkRightsStatus Enter");
612 = getDrmManagerClientImpl(env, thiz)
613 ->checkRightsStatus(uniqueId, Utility::getStringValue(env, path), action);
614 ALOGV("checkRightsStatus Exit");
615 return static_cast<jint>(rightsStatus);
618 static jint android_drm_DrmManagerClient_removeRights(
619 JNIEnv* env, jobject thiz, jint uniqueId, jstring path) {
620 ALOGV("removeRights");
621 return static_cast<jint>(getDrmManagerClientImpl(env, thiz)
622 ->removeRights(uniqueId, Utility::getStringValue(env, path)));
625 static jint android_drm_DrmManagerClient_removeAllRights(
626 JNIEnv* env, jobject thiz, jint uniqueId) {
627 ALOGV("removeAllRights");
628 return static_cast<jint>(getDrmManagerClientImpl(env, thiz)
629 ->removeAllRights(uniqueId));
632 static jint android_drm_DrmManagerClient_openConvertSession(
633 JNIEnv* env, jobject thiz, jint uniqueId, jstring mimeType) {
634 ALOGV("openConvertSession Enter");
636 = getDrmManagerClientImpl(env, thiz)
637 ->openConvertSession(uniqueId, Utility::getStringValue(env, mimeType));
638 ALOGV("openConvertSession Exit");
639 return static_cast<jint>(convertId);
642 static jobject GetConvertedStatus(JNIEnv* env, DrmConvertedStatus* pDrmConvertedStatus) {
643 ALOGV("GetConvertedStatus - Enter");
644 jclass localRef = env->FindClass("android/drm/DrmConvertedStatus");
646 jobject drmConvertedStatus = NULL;
648 if (NULL != localRef && NULL != pDrmConvertedStatus) {
649 int statusCode = pDrmConvertedStatus->statusCode;
651 jbyteArray dataArray = NULL;
652 if (NULL != pDrmConvertedStatus->convertedData) {
653 int length = pDrmConvertedStatus->convertedData->length;
654 dataArray = env->NewByteArray(length);
655 env->SetByteArrayRegion(
656 dataArray, 0, length, (jbyte*) pDrmConvertedStatus->convertedData->data);
658 delete [] pDrmConvertedStatus->convertedData->data;
659 delete pDrmConvertedStatus->convertedData; pDrmConvertedStatus->convertedData = NULL;
661 jmethodID constructorId = env->GetMethodID(localRef, "<init>", "(I[BI)V");
663 = env->NewObject(localRef, constructorId,
664 statusCode, dataArray, pDrmConvertedStatus->offset);
667 delete pDrmConvertedStatus; pDrmConvertedStatus = NULL;
669 ALOGV("GetConvertedStatus - Exit");
670 return drmConvertedStatus;
673 static jobject android_drm_DrmManagerClient_convertData(
674 JNIEnv* env, jobject thiz, jint uniqueId, jint convertId, jbyteArray inputData) {
675 ALOGV("convertData Enter");
678 char* mData = Utility::getByteArrayValue(env, inputData, &dataLength);
679 const DrmBuffer buffer(mData, dataLength);
681 DrmConvertedStatus* pDrmConvertedStatus
682 = getDrmManagerClientImpl(env, thiz)->convertData(uniqueId, convertId, &buffer);
683 jobject status = GetConvertedStatus(env, pDrmConvertedStatus);
688 ALOGV("convertData - Exit");
692 static jobject android_drm_DrmManagerClient_closeConvertSession(
693 JNIEnv* env, jobject thiz, jint uniqueId, jint convertId) {
695 ALOGV("closeConvertSession Enter");
697 DrmConvertedStatus* pDrmConvertedStatus
698 = getDrmManagerClientImpl(env, thiz)->closeConvertSession(uniqueId, convertId);
699 jobject status = GetConvertedStatus(env, pDrmConvertedStatus);
701 ALOGV("closeConvertSession - Exit");
705 static const JNINativeMethod nativeMethods[] = {
707 {"_initialize", "()I",
708 (void*)android_drm_DrmManagerClient_initialize},
710 {"_setListeners", "(ILjava/lang/Object;)V",
711 (void*)android_drm_DrmManagerClient_setListeners},
714 (void*)android_drm_DrmManagerClient_release},
716 {"_getConstraints", "(ILjava/lang/String;I)Landroid/content/ContentValues;",
717 (void*)android_drm_DrmManagerClient_getConstraintsFromContent},
719 {"_getMetadata", "(ILjava/lang/String;)Landroid/content/ContentValues;",
720 (void*)android_drm_DrmManagerClient_getMetadataFromContent},
722 {"_getAllSupportInfo", "(I)[Landroid/drm/DrmSupportInfo;",
723 (void*)android_drm_DrmManagerClient_getAllSupportInfo},
725 {"_installDrmEngine", "(ILjava/lang/String;)V",
726 (void*)android_drm_DrmManagerClient_installDrmEngine},
728 {"_canHandle", "(ILjava/lang/String;Ljava/lang/String;)Z",
729 (void*)android_drm_DrmManagerClient_canHandle},
731 {"_processDrmInfo", "(ILandroid/drm/DrmInfo;)Landroid/drm/DrmInfoStatus;",
732 (void*)android_drm_DrmManagerClient_processDrmInfo},
734 {"_acquireDrmInfo", "(ILandroid/drm/DrmInfoRequest;)Landroid/drm/DrmInfo;",
735 (void*)android_drm_DrmManagerClient_acquireDrmInfo},
737 {"_saveRights", "(ILandroid/drm/DrmRights;Ljava/lang/String;Ljava/lang/String;)I",
738 (void*)android_drm_DrmManagerClient_saveRights},
740 {"_getDrmObjectType", "(ILjava/lang/String;Ljava/lang/String;)I",
741 (void*)android_drm_DrmManagerClient_getDrmObjectType},
743 {"_getOriginalMimeType", "(ILjava/lang/String;Ljava/io/FileDescriptor;)Ljava/lang/String;",
744 (void*)android_drm_DrmManagerClient_getOriginalMimeType},
746 {"_checkRightsStatus", "(ILjava/lang/String;I)I",
747 (void*)android_drm_DrmManagerClient_checkRightsStatus},
749 {"_removeRights", "(ILjava/lang/String;)I",
750 (void*)android_drm_DrmManagerClient_removeRights},
752 {"_removeAllRights", "(I)I",
753 (void*)android_drm_DrmManagerClient_removeAllRights},
755 {"_openConvertSession", "(ILjava/lang/String;)I",
756 (void*)android_drm_DrmManagerClient_openConvertSession},
758 {"_convertData", "(II[B)Landroid/drm/DrmConvertedStatus;",
759 (void*)android_drm_DrmManagerClient_convertData},
761 {"_closeConvertSession", "(II)Landroid/drm/DrmConvertedStatus;",
762 (void*)android_drm_DrmManagerClient_closeConvertSession},
765 static int registerNativeMethods(JNIEnv* env) {
768 /* look up the class */
769 jclass clazz = env->FindClass("android/drm/DrmManagerClient");
772 if (env->RegisterNatives(clazz, nativeMethods, sizeof(nativeMethods)
773 / sizeof(nativeMethods[0])) == JNI_OK) {
780 jint JNI_OnLoad(JavaVM* vm, void* /* reserved */) {
784 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK) {
785 if (NULL != env && registerNativeMethods(env) == 0) {
786 result = JNI_VERSION_1_4;