Ray Essick | 0e0fee1 | 2017-01-25 18:01:56 -0800 | [diff] [blame^] | 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 Bundle object |
| 28 | // decide whether this is appropriately scoped here. |
| 29 | // if we do it somewhere else, we have to figure a "give me all the attrs" |
| 30 | // access to the inside of MediaAnalyticsItem |
| 31 | jobject MediaMetricsJNI::writeMetricsToBundle(JNIEnv* env, MediaAnalyticsItem *item, jobject mybundle) { |
| 32 | |
| 33 | jclass clazzBundle = env->FindClass("android/os/Bundle"); |
| 34 | if (clazzBundle==NULL) { |
| 35 | ALOGD("can't find android/os/Bundle"); |
| 36 | return NULL; |
| 37 | } |
| 38 | // sometimes the caller provides one for us to fill |
| 39 | if (mybundle == NULL) { |
| 40 | // create the bundle |
| 41 | jmethodID constructID = env->GetMethodID(clazzBundle, "<init>", "()V"); |
| 42 | mybundle = env->NewObject(clazzBundle, constructID); |
| 43 | if (mybundle == NULL) { |
| 44 | return NULL; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // grab methods that we can invoke |
| 49 | jmethodID setIntID = env->GetMethodID(clazzBundle, "putInt", "(Ljava/lang/String;I)V"); |
| 50 | jmethodID setLongID = env->GetMethodID(clazzBundle, "putLong", "(Ljava/lang/String;J)V"); |
| 51 | jmethodID setDoubleID = env->GetMethodID(clazzBundle, "putDouble", "(Ljava/lang/String;D)V"); |
| 52 | jmethodID setStringID = env->GetMethodID(clazzBundle, "putString", "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 53 | |
| 54 | // env, class, method, {parms} |
| 55 | //env->CallVoidMethod(env, mybundle, setIntID, jstr, jint); |
| 56 | |
| 57 | // iterate through my attributes |
| 58 | // -- get name, get type, get value |
| 59 | // -- insert appropriately into the bundle |
| 60 | for (size_t i = 0 ; i < item->mPropCount; i++ ) { |
| 61 | MediaAnalyticsItem::Prop *prop = &item->mProps[i]; |
| 62 | // build the key parameter from prop->mName |
| 63 | jstring keyName = env->NewStringUTF(prop->mName); |
| 64 | // invoke the appropriate method to insert |
| 65 | switch (prop->mType) { |
| 66 | case MediaAnalyticsItem::kTypeInt32: |
| 67 | env->CallVoidMethod(mybundle, setIntID, |
| 68 | keyName, (jint) prop->u.int32Value); |
| 69 | break; |
| 70 | case MediaAnalyticsItem::kTypeInt64: |
| 71 | env->CallVoidMethod(mybundle, setLongID, |
| 72 | keyName, (jlong) prop->u.int64Value); |
| 73 | break; |
| 74 | case MediaAnalyticsItem::kTypeDouble: |
| 75 | env->CallVoidMethod(mybundle, setDoubleID, |
| 76 | keyName, (jdouble) prop->u.doubleValue); |
| 77 | break; |
| 78 | case MediaAnalyticsItem::kTypeCString: |
| 79 | env->CallVoidMethod(mybundle, setStringID, keyName, |
| 80 | env->NewStringUTF(prop->u.CStringValue)); |
| 81 | break; |
| 82 | default: |
| 83 | ALOGE("to_String bad item type: %d for %s", |
| 84 | prop->mType, prop->mName); |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return mybundle; |
| 90 | } |
| 91 | |
| 92 | }; // namespace android |
| 93 | |