Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [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 | |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 17 | #define LOG_TAG "MediaMetricsJNI" |
| 18 | |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 19 | #include <binder/Parcel.h> |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 20 | #include <jni.h> |
Ray Essick | 81fbc5b | 2019-12-07 06:24:59 -0800 | [diff] [blame^] | 21 | #include <media/MediaMetricsItem.h> |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 22 | #include <nativehelper/JNIHelp.h> |
| 23 | |
| 24 | #include "android_media_MediaMetricsJNI.h" |
Robert Shih | 4354a96 | 2019-11-10 12:09:08 -0800 | [diff] [blame] | 25 | #include "android_os_Parcel.h" |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 26 | |
Marco Nelissen | a9a802f | 2019-09-24 09:27:22 -0700 | [diff] [blame] | 27 | // This source file is compiled and linked into: |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 28 | // core/jni/ (libandroid_runtime.so) |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 29 | |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 32 | namespace { |
| 33 | struct BundleHelper { |
| 34 | BundleHelper(JNIEnv* _env, jobject _bundle) |
| 35 | : env(_env) |
| 36 | , clazzBundle(env->FindClass("android/os/PersistableBundle")) |
| 37 | , putIntID(env->GetMethodID(clazzBundle, "putInt", "(Ljava/lang/String;I)V")) |
| 38 | , putLongID(env->GetMethodID(clazzBundle, "putLong", "(Ljava/lang/String;J)V")) |
| 39 | , putDoubleID(env->GetMethodID(clazzBundle, "putDouble", "(Ljava/lang/String;D)V")) |
| 40 | , putStringID(env->GetMethodID(clazzBundle, |
| 41 | "putString", "(Ljava/lang/String;Ljava/lang/String;)V")) |
| 42 | , constructID(env->GetMethodID(clazzBundle, "<init>", "()V")) |
| 43 | , bundle(_bundle == nullptr ? env->NewObject(clazzBundle, constructID) : _bundle) |
| 44 | { } |
| 45 | |
| 46 | JNIEnv* const env; |
| 47 | const jclass clazzBundle; |
| 48 | const jmethodID putIntID; |
| 49 | const jmethodID putLongID; |
| 50 | const jmethodID putDoubleID; |
| 51 | const jmethodID putStringID; |
| 52 | const jmethodID constructID; |
| 53 | jobject const bundle; |
| 54 | |
Ray Essick | 81fbc5b | 2019-12-07 06:24:59 -0800 | [diff] [blame^] | 55 | // We use templated put to access mediametrics::Item based on data type not type enum. |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 56 | // See std::variant and std::visit. |
| 57 | template<typename T> |
| 58 | void put(jstring keyName, const T& value) = delete; |
| 59 | |
| 60 | template<> |
| 61 | void put(jstring keyName, const int32_t& value) { |
| 62 | env->CallVoidMethod(bundle, putIntID, keyName, (jint)value); |
| 63 | } |
| 64 | |
| 65 | template<> |
| 66 | void put(jstring keyName, const int64_t& value) { |
| 67 | env->CallVoidMethod(bundle, putLongID, keyName, (jlong)value); |
| 68 | } |
| 69 | |
| 70 | template<> |
| 71 | void put(jstring keyName, const double& value) { |
| 72 | env->CallVoidMethod(bundle, putDoubleID, keyName, (jdouble)value); |
| 73 | } |
| 74 | |
| 75 | template<> |
| 76 | void put(jstring keyName, const char * const& value) { |
| 77 | env->CallVoidMethod(bundle, putStringID, keyName, env->NewStringUTF(value)); |
| 78 | } |
| 79 | |
| 80 | template<> |
| 81 | void put(jstring keyName, char * const& value) { |
| 82 | env->CallVoidMethod(bundle, putStringID, keyName, env->NewStringUTF(value)); |
| 83 | } |
| 84 | |
| 85 | template<> |
| 86 | void put(jstring keyName, const std::pair<int64_t, int64_t>& value) { |
| 87 | ; // rate is currently ignored |
| 88 | } |
| 89 | |
| 90 | // We allow both jstring and non-jstring variants. |
| 91 | template<typename T> |
| 92 | void put(const char *keyName, const T& value) { |
| 93 | put(env->NewStringUTF(keyName), value); |
| 94 | } |
| 95 | }; |
| 96 | } // namespace |
| 97 | |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 98 | // place the attributes into a java PersistableBundle object |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 99 | jobject MediaMetricsJNI::writeMetricsToBundle( |
Ray Essick | 81fbc5b | 2019-12-07 06:24:59 -0800 | [diff] [blame^] | 100 | JNIEnv* env, mediametrics::Item *item, jobject bundle) |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 101 | { |
| 102 | BundleHelper bh(env, bundle); |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 103 | |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 104 | if (bh.bundle == nullptr) { |
| 105 | ALOGE("%s: unable to create Bundle", __func__); |
| 106 | return nullptr; |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 109 | bh.put("__key", item->getKey().c_str()); |
| 110 | if (item->getPid() != -1) { |
| 111 | bh.put("__pid", (int32_t)item->getPid()); |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 112 | } |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 113 | if (item->getTimestamp() > 0) { |
| 114 | bh.put("__timestamp", (int64_t)item->getTimestamp()); |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 115 | } |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 116 | if (item->getUid() != -1) { |
| 117 | bh.put("__uid", (int32_t)item->getUid()); |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 118 | } |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 119 | for (const auto &prop : *item) { |
| 120 | const char *name = prop.getName(); |
| 121 | if (name == nullptr) continue; |
| 122 | prop.visit([&] (auto &value) { bh.put(name, value); }); |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 123 | } |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 124 | return bh.bundle; |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Robert Shih | 4354a96 | 2019-11-10 12:09:08 -0800 | [diff] [blame] | 127 | // Helper function to convert a native PersistableBundle to a Java |
| 128 | // PersistableBundle. |
| 129 | jobject MediaMetricsJNI::nativeToJavaPersistableBundle(JNIEnv *env, |
| 130 | os::PersistableBundle* nativeBundle) { |
| 131 | if (env == NULL || nativeBundle == NULL) { |
| 132 | ALOGE("Unexpected NULL parmeter"); |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | // Create a Java parcel with the native parcel data. |
| 137 | // Then create a new PersistableBundle with that parcel as a parameter. |
| 138 | jobject jParcel = android::createJavaParcelObject(env); |
| 139 | if (jParcel == NULL) { |
| 140 | ALOGE("Failed to create a Java Parcel."); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | android::Parcel* nativeParcel = android::parcelForJavaObject(env, jParcel); |
| 145 | if (nativeParcel == NULL) { |
| 146 | ALOGE("Failed to get the native Parcel."); |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | android::status_t result = nativeBundle->writeToParcel(nativeParcel); |
| 151 | nativeParcel->setDataPosition(0); |
| 152 | if (result != android::OK) { |
| 153 | ALOGE("Failed to write nativeBundle to Parcel: %d.", result); |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | #define STATIC_INIT_JNI(T, obj, method, globalref, ...) \ |
| 158 | static T obj{};\ |
| 159 | if (obj == NULL) { \ |
| 160 | obj = method(__VA_ARGS__); \ |
| 161 | if (obj == NULL) { \ |
| 162 | ALOGE("%s can't find " #obj, __func__); \ |
| 163 | return NULL; \ |
| 164 | } else { \ |
| 165 | obj = globalref; \ |
| 166 | }\ |
| 167 | } \ |
| 168 | |
| 169 | STATIC_INIT_JNI(jclass, clazzBundle, env->FindClass, |
| 170 | static_cast<jclass>(env->NewGlobalRef(clazzBundle)), |
| 171 | "android/os/PersistableBundle"); |
| 172 | STATIC_INIT_JNI(jfieldID, bundleCreatorId, env->GetStaticFieldID, |
| 173 | bundleCreatorId, |
| 174 | clazzBundle, "CREATOR", "Landroid/os/Parcelable$Creator;"); |
| 175 | STATIC_INIT_JNI(jobject, bundleCreator, env->GetStaticObjectField, |
| 176 | env->NewGlobalRef(bundleCreator), |
| 177 | clazzBundle, bundleCreatorId); |
| 178 | STATIC_INIT_JNI(jclass, clazzCreator, env->FindClass, |
| 179 | static_cast<jclass>(env->NewGlobalRef(clazzCreator)), |
| 180 | "android/os/Parcelable$Creator"); |
| 181 | STATIC_INIT_JNI(jmethodID, createFromParcelId, env->GetMethodID, |
| 182 | createFromParcelId, |
| 183 | clazzCreator, "createFromParcel", "(Landroid/os/Parcel;)Ljava/lang/Object;"); |
| 184 | |
| 185 | jobject newBundle = env->CallObjectMethod(bundleCreator, createFromParcelId, jParcel); |
| 186 | if (newBundle == NULL) { |
| 187 | ALOGE("Failed to create a new PersistableBundle " |
| 188 | "from the createFromParcel call."); |
| 189 | } |
| 190 | |
| 191 | return newBundle; |
| 192 | } |
| 193 | |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 194 | }; // namespace android |
| 195 | |