package com.susion.boring.utils;
import android.content.Context;
import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.common.internal.Supplier;
import com.facebook.common.util.ByteConstants;
import com.facebook.imagepipeline.cache.MemoryCacheParams;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
/**
* Creates ImagePipeline configuration for the sample app
*/
public class ImagePipelineConfigFactory {
private static final String IMAGE_PIPELINE_CACHE_DIR = "image_cache";
private static ImagePipelineConfig sImagePipelineConfig;
private static final int MAX_HEAP_SIZE = (int) Runtime.getRuntime().maxMemory();
public static final int MAX_DISK_CACHE_SIZE = 300 * ByteConstants.MB;
public static final int MAX_MEMORY_CACHE_SIZE = MAX_HEAP_SIZE / 3;
/**
* Creates config using android http stack as network backend.
*/
public static ImagePipelineConfig getImagePipelineConfig(Context context) {
if (sImagePipelineConfig == null) {
ImagePipelineConfig.Builder configBuilder = ImagePipelineConfig.newBuilder(context);
configureCaches(configBuilder, context);
sImagePipelineConfig = configBuilder.build();
}
return sImagePipelineConfig;
}
/**
* Configures disk and memory cache not to exceed common limits
*/
private static void configureCaches(ImagePipelineConfig.Builder configBuilder, Context context) {
final MemoryCacheParams bitmapCacheParams = new MemoryCacheParams(
MAX_MEMORY_CACHE_SIZE, // Max total size of elements in the cache
Integer.MAX_VALUE, // Max entries in the cache
MAX_MEMORY_CACHE_SIZE, // Max total size of elements in eviction queue
Integer.MAX_VALUE, // Max length of eviction queue
Integer.MAX_VALUE); // Max cache entry size
configBuilder
.setBitmapMemoryCacheParamsSupplier(
new Supplier<MemoryCacheParams>() {
public MemoryCacheParams get() {
return bitmapCacheParams;
}
})
.setMainDiskCacheConfig(DiskCacheConfig.newBuilder(context)
.setBaseDirectoryPath(FileUtils.getCacheDir())
.setBaseDirectoryName(IMAGE_PIPELINE_CACHE_DIR)
.setMaxCacheSize(MAX_DISK_CACHE_SIZE)
.build());
}
}