|
| 1 | + |
| 2 | +using Sentry.Infrastructure; |
| 3 | +#if __ANDROID__ |
| 4 | +using View = Android.Views.View; |
| 5 | +using Android.Views; |
| 6 | +using Java.Lang; |
| 7 | +using Microsoft.Maui.Handlers; |
| 8 | +using Microsoft.Maui.Platform; |
| 9 | +#endif |
| 10 | + |
| 11 | +namespace Sentry.Maui; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Contains custom <see cref="BindableProperty"/> definitions used to control the behaviour of the Sentry SessionReplay |
| 15 | +/// feature in MAUI apps. |
| 16 | +/// <remarks> |
| 17 | +/// NOTE: Session Replay is currently an experimental feature for MAUI and is subject to change. |
| 18 | +/// </remarks> |
| 19 | +/// </summary> |
| 20 | +public static class SessionReplay |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Mask can be used to either unmask or mask a view. |
| 24 | + /// </summary> |
| 25 | + public static readonly BindableProperty MaskProperty = |
| 26 | + BindableProperty.CreateAttached( |
| 27 | + "Mask", |
| 28 | + typeof(SessionReplayMaskMode), |
| 29 | + typeof(SessionReplay), |
| 30 | + defaultValue: SessionReplayMaskMode.Mask, |
| 31 | + propertyChanged: OnMaskChanged); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Gets the value of the Mask property for a view. |
| 35 | + /// </summary> |
| 36 | + public static SessionReplayMaskMode GetMask(BindableObject view) => (SessionReplayMaskMode)view.GetValue(MaskProperty); |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Sets the value of the Mask property for a view. |
| 40 | + /// </summary> |
| 41 | + /// <param name="view">The view element to mask or unmask</param> |
| 42 | + /// <param name="value">The value to assign. Can be either "sentry-mask" or "sentry-unmask".</param> |
| 43 | + public static void SetMask(BindableObject view, SessionReplayMaskMode value) => view.SetValue(MaskProperty, value); |
| 44 | + |
| 45 | + private static void OnMaskChanged(BindableObject bindable, object oldValue, object newValue) |
| 46 | + { |
| 47 | +#if __ANDROID__ |
| 48 | + if (bindable is not VisualElement ve || newValue is not SessionReplayMaskMode maskSetting) |
| 49 | + { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + // This code looks pretty funky... just matching how funky MAUI is though. |
| 54 | + // See https://github.com/getsentry/sentry-dotnet/pull/4121#discussion_r2054129378 |
| 55 | + ve.HandlerChanged -= OnMaskedElementHandlerChanged; |
| 56 | + ve.HandlerChanged -= OnUnmaskedElementHandlerChanged; |
| 57 | + |
| 58 | + if (maskSetting == SessionReplayMaskMode.Mask) |
| 59 | + { |
| 60 | + ve.HandlerChanged += OnMaskedElementHandlerChanged; |
| 61 | + } |
| 62 | + else if (maskSetting == SessionReplayMaskMode.Unmask) |
| 63 | + { |
| 64 | + ve.HandlerChanged += OnUnmaskedElementHandlerChanged; |
| 65 | + } |
| 66 | +#endif |
| 67 | + } |
| 68 | + |
| 69 | +#if __ANDROID__ |
| 70 | + private static void OnMaskedElementHandlerChanged(object? sender, EventArgs _) |
| 71 | + { |
| 72 | + if ((sender as VisualElement)?.Handler?.PlatformView is not View nativeView) |
| 73 | + { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + nativeView.Tag = SessionReplayMaskMode.Mask.ToNativeTag(); |
| 78 | + } |
| 79 | + |
| 80 | + private static void OnUnmaskedElementHandlerChanged(object? sender, EventArgs _) |
| 81 | + { |
| 82 | + if ((sender as VisualElement)?.Handler?.PlatformView is not View nativeView) |
| 83 | + { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + nativeView.Tag = SessionReplayMaskMode.Unmask.ToNativeTag(); |
| 88 | + } |
| 89 | +#endif |
| 90 | +} |
0 commit comments