Skip to content

Commit

Permalink
Take 2?
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Jun 6, 2024
1 parent d576c82 commit 1e82e8b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
16 changes: 2 additions & 14 deletions src/Core/src/Platform/Android/ImageViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IAnimatable>();
if (animatable is not null)
{
if (image.IsAnimationPlaying)
{
Expand Down
16 changes: 16 additions & 0 deletions src/Core/src/Platform/Android/JavaObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<TResult>();
}
catch
{
return null;
}
}
}
}

0 comments on commit 1e82e8b

Please sign in to comment.