blob: 91d4aaf218714993d20ec5e97be16c54aa06ed03 [file] [log] [blame]
Joe Onorato0589f0f2010-02-08 13:44:00 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Joe Onorato0589f0f2010-02-08 13:44:00 -080018
Winson Chungd83f5f42012-02-13 14:27:42 -080019import android.app.ActivityManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -080020import android.content.ComponentName;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080021import android.content.ContentValues;
Winson Chungd83f5f42012-02-13 14:27:42 -080022import android.content.Context;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent;
Michael Jurkadac85912012-05-18 15:04:49 -070024import android.content.pm.ActivityInfo;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070025import android.content.pm.ApplicationInfo;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080026import android.content.pm.PackageInfo;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.pm.PackageManager;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070028import android.content.pm.PackageManager.NameNotFoundException;
Michael Jurkac9a96192010-11-01 11:52:08 -070029import android.content.res.Resources;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080030import android.database.Cursor;
31import android.database.sqlite.SQLiteDatabase;
32import android.database.sqlite.SQLiteOpenHelper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080033import android.graphics.Bitmap;
Romain Guya28fd3f2010-03-15 14:44:42 -070034import android.graphics.Canvas;
Joe Onorato0589f0f2010-02-08 13:44:00 -080035import android.graphics.drawable.Drawable;
Sunny Goyal34942622014-08-29 17:20:55 -070036import android.text.TextUtils;
Chris Wren6d0dde02014-02-10 12:16:54 -050037import android.util.Log;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038
Kenny Guyed131872014-04-30 03:02:21 +010039import com.android.launcher3.compat.LauncherActivityInfoCompat;
40import com.android.launcher3.compat.LauncherAppsCompat;
41import com.android.launcher3.compat.UserHandleCompat;
42import com.android.launcher3.compat.UserManagerCompat;
43
Joe Onorato0589f0f2010-02-08 13:44:00 -080044import java.util.HashMap;
Chris Wren6d0dde02014-02-10 12:16:54 -050045import java.util.HashSet;
Adam Cohenb6d33df2013-10-15 10:18:02 -070046import java.util.Iterator;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080047import java.util.List;
Adam Cohenb6d33df2013-10-15 10:18:02 -070048import java.util.Map.Entry;
Joe Onorato0589f0f2010-02-08 13:44:00 -080049
50/**
51 * Cache of application icons. Icons can be made from any thread.
52 */
53public class IconCache {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070054
Joe Onorato0589f0f2010-02-08 13:44:00 -080055 private static final String TAG = "Launcher.IconCache";
56
57 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050058
Sunny Goyal0fc1be12014-08-11 17:05:23 -070059 // Empty class name is used for storing package default entry.
60 private static final String EMPTY_CLASS_NAME = ".";
61
Sunny Goyalbbef77d2014-09-09 16:27:55 -070062 private static final boolean DEBUG = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -080063
64 private static class CacheEntry {
65 public Bitmap icon;
Kenny Guyd6fe5262014-07-21 17:11:41 +010066 public CharSequence title;
67 public CharSequence contentDescription;
Joe Onorato0589f0f2010-02-08 13:44:00 -080068 }
69
Kenny Guyed131872014-04-30 03:02:21 +010070 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 Sandlercc8befa2013-06-11 14:45:48 -040093 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070094 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010095 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);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080099 private final int mIconDpi;
100 private final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800101
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400102 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -0800103 ActivityManager activityManager =
104 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
105
Joe Onorato0589f0f2010-02-08 13:44:00 -0800106 mContext = context;
107 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +0100108 mUserManager = UserManagerCompat.getInstance(mContext);
109 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Winson Chungd83f5f42012-02-13 14:27:42 -0800110 mIconDpi = activityManager.getLauncherLargeIconDensity();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800111 mIconDb = new IconDB(context);
Romain Guya28fd3f2010-03-15 14:44:42 -0700112 }
113
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800114 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700115 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700116 }
117
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700118 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700119 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700120 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700121 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700122 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700123 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700124 }
Michael Jurka721d9722011-08-03 11:49:59 -0700125
126 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700127 }
128
Winson Chung0b9fcf52011-10-31 13:05:15 -0700129 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700130 Resources resources;
131 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700132 resources = mPackageManager.getResourcesForApplication(packageName);
133 } catch (PackageManager.NameNotFoundException e) {
134 resources = null;
135 }
136 if (resources != null) {
137 if (iconId != 0) {
138 return getFullResIcon(resources, iconId);
139 }
140 }
141 return getFullResDefaultActivityIcon();
142 }
143
Sunny Goyalffe83f12014-08-14 17:39:34 -0700144 public int getFullResIconDpi() {
145 return mIconDpi;
146 }
147
Michael Jurkadac85912012-05-18 15:04:49 -0700148 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700149 Resources resources;
150 try {
151 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700152 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700153 } catch (PackageManager.NameNotFoundException e) {
154 resources = null;
155 }
156 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700157 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700158 if (iconId != 0) {
159 return getFullResIcon(resources, iconId);
160 }
161 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500162
Michael Jurkac9a96192010-11-01 11:52:08 -0700163 return getFullResDefaultActivityIcon();
164 }
165
Kenny Guyed131872014-04-30 03:02:21 +0100166 private Bitmap makeDefaultIcon(UserHandleCompat user) {
167 Drawable unbadged = getFullResDefaultActivityIcon();
168 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700169 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
170 Math.max(d.getIntrinsicHeight(), 1),
171 Bitmap.Config.ARGB_8888);
172 Canvas c = new Canvas(b);
173 d.setBounds(0, 0, b.getWidth(), b.getHeight());
174 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700175 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700176 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800177 }
178
179 /**
180 * Remove any records for the supplied ComponentName.
181 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700182 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
183 mCache.remove(new CacheKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800184 }
185
186 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800187 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500188 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800189 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100190 HashSet<CacheKey> forDeletion = new HashSet<CacheKey>();
191 for (CacheKey key: mCache.keySet()) {
192 if (key.componentName.getPackageName().equals(packageName)
193 && key.user.equals(user)) {
194 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500195 }
196 }
Kenny Guyed131872014-04-30 03:02:21 +0100197 for (CacheKey condemned: forDeletion) {
198 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500199 }
200 }
201
202 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800203 * Updates the entries related to the given package in memory and persistent DB.
204 */
205 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
206 removeIconsForPkg(packageName, user);
207 try {
208 PackageInfo info = mPackageManager.getPackageInfo(packageName,
209 PackageManager.GET_UNINSTALLED_PACKAGES);
210 long userSerial = mUserManager.getSerialNumberForUser(user);
211 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
212 addIconToDB(app, info, userSerial);
213 }
214 } catch (NameNotFoundException e) {
215 Log.d(TAG, "Package not found", e);
216 return;
217 }
218 }
219
220 /**
221 * Removes the entries related to the given package in memory and persistent DB.
222 */
223 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
224 removeFromMemCacheLocked(packageName, user);
225 long userSerial = mUserManager.getSerialNumberForUser(user);
226 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
227 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
228 new String[] {packageName + "/%", Long.toString(userSerial)});
229 }
230
231 /**
232 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
233 * the DB and are updated.
234 * @return The set of packages for which icons have updated.
235 */
236 public HashSet<String> updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
237 long userSerial = mUserManager.getSerialNumberForUser(user);
238 PackageManager pm = mContext.getPackageManager();
239 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
240 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
241 pkgInfoMap.put(info.packageName, info);
242 }
243
244 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
245 for (LauncherActivityInfoCompat app : apps) {
246 componentMap.put(app.getComponentName(), app);
247 }
248
249 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
250 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
251 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION},
252 IconDB.COLUMN_USER + " = ? ",
253 new String[] {Long.toString(userSerial)},
254 null, null, null);
255
256 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
257 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
258 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
259 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
260
261 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
262 HashSet<String> updatedPackages = new HashSet<String>();
263
264 while (c.moveToNext()) {
265 String cn = c.getString(indexComponent);
266 ComponentName component = ComponentName.unflattenFromString(cn);
267 PackageInfo info = pkgInfoMap.get(component.getPackageName());
268 if (info == null) {
269 itemsToRemove.add(c.getInt(rowIndex));
270 continue;
271 }
272 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
273 // Application is not present
274 continue;
275 }
276
277 long updateTime = c.getLong(indexLastUpdate);
278 int version = c.getInt(indexVersion);
279 LauncherActivityInfoCompat app = componentMap.remove(component);
280 if (version == info.versionCode && updateTime == info.lastUpdateTime) {
281 continue;
282 }
283 if (app == null) {
284 itemsToRemove.add(c.getInt(rowIndex));
285 continue;
286 }
287 ContentValues values = updateCacheAndGetContentValues(app);
288 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
289 IconDB.COLUMN_COMPONENT + " = ?",
290 new String[] { cn });
291
292 updatedPackages.add(component.getPackageName());
293 }
294 c.close();
295 if (!itemsToRemove.isEmpty()) {
296 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
297 IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )",
298 null);
299 }
300
301 // Insert remaining apps.
302 for (LauncherActivityInfoCompat app : componentMap.values()) {
303 PackageInfo info = pkgInfoMap.get(app.getComponentName().getPackageName());
304 if (info == null) {
305 continue;
306 }
307 addIconToDB(app, info, userSerial);
308 }
309 return updatedPackages;
310 }
311
312 private void addIconToDB(LauncherActivityInfoCompat app, PackageInfo info, long userSerial) {
313 ContentValues values = updateCacheAndGetContentValues(app);
314 values.put(IconDB.COLUMN_COMPONENT, app.getComponentName().flattenToString());
315 values.put(IconDB.COLUMN_USER, userSerial);
316 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
317 values.put(IconDB.COLUMN_VERSION, info.versionCode);
318 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
319 SQLiteDatabase.CONFLICT_REPLACE);
320 }
321
322 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app) {
323 CacheEntry entry = new CacheEntry();
324 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
325 entry.title = app.getLabel();
326 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
327 mCache.put(new CacheKey(app.getComponentName(), app.getUser()), entry);
328
329 ContentValues values = new ContentValues();
330 values.put(IconDB.COLUMN_ICON, ItemInfo.flattenBitmap(entry.icon));
331 values.put(IconDB.COLUMN_LABEL, entry.title.toString());
332 return values;
333 }
334
335
336 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800337 * Empty out the cache.
338 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700339 public synchronized void flush() {
340 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800341 }
342
343 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700344 * Empty out the cache that aren't of the correct grid size
345 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700346 public synchronized void flushInvalidIcons(DeviceProfile grid) {
347 Iterator<Entry<CacheKey, CacheEntry>> it = mCache.entrySet().iterator();
348 while (it.hasNext()) {
349 final CacheEntry e = it.next().getValue();
350 if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
351 || e.icon.getHeight() < grid.iconSizePx)) {
352 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700353 }
354 }
355 }
356
357 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800358 * Fill in "application" with the icon and label for "info."
359 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800360 public synchronized void getTitleAndIcon(AppInfo application, LauncherActivityInfoCompat info) {
361 CacheEntry entry = cacheLocked(application.componentName, info, info.getUser(), false);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800362
Sunny Goyal736f5af2014-10-16 14:07:29 -0700363 application.title = entry.title;
364 application.iconBitmap = entry.icon;
365 application.contentDescription = entry.contentDescription;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800366 }
367
Sunny Goyal736f5af2014-10-16 14:07:29 -0700368 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
369 ComponentName component = intent.getComponent();
370 // null info means not installed, but if we have a component from the intent then
371 // we should still look in the cache for restored app icons.
372 if (component == null) {
373 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800374 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700375
376 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800377 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700378 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800379 }
380
Sunny Goyal34942622014-08-29 17:20:55 -0700381 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800382 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
383 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700384 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700385 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800386 UserHandleCompat user) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700387 ComponentName component = intent.getComponent();
388 // null info means not installed, but if we have a component from the intent then
389 // we should still look in the cache for restored app icons.
390 if (component == null) {
391 shortcutInfo.setIcon(getDefaultIcon(user));
392 shortcutInfo.title = "";
393 shortcutInfo.usingFallbackIcon = true;
394 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800395 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
396 getTitleAndIcon(shortcutInfo, component, info, user, true);
Sunny Goyal34942622014-08-29 17:20:55 -0700397 }
398 }
399
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800400 /**
401 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
402 */
403 public synchronized void getTitleAndIcon(
404 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
405 UserHandleCompat user, boolean usePkgIcon) {
406 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon);
407 shortcutInfo.setIcon(entry.icon);
408 shortcutInfo.title = entry.title;
409 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
410 }
Sunny Goyal34942622014-08-29 17:20:55 -0700411
Sunny Goyal736f5af2014-10-16 14:07:29 -0700412 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100413 if (!mDefaultIcons.containsKey(user)) {
414 mDefaultIcons.put(user, makeDefaultIcon(user));
415 }
416 return mDefaultIcons.get(user);
417 }
418
Kenny Guyed131872014-04-30 03:02:21 +0100419 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
420 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700421 }
422
Sunny Goyal736f5af2014-10-16 14:07:29 -0700423 /**
424 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
425 * This method is not thread safe, it must be called from a synchronized method.
426 */
Kenny Guyed131872014-04-30 03:02:21 +0100427 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800428 UserHandleCompat user, boolean usePackageIcon) {
Kenny Guyed131872014-04-30 03:02:21 +0100429 CacheKey cacheKey = new CacheKey(componentName, user);
430 CacheEntry entry = mCache.get(cacheKey);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800431 if (entry == null) {
432 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100433 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500434
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800435 // Check the DB first.
436 if (!getEntryFromDB(componentName, user, entry)) {
437 if (info != null) {
438 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500439 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700440 if (usePackageIcon) {
441 CacheEntry packageEntry = getEntryForPackage(
442 componentName.getPackageName(), user);
Sunny Goyal34942622014-08-29 17:20:55 -0700443 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700444 if (DEBUG) Log.d(TAG, "using package default icon for " +
445 componentName.toShortString());
446 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700447 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800448 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700449 }
450 }
451 if (entry.icon == null) {
452 if (DEBUG) Log.d(TAG, "using default icon for " +
453 componentName.toShortString());
454 entry.icon = getDefaultIcon(user);
455 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700456 }
457 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800458
459 if (TextUtils.isEmpty(entry.title) && info != null) {
460 entry.title = info.getLabel().toString();
461 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
462 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800463 }
464 return entry;
465 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400466
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700467 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700468 * Adds a default package entry in the cache. This entry is not persisted and will be removed
469 * when the cache is flushed.
470 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700471 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700472 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800473 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700474
Sunny Goyal34942622014-08-29 17:20:55 -0700475 CacheEntry entry = getEntryForPackage(packageName, user);
476 if (!TextUtils.isEmpty(title)) {
477 entry.title = title;
478 }
479 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700480 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700481 }
482 }
483
484 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700485 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700486 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700487 */
488 private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700489 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700490 CacheKey cacheKey = new CacheKey(cn, user);
491 CacheEntry entry = mCache.get(cacheKey);
492 if (entry == null) {
493 entry = new CacheEntry();
Sunny Goyal34942622014-08-29 17:20:55 -0700494 entry.title = "";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800495 entry.contentDescription = "";
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700496 mCache.put(cacheKey, entry);
497
498 try {
499 ApplicationInfo info = mPackageManager.getApplicationInfo(packageName, 0);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700500 entry.icon = Utilities.createIconBitmap(info.loadIcon(mPackageManager), mContext);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800501 entry.title = info.loadLabel(mPackageManager);
502 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700503 } catch (NameNotFoundException e) {
504 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
505 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700506 }
507 return entry;
508 }
509
Chris Wren6d0dde02014-02-10 12:16:54 -0500510 /**
511 * Pre-load an icon into the persistent cache.
512 *
513 * <P>Queries for a component that does not exist in the package manager
514 * will be answered by the persistent cache.
515 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500516 * @param componentName the icon should be returned for this component
517 * @param icon the icon to be persisted
518 * @param dpi the native density of the icon
519 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800520 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
521 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500522 // TODO rescale to the correct native DPI
523 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800524 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500525 packageManager.getActivityIcon(componentName);
526 // component is present on the system already, do nothing
527 return;
528 } catch (PackageManager.NameNotFoundException e) {
529 // pass
530 }
531
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800532 ContentValues values = new ContentValues();
533 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
534 values.put(IconDB.COLUMN_USER, userSerial);
535 values.put(IconDB.COLUMN_ICON, ItemInfo.flattenBitmap(icon));
536 values.put(IconDB.COLUMN_LABEL, label);
537 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
538 SQLiteDatabase.CONFLICT_REPLACE);
539 }
540
541 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user, CacheEntry entry) {
542 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
543 new String[] {IconDB.COLUMN_ICON, IconDB.COLUMN_LABEL},
544 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
545 new String[] {component.flattenToString(),
546 Long.toString(mUserManager.getSerialNumberForUser(user))},
547 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500548 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800549 if (c.moveToNext()) {
550 entry.icon = Utilities.createIconBitmap(c, 0, mContext);
551 entry.title = c.getString(1);
552 if (entry.title == null) {
553 entry.title = "";
554 entry.contentDescription = "";
555 } else {
556 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500557 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800558 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500559 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500560 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800561 c.close();
562 }
563 return false;
564 }
565
566 private static final class IconDB extends SQLiteOpenHelper {
567 private final static int DB_VERSION = 1;
568
569 private final static String TABLE_NAME = "icons";
570 private final static String COLUMN_ROWID = "rowid";
571 private final static String COLUMN_COMPONENT = "componentName";
572 private final static String COLUMN_USER = "profileId";
573 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
574 private final static String COLUMN_VERSION = "version";
575 private final static String COLUMN_ICON = "icon";
576 private final static String COLUMN_LABEL = "label";
577
578 public IconDB(Context context) {
579 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
580 }
581
582 @Override
583 public void onCreate(SQLiteDatabase db) {
584 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
585 COLUMN_COMPONENT + " TEXT NOT NULL, " +
586 COLUMN_USER + " INTEGER NOT NULL, " +
587 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
588 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
589 COLUMN_ICON + " BLOB, " +
590 COLUMN_LABEL + " TEXT, " +
591 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
592 ");");
593 }
594
595 @Override
596 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
597 if (oldVersion != newVersion) {
598 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500599 }
600 }
601
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800602 @Override
603 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
604 if (oldVersion != newVersion) {
605 clearDB(db);
606 }
Kenny Guyed131872014-04-30 03:02:21 +0100607 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500608
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800609 private void clearDB(SQLiteDatabase db) {
610 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
611 onCreate(db);
612 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500613 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800614}