Skip to content

Commit

Permalink
Avoid JavaCast and exceptions and ask Java (#23215)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow authored Jun 24, 2024
1 parent 1707990 commit 208241b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.net.Uri;
Expand Down Expand Up @@ -63,15 +64,16 @@
import java.util.List;

public class PlatformInterop {
public static void requestLayoutIfNeeded(View view) {

public static void requestLayoutIfNeeded(View view) {

// If the view isn't currently in the layout process, then we simply request
// that layout happen next time around
if (!view.isInLayout()) {
view.requestLayout();
return;
}
return;
}
/*
Something is requesting layout while the view is already in the middle of a layout pass. This is most
likely because a layout-affecting property has been data bound to another layout-affecting property, e.g.
Expand All @@ -84,13 +86,13 @@ public static void requestLayoutIfNeeded(View view) {
layout pass has finished. Layout will happen again with the updated values.
*/

Runnable runnable = () -> {
if (!view.isInLayout()) {
view.requestLayout();
}
};
view.post(runnable);
Runnable runnable = () -> {
if (!view.isInLayout()) {
view.requestLayout();
}
};
view.post(runnable);
}

public static void removeFromParent(View view) {
Expand Down Expand Up @@ -673,4 +675,18 @@ static int[][] getButtonState()
return buttonState;
}
}

/*
* This method is used to get the Animatable object from a Drawable.
* This is useful when we need to start/stop animations on a drawable.
* @param drawable The drawable to get the Animatable object from.
* @return The Animatable object if the drawable is an instance of Animatable, otherwise null.
*/
public static Animatable getAnimatable(Drawable drawable) {
if (drawable instanceof Animatable) {
return (Animatable) drawable;
}
return null;
}

}
15 changes: 15 additions & 0 deletions src/Core/src/Platform/Android/DrawableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Android.Graphics;
using Android.Graphics.Drawables;
using AColor = Android.Graphics.Color;
using AColorFilter = Android.Graphics.ColorFilter;
using ADrawable = Android.Graphics.Drawables.Drawable;
Expand Down Expand Up @@ -58,5 +59,19 @@ public static void SetColorFilter(this ADrawable drawable, AColor color, FilterM
if (drawable is not null)
PlatformInterop.SetColorFilter(drawable, color, (int)mode);
}

internal static IAnimatable? AsAnimatable(this ADrawable? drawable)
{
if (drawable is null)
return null;

if (drawable is IAnimatable animatable)
return animatable;

if (PlatformInterop.GetAnimatable(drawable) is IAnimatable javaAnimatable)
return javaAnimatable;

return null;
}
}
}
10 changes: 2 additions & 8 deletions src/Core/src/Platform/Android/ImageViewExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Android.Graphics.Drawables;
using Android.Views;
using Android.Graphics.Drawables;
using Android.Widget;
using Bumptech.Glide.Load.Resource.Gif;

namespace Microsoft.Maui.Platform
{
Expand All @@ -30,8 +25,7 @@ public static void UpdateIsAnimationPlaying(this ImageView imageView, IImageSour

public static void UpdateIsAnimationPlaying(this Drawable? drawable, IImageSourcePart image)
{
var animatable = drawable.TryJavaCast<IAnimatable>();
if (animatable is not null)
if (drawable.IsAlive() && drawable.AsAnimatable() is IAnimatable animatable)
{
if (image.IsAnimationPlaying)
{
Expand Down
14 changes: 0 additions & 14 deletions src/Core/src/Platform/Android/JavaObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Android.Runtime;

namespace Microsoft.Maui
{
Expand Down Expand Up @@ -33,18 +32,5 @@ 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;
}
}
}
}
Binary file modified src/Core/src/maui.aar
Binary file not shown.

0 comments on commit 208241b

Please sign in to comment.