Glide.java revision 15df8d073a97cfa2b589b547535a5b055c71bfbb
1d76f655fc29c5b3858d6657f22a5c656c3143767Sam Juddpackage com.bumptech.glide;
2d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd
3d76f655fc29c5b3858d6657f22a5c656c3143767Sam Juddimport android.content.Context;
40ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Juddimport android.graphics.Bitmap;
5b44604067356bb73cae3c910ac0e0044d26974a5Sam Juddimport android.graphics.drawable.Drawable;
64e8c4e61ae9f011c1bfccbace9c24aee61865bf0Sam Juddimport android.net.Uri;
7fcd787c911d5fbca2a34ff0963d4665543a03275Sam Juddimport android.os.ParcelFileDescriptor;
8b44604067356bb73cae3c910ac0e0044d26974a5Sam Juddimport android.view.View;
929f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Juddimport com.android.volley.RequestQueue;
100ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Juddimport com.bumptech.glide.loader.bitmap.model.GenericLoaderFactory;
110ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Juddimport com.bumptech.glide.loader.bitmap.model.ModelLoader;
120ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Juddimport com.bumptech.glide.loader.bitmap.model.ModelLoaderFactory;
13fcd787c911d5fbca2a34ff0963d4665543a03275Sam Juddimport com.bumptech.glide.loader.bitmap.model.file_descriptor.FileDescriptorFileLoader;
14fcd787c911d5fbca2a34ff0963d4665543a03275Sam Juddimport com.bumptech.glide.loader.bitmap.model.file_descriptor.FileDescriptorModelLoader;
15fcd787c911d5fbca2a34ff0963d4665543a03275Sam Juddimport com.bumptech.glide.loader.bitmap.model.file_descriptor.FileDescriptorResourceLoader;
16fcd787c911d5fbca2a34ff0963d4665543a03275Sam Juddimport com.bumptech.glide.loader.bitmap.model.file_descriptor.FileDescriptorStringLoader;
17fcd787c911d5fbca2a34ff0963d4665543a03275Sam Juddimport com.bumptech.glide.loader.bitmap.model.file_descriptor.FileDescriptorUriLoader;
18370ebf896886ab79e59644f0d82af9b8facdacd1Sam Juddimport com.bumptech.glide.loader.bitmap.model.stream.StreamByteArrayLoader;
19fcd787c911d5fbca2a34ff0963d4665543a03275Sam Juddimport com.bumptech.glide.loader.bitmap.model.stream.StreamFileLoader;
20fcd787c911d5fbca2a34ff0963d4665543a03275Sam Juddimport com.bumptech.glide.loader.bitmap.model.stream.StreamModelLoader;
210ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Juddimport com.bumptech.glide.loader.bitmap.model.stream.StreamResourceLoader;
22fcd787c911d5fbca2a34ff0963d4665543a03275Sam Juddimport com.bumptech.glide.loader.bitmap.model.stream.StreamStringLoader;
23fcd787c911d5fbca2a34ff0963d4665543a03275Sam Juddimport com.bumptech.glide.loader.bitmap.model.stream.StreamUriLoader;
24d76f655fc29c5b3858d6657f22a5c656c3143767Sam Juddimport com.bumptech.glide.resize.ImageManager;
259f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Juddimport com.bumptech.glide.resize.bitmap_recycle.BitmapPool;
269f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Juddimport com.bumptech.glide.resize.cache.DiskCache;
279f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Juddimport com.bumptech.glide.resize.cache.MemoryCache;
289f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Juddimport com.bumptech.glide.resize.request.Request;
299f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Juddimport com.bumptech.glide.resize.target.Target;
309f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Juddimport com.bumptech.glide.resize.target.ViewTarget;
312d923581ee57592181845aa98231eec988fab9f6Sam Juddimport com.bumptech.glide.volley.VolleyUrlLoader;
32d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd
33d76f655fc29c5b3858d6657f22a5c656c3143767Sam Juddimport java.io.File;
340ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Juddimport java.io.InputStream;
35d76f655fc29c5b3858d6657f22a5c656c3143767Sam Juddimport java.net.URL;
369ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Juddimport java.util.UUID;
3778790c322a30ac2e62498b79bdc965832bf2342dSam Judd
38d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd/**
399f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd * A singleton to present a simple static interface for building requests with {@link RequestBuilder} and maintaining
409f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd * an {@link ImageManager} and it's {@link BitmapPool}, {@link DiskCache} and {@link MemoryCache}.
4156e62046cf9e1bd2d341b6455a9b81b5757e269cSam Judd *
42058ef77ccc7fd40a80a2da72dc97695c7dda2c0bSam Judd * <p>
4356e62046cf9e1bd2d341b6455a9b81b5757e269cSam Judd * Note - This class is not thread safe.
44058ef77ccc7fd40a80a2da72dc97695c7dda2c0bSam Judd * </p>
45d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd */
46d76f655fc29c5b3858d6657f22a5c656c3143767Sam Juddpublic class Glide {
4729f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd    private static Glide GLIDE;
48c95a13d712f57271047e467d367e1145517b0c2aSam Judd    private final GenericLoaderFactory loaderFactory = new GenericLoaderFactory();
4929f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd    private final RequestQueue requestQueue;
50217da78d74c0045848383c323efdd8b8f495ca6eSam Judd    private final ImageManager imageManager;
51d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd
5256e62046cf9e1bd2d341b6455a9b81b5757e269cSam Judd    /**
5356e62046cf9e1bd2d341b6455a9b81b5757e269cSam Judd     * Get the singleton.
5456e62046cf9e1bd2d341b6455a9b81b5757e269cSam Judd     *
5556e62046cf9e1bd2d341b6455a9b81b5757e269cSam Judd     * @return the singleton
5656e62046cf9e1bd2d341b6455a9b81b5757e269cSam Judd     */
5729f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd    public static Glide get(Context context) {
5829f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd        if (GLIDE == null) {
5929f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd            GLIDE = new GlideBuilder(context).createGlide();
6029f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd        }
6129f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd
62d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd        return GLIDE;
63d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd    }
64d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd
659f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd    /**
669f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     * Returns false if the {@link Glide} singleton has not yet been created and can therefore be setup using
679f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     * {@link #setup(GlideBuilder)}.
689f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     *
699f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     * @see #setup(GlideBuilder)
709f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     */
7129f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd    public static boolean isSetup() {
7229f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd        return GLIDE != null;
7329f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd    }
7429f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd
759f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd    /**
769f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     * Creates the {@link Glide} singleton using the given builder. Can be used to set options like cache sizes and
779f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     * locations.
789f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     *
799f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     * @see #isSetup()
809f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     *
819f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     * @param builder The builder.
829f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     * @throws IllegalArgumentException if the Glide singleton has already been created.
839f0bef767aca7d0e40f9eba8c1ea52159bed2091Sam Judd     */
8429f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd    public static void setup(GlideBuilder builder) {
8529f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd        if (GLIDE != null) {
8629f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd            throw new IllegalArgumentException("Glide is already setup, check with isSetup() first");
8729f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd        }
8829f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd
8929f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd        GLIDE = builder.createGlide();
9029f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd    }
9129f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd
9229f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd    Glide(ImageManager imageManager, RequestQueue requestQueue) {
9329f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd        this.imageManager = imageManager;
9429f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd        this.requestQueue = requestQueue;
95896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd        register(File.class, ParcelFileDescriptor.class, new FileDescriptorFileLoader.Factory());
96896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd        register(File.class, InputStream.class, new StreamFileLoader.Factory());
97896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd        register(Integer.class, ParcelFileDescriptor.class, new FileDescriptorResourceLoader.Factory());
98896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd        register(Integer.class, InputStream.class, new StreamResourceLoader.Factory());
99896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd        register(String.class, ParcelFileDescriptor.class, new FileDescriptorStringLoader.Factory());
100896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd        register(String.class, InputStream.class, new StreamStringLoader.Factory());
101896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd        register(Uri.class, ParcelFileDescriptor.class, new FileDescriptorUriLoader.Factory());
102896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd        register(Uri.class, InputStream.class, new StreamUriLoader.Factory());
10329f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd        register(URL.class, InputStream.class, new VolleyUrlLoader.Factory(requestQueue));
104e13dcedf144c02927c122fae32179f460b21fef5Sam Judd    }
105e13dcedf144c02927c122fae32179f460b21fef5Sam Judd
106d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd    /**
10729f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd     * Returns the {@link ImageManager} Glide is using to load images.
108d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd     */
10929f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd    public ImageManager getImageManager() {
110d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd        return imageManager;
111d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd    }
112d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd
113d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd    /**
11429f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd     * Returns the {@link RequestQueue} Glide is using to fetch images over http/https.
115d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd     */
11629f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd    public RequestQueue getRequestQueue() {
11729f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd        return requestQueue;
118d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd    }
119d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd
120d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd    /**
121b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     * Cancel any pending loads Glide may have for the target and free any resources (such as {@link Bitmap}s) that may
122b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     * have been loaded for the target so they may be reused.
123b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     *
124b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     * @param target The Target to cancel loads for.
125b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     */
126b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd    public static void clear(Target target) {
127b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd        Request request = target.getRequest();
128b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd        if (request!= null) {
129b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd            request.clear();
130b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd        }
131b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd    }
132b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd
133b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd    /**
134b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     * Cancel any pending loads Glide may have for the view and free any resources that may have been loaded for the
135b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     * view.
136b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     *
137b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     * <p>
138b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     *     Note that this will only work if {@link View#setTag(Object)} is not called on this view outside of Glide.
139b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     * </p>
140b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     *
141b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     * @see #clear(Target).
142b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     *
143b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     * @param view The view to cancel loads and free resources for.
144b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     * @throws IllegalArgumentException if an object other than Glide's metadata is set as the view's tag.
145b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd     */
146b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd    public static void clear(View view) {
147b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd        Target viewTarget = new ClearTarget(view);
148b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd        clear(viewTarget);
149b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd    }
150b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd
151b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd    /**
152da9073795a5ca847f17d39cda1d964cfc1fa25dcSam Judd     * Use the given factory to build a {@link ModelLoader} for models of the given class. Generally the best use of
153da9073795a5ca847f17d39cda1d964cfc1fa25dcSam Judd     * this method is to replace one of the default factories or add an implementation for other similar low level
1548c7ae0d1551752a2ff6260dd807580a90020451eSam Judd     * models. Typically the {@link ModelRequest#using(StreamModelLoader)} or
1558c7ae0d1551752a2ff6260dd807580a90020451eSam Judd     * {@link ModelRequest#using(FileDescriptorModelLoader)} syntax is preferred because it directly links the model
1568c7ae0d1551752a2ff6260dd807580a90020451eSam Judd     * with the ModelLoader being used to load it.
157633966f8087dc4ef5e52ef0f559ec8090fbbc945Sam Judd     *
158058ef77ccc7fd40a80a2da72dc97695c7dda2c0bSam Judd     * <p>
1599c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd     *     Note - If a factory already exists for the given class, it will be replaced. If that factory is not being
1600ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     *     used for any other model class, {@link ModelLoaderFactory#teardown()}
1619c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd     *     will be called.
1629c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd     * </p>
1639c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd     *
164c95a13d712f57271047e467d367e1145517b0c2aSam Judd     * <p>
165c95a13d712f57271047e467d367e1145517b0c2aSam Judd     *     Note - The factory must not be an anonymous inner class of an Activity or another object that cannot be
166c95a13d712f57271047e467d367e1145517b0c2aSam Judd     *     retained statically.
167c95a13d712f57271047e467d367e1145517b0c2aSam Judd     * </p>
168c95a13d712f57271047e467d367e1145517b0c2aSam Judd     *
1698c7ae0d1551752a2ff6260dd807580a90020451eSam Judd     * @see ModelRequest#using(FileDescriptorModelLoader)
1708c7ae0d1551752a2ff6260dd807580a90020451eSam Judd     * @see ModelRequest#using(StreamModelLoader)
171896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd     *
172896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd     * @param modelClass The model class.
173896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd     * @param resourceClass The resource class the model loader will translate the model type into.
174896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd     * @param factory The factory to use.
175896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd     * @param <T> The type of the model.
176896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd     * @param <Y> the type of the resource.
1779c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd     */
178896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd    public <T, Y> void register(Class<T> modelClass, Class<Y> resourceClass, ModelLoaderFactory<T, Y> factory) {
179896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd        ModelLoaderFactory<T, Y> removed = loaderFactory.register(modelClass, resourceClass, factory);
1809c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd        if (removed != null) {
1819c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd            removed.teardown();
1829c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd        }
1839c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd    }
1849c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd
1859c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd    /**
18615df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * Build a {@link ModelLoader} for the given model class using registered {@link ModelLoaderFactory}s.
1879c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd     *
18815df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @see  #buildModelLoader(Object, Class, Context)
18915df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @see  #buildStreamModelLoader(Class, Context)
19015df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @see  #buildFileDescriptorModelLoader(Class, Context)
19115df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     *
19215df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @param modelClass The class to get a {@link ModelLoader} for.
19315df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @param resourceClass The resource class to get a {@link ModelLoader} for.
19415df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @param context Any context.
19515df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @param <T> The type of the model.
19615df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @param <Y> The type of the resource.
19715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @return A new {@link ModelLoader} for the given model class.
1989c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd     */
1990ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd    public static <T, Y> ModelLoader<T, Y> buildModelLoader(Class<T> modelClass, Class<Y> resourceClass,
2000ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd            Context context) {
20115df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd        return Glide.get(context).getLoaderFactory().buildModelLoader(modelClass, resourceClass, context);
20215df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd    }
20315df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd
20415df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd    /**
20515df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * A convenience method to build a {@link ModelLoader} for a given model object using registered
20615df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * {@link ModelLoaderFactory}s.
20715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     *
20815df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @see #buildModelLoader(Class, Class, Context)
20915df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     *
21015df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @param model A non null model object whose class we will get a {@link ModelLoader} for.
21115df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @param resourceClass The resource class to get a {@link ModelLoader} for.
21215df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @param context Any context.
21315df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @param <T> The type of the model.
21415df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @param <Y> The type of the resource.
21515df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @return A new {@link ModelLoader} for the given model and resource classes.
21615df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @throws NullPointerException if model is null.
21715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     */
21815df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd    @SuppressWarnings("unchecked")
21915df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd    public static <T, Y> ModelLoader<T, Y> buildModelLoader(T model, Class<Y> resourceClass, Context context) {
22015df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd        if (model == null) {
22115df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            throw new NullPointerException("You must pass in a non-null model (URL, file path, etc).");
22215df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd        }
22315df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd        return buildModelLoader((Class<T>) model.getClass(), resourceClass, context);
2249c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd    }
2259c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd
226ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd    /**
22715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * A method to build a {@link ModelLoader} for the given model that produces {@link InputStream}s using a registered
22815df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * factory.
229ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd     *
230ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd     * @see #buildModelLoader(Class, Class, android.content.Context)
231ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd     */
232ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd    public static <T> ModelLoader<T, InputStream> buildStreamModelLoader(Class<T> modelClass, Context context) {
233ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd        return buildModelLoader(modelClass, InputStream.class, context);
234ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd    }
235ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd
236ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd    /**
23715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * A method to build a {@link ModelLoader} for the given model that produces {@link InputStream}s using a registered
23815df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * factory.
23915df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     *
24015df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @see #buildModelLoader(Object, Class, Context)
24115df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     */
24215df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd    public static <T> ModelLoader<T, InputStream> buildStreamModelLoader(T model, Context context) {
24315df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd        return buildModelLoader(model, InputStream.class, context);
24415df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd    }
24515df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd
24615df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd    /**
24715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * A method to build a {@link ModelLoader} for the given model class that produces
248ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd     * {@link ParcelFileDescriptor}s using a registered factory.
249ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd     *
250ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd     * @see #buildModelLoader(Class, Class, android.content.Context)
251ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd     */
252ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd    public static <T> ModelLoader<T, ParcelFileDescriptor> buildFileDescriptorModelLoader(Class<T> modelClass,
253ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd            Context context) {
254ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd        return buildModelLoader(modelClass, ParcelFileDescriptor.class, context);
255ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd    }
256ebf002122d17f21676230a4499894cc9f103d0b4Sam Judd
2578c7ae0d1551752a2ff6260dd807580a90020451eSam Judd    /**
25815df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * A method to build a {@link ModelLoader} for the given model class that produces
25915df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * {@link ParcelFileDescriptor}s using a registered factory.
26015df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     *
26115df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     * @see #buildModelLoader(Object, Class, android.content.Context)
26215df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd     */
26315df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd    public static <T> ModelLoader<T, ParcelFileDescriptor> buildFileDescriptorModelLoader(T model, Context context) {
26415df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd        return buildModelLoader(model, ParcelFileDescriptor.class, context);
26515df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd    }
26615df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd
26715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd    /**
2688c7ae0d1551752a2ff6260dd807580a90020451eSam Judd     * Begin a load with Glide by passing in a context.
2699c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd     *
2708c7ae0d1551752a2ff6260dd807580a90020451eSam Judd     * @param context Any context, will not be retained.
2718c7ae0d1551752a2ff6260dd807580a90020451eSam Judd     * @return A model request to pass in the object representing the image to be loaded.
2729c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd     */
2738c7ae0d1551752a2ff6260dd807580a90020451eSam Judd    public static ModelRequest with(Context context) {
2748c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        return new ModelRequest(context);
2750ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd    }
2769c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd
27715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd    private GenericLoaderFactory getLoaderFactory() {
278b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd        return loaderFactory;
279b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd    }
280b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd
2810ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd    /**
2820ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd     * A {@link RequestBuilder} builder that returns a request for a model that represents an image.
2830ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     */
2848c7ae0d1551752a2ff6260dd807580a90020451eSam Judd    public static class ModelRequest {
2858c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        private final Context context;
2860ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd
2878c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        private ModelRequest(Context context) {
2888c7ae0d1551752a2ff6260dd807580a90020451eSam Judd            this.context = context;
2898c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        }
2909c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd
2918c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        /**
2928c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * Set the {@link ModelLoader} to use for for a new load where the model loader translates from a model to an
2938c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * {@link InputStream} resource for loading images.
2948c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         *
2958c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @param modelLoader The model loader to use.
2968c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @param <T> The type of the model.
2978c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @return A new {@link ImageModelRequest}.
2988c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         */
2998c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        public <T> ImageModelRequest<T> using(final StreamModelLoader<T> modelLoader) {
30015df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            return new ImageModelRequest<T>(context, modelLoader);
3018c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        }
302b1ef5cd4b890c9f5020f3f9a03f2bd24a2084d42Sam Judd
3038c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        /**
304370ebf896886ab79e59644f0d82af9b8facdacd1Sam Judd         * A convenience method to use a {@link StreamByteArrayLoader} to decode an image from a byte array.
305370ebf896886ab79e59644f0d82af9b8facdacd1Sam Judd         *
306370ebf896886ab79e59644f0d82af9b8facdacd1Sam Judd         * @param modelLoader The byte array loader.
307370ebf896886ab79e59644f0d82af9b8facdacd1Sam Judd         * @return A new {@link ImageModelRequest}.
308370ebf896886ab79e59644f0d82af9b8facdacd1Sam Judd         */
309370ebf896886ab79e59644f0d82af9b8facdacd1Sam Judd        public ImageModelRequest<byte[]> using(StreamByteArrayLoader modelLoader) {
31015df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            return new ImageModelRequest<byte[]>(context, modelLoader);
311370ebf896886ab79e59644f0d82af9b8facdacd1Sam Judd        }
312370ebf896886ab79e59644f0d82af9b8facdacd1Sam Judd
313370ebf896886ab79e59644f0d82af9b8facdacd1Sam Judd        /**
3148c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * Set the {@link ModelLoader} to use for a new load where the model loader translates from a model to an
3158c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * {@link ParcelFileDescriptor} resource for loading video thumbnails.
3168c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         *
3178c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @param modelLoader The model loader to use.
3188c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @param <T> The type of the model.
3198c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @return A new {@link VideoModelRequest}.
3208c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         */
3218c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        public <T> VideoModelRequest<T> using(final FileDescriptorModelLoader<T> modelLoader) {
32215df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            return new VideoModelRequest<T>(context, modelLoader);
3238c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        }
324633966f8087dc4ef5e52ef0f559ec8090fbbc945Sam Judd
3258c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        /**
3260ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd         * Use the {@link ModelLoaderFactory} currently registered for {@link String} to load the image represented by
3270ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd         * the given {@link String}. Defaults to {@link StreamStringLoader.Factory} and {@link StreamStringLoader} to
3280ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd         * load the given model.
3298c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         *
3308c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @see #using(StreamModelLoader)
3318c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         *
3320ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd         * @param string The string representing the image. Must be either a path, or a uri handled by
3330ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd         *      {@link StreamUriLoader}
3340ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd         * @return A {@link RequestBuilder} to set options for the load and ultimately the target to load the model into
3358c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         */
3360ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd        public RequestBuilder<String> load(String string) {
3370ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd            return new RequestBuilder<String>(context, string);
3388c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        }
339633966f8087dc4ef5e52ef0f559ec8090fbbc945Sam Judd
3408c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        /**
3418c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * Use the {@link ModelLoaderFactory} currently registered for {@link Uri} to load the image at the given uri.
3428c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * Defaults to {@link StreamUriLoader.Factory} and {@link StreamUriLoader}.
3438c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         *
3448c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @see #using(StreamModelLoader)
3458c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         *
3468c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @param uri The uri representing the image. Must be a uri handled by {@link StreamUriLoader}
3470ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd         * @return A {@link RequestBuilder} to set options for the load and ultimately the target to load the model into
3488c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         */
3490ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd        public RequestBuilder<Uri> load(Uri uri) {
3500ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd            return new RequestBuilder<Uri>(context, uri);
3518c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        }
352b1ef5cd4b890c9f5020f3f9a03f2bd24a2084d42Sam Judd
3538c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        /**
3548c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * Use the {@link ModelLoaderFactory} currently registered for {@link File} to load the image represented by the
3558c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * given {@link File}. Defaults to {@link StreamFileLoader.Factory} and {@link StreamFileLoader} to load the given model.
3568c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         *
3578c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @see #using(StreamModelLoader)
3588c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         *
3598c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @param file The File containing the image
3600ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd         * @return A {@link RequestBuilder} to set options for the load and ultimately the target to load the model into
3618c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         */
3620ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd        public RequestBuilder<File> load(File file) {
3630ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd            return new RequestBuilder<File>(context, file);
3648c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        }
3659c04fc7a0c1d5c9e8f54660b0f954f754100f250Sam Judd
3668c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        /**
3678c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * Use the {@link ModelLoaderFactory} currently registered for {@link Integer} to load the image represented by the
3688c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * given {@link Integer} resource id. Defaults to {@link StreamResourceLoader.Factory} and {@link StreamResourceLoader} to load
3698c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * the given model.
3708c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         *
3718c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @see #using(StreamModelLoader)
3728c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         *
3738c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @param resourceId the id of the resource containing the image
37415df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd         * @return A {@link RequestBuilder} to set options for the load and ultimately the target to load the model
37515df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd         * into.
3768c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         */
3770ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd        public RequestBuilder<Integer> load(Integer resourceId) {
3780ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd            return new RequestBuilder<Integer>(context, resourceId);
3798c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        }
38002ddf36100d8b90b0ada2ef1dc67cb0ce6f73530Sam Judd
3818c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        /**
38215df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd         * Use the {@link ModelLoaderFactory} currently registered for the given model type to load the image
38315df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd         * represented by the given model.
3848c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         *
3858c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @param model The model to load.
3868c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @param <T> The type of the model to load.
38715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd         * @return A {@link RequestBuilder} to set options for the load and ultimately the target to load the image
38815df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd         * into.
3898c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         */
3908c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        @SuppressWarnings("unused")
3910ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd        public <T> RequestBuilder<T> loadFromImage(T model) {
39215df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            return new ImageModelRequest<T>(context, buildStreamModelLoader(model, context))
39329f3a82eaaf784c3722e7673b6ea6acff9449a4dSam Judd                    .load(model);
39402ddf36100d8b90b0ada2ef1dc67cb0ce6f73530Sam Judd        }
39502ddf36100d8b90b0ada2ef1dc67cb0ce6f73530Sam Judd
3968c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        /**
3979ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         * Use the {@link ModelLoaderFactory} currently registered for {@link URL} to load the image represented by the
3989ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         * given {@link URL}. Defaults to {@link VolleyUrlLoader.Factory} and {@link VolleyUrlLoader} to load the given
3999ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         * model.
4009ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         *
4019ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         * @see #using(StreamModelLoader)
4029ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         *
4039ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         * @param url The URL representing the image.
4040ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd         * @return A {@link RequestBuilder} to set options for the load and ultimately the target to load the model into
4059ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         */
4060ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd        public RequestBuilder<URL> loadFromImage(URL url) {
40715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            return new ImageModelRequest<URL>(context, buildStreamModelLoader(url, context))
40815df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd                    .load(url);
4099ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd        }
4109ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd
4119ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd        /**
4129ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         * Use a new {@link StreamByteArrayLoader} to load an image from the given model.
4139ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         *
4149ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         * @see #loadFromImage(byte[])
4159ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         *
4169ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         * @param model The data to load.
4179ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         * @param id A unique id that identifies the image represented by the model suitable for use as a cache key
4189ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         *           (url, filepath etc). If there is no suitable id, use {@link #loadFromImage(byte[])} instaed.
4190ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd         * @return A {@link RequestBuilder} to set options for the load and ultimately the target to load the image into.
4209ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         */
4210ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd        public RequestBuilder<byte[]> loadFromImage(byte[] model, final String id) {
42215df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            return new ImageModelRequest<byte[]>(context, new StreamByteArrayLoader() {
4239ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd                @Override
4249ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd                public String getId(byte[] model) {
4259ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd                    return id;
4269ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd                }
42715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            }).load(model);
4289ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd        }
4299ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd
4309ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd        /**
4319ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         * Use a new {@link StreamByteArrayLoader} to load an image from the given model. Suitable when there is no
4329ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         * simple id that represents the given data.
4339ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         *
4349ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         * @param model the data to load.
43515df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd         * @return A {@link RequestBuilder} to set options for the load and ultimately the target to load the image
43615df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd         * into.
4379ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd         */
4380ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd        public RequestBuilder<byte[]> loadFromImage(byte[] model) {
4399ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd            return loadFromImage(model, UUID.randomUUID()
4409ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd                    .toString());
4419ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd        }
4429ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd
4439ab1117d741cf18f7606685f96cba216bd5c1cb5Sam Judd        /**
4448c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * Use the {@link ModelLoaderFactory} currently registered for the given model type for
4458c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * {@link ParcelFileDescriptor}s to load a thumbnail for the video represented by the given model.
4468c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         *
4478c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @param model The model to load.
4488c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         * @param <T> The type of the model to load.
44915df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd         * @return A {@link RequestBuilder} to set options for the load an ultimately the target to load the thumbnail
45015df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd         * into.
4518c7ae0d1551752a2ff6260dd807580a90020451eSam Judd         */
4528c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        @SuppressWarnings("unused")
4530ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd        public <T> RequestBuilder<T> loadFromVideo(T model) {
45415df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            return new VideoModelRequest<T>(context, buildFileDescriptorModelLoader(model, context))
4558c7ae0d1551752a2ff6260dd807580a90020451eSam Judd                    .loadFromVideo(model);
4568c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        }
45702ddf36100d8b90b0ada2ef1dc67cb0ce6f73530Sam Judd    }
45802ddf36100d8b90b0ada2ef1dc67cb0ce6f73530Sam Judd
459896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd    /**
460896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd     * A helper class for building requests with custom {@link ModelLoader}s that translate models to
461896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd     * {@link ParcelFileDescriptor} resources for loading video thumbnails.
462896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd     *
463896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd     * @param <T> The type of the model.
464896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd     */
465896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd    public static class VideoModelRequest<T> {
4668c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        private final Context context;
46715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd        private final ModelLoader<T, ParcelFileDescriptor> loader;
468fcd787c911d5fbca2a34ff0963d4665543a03275Sam Judd
46915df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd        private VideoModelRequest(Context context, ModelLoader<T, ParcelFileDescriptor> loader) {
4708c7ae0d1551752a2ff6260dd807580a90020451eSam Judd            this.context = context;
47115df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            this.loader = loader;
472fcd787c911d5fbca2a34ff0963d4665543a03275Sam Judd        }
473fcd787c911d5fbca2a34ff0963d4665543a03275Sam Judd
4740ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd        public RequestBuilder<T> loadFromVideo(T model) {
47515df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            return new RequestBuilder<T>(context, model, null, loader);
476fcd787c911d5fbca2a34ff0963d4665543a03275Sam Judd        }
477fcd787c911d5fbca2a34ff0963d4665543a03275Sam Judd    }
478fcd787c911d5fbca2a34ff0963d4665543a03275Sam Judd
479b1ef5cd4b890c9f5020f3f9a03f2bd24a2084d42Sam Judd    /**
4800ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     * A helper class for building requests with custom {@link ModelLoader}s that translate models to
481896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd     * {@link InputStream} resources for loading images.
4820ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     *
4830ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     * @param <T> The type of the model.
4840ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     */
485896af010afa2c88aa6b63fc80c1a412357caed97Sam Judd    public static class ImageModelRequest<T> {
4868c7ae0d1551752a2ff6260dd807580a90020451eSam Judd        private final Context context;
48715df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd        private final ModelLoader<T, InputStream> loader;
4880ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd
48915df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd        private ImageModelRequest(Context context, ModelLoader<T, InputStream> loader) {
4908c7ae0d1551752a2ff6260dd807580a90020451eSam Judd            this.context = context;
49115df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            this.loader = loader;
4920ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd        }
4930ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd
4940ba0245a97116b2f2c7bd67213961ac4e2dc3e56Sam Judd        public RequestBuilder<T> load(T model) {
49515df8d073a97cfa2b589b547535a5b055c71bfbbSam Judd            return new RequestBuilder<T>(context, model, loader, null);
496d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd        }
497d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd    }
498d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd
499b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd    private static class ClearTarget extends ViewTarget<View> {
500b8963a023ceb009cbf7f73a4506e7af7e693a219Sam Judd        public ClearTarget(View view) {
501b44604067356bb73cae3c910ac0e0044d26974a5Sam Judd            super(view);
502b44604067356bb73cae3c910ac0e0044d26974a5Sam Judd        }
503b44604067356bb73cae3c910ac0e0044d26974a5Sam Judd
504b44604067356bb73cae3c910ac0e0044d26974a5Sam Judd        @Override
505b44604067356bb73cae3c910ac0e0044d26974a5Sam Judd        public void onImageReady(Bitmap bitmap) { }
506b44604067356bb73cae3c910ac0e0044d26974a5Sam Judd
507b44604067356bb73cae3c910ac0e0044d26974a5Sam Judd        @Override
508b44604067356bb73cae3c910ac0e0044d26974a5Sam Judd        public void setPlaceholder(Drawable placeholder) { }
509b44604067356bb73cae3c910ac0e0044d26974a5Sam Judd    }
510d76f655fc29c5b3858d6657f22a5c656c3143767Sam Judd}
511