From 1e82e8b9190c705d61724098bd327c6c86982761 Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Thu, 6 Jun 2024 18:44:18 +0800 Subject: [PATCH] Take 2? --- .../src/Platform/Android/ImageViewExtensions.cs | 16 ++-------------- .../src/Platform/Android/JavaObjectExtensions.cs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Core/src/Platform/Android/ImageViewExtensions.cs b/src/Core/src/Platform/Android/ImageViewExtensions.cs index c2df83e28620..80c69d6c5c54 100644 --- a/src/Core/src/Platform/Android/ImageViewExtensions.cs +++ b/src/Core/src/Platform/Android/ImageViewExtensions.cs @@ -30,20 +30,8 @@ public static void UpdateIsAnimationPlaying(this ImageView imageView, IImageSour public static void UpdateIsAnimationPlaying(this Drawable? drawable, IImageSourcePart image) { - // IMPORTANT: - // The linker will remove the interface from the concrete type if we don't force - // the linker to be aware of both the concrete and interface types. - - if (drawable is IAnimatable animatable) - Update(image, animatable); - else if (drawable is AnimationDrawable ad) - Update(image, ad); - else if (drawable is GifDrawable gif) - Update(image, gif); - else if (OperatingSystem.IsAndroidVersionAtLeast(28) && drawable is AnimatedImageDrawable aid) - Update(image, aid); - - static void Update(IImageSourcePart image, IAnimatable animatable) + var animatable = drawable.TryJavaCast(); + if (animatable is not null) { if (image.IsAnimationPlaying) { diff --git a/src/Core/src/Platform/Android/JavaObjectExtensions.cs b/src/Core/src/Platform/Android/JavaObjectExtensions.cs index 014c0fed5eaa..ecac7f09b73f 100644 --- a/src/Core/src/Platform/Android/JavaObjectExtensions.cs +++ b/src/Core/src/Platform/Android/JavaObjectExtensions.cs @@ -1,10 +1,13 @@ using System; using System.Diagnostics.CodeAnalysis; +using Android.Runtime; namespace Microsoft.Maui { static class JavaObjectExtensions { + const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors; + public static bool IsDisposed(this Java.Lang.Object obj) { return obj.Handle == IntPtr.Zero; @@ -30,5 +33,18 @@ public static bool IsAlive([NotNullWhen(true)] this global::Android.Runtime.IJav return !obj.IsDisposed(); } + + public static TResult? TryJavaCast<[DynamicallyAccessedMembers (Constructors)] TResult>(this IJavaObject? instance) + where TResult : class, IJavaObject + { + try + { + return instance.JavaCast(); + } + catch + { + return null; + } + } } } \ No newline at end of file