Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 1 | /* |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.content.ComponentCallbacks2; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 23 | import android.graphics.Point; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 24 | import android.net.Uri; |
| 25 | import android.util.LruCache; |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 26 | import android.util.Pair; |
| 27 | import android.util.Pools; |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 28 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
| 31 | import java.util.Comparator; |
| 32 | import java.util.HashMap; |
| 33 | import java.util.TreeMap; |
| 34 | |
| 35 | /** |
| 36 | * An LRU cache that supports finding the thumbnail of the requested uri with a different size than |
| 37 | * the requested one. |
| 38 | */ |
| 39 | public class ThumbnailCache { |
| 40 | |
| 41 | private static final SizeComparator SIZE_COMPARATOR = new SizeComparator(); |
| 42 | |
| 43 | /** |
| 44 | * A 2-dimensional index into {@link #mCache} entries. Pair<Uri, Point> is the key to |
| 45 | * {@link #mCache}. TreeMap is used to search the closest size to a given size and a given uri. |
| 46 | */ |
| 47 | private final HashMap<Uri, TreeMap<Point, Pair<Uri, Point>>> mSizeIndex; |
| 48 | private final Cache mCache; |
| 49 | |
| 50 | /** |
| 51 | * Creates a thumbnail LRU cache. |
| 52 | * |
| 53 | * @param maxCacheSizeInBytes the maximum size of thumbnails in bytes this cache can hold. |
| 54 | */ |
| 55 | public ThumbnailCache(int maxCacheSizeInBytes) { |
| 56 | mSizeIndex = new HashMap<>(); |
| 57 | mCache = new Cache(maxCacheSizeInBytes); |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Obtains thumbnail given a uri and a size. |
| 62 | * |
| 63 | * @param uri the uri of the thumbnail in need |
| 64 | * @param size the desired size of the thumbnail |
| 65 | * @return the thumbnail result |
| 66 | */ |
| 67 | public Result getThumbnail(Uri uri, Point size) { |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 68 | TreeMap<Point, Pair<Uri, Point>> sizeMap; |
| 69 | sizeMap = mSizeIndex.get(uri); |
| 70 | if (sizeMap == null || sizeMap.isEmpty()) { |
| 71 | // There is not any thumbnail for this uri. |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 72 | return Result.obtainMiss(); |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Look for thumbnail of the same size. |
| 76 | Pair<Uri, Point> cacheKey = sizeMap.get(size); |
| 77 | if (cacheKey != null) { |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 78 | Entry entry = mCache.get(cacheKey); |
| 79 | if (entry != null) { |
| 80 | return Result.obtain(Result.CACHE_HIT_EXACT, size, entry); |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | // Look for thumbnail of bigger sizes. |
| 85 | Point otherSize = sizeMap.higherKey(size); |
| 86 | if (otherSize != null) { |
| 87 | cacheKey = sizeMap.get(otherSize); |
| 88 | |
| 89 | if (cacheKey != null) { |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 90 | Entry entry = mCache.get(cacheKey); |
| 91 | if (entry != null) { |
| 92 | return Result.obtain(Result.CACHE_HIT_LARGER, otherSize, entry); |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Look for thumbnail of smaller sizes. |
| 98 | otherSize = sizeMap.lowerKey(size); |
| 99 | if (otherSize != null) { |
| 100 | cacheKey = sizeMap.get(otherSize); |
| 101 | |
| 102 | if (cacheKey != null) { |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 103 | Entry entry = mCache.get(cacheKey); |
| 104 | if (entry != null) { |
| 105 | return Result.obtain(Result.CACHE_HIT_SMALLER, otherSize, entry); |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Cache miss. |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 111 | return Result.obtainMiss(); |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 114 | public void putThumbnail(Uri uri, Point size, Bitmap thumbnail, long lastModified) { |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 115 | Pair<Uri, Point> cacheKey = Pair.create(uri, size); |
| 116 | |
| 117 | TreeMap<Point, Pair<Uri, Point>> sizeMap; |
| 118 | synchronized (mSizeIndex) { |
| 119 | sizeMap = mSizeIndex.get(uri); |
| 120 | if (sizeMap == null) { |
| 121 | sizeMap = new TreeMap<>(SIZE_COMPARATOR); |
| 122 | mSizeIndex.put(uri, sizeMap); |
| 123 | } |
| 124 | } |
| 125 | |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 126 | Entry entry = new Entry(thumbnail, lastModified); |
| 127 | mCache.put(cacheKey, entry); |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 128 | synchronized (sizeMap) { |
| 129 | sizeMap.put(size, cacheKey); |
| 130 | } |
| 131 | } |
| 132 | |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 133 | private void removeKey(Uri uri, Point size) { |
| 134 | TreeMap<Point, Pair<Uri, Point>> sizeMap; |
| 135 | synchronized (mSizeIndex) { |
| 136 | sizeMap = mSizeIndex.get(uri); |
| 137 | } |
| 138 | |
| 139 | // LruCache tells us to remove a key, which should exist, so sizeMap can't be null. |
| 140 | assert (sizeMap != null); |
| 141 | synchronized (sizeMap) { |
| 142 | sizeMap.remove(size); |
| 143 | } |
| 144 | } |
| 145 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 146 | public void onTrimMemory(int level) { |
| 147 | if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) { |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 148 | mCache.evictAll(); |
| 149 | } else if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) { |
| 150 | mCache.trimToSize(mCache.size() / 2); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * A class that holds thumbnail and cache status. |
| 156 | */ |
| 157 | public static final class Result { |
| 158 | |
| 159 | @Retention(RetentionPolicy.SOURCE) |
| 160 | @IntDef({CACHE_MISS, CACHE_HIT_EXACT, CACHE_HIT_SMALLER, CACHE_HIT_LARGER}) |
| 161 | @interface Status {} |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 162 | /** |
| 163 | * Indicates there is no thumbnail for the requested uri. The thumbnail will be null. |
| 164 | */ |
| 165 | public static final int CACHE_MISS = 0; |
| 166 | /** |
| 167 | * Indicates the thumbnail matches the requested size and requested uri. |
| 168 | */ |
| 169 | public static final int CACHE_HIT_EXACT = 1; |
| 170 | /** |
| 171 | * Indicates the thumbnail is in a smaller size than the requested one from the requested |
| 172 | * uri. |
| 173 | */ |
| 174 | public static final int CACHE_HIT_SMALLER = 2; |
| 175 | /** |
| 176 | * Indicates the thumbnail is in a larger size than the requested one from the requested |
| 177 | * uri. |
| 178 | */ |
| 179 | public static final int CACHE_HIT_LARGER = 3; |
| 180 | |
| 181 | private static final Pools.SimplePool<Result> sPool = new Pools.SimplePool<>(1); |
| 182 | |
| 183 | private @Status int mStatus; |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 184 | private @Nullable Bitmap mThumbnail; |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 185 | private @Nullable Point mSize; |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 186 | private long mLastModified; |
| 187 | |
| 188 | private static Result obtainMiss() { |
| 189 | return obtain(CACHE_MISS, null, null, 0); |
| 190 | } |
| 191 | |
| 192 | private static Result obtain(@Status int status, Point size, Entry entry) { |
| 193 | return obtain(status, entry.mThumbnail, size, entry.mLastModified); |
| 194 | } |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 195 | |
| 196 | private static Result obtain(@Status int status, @Nullable Bitmap thumbnail, |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 197 | @Nullable Point size, long lastModified) { |
Garfield, Tan | 2056205 | 2016-06-10 11:23:40 -0700 | [diff] [blame^] | 198 | Shared.checkMainLoop(); |
Garfield, Tan | c292378 | 2016-06-07 15:09:51 -0700 | [diff] [blame] | 199 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 200 | Result instance = sPool.acquire(); |
| 201 | instance = (instance != null ? instance : new Result()); |
| 202 | |
| 203 | instance.mStatus = status; |
| 204 | instance.mThumbnail = thumbnail; |
| 205 | instance.mSize = size; |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 206 | instance.mLastModified = lastModified; |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 207 | |
| 208 | return instance; |
| 209 | } |
| 210 | |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 211 | private Result() {} |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 212 | |
| 213 | public void recycle() { |
Garfield, Tan | 2056205 | 2016-06-10 11:23:40 -0700 | [diff] [blame^] | 214 | Shared.checkMainLoop(); |
Garfield, Tan | c292378 | 2016-06-07 15:09:51 -0700 | [diff] [blame] | 215 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 216 | mStatus = -1; |
| 217 | mThumbnail = null; |
| 218 | mSize = null; |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 219 | mLastModified = -1; |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 220 | |
| 221 | boolean released = sPool.release(this); |
| 222 | // This assert is used to guarantee we won't generate too many instances that can't be |
| 223 | // held in the pool, which indicates our pool size is too small. |
| 224 | // |
| 225 | // Right now one instance is enough because we expect all instances are only used in |
| 226 | // main thread. |
| 227 | assert (released); |
| 228 | } |
| 229 | |
| 230 | public @Status int getStatus() { |
| 231 | return mStatus; |
| 232 | } |
| 233 | |
| 234 | public @Nullable Bitmap getThumbnail() { |
| 235 | return mThumbnail; |
| 236 | } |
| 237 | |
| 238 | public @Nullable Point getSize() { |
| 239 | return mSize; |
| 240 | } |
| 241 | |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 242 | public long getLastModified() { |
| 243 | return mLastModified; |
| 244 | } |
| 245 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 246 | public boolean isHit() { |
| 247 | return (mStatus != CACHE_MISS); |
| 248 | } |
| 249 | |
| 250 | public boolean isExactHit() { |
| 251 | return (mStatus == CACHE_HIT_EXACT); |
| 252 | } |
| 253 | } |
| 254 | |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 255 | private static final class Entry { |
| 256 | private final Bitmap mThumbnail; |
| 257 | private final long mLastModified; |
| 258 | |
| 259 | private Entry(Bitmap thumbnail, long lastModified) { |
| 260 | mThumbnail = thumbnail; |
| 261 | mLastModified = lastModified; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | private final class Cache extends LruCache<Pair<Uri, Point>, Entry> { |
| 266 | |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 267 | private Cache(int maxSizeBytes) { |
| 268 | super(maxSizeBytes); |
| 269 | } |
| 270 | |
| 271 | @Override |
Garfield, Tan | 16502a8 | 2016-05-19 15:36:36 -0700 | [diff] [blame] | 272 | protected int sizeOf(Pair<Uri, Point> key, Entry value) { |
| 273 | return value.mThumbnail.getByteCount(); |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | protected void entryRemoved( |
| 278 | boolean evicted, Pair<Uri, Point> key, Entry oldValue, Entry newValue) { |
| 279 | if (newValue == null) { |
| 280 | removeKey(key.first, key.second); |
| 281 | } |
Garfield, Tan | c8099c0 | 2016-05-02 12:01:30 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | private static final class SizeComparator implements Comparator<Point> { |
| 286 | @Override |
| 287 | public int compare(Point size0, Point size1) { |
| 288 | // Assume all sizes are roughly square, so we only compare them in one dimension. |
| 289 | return size0.x - size1.x; |
| 290 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 291 | } |
| 292 | } |