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