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