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