Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 18 | |
Winson Chung | d83f5f4 | 2012-02-13 14:27:42 -0800 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 20 | import android.content.ComponentName; |
Winson Chung | d83f5f4 | 2012-02-13 14:27:42 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 22 | import android.content.Intent; |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 23 | import android.content.pm.ActivityInfo; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 24 | import android.content.pm.ApplicationInfo; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageManager.NameNotFoundException; |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 27 | import android.content.res.Resources; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 28 | import android.graphics.Bitmap; |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 29 | import android.graphics.BitmapFactory; |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 30 | import android.graphics.Canvas; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 31 | import android.graphics.drawable.Drawable; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 32 | import android.text.TextUtils; |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 33 | import android.util.Log; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 34 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 35 | import com.android.launcher3.compat.LauncherActivityInfoCompat; |
| 36 | import com.android.launcher3.compat.LauncherAppsCompat; |
| 37 | import com.android.launcher3.compat.UserHandleCompat; |
| 38 | import com.android.launcher3.compat.UserManagerCompat; |
| 39 | |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 40 | import java.io.ByteArrayOutputStream; |
| 41 | import java.io.File; |
| 42 | import java.io.FileInputStream; |
| 43 | import java.io.FileNotFoundException; |
| 44 | import java.io.FileOutputStream; |
| 45 | import java.io.IOException; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 46 | import java.util.HashMap; |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 47 | import java.util.HashSet; |
Adam Cohen | b6d33df | 2013-10-15 10:18:02 -0700 | [diff] [blame] | 48 | import java.util.Iterator; |
| 49 | import java.util.Map.Entry; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Cache of application icons. Icons can be made from any thread. |
| 53 | */ |
| 54 | public class IconCache { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 55 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 56 | private static final String TAG = "Launcher.IconCache"; |
| 57 | |
| 58 | private static final int INITIAL_ICON_CACHE_CAPACITY = 50; |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 59 | private static final String RESOURCE_FILE_PREFIX = "icon_"; |
| 60 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 61 | // Empty class name is used for storing package default entry. |
| 62 | private static final String EMPTY_CLASS_NAME = "."; |
| 63 | |
Sunny Goyal | bbef77d | 2014-09-09 16:27:55 -0700 | [diff] [blame] | 64 | private static final boolean DEBUG = false; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 65 | |
| 66 | private static class CacheEntry { |
| 67 | public Bitmap icon; |
Kenny Guy | d6fe526 | 2014-07-21 17:11:41 +0100 | [diff] [blame] | 68 | public CharSequence title; |
| 69 | public CharSequence contentDescription; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 72 | private static class CacheKey { |
| 73 | public ComponentName componentName; |
| 74 | public UserHandleCompat user; |
| 75 | |
| 76 | CacheKey(ComponentName componentName, UserHandleCompat user) { |
| 77 | this.componentName = componentName; |
| 78 | this.user = user; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public int hashCode() { |
| 83 | return componentName.hashCode() + user.hashCode(); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public boolean equals(Object o) { |
| 88 | CacheKey other = (CacheKey) o; |
| 89 | return other.componentName.equals(componentName) && other.user.equals(user); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = |
| 94 | new HashMap<UserHandleCompat, Bitmap>(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 95 | private final Context mContext; |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 96 | private final PackageManager mPackageManager; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 97 | private final UserManagerCompat mUserManager; |
| 98 | private final LauncherAppsCompat mLauncherApps; |
| 99 | private final HashMap<CacheKey, CacheEntry> mCache = |
| 100 | new HashMap<CacheKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 101 | private int mIconDpi; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 102 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 103 | public IconCache(Context context) { |
Winson Chung | d83f5f4 | 2012-02-13 14:27:42 -0800 | [diff] [blame] | 104 | ActivityManager activityManager = |
| 105 | (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
| 106 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 107 | mContext = context; |
| 108 | mPackageManager = context.getPackageManager(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 109 | mUserManager = UserManagerCompat.getInstance(mContext); |
| 110 | mLauncherApps = LauncherAppsCompat.getInstance(mContext); |
Winson Chung | d83f5f4 | 2012-02-13 14:27:42 -0800 | [diff] [blame] | 111 | mIconDpi = activityManager.getLauncherLargeIconDensity(); |
| 112 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 113 | // need to set mIconDpi before getting default icon |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 114 | UserHandleCompat myUser = UserHandleCompat.myUserHandle(); |
| 115 | mDefaultIcons.put(myUser, makeDefaultIcon(myUser)); |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 118 | public Drawable getFullResDefaultActivityIcon() { |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 119 | return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 122 | private Drawable getFullResIcon(Resources resources, int iconId) { |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 123 | Drawable d; |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 124 | try { |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 125 | d = resources.getDrawableForDensity(iconId, mIconDpi); |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 126 | } catch (Resources.NotFoundException e) { |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 127 | d = null; |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 128 | } |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 129 | |
| 130 | return (d != null) ? d : getFullResDefaultActivityIcon(); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 133 | public Drawable getFullResIcon(String packageName, int iconId) { |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 134 | Resources resources; |
| 135 | try { |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 136 | resources = mPackageManager.getResourcesForApplication(packageName); |
| 137 | } catch (PackageManager.NameNotFoundException e) { |
| 138 | resources = null; |
| 139 | } |
| 140 | if (resources != null) { |
| 141 | if (iconId != 0) { |
| 142 | return getFullResIcon(resources, iconId); |
| 143 | } |
| 144 | } |
| 145 | return getFullResDefaultActivityIcon(); |
| 146 | } |
| 147 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 148 | public int getFullResIconDpi() { |
| 149 | return mIconDpi; |
| 150 | } |
| 151 | |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 152 | public Drawable getFullResIcon(ActivityInfo info) { |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 153 | Resources resources; |
| 154 | try { |
| 155 | resources = mPackageManager.getResourcesForApplication( |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 156 | info.applicationInfo); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 157 | } catch (PackageManager.NameNotFoundException e) { |
| 158 | resources = null; |
| 159 | } |
| 160 | if (resources != null) { |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 161 | int iconId = info.getIconResource(); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 162 | if (iconId != 0) { |
| 163 | return getFullResIcon(resources, iconId); |
| 164 | } |
| 165 | } |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 166 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 167 | return getFullResDefaultActivityIcon(); |
| 168 | } |
| 169 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 170 | private Bitmap makeDefaultIcon(UserHandleCompat user) { |
| 171 | Drawable unbadged = getFullResDefaultActivityIcon(); |
| 172 | Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user); |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 173 | Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1), |
| 174 | Math.max(d.getIntrinsicHeight(), 1), |
| 175 | Bitmap.Config.ARGB_8888); |
| 176 | Canvas c = new Canvas(b); |
| 177 | d.setBounds(0, 0, b.getWidth(), b.getHeight()); |
| 178 | d.draw(c); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 179 | c.setBitmap(null); |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 180 | return b; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Remove any records for the supplied ComponentName. |
| 185 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 186 | public synchronized void remove(ComponentName componentName, UserHandleCompat user) { |
| 187 | mCache.remove(new CacheKey(componentName, user)); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 191 | * Remove any records for the supplied package name. |
| 192 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 193 | public synchronized void remove(String packageName, UserHandleCompat user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 194 | HashSet<CacheKey> forDeletion = new HashSet<CacheKey>(); |
| 195 | for (CacheKey key: mCache.keySet()) { |
| 196 | if (key.componentName.getPackageName().equals(packageName) |
| 197 | && key.user.equals(user)) { |
| 198 | forDeletion.add(key); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 199 | } |
| 200 | } |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 201 | for (CacheKey condemned: forDeletion) { |
| 202 | mCache.remove(condemned); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 207 | * Empty out the cache. |
| 208 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 209 | public synchronized void flush() { |
| 210 | mCache.clear(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
Winson Chung | e5467dc | 2013-10-14 17:03:04 -0700 | [diff] [blame] | 214 | * Empty out the cache that aren't of the correct grid size |
| 215 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 216 | public synchronized void flushInvalidIcons(DeviceProfile grid) { |
| 217 | Iterator<Entry<CacheKey, CacheEntry>> it = mCache.entrySet().iterator(); |
| 218 | while (it.hasNext()) { |
| 219 | final CacheEntry e = it.next().getValue(); |
| 220 | if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx |
| 221 | || e.icon.getHeight() < grid.iconSizePx)) { |
| 222 | it.remove(); |
Winson Chung | e5467dc | 2013-10-14 17:03:04 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 228 | * Fill in "application" with the icon and label for "info." |
| 229 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 230 | public synchronized void getTitleAndIcon(AppInfo application, LauncherActivityInfoCompat info, |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 231 | HashMap<Object, CharSequence> labelCache) { |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 232 | CacheEntry entry = cacheLocked(application.componentName, info, labelCache, |
| 233 | info.getUser(), false); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 234 | |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 235 | application.title = entry.title; |
| 236 | application.iconBitmap = entry.icon; |
| 237 | application.contentDescription = entry.contentDescription; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 240 | public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) { |
| 241 | ComponentName component = intent.getComponent(); |
| 242 | // null info means not installed, but if we have a component from the intent then |
| 243 | // we should still look in the cache for restored app icons. |
| 244 | if (component == null) { |
| 245 | return getDefaultIcon(user); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 246 | } |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 247 | |
| 248 | LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user); |
| 249 | CacheEntry entry = cacheLocked(component, launcherActInfo, null, user, true); |
| 250 | return entry.icon; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Fill in "shortcutInfo" with the icon and label for "info." |
| 255 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 256 | public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent, |
| 257 | UserHandleCompat user, boolean usePkgIcon) { |
| 258 | ComponentName component = intent.getComponent(); |
| 259 | // null info means not installed, but if we have a component from the intent then |
| 260 | // we should still look in the cache for restored app icons. |
| 261 | if (component == null) { |
| 262 | shortcutInfo.setIcon(getDefaultIcon(user)); |
| 263 | shortcutInfo.title = ""; |
| 264 | shortcutInfo.usingFallbackIcon = true; |
| 265 | } else { |
| 266 | LauncherActivityInfoCompat launcherActInfo = |
| 267 | mLauncherApps.resolveActivity(intent, user); |
| 268 | CacheEntry entry = cacheLocked(component, launcherActInfo, null, user, usePkgIcon); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 269 | |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 270 | shortcutInfo.setIcon(entry.icon); |
| 271 | shortcutInfo.title = entry.title; |
| 272 | shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 277 | public synchronized Bitmap getDefaultIcon(UserHandleCompat user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 278 | if (!mDefaultIcons.containsKey(user)) { |
| 279 | mDefaultIcons.put(user, makeDefaultIcon(user)); |
| 280 | } |
| 281 | return mDefaultIcons.get(user); |
| 282 | } |
| 283 | |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 284 | public synchronized Bitmap getIcon(ComponentName component, LauncherActivityInfoCompat info, |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 285 | HashMap<Object, CharSequence> labelCache) { |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 286 | if (info == null || component == null) { |
| 287 | return null; |
Joe Onorato | fad1fb5 | 2010-05-04 12:12:41 -0700 | [diff] [blame] | 288 | } |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 289 | |
| 290 | CacheEntry entry = cacheLocked(component, info, labelCache, info.getUser(), false); |
| 291 | return entry.icon; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 294 | public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) { |
| 295 | return mDefaultIcons.get(user) == icon; |
Joe Onorato | ddc9c1f | 2010-08-30 18:30:15 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 298 | /** |
| 299 | * Retrieves the entry from the cache. If the entry is not present, it creates a new entry. |
| 300 | * This method is not thread safe, it must be called from a synchronized method. |
| 301 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 302 | private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info, |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 303 | HashMap<Object, CharSequence> labelCache, UserHandleCompat user, boolean usePackageIcon) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 304 | CacheKey cacheKey = new CacheKey(componentName, user); |
| 305 | CacheEntry entry = mCache.get(cacheKey); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 306 | if (entry == null) { |
| 307 | entry = new CacheEntry(); |
| 308 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 309 | mCache.put(cacheKey, entry); |
Joe Onorato | 84f6a8d | 2010-02-12 17:53:35 -0500 | [diff] [blame] | 310 | |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 311 | if (info != null) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 312 | ComponentName labelKey = info.getComponentName(); |
| 313 | if (labelCache != null && labelCache.containsKey(labelKey)) { |
| 314 | entry.title = labelCache.get(labelKey).toString(); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 315 | } else { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 316 | entry.title = info.getLabel().toString(); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 317 | if (labelCache != null) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 318 | labelCache.put(labelKey, entry.title); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 319 | } |
| 320 | } |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 321 | |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 322 | entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 323 | entry.icon = Utilities.createIconBitmap( |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 324 | info.getBadgedIcon(mIconDpi), mContext); |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 325 | } else { |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 326 | entry.title = ""; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 327 | Bitmap preloaded = getPreloadedIcon(componentName, user); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 328 | if (preloaded != null) { |
| 329 | if (DEBUG) Log.d(TAG, "using preloaded icon for " + |
| 330 | componentName.toShortString()); |
| 331 | entry.icon = preloaded; |
| 332 | } else { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 333 | if (usePackageIcon) { |
| 334 | CacheEntry packageEntry = getEntryForPackage( |
| 335 | componentName.getPackageName(), user); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 336 | if (packageEntry != null) { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 337 | if (DEBUG) Log.d(TAG, "using package default icon for " + |
| 338 | componentName.toShortString()); |
| 339 | entry.icon = packageEntry.icon; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 340 | entry.title = packageEntry.title; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | if (entry.icon == null) { |
| 344 | if (DEBUG) Log.d(TAG, "using default icon for " + |
| 345 | componentName.toShortString()); |
| 346 | entry.icon = getDefaultIcon(user); |
| 347 | } |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 350 | } |
| 351 | return entry; |
| 352 | } |
Daniel Sandler | 4e1cd23 | 2011-05-12 00:06:32 -0400 | [diff] [blame] | 353 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 354 | /** |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 355 | * Adds a default package entry in the cache. This entry is not persisted and will be removed |
| 356 | * when the cache is flushed. |
| 357 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 358 | public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user, |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 359 | Bitmap icon, CharSequence title) { |
Sunny Goyal | a22666f | 2014-09-18 13:25:15 -0700 | [diff] [blame] | 360 | remove(packageName, user); |
| 361 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 362 | CacheEntry entry = getEntryForPackage(packageName, user); |
| 363 | if (!TextUtils.isEmpty(title)) { |
| 364 | entry.title = title; |
| 365 | } |
| 366 | if (icon != null) { |
Sunny Goyal | 2fce90c | 2014-10-07 12:01:58 -0700 | [diff] [blame] | 367 | entry.icon = Utilities.createIconBitmap(icon, mContext); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 372 | * Gets an entry for the package, which can be used as a fallback entry for various components. |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 373 | * This method is not thread safe, it must be called from a synchronized method. |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 374 | */ |
| 375 | private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) { |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 376 | ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 377 | CacheKey cacheKey = new CacheKey(cn, user); |
| 378 | CacheEntry entry = mCache.get(cacheKey); |
| 379 | if (entry == null) { |
| 380 | entry = new CacheEntry(); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 381 | entry.title = ""; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 382 | mCache.put(cacheKey, entry); |
| 383 | |
| 384 | try { |
| 385 | ApplicationInfo info = mPackageManager.getApplicationInfo(packageName, 0); |
| 386 | entry.title = info.loadLabel(mPackageManager); |
| 387 | entry.icon = Utilities.createIconBitmap(info.loadIcon(mPackageManager), mContext); |
| 388 | } catch (NameNotFoundException e) { |
| 389 | if (DEBUG) Log.d(TAG, "Application not installed " + packageName); |
| 390 | } |
| 391 | |
| 392 | if (entry.icon == null) { |
| 393 | entry.icon = getPreloadedIcon(cn, user); |
| 394 | } |
| 395 | } |
| 396 | return entry; |
| 397 | } |
| 398 | |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 399 | public synchronized HashMap<ComponentName,Bitmap> getAllIcons() { |
| 400 | HashMap<ComponentName,Bitmap> set = new HashMap<ComponentName,Bitmap>(); |
| 401 | for (CacheKey ck : mCache.keySet()) { |
| 402 | final CacheEntry e = mCache.get(ck); |
| 403 | set.put(ck.componentName, e.icon); |
Daniel Sandler | 4e1cd23 | 2011-05-12 00:06:32 -0400 | [diff] [blame] | 404 | } |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 405 | return set; |
Daniel Sandler | 4e1cd23 | 2011-05-12 00:06:32 -0400 | [diff] [blame] | 406 | } |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 407 | |
| 408 | /** |
| 409 | * Pre-load an icon into the persistent cache. |
| 410 | * |
| 411 | * <P>Queries for a component that does not exist in the package manager |
| 412 | * will be answered by the persistent cache. |
| 413 | * |
| 414 | * @param context application context |
| 415 | * @param componentName the icon should be returned for this component |
| 416 | * @param icon the icon to be persisted |
| 417 | * @param dpi the native density of the icon |
| 418 | */ |
| 419 | public static void preloadIcon(Context context, ComponentName componentName, Bitmap icon, |
| 420 | int dpi) { |
| 421 | // TODO rescale to the correct native DPI |
| 422 | try { |
| 423 | PackageManager packageManager = context.getPackageManager(); |
| 424 | packageManager.getActivityIcon(componentName); |
| 425 | // component is present on the system already, do nothing |
| 426 | return; |
| 427 | } catch (PackageManager.NameNotFoundException e) { |
| 428 | // pass |
| 429 | } |
| 430 | |
| 431 | final String key = componentName.flattenToString(); |
| 432 | FileOutputStream resourceFile = null; |
| 433 | try { |
| 434 | resourceFile = context.openFileOutput(getResourceFilename(componentName), |
| 435 | Context.MODE_PRIVATE); |
| 436 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 437 | if (icon.compress(android.graphics.Bitmap.CompressFormat.PNG, 75, os)) { |
| 438 | byte[] buffer = os.toByteArray(); |
| 439 | resourceFile.write(buffer, 0, buffer.length); |
| 440 | } else { |
| 441 | Log.w(TAG, "failed to encode cache for " + key); |
| 442 | return; |
| 443 | } |
| 444 | } catch (FileNotFoundException e) { |
| 445 | Log.w(TAG, "failed to pre-load cache for " + key, e); |
| 446 | } catch (IOException e) { |
| 447 | Log.w(TAG, "failed to pre-load cache for " + key, e); |
| 448 | } finally { |
| 449 | if (resourceFile != null) { |
| 450 | try { |
| 451 | resourceFile.close(); |
| 452 | } catch (IOException e) { |
| 453 | Log.d(TAG, "failed to save restored icon for: " + key, e); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Read a pre-loaded icon from the persistent icon cache. |
| 461 | * |
| 462 | * @param componentName the component that should own the icon |
| 463 | * @returns a bitmap if one is cached, or null. |
| 464 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 465 | private Bitmap getPreloadedIcon(ComponentName componentName, UserHandleCompat user) { |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 466 | final String key = componentName.flattenToShortString(); |
| 467 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 468 | // We don't keep icons for other profiles in persistent cache. |
| 469 | if (!user.equals(UserHandleCompat.myUserHandle())) { |
| 470 | return null; |
| 471 | } |
| 472 | |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 473 | if (DEBUG) Log.v(TAG, "looking for pre-load icon for " + key); |
| 474 | Bitmap icon = null; |
| 475 | FileInputStream resourceFile = null; |
| 476 | try { |
| 477 | resourceFile = mContext.openFileInput(getResourceFilename(componentName)); |
| 478 | byte[] buffer = new byte[1024]; |
| 479 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 480 | int bytesRead = 0; |
| 481 | while(bytesRead >= 0) { |
| 482 | bytes.write(buffer, 0, bytesRead); |
| 483 | bytesRead = resourceFile.read(buffer, 0, buffer.length); |
| 484 | } |
| 485 | if (DEBUG) Log.d(TAG, "read " + bytes.size()); |
| 486 | icon = BitmapFactory.decodeByteArray(bytes.toByteArray(), 0, bytes.size()); |
| 487 | if (icon == null) { |
| 488 | Log.w(TAG, "failed to decode pre-load icon for " + key); |
| 489 | } |
| 490 | } catch (FileNotFoundException e) { |
Sunny Goyal | bbef77d | 2014-09-09 16:27:55 -0700 | [diff] [blame] | 491 | if (DEBUG) Log.d(TAG, "there is no restored icon for: " + key); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 492 | } catch (IOException e) { |
| 493 | Log.w(TAG, "failed to read pre-load icon for: " + key, e); |
| 494 | } finally { |
| 495 | if(resourceFile != null) { |
| 496 | try { |
| 497 | resourceFile.close(); |
| 498 | } catch (IOException e) { |
| 499 | Log.d(TAG, "failed to manage pre-load icon file: " + key, e); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 504 | return icon; |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Remove a pre-loaded icon from the persistent icon cache. |
| 509 | * |
| 510 | * @param componentName the component that should own the icon |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 511 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 512 | public void deletePreloadedIcon(ComponentName componentName, UserHandleCompat user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 513 | // We don't keep icons for other profiles in persistent cache. |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 514 | if (!user.equals(UserHandleCompat.myUserHandle()) || componentName == null) { |
| 515 | return; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 516 | } |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame^] | 517 | remove(componentName, user); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 518 | boolean success = mContext.deleteFile(getResourceFilename(componentName)); |
| 519 | if (DEBUG && success) Log.d(TAG, "removed pre-loaded icon from persistent cache"); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | private static String getResourceFilename(ComponentName component) { |
| 523 | String resourceName = component.flattenToShortString(); |
| 524 | String filename = resourceName.replace(File.separatorChar, '_'); |
| 525 | return RESOURCE_FILE_PREFIX + filename; |
| 526 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 527 | } |