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