From 17cae33fd8a966f741495e5c8604661629821006 Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Wed, 1 Jun 2022 16:53:26 -0700 Subject: [PATCH] Allow Drawable requests to be loaded with a specified theme PiperOrigin-RevId: 452415405 --- .../java/com/bumptech/glide/load/Options.java | 1 + .../drawable/ResourceDrawableDecoder.java | 17 +++++++++++++- .../glide/request/BaseRequestOptions.java | 22 +++++++------------ .../glide/request/RequestOptionsTest.java | 1 - 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/library/src/main/java/com/bumptech/glide/load/Options.java b/library/src/main/java/com/bumptech/glide/load/Options.java index 281470acd1..e16b74d138 100644 --- a/library/src/main/java/com/bumptech/glide/load/Options.java +++ b/library/src/main/java/com/bumptech/glide/load/Options.java @@ -15,6 +15,7 @@ public void putAll(@NonNull Options other) { values.putAll((SimpleArrayMap, Object>) other.values); } + // TODO(b/234614365): Allow nullability. @NonNull public Options set(@NonNull Option option, @NonNull T value) { values.put(option, value); diff --git a/library/src/main/java/com/bumptech/glide/load/resource/drawable/ResourceDrawableDecoder.java b/library/src/main/java/com/bumptech/glide/load/resource/drawable/ResourceDrawableDecoder.java index 4c7132cf95..b30e461261 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/drawable/ResourceDrawableDecoder.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/drawable/ResourceDrawableDecoder.java @@ -4,14 +4,17 @@ import android.content.Context; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Resources; +import android.content.res.Resources.Theme; import android.graphics.drawable.Drawable; import android.net.Uri; import androidx.annotation.DrawableRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import com.bumptech.glide.load.Option; import com.bumptech.glide.load.Options; import com.bumptech.glide.load.ResourceDecoder; import com.bumptech.glide.load.engine.Resource; +import com.bumptech.glide.util.Preconditions; import java.util.List; /** @@ -23,6 +26,11 @@ * other packages. */ public class ResourceDrawableDecoder implements ResourceDecoder { + + /** Specifies a {@link Theme} which will be used to load the drawable. */ + public static final Option THEME = + Option.memory("com.bumptech.glide.load.resource.bitmap.Downsampler.Theme"); + /** * The package name to provide {@link Resources#getIdentifier(String, String, String)} when trying * to find system resource ids. @@ -62,7 +70,14 @@ public Resource decode( Context targetContext = findContextForPackage(source, packageName); @DrawableRes int resId = findResourceIdFromUri(targetContext, source); // We can't get a theme from another application. - Drawable drawable = DrawableDecoderCompat.getDrawable(context, targetContext, resId); + Theme theme = options.get(THEME); + Preconditions.checkArgument( + targetContext.getPackageName().equals(packageName) || theme == null, + "Can't get a theme from another package"); + Drawable drawable = + theme == null + ? DrawableDecoderCompat.getDrawable(context, targetContext, resId) + : DrawableDecoderCompat.getDrawable(context, resId, theme); return NonOwnedDrawableResource.newInstance(drawable); } diff --git a/library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java b/library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java index dd50ca1490..ff20bbfb20 100644 --- a/library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java +++ b/library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java @@ -28,6 +28,7 @@ import com.bumptech.glide.load.resource.bitmap.DrawableTransformation; import com.bumptech.glide.load.resource.bitmap.FitCenter; import com.bumptech.glide.load.resource.bitmap.VideoDecoder; +import com.bumptech.glide.load.resource.drawable.ResourceDrawableDecoder; import com.bumptech.glide.load.resource.gif.GifDrawable; import com.bumptech.glide.load.resource.gif.GifDrawableTransformation; import com.bumptech.glide.load.resource.gif.GifOptions; @@ -396,18 +397,11 @@ public T error(@DrawableRes int resourceId) { /** * Sets the {@link android.content.res.Resources.Theme} to apply when loading {@link Drawable}s - * for resource ids provided via {@link #error(int)}, {@link #placeholder(int)}, and {@link - * #fallback(Drawable)}. + * for resource ids, including those provided via {@link #error(int)}, {@link #placeholder(int)}, + * and {@link #fallback(Drawable)}. * - *

The theme is NOT applied in the decoder that will attempt to decode a given - * resource id model on Glide's background threads. The theme is used exclusively on the main - * thread to obtain placeholder/error/fallback drawables to avoid leaking Activities. - * - *

If the {@link android.content.Context} of the {@link android.app.Fragment} or {@link - * android.app.Activity} used to start this load has a different {@link - * android.content.res.Resources.Theme}, the {@link android.content.res.Resources.Theme} provided - * here will override the {@link android.content.res.Resources.Theme} of the {@link - * android.content.Context}. + *

The {@link android.content.res.Resources.Theme} provided here will override the {@link + * android.content.res.Resources.Theme} of the application {@link android.content.Context}. * * @param theme The theme to use when loading Drawables. * @return this request builder. @@ -418,11 +412,11 @@ public T theme(@Nullable Resources.Theme theme) { if (isAutoCloneEnabled) { return clone().theme(theme); } - + // TODO(b/234614365): Allow the theme option to be null. + Preconditions.checkNotNull(theme); this.theme = theme; fields |= THEME; - - return selfOrThrowIfLocked(); + return set(ResourceDrawableDecoder.THEME, theme); } /** diff --git a/library/test/src/test/java/com/bumptech/glide/request/RequestOptionsTest.java b/library/test/src/test/java/com/bumptech/glide/request/RequestOptionsTest.java index 67b4da7a8a..3eed10c228 100644 --- a/library/test/src/test/java/com/bumptech/glide/request/RequestOptionsTest.java +++ b/library/test/src/test/java/com/bumptech/glide/request/RequestOptionsTest.java @@ -582,7 +582,6 @@ public void testEqualsHashCode() { .addEqualityGroup( new RequestOptions(), new RequestOptions().skipMemoryCache(false), - new RequestOptions().theme(null), new RequestOptions().onlyRetrieveFromCache(false), new RequestOptions().useUnlimitedSourceGeneratorsPool(false)) .addEqualityGroup(