@@ -355,6 +355,93 @@ void AddToBundle(JNIEnv* env, jobject bundle, const char* key, int64_t value) {
355
355
env->DeleteLocalRef (key_string);
356
356
}
357
357
358
+ void AddArrayListToBundle (JNIEnv* env, jobject bundle, const char * key, jobject arraylist) {
359
+ jstring key_string = env->NewStringUTF (key);
360
+ env->CallVoidMethod (bundle, util::bundle::GetMethodId (util::bundle::kPutParcelableArrayList ),
361
+ key_string, arraylist);
362
+ util::CheckAndClearJniExceptions (env);
363
+ env->DeleteLocalRef (key_string);
364
+ }
365
+
366
+ void AddBundleToBundle (JNIEnv* env, jobject bundle, const char * key, jobject inner_bundle) {
367
+ jstring key_string = env->NewStringUTF (key);
368
+ env->CallVoidMethod (bundle, util::bundle::GetMethodId (util::bundle::kPutBundle ),
369
+ key_string, inner_bundle);
370
+ util::CheckAndClearJniExceptions (env);
371
+ env->DeleteLocalRef (key_string);
372
+ }
373
+
374
+ jobject MapToBundle (JNIEnv* env, const std::map<Variant, Variant>& map);
375
+
376
+ jobject VectorToArrayList (JNIEnv* env, const std::vector<Variant>& vector) {
377
+ jobject arraylist =
378
+ env->NewObject (util::array_list::GetClass (),
379
+ util::array_list::GetMethodId (util::array_list::kConstructor ));
380
+
381
+ for (const Variant& element : vector) {
382
+ if (element.is_map ()) {
383
+ jobject bundle = MapToBundle (env, element.map ());
384
+ env->CallBooleanMethod (arraylist, util::array_list::GetMethodId (util::array_list::kAdd ),
385
+ bundle);
386
+ env->DeleteLocalRef (bundle);
387
+ } else {
388
+ LogError (" VectorToArrayList: Unsupported type (%s) within vector." ,
389
+ Variant::TypeName (element.type ()));
390
+ }
391
+ }
392
+ return arraylist;
393
+ }
394
+
395
+ bool AddVariantToBundle (JNIEnv* env, jobject bundle, const char * key, const Variant& value) {
396
+ if (value.is_int64 ()) {
397
+ AddToBundle (env, bundle, key, value.int64_value ());
398
+ } else if (value.is_double ()) {
399
+ AddToBundle (env, bundle, key,
400
+ value.double_value ());
401
+ } else if (value.is_string ()) {
402
+ AddToBundle (env, bundle, key,
403
+ value.string_value ());
404
+ } else if (value.is_bool ()) {
405
+ // Just use integer 0 or 1.
406
+ AddToBundle (env, bundle, key,
407
+ value.bool_value () ? static_cast <int64_t >(1L )
408
+ : static_cast <int64_t >(0L ));
409
+ } else if (value.is_null ()) {
410
+ // Just use integer 0 for null.
411
+ AddToBundle (env, bundle, key, static_cast <int64_t >(0L ));
412
+ } else if (value.is_vector ()) {
413
+ jobject arraylist = VectorToArrayList (env, value.vector ());
414
+ AddArrayListToBundle (env, bundle, key, arraylist);
415
+ env->DeleteLocalRef (arraylist);
416
+ } else if (value.is_map ()) {
417
+ jobject inner_bundle = MapToBundle (env, value.map ());
418
+ AddBundleToBundle (env, bundle, key, inner_bundle);
419
+ env->DeleteLocalRef (inner_bundle);
420
+ } else {
421
+ // A Variant type that couldn't be handled was passed in.
422
+ return false ;
423
+ }
424
+ return true ;
425
+ }
426
+
427
+ jobject MapToBundle (JNIEnv* env, const std::map<Variant, Variant>& map) {
428
+ jobject bundle =
429
+ env->NewObject (util::bundle::GetClass (),
430
+ util::bundle::GetMethodId (util::bundle::kConstructor ));
431
+ for (const auto & pair : map) {
432
+ // Only add elements that use a string key
433
+ if (!pair.first .is_string ()) {
434
+ continue ;
435
+ }
436
+ if (!AddVariantToBundle (env, bundle, pair.first .string_value (), pair.second )) {
437
+ LogError (" MapToBundle: Unsupported type (%s) within map with key %s." ,
438
+ Variant::TypeName (pair.second .type ()), pair.first .string_value ());
439
+ }
440
+ util::CheckAndClearJniExceptions (env);
441
+ }
442
+ return bundle;
443
+ }
444
+
358
445
// Log an event with one string parameter.
359
446
void LogEvent (const char * name, const char * parameter_name,
360
447
const char * parameter_value) {
@@ -404,27 +491,11 @@ void LogEvent(const char* name, const Parameter* parameters,
404
491
LogEvent (env, name, [env, parameters, number_of_parameters](jobject bundle) {
405
492
for (size_t i = 0 ; i < number_of_parameters; ++i) {
406
493
const Parameter& parameter = parameters[i];
407
- if (parameter.value .is_int64 ()) {
408
- AddToBundle (env, bundle, parameter.name , parameter.value .int64_value ());
409
- } else if (parameter.value .is_double ()) {
410
- AddToBundle (env, bundle, parameter.name ,
411
- parameter.value .double_value ());
412
- } else if (parameter.value .is_string ()) {
413
- AddToBundle (env, bundle, parameter.name ,
414
- parameter.value .string_value ());
415
- } else if (parameter.value .is_bool ()) {
416
- // Just use integer 0 or 1.
417
- AddToBundle (env, bundle, parameter.name ,
418
- parameter.value .bool_value () ? static_cast <int64_t >(1L )
419
- : static_cast <int64_t >(0L ));
420
- } else if (parameter.value .is_null ()) {
421
- // Just use integer 0 for null.
422
- AddToBundle (env, bundle, parameter.name , static_cast <int64_t >(0L ));
423
- } else {
424
- // Vector or Map were passed in.
494
+ if (!AddVariantToBundle (env, bundle, parameter.name , parameter.value )) {
495
+ // A Variant type that couldn't be handled was passed in.
425
496
LogError (
426
497
" LogEvent(%s): %s is not a valid parameter value type. "
427
- " Container types are not allowed. No event was logged." ,
498
+ " No event was logged." ,
428
499
parameter.name , Variant::TypeName (parameter.value .type ()));
429
500
}
430
501
}
0 commit comments