blob: 38f7a7e253896746c04b31b033df6ab99e23230a [file] [log] [blame]
Ray Essick0e0fee12017-01-25 18:01:56 -08001/*
2 * Copyright 2017, The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <android_runtime/AndroidRuntime.h>
18#include <jni.h>
Steven Moreland60cc6c02017-08-25 15:49:25 -070019#include <nativehelper/JNIHelp.h>
Ray Essick0e0fee12017-01-25 18:01:56 -080020
21#include "android_media_MediaMetricsJNI.h"
22#include <media/MediaAnalyticsItem.h>
23
24
25namespace android {
26
Ray Essick10353e32017-04-14 10:22:55 -070027// place the attributes into a java PersistableBundle object
Ray Essick0e0fee12017-01-25 18:01:56 -080028jobject MediaMetricsJNI::writeMetricsToBundle(JNIEnv* env, MediaAnalyticsItem *item, jobject mybundle) {
29
Ray Essick10353e32017-04-14 10:22:55 -070030 jclass clazzBundle = env->FindClass("android/os/PersistableBundle");
Ray Essick0e0fee12017-01-25 18:01:56 -080031 if (clazzBundle==NULL) {
Ray Essick10353e32017-04-14 10:22:55 -070032 ALOGD("can't find android/os/PersistableBundle");
Ray Essick0e0fee12017-01-25 18:01:56 -080033 return NULL;
34 }
35 // sometimes the caller provides one for us to fill
36 if (mybundle == NULL) {
37 // create the bundle
38 jmethodID constructID = env->GetMethodID(clazzBundle, "<init>", "()V");
39 mybundle = env->NewObject(clazzBundle, constructID);
40 if (mybundle == NULL) {
41 return NULL;
42 }
43 }
44
45 // grab methods that we can invoke
46 jmethodID setIntID = env->GetMethodID(clazzBundle, "putInt", "(Ljava/lang/String;I)V");
47 jmethodID setLongID = env->GetMethodID(clazzBundle, "putLong", "(Ljava/lang/String;J)V");
48 jmethodID setDoubleID = env->GetMethodID(clazzBundle, "putDouble", "(Ljava/lang/String;D)V");
49 jmethodID setStringID = env->GetMethodID(clazzBundle, "putString", "(Ljava/lang/String;Ljava/lang/String;)V");
50
51 // env, class, method, {parms}
52 //env->CallVoidMethod(env, mybundle, setIntID, jstr, jint);
53
54 // iterate through my attributes
55 // -- get name, get type, get value
56 // -- insert appropriately into the bundle
57 for (size_t i = 0 ; i < item->mPropCount; i++ ) {
Ray Essick10353e32017-04-14 10:22:55 -070058 MediaAnalyticsItem::Prop *prop = &item->mProps[i];
Ray Essick0e0fee12017-01-25 18:01:56 -080059 // build the key parameter from prop->mName
60 jstring keyName = env->NewStringUTF(prop->mName);
61 // invoke the appropriate method to insert
62 switch (prop->mType) {
63 case MediaAnalyticsItem::kTypeInt32:
64 env->CallVoidMethod(mybundle, setIntID,
65 keyName, (jint) prop->u.int32Value);
66 break;
67 case MediaAnalyticsItem::kTypeInt64:
68 env->CallVoidMethod(mybundle, setLongID,
69 keyName, (jlong) prop->u.int64Value);
70 break;
71 case MediaAnalyticsItem::kTypeDouble:
72 env->CallVoidMethod(mybundle, setDoubleID,
73 keyName, (jdouble) prop->u.doubleValue);
74 break;
75 case MediaAnalyticsItem::kTypeCString:
76 env->CallVoidMethod(mybundle, setStringID, keyName,
77 env->NewStringUTF(prop->u.CStringValue));
78 break;
79 default:
80 ALOGE("to_String bad item type: %d for %s",
81 prop->mType, prop->mName);
82 break;
83 }
84 }
85
86 return mybundle;
87}
88
89}; // namespace android
90