blob: 21ac5c03094d5de80187a6b32db3795f3bc42a92 [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;
Sunny Goyal0c9a3542015-04-01 16:04:21 -070052import java.util.Locale;
Adam Cohenb6d33df2013-10-15 10:18:02 -070053import java.util.Map.Entry;
Joe Onorato0589f0f2010-02-08 13:44:00 -080054
55/**
56 * Cache of application icons. Icons can be made from any thread.
57 */
58public class IconCache {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070059
Joe Onorato0589f0f2010-02-08 13:44:00 -080060 private static final String TAG = "Launcher.IconCache";
61
62 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050063
Sunny Goyal0fc1be12014-08-11 17:05:23 -070064 // Empty class name is used for storing package default entry.
65 private static final String EMPTY_CLASS_NAME = ".";
66
Sunny Goyalbbef77d2014-09-09 16:27:55 -070067 private static final boolean DEBUG = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -080068
Sunny Goyal34b65272015-03-11 16:56:52 -070069 private static final int LOW_RES_SCALE_FACTOR = 8;
70
Adam Cohen091440a2015-03-18 14:16:05 -070071 @Thunk static class CacheEntry {
Joe Onorato0589f0f2010-02-08 13:44:00 -080072 public Bitmap icon;
Kenny Guyd6fe5262014-07-21 17:11:41 +010073 public CharSequence title;
74 public CharSequence contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -070075 public boolean isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -080076 }
77
Sunny Goyal8758ea02015-03-18 10:07:49 -070078 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
79 private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
80
Daniel Sandlercc8befa2013-06-11 14:45:48 -040081 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070082 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010083 private final UserManagerCompat mUserManager;
84 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070085 private final HashMap<ComponentKey, CacheEntry> mCache =
86 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080087 private final int mIconDpi;
88 private final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080089
Sunny Goyal34b65272015-03-11 16:56:52 -070090 private final Handler mWorkerHandler;
91
Daniel Sandlercc8befa2013-06-11 14:45:48 -040092 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -080093 ActivityManager activityManager =
94 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
95
Joe Onorato0589f0f2010-02-08 13:44:00 -080096 mContext = context;
97 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +010098 mUserManager = UserManagerCompat.getInstance(mContext);
99 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Winson Chungd83f5f42012-02-13 14:27:42 -0800100 mIconDpi = activityManager.getLauncherLargeIconDensity();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800101 mIconDb = new IconDB(context);
Sunny Goyal34b65272015-03-11 16:56:52 -0700102
103 mWorkerHandler = new Handler(LauncherModel.sWorkerThread.getLooper());
Romain Guya28fd3f2010-03-15 14:44:42 -0700104 }
105
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800106 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700107 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700108 }
109
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700110 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700111 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700112 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700113 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700114 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700115 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700116 }
Michael Jurka721d9722011-08-03 11:49:59 -0700117
118 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700119 }
120
Winson Chung0b9fcf52011-10-31 13:05:15 -0700121 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700122 Resources resources;
123 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700124 resources = mPackageManager.getResourcesForApplication(packageName);
125 } catch (PackageManager.NameNotFoundException e) {
126 resources = null;
127 }
128 if (resources != null) {
129 if (iconId != 0) {
130 return getFullResIcon(resources, iconId);
131 }
132 }
133 return getFullResDefaultActivityIcon();
134 }
135
Sunny Goyalffe83f12014-08-14 17:39:34 -0700136 public int getFullResIconDpi() {
137 return mIconDpi;
138 }
139
Michael Jurkadac85912012-05-18 15:04:49 -0700140 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700141 Resources resources;
142 try {
143 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700144 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700145 } catch (PackageManager.NameNotFoundException e) {
146 resources = null;
147 }
148 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700149 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700150 if (iconId != 0) {
151 return getFullResIcon(resources, iconId);
152 }
153 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500154
Michael Jurkac9a96192010-11-01 11:52:08 -0700155 return getFullResDefaultActivityIcon();
156 }
157
Kenny Guyed131872014-04-30 03:02:21 +0100158 private Bitmap makeDefaultIcon(UserHandleCompat user) {
159 Drawable unbadged = getFullResDefaultActivityIcon();
160 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700161 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
162 Math.max(d.getIntrinsicHeight(), 1),
163 Bitmap.Config.ARGB_8888);
164 Canvas c = new Canvas(b);
165 d.setBounds(0, 0, b.getWidth(), b.getHeight());
166 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700167 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700168 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800169 }
170
171 /**
172 * Remove any records for the supplied ComponentName.
173 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700174 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700175 mCache.remove(new ComponentKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800176 }
177
178 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800179 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500180 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800181 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700182 HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
183 for (ComponentKey key: mCache.keySet()) {
Kenny Guyed131872014-04-30 03:02:21 +0100184 if (key.componentName.getPackageName().equals(packageName)
185 && key.user.equals(user)) {
186 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500187 }
188 }
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700189 for (ComponentKey condemned: forDeletion) {
Kenny Guyed131872014-04-30 03:02:21 +0100190 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500191 }
192 }
193
194 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800195 * Updates the entries related to the given package in memory and persistent DB.
196 */
197 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
198 removeIconsForPkg(packageName, user);
199 try {
200 PackageInfo info = mPackageManager.getPackageInfo(packageName,
201 PackageManager.GET_UNINSTALLED_PACKAGES);
202 long userSerial = mUserManager.getSerialNumberForUser(user);
203 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
204 addIconToDB(app, info, userSerial);
205 }
206 } catch (NameNotFoundException e) {
207 Log.d(TAG, "Package not found", e);
208 return;
209 }
210 }
211
212 /**
213 * Removes the entries related to the given package in memory and persistent DB.
214 */
215 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
216 removeFromMemCacheLocked(packageName, user);
217 long userSerial = mUserManager.getSerialNumberForUser(user);
218 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
219 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
220 new String[] {packageName + "/%", Long.toString(userSerial)});
221 }
222
223 /**
224 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
225 * the DB and are updated.
226 * @return The set of packages for which icons have updated.
227 */
228 public HashSet<String> updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700229 mIconDb.updateSystemStateString(mContext);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800230 long userSerial = mUserManager.getSerialNumberForUser(user);
231 PackageManager pm = mContext.getPackageManager();
232 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
233 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
234 pkgInfoMap.put(info.packageName, info);
235 }
236
237 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
238 for (LauncherActivityInfoCompat app : apps) {
239 componentMap.put(app.getComponentName(), app);
240 }
241
242 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
243 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700244 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION,
245 IconDB.COLUMN_SYSTEM_STATE},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800246 IconDB.COLUMN_USER + " = ? ",
247 new String[] {Long.toString(userSerial)},
248 null, null, null);
249
250 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
251 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
252 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
253 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700254 final int systemStateIndex = c.getColumnIndex(IconDB.COLUMN_SYSTEM_STATE);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800255
256 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
257 HashSet<String> updatedPackages = new HashSet<String>();
258
259 while (c.moveToNext()) {
260 String cn = c.getString(indexComponent);
261 ComponentName component = ComponentName.unflattenFromString(cn);
262 PackageInfo info = pkgInfoMap.get(component.getPackageName());
263 if (info == null) {
264 itemsToRemove.add(c.getInt(rowIndex));
265 continue;
266 }
267 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
268 // Application is not present
269 continue;
270 }
271
272 long updateTime = c.getLong(indexLastUpdate);
273 int version = c.getInt(indexVersion);
274 LauncherActivityInfoCompat app = componentMap.remove(component);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700275 if (version == info.versionCode && updateTime == info.lastUpdateTime &&
276 TextUtils.equals(mIconDb.mSystemState, c.getString(systemStateIndex))) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800277 continue;
278 }
279 if (app == null) {
280 itemsToRemove.add(c.getInt(rowIndex));
281 continue;
282 }
283 ContentValues values = updateCacheAndGetContentValues(app);
284 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
285 IconDB.COLUMN_COMPONENT + " = ?",
286 new String[] { cn });
287
288 updatedPackages.add(component.getPackageName());
289 }
290 c.close();
291 if (!itemsToRemove.isEmpty()) {
292 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
293 IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )",
294 null);
295 }
296
297 // Insert remaining apps.
298 for (LauncherActivityInfoCompat app : componentMap.values()) {
299 PackageInfo info = pkgInfoMap.get(app.getComponentName().getPackageName());
300 if (info == null) {
301 continue;
302 }
303 addIconToDB(app, info, userSerial);
304 }
305 return updatedPackages;
306 }
307
308 private void addIconToDB(LauncherActivityInfoCompat app, PackageInfo info, long userSerial) {
309 ContentValues values = updateCacheAndGetContentValues(app);
310 values.put(IconDB.COLUMN_COMPONENT, app.getComponentName().flattenToString());
311 values.put(IconDB.COLUMN_USER, userSerial);
312 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
313 values.put(IconDB.COLUMN_VERSION, info.versionCode);
314 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
315 SQLiteDatabase.CONFLICT_REPLACE);
316 }
317
318 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app) {
319 CacheEntry entry = new CacheEntry();
320 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
321 entry.title = app.getLabel();
322 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700323 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800324
Sunny Goyal34b65272015-03-11 16:56:52 -0700325 return mIconDb.newContentValues(entry.icon, entry.title.toString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800326 }
327
328
329 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800330 * Empty out the cache.
331 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700332 public synchronized void flush() {
333 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800334 }
335
336 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700337 * Empty out the cache that aren't of the correct grid size
338 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700339 public synchronized void flushInvalidIcons(DeviceProfile grid) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700340 Iterator<Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator();
Sunny Goyal736f5af2014-10-16 14:07:29 -0700341 while (it.hasNext()) {
342 final CacheEntry e = it.next().getValue();
343 if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
344 || e.icon.getHeight() < grid.iconSizePx)) {
345 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700346 }
347 }
348 }
349
350 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700351 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
352 * @return a request ID that can be used to cancel the request.
353 */
354 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
355 Runnable request = new Runnable() {
356
357 @Override
358 public void run() {
359 if (info instanceof AppInfo) {
360 getTitleAndIcon((AppInfo) info, null, false);
361 } else if (info instanceof ShortcutInfo) {
362 ShortcutInfo st = (ShortcutInfo) info;
363 getTitleAndIcon(st,
364 st.promisedIntent != null ? st.promisedIntent : st.intent,
365 st.user, false);
366 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700367 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700368
369 @Override
370 public void run() {
371 caller.reapplyItemInfo(info);
372 }
373 });
374 }
375 };
376 mWorkerHandler.post(request);
377 return new IconLoadRequest(request, mWorkerHandler);
378 }
379
380 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800381 * Fill in "application" with the icon and label for "info."
382 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700383 public synchronized void getTitleAndIcon(AppInfo application,
384 LauncherActivityInfoCompat info, boolean useLowResIcon) {
385 CacheEntry entry = cacheLocked(application.componentName, info,
386 info == null ? application.user : info.getUser(),
387 false, useLowResIcon);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700388 application.title = entry.title;
389 application.iconBitmap = entry.icon;
390 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700391 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800392 }
393
Sunny Goyal34b65272015-03-11 16:56:52 -0700394 /**
395 * Returns a high res icon for the given intent and user
396 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700397 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
398 ComponentName component = intent.getComponent();
399 // null info means not installed, but if we have a component from the intent then
400 // we should still look in the cache for restored app icons.
401 if (component == null) {
402 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800403 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700404
405 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700406 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700407 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800408 }
409
Sunny Goyal34942622014-08-29 17:20:55 -0700410 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800411 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
412 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700413 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700414 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700415 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700416 ComponentName component = intent.getComponent();
417 // null info means not installed, but if we have a component from the intent then
418 // we should still look in the cache for restored app icons.
419 if (component == null) {
420 shortcutInfo.setIcon(getDefaultIcon(user));
421 shortcutInfo.title = "";
422 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700423 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700424 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800425 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700426 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700427 }
428 }
429
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800430 /**
431 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
432 */
433 public synchronized void getTitleAndIcon(
434 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700435 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
436 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800437 shortcutInfo.setIcon(entry.icon);
438 shortcutInfo.title = entry.title;
439 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700440 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800441 }
Sunny Goyal34942622014-08-29 17:20:55 -0700442
Sunny Goyal736f5af2014-10-16 14:07:29 -0700443 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100444 if (!mDefaultIcons.containsKey(user)) {
445 mDefaultIcons.put(user, makeDefaultIcon(user));
446 }
447 return mDefaultIcons.get(user);
448 }
449
Kenny Guyed131872014-04-30 03:02:21 +0100450 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
451 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700452 }
453
Sunny Goyal736f5af2014-10-16 14:07:29 -0700454 /**
455 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
456 * This method is not thread safe, it must be called from a synchronized method.
457 */
Kenny Guyed131872014-04-30 03:02:21 +0100458 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700459 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700460 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100461 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700462 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800463 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100464 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500465
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800466 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700467 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800468 if (info != null) {
469 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500470 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700471 if (usePackageIcon) {
472 CacheEntry packageEntry = getEntryForPackage(
473 componentName.getPackageName(), user);
Sunny Goyal34942622014-08-29 17:20:55 -0700474 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700475 if (DEBUG) Log.d(TAG, "using package default icon for " +
476 componentName.toShortString());
477 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700478 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800479 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700480 }
481 }
482 if (entry.icon == null) {
483 if (DEBUG) Log.d(TAG, "using default icon for " +
484 componentName.toShortString());
485 entry.icon = getDefaultIcon(user);
486 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700487 }
488 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800489
490 if (TextUtils.isEmpty(entry.title) && info != null) {
491 entry.title = info.getLabel().toString();
492 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
493 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800494 }
495 return entry;
496 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400497
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700498 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700499 * Adds a default package entry in the cache. This entry is not persisted and will be removed
500 * when the cache is flushed.
501 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700502 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700503 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800504 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700505
Sunny Goyal34942622014-08-29 17:20:55 -0700506 CacheEntry entry = getEntryForPackage(packageName, user);
507 if (!TextUtils.isEmpty(title)) {
508 entry.title = title;
509 }
510 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700511 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700512 }
513 }
514
515 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700516 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700517 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700518 */
519 private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700520 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700521 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700522 CacheEntry entry = mCache.get(cacheKey);
523 if (entry == null) {
524 entry = new CacheEntry();
Sunny Goyal34942622014-08-29 17:20:55 -0700525 entry.title = "";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800526 entry.contentDescription = "";
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700527 mCache.put(cacheKey, entry);
528
529 try {
530 ApplicationInfo info = mPackageManager.getApplicationInfo(packageName, 0);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700531 entry.icon = Utilities.createIconBitmap(info.loadIcon(mPackageManager), mContext);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800532 entry.title = info.loadLabel(mPackageManager);
533 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700534 } catch (NameNotFoundException e) {
535 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
536 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700537 }
538 return entry;
539 }
540
Chris Wren6d0dde02014-02-10 12:16:54 -0500541 /**
542 * Pre-load an icon into the persistent cache.
543 *
544 * <P>Queries for a component that does not exist in the package manager
545 * will be answered by the persistent cache.
546 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500547 * @param componentName the icon should be returned for this component
548 * @param icon the icon to be persisted
549 * @param dpi the native density of the icon
550 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800551 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
552 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500553 // TODO rescale to the correct native DPI
554 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800555 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500556 packageManager.getActivityIcon(componentName);
557 // component is present on the system already, do nothing
558 return;
559 } catch (PackageManager.NameNotFoundException e) {
560 // pass
561 }
562
Sunny Goyal34b65272015-03-11 16:56:52 -0700563 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800564 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
565 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800566 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
567 SQLiteDatabase.CONFLICT_REPLACE);
568 }
569
Sunny Goyal34b65272015-03-11 16:56:52 -0700570 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
571 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800572 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700573 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
574 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800575 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
576 new String[] {component.flattenToString(),
577 Long.toString(mUserManager.getSerialNumberForUser(user))},
578 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500579 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800580 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700581 entry.icon = loadIconNoResize(c, 0);
582 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800583 entry.title = c.getString(1);
584 if (entry.title == null) {
585 entry.title = "";
586 entry.contentDescription = "";
587 } else {
588 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500589 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800590 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500591 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500592 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800593 c.close();
594 }
595 return false;
596 }
597
Sunny Goyal34b65272015-03-11 16:56:52 -0700598 public static class IconLoadRequest {
599 private final Runnable mRunnable;
600 private final Handler mHandler;
601
602 IconLoadRequest(Runnable runnable, Handler handler) {
603 mRunnable = runnable;
604 mHandler = handler;
605 }
606
607 public void cancel() {
608 mHandler.removeCallbacks(mRunnable);
609 }
610 }
611
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800612 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700613 private final static int DB_VERSION = 3;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800614
615 private final static String TABLE_NAME = "icons";
616 private final static String COLUMN_ROWID = "rowid";
617 private final static String COLUMN_COMPONENT = "componentName";
618 private final static String COLUMN_USER = "profileId";
619 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
620 private final static String COLUMN_VERSION = "version";
621 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700622 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800623 private final static String COLUMN_LABEL = "label";
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700624 private final static String COLUMN_SYSTEM_STATE = "system_state";
625
626 public String mSystemState;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800627
628 public IconDB(Context context) {
629 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700630 updateSystemStateString(context);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800631 }
632
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700633 public void updateSystemStateString(Context c) {
634 mSystemState = Locale.getDefault().toString() + ","
635 + c.getResources().getConfiguration().mcc;
636 }
637
638
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800639 @Override
640 public void onCreate(SQLiteDatabase db) {
641 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
642 COLUMN_COMPONENT + " TEXT NOT NULL, " +
643 COLUMN_USER + " INTEGER NOT NULL, " +
644 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
645 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
646 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700647 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800648 COLUMN_LABEL + " TEXT, " +
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700649 COLUMN_SYSTEM_STATE + " TEXT, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800650 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
651 ");");
652 }
653
654 @Override
655 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
656 if (oldVersion != newVersion) {
657 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500658 }
659 }
660
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800661 @Override
662 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
663 if (oldVersion != newVersion) {
664 clearDB(db);
665 }
Kenny Guyed131872014-04-30 03:02:21 +0100666 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500667
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800668 private void clearDB(SQLiteDatabase db) {
669 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
670 onCreate(db);
671 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700672
673 public ContentValues newContentValues(Bitmap icon, String label) {
674 ContentValues values = new ContentValues();
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700675 values.put(COLUMN_ICON, Utilities.flattenBitmap(icon));
676 values.put(COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700677 Bitmap.createScaledBitmap(icon,
678 icon.getWidth() / LOW_RES_SCALE_FACTOR,
679 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700680 values.put(COLUMN_LABEL, label);
681 values.put(COLUMN_SYSTEM_STATE, mSystemState);
Sunny Goyal34b65272015-03-11 16:56:52 -0700682 return values;
683 }
684 }
685
686 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
687 byte[] data = c.getBlob(iconIndex);
688 try {
689 return BitmapFactory.decodeByteArray(data, 0, data.length);
690 } catch (Exception e) {
691 return null;
692 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500693 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800694}