blob: de05ff1acf40e0771b6f2a5b81ec30e625c5aa32 [file] [log] [blame]
Winson Chung04998342011-01-05 13:54:43 -08001/*
2 * Copyright (C) 2011 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
17package com.android.launcher2;
18
19import java.util.ArrayList;
20import java.util.HashMap;
21import java.util.HashSet;
22import java.util.List;
23
24import android.appwidget.AppWidgetProviderInfo;
25import android.content.ComponentName;
26import android.content.pm.ComponentInfo;
27import android.content.pm.ResolveInfo;
28import android.graphics.Bitmap;
29
30/**
31 * Simple cache mechanism for PagedView outlines.
32 */
33public class PagedViewIconCache {
34 public static class Key {
35 public enum Type {
36 ApplicationInfoKey,
37 AppWidgetProviderInfoKey,
38 ResolveInfoKey
39 }
40 private final ComponentName mComponentName;
41 private final Type mType;
42
43 public Key(ApplicationInfo info) {
44 mComponentName = info.componentName;
45 mType = Type.ApplicationInfoKey;
46 }
47 public Key(ResolveInfo info) {
48 final ComponentInfo ci = info.activityInfo != null ? info.activityInfo :
49 info.serviceInfo;
50 mComponentName = new ComponentName(ci.packageName, ci.name);
51 mType = Type.AppWidgetProviderInfoKey;
52 }
53 public Key(AppWidgetProviderInfo info) {
54 mComponentName = info.provider;
55 mType = Type.ResolveInfoKey;
56 }
57
58 private ComponentName getComponentName() {
59 return mComponentName;
60 }
61 public boolean isKeyType(Type t) {
62 return (mType == t);
63 }
64
65 @Override
66 public boolean equals(Object o) {
67 if (o instanceof Key) {
68 Key k = (Key) o;
69 return mComponentName.equals(k.mComponentName);
70 }
71 return super.equals(o);
72 }
73 @Override
74 public int hashCode() {
75 return getComponentName().hashCode();
76 }
77 }
78
79 private final HashMap<Key, Bitmap> mIconOutlineCache = new HashMap<Key, Bitmap>();
80
81 public void clear() {
82 for (Key key : mIconOutlineCache.keySet()) {
83 mIconOutlineCache.get(key).recycle();
84 }
85 mIconOutlineCache.clear();
86 }
87 private void retainAll(HashSet<Key> keysToKeep, Key.Type t) {
88 HashSet<Key> keysToRemove = new HashSet<Key>(mIconOutlineCache.keySet());
89 keysToRemove.removeAll(keysToKeep);
90 for (Key key : keysToRemove) {
91 if (key.isKeyType(t)) {
92 mIconOutlineCache.get(key).recycle();
93 mIconOutlineCache.remove(key);
94 }
95 }
96 }
97 /** Removes all the keys to applications that aren't in the passed in collection */
98 public void retainAllApps(ArrayList<ApplicationInfo> keys) {
99 HashSet<Key> keysSet = new HashSet<Key>();
100 for (ApplicationInfo info : keys) {
101 keysSet.add(new Key(info));
102 }
103 retainAll(keysSet, Key.Type.ApplicationInfoKey);
104 }
105 /** Removes all the keys to shortcuts that aren't in the passed in collection */
106 public void retainAllShortcuts(List<ResolveInfo> keys) {
107 HashSet<Key> keysSet = new HashSet<Key>();
108 for (ResolveInfo info : keys) {
109 keysSet.add(new Key(info));
110 }
111 retainAll(keysSet, Key.Type.ResolveInfoKey);
112 }
113 /** Removes all the keys to widgets that aren't in the passed in collection */
114 public void retainAllAppWidgets(List<AppWidgetProviderInfo> keys) {
115 HashSet<Key> keysSet = new HashSet<Key>();
116 for (AppWidgetProviderInfo info : keys) {
117 keysSet.add(new Key(info));
118 }
119 retainAll(keysSet, Key.Type.AppWidgetProviderInfoKey);
120 }
121 public void addOutline(Key key, Bitmap b) {
122 mIconOutlineCache.put(key, b);
123 }
124 public void removeOutline(Key key) {
125 if (mIconOutlineCache.containsKey(key)) {
126 mIconOutlineCache.get(key).recycle();
127 mIconOutlineCache.remove(key);
128 }
129 }
130 public Bitmap getOutline(Key key) {
131 return mIconOutlineCache.get(key);
132 }
133}