1bcf4a0dae04a4ad14287eeb34069a97c96fe9bb1Sam Juddpackage com.bumptech.glide.load.data;
20ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd
39bebdf4ee5dcaa1569bea3985dfe08f93ed8bd38Sam Juddimport com.bumptech.glide.Priority;
40ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd
50ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd/**
65f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd * An interface for lazily retrieving data that can be used to load a resource. A new instance is created per
75f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd * resource load by {@link com.bumptech.glide.load.model.ModelLoader}. {@link #loadData(Priority)} may or may not be
85f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd * called for any given load depending on whether or not the corresponding resource is cached. Cancel also may or may
95f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd * not be called. If {@link #loadData(Priority)} is called, then so {@link #cleanup()} will be called.
100ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd *
115f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd * @param <T> The type of data to be loaded (InputStream, byte[], File etc).
120ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd */
13bcf4a0dae04a4ad14287eeb34069a97c96fe9bb1Sam Juddpublic interface DataFetcher<T> {
140ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd
150ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd    /**
16bcf4a0dae04a4ad14287eeb34069a97c96fe9bb1Sam Judd     * Asynchronously fetch data from which a resource can be decoded. This will always be called on
170ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     * background thread so it is safe to perform long running tasks here. Any third party libraries called
180ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     * must be thread safe since this method will be called from a thread in a
190ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     * {@link java.util.concurrent.ExecutorService} that may have more than one background thread.
200ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     *
215f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     * This method will only be called when the corresponding resource is not in the cache.
225f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *
235f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     * <p>
245f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *     Note - this method will be run on a background thread so blocking I/O is safe.
255f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     * </p>
265f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *
279bebdf4ee5dcaa1569bea3985dfe08f93ed8bd38Sam Judd     * @param priority The priority with which the request should be completed.
2819611a3aaf2d45bb8274bdaabb4bf4b392e05feaRobert Papp     * @see #cleanup() where the data retuned will be cleaned up
290ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     */
305ba19a0e69ad3a651b8f13ba45de48a56b56ce36Sam Judd    T loadData(Priority priority) throws Exception;
310ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd
320ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd    /**
33bcf4a0dae04a4ad14287eeb34069a97c96fe9bb1Sam Judd     * Cleanup or recycle any resources used by this data fetcher. This method will be called in a finally block
34fe090f50f3040f4d478143a3e0ffa8cdf813fefcSam Judd     * after the data returned by {@link #loadData(Priority)} has been decoded by the
35fe090f50f3040f4d478143a3e0ffa8cdf813fefcSam Judd     * {@link com.bumptech.glide.load.ResourceDecoder}.
365f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *
375f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     * <p>
385f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *     Note - this method will be run on a background thread so blocking I/O is safe.
395f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     * </p>
405f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *
417f6d858477e6852fff5c5e49897fc2f9e7140edcSam Judd     */
425ba19a0e69ad3a651b8f13ba45de48a56b56ce36Sam Judd    void cleanup();
437f6d858477e6852fff5c5e49897fc2f9e7140edcSam Judd
447f6d858477e6852fff5c5e49897fc2f9e7140edcSam Judd    /**
4578bad2aa32f824f9e098b5058dfa3506a7ed3f62Sam Judd     * Returns a string uniquely identifying the data that this fetcher will fetch including the specific size.
4678bad2aa32f824f9e098b5058dfa3506a7ed3f62Sam Judd     *
4778bad2aa32f824f9e098b5058dfa3506a7ed3f62Sam Judd     * <p>
4878bad2aa32f824f9e098b5058dfa3506a7ed3f62Sam Judd     *     A hash of the bytes of the data that will be fetched is the ideal id but since that is in many cases
495f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *     impractical, urls, file paths, and uris are normally sufficient.
505f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     * </p>
515f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *
525f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     * <p>
535f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *     Note - this method will be run on the main thread so it should not perform blocking operations and should
545f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *     finish quickly.
5578bad2aa32f824f9e098b5058dfa3506a7ed3f62Sam Judd     * </p>
5678bad2aa32f824f9e098b5058dfa3506a7ed3f62Sam Judd     */
575ba19a0e69ad3a651b8f13ba45de48a56b56ce36Sam Judd    String getId();
5878bad2aa32f824f9e098b5058dfa3506a7ed3f62Sam Judd
5978bad2aa32f824f9e098b5058dfa3506a7ed3f62Sam Judd    /**
609bebdf4ee5dcaa1569bea3985dfe08f93ed8bd38Sam Judd     * A method that will be called when a load is no longer relevant and has been cancelled. This method does not need
619bebdf4ee5dcaa1569bea3985dfe08f93ed8bd38Sam Judd     * to guarantee that any in process loads do not finish. It also may be called before a load starts or after it
629bebdf4ee5dcaa1569bea3985dfe08f93ed8bd38Sam Judd     * finishes.
630ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     *
649bebdf4ee5dcaa1569bea3985dfe08f93ed8bd38Sam Judd     * <p>
659bebdf4ee5dcaa1569bea3985dfe08f93ed8bd38Sam Judd     *  The best way to use this method is to cancel any loads that have not yet started, but allow those that are in
665f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *  process to finish since its we typically will want to display the same resource in a different view in
679bebdf4ee5dcaa1569bea3985dfe08f93ed8bd38Sam Judd     *  the near future.
689bebdf4ee5dcaa1569bea3985dfe08f93ed8bd38Sam Judd     * </p>
695f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *
705f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     * <p>
715f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *     Note - this method will be run on the main thread so it should not perform blocking operations and should
725f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     *     finish quickly.
735f4610b54d517be58105bcf73ce3291ba79f9f40Sam Judd     * </p>
740ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd     */
755ba19a0e69ad3a651b8f13ba45de48a56b56ce36Sam Judd    void cancel();
760ae32dc10d668a04f9f0484d587aefe5a7210e1cSam Judd}
77