Skip to content

Commit

Permalink
Allow Drawable requests to be loaded with a specified theme
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 452415405
  • Loading branch information
sjudd authored and glide-copybara-robot committed Jun 1, 2022
1 parent 9e93e0b commit 17cae33
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions library/src/main/java/com/bumptech/glide/load/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void putAll(@NonNull Options other) {
values.putAll((SimpleArrayMap<Option<?>, Object>) other.values);
}

// TODO(b/234614365): Allow nullability.
@NonNull
public <T> Options set(@NonNull Option<T> option, @NonNull T value) {
values.put(option, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -23,6 +26,11 @@
* other packages.
*/
public class ResourceDrawableDecoder implements ResourceDecoder<Uri, Drawable> {

/** Specifies a {@link Theme} which will be used to load the drawable. */
public static final Option<Theme> 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.
Expand Down Expand Up @@ -62,7 +70,14 @@ public Resource<Drawable> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)}.
*
* <p>The theme is <em>NOT</em> 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.
*
* <p>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}.
* <p>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.
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 17cae33

Please sign in to comment.