package com.bumptech.glide; import android.content.Context; import android.os.ParcelFileDescriptor; import com.bumptech.glide.load.model.ImageVideoModelLoader; import com.bumptech.glide.load.model.ImageVideoWrapper; import com.bumptech.glide.load.model.ModelLoader; import com.bumptech.glide.load.resource.drawable.GlideDrawable; import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper; import com.bumptech.glide.load.resource.transcode.ResourceTranscoder; import com.bumptech.glide.manager.Lifecycle; import com.bumptech.glide.manager.RequestTracker; import com.bumptech.glide.provider.DataLoadProvider; import com.bumptech.glide.provider.FixedLoadProvider; import com.bumptech.glide.request.FutureTarget; import com.bumptech.glide.request.target.Target; import java.io.File; import java.io.InputStream; /** * A class for creating a load request that loads either an animated GIF drawable or a Bitmap drawable directly, or * adds an {@link com.bumptech.glide.load.resource.transcode.ResourceTranscoder} to transcode the data into a * resource type other than a {@link android.graphics.drawable.Drawable}. * * @param The type of model to use to load the {@link android.graphics.drawable.BitmapDrawable} or * {@link com.bumptech.glide.load.resource.gif.GifDrawable}. */ public class DrawableTypeRequest extends DrawableRequestBuilder implements DownloadOptions { private final ModelLoader streamModelLoader; private final ModelLoader fileDescriptorModelLoader; private final RequestManager.OptionsApplier optionsApplier; private static FixedLoadProvider buildProvider(Glide glide, ModelLoader streamModelLoader, ModelLoader fileDescriptorModelLoader, Class resourceClass, Class transcodedClass, ResourceTranscoder transcoder) { if (streamModelLoader == null && fileDescriptorModelLoader == null) { return null; } if (transcoder == null) { transcoder = glide.buildTranscoder(resourceClass, transcodedClass); } DataLoadProvider dataLoadProvider = glide.buildDataProvider(ImageVideoWrapper.class, resourceClass); ImageVideoModelLoader modelLoader = new ImageVideoModelLoader(streamModelLoader, fileDescriptorModelLoader); return new FixedLoadProvider(modelLoader, transcoder, dataLoadProvider); } DrawableTypeRequest(Class modelClass, ModelLoader streamModelLoader, ModelLoader fileDescriptorModelLoader, Context context, Glide glide, RequestTracker requestTracker, Lifecycle lifecycle, RequestManager.OptionsApplier optionsApplier) { super(context, modelClass, buildProvider(glide, streamModelLoader, fileDescriptorModelLoader, GifBitmapWrapper.class, GlideDrawable.class, null), glide, requestTracker, lifecycle); this.streamModelLoader = streamModelLoader; this.fileDescriptorModelLoader = fileDescriptorModelLoader; this.optionsApplier = optionsApplier; } /** * Attempts to always load the resource as a {@link android.graphics.Bitmap}, even if it could actually be animated. * * @return A new request builder for loading a {@link android.graphics.Bitmap} */ public BitmapTypeRequest asBitmap() { return optionsApplier.apply(new BitmapTypeRequest(this, streamModelLoader, fileDescriptorModelLoader, optionsApplier)); } /** * Attempts to always load the resource as a {@link com.bumptech.glide.load.resource.gif.GifDrawable}. *

* If the underlying data is not a GIF, this will fail. As a result, this should only be used if the model * represents an animated GIF and the caller wants to interact with the GIfDrawable directly. Normally using * just an {@link com.bumptech.glide.DrawableTypeRequest} is sufficient because it will determine whether or * not the given data represents an animated GIF and return the appropriate animated or not animated * {@link android.graphics.drawable.Drawable} automatically. *

* * @return A new request builder for loading a {@link com.bumptech.glide.load.resource.gif.GifDrawable}. */ public GifTypeRequest asGif() { return optionsApplier.apply(new GifTypeRequest(this, streamModelLoader, optionsApplier)); } /** * {@inheritDoc} */ public > Y downloadOnly(Y target) { return getDownloadOnlyRequest().downloadOnly(target); } /** * {@inheritDoc} */ public FutureTarget downloadOnly(int width, int height) { return getDownloadOnlyRequest().downloadOnly(width, height); } private GenericTranscodeRequest getDownloadOnlyRequest() { return optionsApplier.apply(new GenericTranscodeRequest(File.class, this, streamModelLoader, InputStream.class, File.class, optionsApplier)); } }