Android Open Source - AndroidImageCache Cache Config






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 w  w . j av  a  2 s . c o  m*/
 */
package pl.polidea.imagecache;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap.CompressFormat;
import java.io.File;
import pl.polidea.utils.Utils;

/**
 * Image cache configuration.
 *
 * @author Wojciech Piwonski <wojciech.piwonski@pl.polidea.pl>
 */
public final class CacheConfig {

    public static final int DEFAULT_WORKERS_NUMBER = Runtime.getRuntime().availableProcessors();

    public static final CompressFormat DEFAULT_COMPRESS_FORMAT = CompressFormat.PNG;

    public static final int DEFAULT_COMPRESS_QUALITY = 100;

    /**
     * Workers number defines how many threads will process cache's tasks
     * simultaneously. Default value is one thread. Small values are
     * recommended, too big can cause OutOfMemory exception during processing
     * tasks, because of decoding many bitmaps at the same time.
     */
    Integer workersNumber;

    Integer memoryCacheSize;

    String diskCachePath;

    Long diskCacheSize;

    CompressFormat compressFormat;

    Integer compressQuality;

    private CacheConfig() {
    }

    public static CacheConfig buildDefault(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("Context cannot be null");
        }
        return buildDefault(context, null);
    }

    public static CacheConfig buildDefault(Context context, CacheConfig cacheConfig) {
        if (cacheConfig == null) {
            cacheConfig = new CacheConfig();
        }

        if (cacheConfig.workersNumber == null || cacheConfig.workersNumber < 1) {
            cacheConfig.workersNumber = DEFAULT_WORKERS_NUMBER;
        }
        if (cacheConfig.memoryCacheSize == null || cacheConfig.memoryCacheSize < 1) {
            cacheConfig.memoryCacheSize = getDefaultMemoryCacheSize(context);
        }
        if (cacheConfig.diskCachePath == null) {
            cacheConfig.diskCachePath = getDefaultDiskCachePath(context);
        }
        if (cacheConfig.diskCacheSize == null || cacheConfig.diskCacheSize < 1) {
            cacheConfig.diskCacheSize = getDefaultDiskCacheSize(context);
        }
        if (cacheConfig.compressFormat == null) {
            cacheConfig.compressFormat = DEFAULT_COMPRESS_FORMAT;
        }
        if (cacheConfig.compressQuality == null || cacheConfig.compressQuality < 1) {
            cacheConfig.compressQuality = DEFAULT_COMPRESS_QUALITY;
        }

        return cacheConfig;
    }

    private static String getDefaultDiskCachePath(final Context context) {
        return context.getCacheDir().getPath() + File.separator + "bitmaps";
    }

    private static int getDefaultMemoryCacheSize(final Context context) {
        final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        final int memClass = activityManager.getMemoryClass();
        final int size = 1024 * 1024 * memClass / 8;
        Utils.log("Device memory class: " + memClass + " LRUCache size: " + size / 1000 + " kB");
        return size;
    }

    private static long getDefaultDiskCacheSize(final Context context) {
        final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

        final int memClass = activityManager.getMemoryClass();
        final long size = 1024 * 1024 * memClass / 4;
        Utils.log("Device memory class: " + memClass + " DiskLruCache size: " + size / 1000 + " kB");
        return size;
    }
}




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