blob: 48b38f182508100052f33cc2c0dfb5daace1985a [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;
Hyunyoung Song3f471442015-04-08 19:01:34 -070047import com.android.launcher3.widget.PackageItemInfo;
Kenny Guyed131872014-04-30 03:02:21 +010048
Joe Onorato0589f0f2010-02-08 13:44:00 -080049import java.util.HashMap;
Chris Wren6d0dde02014-02-10 12:16:54 -050050import java.util.HashSet;
Adam Cohenb6d33df2013-10-15 10:18:02 -070051import java.util.Iterator;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080052import java.util.List;
Sunny Goyal0c9a3542015-04-01 16:04:21 -070053import java.util.Locale;
Adam Cohenb6d33df2013-10-15 10:18:02 -070054import java.util.Map.Entry;
Joe Onorato0589f0f2010-02-08 13:44:00 -080055
56/**
57 * Cache of application icons. Icons can be made from any thread.
58 */
59public class IconCache {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070060
Joe Onorato0589f0f2010-02-08 13:44:00 -080061 private static final String TAG = "Launcher.IconCache";
62
63 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050064
Sunny Goyal0fc1be12014-08-11 17:05:23 -070065 // Empty class name is used for storing package default entry.
66 private static final String EMPTY_CLASS_NAME = ".";
67
Sunny Goyalbbef77d2014-09-09 16:27:55 -070068 private static final boolean DEBUG = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -080069
Sunny Goyal34b65272015-03-11 16:56:52 -070070 private static final int LOW_RES_SCALE_FACTOR = 8;
71
Adam Cohen091440a2015-03-18 14:16:05 -070072 @Thunk static class CacheEntry {
Joe Onorato0589f0f2010-02-08 13:44:00 -080073 public Bitmap icon;
Kenny Guyd6fe5262014-07-21 17:11:41 +010074 public CharSequence title;
75 public CharSequence contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -070076 public boolean isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -080077 }
78
Sunny Goyal8758ea02015-03-18 10:07:49 -070079 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
80 private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
81
Daniel Sandlercc8befa2013-06-11 14:45:48 -040082 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070083 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010084 private final UserManagerCompat mUserManager;
85 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070086 private final HashMap<ComponentKey, CacheEntry> mCache =
87 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080088 private final int mIconDpi;
89 private final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080090
Sunny Goyal34b65272015-03-11 16:56:52 -070091 private final Handler mWorkerHandler;
92
Daniel Sandlercc8befa2013-06-11 14:45:48 -040093 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -080094 ActivityManager activityManager =
95 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
96
Joe Onorato0589f0f2010-02-08 13:44:00 -080097 mContext = context;
98 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +010099 mUserManager = UserManagerCompat.getInstance(mContext);
100 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Winson Chungd83f5f42012-02-13 14:27:42 -0800101 mIconDpi = activityManager.getLauncherLargeIconDensity();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800102 mIconDb = new IconDB(context);
Sunny Goyal34b65272015-03-11 16:56:52 -0700103
104 mWorkerHandler = new Handler(LauncherModel.sWorkerThread.getLooper());
Romain Guya28fd3f2010-03-15 14:44:42 -0700105 }
106
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800107 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700108 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700109 }
110
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700111 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700112 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700113 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700114 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700115 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700116 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700117 }
Michael Jurka721d9722011-08-03 11:49:59 -0700118
119 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700120 }
121
Winson Chung0b9fcf52011-10-31 13:05:15 -0700122 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700123 Resources resources;
124 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700125 resources = mPackageManager.getResourcesForApplication(packageName);
126 } catch (PackageManager.NameNotFoundException e) {
127 resources = null;
128 }
129 if (resources != null) {
130 if (iconId != 0) {
131 return getFullResIcon(resources, iconId);
132 }
133 }
134 return getFullResDefaultActivityIcon();
135 }
136
Sunny Goyalffe83f12014-08-14 17:39:34 -0700137 public int getFullResIconDpi() {
138 return mIconDpi;
139 }
140
Michael Jurkadac85912012-05-18 15:04:49 -0700141 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700142 Resources resources;
143 try {
144 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700145 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700146 } catch (PackageManager.NameNotFoundException e) {
147 resources = null;
148 }
149 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700150 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700151 if (iconId != 0) {
152 return getFullResIcon(resources, iconId);
153 }
154 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500155
Michael Jurkac9a96192010-11-01 11:52:08 -0700156 return getFullResDefaultActivityIcon();
157 }
158
Kenny Guyed131872014-04-30 03:02:21 +0100159 private Bitmap makeDefaultIcon(UserHandleCompat user) {
160 Drawable unbadged = getFullResDefaultActivityIcon();
161 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700162 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
163 Math.max(d.getIntrinsicHeight(), 1),
164 Bitmap.Config.ARGB_8888);
165 Canvas c = new Canvas(b);
166 d.setBounds(0, 0, b.getWidth(), b.getHeight());
167 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700168 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700169 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800170 }
171
172 /**
173 * Remove any records for the supplied ComponentName.
174 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700175 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700176 mCache.remove(new ComponentKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800177 }
178
179 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800180 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500181 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800182 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700183 HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
184 for (ComponentKey key: mCache.keySet()) {
Kenny Guyed131872014-04-30 03:02:21 +0100185 if (key.componentName.getPackageName().equals(packageName)
186 && key.user.equals(user)) {
187 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500188 }
189 }
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700190 for (ComponentKey condemned: forDeletion) {
Kenny Guyed131872014-04-30 03:02:21 +0100191 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500192 }
193 }
194
195 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800196 * Updates the entries related to the given package in memory and persistent DB.
197 */
198 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
199 removeIconsForPkg(packageName, user);
200 try {
201 PackageInfo info = mPackageManager.getPackageInfo(packageName,
202 PackageManager.GET_UNINSTALLED_PACKAGES);
203 long userSerial = mUserManager.getSerialNumberForUser(user);
204 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700205 addIconToDBAndMemCache(app, info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800206 }
207 } catch (NameNotFoundException e) {
208 Log.d(TAG, "Package not found", e);
209 return;
210 }
211 }
212
213 /**
214 * Removes the entries related to the given package in memory and persistent DB.
215 */
216 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
217 removeFromMemCacheLocked(packageName, user);
218 long userSerial = mUserManager.getSerialNumberForUser(user);
219 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
220 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
221 new String[] {packageName + "/%", Long.toString(userSerial)});
222 }
223
224 /**
225 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
226 * the DB and are updated.
227 * @return The set of packages for which icons have updated.
228 */
229 public HashSet<String> updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700230 mIconDb.updateSystemStateString(mContext);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800231 long userSerial = mUserManager.getSerialNumberForUser(user);
232 PackageManager pm = mContext.getPackageManager();
233 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
234 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
235 pkgInfoMap.put(info.packageName, info);
236 }
237
238 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
239 for (LauncherActivityInfoCompat app : apps) {
240 componentMap.put(app.getComponentName(), app);
241 }
242
243 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
244 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700245 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION,
246 IconDB.COLUMN_SYSTEM_STATE},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800247 IconDB.COLUMN_USER + " = ? ",
248 new String[] {Long.toString(userSerial)},
249 null, null, null);
250
251 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
252 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
253 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
254 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700255 final int systemStateIndex = c.getColumnIndex(IconDB.COLUMN_SYSTEM_STATE);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800256
257 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
258 HashSet<String> updatedPackages = new HashSet<String>();
259
260 while (c.moveToNext()) {
261 String cn = c.getString(indexComponent);
262 ComponentName component = ComponentName.unflattenFromString(cn);
263 PackageInfo info = pkgInfoMap.get(component.getPackageName());
264 if (info == null) {
265 itemsToRemove.add(c.getInt(rowIndex));
266 continue;
267 }
268 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
269 // Application is not present
270 continue;
271 }
272
273 long updateTime = c.getLong(indexLastUpdate);
274 int version = c.getInt(indexVersion);
275 LauncherActivityInfoCompat app = componentMap.remove(component);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700276 if (version == info.versionCode && updateTime == info.lastUpdateTime &&
277 TextUtils.equals(mIconDb.mSystemState, c.getString(systemStateIndex))) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800278 continue;
279 }
280 if (app == null) {
281 itemsToRemove.add(c.getInt(rowIndex));
282 continue;
283 }
284 ContentValues values = updateCacheAndGetContentValues(app);
285 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
Sunny Goyalece6c8b2015-04-13 11:47:00 -0700286 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
287 new String[] {cn, Long.toString(userSerial)});
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800288
289 updatedPackages.add(component.getPackageName());
290 }
291 c.close();
292 if (!itemsToRemove.isEmpty()) {
293 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
294 IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )",
295 null);
296 }
297
298 // Insert remaining apps.
299 for (LauncherActivityInfoCompat app : componentMap.values()) {
300 PackageInfo info = pkgInfoMap.get(app.getComponentName().getPackageName());
301 if (info == null) {
302 continue;
303 }
Sunny Goyald180cf72015-04-06 12:45:40 -0700304 addIconToDBAndMemCache(app, info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800305 }
306 return updatedPackages;
307 }
308
Sunny Goyald180cf72015-04-06 12:45:40 -0700309 private void addIconToDBAndMemCache(LauncherActivityInfoCompat app, PackageInfo info,
310 long userSerial) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800311 ContentValues values = updateCacheAndGetContentValues(app);
Sunny Goyald180cf72015-04-06 12:45:40 -0700312 addIconToDB(values, app.getComponentName(), info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800313 values.put(IconDB.COLUMN_COMPONENT, app.getComponentName().flattenToString());
Sunny Goyald180cf72015-04-06 12:45:40 -0700314 }
315
316 /**
317 * Updates {@param values} to contain versoning information and adds it to the DB.
318 * @param values {@link ContentValues} containing icon & title
319 */
320 private void addIconToDB(ContentValues values, ComponentName key,
321 PackageInfo info, long userSerial) {
322 values.put(IconDB.COLUMN_COMPONENT, key.flattenToString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800323 values.put(IconDB.COLUMN_USER, userSerial);
324 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
325 values.put(IconDB.COLUMN_VERSION, info.versionCode);
326 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
327 SQLiteDatabase.CONFLICT_REPLACE);
328 }
329
330 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app) {
331 CacheEntry entry = new CacheEntry();
332 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
333 entry.title = app.getLabel();
334 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700335 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800336
Sunny Goyal34b65272015-03-11 16:56:52 -0700337 return mIconDb.newContentValues(entry.icon, entry.title.toString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800338 }
339
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800340 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800341 * Empty out the cache.
342 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700343 public synchronized void flush() {
344 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800345 }
346
347 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700348 * Empty out the cache that aren't of the correct grid size
349 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700350 public synchronized void flushInvalidIcons(DeviceProfile grid) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700351 Iterator<Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator();
Sunny Goyal736f5af2014-10-16 14:07:29 -0700352 while (it.hasNext()) {
353 final CacheEntry e = it.next().getValue();
354 if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
355 || e.icon.getHeight() < grid.iconSizePx)) {
356 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700357 }
358 }
359 }
360
361 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700362 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
363 * @return a request ID that can be used to cancel the request.
364 */
365 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
366 Runnable request = new Runnable() {
367
368 @Override
369 public void run() {
370 if (info instanceof AppInfo) {
371 getTitleAndIcon((AppInfo) info, null, false);
372 } else if (info instanceof ShortcutInfo) {
373 ShortcutInfo st = (ShortcutInfo) info;
374 getTitleAndIcon(st,
375 st.promisedIntent != null ? st.promisedIntent : st.intent,
376 st.user, false);
377 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700378 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700379
380 @Override
381 public void run() {
382 caller.reapplyItemInfo(info);
383 }
384 });
385 }
386 };
387 mWorkerHandler.post(request);
388 return new IconLoadRequest(request, mWorkerHandler);
389 }
390
391 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800392 * Fill in "application" with the icon and label for "info."
393 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700394 public synchronized void getTitleAndIcon(AppInfo application,
395 LauncherActivityInfoCompat info, boolean useLowResIcon) {
396 CacheEntry entry = cacheLocked(application.componentName, info,
397 info == null ? application.user : info.getUser(),
398 false, useLowResIcon);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700399 application.title = entry.title;
400 application.iconBitmap = entry.icon;
401 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700402 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800403 }
404
Sunny Goyal34b65272015-03-11 16:56:52 -0700405 /**
406 * Returns a high res icon for the given intent and user
407 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700408 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
409 ComponentName component = intent.getComponent();
410 // null info means not installed, but if we have a component from the intent then
411 // we should still look in the cache for restored app icons.
412 if (component == null) {
413 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800414 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700415
416 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700417 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700418 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800419 }
420
Sunny Goyal34942622014-08-29 17:20:55 -0700421 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800422 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
423 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700424 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700425 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700426 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700427 ComponentName component = intent.getComponent();
428 // null info means not installed, but if we have a component from the intent then
429 // we should still look in the cache for restored app icons.
430 if (component == null) {
431 shortcutInfo.setIcon(getDefaultIcon(user));
432 shortcutInfo.title = "";
433 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700434 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700435 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800436 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700437 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700438 }
439 }
440
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800441 /**
442 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
443 */
444 public synchronized void getTitleAndIcon(
445 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700446 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
447 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800448 shortcutInfo.setIcon(entry.icon);
449 shortcutInfo.title = entry.title;
450 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700451 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800452 }
Sunny Goyal34942622014-08-29 17:20:55 -0700453
Sunny Goyald180cf72015-04-06 12:45:40 -0700454 /**
455 * Fill in {@param appInfo} with the icon and label for {@param packageName}
456 */
457 public synchronized void getTitleAndIconForApp(
Hyunyoung Song3f471442015-04-08 19:01:34 -0700458 String packageName, UserHandleCompat user, boolean useLowResIcon,
459 PackageItemInfo infoOut) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700460 CacheEntry entry = getEntryForPackageLocked(packageName, user, useLowResIcon);
Hyunyoung Song3f471442015-04-08 19:01:34 -0700461 infoOut.iconBitmap = entry.icon;
462 infoOut.title = entry.title;
463 infoOut.usingLowResIcon = entry.isLowResIcon;
464 infoOut.contentDescription = entry.contentDescription;
Sunny Goyald180cf72015-04-06 12:45:40 -0700465 }
466
Sunny Goyal736f5af2014-10-16 14:07:29 -0700467 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100468 if (!mDefaultIcons.containsKey(user)) {
469 mDefaultIcons.put(user, makeDefaultIcon(user));
470 }
471 return mDefaultIcons.get(user);
472 }
473
Kenny Guyed131872014-04-30 03:02:21 +0100474 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
475 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700476 }
477
Sunny Goyal736f5af2014-10-16 14:07:29 -0700478 /**
479 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
480 * This method is not thread safe, it must be called from a synchronized method.
481 */
Kenny Guyed131872014-04-30 03:02:21 +0100482 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700483 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700484 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100485 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700486 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800487 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100488 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500489
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800490 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700491 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800492 if (info != null) {
493 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500494 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700495 if (usePackageIcon) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700496 CacheEntry packageEntry = getEntryForPackageLocked(
497 componentName.getPackageName(), user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700498 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700499 if (DEBUG) Log.d(TAG, "using package default icon for " +
500 componentName.toShortString());
501 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700502 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800503 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700504 }
505 }
506 if (entry.icon == null) {
507 if (DEBUG) Log.d(TAG, "using default icon for " +
508 componentName.toShortString());
509 entry.icon = getDefaultIcon(user);
510 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700511 }
512 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800513
514 if (TextUtils.isEmpty(entry.title) && info != null) {
515 entry.title = info.getLabel().toString();
516 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
517 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800518 }
519 return entry;
520 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400521
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700522 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700523 * Adds a default package entry in the cache. This entry is not persisted and will be removed
524 * when the cache is flushed.
525 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700526 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700527 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800528 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700529
Sunny Goyald180cf72015-04-06 12:45:40 -0700530 CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700531 if (!TextUtils.isEmpty(title)) {
532 entry.title = title;
533 }
534 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700535 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700536 }
537 }
538
539 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700540 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700541 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyald180cf72015-04-06 12:45:40 -0700542 *
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700543 */
Sunny Goyald180cf72015-04-06 12:45:40 -0700544 private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
545 boolean useLowResIcon) {
546 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700547 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700548 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyald180cf72015-04-06 12:45:40 -0700549 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700550 entry = new CacheEntry();
551 mCache.put(cacheKey, entry);
552
Sunny Goyald180cf72015-04-06 12:45:40 -0700553 // Check the DB first.
554 if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
555 try {
556 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
557 ApplicationInfo appInfo = info.applicationInfo;
558 if (appInfo == null) {
559 throw new NameNotFoundException("ApplicationInfo is null");
560 }
561 Drawable drawable = mUserManager.getBadgedDrawableForUser(
562 appInfo.loadIcon(mPackageManager), user);
563 entry.icon = Utilities.createIconBitmap(drawable, mContext);
564 entry.title = appInfo.loadLabel(mPackageManager);
565 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
566 entry.isLowResIcon = false;
567
568 // Add the icon in the DB here, since these do not get written during
569 // package updates.
570 ContentValues values =
571 mIconDb.newContentValues(entry.icon, entry.title.toString());
572 addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
573
574 } catch (NameNotFoundException e) {
575 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
576 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700577 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700578 }
579 return entry;
580 }
581
Chris Wren6d0dde02014-02-10 12:16:54 -0500582 /**
583 * Pre-load an icon into the persistent cache.
584 *
585 * <P>Queries for a component that does not exist in the package manager
586 * will be answered by the persistent cache.
587 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500588 * @param componentName the icon should be returned for this component
589 * @param icon the icon to be persisted
590 * @param dpi the native density of the icon
591 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800592 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
593 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500594 // TODO rescale to the correct native DPI
595 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800596 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500597 packageManager.getActivityIcon(componentName);
598 // component is present on the system already, do nothing
599 return;
600 } catch (PackageManager.NameNotFoundException e) {
601 // pass
602 }
603
Sunny Goyal34b65272015-03-11 16:56:52 -0700604 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800605 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
606 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800607 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
608 SQLiteDatabase.CONFLICT_REPLACE);
609 }
610
Sunny Goyal34b65272015-03-11 16:56:52 -0700611 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
612 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800613 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700614 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
615 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800616 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
617 new String[] {component.flattenToString(),
618 Long.toString(mUserManager.getSerialNumberForUser(user))},
619 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500620 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800621 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700622 entry.icon = loadIconNoResize(c, 0);
623 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800624 entry.title = c.getString(1);
625 if (entry.title == null) {
626 entry.title = "";
627 entry.contentDescription = "";
628 } else {
629 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500630 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800631 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500632 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500633 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800634 c.close();
635 }
636 return false;
637 }
638
Sunny Goyal34b65272015-03-11 16:56:52 -0700639 public static class IconLoadRequest {
640 private final Runnable mRunnable;
641 private final Handler mHandler;
642
643 IconLoadRequest(Runnable runnable, Handler handler) {
644 mRunnable = runnable;
645 mHandler = handler;
646 }
647
648 public void cancel() {
649 mHandler.removeCallbacks(mRunnable);
650 }
651 }
652
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800653 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700654 private final static int DB_VERSION = 3;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800655
656 private final static String TABLE_NAME = "icons";
657 private final static String COLUMN_ROWID = "rowid";
658 private final static String COLUMN_COMPONENT = "componentName";
659 private final static String COLUMN_USER = "profileId";
660 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
661 private final static String COLUMN_VERSION = "version";
662 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700663 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800664 private final static String COLUMN_LABEL = "label";
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700665 private final static String COLUMN_SYSTEM_STATE = "system_state";
666
667 public String mSystemState;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800668
669 public IconDB(Context context) {
670 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700671 updateSystemStateString(context);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800672 }
673
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700674 public void updateSystemStateString(Context c) {
675 mSystemState = Locale.getDefault().toString() + ","
676 + c.getResources().getConfiguration().mcc;
677 }
678
679
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800680 @Override
681 public void onCreate(SQLiteDatabase db) {
682 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
683 COLUMN_COMPONENT + " TEXT NOT NULL, " +
684 COLUMN_USER + " INTEGER NOT NULL, " +
685 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
686 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
687 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700688 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800689 COLUMN_LABEL + " TEXT, " +
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700690 COLUMN_SYSTEM_STATE + " TEXT, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800691 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
692 ");");
693 }
694
695 @Override
696 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
697 if (oldVersion != newVersion) {
698 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500699 }
700 }
701
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800702 @Override
703 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
704 if (oldVersion != newVersion) {
705 clearDB(db);
706 }
Kenny Guyed131872014-04-30 03:02:21 +0100707 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500708
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800709 private void clearDB(SQLiteDatabase db) {
710 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
711 onCreate(db);
712 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700713
714 public ContentValues newContentValues(Bitmap icon, String label) {
715 ContentValues values = new ContentValues();
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700716 values.put(COLUMN_ICON, Utilities.flattenBitmap(icon));
717 values.put(COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700718 Bitmap.createScaledBitmap(icon,
719 icon.getWidth() / LOW_RES_SCALE_FACTOR,
720 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700721 values.put(COLUMN_LABEL, label);
722 values.put(COLUMN_SYSTEM_STATE, mSystemState);
Sunny Goyal34b65272015-03-11 16:56:52 -0700723 return values;
724 }
725 }
726
727 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
728 byte[] data = c.getBlob(iconIndex);
729 try {
730 return BitmapFactory.decodeByteArray(data, 0, data.length);
731 } catch (Exception e) {
732 return null;
733 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500734 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800735}