• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.bumptech.glide;
2 
3 import android.content.Context;
4 import android.os.Build;
5 
6 import com.bumptech.glide.load.DecodeFormat;
7 import com.bumptech.glide.load.engine.Engine;
8 import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
9 import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPoolAdapter;
10 import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
11 import com.bumptech.glide.load.engine.cache.DiskCache;
12 import com.bumptech.glide.load.engine.cache.DiskCacheAdapter;
13 import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper;
14 import com.bumptech.glide.load.engine.cache.LruResourceCache;
15 import com.bumptech.glide.load.engine.cache.MemoryCache;
16 import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
17 import com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor;
18 
19 import java.io.File;
20 import java.util.concurrent.ExecutorService;
21 
22 /**
23  * A builder class for setting default structural classes for Glide to use.
24  */
25 public class GlideBuilder {
26     private final Context context;
27 
28     private Engine engine;
29     private BitmapPool bitmapPool;
30     private MemoryCache memoryCache;
31     private DiskCache diskCache;
32     private ExecutorService sourceService;
33     private ExecutorService diskCacheService;
34     private DecodeFormat decodeFormat;
35 
GlideBuilder(Context context)36     public GlideBuilder(Context context) {
37         this.context = context.getApplicationContext();
38     }
39 
40     /**
41      * Sets the {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool} implementation to use to store and
42      * retrieve reused {@link android.graphics.Bitmap}s.
43      *
44      * @param bitmapPool The pool to use.
45      * @return This builder.
46      */
setBitmapPool(BitmapPool bitmapPool)47     public GlideBuilder setBitmapPool(BitmapPool bitmapPool) {
48         this.bitmapPool = bitmapPool;
49         return this;
50     }
51 
52     /**
53      * Sets the {@link com.bumptech.glide.load.engine.cache.MemoryCache} implementation to store
54      * {@link com.bumptech.glide.load.engine.Resource}s that are not currently in use.
55      *
56      * @param memoryCache  The cache to use.
57      * @return This builder.
58      */
setMemoryCache(MemoryCache memoryCache)59     public GlideBuilder setMemoryCache(MemoryCache memoryCache) {
60         this.memoryCache = memoryCache;
61         return this;
62     }
63 
64     /**
65      * Sets the {@link com.bumptech.glide.load.engine.cache.DiskCache} implementation to use to store
66      * {@link com.bumptech.glide.load.engine.Resource} data and thumbnails.
67      *
68      * @param diskCache The disk cache to use.
69      * @return This builder.
70      */
setDiskCache(DiskCache diskCache)71     public GlideBuilder setDiskCache(DiskCache diskCache) {
72         this.diskCache = diskCache;
73         return this;
74     }
75 
76     /**
77      * Sets the {@link java.util.concurrent.ExecutorService} implementation to use when retrieving
78      * {@link com.bumptech.glide.load.engine.Resource}s that are not already in the cache.
79      *
80      * <p>
81      *     Any implementation must order requests based on their {@link com.bumptech.glide.Priority} for thumbnail
82      *     requests to work properly.
83      * </p>
84      *
85      * @see #setDiskCacheService(java.util.concurrent.ExecutorService)
86      * @see com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor
87      *
88      * @param service The ExecutorService to use.
89      * @return This builder.
90      */
setResizeService(ExecutorService service)91     public GlideBuilder setResizeService(ExecutorService service) {
92         this.sourceService = service;
93         return this;
94     }
95 
96     /**
97      * Sets the {@link java.util.concurrent.ExecutorService} implementation to use when retrieving
98      * {@link com.bumptech.glide.load.engine.Resource}s that are currently in cache.
99      *
100      * <p>
101      *     Any implementation must order requests based on their {@link com.bumptech.glide.Priority} for thumbnail
102      *     requests to work properly.
103      * </p>
104      *
105      * @see #setResizeService(java.util.concurrent.ExecutorService)
106      * @see com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor
107      *
108      * @param service The ExecutorService to use.
109      * @return This builder.
110      */
setDiskCacheService(ExecutorService service)111     public GlideBuilder setDiskCacheService(ExecutorService service) {
112         this.diskCacheService = service;
113         return this;
114     }
115 
116     /**
117      * Sets the {@link com.bumptech.glide.load.DecodeFormat} that will be the default format for all the default
118      * decoders that can change the {@link android.graphics.Bitmap.Config} of the {@link android.graphics.Bitmap}s they
119      * decode.
120      *
121      * <p>
122      *     Decode format is always a suggestion, not a requirement. See {@link com.bumptech.glide.load.DecodeFormat} for
123      *     more details.
124      * </p>
125      *
126      * <p>
127      *     If you instantiate and use a custom decoder, it will use
128      *     {@link com.bumptech.glide.load.DecodeFormat#DEFAULT} as its default.
129      * </p>
130      *
131      * @param decodeFormat The format to use.
132      * @return This builder.
133      */
setDecodeFormat(DecodeFormat decodeFormat)134     public GlideBuilder setDecodeFormat(DecodeFormat decodeFormat) {
135         this.decodeFormat = decodeFormat;
136         return this;
137     }
138 
139     // For testing.
setEngine(Engine engine)140     GlideBuilder setEngine(Engine engine) {
141         this.engine = engine;
142         return this;
143     }
144 
createGlide()145     Glide createGlide() {
146         if (sourceService == null) {
147             final int cores = Math.max(1, Runtime.getRuntime().availableProcessors());
148             sourceService = new FifoPriorityThreadPoolExecutor(cores);
149         }
150         if (diskCacheService == null) {
151             diskCacheService = new FifoPriorityThreadPoolExecutor(1);
152         }
153 
154         MemorySizeCalculator calculator = new MemorySizeCalculator(context);
155         if (bitmapPool == null) {
156             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
157                 bitmapPool = new LruBitmapPool(calculator.getBitmapPoolSize());
158             } else {
159                 bitmapPool = new BitmapPoolAdapter();
160             }
161         }
162 
163         if (memoryCache == null) {
164             memoryCache = new LruResourceCache(calculator.getMemoryCacheSize());
165         }
166 
167         if (diskCache == null) {
168             File cacheDir = Glide.getPhotoCacheDir(context);
169             if (cacheDir != null) {
170                 diskCache = DiskLruCacheWrapper.get(cacheDir, Glide.DEFAULT_DISK_CACHE_SIZE);
171             }
172             if (diskCache == null) {
173                 diskCache = new DiskCacheAdapter();
174             }
175         }
176 
177         if (engine == null) {
178             engine = new Engine(memoryCache, diskCache, diskCacheService, sourceService);
179         }
180 
181         if (decodeFormat == null) {
182             decodeFormat = DecodeFormat.DEFAULT;
183         }
184 
185         return new Glide(engine, memoryCache, bitmapPool, context, decodeFormat);
186     }
187 }