1 /* 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> 19 #include <JNIHelp.h> 20 21 #include "android_media_MediaMetricsJNI.h" 22 #include <media/MediaAnalyticsItem.h> 23 24 25 namespace android { 26 27 // place the attributes into a java PersistableBundle object 28 jobject MediaMetricsJNI::writeMetricsToBundle(JNIEnv* env, MediaAnalyticsItem *item, jobject mybundle) { 29 30 jclass clazzBundle = env->FindClass("android/os/PersistableBundle"); 31 if (clazzBundle==NULL) { 32 ALOGD("can't find android/os/PersistableBundle"); 33 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++ ) { 58 MediaAnalyticsItem::Prop *prop = &item->mProps[i]; 59 // 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 91