1 package com.bumptech.glide; 2 3 import android.content.Context; 4 import android.os.Build; 5 import com.android.volley.RequestQueue; 6 import com.bumptech.glide.load.engine.Engine; 7 import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; 8 import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPoolAdapter; 9 import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool; 10 import com.bumptech.glide.load.engine.cache.DiskCache; 11 import com.bumptech.glide.load.engine.cache.DiskCacheAdapter; 12 import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper; 13 import com.bumptech.glide.load.engine.cache.LruResourceCache; 14 import com.bumptech.glide.load.engine.cache.MemoryCache; 15 import com.bumptech.glide.load.engine.cache.MemorySizeCalculator; 16 import com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor; 17 import com.bumptech.glide.volley.RequestQueueWrapper; 18 19 import java.io.File; 20 import java.util.concurrent.ExecutorService; 21 22 public class GlideBuilder { 23 private RequestQueue requestQueue; 24 private Context context; 25 private Engine engine; 26 private BitmapPool bitmapPool; 27 private MemoryCache memoryCache; 28 private DiskCache diskCache; 29 private ExecutorService resizeService; 30 private ExecutorService diskCacheService; 31 32 public GlideBuilder(Context context) { 33 this.context = context.getApplicationContext(); 34 } 35 36 public GlideBuilder setRequestQueue(RequestQueue requestQueue) { 37 this.requestQueue = requestQueue; 38 return this; 39 } 40 41 public GlideBuilder setBitmapPool(BitmapPool bitmapPool) { 42 this.bitmapPool = bitmapPool; 43 return this; 44 } 45 46 public GlideBuilder setMemoryCache(MemoryCache memoryCache) { 47 this.memoryCache = memoryCache; 48 return this; 49 } 50 51 public GlideBuilder setDiskCache(DiskCache diskCache) { 52 this.diskCache = diskCache; 53 return this; 54 } 55 56 public GlideBuilder setResizeService(ExecutorService service) { 57 this.resizeService = service; 58 return this; 59 } 60 61 public GlideBuilder setDiskCacheService(ExecutorService service) { 62 this.diskCacheService = service; 63 return this; 64 } 65 66 GlideBuilder setEngine(Engine engine) { 67 this.engine = engine; 68 return this; 69 } 70 71 Glide createGlide() { 72 if (resizeService == null) { 73 final int cores = Math.max(1, Runtime.getRuntime().availableProcessors()); 74 resizeService = new FifoPriorityThreadPoolExecutor(cores); 75 } 76 if (diskCacheService == null) { 77 diskCacheService = new FifoPriorityThreadPoolExecutor(1); 78 } 79 80 if (requestQueue == null) { 81 requestQueue = RequestQueueWrapper.getRequestQueue(context); 82 } 83 84 MemorySizeCalculator calculator = new MemorySizeCalculator(context); 85 if (bitmapPool == null) { 86 if (Build.VERSION.SDK_INT >= 11) { 87 bitmapPool = new LruBitmapPool(calculator.getBitmapPoolSize()); 88 } else { 89 bitmapPool = new BitmapPoolAdapter(); 90 } 91 } 92 93 if (memoryCache == null) { 94 memoryCache = new LruResourceCache(calculator.getMemoryCacheSize()); 95 } 96 97 if (diskCache == null) { 98 File cacheDir = Glide.getPhotoCacheDir(context); 99 if (cacheDir != null) { 100 diskCache = DiskLruCacheWrapper.get(cacheDir, Glide.DEFAULT_DISK_CACHE_SIZE); 101 } 102 if (diskCache == null) { 103 diskCache = new DiskCacheAdapter(); 104 } 105 } 106 107 if (engine == null) { 108 engine = new Engine(memoryCache, diskCache, resizeService, diskCacheService); 109 } 110 111 return new Glide(engine, requestQueue, memoryCache, bitmapPool, context); 112 } 113 }