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) { |
| 68 | Result result = Result.obtain(Result.CACHE_MISS, null, null); |
| 69 | |
| 70 | TreeMap<Point, Pair<Uri, Point>> sizeMap; |
| 71 | sizeMap = mSizeIndex.get(uri); |
| 72 | if (sizeMap == null || sizeMap.isEmpty()) { |
| 73 | // There is not any thumbnail for this uri. |
| 74 | return result; |
| 75 | } |
| 76 | |
| 77 | // Look for thumbnail of the same size. |
| 78 | Pair<Uri, Point> cacheKey = sizeMap.get(size); |
| 79 | if (cacheKey != null) { |
| 80 | Bitmap thumbnail = mCache.get(cacheKey); |
| 81 | if (thumbnail != null) { |
| 82 | result.mStatus = Result.CACHE_HIT_EXACT; |
| 83 | result.mThumbnail = thumbnail; |
| 84 | result.mSize = size; |
| 85 | return result; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Look for thumbnail of bigger sizes. |
| 90 | Point otherSize = sizeMap.higherKey(size); |
| 91 | if (otherSize != null) { |
| 92 | cacheKey = sizeMap.get(otherSize); |
| 93 | |
| 94 | if (cacheKey != null) { |
| 95 | Bitmap thumbnail = mCache.get(cacheKey); |
| 96 | if (thumbnail != null) { |
| 97 | result.mStatus = Result.CACHE_HIT_LARGER; |
| 98 | result.mThumbnail = thumbnail; |
| 99 | result.mSize = otherSize; |
| 100 | return result; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Look for thumbnail of smaller sizes. |
| 106 | otherSize = sizeMap.lowerKey(size); |
| 107 | if (otherSize != null) { |
| 108 | cacheKey = sizeMap.get(otherSize); |
| 109 | |
| 110 | if (cacheKey != null) { |
| 111 | Bitmap thumbnail = mCache.get(cacheKey); |
| 112 | if (thumbnail != null) { |
| 113 | result.mStatus = Result.CACHE_HIT_SMALLER; |
| 114 | result.mThumbnail = thumbnail; |
| 115 | result.mSize = otherSize; |
| 116 | return result; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Cache miss. |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | public void putThumbnail(Uri uri, Point size, Bitmap thumbnail) { |
| 126 | Pair<Uri, Point> cacheKey = Pair.create(uri, size); |
| 127 | |
| 128 | TreeMap<Point, Pair<Uri, Point>> sizeMap; |
| 129 | synchronized (mSizeIndex) { |
| 130 | sizeMap = mSizeIndex.get(uri); |
| 131 | if (sizeMap == null) { |
| 132 | sizeMap = new TreeMap<>(SIZE_COMPARATOR); |
| 133 | mSizeIndex.put(uri, sizeMap); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | mCache.put(cacheKey, thumbnail); |
| 138 | synchronized (sizeMap) { |
| 139 | sizeMap.put(size, cacheKey); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | public void onTrimMemory(int level) { |
| 144 | if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) { |
| 145 | synchronized (mSizeIndex) { |
| 146 | mSizeIndex.clear(); |
| 147 | } |
| 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 {} |
| 162 | |
| 163 | /** |
| 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; |
| 185 | |
| 186 | private @Nullable Bitmap mThumbnail; |
| 187 | |
| 188 | private @Nullable Point mSize; |
| 189 | |
| 190 | private static Result obtain(@Status int status, @Nullable Bitmap thumbnail, |
| 191 | @Nullable Point size) { |
| 192 | Result instance = sPool.acquire(); |
| 193 | instance = (instance != null ? instance : new Result()); |
| 194 | |
| 195 | instance.mStatus = status; |
| 196 | instance.mThumbnail = thumbnail; |
| 197 | instance.mSize = size; |
| 198 | |
| 199 | return instance; |
| 200 | } |
| 201 | |
| 202 | private Result() { |
| 203 | } |
| 204 | |
| 205 | public void recycle() { |
| 206 | mStatus = -1; |
| 207 | mThumbnail = null; |
| 208 | mSize = null; |
| 209 | |
| 210 | boolean released = sPool.release(this); |
| 211 | // This assert is used to guarantee we won't generate too many instances that can't be |
| 212 | // held in the pool, which indicates our pool size is too small. |
| 213 | // |
| 214 | // Right now one instance is enough because we expect all instances are only used in |
| 215 | // main thread. |
| 216 | assert (released); |
| 217 | } |
| 218 | |
| 219 | public @Status int getStatus() { |
| 220 | return mStatus; |
| 221 | } |
| 222 | |
| 223 | public @Nullable Bitmap getThumbnail() { |
| 224 | return mThumbnail; |
| 225 | } |
| 226 | |
| 227 | public @Nullable Point getSize() { |
| 228 | return mSize; |
| 229 | } |
| 230 | |
| 231 | public boolean isHit() { |
| 232 | return (mStatus != CACHE_MISS); |
| 233 | } |
| 234 | |
| 235 | public boolean isExactHit() { |
| 236 | return (mStatus == CACHE_HIT_EXACT); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | private static final class Cache extends LruCache<Pair<Uri, Point>, Bitmap> { |
| 241 | private Cache(int maxSizeBytes) { |
| 242 | super(maxSizeBytes); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | protected int sizeOf(Pair<Uri, Point> key, Bitmap value) { |
| 247 | return value.getByteCount(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private static final class SizeComparator implements Comparator<Point> { |
| 252 | @Override |
| 253 | public int compare(Point size0, Point size1) { |
| 254 | // Assume all sizes are roughly square, so we only compare them in one dimension. |
| 255 | return size0.x - size1.x; |
| 256 | } |
Jeff Sharkey | 8a8fb67 | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 257 | } |
| 258 | } |