package com.bumptech.glide.load.model.stream; import android.content.Context; import com.bumptech.glide.Glide; import com.bumptech.glide.load.model.ModelCache; import com.bumptech.glide.load.model.GlideUrl; import com.bumptech.glide.load.model.ModelLoader; import com.bumptech.glide.load.data.DataFetcher; import java.io.InputStream; /** * A base class for loading images over http/https. Can be subclassed for use with any model that can be translated * in to an image. * * @param The type of the model */ public abstract class BaseGlideUrlLoader implements StreamModelLoader { private final ModelLoader concreteLoader; private final ModelCache modelCache; public BaseGlideUrlLoader(Context context) { this(context, null); } public BaseGlideUrlLoader(Context context, ModelCache modelCache) { this(Glide.buildModelLoader(GlideUrl.class, InputStream.class, context), modelCache); } @SuppressWarnings("unused") public BaseGlideUrlLoader(ModelLoader concreteLoader) { this(concreteLoader, null); } public BaseGlideUrlLoader(ModelLoader concreteLoader, ModelCache modelCache) { this.concreteLoader = concreteLoader; this.modelCache = modelCache; } @Override public DataFetcher getResourceFetcher(T model, int width, int height) { GlideUrl result = null; if (modelCache != null) { result = modelCache.get(model, width, height); } if (result == null) { String stringURL = getUrl(model, width, height); result = new GlideUrl(stringURL); if (modelCache != null) { modelCache.put(model, width, height, result); } } return concreteLoader.getResourceFetcher(result, width, height); } /** * Get a valid url http:// or https:// for the given model and dimensions as a string * * @param model The model * @param width The width of the view/target the image will be loaded into * @param height The height of the view/target the image will be loaded into * @return The String url */ protected abstract String getUrl(T model, int width, int height); }