Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.ContentValues; |
| 22 | import android.content.Intent; |
| 23 | import android.content.Context; |
| 24 | import android.content.pm.ActivityInfo; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.content.pm.ResolveInfo; |
| 27 | import android.content.res.Resources; |
| 28 | import android.database.Cursor; |
| 29 | import android.graphics.Bitmap; |
| 30 | import android.graphics.BitmapFactory; |
| 31 | import android.graphics.drawable.Drawable; |
| 32 | import android.graphics.drawable.BitmapDrawable; |
| 33 | import android.net.Uri; |
| 34 | import android.util.Log; |
| 35 | import android.os.Process; |
| 36 | |
| 37 | import java.lang.ref.WeakReference; |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.HashMap; |
| 40 | import java.util.List; |
| 41 | |
| 42 | /** |
| 43 | * Cache of application icons. Icons can be made from any thread. |
| 44 | */ |
| 45 | public 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); |
| 133 | } |
| 134 | |
| 135 | if (cached != null) { |
| 136 | return cached.icon; |
| 137 | } else { |
| 138 | return resolveInfo.activityInfo.loadIcon(packageManager); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Go through the cache and disconnect any of the callbacks in the drawables or we |
| 144 | * leak the previous Home screen on orientation change. |
| 145 | */ |
| 146 | public static void unbindDrawables() { |
| 147 | synchronized (sCache) { |
| 148 | for (ApplicationInfo appInfo: sCache.values()) { |
| 149 | appInfo.icon.setCallback(null); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Update the title and icon. Don't keep a reference to the context! |
| 156 | */ |
| 157 | private static void updateTitleAndIcon(ResolveInfo info, ApplicationInfo application, |
| 158 | Context context, Utilities.BubbleText bubble) { |
| 159 | final PackageManager packageManager = context.getPackageManager(); |
| 160 | |
| 161 | application.title = info.loadLabel(packageManager); |
| 162 | if (application.title == null) { |
| 163 | application.title = info.activityInfo.name; |
| 164 | } |
| 165 | |
| 166 | Drawable icon = Utilities.createIconThumbnail(info.activityInfo.loadIcon(packageManager), |
| 167 | context, true); |
| 168 | application.iconBitmap = ((FastBitmapDrawable)icon).getBitmap(); |
| 169 | application.filtered = true; |
| 170 | |
| 171 | application.titleBitmap = bubble.createTextBitmap(application.title.toString()); |
| 172 | } |
| 173 | } |
| 174 | |