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.android.systemui.shared.recents.model;
18 
19 import android.util.LruCache;
20 
21 import com.android.systemui.shared.recents.model.Task.TaskKey;
22 
23 import java.io.PrintWriter;
24 
25 /**
26  * A mapping of {@link TaskKey} to value, with additional LRU functionality where the least
27  * recently referenced key/values will be evicted as more values than the given cache size are
28  * inserted.
29  *
30  * In addition, this also allows the caller to invalidate cached values for keys that have since
31  * changed.
32  */
33 public class TaskKeyLruCache<V> extends TaskKeyCache<V> {
34 
35     public interface EvictionCallback {
onEntryEvicted(TaskKey key)36         void onEntryEvicted(TaskKey key);
37     }
38 
39     private final LruCache<Integer, V> mCache;
40     private final EvictionCallback mEvictionCallback;
41 
TaskKeyLruCache(int cacheSize)42     public TaskKeyLruCache(int cacheSize) {
43         this(cacheSize, null);
44     }
45 
TaskKeyLruCache(int cacheSize, EvictionCallback evictionCallback)46     public TaskKeyLruCache(int cacheSize, EvictionCallback evictionCallback) {
47         mEvictionCallback = evictionCallback;
48         mCache = new LruCache<Integer, V>(cacheSize) {
49 
50             @Override
51             protected void entryRemoved(boolean evicted, Integer taskId, V oldV, V newV) {
52                 if (mEvictionCallback != null) {
53                     mEvictionCallback.onEntryEvicted(mKeys.get(taskId));
54                 }
55                 mKeys.remove(taskId);
56             }
57         };
58     }
59 
60     /** Trims the cache to a specific size */
trimToSize(int cacheSize)61     final void trimToSize(int cacheSize) {
62         mCache.trimToSize(cacheSize);
63     }
64 
dump(String prefix, PrintWriter writer)65     public void dump(String prefix, PrintWriter writer) {
66         String innerPrefix = prefix + "  ";
67 
68         writer.print(prefix); writer.print(TAG);
69         writer.print(" numEntries="); writer.print(mKeys.size());
70         writer.println();
71         int keyCount = mKeys.size();
72         for (int i = 0; i < keyCount; i++) {
73             writer.print(innerPrefix); writer.println(mKeys.get(mKeys.keyAt(i)));
74         }
75     }
76 
77     @Override
getCacheEntry(int id)78     protected V getCacheEntry(int id) {
79         return mCache.get(id);
80     }
81 
82     @Override
putCacheEntry(int id, V value)83     protected void putCacheEntry(int id, V value) {
84         mCache.put(id, value);
85     }
86 
87     @Override
removeCacheEntry(int id)88     protected void removeCacheEntry(int id) {
89         mCache.remove(id);
90     }
91 
92     @Override
evictAllCache()93     protected void evictAllCache() {
94         mCache.evictAll();
95     }
96 }
97