blob: ee2f168d580877f7008b3e98723175bd1f469236 [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
Garfield, Tanc8099c02016-05-02 12:01:30 -070019import android.annotation.IntDef;
20import android.annotation.Nullable;
21import android.content.ComponentCallbacks2;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070022import android.graphics.Bitmap;
Garfield, Tanc8099c02016-05-02 12:01:30 -070023import android.graphics.Point;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070024import android.net.Uri;
Garfield, Tanc2923782016-06-07 15:09:51 -070025import android.os.Looper;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070026import android.util.LruCache;
Garfield, Tanc8099c02016-05-02 12:01:30 -070027import android.util.Pair;
28import android.util.Pools;
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070029
Garfield, Tanc8099c02016-05-02 12:01:30 -070030import java.lang.annotation.Retention;
31import java.lang.annotation.RetentionPolicy;
32import java.util.Comparator;
33import java.util.HashMap;
34import java.util.TreeMap;
35
36/**
37 * An LRU cache that supports finding the thumbnail of the requested uri with a different size than
38 * the requested one.
39 */
40public class ThumbnailCache {
41
42 private static final SizeComparator SIZE_COMPARATOR = new SizeComparator();
43
44 /**
45 * A 2-dimensional index into {@link #mCache} entries. Pair<Uri, Point> is the key to
46 * {@link #mCache}. TreeMap is used to search the closest size to a given size and a given uri.
47 */
48 private final HashMap<Uri, TreeMap<Point, Pair<Uri, Point>>> mSizeIndex;
49 private final Cache mCache;
50
51 /**
52 * Creates a thumbnail LRU cache.
53 *
54 * @param maxCacheSizeInBytes the maximum size of thumbnails in bytes this cache can hold.
55 */
56 public ThumbnailCache(int maxCacheSizeInBytes) {
57 mSizeIndex = new HashMap<>();
58 mCache = new Cache(maxCacheSizeInBytes);
Jeff Sharkey8a8fb672013-05-07 12:41:33 -070059 }
60
Garfield, Tanc8099c02016-05-02 12:01:30 -070061 /**
62 * Obtains thumbnail given a uri and a size.
63 *
64 * @param uri the uri of the thumbnail in need
65 * @param size the desired size of the thumbnail
66 * @return the thumbnail result
67 */
68 public Result getThumbnail(Uri uri, Point size) {
Garfield, Tanc8099c02016-05-02 12:01:30 -070069 TreeMap<Point, Pair<Uri, Point>> sizeMap;
70 sizeMap = mSizeIndex.get(uri);
71 if (sizeMap == null || sizeMap.isEmpty()) {
72 // There is not any thumbnail for this uri.
Garfield, Tan16502a82016-05-19 15:36:36 -070073 return Result.obtainMiss();
Garfield, Tanc8099c02016-05-02 12:01:30 -070074 }
75
76 // Look for thumbnail of the same size.
77 Pair<Uri, Point> cacheKey = sizeMap.get(size);
78 if (cacheKey != null) {
Garfield, Tan16502a82016-05-19 15:36:36 -070079 Entry entry = mCache.get(cacheKey);
80 if (entry != null) {
81 return Result.obtain(Result.CACHE_HIT_EXACT, size, entry);
Garfield, Tanc8099c02016-05-02 12:01:30 -070082 }
83 }
84
85 // Look for thumbnail of bigger sizes.
86 Point otherSize = sizeMap.higherKey(size);
87 if (otherSize != null) {
88 cacheKey = sizeMap.get(otherSize);
89
90 if (cacheKey != null) {
Garfield, Tan16502a82016-05-19 15:36:36 -070091 Entry entry = mCache.get(cacheKey);
92 if (entry != null) {
93 return Result.obtain(Result.CACHE_HIT_LARGER, otherSize, entry);
Garfield, Tanc8099c02016-05-02 12:01:30 -070094 }
95 }
96 }
97
98 // Look for thumbnail of smaller sizes.
99 otherSize = sizeMap.lowerKey(size);
100 if (otherSize != null) {
101 cacheKey = sizeMap.get(otherSize);
102
103 if (cacheKey != null) {
Garfield, Tan16502a82016-05-19 15:36:36 -0700104 Entry entry = mCache.get(cacheKey);
105 if (entry != null) {
106 return Result.obtain(Result.CACHE_HIT_SMALLER, otherSize, entry);
Garfield, Tanc8099c02016-05-02 12:01:30 -0700107 }
108 }
109 }
110
111 // Cache miss.
Garfield, Tan16502a82016-05-19 15:36:36 -0700112 return Result.obtainMiss();
Garfield, Tanc8099c02016-05-02 12:01:30 -0700113 }
114
Garfield, Tan16502a82016-05-19 15:36:36 -0700115 public void putThumbnail(Uri uri, Point size, Bitmap thumbnail, long lastModified) {
Garfield, Tanc8099c02016-05-02 12:01:30 -0700116 Pair<Uri, Point> cacheKey = Pair.create(uri, size);
117
118 TreeMap<Point, Pair<Uri, Point>> sizeMap;
119 synchronized (mSizeIndex) {
120 sizeMap = mSizeIndex.get(uri);
121 if (sizeMap == null) {
122 sizeMap = new TreeMap<>(SIZE_COMPARATOR);
123 mSizeIndex.put(uri, sizeMap);
124 }
125 }
126
Garfield, Tan16502a82016-05-19 15:36:36 -0700127 Entry entry = new Entry(thumbnail, lastModified);
128 mCache.put(cacheKey, entry);
Garfield, Tanc8099c02016-05-02 12:01:30 -0700129 synchronized (sizeMap) {
130 sizeMap.put(size, cacheKey);
131 }
132 }
133
Garfield, Tan16502a82016-05-19 15:36:36 -0700134 private void removeKey(Uri uri, Point size) {
135 TreeMap<Point, Pair<Uri, Point>> sizeMap;
136 synchronized (mSizeIndex) {
137 sizeMap = mSizeIndex.get(uri);
138 }
139
140 // LruCache tells us to remove a key, which should exist, so sizeMap can't be null.
141 assert (sizeMap != null);
142 synchronized (sizeMap) {
143 sizeMap.remove(size);
144 }
145 }
146
Garfield, Tanc8099c02016-05-02 12:01:30 -0700147 public void onTrimMemory(int level) {
148 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Garfield, Tanc8099c02016-05-02 12:01:30 -0700149 mCache.evictAll();
150 } else if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) {
151 mCache.trimToSize(mCache.size() / 2);
152 }
153 }
154
155 /**
156 * A class that holds thumbnail and cache status.
157 */
158 public static final class Result {
159
160 @Retention(RetentionPolicy.SOURCE)
161 @IntDef({CACHE_MISS, CACHE_HIT_EXACT, CACHE_HIT_SMALLER, CACHE_HIT_LARGER})
162 @interface Status {}
Garfield, Tanc8099c02016-05-02 12:01:30 -0700163 /**
164 * Indicates there is no thumbnail for the requested uri. The thumbnail will be null.
165 */
166 public static final int CACHE_MISS = 0;
167 /**
168 * Indicates the thumbnail matches the requested size and requested uri.
169 */
170 public static final int CACHE_HIT_EXACT = 1;
171 /**
172 * Indicates the thumbnail is in a smaller size than the requested one from the requested
173 * uri.
174 */
175 public static final int CACHE_HIT_SMALLER = 2;
176 /**
177 * Indicates the thumbnail is in a larger size than the requested one from the requested
178 * uri.
179 */
180 public static final int CACHE_HIT_LARGER = 3;
181
182 private static final Pools.SimplePool<Result> sPool = new Pools.SimplePool<>(1);
183
184 private @Status int mStatus;
Garfield, Tanc8099c02016-05-02 12:01:30 -0700185 private @Nullable Bitmap mThumbnail;
Garfield, Tanc8099c02016-05-02 12:01:30 -0700186 private @Nullable Point mSize;
Garfield, Tan16502a82016-05-19 15:36:36 -0700187 private long mLastModified;
188
189 private static Result obtainMiss() {
190 return obtain(CACHE_MISS, null, null, 0);
191 }
192
193 private static Result obtain(@Status int status, Point size, Entry entry) {
194 return obtain(status, entry.mThumbnail, size, entry.mLastModified);
195 }
Garfield, Tanc8099c02016-05-02 12:01:30 -0700196
197 private static Result obtain(@Status int status, @Nullable Bitmap thumbnail,
Garfield, Tan16502a82016-05-19 15:36:36 -0700198 @Nullable Point size, long lastModified) {
Garfield, Tanc2923782016-06-07 15:09:51 -0700199 // Make sure this method is only called from main thread.
200 assert(Looper.myLooper() == Looper.getMainLooper());
201
Garfield, Tanc8099c02016-05-02 12:01:30 -0700202 Result instance = sPool.acquire();
203 instance = (instance != null ? instance : new Result());
204
205 instance.mStatus = status;
206 instance.mThumbnail = thumbnail;
207 instance.mSize = size;
Garfield, Tan16502a82016-05-19 15:36:36 -0700208 instance.mLastModified = lastModified;
Garfield, Tanc8099c02016-05-02 12:01:30 -0700209
210 return instance;
211 }
212
Garfield, Tan16502a82016-05-19 15:36:36 -0700213 private Result() {}
Garfield, Tanc8099c02016-05-02 12:01:30 -0700214
215 public void recycle() {
Garfield, Tanc2923782016-06-07 15:09:51 -0700216 // Make sure this method is only called from main thread.
217 assert(Looper.myLooper() == Looper.getMainLooper());
218
Garfield, Tanc8099c02016-05-02 12:01:30 -0700219 mStatus = -1;
220 mThumbnail = null;
221 mSize = null;
Garfield, Tan16502a82016-05-19 15:36:36 -0700222 mLastModified = -1;
Garfield, Tanc8099c02016-05-02 12:01:30 -0700223
224 boolean released = sPool.release(this);
225 // This assert is used to guarantee we won't generate too many instances that can't be
226 // held in the pool, which indicates our pool size is too small.
227 //
228 // Right now one instance is enough because we expect all instances are only used in
229 // main thread.
230 assert (released);
231 }
232
233 public @Status int getStatus() {
234 return mStatus;
235 }
236
237 public @Nullable Bitmap getThumbnail() {
238 return mThumbnail;
239 }
240
241 public @Nullable Point getSize() {
242 return mSize;
243 }
244
Garfield, Tan16502a82016-05-19 15:36:36 -0700245 public long getLastModified() {
246 return mLastModified;
247 }
248
Garfield, Tanc8099c02016-05-02 12:01:30 -0700249 public boolean isHit() {
250 return (mStatus != CACHE_MISS);
251 }
252
253 public boolean isExactHit() {
254 return (mStatus == CACHE_HIT_EXACT);
255 }
256 }
257
Garfield, Tan16502a82016-05-19 15:36:36 -0700258 private static final class Entry {
259 private final Bitmap mThumbnail;
260 private final long mLastModified;
261
262 private Entry(Bitmap thumbnail, long lastModified) {
263 mThumbnail = thumbnail;
264 mLastModified = lastModified;
265 }
266 }
267
268 private final class Cache extends LruCache<Pair<Uri, Point>, Entry> {
269
Garfield, Tanc8099c02016-05-02 12:01:30 -0700270 private Cache(int maxSizeBytes) {
271 super(maxSizeBytes);
272 }
273
274 @Override
Garfield, Tan16502a82016-05-19 15:36:36 -0700275 protected int sizeOf(Pair<Uri, Point> key, Entry value) {
276 return value.mThumbnail.getByteCount();
277 }
278
279 @Override
280 protected void entryRemoved(
281 boolean evicted, Pair<Uri, Point> key, Entry oldValue, Entry newValue) {
282 if (newValue == null) {
283 removeKey(key.first, key.second);
284 }
Garfield, Tanc8099c02016-05-02 12:01:30 -0700285 }
286 }
287
288 private static final class SizeComparator implements Comparator<Point> {
289 @Override
290 public int compare(Point size0, Point size1) {
291 // Assume all sizes are roughly square, so we only compare them in one dimension.
292 return size0.x - size1.x;
293 }
Jeff Sharkey8a8fb672013-05-07 12:41:33 -0700294 }
295}