blob: 57d23a7bbd0a29f5c28b0ab4f70a1f661e284046 [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;
Sunny Goyal34b65272015-03-11 16:56:52 -070034import android.graphics.BitmapFactory;
Romain Guya28fd3f2010-03-15 14:44:42 -070035import android.graphics.Canvas;
Joe Onorato0589f0f2010-02-08 13:44:00 -080036import android.graphics.drawable.Drawable;
Sunny Goyal34b65272015-03-11 16:56:52 -070037import android.os.Handler;
Sunny Goyal34942622014-08-29 17:20:55 -070038import android.text.TextUtils;
Chris Wren6d0dde02014-02-10 12:16:54 -050039import android.util.Log;
Joe Onorato0589f0f2010-02-08 13:44:00 -080040
Kenny Guyed131872014-04-30 03:02:21 +010041import com.android.launcher3.compat.LauncherActivityInfoCompat;
42import com.android.launcher3.compat.LauncherAppsCompat;
43import com.android.launcher3.compat.UserHandleCompat;
44import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070045import com.android.launcher3.util.ComponentKey;
Kenny Guyed131872014-04-30 03:02:21 +010046
Joe Onorato0589f0f2010-02-08 13:44:00 -080047import java.util.HashMap;
Chris Wren6d0dde02014-02-10 12:16:54 -050048import java.util.HashSet;
Adam Cohenb6d33df2013-10-15 10:18:02 -070049import java.util.Iterator;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080050import java.util.List;
Adam Cohenb6d33df2013-10-15 10:18:02 -070051import java.util.Map.Entry;
Joe Onorato0589f0f2010-02-08 13:44:00 -080052
53/**
54 * Cache of application icons. Icons can be made from any thread.
55 */
56public class IconCache {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070057
Joe Onorato0589f0f2010-02-08 13:44:00 -080058 private static final String TAG = "Launcher.IconCache";
59
60 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050061
Sunny Goyal0fc1be12014-08-11 17:05:23 -070062 // Empty class name is used for storing package default entry.
63 private static final String EMPTY_CLASS_NAME = ".";
64
Sunny Goyalbbef77d2014-09-09 16:27:55 -070065 private static final boolean DEBUG = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -080066
Sunny Goyal34b65272015-03-11 16:56:52 -070067 private static final int LOW_RES_SCALE_FACTOR = 8;
68
Joe Onorato0589f0f2010-02-08 13:44:00 -080069 private static class CacheEntry {
70 public Bitmap icon;
Kenny Guyd6fe5262014-07-21 17:11:41 +010071 public CharSequence title;
72 public CharSequence contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -070073 public boolean isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -080074 }
75
Kenny Guyed131872014-04-30 03:02:21 +010076 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons =
77 new HashMap<UserHandleCompat, Bitmap>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040078 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070079 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010080 private final UserManagerCompat mUserManager;
81 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070082 private final HashMap<ComponentKey, CacheEntry> mCache =
83 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080084 private final int mIconDpi;
85 private final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080086
Sunny Goyal34b65272015-03-11 16:56:52 -070087 private final Handler mWorkerHandler;
88
Daniel Sandlercc8befa2013-06-11 14:45:48 -040089 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -080090 ActivityManager activityManager =
91 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
92
Joe Onorato0589f0f2010-02-08 13:44:00 -080093 mContext = context;
94 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +010095 mUserManager = UserManagerCompat.getInstance(mContext);
96 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Winson Chungd83f5f42012-02-13 14:27:42 -080097 mIconDpi = activityManager.getLauncherLargeIconDensity();
Sunny Goyal4fbc3822015-02-18 16:46:50 -080098 mIconDb = new IconDB(context);
Sunny Goyal34b65272015-03-11 16:56:52 -070099
100 mWorkerHandler = new Handler(LauncherModel.sWorkerThread.getLooper());
Romain Guya28fd3f2010-03-15 14:44:42 -0700101 }
102
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800103 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700104 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700105 }
106
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700107 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700108 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700109 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700110 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700111 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700112 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700113 }
Michael Jurka721d9722011-08-03 11:49:59 -0700114
115 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700116 }
117
Winson Chung0b9fcf52011-10-31 13:05:15 -0700118 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700119 Resources resources;
120 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700121 resources = mPackageManager.getResourcesForApplication(packageName);
122 } catch (PackageManager.NameNotFoundException e) {
123 resources = null;
124 }
125 if (resources != null) {
126 if (iconId != 0) {
127 return getFullResIcon(resources, iconId);
128 }
129 }
130 return getFullResDefaultActivityIcon();
131 }
132
Sunny Goyalffe83f12014-08-14 17:39:34 -0700133 public int getFullResIconDpi() {
134 return mIconDpi;
135 }
136
Michael Jurkadac85912012-05-18 15:04:49 -0700137 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700138 Resources resources;
139 try {
140 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700141 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700142 } catch (PackageManager.NameNotFoundException e) {
143 resources = null;
144 }
145 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700146 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700147 if (iconId != 0) {
148 return getFullResIcon(resources, iconId);
149 }
150 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500151
Michael Jurkac9a96192010-11-01 11:52:08 -0700152 return getFullResDefaultActivityIcon();
153 }
154
Kenny Guyed131872014-04-30 03:02:21 +0100155 private Bitmap makeDefaultIcon(UserHandleCompat user) {
156 Drawable unbadged = getFullResDefaultActivityIcon();
157 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700158 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
159 Math.max(d.getIntrinsicHeight(), 1),
160 Bitmap.Config.ARGB_8888);
161 Canvas c = new Canvas(b);
162 d.setBounds(0, 0, b.getWidth(), b.getHeight());
163 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700164 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700165 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800166 }
167
168 /**
169 * Remove any records for the supplied ComponentName.
170 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700171 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700172 mCache.remove(new ComponentKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800173 }
174
175 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800176 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500177 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800178 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700179 HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
180 for (ComponentKey key: mCache.keySet()) {
Kenny Guyed131872014-04-30 03:02:21 +0100181 if (key.componentName.getPackageName().equals(packageName)
182 && key.user.equals(user)) {
183 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500184 }
185 }
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700186 for (ComponentKey condemned: forDeletion) {
Kenny Guyed131872014-04-30 03:02:21 +0100187 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500188 }
189 }
190
191 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800192 * Updates the entries related to the given package in memory and persistent DB.
193 */
194 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
195 removeIconsForPkg(packageName, user);
196 try {
197 PackageInfo info = mPackageManager.getPackageInfo(packageName,
198 PackageManager.GET_UNINSTALLED_PACKAGES);
199 long userSerial = mUserManager.getSerialNumberForUser(user);
200 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
201 addIconToDB(app, info, userSerial);
202 }
203 } catch (NameNotFoundException e) {
204 Log.d(TAG, "Package not found", e);
205 return;
206 }
207 }
208
209 /**
210 * Removes the entries related to the given package in memory and persistent DB.
211 */
212 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
213 removeFromMemCacheLocked(packageName, user);
214 long userSerial = mUserManager.getSerialNumberForUser(user);
215 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
216 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
217 new String[] {packageName + "/%", Long.toString(userSerial)});
218 }
219
220 /**
221 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
222 * the DB and are updated.
223 * @return The set of packages for which icons have updated.
224 */
225 public HashSet<String> updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
226 long userSerial = mUserManager.getSerialNumberForUser(user);
227 PackageManager pm = mContext.getPackageManager();
228 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
229 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
230 pkgInfoMap.put(info.packageName, info);
231 }
232
233 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
234 for (LauncherActivityInfoCompat app : apps) {
235 componentMap.put(app.getComponentName(), app);
236 }
237
238 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
239 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
240 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION},
241 IconDB.COLUMN_USER + " = ? ",
242 new String[] {Long.toString(userSerial)},
243 null, null, null);
244
245 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
246 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
247 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
248 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
249
250 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
251 HashSet<String> updatedPackages = new HashSet<String>();
252
253 while (c.moveToNext()) {
254 String cn = c.getString(indexComponent);
255 ComponentName component = ComponentName.unflattenFromString(cn);
256 PackageInfo info = pkgInfoMap.get(component.getPackageName());
257 if (info == null) {
258 itemsToRemove.add(c.getInt(rowIndex));
259 continue;
260 }
261 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
262 // Application is not present
263 continue;
264 }
265
266 long updateTime = c.getLong(indexLastUpdate);
267 int version = c.getInt(indexVersion);
268 LauncherActivityInfoCompat app = componentMap.remove(component);
269 if (version == info.versionCode && updateTime == info.lastUpdateTime) {
270 continue;
271 }
272 if (app == null) {
273 itemsToRemove.add(c.getInt(rowIndex));
274 continue;
275 }
276 ContentValues values = updateCacheAndGetContentValues(app);
277 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
278 IconDB.COLUMN_COMPONENT + " = ?",
279 new String[] { cn });
280
281 updatedPackages.add(component.getPackageName());
282 }
283 c.close();
284 if (!itemsToRemove.isEmpty()) {
285 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
286 IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )",
287 null);
288 }
289
290 // Insert remaining apps.
291 for (LauncherActivityInfoCompat app : componentMap.values()) {
292 PackageInfo info = pkgInfoMap.get(app.getComponentName().getPackageName());
293 if (info == null) {
294 continue;
295 }
296 addIconToDB(app, info, userSerial);
297 }
298 return updatedPackages;
299 }
300
301 private void addIconToDB(LauncherActivityInfoCompat app, PackageInfo info, long userSerial) {
302 ContentValues values = updateCacheAndGetContentValues(app);
303 values.put(IconDB.COLUMN_COMPONENT, app.getComponentName().flattenToString());
304 values.put(IconDB.COLUMN_USER, userSerial);
305 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
306 values.put(IconDB.COLUMN_VERSION, info.versionCode);
307 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
308 SQLiteDatabase.CONFLICT_REPLACE);
309 }
310
311 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app) {
312 CacheEntry entry = new CacheEntry();
313 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
314 entry.title = app.getLabel();
315 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700316 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800317
Sunny Goyal34b65272015-03-11 16:56:52 -0700318 return mIconDb.newContentValues(entry.icon, entry.title.toString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800319 }
320
321
322 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800323 * Empty out the cache.
324 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700325 public synchronized void flush() {
326 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800327 }
328
329 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700330 * Empty out the cache that aren't of the correct grid size
331 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700332 public synchronized void flushInvalidIcons(DeviceProfile grid) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700333 Iterator<Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator();
Sunny Goyal736f5af2014-10-16 14:07:29 -0700334 while (it.hasNext()) {
335 final CacheEntry e = it.next().getValue();
336 if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
337 || e.icon.getHeight() < grid.iconSizePx)) {
338 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700339 }
340 }
341 }
342
343 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700344 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
345 * @return a request ID that can be used to cancel the request.
346 */
347 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
348 Runnable request = new Runnable() {
349
350 @Override
351 public void run() {
352 if (info instanceof AppInfo) {
353 getTitleAndIcon((AppInfo) info, null, false);
354 } else if (info instanceof ShortcutInfo) {
355 ShortcutInfo st = (ShortcutInfo) info;
356 getTitleAndIcon(st,
357 st.promisedIntent != null ? st.promisedIntent : st.intent,
358 st.user, false);
359 }
360 caller.post(new Runnable() {
361
362 @Override
363 public void run() {
364 caller.reapplyItemInfo(info);
365 }
366 });
367 }
368 };
369 mWorkerHandler.post(request);
370 return new IconLoadRequest(request, mWorkerHandler);
371 }
372
373 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800374 * Fill in "application" with the icon and label for "info."
375 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700376 public synchronized void getTitleAndIcon(AppInfo application,
377 LauncherActivityInfoCompat info, boolean useLowResIcon) {
378 CacheEntry entry = cacheLocked(application.componentName, info,
379 info == null ? application.user : info.getUser(),
380 false, useLowResIcon);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700381 application.title = entry.title;
382 application.iconBitmap = entry.icon;
383 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700384 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800385 }
386
Sunny Goyal34b65272015-03-11 16:56:52 -0700387 /**
388 * Returns a high res icon for the given intent and user
389 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700390 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
391 ComponentName component = intent.getComponent();
392 // null info means not installed, but if we have a component from the intent then
393 // we should still look in the cache for restored app icons.
394 if (component == null) {
395 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800396 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700397
398 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700399 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700400 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800401 }
402
Sunny Goyal34942622014-08-29 17:20:55 -0700403 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800404 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
405 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700406 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700407 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700408 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700409 ComponentName component = intent.getComponent();
410 // null info means not installed, but if we have a component from the intent then
411 // we should still look in the cache for restored app icons.
412 if (component == null) {
413 shortcutInfo.setIcon(getDefaultIcon(user));
414 shortcutInfo.title = "";
415 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700416 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700417 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800418 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700419 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700420 }
421 }
422
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800423 /**
424 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
425 */
426 public synchronized void getTitleAndIcon(
427 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700428 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
429 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800430 shortcutInfo.setIcon(entry.icon);
431 shortcutInfo.title = entry.title;
432 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700433 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800434 }
Sunny Goyal34942622014-08-29 17:20:55 -0700435
Sunny Goyal736f5af2014-10-16 14:07:29 -0700436 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100437 if (!mDefaultIcons.containsKey(user)) {
438 mDefaultIcons.put(user, makeDefaultIcon(user));
439 }
440 return mDefaultIcons.get(user);
441 }
442
Kenny Guyed131872014-04-30 03:02:21 +0100443 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
444 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700445 }
446
Sunny Goyal736f5af2014-10-16 14:07:29 -0700447 /**
448 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
449 * This method is not thread safe, it must be called from a synchronized method.
450 */
Kenny Guyed131872014-04-30 03:02:21 +0100451 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700452 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700453 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100454 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700455 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800456 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100457 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500458
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800459 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700460 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800461 if (info != null) {
462 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500463 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700464 if (usePackageIcon) {
465 CacheEntry packageEntry = getEntryForPackage(
466 componentName.getPackageName(), user);
Sunny Goyal34942622014-08-29 17:20:55 -0700467 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700468 if (DEBUG) Log.d(TAG, "using package default icon for " +
469 componentName.toShortString());
470 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700471 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800472 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700473 }
474 }
475 if (entry.icon == null) {
476 if (DEBUG) Log.d(TAG, "using default icon for " +
477 componentName.toShortString());
478 entry.icon = getDefaultIcon(user);
479 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700480 }
481 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800482
483 if (TextUtils.isEmpty(entry.title) && info != null) {
484 entry.title = info.getLabel().toString();
485 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
486 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800487 }
488 return entry;
489 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400490
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700491 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700492 * Adds a default package entry in the cache. This entry is not persisted and will be removed
493 * when the cache is flushed.
494 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700495 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700496 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800497 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700498
Sunny Goyal34942622014-08-29 17:20:55 -0700499 CacheEntry entry = getEntryForPackage(packageName, user);
500 if (!TextUtils.isEmpty(title)) {
501 entry.title = title;
502 }
503 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700504 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700505 }
506 }
507
508 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700509 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700510 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700511 */
512 private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700513 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700514 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700515 CacheEntry entry = mCache.get(cacheKey);
516 if (entry == null) {
517 entry = new CacheEntry();
Sunny Goyal34942622014-08-29 17:20:55 -0700518 entry.title = "";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800519 entry.contentDescription = "";
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700520 mCache.put(cacheKey, entry);
521
522 try {
523 ApplicationInfo info = mPackageManager.getApplicationInfo(packageName, 0);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700524 entry.icon = Utilities.createIconBitmap(info.loadIcon(mPackageManager), mContext);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800525 entry.title = info.loadLabel(mPackageManager);
526 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700527 } catch (NameNotFoundException e) {
528 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
529 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700530 }
531 return entry;
532 }
533
Chris Wren6d0dde02014-02-10 12:16:54 -0500534 /**
535 * Pre-load an icon into the persistent cache.
536 *
537 * <P>Queries for a component that does not exist in the package manager
538 * will be answered by the persistent cache.
539 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500540 * @param componentName the icon should be returned for this component
541 * @param icon the icon to be persisted
542 * @param dpi the native density of the icon
543 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800544 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
545 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500546 // TODO rescale to the correct native DPI
547 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800548 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500549 packageManager.getActivityIcon(componentName);
550 // component is present on the system already, do nothing
551 return;
552 } catch (PackageManager.NameNotFoundException e) {
553 // pass
554 }
555
Sunny Goyal34b65272015-03-11 16:56:52 -0700556 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800557 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
558 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800559 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
560 SQLiteDatabase.CONFLICT_REPLACE);
561 }
562
Sunny Goyal34b65272015-03-11 16:56:52 -0700563 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
564 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800565 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700566 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
567 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800568 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
569 new String[] {component.flattenToString(),
570 Long.toString(mUserManager.getSerialNumberForUser(user))},
571 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500572 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800573 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700574 entry.icon = loadIconNoResize(c, 0);
575 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800576 entry.title = c.getString(1);
577 if (entry.title == null) {
578 entry.title = "";
579 entry.contentDescription = "";
580 } else {
581 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500582 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800583 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500584 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500585 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800586 c.close();
587 }
588 return false;
589 }
590
Sunny Goyal34b65272015-03-11 16:56:52 -0700591 public static class IconLoadRequest {
592 private final Runnable mRunnable;
593 private final Handler mHandler;
594
595 IconLoadRequest(Runnable runnable, Handler handler) {
596 mRunnable = runnable;
597 mHandler = handler;
598 }
599
600 public void cancel() {
601 mHandler.removeCallbacks(mRunnable);
602 }
603 }
604
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800605 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal34b65272015-03-11 16:56:52 -0700606 private final static int DB_VERSION = 2;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800607
608 private final static String TABLE_NAME = "icons";
609 private final static String COLUMN_ROWID = "rowid";
610 private final static String COLUMN_COMPONENT = "componentName";
611 private final static String COLUMN_USER = "profileId";
612 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
613 private final static String COLUMN_VERSION = "version";
614 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700615 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800616 private final static String COLUMN_LABEL = "label";
617
618 public IconDB(Context context) {
619 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
620 }
621
622 @Override
623 public void onCreate(SQLiteDatabase db) {
624 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
625 COLUMN_COMPONENT + " TEXT NOT NULL, " +
626 COLUMN_USER + " INTEGER NOT NULL, " +
627 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
628 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
629 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700630 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800631 COLUMN_LABEL + " TEXT, " +
632 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
633 ");");
634 }
635
636 @Override
637 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
638 if (oldVersion != newVersion) {
639 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500640 }
641 }
642
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800643 @Override
644 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
645 if (oldVersion != newVersion) {
646 clearDB(db);
647 }
Kenny Guyed131872014-04-30 03:02:21 +0100648 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500649
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800650 private void clearDB(SQLiteDatabase db) {
651 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
652 onCreate(db);
653 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700654
655 public ContentValues newContentValues(Bitmap icon, String label) {
656 ContentValues values = new ContentValues();
657 values.put(IconDB.COLUMN_ICON, ItemInfo.flattenBitmap(icon));
658 values.put(IconDB.COLUMN_ICON_LOW_RES, ItemInfo.flattenBitmap(
659 Bitmap.createScaledBitmap(icon,
660 icon.getWidth() / LOW_RES_SCALE_FACTOR,
661 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
662 values.put(IconDB.COLUMN_LABEL, label);
663 return values;
664 }
665 }
666
667 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
668 byte[] data = c.getBlob(iconIndex);
669 try {
670 return BitmapFactory.decodeByteArray(data, 0, data.length);
671 } catch (Exception e) {
672 return null;
673 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500674 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800675}