blob: 65ae570222edb34c3d4fae76f0671fdaa32890f2 [file] [log] [blame]
Joe Onorato9c1289c2009-08-17 11:03:03 -04001/*
2 * Copyright (C) 2008 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 android.content.ComponentName;
20import android.content.ContentResolver;
21import android.content.ContentValues;
22import android.content.Intent;
23import android.content.Context;
24import android.content.pm.ActivityInfo;
25import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
27import android.content.res.Resources;
28import android.database.Cursor;
29import android.graphics.Bitmap;
30import android.graphics.BitmapFactory;
31import android.graphics.drawable.Drawable;
32import android.graphics.drawable.BitmapDrawable;
33import android.net.Uri;
34import android.util.Log;
35import android.os.Process;
36
37import java.lang.ref.WeakReference;
38import java.util.ArrayList;
39import java.util.HashMap;
40import java.util.List;
41
42/**
43 * Cache of application icons. Icons can be made from any thread.
44 */
45public class AppInfoCache {
46 private static final String TAG = "Launcher.AppInfoCache";
47
48 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
49
50 private static final HashMap<ComponentName, ApplicationInfo> sCache =
51 new HashMap<ComponentName, ApplicationInfo>(INITIAL_ICON_CACHE_CAPACITY);
52
53 /**
54 * no public constructor.
55 */
56 private AppInfoCache() {
57 }
58
59 /**
60 * For the given ResolveInfo, return an ApplicationInfo and cache the result for later.
61 */
62 public static ApplicationInfo cache(ResolveInfo info, Context context,
63 Utilities.BubbleText bubble) {
64 synchronized (sCache) {
65 ComponentName componentName = new ComponentName(
66 info.activityInfo.applicationInfo.packageName,
67 info.activityInfo.name);
68 ApplicationInfo application = sCache.get(componentName);
69
70 if (application == null) {
71 application = new ApplicationInfo();
72 application.container = ItemInfo.NO_ID;
73
74 updateTitleAndIcon(info, application, context, bubble);
75
76 application.setActivity(componentName,
77 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
78
79 sCache.put(componentName, application);
80 }
81
82 return application;
83 }
84 }
85
86 /**
87 * Update the entry in the in the cache with its new metadata.
88 */
89 public static void update(ResolveInfo info, ApplicationInfo applicationInfo, Context context) {
90 synchronized (sCache) {
91 updateTitleAndIcon(info, applicationInfo, context, new Utilities.BubbleText(context));
92
93 ComponentName componentName = new ComponentName(
94 info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
95 sCache.put(componentName, applicationInfo);
96 }
97 }
98
99 /**
100 * Remove any records for the supplied ComponentName.
101 */
102 public static void remove(ComponentName componentName) {
103 synchronized (sCache) {
104 sCache.remove(componentName);
105 }
106 }
107
108 /**
109 * Empty out the cache.
110 */
111 public static void flush() {
112 synchronized (sCache) {
113 sCache.clear();
114 }
115 }
116
117 /**
118 * Get the icon for the supplied ApplicationInfo. If that activity already
119 * exists in the cache, use that.
120 */
121 public static Drawable getIconDrawable(PackageManager packageManager, ApplicationInfo info) {
122 final ResolveInfo resolveInfo = packageManager.resolveActivity(info.intent, 0);
123 if (resolveInfo == null) {
124 return null;
125 }
126
127 ComponentName componentName = new ComponentName(
128 resolveInfo.activityInfo.applicationInfo.packageName,
129 resolveInfo.activityInfo.name);
130 ApplicationInfo cached;
131 synchronized (sCache) {
132 cached = sCache.get(componentName);
Joe Onorato5162ea92009-09-03 09:39:42 -0700133 if (cached != null) {
134 if (cached.icon == null) {
135 cached.icon = resolveInfo.activityInfo.loadIcon(packageManager);
136 }
137 return cached.icon;
138 } else {
139 return resolveInfo.activityInfo.loadIcon(packageManager);
140 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400141 }
142 }
143
144 /**
145 * Go through the cache and disconnect any of the callbacks in the drawables or we
146 * leak the previous Home screen on orientation change.
147 */
148 public static void unbindDrawables() {
149 synchronized (sCache) {
150 for (ApplicationInfo appInfo: sCache.values()) {
151 appInfo.icon.setCallback(null);
152 }
153 }
154 }
155
156 /**
157 * Update the title and icon. Don't keep a reference to the context!
158 */
159 private static void updateTitleAndIcon(ResolveInfo info, ApplicationInfo application,
160 Context context, Utilities.BubbleText bubble) {
161 final PackageManager packageManager = context.getPackageManager();
162
163 application.title = info.loadLabel(packageManager);
164 if (application.title == null) {
165 application.title = info.activityInfo.name;
166 }
167
Joe Onorato6665c0f2009-09-02 15:27:24 -0700168 application.iconBitmap = Utilities.createAllAppsBitmap(
169 info.activityInfo.loadIcon(packageManager), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400170 application.filtered = true;
171
172 application.titleBitmap = bubble.createTextBitmap(application.title.toString());
173 }
174}
175