blob: aaec2b40af53fdfcf91221e22b93fe1014d823de [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;
Adam Cohen091440a2015-03-18 14:16:05 -070046import com.android.launcher3.util.Thunk;
Kenny Guyed131872014-04-30 03:02:21 +010047
Joe Onorato0589f0f2010-02-08 13:44:00 -080048import java.util.HashMap;
Chris Wren6d0dde02014-02-10 12:16:54 -050049import java.util.HashSet;
Adam Cohenb6d33df2013-10-15 10:18:02 -070050import java.util.Iterator;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080051import java.util.List;
Adam Cohenb6d33df2013-10-15 10:18:02 -070052import java.util.Map.Entry;
Joe Onorato0589f0f2010-02-08 13:44:00 -080053
54/**
55 * Cache of application icons. Icons can be made from any thread.
56 */
57public class IconCache {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070058
Joe Onorato0589f0f2010-02-08 13:44:00 -080059 private static final String TAG = "Launcher.IconCache";
60
61 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050062
Sunny Goyal0fc1be12014-08-11 17:05:23 -070063 // Empty class name is used for storing package default entry.
64 private static final String EMPTY_CLASS_NAME = ".";
65
Sunny Goyalbbef77d2014-09-09 16:27:55 -070066 private static final boolean DEBUG = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -080067
Sunny Goyal34b65272015-03-11 16:56:52 -070068 private static final int LOW_RES_SCALE_FACTOR = 8;
69
Adam Cohen091440a2015-03-18 14:16:05 -070070 @Thunk static class CacheEntry {
Joe Onorato0589f0f2010-02-08 13:44:00 -080071 public Bitmap icon;
Kenny Guyd6fe5262014-07-21 17:11:41 +010072 public CharSequence title;
73 public CharSequence contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -070074 public boolean isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -080075 }
76
Sunny Goyal8758ea02015-03-18 10:07:49 -070077 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
78 private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
79
Daniel Sandlercc8befa2013-06-11 14:45:48 -040080 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070081 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010082 private final UserManagerCompat mUserManager;
83 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070084 private final HashMap<ComponentKey, CacheEntry> mCache =
85 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080086 private final int mIconDpi;
87 private final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080088
Sunny Goyal34b65272015-03-11 16:56:52 -070089 private final Handler mWorkerHandler;
90
Daniel Sandlercc8befa2013-06-11 14:45:48 -040091 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -080092 ActivityManager activityManager =
93 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
94
Joe Onorato0589f0f2010-02-08 13:44:00 -080095 mContext = context;
96 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +010097 mUserManager = UserManagerCompat.getInstance(mContext);
98 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Winson Chungd83f5f42012-02-13 14:27:42 -080099 mIconDpi = activityManager.getLauncherLargeIconDensity();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800100 mIconDb = new IconDB(context);
Sunny Goyal34b65272015-03-11 16:56:52 -0700101
102 mWorkerHandler = new Handler(LauncherModel.sWorkerThread.getLooper());
Romain Guya28fd3f2010-03-15 14:44:42 -0700103 }
104
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800105 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700106 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700107 }
108
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700109 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700110 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700111 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700112 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700113 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700114 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700115 }
Michael Jurka721d9722011-08-03 11:49:59 -0700116
117 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700118 }
119
Winson Chung0b9fcf52011-10-31 13:05:15 -0700120 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700121 Resources resources;
122 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700123 resources = mPackageManager.getResourcesForApplication(packageName);
124 } catch (PackageManager.NameNotFoundException e) {
125 resources = null;
126 }
127 if (resources != null) {
128 if (iconId != 0) {
129 return getFullResIcon(resources, iconId);
130 }
131 }
132 return getFullResDefaultActivityIcon();
133 }
134
Sunny Goyalffe83f12014-08-14 17:39:34 -0700135 public int getFullResIconDpi() {
136 return mIconDpi;
137 }
138
Michael Jurkadac85912012-05-18 15:04:49 -0700139 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700140 Resources resources;
141 try {
142 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700143 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700144 } catch (PackageManager.NameNotFoundException e) {
145 resources = null;
146 }
147 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700148 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700149 if (iconId != 0) {
150 return getFullResIcon(resources, iconId);
151 }
152 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500153
Michael Jurkac9a96192010-11-01 11:52:08 -0700154 return getFullResDefaultActivityIcon();
155 }
156
Kenny Guyed131872014-04-30 03:02:21 +0100157 private Bitmap makeDefaultIcon(UserHandleCompat user) {
158 Drawable unbadged = getFullResDefaultActivityIcon();
159 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700160 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
161 Math.max(d.getIntrinsicHeight(), 1),
162 Bitmap.Config.ARGB_8888);
163 Canvas c = new Canvas(b);
164 d.setBounds(0, 0, b.getWidth(), b.getHeight());
165 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700166 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700167 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800168 }
169
170 /**
171 * Remove any records for the supplied ComponentName.
172 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700173 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700174 mCache.remove(new ComponentKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800175 }
176
177 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800178 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500179 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800180 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700181 HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
182 for (ComponentKey key: mCache.keySet()) {
Kenny Guyed131872014-04-30 03:02:21 +0100183 if (key.componentName.getPackageName().equals(packageName)
184 && key.user.equals(user)) {
185 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500186 }
187 }
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700188 for (ComponentKey condemned: forDeletion) {
Kenny Guyed131872014-04-30 03:02:21 +0100189 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500190 }
191 }
192
193 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800194 * Updates the entries related to the given package in memory and persistent DB.
195 */
196 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
197 removeIconsForPkg(packageName, user);
198 try {
199 PackageInfo info = mPackageManager.getPackageInfo(packageName,
200 PackageManager.GET_UNINSTALLED_PACKAGES);
201 long userSerial = mUserManager.getSerialNumberForUser(user);
202 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700203 addIconToDBAndMemCache(app, info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800204 }
205 } catch (NameNotFoundException e) {
206 Log.d(TAG, "Package not found", e);
207 return;
208 }
209 }
210
211 /**
212 * Removes the entries related to the given package in memory and persistent DB.
213 */
214 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
215 removeFromMemCacheLocked(packageName, user);
216 long userSerial = mUserManager.getSerialNumberForUser(user);
217 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
218 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
219 new String[] {packageName + "/%", Long.toString(userSerial)});
220 }
221
222 /**
223 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
224 * the DB and are updated.
225 * @return The set of packages for which icons have updated.
226 */
227 public HashSet<String> updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
228 long userSerial = mUserManager.getSerialNumberForUser(user);
229 PackageManager pm = mContext.getPackageManager();
230 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
231 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
232 pkgInfoMap.put(info.packageName, info);
233 }
234
235 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
236 for (LauncherActivityInfoCompat app : apps) {
237 componentMap.put(app.getComponentName(), app);
238 }
239
240 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
241 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
242 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION},
243 IconDB.COLUMN_USER + " = ? ",
244 new String[] {Long.toString(userSerial)},
245 null, null, null);
246
247 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
248 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
249 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
250 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
251
252 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
253 HashSet<String> updatedPackages = new HashSet<String>();
254
255 while (c.moveToNext()) {
256 String cn = c.getString(indexComponent);
257 ComponentName component = ComponentName.unflattenFromString(cn);
258 PackageInfo info = pkgInfoMap.get(component.getPackageName());
259 if (info == null) {
260 itemsToRemove.add(c.getInt(rowIndex));
261 continue;
262 }
263 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
264 // Application is not present
265 continue;
266 }
267
268 long updateTime = c.getLong(indexLastUpdate);
269 int version = c.getInt(indexVersion);
270 LauncherActivityInfoCompat app = componentMap.remove(component);
271 if (version == info.versionCode && updateTime == info.lastUpdateTime) {
272 continue;
273 }
274 if (app == null) {
275 itemsToRemove.add(c.getInt(rowIndex));
276 continue;
277 }
278 ContentValues values = updateCacheAndGetContentValues(app);
279 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
280 IconDB.COLUMN_COMPONENT + " = ?",
281 new String[] { cn });
282
283 updatedPackages.add(component.getPackageName());
284 }
285 c.close();
286 if (!itemsToRemove.isEmpty()) {
287 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
288 IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )",
289 null);
290 }
291
292 // Insert remaining apps.
293 for (LauncherActivityInfoCompat app : componentMap.values()) {
294 PackageInfo info = pkgInfoMap.get(app.getComponentName().getPackageName());
295 if (info == null) {
296 continue;
297 }
Sunny Goyald180cf72015-04-06 12:45:40 -0700298 addIconToDBAndMemCache(app, info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800299 }
300 return updatedPackages;
301 }
302
Sunny Goyald180cf72015-04-06 12:45:40 -0700303 private void addIconToDBAndMemCache(LauncherActivityInfoCompat app, PackageInfo info,
304 long userSerial) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800305 ContentValues values = updateCacheAndGetContentValues(app);
Sunny Goyald180cf72015-04-06 12:45:40 -0700306 addIconToDB(values, app.getComponentName(), info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800307 values.put(IconDB.COLUMN_COMPONENT, app.getComponentName().flattenToString());
Sunny Goyald180cf72015-04-06 12:45:40 -0700308 }
309
310 /**
311 * Updates {@param values} to contain versoning information and adds it to the DB.
312 * @param values {@link ContentValues} containing icon & title
313 */
314 private void addIconToDB(ContentValues values, ComponentName key,
315 PackageInfo info, long userSerial) {
316 values.put(IconDB.COLUMN_COMPONENT, key.flattenToString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800317 values.put(IconDB.COLUMN_USER, userSerial);
318 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
319 values.put(IconDB.COLUMN_VERSION, info.versionCode);
320 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
321 SQLiteDatabase.CONFLICT_REPLACE);
322 }
323
324 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app) {
325 CacheEntry entry = new CacheEntry();
326 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
327 entry.title = app.getLabel();
328 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700329 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800330
Sunny Goyal34b65272015-03-11 16:56:52 -0700331 return mIconDb.newContentValues(entry.icon, entry.title.toString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800332 }
333
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800334 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800335 * Empty out the cache.
336 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700337 public synchronized void flush() {
338 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800339 }
340
341 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700342 * Empty out the cache that aren't of the correct grid size
343 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700344 public synchronized void flushInvalidIcons(DeviceProfile grid) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700345 Iterator<Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator();
Sunny Goyal736f5af2014-10-16 14:07:29 -0700346 while (it.hasNext()) {
347 final CacheEntry e = it.next().getValue();
348 if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
349 || e.icon.getHeight() < grid.iconSizePx)) {
350 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700351 }
352 }
353 }
354
355 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700356 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
357 * @return a request ID that can be used to cancel the request.
358 */
359 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
360 Runnable request = new Runnable() {
361
362 @Override
363 public void run() {
364 if (info instanceof AppInfo) {
365 getTitleAndIcon((AppInfo) info, null, false);
366 } else if (info instanceof ShortcutInfo) {
367 ShortcutInfo st = (ShortcutInfo) info;
368 getTitleAndIcon(st,
369 st.promisedIntent != null ? st.promisedIntent : st.intent,
370 st.user, false);
371 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700372 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700373
374 @Override
375 public void run() {
376 caller.reapplyItemInfo(info);
377 }
378 });
379 }
380 };
381 mWorkerHandler.post(request);
382 return new IconLoadRequest(request, mWorkerHandler);
383 }
384
385 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800386 * Fill in "application" with the icon and label for "info."
387 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700388 public synchronized void getTitleAndIcon(AppInfo application,
389 LauncherActivityInfoCompat info, boolean useLowResIcon) {
390 CacheEntry entry = cacheLocked(application.componentName, info,
391 info == null ? application.user : info.getUser(),
392 false, useLowResIcon);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700393 application.title = entry.title;
394 application.iconBitmap = entry.icon;
395 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700396 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800397 }
398
Sunny Goyal34b65272015-03-11 16:56:52 -0700399 /**
400 * Returns a high res icon for the given intent and user
401 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700402 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
403 ComponentName component = intent.getComponent();
404 // null info means not installed, but if we have a component from the intent then
405 // we should still look in the cache for restored app icons.
406 if (component == null) {
407 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800408 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700409
410 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700411 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700412 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800413 }
414
Sunny Goyal34942622014-08-29 17:20:55 -0700415 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800416 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
417 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700418 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700419 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700420 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700421 ComponentName component = intent.getComponent();
422 // null info means not installed, but if we have a component from the intent then
423 // we should still look in the cache for restored app icons.
424 if (component == null) {
425 shortcutInfo.setIcon(getDefaultIcon(user));
426 shortcutInfo.title = "";
427 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700428 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700429 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800430 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700431 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700432 }
433 }
434
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800435 /**
436 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
437 */
438 public synchronized void getTitleAndIcon(
439 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700440 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
441 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800442 shortcutInfo.setIcon(entry.icon);
443 shortcutInfo.title = entry.title;
444 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700445 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800446 }
Sunny Goyal34942622014-08-29 17:20:55 -0700447
Sunny Goyald180cf72015-04-06 12:45:40 -0700448 /**
449 * Fill in {@param appInfo} with the icon and label for {@param packageName}
450 */
451 public synchronized void getTitleAndIconForApp(
452 String packageName, UserHandleCompat user, boolean useLowResIcon, AppInfo appInfoOut) {
453 CacheEntry entry = getEntryForPackageLocked(packageName, user, useLowResIcon);
454 appInfoOut.iconBitmap = entry.icon;
455 appInfoOut.title = entry.title;
456 appInfoOut.usingLowResIcon = entry.isLowResIcon;
457 appInfoOut.contentDescription = entry.contentDescription;
458 }
459
Sunny Goyal736f5af2014-10-16 14:07:29 -0700460 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100461 if (!mDefaultIcons.containsKey(user)) {
462 mDefaultIcons.put(user, makeDefaultIcon(user));
463 }
464 return mDefaultIcons.get(user);
465 }
466
Kenny Guyed131872014-04-30 03:02:21 +0100467 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
468 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700469 }
470
Sunny Goyal736f5af2014-10-16 14:07:29 -0700471 /**
472 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
473 * This method is not thread safe, it must be called from a synchronized method.
474 */
Kenny Guyed131872014-04-30 03:02:21 +0100475 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700476 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700477 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100478 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700479 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800480 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100481 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500482
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800483 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700484 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800485 if (info != null) {
486 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500487 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700488 if (usePackageIcon) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700489 CacheEntry packageEntry = getEntryForPackageLocked(
490 componentName.getPackageName(), user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700491 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700492 if (DEBUG) Log.d(TAG, "using package default icon for " +
493 componentName.toShortString());
494 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700495 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800496 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700497 }
498 }
499 if (entry.icon == null) {
500 if (DEBUG) Log.d(TAG, "using default icon for " +
501 componentName.toShortString());
502 entry.icon = getDefaultIcon(user);
503 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700504 }
505 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800506
507 if (TextUtils.isEmpty(entry.title) && info != null) {
508 entry.title = info.getLabel().toString();
509 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
510 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800511 }
512 return entry;
513 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400514
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700515 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700516 * Adds a default package entry in the cache. This entry is not persisted and will be removed
517 * when the cache is flushed.
518 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700519 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700520 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800521 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700522
Sunny Goyald180cf72015-04-06 12:45:40 -0700523 CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700524 if (!TextUtils.isEmpty(title)) {
525 entry.title = title;
526 }
527 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700528 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700529 }
530 }
531
532 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700533 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700534 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyald180cf72015-04-06 12:45:40 -0700535 *
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700536 */
Sunny Goyald180cf72015-04-06 12:45:40 -0700537 private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
538 boolean useLowResIcon) {
539 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700540 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700541 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyald180cf72015-04-06 12:45:40 -0700542 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700543 entry = new CacheEntry();
544 mCache.put(cacheKey, entry);
545
Sunny Goyald180cf72015-04-06 12:45:40 -0700546 // Check the DB first.
547 if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
548 try {
549 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
550 ApplicationInfo appInfo = info.applicationInfo;
551 if (appInfo == null) {
552 throw new NameNotFoundException("ApplicationInfo is null");
553 }
554 Drawable drawable = mUserManager.getBadgedDrawableForUser(
555 appInfo.loadIcon(mPackageManager), user);
556 entry.icon = Utilities.createIconBitmap(drawable, mContext);
557 entry.title = appInfo.loadLabel(mPackageManager);
558 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
559 entry.isLowResIcon = false;
560
561 // Add the icon in the DB here, since these do not get written during
562 // package updates.
563 ContentValues values =
564 mIconDb.newContentValues(entry.icon, entry.title.toString());
565 addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
566
567 } catch (NameNotFoundException e) {
568 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
569 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700570 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700571 }
572 return entry;
573 }
574
Chris Wren6d0dde02014-02-10 12:16:54 -0500575 /**
576 * Pre-load an icon into the persistent cache.
577 *
578 * <P>Queries for a component that does not exist in the package manager
579 * will be answered by the persistent cache.
580 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500581 * @param componentName the icon should be returned for this component
582 * @param icon the icon to be persisted
583 * @param dpi the native density of the icon
584 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800585 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
586 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500587 // TODO rescale to the correct native DPI
588 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800589 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500590 packageManager.getActivityIcon(componentName);
591 // component is present on the system already, do nothing
592 return;
593 } catch (PackageManager.NameNotFoundException e) {
594 // pass
595 }
596
Sunny Goyal34b65272015-03-11 16:56:52 -0700597 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800598 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
599 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800600 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
601 SQLiteDatabase.CONFLICT_REPLACE);
602 }
603
Sunny Goyal34b65272015-03-11 16:56:52 -0700604 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
605 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800606 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700607 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
608 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800609 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
610 new String[] {component.flattenToString(),
611 Long.toString(mUserManager.getSerialNumberForUser(user))},
612 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500613 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800614 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700615 entry.icon = loadIconNoResize(c, 0);
616 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800617 entry.title = c.getString(1);
618 if (entry.title == null) {
619 entry.title = "";
620 entry.contentDescription = "";
621 } else {
622 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500623 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800624 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500625 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500626 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800627 c.close();
628 }
629 return false;
630 }
631
Sunny Goyal34b65272015-03-11 16:56:52 -0700632 public static class IconLoadRequest {
633 private final Runnable mRunnable;
634 private final Handler mHandler;
635
636 IconLoadRequest(Runnable runnable, Handler handler) {
637 mRunnable = runnable;
638 mHandler = handler;
639 }
640
641 public void cancel() {
642 mHandler.removeCallbacks(mRunnable);
643 }
644 }
645
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800646 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal34b65272015-03-11 16:56:52 -0700647 private final static int DB_VERSION = 2;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800648
649 private final static String TABLE_NAME = "icons";
650 private final static String COLUMN_ROWID = "rowid";
651 private final static String COLUMN_COMPONENT = "componentName";
652 private final static String COLUMN_USER = "profileId";
653 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
654 private final static String COLUMN_VERSION = "version";
655 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700656 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800657 private final static String COLUMN_LABEL = "label";
658
659 public IconDB(Context context) {
660 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
661 }
662
663 @Override
664 public void onCreate(SQLiteDatabase db) {
665 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
666 COLUMN_COMPONENT + " TEXT NOT NULL, " +
667 COLUMN_USER + " INTEGER NOT NULL, " +
668 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
669 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
670 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700671 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800672 COLUMN_LABEL + " TEXT, " +
673 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
674 ");");
675 }
676
677 @Override
678 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
679 if (oldVersion != newVersion) {
680 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500681 }
682 }
683
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800684 @Override
685 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
686 if (oldVersion != newVersion) {
687 clearDB(db);
688 }
Kenny Guyed131872014-04-30 03:02:21 +0100689 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500690
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800691 private void clearDB(SQLiteDatabase db) {
692 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
693 onCreate(db);
694 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700695
696 public ContentValues newContentValues(Bitmap icon, String label) {
697 ContentValues values = new ContentValues();
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700698 values.put(IconDB.COLUMN_ICON, Utilities.flattenBitmap(icon));
699 values.put(IconDB.COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700700 Bitmap.createScaledBitmap(icon,
701 icon.getWidth() / LOW_RES_SCALE_FACTOR,
702 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
703 values.put(IconDB.COLUMN_LABEL, label);
704 return values;
705 }
706 }
707
708 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
709 byte[] data = c.getBlob(iconIndex);
710 try {
711 return BitmapFactory.decodeByteArray(data, 0, data.length);
712 } catch (Exception e) {
713 return null;
714 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500715 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800716}