blob: 39a80be7837574f2c943755d32dbe20f5bcb5853 [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
Sunny Goyal8758ea02015-03-18 10:07:49 -070076 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
77 private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
78
Daniel Sandlercc8befa2013-06-11 14:45:48 -040079 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070080 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010081 private final UserManagerCompat mUserManager;
82 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070083 private final HashMap<ComponentKey, CacheEntry> mCache =
84 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080085 private final int mIconDpi;
86 private final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080087
Sunny Goyal34b65272015-03-11 16:56:52 -070088 private final Handler mWorkerHandler;
89
Daniel Sandlercc8befa2013-06-11 14:45:48 -040090 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -080091 ActivityManager activityManager =
92 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
93
Joe Onorato0589f0f2010-02-08 13:44:00 -080094 mContext = context;
95 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +010096 mUserManager = UserManagerCompat.getInstance(mContext);
97 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Winson Chungd83f5f42012-02-13 14:27:42 -080098 mIconDpi = activityManager.getLauncherLargeIconDensity();
Sunny Goyal4fbc3822015-02-18 16:46:50 -080099 mIconDb = new IconDB(context);
Sunny Goyal34b65272015-03-11 16:56:52 -0700100
101 mWorkerHandler = new Handler(LauncherModel.sWorkerThread.getLooper());
Romain Guya28fd3f2010-03-15 14:44:42 -0700102 }
103
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800104 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700105 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700106 }
107
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700108 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700109 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700110 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700111 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700112 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700113 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700114 }
Michael Jurka721d9722011-08-03 11:49:59 -0700115
116 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700117 }
118
Winson Chung0b9fcf52011-10-31 13:05:15 -0700119 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700120 Resources resources;
121 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700122 resources = mPackageManager.getResourcesForApplication(packageName);
123 } catch (PackageManager.NameNotFoundException e) {
124 resources = null;
125 }
126 if (resources != null) {
127 if (iconId != 0) {
128 return getFullResIcon(resources, iconId);
129 }
130 }
131 return getFullResDefaultActivityIcon();
132 }
133
Sunny Goyalffe83f12014-08-14 17:39:34 -0700134 public int getFullResIconDpi() {
135 return mIconDpi;
136 }
137
Michael Jurkadac85912012-05-18 15:04:49 -0700138 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700139 Resources resources;
140 try {
141 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700142 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700143 } catch (PackageManager.NameNotFoundException e) {
144 resources = null;
145 }
146 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700147 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700148 if (iconId != 0) {
149 return getFullResIcon(resources, iconId);
150 }
151 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500152
Michael Jurkac9a96192010-11-01 11:52:08 -0700153 return getFullResDefaultActivityIcon();
154 }
155
Kenny Guyed131872014-04-30 03:02:21 +0100156 private Bitmap makeDefaultIcon(UserHandleCompat user) {
157 Drawable unbadged = getFullResDefaultActivityIcon();
158 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700159 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
160 Math.max(d.getIntrinsicHeight(), 1),
161 Bitmap.Config.ARGB_8888);
162 Canvas c = new Canvas(b);
163 d.setBounds(0, 0, b.getWidth(), b.getHeight());
164 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700165 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700166 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800167 }
168
169 /**
170 * Remove any records for the supplied ComponentName.
171 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700172 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700173 mCache.remove(new ComponentKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800174 }
175
176 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800177 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500178 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800179 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700180 HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
181 for (ComponentKey key: mCache.keySet()) {
Kenny Guyed131872014-04-30 03:02:21 +0100182 if (key.componentName.getPackageName().equals(packageName)
183 && key.user.equals(user)) {
184 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500185 }
186 }
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700187 for (ComponentKey condemned: forDeletion) {
Kenny Guyed131872014-04-30 03:02:21 +0100188 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500189 }
190 }
191
192 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800193 * Updates the entries related to the given package in memory and persistent DB.
194 */
195 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
196 removeIconsForPkg(packageName, user);
197 try {
198 PackageInfo info = mPackageManager.getPackageInfo(packageName,
199 PackageManager.GET_UNINSTALLED_PACKAGES);
200 long userSerial = mUserManager.getSerialNumberForUser(user);
201 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
202 addIconToDB(app, info, userSerial);
203 }
204 } catch (NameNotFoundException e) {
205 Log.d(TAG, "Package not found", e);
206 return;
207 }
208 }
209
210 /**
211 * Removes the entries related to the given package in memory and persistent DB.
212 */
213 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
214 removeFromMemCacheLocked(packageName, user);
215 long userSerial = mUserManager.getSerialNumberForUser(user);
216 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
217 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
218 new String[] {packageName + "/%", Long.toString(userSerial)});
219 }
220
221 /**
222 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
223 * the DB and are updated.
224 * @return The set of packages for which icons have updated.
225 */
226 public HashSet<String> updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
227 long userSerial = mUserManager.getSerialNumberForUser(user);
228 PackageManager pm = mContext.getPackageManager();
229 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
230 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
231 pkgInfoMap.put(info.packageName, info);
232 }
233
234 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
235 for (LauncherActivityInfoCompat app : apps) {
236 componentMap.put(app.getComponentName(), app);
237 }
238
239 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
240 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
241 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION},
242 IconDB.COLUMN_USER + " = ? ",
243 new String[] {Long.toString(userSerial)},
244 null, null, null);
245
246 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
247 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
248 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
249 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
250
251 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
252 HashSet<String> updatedPackages = new HashSet<String>();
253
254 while (c.moveToNext()) {
255 String cn = c.getString(indexComponent);
256 ComponentName component = ComponentName.unflattenFromString(cn);
257 PackageInfo info = pkgInfoMap.get(component.getPackageName());
258 if (info == null) {
259 itemsToRemove.add(c.getInt(rowIndex));
260 continue;
261 }
262 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
263 // Application is not present
264 continue;
265 }
266
267 long updateTime = c.getLong(indexLastUpdate);
268 int version = c.getInt(indexVersion);
269 LauncherActivityInfoCompat app = componentMap.remove(component);
270 if (version == info.versionCode && updateTime == info.lastUpdateTime) {
271 continue;
272 }
273 if (app == null) {
274 itemsToRemove.add(c.getInt(rowIndex));
275 continue;
276 }
277 ContentValues values = updateCacheAndGetContentValues(app);
278 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
279 IconDB.COLUMN_COMPONENT + " = ?",
280 new String[] { cn });
281
282 updatedPackages.add(component.getPackageName());
283 }
284 c.close();
285 if (!itemsToRemove.isEmpty()) {
286 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
287 IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )",
288 null);
289 }
290
291 // Insert remaining apps.
292 for (LauncherActivityInfoCompat app : componentMap.values()) {
293 PackageInfo info = pkgInfoMap.get(app.getComponentName().getPackageName());
294 if (info == null) {
295 continue;
296 }
297 addIconToDB(app, info, userSerial);
298 }
299 return updatedPackages;
300 }
301
302 private void addIconToDB(LauncherActivityInfoCompat app, PackageInfo info, long userSerial) {
303 ContentValues values = updateCacheAndGetContentValues(app);
304 values.put(IconDB.COLUMN_COMPONENT, app.getComponentName().flattenToString());
305 values.put(IconDB.COLUMN_USER, userSerial);
306 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
307 values.put(IconDB.COLUMN_VERSION, info.versionCode);
308 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
309 SQLiteDatabase.CONFLICT_REPLACE);
310 }
311
312 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app) {
313 CacheEntry entry = new CacheEntry();
314 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
315 entry.title = app.getLabel();
316 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700317 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800318
Sunny Goyal34b65272015-03-11 16:56:52 -0700319 return mIconDb.newContentValues(entry.icon, entry.title.toString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800320 }
321
322
323 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800324 * Empty out the cache.
325 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700326 public synchronized void flush() {
327 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800328 }
329
330 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700331 * Empty out the cache that aren't of the correct grid size
332 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700333 public synchronized void flushInvalidIcons(DeviceProfile grid) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700334 Iterator<Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator();
Sunny Goyal736f5af2014-10-16 14:07:29 -0700335 while (it.hasNext()) {
336 final CacheEntry e = it.next().getValue();
337 if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
338 || e.icon.getHeight() < grid.iconSizePx)) {
339 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700340 }
341 }
342 }
343
344 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700345 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
346 * @return a request ID that can be used to cancel the request.
347 */
348 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
349 Runnable request = new Runnable() {
350
351 @Override
352 public void run() {
353 if (info instanceof AppInfo) {
354 getTitleAndIcon((AppInfo) info, null, false);
355 } else if (info instanceof ShortcutInfo) {
356 ShortcutInfo st = (ShortcutInfo) info;
357 getTitleAndIcon(st,
358 st.promisedIntent != null ? st.promisedIntent : st.intent,
359 st.user, false);
360 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700361 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700362
363 @Override
364 public void run() {
365 caller.reapplyItemInfo(info);
366 }
367 });
368 }
369 };
370 mWorkerHandler.post(request);
371 return new IconLoadRequest(request, mWorkerHandler);
372 }
373
374 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800375 * Fill in "application" with the icon and label for "info."
376 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700377 public synchronized void getTitleAndIcon(AppInfo application,
378 LauncherActivityInfoCompat info, boolean useLowResIcon) {
379 CacheEntry entry = cacheLocked(application.componentName, info,
380 info == null ? application.user : info.getUser(),
381 false, useLowResIcon);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700382 application.title = entry.title;
383 application.iconBitmap = entry.icon;
384 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700385 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800386 }
387
Sunny Goyal34b65272015-03-11 16:56:52 -0700388 /**
389 * Returns a high res icon for the given intent and user
390 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700391 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
392 ComponentName component = intent.getComponent();
393 // null info means not installed, but if we have a component from the intent then
394 // we should still look in the cache for restored app icons.
395 if (component == null) {
396 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800397 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700398
399 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700400 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700401 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800402 }
403
Sunny Goyal34942622014-08-29 17:20:55 -0700404 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800405 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
406 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700407 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700408 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700409 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700410 ComponentName component = intent.getComponent();
411 // null info means not installed, but if we have a component from the intent then
412 // we should still look in the cache for restored app icons.
413 if (component == null) {
414 shortcutInfo.setIcon(getDefaultIcon(user));
415 shortcutInfo.title = "";
416 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700417 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700418 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800419 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700420 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700421 }
422 }
423
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800424 /**
425 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
426 */
427 public synchronized void getTitleAndIcon(
428 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700429 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
430 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800431 shortcutInfo.setIcon(entry.icon);
432 shortcutInfo.title = entry.title;
433 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700434 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800435 }
Sunny Goyal34942622014-08-29 17:20:55 -0700436
Sunny Goyal736f5af2014-10-16 14:07:29 -0700437 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100438 if (!mDefaultIcons.containsKey(user)) {
439 mDefaultIcons.put(user, makeDefaultIcon(user));
440 }
441 return mDefaultIcons.get(user);
442 }
443
Kenny Guyed131872014-04-30 03:02:21 +0100444 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
445 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700446 }
447
Sunny Goyal736f5af2014-10-16 14:07:29 -0700448 /**
449 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
450 * This method is not thread safe, it must be called from a synchronized method.
451 */
Kenny Guyed131872014-04-30 03:02:21 +0100452 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700453 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700454 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100455 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700456 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800457 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100458 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500459
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800460 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700461 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800462 if (info != null) {
463 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500464 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700465 if (usePackageIcon) {
466 CacheEntry packageEntry = getEntryForPackage(
467 componentName.getPackageName(), user);
Sunny Goyal34942622014-08-29 17:20:55 -0700468 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700469 if (DEBUG) Log.d(TAG, "using package default icon for " +
470 componentName.toShortString());
471 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700472 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800473 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700474 }
475 }
476 if (entry.icon == null) {
477 if (DEBUG) Log.d(TAG, "using default icon for " +
478 componentName.toShortString());
479 entry.icon = getDefaultIcon(user);
480 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700481 }
482 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800483
484 if (TextUtils.isEmpty(entry.title) && info != null) {
485 entry.title = info.getLabel().toString();
486 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
487 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800488 }
489 return entry;
490 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400491
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700492 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700493 * Adds a default package entry in the cache. This entry is not persisted and will be removed
494 * when the cache is flushed.
495 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700496 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700497 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800498 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700499
Sunny Goyal34942622014-08-29 17:20:55 -0700500 CacheEntry entry = getEntryForPackage(packageName, user);
501 if (!TextUtils.isEmpty(title)) {
502 entry.title = title;
503 }
504 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700505 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700506 }
507 }
508
509 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700510 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700511 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700512 */
513 private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700514 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700515 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700516 CacheEntry entry = mCache.get(cacheKey);
517 if (entry == null) {
518 entry = new CacheEntry();
Sunny Goyal34942622014-08-29 17:20:55 -0700519 entry.title = "";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800520 entry.contentDescription = "";
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700521 mCache.put(cacheKey, entry);
522
523 try {
524 ApplicationInfo info = mPackageManager.getApplicationInfo(packageName, 0);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700525 entry.icon = Utilities.createIconBitmap(info.loadIcon(mPackageManager), mContext);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800526 entry.title = info.loadLabel(mPackageManager);
527 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700528 } catch (NameNotFoundException e) {
529 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
530 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700531 }
532 return entry;
533 }
534
Chris Wren6d0dde02014-02-10 12:16:54 -0500535 /**
536 * Pre-load an icon into the persistent cache.
537 *
538 * <P>Queries for a component that does not exist in the package manager
539 * will be answered by the persistent cache.
540 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500541 * @param componentName the icon should be returned for this component
542 * @param icon the icon to be persisted
543 * @param dpi the native density of the icon
544 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800545 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
546 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500547 // TODO rescale to the correct native DPI
548 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800549 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500550 packageManager.getActivityIcon(componentName);
551 // component is present on the system already, do nothing
552 return;
553 } catch (PackageManager.NameNotFoundException e) {
554 // pass
555 }
556
Sunny Goyal34b65272015-03-11 16:56:52 -0700557 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800558 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
559 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800560 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
561 SQLiteDatabase.CONFLICT_REPLACE);
562 }
563
Sunny Goyal34b65272015-03-11 16:56:52 -0700564 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
565 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800566 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700567 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
568 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800569 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
570 new String[] {component.flattenToString(),
571 Long.toString(mUserManager.getSerialNumberForUser(user))},
572 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500573 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800574 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700575 entry.icon = loadIconNoResize(c, 0);
576 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800577 entry.title = c.getString(1);
578 if (entry.title == null) {
579 entry.title = "";
580 entry.contentDescription = "";
581 } else {
582 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500583 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800584 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500585 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500586 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800587 c.close();
588 }
589 return false;
590 }
591
Sunny Goyal34b65272015-03-11 16:56:52 -0700592 public static class IconLoadRequest {
593 private final Runnable mRunnable;
594 private final Handler mHandler;
595
596 IconLoadRequest(Runnable runnable, Handler handler) {
597 mRunnable = runnable;
598 mHandler = handler;
599 }
600
601 public void cancel() {
602 mHandler.removeCallbacks(mRunnable);
603 }
604 }
605
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800606 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal34b65272015-03-11 16:56:52 -0700607 private final static int DB_VERSION = 2;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800608
609 private final static String TABLE_NAME = "icons";
610 private final static String COLUMN_ROWID = "rowid";
611 private final static String COLUMN_COMPONENT = "componentName";
612 private final static String COLUMN_USER = "profileId";
613 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
614 private final static String COLUMN_VERSION = "version";
615 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700616 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800617 private final static String COLUMN_LABEL = "label";
618
619 public IconDB(Context context) {
620 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
621 }
622
623 @Override
624 public void onCreate(SQLiteDatabase db) {
625 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
626 COLUMN_COMPONENT + " TEXT NOT NULL, " +
627 COLUMN_USER + " INTEGER NOT NULL, " +
628 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
629 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
630 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700631 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800632 COLUMN_LABEL + " TEXT, " +
633 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
634 ");");
635 }
636
637 @Override
638 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
639 if (oldVersion != newVersion) {
640 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500641 }
642 }
643
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800644 @Override
645 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
646 if (oldVersion != newVersion) {
647 clearDB(db);
648 }
Kenny Guyed131872014-04-30 03:02:21 +0100649 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500650
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800651 private void clearDB(SQLiteDatabase db) {
652 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
653 onCreate(db);
654 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700655
656 public ContentValues newContentValues(Bitmap icon, String label) {
657 ContentValues values = new ContentValues();
658 values.put(IconDB.COLUMN_ICON, ItemInfo.flattenBitmap(icon));
659 values.put(IconDB.COLUMN_ICON_LOW_RES, ItemInfo.flattenBitmap(
660 Bitmap.createScaledBitmap(icon,
661 icon.getWidth() / LOW_RES_SCALE_FACTOR,
662 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
663 values.put(IconDB.COLUMN_LABEL, label);
664 return values;
665 }
666 }
667
668 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
669 byte[] data = c.getBlob(iconIndex);
670 try {
671 return BitmapFactory.decodeByteArray(data, 0, data.length);
672 } catch (Exception e) {
673 return null;
674 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500675 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800676}