@@ -58,6 +58,8 @@ static const ::firebase::App* g_app = nullptr;
58
58
" ()Lcom/google/android/gms/tasks/Task;" ), \
59
59
X (GetSessionId, " getSessionId" , \
60
60
" ()Lcom/google/android/gms/tasks/Task;" ), \
61
+ X (SetDefaultEventParameters, " setDefaultEventParameters" , \
62
+ " (Landroid/os/Bundle;)V" ), \
61
63
X (GetInstance, " getInstance" , " (Landroid/content/Context;)" \
62
64
" Lcom/google/firebase/analytics/FirebaseAnalytics;" , \
63
65
firebase::util::kMethodTypeStatic )
@@ -518,6 +520,64 @@ void LogEvent(const char* name, const Parameter* parameters,
518
520
});
519
521
}
520
522
523
+ // Convert std::map<std::string, Variant> to Bundle.
524
+ jobject StringVariantMapToBundle (JNIEnv* env,
525
+ const std::map<std::string, Variant>& map) {
526
+ jobject bundle =
527
+ env->NewObject (util::bundle::GetClass (),
528
+ util::bundle::GetMethodId (util::bundle::kConstructor ));
529
+ for (const auto & pair : map) {
530
+ // Bundle keys must be strings.
531
+ const char * key = pair.first .c_str ();
532
+ const Variant& value = pair.second ;
533
+ // A null variant clears the default parameter for that key.
534
+ // The Android SDK uses Bundle.putString(key, null) for this.
535
+ if (value.is_null ()) {
536
+ jstring key_string = env->NewStringUTF (key);
537
+ // Call Bundle.putString(key, null)
538
+ env->CallVoidMethod (bundle,
539
+ util::bundle::GetMethodId (util::bundle::kPutString ),
540
+ key_string, nullptr );
541
+ util::CheckAndClearJniExceptions (env);
542
+ env->DeleteLocalRef (key_string);
543
+ } else if (!AddVariantToBundle (env, bundle, key, value)) {
544
+ LogError (" SetDefaultEventParameters: Unsupported type (%s) for key %s." ,
545
+ Variant::TypeName (value.type ()), key);
546
+ }
547
+ // CheckAndClearJniExceptions is called within AddVariantToBundle or above
548
+ // for the null case.
549
+ }
550
+ return bundle;
551
+ }
552
+
553
+ void SetDefaultEventParameters (
554
+ const std::map<std::string, Variant>& default_parameters) {
555
+ FIREBASE_ASSERT_RETURN_VOID (internal::IsInitialized ());
556
+ JNIEnv* env = g_app->GetJNIEnv ();
557
+
558
+ jobject bundle = StringVariantMapToBundle (env, default_parameters);
559
+
560
+ env->CallVoidMethod (
561
+ g_analytics_class_instance,
562
+ analytics::GetMethodId (analytics::kSetDefaultEventParameters ), bundle);
563
+
564
+ util::CheckAndClearJniExceptions (env);
565
+ env->DeleteLocalRef (bundle);
566
+ }
567
+
568
+ void ClearDefaultEventParameters () {
569
+ FIREBASE_ASSERT_RETURN_VOID (internal::IsInitialized ());
570
+ JNIEnv* env = g_app->GetJNIEnv ();
571
+
572
+ // Call FirebaseAnalytics.setDefaultEventParameters(null)
573
+ env->CallVoidMethod (
574
+ g_analytics_class_instance,
575
+ analytics::GetMethodId (analytics::kSetDefaultEventParameters ),
576
+ nullptr ); // Pass null Bundle to clear all parameters
577
+
578
+ util::CheckAndClearJniExceptions (env);
579
+ }
580
+
521
581
// / Initiates on-device conversion measurement given a user email address on iOS
522
582
// / (no-op on Android). On iOS, requires dependency
523
583
// / GoogleAppMeasurementOnDeviceConversion to be linked in, otherwise it is a
0 commit comments