Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4354a960 authored by Robert Shih's avatar Robert Shih
Browse files

Move nativeToJavaPersistableBundle to MediaMetricsJNI

Helper method can be shared between MediaDrm/MediaCrypto

Bug: 141811737
Test: MediaDrmMetricsTest
Change-Id: Iee764e24847e3a9c32a9d3241031b3642cfaf6eb
parent 0f442f72
Loading
Loading
Loading
Loading
+1 −43
Original line number Diff line number Diff line
@@ -211,48 +211,6 @@ static fields_t gFields;

namespace {

// Helper function to convert a native PersistableBundle to a Java
// PersistableBundle.
jobject nativeToJavaPersistableBundle(JNIEnv *env, jobject thiz,
                                      PersistableBundle* nativeBundle) {
    if (env == NULL || thiz == NULL || nativeBundle == NULL) {
        ALOGE("Unexpected NULL parmeter");
        return NULL;
    }

    // Create a Java parcel with the native parcel data.
    // Then create a new PersistableBundle with that parcel as a parameter.
    jobject jParcel = android::createJavaParcelObject(env);
    if (jParcel == NULL) {
      ALOGE("Failed to create a Java Parcel.");
      return NULL;
    }

    android::Parcel* nativeParcel = android::parcelForJavaObject(env, jParcel);
    if (nativeParcel == NULL) {
      ALOGE("Failed to get the native Parcel.");
      return NULL;
    }

    android::status_t result = nativeBundle->writeToParcel(nativeParcel);
    nativeParcel->setDataPosition(0);
    if (result != android::OK) {
      ALOGE("Failed to write nativeBundle to Parcel: %d.", result);
      return NULL;
    }

    jobject newBundle = env->CallObjectMethod(gFields.bundleCreator,
                                              gFields.createFromParcelId,
                                              jParcel);
    if (newBundle == NULL) {
        ALOGE("Failed to create a new PersistableBundle "
              "from the createFromParcel call.");
    }

    return newBundle;
}


jbyteArray hidlVectorToJByteArray(const hardware::hidl_vec<uint8_t> &vector) {
    JNIEnv *env = AndroidRuntime::getJNIEnv();
    size_t length = vector.size();
@@ -1937,7 +1895,7 @@ android_media_MediaDrm_native_getMetrics(JNIEnv *env, jobject thiz)
        return (jobject) NULL;
    }

    return nativeToJavaPersistableBundle(env, thiz, &metrics);
    return MediaMetricsJNI::nativeToJavaPersistableBundle(env, &metrics);
}

static jbyteArray android_media_MediaDrm_signRSANative(
+69 −0
Original line number Diff line number Diff line
@@ -20,7 +20,9 @@
#include <nativehelper/JNIHelp.h>

#include "android_media_MediaMetricsJNI.h"
#include "android_os_Parcel.h"
#include <media/MediaAnalyticsItem.h>
#include <binder/Parcel.h>


// This source file is compiled and linked into:
@@ -223,5 +225,72 @@ jobject MediaMetricsJNI::writeAttributesToBundle(JNIEnv* env, jobject mybundle,
    return NULL;
}

// Helper function to convert a native PersistableBundle to a Java
// PersistableBundle.
jobject MediaMetricsJNI::nativeToJavaPersistableBundle(JNIEnv *env,
                                                       os::PersistableBundle* nativeBundle) {
    if (env == NULL || nativeBundle == NULL) {
        ALOGE("Unexpected NULL parmeter");
        return NULL;
    }

    // Create a Java parcel with the native parcel data.
    // Then create a new PersistableBundle with that parcel as a parameter.
    jobject jParcel = android::createJavaParcelObject(env);
    if (jParcel == NULL) {
      ALOGE("Failed to create a Java Parcel.");
      return NULL;
    }

    android::Parcel* nativeParcel = android::parcelForJavaObject(env, jParcel);
    if (nativeParcel == NULL) {
      ALOGE("Failed to get the native Parcel.");
      return NULL;
    }

    android::status_t result = nativeBundle->writeToParcel(nativeParcel);
    nativeParcel->setDataPosition(0);
    if (result != android::OK) {
      ALOGE("Failed to write nativeBundle to Parcel: %d.", result);
      return NULL;
    }

#define STATIC_INIT_JNI(T, obj, method, globalref, ...) \
    static T obj{};\
    if (obj == NULL) { \
        obj = method(__VA_ARGS__); \
        if (obj == NULL) { \
            ALOGE("%s can't find " #obj, __func__); \
            return NULL; \
        } else { \
            obj = globalref; \
        }\
    } \

    STATIC_INIT_JNI(jclass, clazzBundle, env->FindClass,
            static_cast<jclass>(env->NewGlobalRef(clazzBundle)),
            "android/os/PersistableBundle");
    STATIC_INIT_JNI(jfieldID, bundleCreatorId, env->GetStaticFieldID,
            bundleCreatorId,
            clazzBundle, "CREATOR", "Landroid/os/Parcelable$Creator;");
    STATIC_INIT_JNI(jobject, bundleCreator, env->GetStaticObjectField,
            env->NewGlobalRef(bundleCreator),
            clazzBundle, bundleCreatorId);
    STATIC_INIT_JNI(jclass, clazzCreator, env->FindClass,
            static_cast<jclass>(env->NewGlobalRef(clazzCreator)),
            "android/os/Parcelable$Creator");
    STATIC_INIT_JNI(jmethodID, createFromParcelId, env->GetMethodID,
            createFromParcelId,
            clazzCreator, "createFromParcel", "(Landroid/os/Parcel;)Ljava/lang/Object;");

    jobject newBundle = env->CallObjectMethod(bundleCreator, createFromParcelId, jParcel);
    if (newBundle == NULL) {
        ALOGE("Failed to create a new PersistableBundle "
              "from the createFromParcel call.");
    }

    return newBundle;
}

};  // namespace android
+2 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <jni.h>
#include <nativehelper/JNIHelp.h>
#include <media/MediaAnalyticsItem.h>
#include <binder/PersistableBundle.h>

// Copeid from core/jni/ (libandroid_runtime.so)
namespace android {
@@ -28,6 +29,7 @@ class MediaMetricsJNI {
public:
    static jobject writeMetricsToBundle(JNIEnv* env, MediaAnalyticsItem *item, jobject mybundle);
    static jobject writeAttributesToBundle(JNIEnv* env, jobject mybundle, char *buffer, size_t length);
    static jobject nativeToJavaPersistableBundle(JNIEnv*, os::PersistableBundle*);
};

};  // namespace android