blob: fd4571482348e61c8e284848816434f77d30d7f6 [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
Sunny Goyal756adbc2015-04-16 15:20:51 -0700104 mWorkerHandler = new Handler(LauncherModel.getWorkerLooper());
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
Sunny Goyal756adbc2015-04-16 15:20:51 -0700391 private Bitmap getNonNullIcon(CacheEntry entry, UserHandleCompat user) {
392 return entry.icon == null ? getDefaultIcon(user) : entry.icon;
393 }
394
Sunny Goyal34b65272015-03-11 16:56:52 -0700395 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800396 * Fill in "application" with the icon and label for "info."
397 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700398 public synchronized void getTitleAndIcon(AppInfo application,
399 LauncherActivityInfoCompat info, boolean useLowResIcon) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700400 UserHandleCompat user = info == null ? application.user : info.getUser();
401 CacheEntry entry = cacheLocked(application.componentName, info, user,
Sunny Goyal34b65272015-03-11 16:56:52 -0700402 false, useLowResIcon);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700403 application.title = entry.title;
Sunny Goyal756adbc2015-04-16 15:20:51 -0700404 application.iconBitmap = getNonNullIcon(entry, user);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700405 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700406 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800407 }
408
Sunny Goyal34b65272015-03-11 16:56:52 -0700409 /**
410 * Returns a high res icon for the given intent and user
411 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700412 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
413 ComponentName component = intent.getComponent();
414 // null info means not installed, but if we have a component from the intent then
415 // we should still look in the cache for restored app icons.
416 if (component == null) {
417 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700419
420 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700421 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700422 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800423 }
424
Sunny Goyal34942622014-08-29 17:20:55 -0700425 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800426 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
427 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700428 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700429 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700430 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700431 ComponentName component = intent.getComponent();
432 // null info means not installed, but if we have a component from the intent then
433 // we should still look in the cache for restored app icons.
434 if (component == null) {
435 shortcutInfo.setIcon(getDefaultIcon(user));
436 shortcutInfo.title = "";
437 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700438 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700439 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800440 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700441 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700442 }
443 }
444
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800445 /**
446 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
447 */
448 public synchronized void getTitleAndIcon(
449 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700450 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
451 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700452 shortcutInfo.setIcon(getNonNullIcon(entry, user));
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800453 shortcutInfo.title = entry.title;
454 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700455 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800456 }
Sunny Goyal34942622014-08-29 17:20:55 -0700457
Sunny Goyald180cf72015-04-06 12:45:40 -0700458 /**
459 * Fill in {@param appInfo} with the icon and label for {@param packageName}
460 */
461 public synchronized void getTitleAndIconForApp(
Hyunyoung Song3f471442015-04-08 19:01:34 -0700462 String packageName, UserHandleCompat user, boolean useLowResIcon,
463 PackageItemInfo infoOut) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700464 CacheEntry entry = getEntryForPackageLocked(packageName, user, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700465 infoOut.iconBitmap = getNonNullIcon(entry, user);
Hyunyoung Song3f471442015-04-08 19:01:34 -0700466 infoOut.title = entry.title;
467 infoOut.usingLowResIcon = entry.isLowResIcon;
468 infoOut.contentDescription = entry.contentDescription;
Sunny Goyald180cf72015-04-06 12:45:40 -0700469 }
470
Sunny Goyal736f5af2014-10-16 14:07:29 -0700471 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100472 if (!mDefaultIcons.containsKey(user)) {
473 mDefaultIcons.put(user, makeDefaultIcon(user));
474 }
475 return mDefaultIcons.get(user);
476 }
477
Kenny Guyed131872014-04-30 03:02:21 +0100478 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
479 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700480 }
481
Sunny Goyal736f5af2014-10-16 14:07:29 -0700482 /**
483 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
484 * This method is not thread safe, it must be called from a synchronized method.
485 */
Kenny Guyed131872014-04-30 03:02:21 +0100486 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700487 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700488 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100489 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700490 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800491 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100492 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500493
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800494 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700495 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800496 if (info != null) {
497 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500498 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700499 if (usePackageIcon) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700500 CacheEntry packageEntry = getEntryForPackageLocked(
501 componentName.getPackageName(), user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700502 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700503 if (DEBUG) Log.d(TAG, "using package default icon for " +
504 componentName.toShortString());
505 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700506 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800507 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700508 }
509 }
510 if (entry.icon == null) {
511 if (DEBUG) Log.d(TAG, "using default icon for " +
512 componentName.toShortString());
513 entry.icon = getDefaultIcon(user);
514 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700515 }
516 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800517
518 if (TextUtils.isEmpty(entry.title) && info != null) {
519 entry.title = info.getLabel().toString();
520 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
521 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800522 }
523 return entry;
524 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400525
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700526 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700527 * Adds a default package entry in the cache. This entry is not persisted and will be removed
528 * when the cache is flushed.
529 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700530 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700531 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800532 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700533
Sunny Goyald180cf72015-04-06 12:45:40 -0700534 CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700535 if (!TextUtils.isEmpty(title)) {
536 entry.title = title;
537 }
538 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700539 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700540 }
541 }
542
543 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700544 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700545 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyald180cf72015-04-06 12:45:40 -0700546 *
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700547 */
Sunny Goyald180cf72015-04-06 12:45:40 -0700548 private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
549 boolean useLowResIcon) {
550 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700551 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700552 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyald180cf72015-04-06 12:45:40 -0700553 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700554 entry = new CacheEntry();
555 mCache.put(cacheKey, entry);
556
Sunny Goyald180cf72015-04-06 12:45:40 -0700557 // Check the DB first.
558 if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
559 try {
560 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
561 ApplicationInfo appInfo = info.applicationInfo;
562 if (appInfo == null) {
563 throw new NameNotFoundException("ApplicationInfo is null");
564 }
565 Drawable drawable = mUserManager.getBadgedDrawableForUser(
566 appInfo.loadIcon(mPackageManager), user);
567 entry.icon = Utilities.createIconBitmap(drawable, mContext);
568 entry.title = appInfo.loadLabel(mPackageManager);
569 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
570 entry.isLowResIcon = false;
571
572 // Add the icon in the DB here, since these do not get written during
573 // package updates.
574 ContentValues values =
575 mIconDb.newContentValues(entry.icon, entry.title.toString());
576 addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
577
578 } catch (NameNotFoundException e) {
579 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
580 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700581 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700582 }
583 return entry;
584 }
585
Chris Wren6d0dde02014-02-10 12:16:54 -0500586 /**
587 * Pre-load an icon into the persistent cache.
588 *
589 * <P>Queries for a component that does not exist in the package manager
590 * will be answered by the persistent cache.
591 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500592 * @param componentName the icon should be returned for this component
593 * @param icon the icon to be persisted
594 * @param dpi the native density of the icon
595 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800596 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
597 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500598 // TODO rescale to the correct native DPI
599 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800600 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500601 packageManager.getActivityIcon(componentName);
602 // component is present on the system already, do nothing
603 return;
604 } catch (PackageManager.NameNotFoundException e) {
605 // pass
606 }
607
Sunny Goyal34b65272015-03-11 16:56:52 -0700608 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800609 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
610 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800611 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
612 SQLiteDatabase.CONFLICT_REPLACE);
613 }
614
Sunny Goyal34b65272015-03-11 16:56:52 -0700615 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
616 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800617 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700618 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
619 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800620 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
621 new String[] {component.flattenToString(),
622 Long.toString(mUserManager.getSerialNumberForUser(user))},
623 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500624 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800625 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700626 entry.icon = loadIconNoResize(c, 0);
627 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800628 entry.title = c.getString(1);
629 if (entry.title == null) {
630 entry.title = "";
631 entry.contentDescription = "";
632 } else {
633 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500634 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800635 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500636 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500637 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800638 c.close();
639 }
640 return false;
641 }
642
Sunny Goyal34b65272015-03-11 16:56:52 -0700643 public static class IconLoadRequest {
644 private final Runnable mRunnable;
645 private final Handler mHandler;
646
647 IconLoadRequest(Runnable runnable, Handler handler) {
648 mRunnable = runnable;
649 mHandler = handler;
650 }
651
652 public void cancel() {
653 mHandler.removeCallbacks(mRunnable);
654 }
655 }
656
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800657 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700658 private final static int DB_VERSION = 3;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800659
660 private final static String TABLE_NAME = "icons";
661 private final static String COLUMN_ROWID = "rowid";
662 private final static String COLUMN_COMPONENT = "componentName";
663 private final static String COLUMN_USER = "profileId";
664 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
665 private final static String COLUMN_VERSION = "version";
666 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700667 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800668 private final static String COLUMN_LABEL = "label";
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700669 private final static String COLUMN_SYSTEM_STATE = "system_state";
670
671 public String mSystemState;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800672
673 public IconDB(Context context) {
674 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700675 updateSystemStateString(context);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800676 }
677
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700678 public void updateSystemStateString(Context c) {
679 mSystemState = Locale.getDefault().toString() + ","
680 + c.getResources().getConfiguration().mcc;
681 }
682
683
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800684 @Override
685 public void onCreate(SQLiteDatabase db) {
686 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
687 COLUMN_COMPONENT + " TEXT NOT NULL, " +
688 COLUMN_USER + " INTEGER NOT NULL, " +
689 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
690 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
691 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700692 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800693 COLUMN_LABEL + " TEXT, " +
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700694 COLUMN_SYSTEM_STATE + " TEXT, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800695 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
696 ");");
697 }
698
699 @Override
700 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
701 if (oldVersion != newVersion) {
702 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500703 }
704 }
705
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800706 @Override
707 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
708 if (oldVersion != newVersion) {
709 clearDB(db);
710 }
Kenny Guyed131872014-04-30 03:02:21 +0100711 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500712
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800713 private void clearDB(SQLiteDatabase db) {
714 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
715 onCreate(db);
716 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700717
718 public ContentValues newContentValues(Bitmap icon, String label) {
719 ContentValues values = new ContentValues();
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700720 values.put(COLUMN_ICON, Utilities.flattenBitmap(icon));
721 values.put(COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700722 Bitmap.createScaledBitmap(icon,
723 icon.getWidth() / LOW_RES_SCALE_FACTOR,
724 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700725 values.put(COLUMN_LABEL, label);
726 values.put(COLUMN_SYSTEM_STATE, mSystemState);
Sunny Goyal34b65272015-03-11 16:56:52 -0700727 return values;
728 }
729 }
730
731 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
732 byte[] data = c.getBlob(iconIndex);
733 try {
734 return BitmapFactory.decodeByteArray(data, 0, data.length);
735 } catch (Exception e) {
736 return null;
737 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500738 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800739}