blob: 855d9143134206c8cc456fb81ed122a19d9f3441 [file] [log] [blame]
Joe Onorato0589f0f2010-02-08 13:44:00 -08001/*
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 IconCache {
46 private static final String TAG = "Launcher.IconCache";
47
48 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
49
50 private static class CacheEntry {
51 public Bitmap icon;
52 public String title;
53 public Bitmap titleBitmap;
54 }
55
56 private LauncherApplication mContext;
57 private PackageManager mPackageManager;
58 private Utilities.BubbleText mBubble;
59 private final HashMap<ComponentName, CacheEntry> mCache =
60 new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
61
62 public IconCache(LauncherApplication context) {
63 mContext = context;
64 mPackageManager = context.getPackageManager();
65 mBubble = new Utilities.BubbleText(context);
66 }
67
68 /**
69 * Remove any records for the supplied ComponentName.
70 */
71 public void remove(ComponentName componentName) {
72 synchronized (mCache) {
73 mCache.remove(componentName);
74 }
75 }
76
77 /**
78 * Empty out the cache.
79 */
80 public void flush() {
81 synchronized (mCache) {
82 mCache.clear();
83 }
84 }
85
86 /**
87 * Fill in "application" with the icon and label for "info."
88 */
89 public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info) {
90 synchronized (mCache) {
91 CacheEntry entry = cacheLocked(application.componentName, info);
Joe Onorato84f6a8d2010-02-12 17:53:35 -050092 if (entry.titleBitmap == null) {
93 entry.titleBitmap = mBubble.createTextBitmap(entry.title.toString());
94 }
Joe Onorato0589f0f2010-02-08 13:44:00 -080095
96 application.title = entry.title;
97 application.titleBitmap = entry.titleBitmap;
98 application.iconBitmap = entry.icon;
99 }
100 }
101
102 public Bitmap getIcon(Intent intent) {
103 final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0);
104 ComponentName component = intent.getComponent();
105
106 if (resolveInfo == null || component == null) {
107 return null;
108 }
109
110 CacheEntry entry = cacheLocked(component, resolveInfo);
111 return entry.icon;
112 }
113
114 public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) {
115 if (resolveInfo == null || component == null) {
116 return null;
117 }
118
119 CacheEntry entry = cacheLocked(component, resolveInfo);
120 return entry.icon;
121 }
122
123 private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) {
124 CacheEntry entry = mCache.get(componentName);
125 if (entry == null) {
126 entry = new CacheEntry();
127
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500128 mCache.put(componentName, entry);
129
Joe Onorato0589f0f2010-02-08 13:44:00 -0800130 entry.title = info.loadLabel(mPackageManager).toString();
131 if (entry.title == null) {
132 entry.title = info.activityInfo.name;
133 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800134 entry.icon = Utilities.createIconBitmap(
135 info.activityInfo.loadIcon(mPackageManager), mContext);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800136 }
137 return entry;
138 }
139}
140