blob: f4af7f542770db5308c3a952063d3b81c534d35c [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)) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700204 addIconToDBAndMemCache(app, info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800205 }
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 }
Sunny Goyald180cf72015-04-06 12:45:40 -0700303 addIconToDBAndMemCache(app, info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800304 }
305 return updatedPackages;
306 }
307
Sunny Goyald180cf72015-04-06 12:45:40 -0700308 private void addIconToDBAndMemCache(LauncherActivityInfoCompat app, PackageInfo info,
309 long userSerial) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800310 ContentValues values = updateCacheAndGetContentValues(app);
Sunny Goyald180cf72015-04-06 12:45:40 -0700311 addIconToDB(values, app.getComponentName(), info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800312 values.put(IconDB.COLUMN_COMPONENT, app.getComponentName().flattenToString());
Sunny Goyald180cf72015-04-06 12:45:40 -0700313 }
314
315 /**
316 * Updates {@param values} to contain versoning information and adds it to the DB.
317 * @param values {@link ContentValues} containing icon & title
318 */
319 private void addIconToDB(ContentValues values, ComponentName key,
320 PackageInfo info, long userSerial) {
321 values.put(IconDB.COLUMN_COMPONENT, key.flattenToString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800322 values.put(IconDB.COLUMN_USER, userSerial);
323 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
324 values.put(IconDB.COLUMN_VERSION, info.versionCode);
325 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
326 SQLiteDatabase.CONFLICT_REPLACE);
327 }
328
329 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app) {
330 CacheEntry entry = new CacheEntry();
331 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
332 entry.title = app.getLabel();
333 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700334 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800335
Sunny Goyal34b65272015-03-11 16:56:52 -0700336 return mIconDb.newContentValues(entry.icon, entry.title.toString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800337 }
338
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800339 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800340 * Empty out the cache.
341 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700342 public synchronized void flush() {
343 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800344 }
345
346 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700347 * Empty out the cache that aren't of the correct grid size
348 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700349 public synchronized void flushInvalidIcons(DeviceProfile grid) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700350 Iterator<Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator();
Sunny Goyal736f5af2014-10-16 14:07:29 -0700351 while (it.hasNext()) {
352 final CacheEntry e = it.next().getValue();
353 if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
354 || e.icon.getHeight() < grid.iconSizePx)) {
355 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700356 }
357 }
358 }
359
360 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700361 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
362 * @return a request ID that can be used to cancel the request.
363 */
364 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
365 Runnable request = new Runnable() {
366
367 @Override
368 public void run() {
369 if (info instanceof AppInfo) {
370 getTitleAndIcon((AppInfo) info, null, false);
371 } else if (info instanceof ShortcutInfo) {
372 ShortcutInfo st = (ShortcutInfo) info;
373 getTitleAndIcon(st,
374 st.promisedIntent != null ? st.promisedIntent : st.intent,
375 st.user, false);
376 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700377 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700378
379 @Override
380 public void run() {
381 caller.reapplyItemInfo(info);
382 }
383 });
384 }
385 };
386 mWorkerHandler.post(request);
387 return new IconLoadRequest(request, mWorkerHandler);
388 }
389
390 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800391 * Fill in "application" with the icon and label for "info."
392 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700393 public synchronized void getTitleAndIcon(AppInfo application,
394 LauncherActivityInfoCompat info, boolean useLowResIcon) {
395 CacheEntry entry = cacheLocked(application.componentName, info,
396 info == null ? application.user : info.getUser(),
397 false, useLowResIcon);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700398 application.title = entry.title;
399 application.iconBitmap = entry.icon;
400 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700401 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800402 }
403
Sunny Goyal34b65272015-03-11 16:56:52 -0700404 /**
405 * Returns a high res icon for the given intent and user
406 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700407 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
408 ComponentName component = intent.getComponent();
409 // null info means not installed, but if we have a component from the intent then
410 // we should still look in the cache for restored app icons.
411 if (component == null) {
412 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800413 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700414
415 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700416 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700417 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 }
419
Sunny Goyal34942622014-08-29 17:20:55 -0700420 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800421 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
422 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700423 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700424 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700425 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700426 ComponentName component = intent.getComponent();
427 // null info means not installed, but if we have a component from the intent then
428 // we should still look in the cache for restored app icons.
429 if (component == null) {
430 shortcutInfo.setIcon(getDefaultIcon(user));
431 shortcutInfo.title = "";
432 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700433 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700434 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800435 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700436 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700437 }
438 }
439
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800440 /**
441 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
442 */
443 public synchronized void getTitleAndIcon(
444 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700445 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
446 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800447 shortcutInfo.setIcon(entry.icon);
448 shortcutInfo.title = entry.title;
449 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700450 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800451 }
Sunny Goyal34942622014-08-29 17:20:55 -0700452
Sunny Goyald180cf72015-04-06 12:45:40 -0700453 /**
454 * Fill in {@param appInfo} with the icon and label for {@param packageName}
455 */
456 public synchronized void getTitleAndIconForApp(
457 String packageName, UserHandleCompat user, boolean useLowResIcon, AppInfo appInfoOut) {
458 CacheEntry entry = getEntryForPackageLocked(packageName, user, useLowResIcon);
459 appInfoOut.iconBitmap = entry.icon;
460 appInfoOut.title = entry.title;
461 appInfoOut.usingLowResIcon = entry.isLowResIcon;
462 appInfoOut.contentDescription = entry.contentDescription;
463 }
464
Sunny Goyal736f5af2014-10-16 14:07:29 -0700465 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100466 if (!mDefaultIcons.containsKey(user)) {
467 mDefaultIcons.put(user, makeDefaultIcon(user));
468 }
469 return mDefaultIcons.get(user);
470 }
471
Kenny Guyed131872014-04-30 03:02:21 +0100472 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
473 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700474 }
475
Sunny Goyal736f5af2014-10-16 14:07:29 -0700476 /**
477 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
478 * This method is not thread safe, it must be called from a synchronized method.
479 */
Kenny Guyed131872014-04-30 03:02:21 +0100480 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700481 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700482 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100483 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700484 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800485 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100486 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500487
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800488 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700489 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800490 if (info != null) {
491 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500492 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700493 if (usePackageIcon) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700494 CacheEntry packageEntry = getEntryForPackageLocked(
495 componentName.getPackageName(), user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700496 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700497 if (DEBUG) Log.d(TAG, "using package default icon for " +
498 componentName.toShortString());
499 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700500 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800501 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700502 }
503 }
504 if (entry.icon == null) {
505 if (DEBUG) Log.d(TAG, "using default icon for " +
506 componentName.toShortString());
507 entry.icon = getDefaultIcon(user);
508 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700509 }
510 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800511
512 if (TextUtils.isEmpty(entry.title) && info != null) {
513 entry.title = info.getLabel().toString();
514 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
515 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800516 }
517 return entry;
518 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400519
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700520 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700521 * Adds a default package entry in the cache. This entry is not persisted and will be removed
522 * when the cache is flushed.
523 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700524 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700525 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800526 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700527
Sunny Goyald180cf72015-04-06 12:45:40 -0700528 CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700529 if (!TextUtils.isEmpty(title)) {
530 entry.title = title;
531 }
532 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700533 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700534 }
535 }
536
537 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700538 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700539 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyald180cf72015-04-06 12:45:40 -0700540 *
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700541 */
Sunny Goyald180cf72015-04-06 12:45:40 -0700542 private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
543 boolean useLowResIcon) {
544 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700545 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700546 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyald180cf72015-04-06 12:45:40 -0700547 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700548 entry = new CacheEntry();
549 mCache.put(cacheKey, entry);
550
Sunny Goyald180cf72015-04-06 12:45:40 -0700551 // Check the DB first.
552 if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
553 try {
554 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
555 ApplicationInfo appInfo = info.applicationInfo;
556 if (appInfo == null) {
557 throw new NameNotFoundException("ApplicationInfo is null");
558 }
559 Drawable drawable = mUserManager.getBadgedDrawableForUser(
560 appInfo.loadIcon(mPackageManager), user);
561 entry.icon = Utilities.createIconBitmap(drawable, mContext);
562 entry.title = appInfo.loadLabel(mPackageManager);
563 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
564 entry.isLowResIcon = false;
565
566 // Add the icon in the DB here, since these do not get written during
567 // package updates.
568 ContentValues values =
569 mIconDb.newContentValues(entry.icon, entry.title.toString());
570 addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
571
572 } catch (NameNotFoundException e) {
573 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
574 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700575 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700576 }
577 return entry;
578 }
579
Chris Wren6d0dde02014-02-10 12:16:54 -0500580 /**
581 * Pre-load an icon into the persistent cache.
582 *
583 * <P>Queries for a component that does not exist in the package manager
584 * will be answered by the persistent cache.
585 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500586 * @param componentName the icon should be returned for this component
587 * @param icon the icon to be persisted
588 * @param dpi the native density of the icon
589 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800590 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
591 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500592 // TODO rescale to the correct native DPI
593 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800594 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500595 packageManager.getActivityIcon(componentName);
596 // component is present on the system already, do nothing
597 return;
598 } catch (PackageManager.NameNotFoundException e) {
599 // pass
600 }
601
Sunny Goyal34b65272015-03-11 16:56:52 -0700602 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800603 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
604 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800605 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
606 SQLiteDatabase.CONFLICT_REPLACE);
607 }
608
Sunny Goyal34b65272015-03-11 16:56:52 -0700609 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
610 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800611 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700612 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
613 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800614 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
615 new String[] {component.flattenToString(),
616 Long.toString(mUserManager.getSerialNumberForUser(user))},
617 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500618 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800619 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700620 entry.icon = loadIconNoResize(c, 0);
621 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800622 entry.title = c.getString(1);
623 if (entry.title == null) {
624 entry.title = "";
625 entry.contentDescription = "";
626 } else {
627 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500628 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800629 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500630 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500631 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800632 c.close();
633 }
634 return false;
635 }
636
Sunny Goyal34b65272015-03-11 16:56:52 -0700637 public static class IconLoadRequest {
638 private final Runnable mRunnable;
639 private final Handler mHandler;
640
641 IconLoadRequest(Runnable runnable, Handler handler) {
642 mRunnable = runnable;
643 mHandler = handler;
644 }
645
646 public void cancel() {
647 mHandler.removeCallbacks(mRunnable);
648 }
649 }
650
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800651 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700652 private final static int DB_VERSION = 3;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800653
654 private final static String TABLE_NAME = "icons";
655 private final static String COLUMN_ROWID = "rowid";
656 private final static String COLUMN_COMPONENT = "componentName";
657 private final static String COLUMN_USER = "profileId";
658 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
659 private final static String COLUMN_VERSION = "version";
660 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700661 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800662 private final static String COLUMN_LABEL = "label";
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700663 private final static String COLUMN_SYSTEM_STATE = "system_state";
664
665 public String mSystemState;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800666
667 public IconDB(Context context) {
668 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700669 updateSystemStateString(context);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800670 }
671
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700672 public void updateSystemStateString(Context c) {
673 mSystemState = Locale.getDefault().toString() + ","
674 + c.getResources().getConfiguration().mcc;
675 }
676
677
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800678 @Override
679 public void onCreate(SQLiteDatabase db) {
680 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
681 COLUMN_COMPONENT + " TEXT NOT NULL, " +
682 COLUMN_USER + " INTEGER NOT NULL, " +
683 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
684 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
685 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700686 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800687 COLUMN_LABEL + " TEXT, " +
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700688 COLUMN_SYSTEM_STATE + " TEXT, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800689 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
690 ");");
691 }
692
693 @Override
694 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
695 if (oldVersion != newVersion) {
696 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500697 }
698 }
699
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800700 @Override
701 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
702 if (oldVersion != newVersion) {
703 clearDB(db);
704 }
Kenny Guyed131872014-04-30 03:02:21 +0100705 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500706
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800707 private void clearDB(SQLiteDatabase db) {
708 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
709 onCreate(db);
710 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700711
712 public ContentValues newContentValues(Bitmap icon, String label) {
713 ContentValues values = new ContentValues();
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700714 values.put(COLUMN_ICON, Utilities.flattenBitmap(icon));
715 values.put(COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700716 Bitmap.createScaledBitmap(icon,
717 icon.getWidth() / LOW_RES_SCALE_FACTOR,
718 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700719 values.put(COLUMN_LABEL, label);
720 values.put(COLUMN_SYSTEM_STATE, mSystemState);
Sunny Goyal34b65272015-03-11 16:56:52 -0700721 return values;
722 }
723 }
724
725 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
726 byte[] data = c.getBlob(iconIndex);
727 try {
728 return BitmapFactory.decodeByteArray(data, 0, data.length);
729 } catch (Exception e) {
730 return null;
731 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500732 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800733}