blob: 9b2119e0b9c6d8704a10333f330e1712a2861d1d [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)) {
203 addIconToDB(app, info, userSerial);
204 }
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 }
298 addIconToDB(app, info, userSerial);
299 }
300 return updatedPackages;
301 }
302
303 private void addIconToDB(LauncherActivityInfoCompat app, PackageInfo info, long userSerial) {
304 ContentValues values = updateCacheAndGetContentValues(app);
305 values.put(IconDB.COLUMN_COMPONENT, app.getComponentName().flattenToString());
306 values.put(IconDB.COLUMN_USER, userSerial);
307 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
308 values.put(IconDB.COLUMN_VERSION, info.versionCode);
309 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
310 SQLiteDatabase.CONFLICT_REPLACE);
311 }
312
313 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app) {
314 CacheEntry entry = new CacheEntry();
315 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
316 entry.title = app.getLabel();
317 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700318 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800319
Sunny Goyal34b65272015-03-11 16:56:52 -0700320 return mIconDb.newContentValues(entry.icon, entry.title.toString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800321 }
322
323
324 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800325 * Empty out the cache.
326 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700327 public synchronized void flush() {
328 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800329 }
330
331 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700332 * Empty out the cache that aren't of the correct grid size
333 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700334 public synchronized void flushInvalidIcons(DeviceProfile grid) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700335 Iterator<Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator();
Sunny Goyal736f5af2014-10-16 14:07:29 -0700336 while (it.hasNext()) {
337 final CacheEntry e = it.next().getValue();
338 if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
339 || e.icon.getHeight() < grid.iconSizePx)) {
340 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700341 }
342 }
343 }
344
345 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700346 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
347 * @return a request ID that can be used to cancel the request.
348 */
349 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
350 Runnable request = new Runnable() {
351
352 @Override
353 public void run() {
354 if (info instanceof AppInfo) {
355 getTitleAndIcon((AppInfo) info, null, false);
356 } else if (info instanceof ShortcutInfo) {
357 ShortcutInfo st = (ShortcutInfo) info;
358 getTitleAndIcon(st,
359 st.promisedIntent != null ? st.promisedIntent : st.intent,
360 st.user, false);
361 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700362 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700363
364 @Override
365 public void run() {
366 caller.reapplyItemInfo(info);
367 }
368 });
369 }
370 };
371 mWorkerHandler.post(request);
372 return new IconLoadRequest(request, mWorkerHandler);
373 }
374
375 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800376 * Fill in "application" with the icon and label for "info."
377 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700378 public synchronized void getTitleAndIcon(AppInfo application,
379 LauncherActivityInfoCompat info, boolean useLowResIcon) {
380 CacheEntry entry = cacheLocked(application.componentName, info,
381 info == null ? application.user : info.getUser(),
382 false, useLowResIcon);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700383 application.title = entry.title;
384 application.iconBitmap = entry.icon;
385 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700386 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800387 }
388
Sunny Goyal34b65272015-03-11 16:56:52 -0700389 /**
390 * Returns a high res icon for the given intent and user
391 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700392 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
393 ComponentName component = intent.getComponent();
394 // null info means not installed, but if we have a component from the intent then
395 // we should still look in the cache for restored app icons.
396 if (component == null) {
397 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800398 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700399
400 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700401 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700402 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800403 }
404
Sunny Goyal34942622014-08-29 17:20:55 -0700405 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800406 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
407 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700408 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700409 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700410 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700411 ComponentName component = intent.getComponent();
412 // null info means not installed, but if we have a component from the intent then
413 // we should still look in the cache for restored app icons.
414 if (component == null) {
415 shortcutInfo.setIcon(getDefaultIcon(user));
416 shortcutInfo.title = "";
417 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700418 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700419 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800420 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700421 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700422 }
423 }
424
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800425 /**
426 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
427 */
428 public synchronized void getTitleAndIcon(
429 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700430 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
431 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800432 shortcutInfo.setIcon(entry.icon);
433 shortcutInfo.title = entry.title;
434 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700435 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800436 }
Sunny Goyal34942622014-08-29 17:20:55 -0700437
Sunny Goyal736f5af2014-10-16 14:07:29 -0700438 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100439 if (!mDefaultIcons.containsKey(user)) {
440 mDefaultIcons.put(user, makeDefaultIcon(user));
441 }
442 return mDefaultIcons.get(user);
443 }
444
Kenny Guyed131872014-04-30 03:02:21 +0100445 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
446 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700447 }
448
Sunny Goyal736f5af2014-10-16 14:07:29 -0700449 /**
450 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
451 * This method is not thread safe, it must be called from a synchronized method.
452 */
Kenny Guyed131872014-04-30 03:02:21 +0100453 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700454 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700455 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100456 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700457 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800458 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100459 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500460
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800461 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700462 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800463 if (info != null) {
464 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500465 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700466 if (usePackageIcon) {
467 CacheEntry packageEntry = getEntryForPackage(
468 componentName.getPackageName(), user);
Sunny Goyal34942622014-08-29 17:20:55 -0700469 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700470 if (DEBUG) Log.d(TAG, "using package default icon for " +
471 componentName.toShortString());
472 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700473 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800474 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700475 }
476 }
477 if (entry.icon == null) {
478 if (DEBUG) Log.d(TAG, "using default icon for " +
479 componentName.toShortString());
480 entry.icon = getDefaultIcon(user);
481 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700482 }
483 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800484
485 if (TextUtils.isEmpty(entry.title) && info != null) {
486 entry.title = info.getLabel().toString();
487 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
488 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800489 }
490 return entry;
491 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400492
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700493 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700494 * Adds a default package entry in the cache. This entry is not persisted and will be removed
495 * when the cache is flushed.
496 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700497 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700498 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800499 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700500
Sunny Goyal34942622014-08-29 17:20:55 -0700501 CacheEntry entry = getEntryForPackage(packageName, user);
502 if (!TextUtils.isEmpty(title)) {
503 entry.title = title;
504 }
505 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700506 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700507 }
508 }
509
510 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700511 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700512 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700513 */
514 private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700515 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700516 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700517 CacheEntry entry = mCache.get(cacheKey);
518 if (entry == null) {
519 entry = new CacheEntry();
Sunny Goyal34942622014-08-29 17:20:55 -0700520 entry.title = "";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800521 entry.contentDescription = "";
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700522 mCache.put(cacheKey, entry);
523
524 try {
525 ApplicationInfo info = mPackageManager.getApplicationInfo(packageName, 0);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700526 entry.icon = Utilities.createIconBitmap(info.loadIcon(mPackageManager), mContext);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800527 entry.title = info.loadLabel(mPackageManager);
528 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700529 } catch (NameNotFoundException e) {
530 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
531 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700532 }
533 return entry;
534 }
535
Chris Wren6d0dde02014-02-10 12:16:54 -0500536 /**
537 * Pre-load an icon into the persistent cache.
538 *
539 * <P>Queries for a component that does not exist in the package manager
540 * will be answered by the persistent cache.
541 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500542 * @param componentName the icon should be returned for this component
543 * @param icon the icon to be persisted
544 * @param dpi the native density of the icon
545 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800546 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
547 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500548 // TODO rescale to the correct native DPI
549 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800550 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500551 packageManager.getActivityIcon(componentName);
552 // component is present on the system already, do nothing
553 return;
554 } catch (PackageManager.NameNotFoundException e) {
555 // pass
556 }
557
Sunny Goyal34b65272015-03-11 16:56:52 -0700558 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800559 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
560 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800561 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
562 SQLiteDatabase.CONFLICT_REPLACE);
563 }
564
Sunny Goyal34b65272015-03-11 16:56:52 -0700565 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
566 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800567 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700568 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
569 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800570 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
571 new String[] {component.flattenToString(),
572 Long.toString(mUserManager.getSerialNumberForUser(user))},
573 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500574 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800575 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700576 entry.icon = loadIconNoResize(c, 0);
577 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800578 entry.title = c.getString(1);
579 if (entry.title == null) {
580 entry.title = "";
581 entry.contentDescription = "";
582 } else {
583 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500584 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800585 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500586 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500587 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800588 c.close();
589 }
590 return false;
591 }
592
Sunny Goyal34b65272015-03-11 16:56:52 -0700593 public static class IconLoadRequest {
594 private final Runnable mRunnable;
595 private final Handler mHandler;
596
597 IconLoadRequest(Runnable runnable, Handler handler) {
598 mRunnable = runnable;
599 mHandler = handler;
600 }
601
602 public void cancel() {
603 mHandler.removeCallbacks(mRunnable);
604 }
605 }
606
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800607 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal34b65272015-03-11 16:56:52 -0700608 private final static int DB_VERSION = 2;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800609
610 private final static String TABLE_NAME = "icons";
611 private final static String COLUMN_ROWID = "rowid";
612 private final static String COLUMN_COMPONENT = "componentName";
613 private final static String COLUMN_USER = "profileId";
614 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
615 private final static String COLUMN_VERSION = "version";
616 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700617 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800618 private final static String COLUMN_LABEL = "label";
619
620 public IconDB(Context context) {
621 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
622 }
623
624 @Override
625 public void onCreate(SQLiteDatabase db) {
626 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
627 COLUMN_COMPONENT + " TEXT NOT NULL, " +
628 COLUMN_USER + " INTEGER NOT NULL, " +
629 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
630 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
631 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700632 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800633 COLUMN_LABEL + " TEXT, " +
634 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
635 ");");
636 }
637
638 @Override
639 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
640 if (oldVersion != newVersion) {
641 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500642 }
643 }
644
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800645 @Override
646 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
647 if (oldVersion != newVersion) {
648 clearDB(db);
649 }
Kenny Guyed131872014-04-30 03:02:21 +0100650 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500651
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800652 private void clearDB(SQLiteDatabase db) {
653 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
654 onCreate(db);
655 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700656
657 public ContentValues newContentValues(Bitmap icon, String label) {
658 ContentValues values = new ContentValues();
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700659 values.put(IconDB.COLUMN_ICON, Utilities.flattenBitmap(icon));
660 values.put(IconDB.COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700661 Bitmap.createScaledBitmap(icon,
662 icon.getWidth() / LOW_RES_SCALE_FACTOR,
663 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
664 values.put(IconDB.COLUMN_LABEL, label);
665 return values;
666 }
667 }
668
669 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
670 byte[] data = c.getBlob(iconIndex);
671 try {
672 return BitmapFactory.decodeByteArray(data, 0, data.length);
673 } catch (Exception e) {
674 return null;
675 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500676 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800677}