// Copyright (c) 2022 Sentry. All Rights Reserved. #include "SentryScopeAndroid.h" #include "SentryBreadcrumbAndroid.h" #include "SentryAttachmentAndroid.h" #include "Infrastructure/SentryConvertorsAndroid.h" #include "Infrastructure/SentryJavaClasses.h" SentryScopeAndroid::SentryScopeAndroid() : FSentryJavaObjectWrapper(SentryJavaClasses::Scope, "(Lio/sentry/SentryOptions;)V", *FSentryJavaObjectWrapper::CallStaticObjectMethod(SentryJavaClasses::SentryBridgeJava, "getOptions", "()Lio/sentry/SentryOptions;")) { SetupClassMethods(); } SentryScopeAndroid::SentryScopeAndroid(jobject scope) : FSentryJavaObjectWrapper(SentryJavaClasses::Scope, scope) { SetupClassMethods(); } void SentryScopeAndroid::SetupClassMethods() { AddBreadcrumbMethod = GetMethod("addBreadcrumb", "(Lio/sentry/Breadcrumb;)V"); ClearBreadcrumbsMethod = GetMethod("clearBreadcrumbs", "()V"); AddAttachmentMethod = GetMethod("addAttachment", "(Lio/sentry/Attachment;)V"); ClearAttachmentsMethod = GetMethod("clearAttachments", "()V"); SetTagValueMethod = GetMethod("setTag", "(Ljava/lang/String;Ljava/lang/String;)V"); RemoveTagMethod = GetMethod("removeTag", "(Ljava/lang/String;)V"); GetTagsMethod = GetMethod("getTags", "()Ljava/util/Map;"); SetFingerprintMethod = GetMethod("setFingerprint", "(Ljava/util/List;)V"); GetFingerprintMethod = GetMethod("getFingerprint", "()Ljava/util/List;"); SetLevelMethod = GetMethod("setLevel", "(Lio/sentry/SentryLevel;)V"); GetLevelMethod = GetMethod("getLevel", "()Lio/sentry/SentryLevel;"); SetContextMethod = GetMethod("setContexts", "(Ljava/lang/String;Ljava/lang/Object;)V"); RemoveContextMethod = GetMethod("removeContexts", "(Ljava/lang/String;)V"); SetExtraValueMethod = GetMethod("setExtra", "(Ljava/lang/String;Ljava/lang/String;)V"); RemoveExtraMethod = GetMethod("removeExtra", "(Ljava/lang/String;)V"); GetExtrasMethod = GetMethod("getExtras", "()Ljava/util/Map;"); ClearMethod = GetMethod("clear", "()V"); } void SentryScopeAndroid::AddBreadcrumb(TSharedPtr breadcrumb) { TSharedPtr breadcrumbAndroid = StaticCastSharedPtr(breadcrumb); CallMethod(AddBreadcrumbMethod, breadcrumbAndroid->GetJObject()); } void SentryScopeAndroid::ClearBreadcrumbs() { CallMethod(ClearBreadcrumbsMethod); } void SentryScopeAndroid::AddAttachment(TSharedPtr attachment) { TSharedPtr attachmentAndroid = StaticCastSharedPtr(attachment); CallMethod(AddAttachmentMethod, attachmentAndroid->GetJObject()); } void SentryScopeAndroid::ClearAttachments() { CallMethod(ClearAttachmentsMethod); } void SentryScopeAndroid::SetTagValue(const FString& key, const FString& value) { CallMethod(SetTagValueMethod, *GetJString(key), *GetJString(value)); } FString SentryScopeAndroid::GetTagValue(const FString& key) const { TMap tags = GetTags(); FString* tagValue = tags.Find(key); if (!tagValue) return FString(); return *tagValue; } void SentryScopeAndroid::RemoveTag(const FString& key) { CallMethod(RemoveTagMethod, *GetJString(key)); } void SentryScopeAndroid::SetTags(const TMap& tags) { for (const auto& tag : tags) { SetTagValue(tag.Key, tag.Value); } } TMap SentryScopeAndroid::GetTags() const { auto tags = CallObjectMethod(GetTagsMethod); return SentryConvertorsAndroid::StringMapToUnreal(*tags); } void SentryScopeAndroid::SetDist(const FString& dist) { SetTagValue("dist", dist); } FString SentryScopeAndroid::GetDist() const { return GetTagValue("dist"); } void SentryScopeAndroid::SetEnvironment(const FString& environment) { SetTagValue("environment", environment); } FString SentryScopeAndroid::GetEnvironment() const { return GetTagValue("environment"); } void SentryScopeAndroid::SetFingerprint(const TArray& fingerprint) { CallMethod(SetFingerprintMethod, SentryConvertorsAndroid::StringArrayToNative(fingerprint)->GetJObject()); } TArray SentryScopeAndroid::GetFingerprint() const { auto fingerprint = CallObjectMethod(GetFingerprintMethod); return SentryConvertorsAndroid::StringListToUnreal(*fingerprint); } void SentryScopeAndroid::SetLevel(ESentryLevel level) { CallMethod(SetLevelMethod, SentryConvertorsAndroid::SentryLevelToNative(level)->GetJObject()); } ESentryLevel SentryScopeAndroid::GetLevel() const { auto level = CallObjectMethod(GetLevelMethod); return SentryConvertorsAndroid::SentryLevelToUnreal(*level); } void SentryScopeAndroid::SetContext(const FString& key, const TMap& values) { CallMethod(SetContextMethod, *GetJString(key), SentryConvertorsAndroid::StringMapToNative(values)->GetJObject()); } void SentryScopeAndroid::RemoveContext(const FString& key) { CallMethod(RemoveContextMethod, *GetJString(key)); } void SentryScopeAndroid::SetExtraValue(const FString& key, const FString& value) { CallMethod(SetExtraValueMethod, *GetJString(key), *GetJString(value)); } FString SentryScopeAndroid::GetExtraValue(const FString& key) const { TMap extras = GetTags(); FString* extraValue = extras.Find(key); if (!extraValue) return FString(); return *extraValue; } void SentryScopeAndroid::RemoveExtra(const FString& key) { CallMethod(RemoveExtraMethod, *GetJString(key)); } void SentryScopeAndroid::SetExtras(const TMap& extras) { for (const auto& extra : extras) { SetExtraValue(extra.Key, extra.Value); } } TMap SentryScopeAndroid::GetExtras() const { auto extras = CallObjectMethod(GetExtrasMethod); return SentryConvertorsAndroid::StringMapToUnreal(*extras); } void SentryScopeAndroid::Clear() { CallMethod(ClearMethod); }