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