Android Open Source - AndroidImageCache Image Cache






From Project

Back to project page AndroidImageCache.

License

The source code is released under:

Przemys?aw Jakubczyk Polidea Sp. z o.o. Copyright (c) 2012 All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the follo...

If you think the Android project AndroidImageCache 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

/**
 *//w ww  . j ava2  s . c  o m
 */
package pl.polidea.imagecache;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.text.TextUtils;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import pl.polidea.thridparty.DiskCache;
import pl.polidea.utils.StackPoolExecutor;
import pl.polidea.utils.Utils;

/**
 * @author Przemys?aw Jakubczyk <przemyslaw.jakubczyk@pl.polidea.pl>
 */
public class ImageCache {

    MemoryCache memCache;

    DiskCache diskCache;

    ExecutorService decodingBitmapsExecutor;

    ImageCache(final CacheConfig config) {
        // XXX: this is done in UI thread !
        checkAllValuesFilled(config);
        memCache = new MemoryCache(config.memoryCacheSize);
        diskCache = new DiskCache(config.diskCachePath, config.diskCacheSize, config.compressFormat,
                config.compressQuality);

        decodingBitmapsExecutor = new StackPoolExecutor(config.workersNumber);
    }

    private static void checkConfigNotNull(final CacheConfig config) {
        if (config == null) {
            throw new IllegalArgumentException("Config cannot be null");
        }
    }

    void checkAllValuesFilled(final CacheConfig config) {
        checkConfigNotNull(config);
        if (config.workersNumber == null || config.memoryCacheSize == null
                || config.diskCachePath == null || config.diskCacheSize == null
                || config.compressFormat == null || config.compressQuality == null) {
            throw new IllegalArgumentException("All config's fields have to be filled");
        }
    }

    /**
     * Ask ImageCache for given key. If memory cache doesn't have the key it
     * continue operation in other thread asking disk cache for the key. Passing
     * null listener throws IllegalArgumentException because passing the key
     * value is always done by the listener.
     */
    public void get(final String key, final OnCacheResultListener onCacheResultListener) {
        final String hashedKey = Utils.sha1(key);

        if (onCacheResultListener == null) {
            throw new IllegalArgumentException("onCacheResult cannot be null");
        }
        final Bitmap bitmap = memCache.get(hashedKey);
        if (bitmap == null) {
            decodingBitmapsExecutor.submit(buildTask(key, hashedKey, onCacheResultListener));
        } else {
            onCacheResultListener.onCacheHit(key, bitmap);
        }

    }

    /**
     * Removes bitmpa under key from memory and disc cache.
     */
    public boolean remove(final String key) {
        final String hashedKey = Utils.sha1(key);
        boolean removed = memCache.remove(hashedKey) != null;
        try {
            removed = diskCache.remove(hashedKey) || removed;
        } catch (final IOException e) {
            Utils.log("Removing bitmap error");
        }
        return removed;
    }

    /**
     * Puts bitmap to both memory and disc cache.
     */
    public void put(final String key, final Bitmap bitmap) {
        if (TextUtils.isEmpty(key) || bitmap == null || bitmap.isRecycled()) {
            throw new IllegalArgumentException("Key is empty either bitmap isn't valid");
        }
        final String hashedKey = Utils.sha1(key);
        memCache.put(hashedKey, bitmap);
        diskCache.put(hashedKey, bitmap);
    }

    public void clear() {
        memCache.evictAll();
        diskCache.clearCache();
    }

    public int getMemoryCacheSize() {
        return memCache.size();
    }

    public int getMemoryCacheMaxSize() {
        return memCache.maxSize();
    }

    public long getDiskCacheSize() {
        return diskCache.getSize();
    }

    public long getDiskCacheMaxSize() {
        return diskCache.getMaxSize();
    }

    public String getDiskCachePath() {
        return diskCache.getDirectory().getPath();
    }

    public CompressFormat getCompressFormat() {
        return diskCache.getCompressFormat();
    }

    public int getCompressQuality() {
        return diskCache.getCompressQuality();
    }

    CacheTask buildTask(String key, String hashedKey, OnCacheResultListener onCacheResultListener) {
        return new CacheTask(key, hashedKey, onCacheResultListener);
    }

    class CacheTask implements Runnable {

        String hashedKey;

        String key;

        OnCacheResultListener onCacheResultListener;

        public CacheTask(String key, String hashedKey, OnCacheResultListener onCacheResultListener) {
            this.key = key;
            this.hashedKey = hashedKey;
            this.onCacheResultListener = onCacheResultListener;
        }

        @Override
        public void run() {
            Bitmap bitmap = diskCache.getBitmap(hashedKey);
            if (bitmap == null || bitmap.isRecycled()) {
                onCacheResultListener.onCacheMiss(key);
            } else {
                memCache.put(hashedKey, bitmap);
                onCacheResultListener.onCacheHit(key, bitmap);
            }
        }
    }

}




Java Source Code List

pl.polidea.imagecache.BitmapLRUCache.java
pl.polidea.imagecache.CacheConfig.java
pl.polidea.imagecache.ImageCacheFactory.java
pl.polidea.imagecache.ImageCache.java
pl.polidea.imagecache.MemoryCache.java
pl.polidea.imagecache.OnCacheResultListener.java
pl.polidea.imagecache.StaticCachedImageCacheFactory.java
pl.polidea.thridparty.DiskCache.java
pl.polidea.thridparty.LinkedBlockingDeque.java
pl.polidea.thridparty.LruCache.java
pl.polidea.utils.Dimensions.java
pl.polidea.utils.StackBlockingDeque.java
pl.polidea.utils.StackPoolExecutor.java
pl.polidea.utils.TempFile.java
pl.polidea.utils.Utils.java
pl.polidea.webimagesampleapp.AndroidImageCacheExample.java
pl.polidea.webimageview.BitmapDecodeException.java
pl.polidea.webimageview.Bitmaps.java
pl.polidea.webimageview.DefaultBitmapProcessor.java
pl.polidea.webimageview.ImageViewUpdater.java
pl.polidea.webimageview.WebImageListener.java
pl.polidea.webimageview.WebImageView.java
pl.polidea.webimageview.net.StaticCachedWebClientFactory.java
pl.polidea.webimageview.net.WebCallback.java
pl.polidea.webimageview.net.WebClientFactory.java
pl.polidea.webimageview.net.WebClient.java
pl.polidea.webimageview.net.WebInterfaceImpl.java
pl.polidea.webimageview.net.WebInterface.java
pl.polidea.webimageview.processor.AbstractBitmapProcessorCreationChain.java
pl.polidea.webimageview.processor.BitmapProcessor.java
pl.polidea.webimageview.processor.BothWidthAndHeightFixed.java
pl.polidea.webimageview.processor.BothWidthAndHeightNotFixed.java
pl.polidea.webimageview.processor.OnlyHeightFixed.java
pl.polidea.webimageview.processor.OnlyWidthFixed.java
pl.polidea.webimageview.processor.ProcessorFactory.java
pl.polidea.webimageview.processor.Processor.java
pl.polidea.webimageview.processor.ProgramaticallyCreated.java
pl.polidea.webimageview.processor.Unknown.java