Skip to content

Commit

Permalink
[android] use Java primitive boolean for UriImageSource (#22040)
Browse files Browse the repository at this point in the history
Context: https://cs.android.com/android/platform/superproject/main/+/main:libcore/ojluni/annotations/hiddenapi/java/lang/Boolean.java;l=109?q=boolean.java

Some internal APIs in MAUI were using `java.lang.Boolean` when they
could just use `boolean` instead.

Java `boolean` is a primitive type, while `java.lang.Boolean` is an
object (that can be null). This is similar to `bool` vs `bool?` in C#,
except Java does not have `struct`.

This avoids:

* JNI field lookup of `Boolean.TRUE` and `Boolean.FALSE` on startup

* JNI field reads of `Boolean.TRUE` and `Boolean.FALSE` on every
  `UriImageSource` creation

* Creating a C# instance of `Java.Lang.Boolean`, and the bookkeeping
  for reusing C# instances

I was also going to change `ImageLoaderCallback.onComplete`, but it is
listed as a public API.

This is a small improvement, but should help all `UriImageSource` on
Android.
  • Loading branch information
jonathanpeppers authored Apr 27, 2024
1 parent 0117681 commit 48d5ebf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ static PorterDuff.Mode getPorterMode(int mode) {
}
}

private static void prepare(RequestBuilder<Drawable> builder, Target<Drawable> target, Boolean cachingEnabled, ImageLoaderCallback callback) {
private static void prepare(RequestBuilder<Drawable> builder, Target<Drawable> target, boolean cachingEnabled, ImageLoaderCallback callback) {
// A special value to work around https://github.com/dotnet/maui/issues/6783 where targets
// are actually re-used if all the variables are the same.
// Adding this "error image" that will always load a null image makes each request unique,
Expand All @@ -311,12 +311,12 @@ private static void prepare(RequestBuilder<Drawable> builder, Target<Drawable> t
.into(target);
}

private static void loadInto(RequestBuilder<Drawable> builder, ImageView imageView, Boolean cachingEnabled, ImageLoaderCallback callback) {
private static void loadInto(RequestBuilder<Drawable> builder, ImageView imageView, boolean cachingEnabled, ImageLoaderCallback callback) {
MauiCustomViewTarget target = new MauiCustomViewTarget(imageView, callback);
prepare(builder, target, cachingEnabled, callback);
}

private static void load(RequestBuilder<Drawable> builder, Context context, Boolean cachingEnabled, ImageLoaderCallback callback) {
private static void load(RequestBuilder<Drawable> builder, Context context, boolean cachingEnabled, ImageLoaderCallback callback) {
MauiCustomTarget target = new MauiCustomTarget(context, callback);
prepare(builder, target, cachingEnabled, callback);
}
Expand All @@ -328,7 +328,7 @@ public static void loadImageFromFile(ImageView imageView, String file, ImageLoad
loadInto(builder, imageView, true, callback);
}

public static void loadImageFromUri(ImageView imageView, String uri, Boolean cachingEnabled, ImageLoaderCallback callback) {
public static void loadImageFromUri(ImageView imageView, String uri, boolean cachingEnabled, ImageLoaderCallback callback) {
Uri androidUri = Uri.parse(uri);
if (androidUri == null) {
callback.onComplete(false, null, null);
Expand Down Expand Up @@ -363,7 +363,7 @@ public static void loadImageFromFile(Context context, String file, ImageLoaderCa
load(builder, context, true, callback);
}

public static void loadImageFromUri(Context context, String uri, Boolean cachingEnabled, ImageLoaderCallback callback) {
public static void loadImageFromUri(Context context, String uri, boolean cachingEnabled, ImageLoaderCallback callback) {
Uri androidUri = Uri.parse(uri);
if (androidUri == null) {
callback.onComplete(false, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ public partial class UriImageSourceService
{
var callback = new ImageLoaderCallback();

var cachingEnabled = uriImageSource.CachingEnabled
? Java.Lang.Boolean.True
: Java.Lang.Boolean.False;
PlatformInterop.LoadImageFromUri(imageView, uriImageSource.Uri.OriginalString, cachingEnabled, callback);
PlatformInterop.LoadImageFromUri(imageView, uriImageSource.Uri.OriginalString, uriImageSource.CachingEnabled, callback);

return callback.Result;
}
Expand All @@ -45,10 +42,7 @@ public partial class UriImageSourceService
{
var drawableCallback = new ImageLoaderResultCallback();

var cachingEnabled = uriImageSource.CachingEnabled
? Java.Lang.Boolean.True
: Java.Lang.Boolean.False;
PlatformInterop.LoadImageFromUri(context, uriImageSource.Uri.OriginalString, cachingEnabled, drawableCallback);
PlatformInterop.LoadImageFromUri(context, uriImageSource.Uri.OriginalString, uriImageSource.CachingEnabled, drawableCallback);

return drawableCallback.Result;
}
Expand Down
Binary file modified src/Core/src/maui.aar
Binary file not shown.

0 comments on commit 48d5ebf

Please sign in to comment.