|
| 1 | +using Microsoft.Extensions.Options; |
| 2 | +using Sentry.Extensibility; |
| 3 | +#if __ANDROID__ |
| 4 | +using View = Android.Views.View; |
| 5 | +#endif |
| 6 | + |
| 7 | +namespace Sentry.Maui.Internal; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Masks or unmasks visual elements for session replay recordings |
| 11 | +/// </summary> |
| 12 | +internal class MauiVisualElementEventsBinder : IMauiElementEventBinder |
| 13 | +{ |
| 14 | + private readonly SentryMauiOptions _options; |
| 15 | + |
| 16 | + public MauiVisualElementEventsBinder(IOptions<SentryMauiOptions> options) |
| 17 | + { |
| 18 | + _options = options.Value; |
| 19 | + } |
| 20 | + |
| 21 | + /// <inheritdoc /> |
| 22 | + public void Bind(VisualElement element, Action<BreadcrumbEvent> _) |
| 23 | + { |
| 24 | + element.Loaded += OnElementLoaded; |
| 25 | + } |
| 26 | + |
| 27 | + /// <inheritdoc /> |
| 28 | + public void UnBind(VisualElement element) |
| 29 | + { |
| 30 | + element.Loaded -= OnElementLoaded; |
| 31 | + } |
| 32 | + |
| 33 | + internal void OnElementLoaded(object? sender, EventArgs _) |
| 34 | + { |
| 35 | + if (sender is not VisualElement element) |
| 36 | + { |
| 37 | + _options.LogDebug("OnElementLoaded: sender is not a VisualElement"); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + var handler = element.Handler; |
| 42 | + if (handler is null) |
| 43 | + { |
| 44 | + _options.LogDebug("OnElementLoaded: element.Handler is null"); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | +#if __ANDROID__ |
| 49 | + if (element.Handler?.PlatformView is not View nativeView) |
| 50 | + { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (_options.Native.ExperimentalOptions.SessionReplay.MaskedControls.FirstOrDefault(maskType => element.GetType().IsAssignableFrom(maskType)) is not null) |
| 55 | + { |
| 56 | + nativeView.Tag = SessionReplayMaskMode.Mask.ToNativeTag(); |
| 57 | + _options.LogDebug("OnElementLoaded: Successfully set sentry-mask tag on native view"); |
| 58 | + } |
| 59 | + else if (_options.Native.ExperimentalOptions.SessionReplay.UnmaskedControls.FirstOrDefault(unmaskType => element.GetType().IsAssignableFrom(unmaskType)) is not null) |
| 60 | + { |
| 61 | + nativeView.Tag = SessionReplayMaskMode.Unmask.ToNativeTag(); |
| 62 | + _options.LogDebug("OnElementLoaded: Successfully set sentry-unmask tag on native view"); |
| 63 | + } |
| 64 | +#endif |
| 65 | + } |
| 66 | +} |
0 commit comments