blob: b98ee20ca1acda0493d4b9fe54af7364dd8666a2 [file] [log] [blame]
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07001/*
Garfield, Tanc8099c02016-05-02 12:01:30 -07002 * Copyright (C) 2016 The Android Open Source Project
Jeff Sharkey8a8fb672013-05-07 12:41:33 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Jeff Sharkeya4ff00f2018-07-09 14:57:51 -060019import androidx.annotation.IntDef;
20import androidx.annotation.Nullable;
Leon Liaoefcd5112018-11-02 10:57:56 +080021import androidx.core.util.Pools;
22
Garfield, Tanc8099c02016-05-02 12:01:30 -070023import android.content.ComponentCallbacks2;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070024import android.graphics.Bitmap;
Garfield, Tanc8099c02016-05-02 12:01:30 -070025import android.graphics.Point;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070026import android.net.Uri;
27import android.util.LruCache;
Garfield, Tanc8099c02016-05-02 12:01:30 -070028import android.util.Pair;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070029
Steve McKayd9caa6a2016-09-15 16:36:45 -070030import com.android.documentsui.base.Shared;
31
Garfield, Tanc8099c02016-05-02 12:01:30 -070032import java.lang.annotation.Retention;
33import java.lang.annotation.RetentionPolicy;
34import java.util.Comparator;
35import java.util.HashMap;
36import java.util.TreeMap;
37
38/**
39 * An LRU cache that supports finding the thumbnail of the requested uri with a different size than
40 * the requested one.
41 */
42public class ThumbnailCache {
43
44 private static final SizeComparator SIZE_COMPARATOR = new SizeComparator();
45
46 /**
47 * A 2-dimensional index into {@link #mCache} entries. Pair<Uri, Point> is the key to
48 * {@link #mCache}. TreeMap is used to search the closest size to a given size and a given uri.
49 */
50 private final HashMap<Uri, TreeMap<Point, Pair<Uri, Point>>> mSizeIndex;
51 private final Cache mCache;
52
53 /**
54 * Creates a thumbnail LRU cache.
55 *
56 * @param maxCacheSizeInBytes the maximum size of thumbnails in bytes this cache can hold.
57 */
58 public ThumbnailCache(int maxCacheSizeInBytes) {
59 mSizeIndex = new HashMap<>();
60 mCache = new Cache(maxCacheSizeInBytes);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070061 }
62
Garfield, Tanc8099c02016-05-02 12:01:30 -070063 /**
64 * Obtains thumbnail given a uri and a size.
65 *
66 * @param uri the uri of the thumbnail in need
67 * @param size the desired size of the thumbnail
68 * @return the thumbnail result
69 */
70 public Result getThumbnail(Uri uri, Point size) {
Garfield, Tanc8099c02016-05-02 12:01:30 -070071 TreeMap<Point, Pair<Uri, Point>> sizeMap;
72 sizeMap = mSizeIndex.get(uri);
73 if (sizeMap == null || sizeMap.isEmpty()) {
74 // There is not any thumbnail for this uri.
Garfield, Tan16502a82016-05-19 15:36:36 -070075 return Result.obtainMiss();
Garfield, Tanc8099c02016-05-02 12:01:30 -070076 }
77
78 // Look for thumbnail of the same size.
79 Pair<Uri, Point> cacheKey = sizeMap.get(size);
80 if (cacheKey != null) {
Garfield, Tan16502a82016-05-19 15:36:36 -070081 Entry entry = mCache.get(cacheKey);
82 if (entry != null) {
83 return Result.obtain(Result.CACHE_HIT_EXACT, size, entry);
Garfield, Tanc8099c02016-05-02 12:01:30 -070084 }
85 }
86
87 // Look for thumbnail of bigger sizes.
88 Point otherSize = sizeMap.higherKey(size);
89 if (otherSize != null) {
90 cacheKey = sizeMap.get(otherSize);
91
92 if (cacheKey != null) {
Garfield, Tan16502a82016-05-19 15:36:36 -070093 Entry entry = mCache.get(cacheKey);
94 if (entry != null) {
95 return Result.obtain(Result.CACHE_HIT_LARGER, otherSize, entry);
Garfield, Tanc8099c02016-05-02 12:01:30 -070096 }
97 }
98 }
99
100 // Look for thumbnail of smaller sizes.
101 otherSize = sizeMap.lowerKey(size);
102 if (otherSize != null) {
103 cacheKey = sizeMap.get(otherSize);
104
105 if (cacheKey != null) {
Garfield, Tan16502a82016-05-19 15:36:36 -0700106 Entry entry = mCache.get(cacheKey);
107 if (entry != null) {
108 return Result.obtain(Result.CACHE_HIT_SMALLER, otherSize, entry);
Garfield, Tanc8099c02016-05-02 12:01:30 -0700109 }
110 }
111 }
112
113 // Cache miss.
Garfield, Tan16502a82016-05-19 15:36:36 -0700114 return Result.obtainMiss();
Garfield, Tanc8099c02016-05-02 12:01:30 -0700115 }
116
Garfield, Taneba5bb92016-07-22 10:30:49 -0700117 /**
118 * Puts a thumbnail for the given uri and size in to the cache.
119 * @param uri the uri of the thumbnail
120 * @param size the size of the thumbnail
121 * @param thumbnail the thumbnail to put in cache
122 * @param lastModified last modified value of the thumbnail to track its validity
123 */
Garfield, Tan16502a82016-05-19 15:36:36 -0700124 public void putThumbnail(Uri uri, Point size, Bitmap thumbnail, long lastModified) {
Garfield, Tanc8099c02016-05-02 12:01:30 -0700125 Pair<Uri, Point> cacheKey = Pair.create(uri, size);
126
127 TreeMap<Point, Pair<Uri, Point>> sizeMap;
128 synchronized (mSizeIndex) {
129 sizeMap = mSizeIndex.get(uri);
130 if (sizeMap == null) {
131 sizeMap = new TreeMap<>(SIZE_COMPARATOR);
132 mSizeIndex.put(uri, sizeMap);
133 }
134 }
135
Garfield, Tan16502a82016-05-19 15:36:36 -0700136 Entry entry = new Entry(thumbnail, lastModified);
137 mCache.put(cacheKey, entry);
Garfield, Tanc8099c02016-05-02 12:01:30 -0700138 synchronized (sizeMap) {
139 sizeMap.put(size, cacheKey);
140 }
141 }
142
Garfield, Taneba5bb92016-07-22 10:30:49 -0700143 /**
144 * Removes all thumbnail cache associated to the given uri.
145 * @param uri the uri which thumbnail cache to remove
146 */
147 public void removeUri(Uri uri) {
148 TreeMap<Point, Pair<Uri, Point>> sizeMap;
149 synchronized (mSizeIndex) {
150 sizeMap = mSizeIndex.get(uri);
151 }
152
153 if (sizeMap != null) {
154 // Create an array to hold all values to avoid ConcurrentModificationException because
155 // removeKey() will be called by LruCache but we can't modify the map while we're
156 // iterating over the collection of values.
157 for (Pair<Uri, Point> index : sizeMap.values().toArray(new Pair[0])) {
158 mCache.remove(index);
159 }
160 }
161 }
162
Garfield, Tan16502a82016-05-19 15:36:36 -0700163 private void removeKey(Uri uri, Point size) {
164 TreeMap<Point, Pair<Uri, Point>> sizeMap;
165 synchronized (mSizeIndex) {
166 sizeMap = mSizeIndex.get(uri);
167 }
168
Garfield, Taneba5bb92016-07-22 10:30:49 -0700169 assert(sizeMap != null);
Garfield, Tan16502a82016-05-19 15:36:36 -0700170 synchronized (sizeMap) {
171 sizeMap.remove(size);
172 }
173 }
174
Garfield, Tanc8099c02016-05-02 12:01:30 -0700175 public void onTrimMemory(int level) {
176 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Garfield, Tanc8099c02016-05-02 12:01:30 -0700177 mCache.evictAll();
178 } else if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) {
179 mCache.trimToSize(mCache.size() / 2);
180 }
181 }
182
183 /**
184 * A class that holds thumbnail and cache status.
185 */
186 public static final class Result {
187
188 @Retention(RetentionPolicy.SOURCE)
189 @IntDef({CACHE_MISS, CACHE_HIT_EXACT, CACHE_HIT_SMALLER, CACHE_HIT_LARGER})
190 @interface Status {}
Garfield, Tanc8099c02016-05-02 12:01:30 -0700191 /**
192 * Indicates there is no thumbnail for the requested uri. The thumbnail will be null.
193 */
194 public static final int CACHE_MISS = 0;
195 /**
196 * Indicates the thumbnail matches the requested size and requested uri.
197 */
198 public static final int CACHE_HIT_EXACT = 1;
199 /**
200 * Indicates the thumbnail is in a smaller size than the requested one from the requested
201 * uri.
202 */
203 public static final int CACHE_HIT_SMALLER = 2;
204 /**
205 * Indicates the thumbnail is in a larger size than the requested one from the requested
206 * uri.
207 */
208 public static final int CACHE_HIT_LARGER = 3;
209
210 private static final Pools.SimplePool<Result> sPool = new Pools.SimplePool<>(1);
211
212 private @Status int mStatus;
Garfield, Tanc8099c02016-05-02 12:01:30 -0700213 private @Nullable Bitmap mThumbnail;
Garfield, Tanc8099c02016-05-02 12:01:30 -0700214 private @Nullable Point mSize;
Garfield, Tan16502a82016-05-19 15:36:36 -0700215 private long mLastModified;
216
217 private static Result obtainMiss() {
218 return obtain(CACHE_MISS, null, null, 0);
219 }
220
221 private static Result obtain(@Status int status, Point size, Entry entry) {
222 return obtain(status, entry.mThumbnail, size, entry.mLastModified);
223 }
Garfield, Tanc8099c02016-05-02 12:01:30 -0700224
225 private static Result obtain(@Status int status, @Nullable Bitmap thumbnail,
Garfield, Tan16502a82016-05-19 15:36:36 -0700226 @Nullable Point size, long lastModified) {
Garfield, Tan20562052016-06-10 11:23:40 -0700227 Shared.checkMainLoop();
Garfield, Tanc2923782016-06-07 15:09:51 -0700228
Garfield, Tanc8099c02016-05-02 12:01:30 -0700229 Result instance = sPool.acquire();
230 instance = (instance != null ? instance : new Result());
231
232 instance.mStatus = status;
233 instance.mThumbnail = thumbnail;
234 instance.mSize = size;
Garfield, Tan16502a82016-05-19 15:36:36 -0700235 instance.mLastModified = lastModified;
Garfield, Tanc8099c02016-05-02 12:01:30 -0700236
237 return instance;
238 }
239
Garfield, Tan16502a82016-05-19 15:36:36 -0700240 private Result() {}
Garfield, Tanc8099c02016-05-02 12:01:30 -0700241
242 public void recycle() {
Garfield, Tan20562052016-06-10 11:23:40 -0700243 Shared.checkMainLoop();
Garfield, Tanc2923782016-06-07 15:09:51 -0700244
Garfield, Tanc8099c02016-05-02 12:01:30 -0700245 mStatus = -1;
246 mThumbnail = null;
247 mSize = null;
Garfield, Tan16502a82016-05-19 15:36:36 -0700248 mLastModified = -1;
Garfield, Tanc8099c02016-05-02 12:01:30 -0700249
250 boolean released = sPool.release(this);
251 // This assert is used to guarantee we won't generate too many instances that can't be
252 // held in the pool, which indicates our pool size is too small.
253 //
254 // Right now one instance is enough because we expect all instances are only used in
255 // main thread.
256 assert (released);
257 }
258
259 public @Status int getStatus() {
260 return mStatus;
261 }
262
263 public @Nullable Bitmap getThumbnail() {
264 return mThumbnail;
265 }
266
267 public @Nullable Point getSize() {
268 return mSize;
269 }
270
Garfield, Tan16502a82016-05-19 15:36:36 -0700271 public long getLastModified() {
272 return mLastModified;
273 }
274
Garfield, Tanc8099c02016-05-02 12:01:30 -0700275 public boolean isHit() {
276 return (mStatus != CACHE_MISS);
277 }
278
279 public boolean isExactHit() {
280 return (mStatus == CACHE_HIT_EXACT);
281 }
282 }
283
Garfield, Tan16502a82016-05-19 15:36:36 -0700284 private static final class Entry {
285 private final Bitmap mThumbnail;
286 private final long mLastModified;
287
288 private Entry(Bitmap thumbnail, long lastModified) {
289 mThumbnail = thumbnail;
290 mLastModified = lastModified;
291 }
292 }
293
294 private final class Cache extends LruCache<Pair<Uri, Point>, Entry> {
295
Garfield, Tanc8099c02016-05-02 12:01:30 -0700296 private Cache(int maxSizeBytes) {
297 super(maxSizeBytes);
298 }
299
300 @Override
Garfield, Tan16502a82016-05-19 15:36:36 -0700301 protected int sizeOf(Pair<Uri, Point> key, Entry value) {
302 return value.mThumbnail.getByteCount();
303 }
304
305 @Override
306 protected void entryRemoved(
307 boolean evicted, Pair<Uri, Point> key, Entry oldValue, Entry newValue) {
308 if (newValue == null) {
309 removeKey(key.first, key.second);
310 }
Garfield, Tanc8099c02016-05-02 12:01:30 -0700311 }
312 }
313
314 private static final class SizeComparator implements Comparator<Point> {
315 @Override
316 public int compare(Point size0, Point size1) {
317 // Assume all sizes are roughly square, so we only compare them in one dimension.
318 return size0.x - size1.x;
319 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700320 }
321}