• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
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.example.android.activityscenetransitionbasic;
18 
19 import com.android.volley.toolbox.ImageLoader;
20 
21 import android.graphics.Bitmap;
22 import android.util.LruCache;
23 
24 import java.util.Map;
25 
26 /**
27  * An image memory cache implementation for Volley which allows the retrieval of entires via a URL.
28  * Volley internally inserts items with a key which is a combination of a the size of the image,
29  * and the url.
30  *
31  * This class provide the method {@link #getBitmapFromUrl(String)} which allows the retrieval of
32  * a bitmap solely on the URL.
33  */
34 public class ImageMemoryCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {
35 
36     /**
37      * Singleton instance which has it's maximum size set to be 1/8th of the allowed memory size.
38      */
39     public static final ImageMemoryCache INSTANCE = new ImageMemoryCache(
40             (int) (Runtime.getRuntime().maxMemory() / 8));
41 
42     // Cache the last created snapshot
43     private Map<String, Bitmap> mLastSnapshot;
44 
ImageMemoryCache(int maxSize)45     private ImageMemoryCache(int maxSize) {
46         super(maxSize);
47     }
48 
getBitmapFromUrl(String url)49     public Bitmap getBitmapFromUrl(String url) {
50         // If we do not have a snapshot to use, generate one
51         if (mLastSnapshot == null) {
52             mLastSnapshot = snapshot();
53         }
54 
55         // Iterate through the snapshot to find any entries which match our url
56         for (Map.Entry<String, Bitmap> entry : mLastSnapshot.entrySet()) {
57             if (url.equals(extractUrl(entry.getKey()))) {
58                 // We've found an entry with the same url, return the bitmap
59                 return entry.getValue();
60             }
61         }
62 
63         // We didn't find an entry, so return null
64         return null;
65     }
66 
67     @Override
getBitmap(String key)68     public Bitmap getBitmap(String key) {
69         return get(key);
70     }
71 
72     @Override
putBitmap(String key, Bitmap bitmap)73     public void putBitmap(String key, Bitmap bitmap) {
74         put(key, bitmap);
75 
76         // An entry has been added, so invalidate the snapshot
77         mLastSnapshot = null;
78     }
79 
80     @Override
entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue)81     protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
82         super.entryRemoved(evicted, key, oldValue, newValue);
83 
84         // An entry has been removed, so invalidate the snapshot
85         mLastSnapshot = null;
86     }
87 
extractUrl(String key)88     private static String extractUrl(String key) {
89         return key.substring(key.indexOf("http"));
90     }
91 
92     @Override
sizeOf(String key, Bitmap value)93     protected int sizeOf(String key, Bitmap value) {
94         return value.getAllocationByteCount();
95     }
96 }
97