Android Open Source - webimageloader Image Loader






From Project

Back to project page webimageloader.

License

The source code is released under:

Apache License

If you think the Android project webimageloader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.webimageloader;
//from ww  w.j  a v  a 2  s .c o m
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import com.webimageloader.content.ContentURLStreamHandler;
import com.webimageloader.loader.DiskLoader;
import com.webimageloader.loader.LoaderManager;
import com.webimageloader.loader.MemoryCache;
import com.webimageloader.loader.NetworkLoader;
import com.webimageloader.transformation.Transformation;

import java.io.File;
import java.io.IOException;
import java.net.URLStreamHandler;

/**
 * This is the main class of WebImageLoader which can be constructed using a
 * {@link Builder}. It's often more convenient to use the provided
 * {@link com.webimageloader.ext.ImageHelper} to load images.
 * <p>
 * It's safe to call the methods on this class from any thread. However, callbacks
 * will always be done on the UI thread if not otherwise specified.
 *
 * @author Alexander Blom <alexanderblom.se>
 */
public interface ImageLoader {
    static final String TAG = "ImageLoader";

    /**
     * Listener for a request which will always be called on the main thread of
     * the application. You should try to avoid keeping a reference to the tag,
     * for example by declaring this as a static inner class and then using the
     * passed tag when handling the callbacks. This allows us to properly let
     * the tag be GC'ed.
     *
     * @author Alexander Blom <alexanderblom.se>
     *
     * @param <T> the tag class
     */
    public interface Listener<T> {
        /**
         * Called if the request succeeded
         *
         * @param tag the tag which was passed in
         * @param b the resulting bitmap
         */
        void onSuccess(T tag, Bitmap b);

        /**
         * Called if the request failed
         *
         * @param tag the tag which was passed in
         * @param t the reason the request failed
         */
        void onError(T tag, Throwable t);
    }

    /**
     * Listener for progress updates
     *
     * @author Alexander Blom <alexanderblom.se>
     */
    public interface ProgressListener {
        /**
         * Called when there is a progress update, float ranges
         * from 0f to 1f
         * @param value progress in the range 0f-1f
         */
        void onProgress(float value);
    }

    /**
     * Get memory cache debug info
     *
     * @return debug info or null if not available
     */
    MemoryCache.DebugInfo getMemoryCacheInfo();

    /**
     * Get the memory cache
     *
     * @return memory cache or null if not available
     */
    MemoryCache getMemoryCache();

    /**
     * Load the specified request blocking the calling thread.
     *
     * @param url the url to load
     * @return the bitmap
     * @throws IOException if the load failed
     *
     * @see #loadBlocking(Request)
     */
    Bitmap loadBlocking(String url) throws IOException;

    /**
     * Load the specified request blocking the calling thread.
     *
     * @param url the url to load
     * @param transformation can be null
     * @return the bitmap
     * @throws IOException if the load failed
     *
     * @see #loadBlocking(Request)
     */
    Bitmap loadBlocking(String url, Transformation transformation) throws IOException;

    /**
     * Load the specified request blocking the calling thread.
     *
     * @param request the request to load
     * @return the bitmap
     * @throws IOException if the load failed
     */
    Bitmap loadBlocking(Request request) throws IOException;

    /**
     * Load the specified request blocking the calling thread. The progress
     * updates happen on a background thread.
     *
     * @param request the request to load
     * @param progressListener called when there is a progress update
     * @return the bitmap
     * @throws IOException if the load failed
     */
    Bitmap loadBlocking(Request request, ProgressListener progressListener) throws IOException;

    /**
     * Used to prime the file and memory cache. It's safe to later call load
     * with the same request, it will automatically be reused.
     *
     * @param url which resource to get
     *
     * @see #preload(Request)
     */
    void preload(String url);

    /**
     * Used to prime the file and memory cache. It's safe to later call load
     * with the same request, it will automatically be reused.
     *
     * @param url which resource to get
     * @param transformation can be null
     *
     * @see #preload(Request)
     */
    void preload(String url, Transformation transformation);

    /**
     * Used to prime the file and memory cache. It's safe to later call load
     * with the same request, it will automatically be reused.
     *
     * @param request the request to preload
     */
    void preload(Request request);

    /**
     * Load an image from an url with the given listener. Previously pending
     * request for this tag will be automatically cancelled.
     *
     * @param tag used to determine when we this request should be cancelled
     * @param url which resource to get
     * @param listener called when the request has finished or failed
     * @return the bitmap if it was already loaded
     *
     * @see #load(Object, Request, Listener)
     */
    <T> Bitmap load(T tag, String url, Listener<T> listener);

    /**
     * Load an image from an url with the given listener. Previously pending
     * request for this tag will be automatically cancelled.
     *
     * @param tag used to determine when we this request should be cancelled
     * @param url which resource to get
     * @param transformation can be null
     * @param listener called when the request has finished or failed
     * @return the bitmap if it was already loaded
     *
     * @see #load(Object, Request, Listener)
     */
    <T> Bitmap load(T tag, String url, Transformation transformation, Listener<T> listener);

    /**
     * Load an image from an url with the given listener. Previously pending
     * request for this tag will be automatically cancelled.
     *
     * @param tag used to determine when we this request should be cancelled
     * @param request what to to fetch
     * @param listener called when the request has finished or failed
     * @return the bitmap if it was already loaded
     */
    <T> Bitmap load(T tag, Request request, Listener<T> listener);

    /**
     * Load an image from an url with the given listeners. Previously pending
     * request for this tag will be automatically cancelled.
     *
     * @param tag used to determine when we this request should be cancelled
     * @param request what to to fetch
     * @param listener called when the request has finished or failed
     * @param progressListener called when there is a progress update
     * @return the bitmap if it was already loaded
     */
    <T> Bitmap load(T tag, Request request, Listener<T> listener, ProgressListener progressListener);

    /**
     * Cancel any pending requests for this tag.
     *
     * @param tag the tag
     */
    <T> void cancel(T tag);

    void destroy();

    /**
     * Builder class used to construct a {@link com.webimageloader.ImageLoader}.
     *
     * @author Alexander Blom <alexanderblom.se>
     */
    public static class Builder {
        public static final long MAX_AGE_INFINITY = Constants.MAX_AGE_INFINITY;

        private Context context;

        private NetworkLoader.Builder networkBuilder;

        private DiskLoader diskLoader;
        private MemoryCache memoryCache;

        /**
         * Create a new builder
         * @param context the context
         */
        public Builder(Context context) {
            this.context = context.getApplicationContext();

            networkBuilder = new NetworkLoader.Builder();
        }

        /**
         * Enable the disk cache
         * @param cacheDir cache location
         * @param maxSize max size of the cache
         * @return this builder
         */
        public Builder enableDiskCache(File cacheDir, int maxSize) {
            return enableDiskCache(cacheDir, maxSize, Constants.DEFAULT_DISK_THREADS);
        }

        /**
         * Enable the disk cache
         * @param cacheDir cache location
         * @param maxSize max size of the cache
         * @param threadCount number of threads
         * @return this builder
         */
        public Builder enableDiskCache(File cacheDir, int maxSize, int threadCount) {
            try {
                diskLoader = DiskLoader.open(cacheDir, maxSize, threadCount);
            } catch (IOException e) {
                Log.e(TAG, "Disk cache not available", e);
            }

            return this;
        }

        /**
         * Enable the memory cache
         * @param maxSize max size of the cache
         * @return this builder
         */
        public Builder enableMemoryCache(int maxSize) {
            memoryCache = new MemoryCache(maxSize);

            return this;
        }

        /**
         * Set the number of threads to be used for downloading images
         * @param count thread count
         * @return this builder
         */
        public Builder setNetworkThreadCount(int count) {
            networkBuilder.setThreadCount(count);

            return this;
        }

        /**
         * Add a URL scheme handler
         * @param scheme the scheme to handle
         * @param handler the handler
         * @return this builder
         *
         * @see java.net.URLStreamHandler
         */
        public Builder addURLSchemeHandler(String scheme, URLStreamHandler handler) {
            networkBuilder.addURLSchemeHandler(scheme, handler);

            return this;
        }

        /**
         * Set a connection factory, for example to provide your own connection implementation
         * @param factory the factory which creates connections
         * @return this builder
         */
        public Builder setConnectionFactory(ConnectionFactory factory) {
            networkBuilder.setConnectionFactory(factory);

            return this;
        }

        /**
         * Set a connection handler
         * @param handler handler to handle connections
         * @return this builder
         */
        public Builder setConnectionHandler(ConnectionHandler handler) {
            networkBuilder.setConnectionHandler(handler);

            return this;
        }

        /**
         * Set connection timeout, by default 10 seconds
         * @param connectionTimeout the connection timeout
         * @return this builder
         *
         * @see java.net.URLConnection#setConnectTimeout(int)
         */
        public Builder setConnectionTimeout(int connectionTimeout) {
            networkBuilder.setConnectionTimeout(connectionTimeout);

            return this;
        }


        /**
         * Set read timeout, by default 15 seconds
         * @param readTimeout the read timeout
         * @return this builder
         *
         * @see java.net.URLConnection#setReadTimeout(int)
         */
        public Builder setReadTimeout(int readTimeout) {
            networkBuilder.setReadTimeout(readTimeout);

            return this;
        }

        /**
         * Set what max-age to use when a response doesn't have one set
         * @param maxAge default max-age, 0 means infinity
         * @return this builder
         */
        public Builder setDefaultCacheMaxAge(long maxAge) {
            networkBuilder.setDefaultCacheMaxAge(maxAge);

            return this;
        }

        /**
         * Override max-age and expires headers
         * @param maxAge max-age to use for all requests, 0 means infinity
         * @return this builder
         */
        public Builder setCacheMaxAge(long maxAge) {
            networkBuilder.setCacheMaxAge(maxAge);

            return this;
        }

        /**
         * Build the {@link com.webimageloader.ImageLoader} from the settings in this builder
         * @return a {@link com.webimageloader.ImageLoader}
         */
        public ImageLoader build() {
            URLStreamHandler handler = new ContentURLStreamHandler(context.getContentResolver());
            networkBuilder.addURLSchemeHandler(ContentResolver.SCHEME_CONTENT, handler);
            networkBuilder.addURLSchemeHandler(ContentResolver.SCHEME_FILE, handler);
            networkBuilder.addURLSchemeHandler(ContentResolver.SCHEME_ANDROID_RESOURCE, handler);

            NetworkLoader networkLoader = new NetworkLoader(networkBuilder);
            LoaderManager loaderManager = new LoaderManager(memoryCache, diskLoader, networkLoader);

            return new ImageLoaderImpl(loaderManager);
        }
    }

    /**
     * Handles logging for all {@link com.webimageloader.ImageLoader} instances
     *
     * @author Alexander Blom <alexanderblom.se>
     */
    public static class Logger {
        public static boolean DEBUG = false;
        public static boolean VERBOSE = false;

        /**
         * Log both debug and verbose messages
         */
        public static void logAll() {
            DEBUG = true;
            VERBOSE = true;
        }

        private Logger() {}
    }
}




Java Source Code List

com.webimageloader.ConnectionFactory.java
com.webimageloader.ConnectionHandler.java
com.webimageloader.Constants.java
com.webimageloader.ImageLoaderImpl.java
com.webimageloader.ImageLoader.java
com.webimageloader.Request.java
com.webimageloader.content.ContentURLConnection.java
com.webimageloader.content.ContentURLStreamHandler.java
com.webimageloader.ext.ImageHelper.java
com.webimageloader.ext.ImageLoaderApplication.java
com.webimageloader.loader.BackgroundLoader.java
com.webimageloader.loader.DiskLoader.java
com.webimageloader.loader.LoaderManager.java
com.webimageloader.loader.LoaderRequest.java
com.webimageloader.loader.LoaderWork.java
com.webimageloader.loader.Loader.java
com.webimageloader.loader.MemoryCache.java
com.webimageloader.loader.MemoryLoader.java
com.webimageloader.loader.Metadata.java
com.webimageloader.loader.NetworkLoader.java
com.webimageloader.loader.PendingRequests.java
com.webimageloader.loader.SimpleBackgroundLoader.java
com.webimageloader.loader.TransformingLoader.java
com.webimageloader.sample.AsyncLoader.java
com.webimageloader.sample.ExampleApplication.java
com.webimageloader.sample.FastImageView.java
com.webimageloader.sample.MainActivity.java
com.webimageloader.sample.numbers.NumberDetailsActivity.java
com.webimageloader.sample.numbers.NumbersActivity.java
com.webimageloader.sample.patterns.PatternDetailsActivity.java
com.webimageloader.sample.patterns.PatternsActivity.java
com.webimageloader.sample.patterns.PatternsListFragment.java
com.webimageloader.sample.progress.ProgressActivity.java
com.webimageloader.transformation.ScaleTransformation.java
com.webimageloader.transformation.SimpleTransformation.java
com.webimageloader.transformation.Transformation.java
com.webimageloader.util.AbstractImageLoader.java
com.webimageloader.util.Android.java
com.webimageloader.util.BitmapUtils.java
com.webimageloader.util.FlushedInputStream.java
com.webimageloader.util.Hasher.java
com.webimageloader.util.HeaderParser.java
com.webimageloader.util.IOUtil.java
com.webimageloader.util.InputSupplier.java
com.webimageloader.util.ListenerFuture.java
com.webimageloader.util.LruCache.java
com.webimageloader.util.PriorityThreadFactory.java
com.webimageloader.util.WaitFuture.java