blob: 43f838e7f6b7e1fd74e3ac949a576f95adb85988 [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;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070043import com.android.launcher3.util.ComponentKey;
Kenny Guyed131872014-04-30 03:02:21 +010044
Joe Onorato0589f0f2010-02-08 13:44:00 -080045import java.util.HashMap;
Chris Wren6d0dde02014-02-10 12:16:54 -050046import java.util.HashSet;
Adam Cohenb6d33df2013-10-15 10:18:02 -070047import java.util.Iterator;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080048import java.util.List;
Adam Cohenb6d33df2013-10-15 10:18:02 -070049import java.util.Map.Entry;
Joe Onorato0589f0f2010-02-08 13:44:00 -080050
51/**
52 * Cache of application icons. Icons can be made from any thread.
53 */
54public class IconCache {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070055
Joe Onorato0589f0f2010-02-08 13:44:00 -080056 private static final String TAG = "Launcher.IconCache";
57
58 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050059
Sunny Goyal0fc1be12014-08-11 17:05:23 -070060 // Empty class name is used for storing package default entry.
61 private static final String EMPTY_CLASS_NAME = ".";
62
Sunny Goyalbbef77d2014-09-09 16:27:55 -070063 private static final boolean DEBUG = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -080064
65 private static class CacheEntry {
66 public Bitmap icon;
Kenny Guyd6fe5262014-07-21 17:11:41 +010067 public CharSequence title;
68 public CharSequence contentDescription;
Joe Onorato0589f0f2010-02-08 13:44:00 -080069 }
70
Kenny Guyed131872014-04-30 03:02:21 +010071 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons =
72 new HashMap<UserHandleCompat, Bitmap>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040073 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070074 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010075 private final UserManagerCompat mUserManager;
76 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070077 private final HashMap<ComponentKey, CacheEntry> mCache =
78 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080079 private final int mIconDpi;
80 private final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080081
Daniel Sandlercc8befa2013-06-11 14:45:48 -040082 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -080083 ActivityManager activityManager =
84 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
85
Joe Onorato0589f0f2010-02-08 13:44:00 -080086 mContext = context;
87 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +010088 mUserManager = UserManagerCompat.getInstance(mContext);
89 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Winson Chungd83f5f42012-02-13 14:27:42 -080090 mIconDpi = activityManager.getLauncherLargeIconDensity();
Sunny Goyal4fbc3822015-02-18 16:46:50 -080091 mIconDb = new IconDB(context);
Romain Guya28fd3f2010-03-15 14:44:42 -070092 }
93
Sunny Goyal4fbc3822015-02-18 16:46:50 -080094 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -070095 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -070096 }
97
Sunny Goyalb50cc8c2014-10-06 16:23:56 -070098 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -070099 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700100 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700101 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700102 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700103 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700104 }
Michael Jurka721d9722011-08-03 11:49:59 -0700105
106 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700107 }
108
Winson Chung0b9fcf52011-10-31 13:05:15 -0700109 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700110 Resources resources;
111 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700112 resources = mPackageManager.getResourcesForApplication(packageName);
113 } catch (PackageManager.NameNotFoundException e) {
114 resources = null;
115 }
116 if (resources != null) {
117 if (iconId != 0) {
118 return getFullResIcon(resources, iconId);
119 }
120 }
121 return getFullResDefaultActivityIcon();
122 }
123
Sunny Goyalffe83f12014-08-14 17:39:34 -0700124 public int getFullResIconDpi() {
125 return mIconDpi;
126 }
127
Michael Jurkadac85912012-05-18 15:04:49 -0700128 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700129 Resources resources;
130 try {
131 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700132 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700133 } catch (PackageManager.NameNotFoundException e) {
134 resources = null;
135 }
136 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700137 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700138 if (iconId != 0) {
139 return getFullResIcon(resources, iconId);
140 }
141 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500142
Michael Jurkac9a96192010-11-01 11:52:08 -0700143 return getFullResDefaultActivityIcon();
144 }
145
Kenny Guyed131872014-04-30 03:02:21 +0100146 private Bitmap makeDefaultIcon(UserHandleCompat user) {
147 Drawable unbadged = getFullResDefaultActivityIcon();
148 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700149 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
150 Math.max(d.getIntrinsicHeight(), 1),
151 Bitmap.Config.ARGB_8888);
152 Canvas c = new Canvas(b);
153 d.setBounds(0, 0, b.getWidth(), b.getHeight());
154 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700155 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700156 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800157 }
158
159 /**
160 * Remove any records for the supplied ComponentName.
161 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700162 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700163 mCache.remove(new ComponentKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800164 }
165
166 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800167 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500168 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800169 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700170 HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
171 for (ComponentKey key: mCache.keySet()) {
Kenny Guyed131872014-04-30 03:02:21 +0100172 if (key.componentName.getPackageName().equals(packageName)
173 && key.user.equals(user)) {
174 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500175 }
176 }
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700177 for (ComponentKey condemned: forDeletion) {
Kenny Guyed131872014-04-30 03:02:21 +0100178 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500179 }
180 }
181
182 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800183 * Updates the entries related to the given package in memory and persistent DB.
184 */
185 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
186 removeIconsForPkg(packageName, user);
187 try {
188 PackageInfo info = mPackageManager.getPackageInfo(packageName,
189 PackageManager.GET_UNINSTALLED_PACKAGES);
190 long userSerial = mUserManager.getSerialNumberForUser(user);
191 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
192 addIconToDB(app, info, userSerial);
193 }
194 } catch (NameNotFoundException e) {
195 Log.d(TAG, "Package not found", e);
196 return;
197 }
198 }
199
200 /**
201 * Removes the entries related to the given package in memory and persistent DB.
202 */
203 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
204 removeFromMemCacheLocked(packageName, user);
205 long userSerial = mUserManager.getSerialNumberForUser(user);
206 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
207 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
208 new String[] {packageName + "/%", Long.toString(userSerial)});
209 }
210
211 /**
212 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
213 * the DB and are updated.
214 * @return The set of packages for which icons have updated.
215 */
216 public HashSet<String> updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
217 long userSerial = mUserManager.getSerialNumberForUser(user);
218 PackageManager pm = mContext.getPackageManager();
219 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
220 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
221 pkgInfoMap.put(info.packageName, info);
222 }
223
224 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
225 for (LauncherActivityInfoCompat app : apps) {
226 componentMap.put(app.getComponentName(), app);
227 }
228
229 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
230 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
231 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION},
232 IconDB.COLUMN_USER + " = ? ",
233 new String[] {Long.toString(userSerial)},
234 null, null, null);
235
236 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
237 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
238 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
239 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
240
241 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
242 HashSet<String> updatedPackages = new HashSet<String>();
243
244 while (c.moveToNext()) {
245 String cn = c.getString(indexComponent);
246 ComponentName component = ComponentName.unflattenFromString(cn);
247 PackageInfo info = pkgInfoMap.get(component.getPackageName());
248 if (info == null) {
249 itemsToRemove.add(c.getInt(rowIndex));
250 continue;
251 }
252 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
253 // Application is not present
254 continue;
255 }
256
257 long updateTime = c.getLong(indexLastUpdate);
258 int version = c.getInt(indexVersion);
259 LauncherActivityInfoCompat app = componentMap.remove(component);
260 if (version == info.versionCode && updateTime == info.lastUpdateTime) {
261 continue;
262 }
263 if (app == null) {
264 itemsToRemove.add(c.getInt(rowIndex));
265 continue;
266 }
267 ContentValues values = updateCacheAndGetContentValues(app);
268 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
269 IconDB.COLUMN_COMPONENT + " = ?",
270 new String[] { cn });
271
272 updatedPackages.add(component.getPackageName());
273 }
274 c.close();
275 if (!itemsToRemove.isEmpty()) {
276 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
277 IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )",
278 null);
279 }
280
281 // Insert remaining apps.
282 for (LauncherActivityInfoCompat app : componentMap.values()) {
283 PackageInfo info = pkgInfoMap.get(app.getComponentName().getPackageName());
284 if (info == null) {
285 continue;
286 }
287 addIconToDB(app, info, userSerial);
288 }
289 return updatedPackages;
290 }
291
292 private void addIconToDB(LauncherActivityInfoCompat app, PackageInfo info, long userSerial) {
293 ContentValues values = updateCacheAndGetContentValues(app);
294 values.put(IconDB.COLUMN_COMPONENT, app.getComponentName().flattenToString());
295 values.put(IconDB.COLUMN_USER, userSerial);
296 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
297 values.put(IconDB.COLUMN_VERSION, info.versionCode);
298 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
299 SQLiteDatabase.CONFLICT_REPLACE);
300 }
301
302 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app) {
303 CacheEntry entry = new CacheEntry();
304 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
305 entry.title = app.getLabel();
306 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700307 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800308
309 ContentValues values = new ContentValues();
310 values.put(IconDB.COLUMN_ICON, ItemInfo.flattenBitmap(entry.icon));
311 values.put(IconDB.COLUMN_LABEL, entry.title.toString());
312 return values;
313 }
314
315
316 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800317 * Empty out the cache.
318 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700319 public synchronized void flush() {
320 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800321 }
322
323 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700324 * Empty out the cache that aren't of the correct grid size
325 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700326 public synchronized void flushInvalidIcons(DeviceProfile grid) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700327 Iterator<Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator();
Sunny Goyal736f5af2014-10-16 14:07:29 -0700328 while (it.hasNext()) {
329 final CacheEntry e = it.next().getValue();
330 if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
331 || e.icon.getHeight() < grid.iconSizePx)) {
332 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700333 }
334 }
335 }
336
337 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800338 * Fill in "application" with the icon and label for "info."
339 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800340 public synchronized void getTitleAndIcon(AppInfo application, LauncherActivityInfoCompat info) {
341 CacheEntry entry = cacheLocked(application.componentName, info, info.getUser(), false);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800342
Sunny Goyal736f5af2014-10-16 14:07:29 -0700343 application.title = entry.title;
344 application.iconBitmap = entry.icon;
345 application.contentDescription = entry.contentDescription;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800346 }
347
Sunny Goyal736f5af2014-10-16 14:07:29 -0700348 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
349 ComponentName component = intent.getComponent();
350 // null info means not installed, but if we have a component from the intent then
351 // we should still look in the cache for restored app icons.
352 if (component == null) {
353 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800354 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700355
356 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800357 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700358 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800359 }
360
Sunny Goyal34942622014-08-29 17:20:55 -0700361 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800362 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
363 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700364 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700365 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800366 UserHandleCompat user) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700367 ComponentName component = intent.getComponent();
368 // null info means not installed, but if we have a component from the intent then
369 // we should still look in the cache for restored app icons.
370 if (component == null) {
371 shortcutInfo.setIcon(getDefaultIcon(user));
372 shortcutInfo.title = "";
373 shortcutInfo.usingFallbackIcon = true;
374 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800375 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
376 getTitleAndIcon(shortcutInfo, component, info, user, true);
Sunny Goyal34942622014-08-29 17:20:55 -0700377 }
378 }
379
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800380 /**
381 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
382 */
383 public synchronized void getTitleAndIcon(
384 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
385 UserHandleCompat user, boolean usePkgIcon) {
386 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon);
387 shortcutInfo.setIcon(entry.icon);
388 shortcutInfo.title = entry.title;
389 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
390 }
Sunny Goyal34942622014-08-29 17:20:55 -0700391
Sunny Goyal736f5af2014-10-16 14:07:29 -0700392 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100393 if (!mDefaultIcons.containsKey(user)) {
394 mDefaultIcons.put(user, makeDefaultIcon(user));
395 }
396 return mDefaultIcons.get(user);
397 }
398
Kenny Guyed131872014-04-30 03:02:21 +0100399 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
400 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700401 }
402
Sunny Goyal736f5af2014-10-16 14:07:29 -0700403 /**
404 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
405 * This method is not thread safe, it must be called from a synchronized method.
406 */
Kenny Guyed131872014-04-30 03:02:21 +0100407 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800408 UserHandleCompat user, boolean usePackageIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700409 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100410 CacheEntry entry = mCache.get(cacheKey);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800411 if (entry == null) {
412 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100413 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500414
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800415 // Check the DB first.
416 if (!getEntryFromDB(componentName, user, entry)) {
417 if (info != null) {
418 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500419 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700420 if (usePackageIcon) {
421 CacheEntry packageEntry = getEntryForPackage(
422 componentName.getPackageName(), user);
Sunny Goyal34942622014-08-29 17:20:55 -0700423 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700424 if (DEBUG) Log.d(TAG, "using package default icon for " +
425 componentName.toShortString());
426 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700427 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800428 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700429 }
430 }
431 if (entry.icon == null) {
432 if (DEBUG) Log.d(TAG, "using default icon for " +
433 componentName.toShortString());
434 entry.icon = getDefaultIcon(user);
435 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700436 }
437 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800438
439 if (TextUtils.isEmpty(entry.title) && info != null) {
440 entry.title = info.getLabel().toString();
441 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
442 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800443 }
444 return entry;
445 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400446
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700447 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700448 * Adds a default package entry in the cache. This entry is not persisted and will be removed
449 * when the cache is flushed.
450 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700451 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700452 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800453 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700454
Sunny Goyal34942622014-08-29 17:20:55 -0700455 CacheEntry entry = getEntryForPackage(packageName, user);
456 if (!TextUtils.isEmpty(title)) {
457 entry.title = title;
458 }
459 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700460 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700461 }
462 }
463
464 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700465 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700466 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700467 */
468 private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700469 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700470 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700471 CacheEntry entry = mCache.get(cacheKey);
472 if (entry == null) {
473 entry = new CacheEntry();
Sunny Goyal34942622014-08-29 17:20:55 -0700474 entry.title = "";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800475 entry.contentDescription = "";
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700476 mCache.put(cacheKey, entry);
477
478 try {
479 ApplicationInfo info = mPackageManager.getApplicationInfo(packageName, 0);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700480 entry.icon = Utilities.createIconBitmap(info.loadIcon(mPackageManager), mContext);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800481 entry.title = info.loadLabel(mPackageManager);
482 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700483 } catch (NameNotFoundException e) {
484 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
485 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700486 }
487 return entry;
488 }
489
Chris Wren6d0dde02014-02-10 12:16:54 -0500490 /**
491 * Pre-load an icon into the persistent cache.
492 *
493 * <P>Queries for a component that does not exist in the package manager
494 * will be answered by the persistent cache.
495 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500496 * @param componentName the icon should be returned for this component
497 * @param icon the icon to be persisted
498 * @param dpi the native density of the icon
499 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800500 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
501 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500502 // TODO rescale to the correct native DPI
503 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800504 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500505 packageManager.getActivityIcon(componentName);
506 // component is present on the system already, do nothing
507 return;
508 } catch (PackageManager.NameNotFoundException e) {
509 // pass
510 }
511
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800512 ContentValues values = new ContentValues();
513 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
514 values.put(IconDB.COLUMN_USER, userSerial);
515 values.put(IconDB.COLUMN_ICON, ItemInfo.flattenBitmap(icon));
516 values.put(IconDB.COLUMN_LABEL, label);
517 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
518 SQLiteDatabase.CONFLICT_REPLACE);
519 }
520
521 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user, CacheEntry entry) {
522 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
523 new String[] {IconDB.COLUMN_ICON, IconDB.COLUMN_LABEL},
524 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
525 new String[] {component.flattenToString(),
526 Long.toString(mUserManager.getSerialNumberForUser(user))},
527 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500528 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800529 if (c.moveToNext()) {
530 entry.icon = Utilities.createIconBitmap(c, 0, mContext);
531 entry.title = c.getString(1);
532 if (entry.title == null) {
533 entry.title = "";
534 entry.contentDescription = "";
535 } else {
536 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500537 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800538 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500539 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500540 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800541 c.close();
542 }
543 return false;
544 }
545
546 private static final class IconDB extends SQLiteOpenHelper {
547 private final static int DB_VERSION = 1;
548
549 private final static String TABLE_NAME = "icons";
550 private final static String COLUMN_ROWID = "rowid";
551 private final static String COLUMN_COMPONENT = "componentName";
552 private final static String COLUMN_USER = "profileId";
553 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
554 private final static String COLUMN_VERSION = "version";
555 private final static String COLUMN_ICON = "icon";
556 private final static String COLUMN_LABEL = "label";
557
558 public IconDB(Context context) {
559 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
560 }
561
562 @Override
563 public void onCreate(SQLiteDatabase db) {
564 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
565 COLUMN_COMPONENT + " TEXT NOT NULL, " +
566 COLUMN_USER + " INTEGER NOT NULL, " +
567 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
568 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
569 COLUMN_ICON + " BLOB, " +
570 COLUMN_LABEL + " TEXT, " +
571 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
572 ");");
573 }
574
575 @Override
576 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
577 if (oldVersion != newVersion) {
578 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500579 }
580 }
581
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800582 @Override
583 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
584 if (oldVersion != newVersion) {
585 clearDB(db);
586 }
Kenny Guyed131872014-04-30 03:02:21 +0100587 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500588
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800589 private void clearDB(SQLiteDatabase db) {
590 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
591 onCreate(db);
592 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500593 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800594}