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